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

Last change on this file since 11327 was 10668, checked in by mdewsnip, 19 years ago

Added code to remove comments from the text strings -- this is needed when the GTI is used, as it adds "# Updated XX-XX-XXXX" to the end of strings to keep track of when they were last updated.

  • Property svn:keywords set to Author Date Id Revision
File size: 11.2 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 exception) {
135 DebugStream.printStackTrace(exception);
136 return initial_raw;
137 }
138
139 // Remove any comments from the string
140 if (initial.indexOf("#") != -1) {
141 initial = initial.substring(0, initial.indexOf("#"));
142 }
143
144 // If the string contains arguments we have to insert them.
145 String complete = "";
146
147 // While we still have initial string left.
148 while (initial.length() > 0 && initial.indexOf('{') != -1 && initial.indexOf('}') != -1) {
149 // Remove preamble
150 int opening = initial.indexOf('{');
151 int closing = initial.indexOf('}');
152 int comment_mark = initial.indexOf('-', opening); // May not exist
153 if (comment_mark > closing) { // May also be detecting a later comment
154 comment_mark = -1;
155 }
156 complete = complete + initial.substring(0, opening);
157
158 // Parse arg_num
159 String arg_str = null;
160 if (comment_mark != -1) {
161 arg_str = initial.substring(opening + 1, comment_mark);
162 }
163 else {
164 arg_str = initial.substring(opening + 1, closing);
165 }
166
167 if (closing + 1 < initial.length()) {
168 initial = initial.substring(closing + 1);
169 }
170 else {
171 initial = "";
172 }
173
174 // Insert argument
175 if (arg_str.equals("FONT")) {
176 complete = complete + (font != null ? font.getFontName() : "Arial");
177 }
178 else {
179 int arg_num = Integer.parseInt(arg_str);
180 if (args != null && 0 <= arg_num && arg_num < args.length) {
181 complete = complete + args[arg_num];
182 }
183 }
184 }
185 complete = complete + initial;
186
187 return complete;
188 }
189 catch (Exception exception) {
190 System.err.println("Missing value for key: " + key);
191 // DebugStream.printStackTrace(e);
192 return key;
193 }
194 }
195
196
197 static public void registerBoth(Component component, String text_key, String tooltip_key)
198 {
199 setText(component, text_key);
200 setTooltip(component, tooltip_key);
201 }
202
203
204 static public void registerText(Component component, String text_key)
205 {
206 setText(component, text_key);
207 }
208
209
210 static public void registerText(Component component, String text_key, String[] args)
211 {
212 setText(component, text_key, args);
213 }
214
215
216 static public void registerTooltip(Component component, String tooltip_key)
217 {
218 setTooltip(component, tooltip_key);
219 }
220
221
222 static public void registerTooltipText(Component component, String tooltip)
223 {
224 setTooltipText(component, tooltip);
225 }
226
227
228 static public void setBoth(Component component, String text_key, String tooltip_key)
229 {
230 setText(component, text_key);
231 setTooltip(component, tooltip_key);
232 }
233
234
235 static public void setText(Component component, String text_key)
236 {
237 setText(component, text_key, null);
238 }
239
240
241 static public void setText(Component component, String text_key, String[] args)
242 {
243 if (component != null) {
244 // Update the component using the AWTEvent queue
245 SwingUtilities.invokeLater(new ComponentUpdateTask(component, get(text_key, args), null));
246 }
247 }
248
249
250 static public void setTooltip(Component component, String tooltip_key)
251 {
252 if (component != null) {
253 // Update the component using the AWTEvent queue
254 SwingUtilities.invokeLater(new ComponentUpdateTask(component, null, get(tooltip_key)));
255 }
256 }
257
258
259 static public void setTooltipText(Component component, String tooltip)
260 {
261 if (component != null) {
262 // Update the component using the AWTEvent queue
263 SwingUtilities.invokeLater(new ComponentUpdateTask(component, null, tooltip));
264 }
265 }
266
267
268 static private class ComponentUpdateTask
269 implements Runnable {
270
271 private Component component = null;
272 private String text = null;
273 private String tooltip = null;
274
275
276 public ComponentUpdateTask(Component component, String text, String tooltip)
277 {
278 this.component = component;
279 this.text = text;
280 this.tooltip = tooltip;
281 }
282
283
284 public void run()
285 {
286 // If the component has text
287 if (text != null) {
288 if (component instanceof AbstractButton) {
289 ((AbstractButton) component).setText(text);
290 }
291 else if (component instanceof Frame) {
292 ((Frame) component).setTitle(text);
293 }
294 else if (component instanceof JDialog) {
295 ((JDialog) component).setTitle(text);
296 }
297 else if (component instanceof JLabel) {
298 ((JLabel) component).setText(text);
299 }
300 else if (component instanceof JProgressBar) {
301 ((JProgressBar) component).setString(text);
302 }
303 else if (component instanceof JTextComponent) {
304 ((JTextComponent) component).setText(text);
305 ((JTextComponent) component).setCaretPosition(0);
306 }
307 else {
308 System.err.println("Unhandled component: " + component.getClass());
309 }
310 }
311
312 // If the component has a tooltip
313 if (tooltip != null) {
314 if (component instanceof JComponent) {
315 ((JComponent) component).setToolTipText(tooltip);
316 }
317 }
318 }
319 }
320
321
322 /**
323 * Register a tab pane component. This will be deprecated eventually. */
324 static public void register(JTabbedPane component)
325 {
326 if (component != null) {
327 String[] args = new String[component.getTabCount()];
328
329 // Iterate through the tabbed panes tabs, updating values and recording the original key of each item in args.
330 for (int i = 0; i < args.length; i++) {
331 if (args[i] == null) {
332 args[i] = component.getTitleAt(i);
333 }
334 String value = get(args[i], (String[]) null);
335 String tooltip = get(args[i] + "_Tooltip", (String[]) null);
336 JTabbedPaneChangeTask task = new JTabbedPaneChangeTask(component, i, value, tooltip);
337 SwingUtilities.invokeLater(task);
338 }
339 }
340 }
341
342
343 /** Updates a tabbed panes tab title and tooltip. */
344 static private class JTabbedPaneChangeTask
345 implements Runnable {
346
347 private JTabbedPane component;
348 private int index;
349 private String value;
350 private String tooltip;
351
352 public JTabbedPaneChangeTask(JTabbedPane component, int index, String value, String tooltip) {
353 this.component = component;
354 this.index = index;
355 this.value = value;
356 this.tooltip = tooltip;
357 }
358
359 public void run() {
360 component.setTitleAt(index, value);
361 component.setToolTipTextAt(index, tooltip);
362 }
363 }
364}
Note: See TracBrowser for help on using the repository browser.