/** *######################################################################### * * A component of the Gatherer application, part of the Greenstone digital * library suite from the New Zealand Digital Library Project at the * University of Waikato, New Zealand. * * Author: John Thompson, Greenstone Digital Library, University of Waikato * * Copyright (C) 1999 New Zealand Digital Library Project * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *######################################################################## */ package org.greenstone.gatherer.gems; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.util.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.border.*; import org.greenstone.gatherer.Configuration; import org.greenstone.gatherer.Dictionary; import org.greenstone.gatherer.Gatherer; import org.greenstone.gatherer.cdm.LanguageManager; import org.greenstone.gatherer.gui.*; import org.greenstone.gatherer.util.StaticStrings; import org.w3c.dom.*; public class GEMSPreferences extends ModalDialog { static final public String CONNECTION_PREFS = "connection"; static final public String GENERAL_PREFS = "general"; static final private Dimension LABEL_SIZE = new Dimension(280, 25); static final private Dimension SIZE = new Dimension(640, 345); private JButton apply_button; private JButton cancel_button; private JButton ok_button; private JComboBox language_combobox; private JList language_limited_jlist; private JList language_code_jlist; private JLabel interface_language_label; private JLabel language_label; private JTabbedPane tab_pane; private GEMSPreferences self; public GEMSPreferences() { // Initialize super(Gatherer.g_man, true); this.self = this; setSize(SIZE); Dictionary.registerText(this, "Preferences"); setJMenuBar(new SimpleMenuBar("preferences")); // Creation JPanel content_pane = (JPanel) getContentPane(); tab_pane = new JTabbedPane(); JPanel general_preferences = createGeneralPreferences(); tab_pane.add("Preferences.General", general_preferences); Dictionary.register(tab_pane); JPanel button_pane = new JPanel(); ok_button = new GLIButton(); ok_button.setMnemonic(KeyEvent.VK_O); Dictionary.registerBoth(ok_button, "General.OK", "General.OK_Tooltip"); apply_button = new GLIButton(); apply_button.setMnemonic(KeyEvent.VK_A); Dictionary.registerBoth(apply_button, "General.Apply", "General.Apply_Tooltip"); cancel_button = new GLIButton(); cancel_button.setMnemonic(KeyEvent.VK_C); Dictionary.registerBoth(cancel_button, "General.Cancel", "General.Cancel_Tooltip"); // Connection ok_button.addActionListener(new OKButtonListener(true)); apply_button.addActionListener(new OKButtonListener(false)); cancel_button.addActionListener(new CancelButtonListener()); // Layout button_pane.setBorder(org.greenstone.gatherer.gui.BorderFactory.createEmptyBorder(5,2,2,2)); button_pane.setLayout(new GridLayout(1,3,0,5)); button_pane.add(ok_button); button_pane.add(apply_button); button_pane.add(cancel_button); content_pane.setLayout(new BorderLayout()); content_pane.add(tab_pane, BorderLayout.CENTER); content_pane.add(button_pane, BorderLayout.SOUTH); setLocation(400, 300); tab_pane.setSelectedComponent(general_preferences); // Clean up general_preferences = null; cancel_button = null; ok_button = null; button_pane = null; tab_pane = null; content_pane = null; setVisible(true); } private JPanel createGeneralPreferences() { JPanel general_pane = new JPanel(); // Build the model of available languages ArrayList dictionary_model = new ArrayList(); // The new method makes use of the successor to the languages.dat file, classes/xml/languages.xml // Should update to also give the full name of the language --Matthew NodeList language_elements = LanguageManager.LANGUAGES_DOCUMENT.getDocumentElement().getElementsByTagName(StaticStrings.LANGUAGE_ELEMENT); for(int i = 0; i < language_elements.getLength(); i++) { Element language_element = (Element) language_elements.item(i); if((language_element.getAttribute(StaticStrings.GLI_ATTRIBUTE)).equalsIgnoreCase(StaticStrings.TRUE_STR) || (language_element.getAttribute(StaticStrings.MDS_ATTRIBUTE)).equalsIgnoreCase(StaticStrings.TRUE_STR)) { Locale locale = new Locale(language_element.getAttribute(StaticStrings.CODE_ATTRIBUTE)); String description = language_element.getAttribute(StaticStrings.NAME_ATTRIBUTE); DictionaryEntry entry = new DictionaryEntry(description, locale); if(!dictionary_model.contains(entry)) { dictionary_model.add(entry); } entry = null; description = null; locale = null; } language_element = null; } language_elements = null; // Users email JPanel lang_limited_pane = new JPanel(); interface_language_label = new JLabel(); interface_language_label.setPreferredSize(LABEL_SIZE); Dictionary.registerText(interface_language_label, "GEMS.Preferences.Selected_Languages_Tooltip"); // Language JPanel language_pane = new JPanel(); language_label = new JLabel(); language_label.setPreferredSize(LABEL_SIZE); Dictionary.registerText(language_label, "Preferences.General.Interface_Language"); language_combobox = new JComboBox(dictionary_model.toArray()); Dictionary.registerTooltip(language_combobox, "Preferences.General.Interface_Language_Tooltip"); // Try to locate and select the current language String language_code = Configuration.getLanguage(); for(int b = 0; b < language_combobox.getItemCount(); b++) { DictionaryEntry entry = (DictionaryEntry) language_combobox.getItemAt(b); if(language_code.equalsIgnoreCase(entry.getLocale().getLanguage())) { language_combobox.setSelectedIndex(b); } } //create language selected box, and high light the apprpropate ones int [] selectedLanguages = new int[300]; int selectedLanguagesLength = 0; GEMSLanguageManager gemsLangManager = new GEMSLanguageManager(Configuration.gsdl_path + File.separator + "gli" + File.separator + "classes" + File.separator + "xml" + File.separator + "languages.xml"); //Create the visible JList, and then another one with just the language codes. language_code_jlist = new JList(gemsLangManager.getLanguageCodesToArray()); language_limited_jlist = new JList(gemsLangManager.getLanguageCodesAndNames()); //Set the list up to display tabs properly TabListCellRenderer renderer = new TabListCellRenderer(); renderer.setTabs(new int[] {50, 200, 300}); language_limited_jlist.setCellRenderer(renderer); String enabled_languages = Configuration.getString("GEMS.Preferences.Selected_Languages", true); //we have a string: enabled_languages that contains an array of strings delimited by comma's String []enabled_languages_split = enabled_languages.split(","); Object []language_codes_array = gemsLangManager.getLanguageCodesToArray(); //for each enabled_languages_split, check which languages match, and store the index in selectedLanguages for(int k = 0; k < enabled_languages_split.length; k++) { for(int p =0; p < language_codes_array.length; p++){ //if the codes match, then include the index p into the vector selectedLanguages if (language_codes_array[p].toString().toLowerCase().trim().compareTo(enabled_languages_split[k].toLowerCase().trim()) == 0) { selectedLanguages[selectedLanguagesLength] = p; selectedLanguagesLength++; language_limited_jlist.addSelectionInterval(p,p); } } } // Connect language_combobox.addActionListener(new LanguageComboboxListener()); // Layout JScrollPane langScrollPane = new JScrollPane(language_limited_jlist); langScrollPane.setPreferredSize(new Dimension(100, 100)); lang_limited_pane.setLayout(new GridLayout(1,3)); lang_limited_pane.add(interface_language_label); lang_limited_pane.add(langScrollPane); language_pane.setLayout(new BorderLayout()); language_pane.add(language_label, BorderLayout.WEST); language_pane.add(language_combobox, BorderLayout.CENTER); general_pane.setBorder(org.greenstone.gatherer.gui.BorderFactory.createEmptyBorder(5,5,5,5)); general_pane.setLayout(null); general_pane.add(lang_limited_pane); general_pane.add(language_pane); //general_pane.add(view_extracted_metadata_checkbox); Insets general_pane_insets = general_pane.getInsets(); lang_limited_pane.setBounds(20+general_pane_insets.left, 20+general_pane_insets.top, lang_limited_pane.getPreferredSize().width, lang_limited_pane.getPreferredSize().height); language_pane.setBounds(20+general_pane_insets.left, 130+general_pane_insets.top, language_pane.getPreferredSize().width, language_pane.getPreferredSize().height); return general_pane; } private class OKButtonListener implements ActionListener { private boolean close; public OKButtonListener(boolean close) { this.close = close; } public void actionPerformed(ActionEvent event) { // Submit the various changes //save interface language String current_lang = Configuration.getLanguage(); String new_lang = ((DictionaryEntry)language_combobox.getSelectedItem()).getLocale().getLanguage(); if (!current_lang.equals(new_lang)) { Configuration.setLocale("general.locale", Configuration.GENERAL_SETTING, ((DictionaryEntry)language_combobox.getSelectedItem()).getLocale()); } //Set language_code_jlist to have the same selections as the language_limited_jlist. //(before, there was only one JList, and it only contained the language codes). int[] selection = language_limited_jlist.getSelectedIndices(); language_code_jlist.setSelectedIndices(selection); Object[] selectedValues = language_code_jlist.getSelectedValues(); String concatString = new String(); for(int k = 0; k < selectedValues.length; k++) { if(selectedValues.length-k > 1) concatString = concatString.concat(selectedValues[k].toString() +","); else concatString = concatString.concat(selectedValues[k].toString()); } Configuration.setString("GEMS.Preferences.Selected_Languages",true, concatString); // Always save configuration changes immediately (in case the GLI crashes) Configuration.save(); // Hide dialog if(close) { self.dispose(); } } } private class CancelButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { self.dispose(); } } private class DictionaryEntry implements Comparable { private Locale locale; private String description; public DictionaryEntry(Locale locale) { this.description = null; this.locale = locale; } public DictionaryEntry(String description, Locale locale) { this.description = description; this.locale = locale; } public int compareTo(Object object) { return toString().compareTo(object.toString()); } public boolean equals(Object object) { return toString().equals(object.toString()); } public Locale getLocale() { return locale; } public String toString() { if(description != null) { return description; } else { return locale.getDisplayName(); } } } private class LanguageComboboxListener implements ActionListener { public void actionPerformed(ActionEvent event) { // Retrieve the entry DictionaryEntry entry = (DictionaryEntry) language_combobox.getSelectedItem(); if(entry != null) { //Gatherer.dictionary.changeDictionary(entry.getLocale()); // Display message JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("Preferences.General.Restart_Required"), Dictionary.get("General.Warning"), JOptionPane.WARNING_MESSAGE); } } } } /** A custom list renderer, to display tabs in the JList properly. @see http://manning.com/sbe/files/uts2/Chapter10html/Chapter10.htm Added by Matthew Whyte Date last modified: 1/02/05 */ class TabListCellRenderer extends JLabel implements ListCellRenderer { protected static Border m_noFocusBorder; protected FontMetrics m_fm = null; protected Insets m_insets = new Insets(0, 0, 0, 0); protected int m_defaultTab = 50; protected int[] m_tabs = null; public TabListCellRenderer() { super(); m_noFocusBorder = new EmptyBorder(1, 1, 1, 1); setOpaque(true); setBorder(m_noFocusBorder); } public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { setText(value.toString()); setBackground(isSelected ? list.getSelectionBackground() : list.getBackground()); setForeground(isSelected ? list.getSelectionForeground() : list.getForeground()); setFont(list.getFont()); setBorder((cellHasFocus) ? UIManager.getBorder("List.focusCellHighlightBorder") : m_noFocusBorder); return this; } public void setDefaultTab(int defaultTab) { m_defaultTab = defaultTab; } public int getDefaultTab() { return m_defaultTab; } public void setTabs(int[] tabs) { m_tabs = tabs; } public int[] getTabs() { return m_tabs; } public int getTab(int index) { if (m_tabs == null) return m_defaultTab*index; int len = m_tabs.length; if (index >= 0 && index < len) return m_tabs[index]; return m_tabs[len-1] + m_defaultTab*(index-len+1); } public void paint(Graphics g) { m_fm = g.getFontMetrics(); g.setColor(getBackground()); g.fillRect(0, 0, getWidth(), getHeight()); getBorder().paintBorder(this, g, 0, 0, getWidth(), getHeight()); g.setColor(getForeground()); g.setFont(getFont()); m_insets = getInsets(); int x = m_insets.left; int y = m_insets.top + m_fm.getAscent(); StringTokenizer st = new StringTokenizer(getText(), "\t"); while (st.hasMoreTokens()) { String sNext = st.nextToken(); g.drawString(sNext, x, y); x += m_fm.stringWidth(sNext); if (!st.hasMoreTokens()) break; int index = 0; while (x >= getTab(index)) index++; x = getTab(index); } } }