/** *######################################################################### * * 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; import java.awt.*; import java.io.*; import java.util.*; import javax.swing.*; import javax.swing.plaf.*; import javax.swing.text.*; import javax.swing.tree.*; import org.greenstone.gatherer.util.Utility; /** Extends the ResourceBundle class to allow for the automatic insertion of arguments. Note that the key names beginning Farg are reserved for formatting.
*
* Property files usable by this class have the Backus-Naur form:
*
* FileLine ::= Comment | Mapping
* Comment ::= '#' SString
* Mapping ::= NZString ':' SString ( Argument SString )*
* NZString ::= ( Char | Int ) SString
* Argument ::= '{' Int '}'
* SString ::= String . ['"','#',...] -> ['\"','\#',...]
*
* In order to add a new dictionary Locale, simply copy the existing dictionary.properties files, replace the values (Strings after the ':') with the new language specific ones being careful to maintain formatting and Gatherer placed arguments, then save the new dictionary as:
*
* dictionary_locale.properties
*
* where locale is made of two two-letter codes seperated by an underscore. The first code is in lower-case and defines the language. The second is in upper-case and defines the country. For example the default dictionary could also correctly be called: *
* dictionary_en_NZ.properties
* @author John Thompson, Greenstone Digital Library, University of Waikato * @version 2.3 */ public class Dictionary extends HashMap { static final private boolean KEY_LIST_DEBUG = false; static final private String KEY_LIST_FILENAME = "keylist.txt"; /** A String which more explicitly states the Locale of this dictionary. */ public String language = null; /** A static reference to ourself. */ static public Dictionary self; /** The font used when displaying various html text. */ private FontUIResource font = null; /** A reference to remind us of the current locale. */ private Locale locale = null; /** The ResourceBundle which contains the raw key-value mappings. Loaded from a file named "dictionarylocale.properties*/ private ResourceBundle dictionary = null; private TreeSet key_list = null;; /** Constructs the Dictionary class by first checking if a Locale has been set. If not the default locale is used, and a ResourceBundle is created. Finally a single important String, Language, is made available outside the class so a more read-able version of the Locale of this Dictionary is present. * @param locale The Locale used to load the desired dictionary resource bundle. */ public Dictionary(Locale locale, FontUIResource font) { super(); this.self = this; if (KEY_LIST_DEBUG) { // Reload the keylist File file = new File(KEY_LIST_FILENAME); key_list = new TreeSet(); try { BufferedReader br = new BufferedReader(new FileReader(file)); String line; while((line = br.readLine()) != null) { key_list.add(line); } br.close(); br = null; } catch(Exception error) { error.printStackTrace(); } } // Initialize. this.font = font; if (locale == null) { this.locale = Locale.getDefault(); } else { this.locale = locale; Locale.setDefault(locale); } dictionary = ResourceBundle.getBundle(Utility.DICTIONARY, this.locale); // Now quickly read in language name. language = dictionary.getString("Language"); } /** Change the currently loaded dictionary and update registered components */ public void changeDictionary(Locale locale) { this.locale = locale; // Load new dictionary dictionary = ResourceBundle.getBundle(Utility.DICTIONARY, locale); language = dictionary.getString("Language"); Gatherer.println("Loaded new dictionary: " + language); // Refresh all registered component // !! TO FINISH !! Gatherer.println("Updating components..."); // ArrayList temp_list = new ArrayList(); // Iterator components = keySet().iterator(); // while (components.hasNext()) { // Component component = (Component) components.next(); // temp_list.add(component); // } // for (int i = 0; i < temp_list.size(); i++) { // Component component = (Component) temp_list.get(i); // ComponentDetails details = (ComponentDetails) get(component); // oldSetBoth(component, details.text_key, details.text_args, details.tooltip_key); // } // for (Iterator keys = keySet().iterator(); keys.hasNext(); ) { // Component component = (Component) keys.next(); // ComponentDetails details = (ComponentDetails) get(component); // oldSetBoth(component, details.text_key, details.text_args, details.tooltip_key); // } } /** Change the currently loaded dictionary and update registered (ie dynamic) components as possible. */ // public void oldChangeDictionary(Locale locale) { // this.locale = locale; // // Load new dictionary // dictionary = ResourceBundle.getBundle(Utility.DICTIONARY, locale); // language = dictionary.getString("Language"); // Gatherer.println("Having loaded new dictionary: " + language); // // Refresh all registered component // Gatherer.println("Updating components"); // for(Iterator keys = keySet().iterator(); keys.hasNext(); ) { // Object component = keys.next(); // String[] args = (String[]) get(component); // if(component instanceof AbstractButton) { // register((AbstractButton)component, args, true); // } // else if(component instanceof JComboBox) { // register((JComboBox)component, args, true); // } // else if(component instanceof JDialog) { // register((JDialog)component, args, true); // } // else if(component instanceof JFrame) { // register((JFrame)component, args, true); // } // else if(component instanceof JLabel) { // register((JLabel)component, args, true); // } // else if(component instanceof JTabbedPane) { // register((JTabbedPane)component, args, true); // } // else if(component instanceof JTextComponent) { // register((JTextComponent)component, args, true); // } // else if(component instanceof JTree) { // register((JTree)component, args, true); // } // else if(component instanceof TitledBorder) { // register((TitledBorder)component, args, true); // } // args = null; // component = null; // } // } /** Remove the component from our registered components list. */ /* private void deregister(Object component) { remove(component); } */ public void destroy() { if(key_list != null) { try { FileOutputStream fos = new FileOutputStream(KEY_LIST_FILENAME); for(Iterator iter = key_list.iterator(); iter.hasNext(); ) { String value = (String) iter.next(); fos.write(value.getBytes()); fos.write('\n'); value = null; } fos.close(); fos = null; } catch(Exception error) { error.printStackTrace(); } } } static public String get(String key) { return self.oldget(key); } static public String get(String key, String arg) { String[] args = new String[1]; args[0] = arg; return self.oldget(key, args); } static public String get(String key, String[] args) { return self.oldget(key, args); } /** * @deprecated * Overloaded to call get with both a key and an empty argument array. * @param key A String which is mapped to a initial String within the ResourceBundle. * @return A String which has been referenced by the key String and that either contains no argument fields, or has had the argument fields automatiically populated with formatting Strings of with argument String provided in the get call. */ public String oldget(String key) { return oldget(key, (String[])null); } /** * @deprecated * Convienence method with transforms the second string argument into a string array. */ public String oldget(String key, String arg) { String[] args = new String[1]; args[0] = arg; return oldget(key, args); } /** Used to retrieve a property value from the Locale specific ResourceBundle, based upon the key and arguments supplied. If the key cannot be found or if some other part of the call fails a default (English) error message is returned.
* Here the get recieves a second argument which is an array of Strings used to populate argument fields, denoted {n}, within the value String returned. Note that argument numbers greater than or equal to 32 are automatically mapped to the formatting String named Fargn. * @param key A String which is mapped to a initial String within the ResourceBundle. * @param args A String[] used to populate argument fields within the complete String. * @return A String which has been referenced by the key String and that either contains no argument fields, or has had the argument fields automatiically populated with formatting Strings of with argument String provided in the get call. */ public String oldget(String key, String args[]) { if(key_list != null) { synchronized(this) { key_list.add(key); } } try { String initial = dictionary.getString(key); // If the string contains arguments we have to insert them. String complete = ""; // While we still have initial string left. while(initial.length() > 0 && initial.indexOf('{') != -1 && initial.indexOf('}') != -1) { // Remove preamble int opening = initial.indexOf('{'); int closing = initial.indexOf('}'); int comment_mark = initial.indexOf('-', opening); // May not exist if(comment_mark > closing) { // May also be detecting a later comment comment_mark = -1; } complete = complete + initial.substring(0, opening); // Parse arg_num String arg_str = null; if(comment_mark != -1) { arg_str = initial.substring(opening + 1, comment_mark); } else { arg_str = initial.substring(opening + 1, closing); } int arg_num = Integer.parseInt(arg_str); if(closing + 1 < initial.length()) { initial = initial.substring(closing + 1); } else { initial = ""; } // Insert argument if(args != null && 0 <= arg_num && arg_num < args.length) { complete = complete + args[arg_num]; } else if(arg_num >= 32) { String f_subargs[] = new String[1]; if(font != null) { f_subargs[0] = font.getFontName(); } else { f_subargs[0] = "Arial"; } complete = complete + oldget("Farg" + arg_num, f_subargs); } } return complete + initial; } catch (Exception e) { System.err.println("Missing value for key: " + key); Gatherer.printStackTrace(e); return key; } } /** Retrieve the two letter code of the current language we are using, according to the stored locale. * @return A String containing the two letter ISO639 language code. */ public String getLanguage() { return locale.getLanguage(); } static public void setBoth(Component component, String text_key, String tooltip_key) { setText(component, text_key); setTooltip(component, tooltip_key); } static public void setText(Component component, String text_key) { setText(component, text_key, null); } static public void setText(Component component, String text_key, String[] args) { if (component != null) { // Update the component using the AWTEvent queue ComponentDetails details = new ComponentDetails(true, text_key, args, false, null); ComponentUpdateTask task = new ComponentUpdateTask(component, details, true); SwingUtilities.invokeLater(task); } } static public void setTooltip(Component component, String tooltip_key) { if (component != null) { // Update the component using the AWTEvent queue ComponentDetails details = new ComponentDetails(false, null, null, true, tooltip_key); ComponentUpdateTask task = new ComponentUpdateTask(component, details, true); SwingUtilities.invokeLater(task); } } static public void registerBoth(Component component, String text_key, String tooltip_key) { registerText(component, text_key, null); registerTooltip(component, tooltip_key); } static public void registerText(Component component, String text_key) { registerText(component, text_key, null); } static public void registerText(Component component, String text_key, String[] args) { if (component != null) { // Update the component's details ComponentDetails details = (ComponentDetails) self.remove(component); if (details == null) { details = new ComponentDetails(true, text_key, args, false, null); } else { details.has_text = true; details.text_key = text_key; details.text_args = args; } self.put(component, details); // Update the component using the AWTEvent queue ComponentUpdateTask task = new ComponentUpdateTask(component, details, true); SwingUtilities.invokeLater(task); } } static public void registerTooltip(Component component, String tooltip_key) { self.registerTooltip(component, tooltip_key, true); } static public void registerTooltip(Component component, String tooltip_key, boolean is_key) { if (component != null) { // Update the component's details ComponentDetails details = (ComponentDetails) self.remove(component); if (details == null) { details = new ComponentDetails(false, null, null, true, tooltip_key); } else { details.has_tooltip = true; details.tooltip_key = tooltip_key; } self.put(component, details); // Update the component using the AWTEvent queue ComponentUpdateTask task = new ComponentUpdateTask(component, details, is_key); SwingUtilities.invokeLater(task); } } static public void unregister(Component component) { self.remove(component); } static private class ComponentDetails { public boolean has_text; public String text_key; public String[] text_args; public boolean has_tooltip; public String tooltip_key; public ComponentDetails(boolean has_text, String text_key, String[] text_args, boolean has_tooltip, String tooltip_key) { this.has_text = has_text; this.text_key = text_key; this.text_args = text_args; this.has_tooltip = has_tooltip; this.tooltip_key = tooltip_key; } } static private class ComponentUpdateTask implements Runnable { private Component component; private String text = null; private String tooltip = null; public ComponentUpdateTask(Component component, ComponentDetails details, boolean is_key) { this.component = component; if (details.has_text) { text = self.oldget(details.text_key, details.text_args); } if (details.has_tooltip) { if(is_key) { tooltip = self.oldget(details.tooltip_key, (String[]) null); } else { tooltip = details.tooltip_key; } } } public void run() { // If the component has text if (text != null) { if (component instanceof AbstractButton) { ((AbstractButton) component).setText(text); } else if (component instanceof Frame) { ((Frame) component).setTitle(text); } else if (component instanceof JDialog) { ((JDialog) component).setTitle(text); } else if (component instanceof JLabel) { ((JLabel) component).setText(text); } else if (component instanceof JProgressBar) { ((JProgressBar) component).setString(text); } else if (component instanceof JTextComponent) { ((JTextComponent) component).setText(text); ((JTextComponent) component).setCaretPosition(0); } else { System.err.println("Unhandled component: " + component.getClass()); } } // If the component has a tooltip if (tooltip != null) { if (component instanceof JComponent) { ((JComponent) component).setToolTipText(tooltip); } } } } /** * Register a tab pane component. This will be deprecated eventually. */ static public void register(JTabbedPane component) { if (component != null) { String[] args = new String[component.getTabCount()]; // Iterate through the tabbed panes tabs, updating values and recording the original key of each item in args. for (int i = 0; i < args.length; i++) { if (args[i] == null) { args[i] = component.getTitleAt(i); } String value = self.oldget(args[i], (String[]) null); String tooltip = self.oldget(args[i] + "_Tooltip", (String[]) null); JTabbedPaneChangeTask task = new JTabbedPaneChangeTask(component, i, value, tooltip); SwingUtilities.invokeLater(task); } } } /** Updates a tabbed panes tab title and tooltip. */ static private class JTabbedPaneChangeTask implements Runnable { private JTabbedPane component; private int index; private String value; private String tooltip; public JTabbedPaneChangeTask(JTabbedPane component, int index, String value, String tooltip) { this.component = component; this.index = index; this.value = value; this.tooltip = tooltip; } public void run() { component.setTitleAt(index, value); component.setToolTipTextAt(index, tooltip); } } }