/** *######################################################################### * * 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 { /** The font used when displaying various html text. */ static private FontUIResource font = null; /** A reference to remind us of the current locale. */ static private Locale locale = null; /** The ResourceBundle which contains the raw key-value mappings. Loaded from a file named "dictionarylocale.properties. */ static private ResourceBundle dictionary = null; public Dictionary(Locale locale, FontUIResource font) { super(); // Initialize this.font = font; this.locale = ((locale == null) ? Locale.getDefault() : locale); this.dictionary = ResourceBundle.getBundle(Utility.DICTIONARY, this.locale); } /** 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. */ static public String getLanguage() { return locale.getLanguage(); } static public String get(String key) { return get(key, (String[]) null); } static public String get(String key, String arg) { String[] args = new String[1]; args[0] = arg; return get(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. */ static public String get(String key, String[] args) { try { String initial_raw = dictionary.getString(key); // Convert into Unicode String initial = ""; try { // This "ISO-8859-1" looks out of place, but it is very important. // It is essential to call getBytes with an 8-bit encoding, otherwise // Java kindly deems some characters "undisplayable", and replaces // them with question marks. This is NOT good. initial = new String(initial_raw.getBytes("ISO-8859-1"), "UTF-8"); } catch (Exception ex) { System.err.println("Exception: " + ex); ex.printStackTrace(); return initial_raw; } // 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 + get("Farg" + arg_num, f_subargs); } } complete = complete + initial; return complete; } catch (Exception e) { System.err.println("Missing value for key: " + key); // DebugStream.printStackTrace(e); return key; } } static public void registerBoth(Component component, String text_key, String tooltip_key) { setText(component, text_key); setTooltip(component, tooltip_key); } static public void registerText(Component component, String text_key) { setText(component, text_key); } static public void registerText(Component component, String text_key, String[] args) { setText(component, text_key, args); } static public void registerTooltip(Component component, String tooltip_key) { setTooltip(component, tooltip_key); } static public void registerTooltipText(Component component, String tooltip) { setTooltipText(component, tooltip); } 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 SwingUtilities.invokeLater(new ComponentUpdateTask(component, get(text_key, args), null)); } } static public void setTooltip(Component component, String tooltip_key) { if (component != null) { // Update the component using the AWTEvent queue SwingUtilities.invokeLater(new ComponentUpdateTask(component, null, get(tooltip_key))); } } static public void setTooltipText(Component component, String tooltip) { if (component != null) { // Update the component using the AWTEvent queue SwingUtilities.invokeLater(new ComponentUpdateTask(component, null, tooltip)); } } static private class ComponentUpdateTask implements Runnable { private Component component = null; private String text = null; private String tooltip = null; public ComponentUpdateTask(Component component, String text, String tooltip) { this.component = component; this.text = text; this.tooltip = tooltip; } 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 = get(args[i], (String[]) null); String tooltip = get(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); } } }