source: trunk/gli/src/org/greenstone/gatherer/Dictionary.java@ 10610

Last change on this file since 10610 was 10345, checked in by mdewsnip, 19 years ago

Removed some more crap out of the Utility class.

  • Property svn:keywords set to Author Date Id Revision
File size: 11.0 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * <BR><BR>
9 *
10 * Author: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 New Zealand Digital Library Project
15 *
16 * <BR><BR>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * <BR><BR>
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * <BR><BR>
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *########################################################################
36 */
37package org.greenstone.gatherer;
38
39import java.awt.*;
40import java.io.*;
41import java.util.*;
42import javax.swing.*;
43import javax.swing.plaf.*;
44import javax.swing.text.*;
45import javax.swing.tree.*;
46
47/** Extends the ResourceBundle class to allow for the automatic insertion of arguments.<BR>
48 * <BR>
49 * Property files usable by this class have the Backus-Naur form: <BR>
50 * <BR>
51 * FileLine ::= Comment | Mapping <BR>
52 * Comment ::= '#' SString <BR>
53 * Mapping ::= NZString ':' SString ( Argument SString )* <BR>
54 * NZString ::= ( Char | Int ) SString <BR>
55 * Argument ::= '{' Int '}' <BR>
56 * SString ::= String . ['"','#',...] -> ['\"','\#',...] <BR>
57 * <BR>
58 * 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: <br>
59 * <BR>
60 * dictionary_<I>locale</I>.properties<BR>
61 * <BR>
62 * 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:
63 * <BR>
64 * dictionary_en_NZ.properties<BR>
65 * @author John Thompson, Greenstone Digital Library, University of Waikato
66 * @version 2.3
67 */
68public class Dictionary
69 extends HashMap {
70
71 /** The font used when displaying various html text. */
72 static private FontUIResource font = null;
73 /** A reference to remind us of the current locale. */
74 static private Locale locale = null;
75 /** The ResourceBundle which contains the raw key-value mappings.
76 Loaded from a file named "dictionary<I>locale</I>.properties. */
77 static private ResourceBundle dictionary = null;
78
79
80 public Dictionary(Locale locale, FontUIResource font)
81 {
82 super();
83
84 // Initialize
85 this.font = font;
86 this.locale = ((locale == null) ? Locale.getDefault() : locale);
87 this.dictionary = ResourceBundle.getBundle("dictionary", this.locale);
88 }
89
90
91 /** Retrieve the two letter code of the current language we are using, according to the stored locale.
92 * @return A <strong>String</strong> containing the two letter ISO639 language code.
93 */
94 static public String getLanguage()
95 {
96 return locale.getLanguage();
97 }
98
99
100 static public String get(String key)
101 {
102 return get(key, (String[]) null);
103 }
104
105
106 static public String get(String key, String arg)
107 {
108 String[] args = new String[1];
109 args[0] = arg;
110 return get(key, args);
111 }
112
113
114 /** 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. <BR>
115 * Here the get recieves a second argument which is an array of Strings used to populate argument fields, denoted {<I>n</I>}, within the value String returned.
116 * @param key A <strong>String</strong> which is mapped to a initial String within the ResourceBundle.
117 * @param args A <strong>String[]</strong> used to populate argument fields within the complete String.
118 * @return A <strong>String</strong> 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.
119 */
120 static public String get(String key, String[] args)
121 {
122 try {
123 String initial_raw = dictionary.getString(key);
124
125 // Convert into Unicode
126 String initial = "";
127 try {
128 // This "ISO-8859-1" looks out of place, but it is very important.
129 // It is essential to call getBytes with an 8-bit encoding, otherwise
130 // Java kindly deems some characters "undisplayable", and replaces
131 // them with question marks. This is NOT good.
132 initial = new String(initial_raw.getBytes("ISO-8859-1"), "UTF-8");
133 }
134 catch (Exception ex) {
135 System.err.println("Exception: " + ex);
136 ex.printStackTrace();
137 return initial_raw;
138 }
139
140 // If the string contains arguments we have to insert them.
141 String complete = "";
142
143 // While we still have initial string left.
144 while(initial.length() > 0 && initial.indexOf('{') != -1 && initial.indexOf('}') != -1) {
145 // Remove preamble
146 int opening = initial.indexOf('{');
147 int closing = initial.indexOf('}');
148 int comment_mark = initial.indexOf('-', opening); // May not exist
149 if (comment_mark > closing) { // May also be detecting a later comment
150 comment_mark = -1;
151 }
152 complete = complete + initial.substring(0, opening);
153
154 // Parse arg_num
155 String arg_str = null;
156 if (comment_mark != -1) {
157 arg_str = initial.substring(opening + 1, comment_mark);
158 }
159 else {
160 arg_str = initial.substring(opening + 1, closing);
161 }
162
163 if (closing + 1 < initial.length()) {
164 initial = initial.substring(closing + 1);
165 }
166 else {
167 initial = "";
168 }
169
170 // Insert argument
171 if (arg_str.equals("FONT")) {
172 complete = complete + (font != null ? font.getFontName() : "Arial");
173 }
174 else {
175 int arg_num = Integer.parseInt(arg_str);
176 if (args != null && 0 <= arg_num && arg_num < args.length) {
177 complete = complete + args[arg_num];
178 }
179 }
180 }
181 complete = complete + initial;
182
183 return complete;
184 }
185 catch (Exception e) {
186 System.err.println("Missing value for key: " + key);
187 // DebugStream.printStackTrace(e);
188 return key;
189 }
190 }
191
192
193 static public void registerBoth(Component component, String text_key, String tooltip_key)
194 {
195 setText(component, text_key);
196 setTooltip(component, tooltip_key);
197 }
198
199
200 static public void registerText(Component component, String text_key)
201 {
202 setText(component, text_key);
203 }
204
205
206 static public void registerText(Component component, String text_key, String[] args)
207 {
208 setText(component, text_key, args);
209 }
210
211
212 static public void registerTooltip(Component component, String tooltip_key)
213 {
214 setTooltip(component, tooltip_key);
215 }
216
217
218 static public void registerTooltipText(Component component, String tooltip)
219 {
220 setTooltipText(component, tooltip);
221 }
222
223
224 static public void setBoth(Component component, String text_key, String tooltip_key)
225 {
226 setText(component, text_key);
227 setTooltip(component, tooltip_key);
228 }
229
230
231 static public void setText(Component component, String text_key)
232 {
233 setText(component, text_key, null);
234 }
235
236
237 static public void setText(Component component, String text_key, String[] args)
238 {
239 if (component != null) {
240 // Update the component using the AWTEvent queue
241 SwingUtilities.invokeLater(new ComponentUpdateTask(component, get(text_key, args), null));
242 }
243 }
244
245
246 static public void setTooltip(Component component, String tooltip_key)
247 {
248 if (component != null) {
249 // Update the component using the AWTEvent queue
250 SwingUtilities.invokeLater(new ComponentUpdateTask(component, null, get(tooltip_key)));
251 }
252 }
253
254
255 static public void setTooltipText(Component component, String tooltip)
256 {
257 if (component != null) {
258 // Update the component using the AWTEvent queue
259 SwingUtilities.invokeLater(new ComponentUpdateTask(component, null, tooltip));
260 }
261 }
262
263
264 static private class ComponentUpdateTask
265 implements Runnable {
266
267 private Component component = null;
268 private String text = null;
269 private String tooltip = null;
270
271
272 public ComponentUpdateTask(Component component, String text, String tooltip)
273 {
274 this.component = component;
275 this.text = text;
276 this.tooltip = tooltip;
277 }
278
279
280 public void run()
281 {
282 // If the component has text
283 if (text != null) {
284 if (component instanceof AbstractButton) {
285 ((AbstractButton) component).setText(text);
286 }
287 else if (component instanceof Frame) {
288 ((Frame) component).setTitle(text);
289 }
290 else if (component instanceof JDialog) {
291 ((JDialog) component).setTitle(text);
292 }
293 else if (component instanceof JLabel) {
294 ((JLabel) component).setText(text);
295 }
296 else if (component instanceof JProgressBar) {
297 ((JProgressBar) component).setString(text);
298 }
299 else if (component instanceof JTextComponent) {
300 ((JTextComponent) component).setText(text);
301 ((JTextComponent) component).setCaretPosition(0);
302 }
303 else {
304 System.err.println("Unhandled component: " + component.getClass());
305 }
306 }
307
308 // If the component has a tooltip
309 if (tooltip != null) {
310 if (component instanceof JComponent) {
311 ((JComponent) component).setToolTipText(tooltip);
312 }
313 }
314 }
315 }
316
317
318 /**
319 * Register a tab pane component. This will be deprecated eventually. */
320 static public void register(JTabbedPane component)
321 {
322 if (component != null) {
323 String[] args = new String[component.getTabCount()];
324
325 // Iterate through the tabbed panes tabs, updating values and recording the original key of each item in args.
326 for (int i = 0; i < args.length; i++) {
327 if (args[i] == null) {
328 args[i] = component.getTitleAt(i);
329 }
330 String value = get(args[i], (String[]) null);
331 String tooltip = get(args[i] + "_Tooltip", (String[]) null);
332 JTabbedPaneChangeTask task = new JTabbedPaneChangeTask(component, i, value, tooltip);
333 SwingUtilities.invokeLater(task);
334 }
335 }
336 }
337
338
339 /** Updates a tabbed panes tab title and tooltip. */
340 static private class JTabbedPaneChangeTask
341 implements Runnable {
342
343 private JTabbedPane component;
344 private int index;
345 private String value;
346 private String tooltip;
347
348 public JTabbedPaneChangeTask(JTabbedPane component, int index, String value, String tooltip) {
349 this.component = component;
350 this.index = index;
351 this.value = value;
352 this.tooltip = tooltip;
353 }
354
355 public void run() {
356 component.setTitleAt(index, value);
357 component.setToolTipTextAt(index, tooltip);
358 }
359 }
360}
Note: See TracBrowser for help on using the repository browser.