Changeset 6821
- Timestamp:
- 2004-02-18T10:15:38+13:00 (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gli/src/org/greenstone/gatherer/Dictionary.java
r6633 r6821 75 75 /** A String which more explicitly states the Locale of this dictionary. */ 76 76 public String language = null; 77 /** A static reference to ourself. */78 static public Dictionary self;79 77 /** The font used when displaying various html text. */ 80 private FontUIResource font = null;78 static private FontUIResource font = null; 81 79 /** A reference to remind us of the current locale. */ 82 80 private Locale locale = null; 83 81 /** The ResourceBundle which contains the raw key-value mappings. Loaded from a file named "dictionary<I>locale</I>.properties*/ 84 private ResourceBundle dictionary = null;82 static private ResourceBundle dictionary = null; 85 83 86 84 private TreeSet key_list = null; … … 91 89 public Dictionary(Locale locale, FontUIResource font) { 92 90 super(); 93 this.self = this;94 91 95 92 if (KEY_LIST_DEBUG) { … … 126 123 127 124 128 /** Change the currently loaded dictionary and update registered components */ 129 public void changeDictionary(Locale locale) 130 { 131 //this.locale = locale; 132 133 // Load new dictionary 134 //dictionary = ResourceBundle.getBundle(Utility.DICTIONARY, locale); 135 //language = dictionary.getString("Language"); 136 //Gatherer.println("Loaded new dictionary: " + language); 137 138 // Refresh all registered component 139 // !! TO FINISH !! 140 //Gatherer.println("Updating components..."); 141 // ArrayList temp_list = new ArrayList(); 142 // Iterator components = keySet().iterator(); 143 // while (components.hasNext()) { 144 // Component component = (Component) components.next(); 145 // temp_list.add(component); 125 public void destroy() { 126 // if(key_list != null) { 127 // try { 128 // FileOutputStream fos = new FileOutputStream(KEY_LIST_FILENAME); 129 // for(Iterator iter = key_list.iterator(); iter.hasNext(); ) { 130 // String value = (String) iter.next(); 131 // fos.write(value.getBytes()); 132 // fos.write('\n'); 133 // value = null; 134 // } 135 // fos.close(); 136 // fos = null; 137 // } 138 // catch(Exception error) { 139 // error.printStackTrace(); 140 // } 146 141 // } 147 148 // for (int i = 0; i < temp_list.size(); i++) { 149 // Component component = (Component) temp_list.get(i); 150 // ComponentDetails details = (ComponentDetails) get(component); 151 // oldSetBoth(component, details.text_key, details.text_args, details.tooltip_key); 152 // } 153 154 // for (Iterator keys = keySet().iterator(); keys.hasNext(); ) { 155 // Component component = (Component) keys.next(); 156 // ComponentDetails details = (ComponentDetails) get(component); 157 // oldSetBoth(component, details.text_key, details.text_args, details.tooltip_key); 158 // } 159 } 160 161 162 /** Change the currently loaded dictionary and update registered (ie dynamic) components as possible. */ 163 // public void oldChangeDictionary(Locale locale) { 164 // this.locale = locale; 165 // // Load new dictionary 166 // dictionary = ResourceBundle.getBundle(Utility.DICTIONARY, locale); 167 // language = dictionary.getString("Language"); 168 // Gatherer.println("Having loaded new dictionary: " + language); 169 // // Refresh all registered component 170 // Gatherer.println("Updating components"); 171 // for(Iterator keys = keySet().iterator(); keys.hasNext(); ) { 172 // Object component = keys.next(); 173 // String[] args = (String[]) get(component); 174 // if(component instanceof AbstractButton) { 175 // register((AbstractButton)component, args, true); 176 // } 177 // else if(component instanceof JComboBox) { 178 // register((JComboBox)component, args, true); 179 // } 180 // else if(component instanceof JDialog) { 181 // register((JDialog)component, args, true); 182 // } 183 // else if(component instanceof JFrame) { 184 // register((JFrame)component, args, true); 185 // } 186 // else if(component instanceof JLabel) { 187 // register((JLabel)component, args, true); 188 // } 189 // else if(component instanceof JTabbedPane) { 190 // register((JTabbedPane)component, args, true); 191 // } 192 // else if(component instanceof JTextComponent) { 193 // register((JTextComponent)component, args, true); 194 // } 195 // else if(component instanceof JTree) { 196 // register((JTree)component, args, true); 197 // } 198 // else if(component instanceof TitledBorder) { 199 // register((TitledBorder)component, args, true); 200 // } 201 // args = null; 202 // component = null; 203 // } 204 // } 205 206 /** Remove the component from our registered components list. */ 207 /* private void deregister(Object component) { 208 remove(component); 209 } */ 210 211 public void destroy() { 212 if(key_list != null) { 213 try { 214 FileOutputStream fos = new FileOutputStream(KEY_LIST_FILENAME); 215 for(Iterator iter = key_list.iterator(); iter.hasNext(); ) { 216 String value = (String) iter.next(); 217 fos.write(value.getBytes()); 218 fos.write('\n'); 219 value = null; 220 } 221 fos.close(); 222 fos = null; 223 } 224 catch(Exception error) { 225 error.printStackTrace(); 226 } 227 } 142 } 143 144 145 /** Retrieve the two letter code of the current language we are using, according to the stored locale. 146 * @return A <strong>String</strong> containing the two letter ISO639 language code. 147 */ 148 public String getLanguage() { 149 return locale.getLanguage(); 228 150 } 229 151 … … 231 153 static public String get(String key) 232 154 { 233 return self.oldget(key);155 return get(key, (String[]) null); 234 156 } 235 157 … … 239 161 String[] args = new String[1]; 240 162 args[0] = arg; 241 return self.oldget(key, args); 242 } 243 244 245 static public String get(String key, String[] args) 246 { 247 return self.oldget(key, args); 248 } 249 250 251 /** 252 * @deprecated 253 * Overloaded to call get with both a key and an empty argument array. 254 * @param key A <strong>String</strong> which is mapped to a initial String within the ResourceBundle. 255 * @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. 256 */ 257 public String oldget(String key) { 258 return oldget(key, (String[])null); 259 } 260 261 /** 262 * @deprecated 263 * Convienence method with transforms the second string argument into a string array. */ 264 public String oldget(String key, String arg) { 265 String[] args = new String[1]; 266 args[0] = arg; 267 return oldget(key, args); 268 } 163 return get(key, args); 164 } 165 269 166 270 167 /** 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> … … 274 171 * @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. 275 172 */ 276 public String oldget(String key, String args[]) { 277 if(key_list != null) { 278 synchronized(this) { 279 key_list.add(key); 280 } 281 } 173 static public String get(String key, String[] args) 174 { 175 // if(key_list != null) { 176 // synchronized(this) { 177 // key_list.add(key); 178 // } 179 // } 282 180 try { 283 181 String initial = dictionary.getString(key); … … 285 183 // If the string contains arguments we have to insert them. 286 184 String complete = ""; 185 287 186 // While we still have initial string left. 288 187 while(initial.length() > 0 && initial.indexOf('{') != -1 && initial.indexOf('}') != -1) { … … 322 221 f_subargs[0] = "Arial"; 323 222 } 324 complete = complete + oldget("Farg" + arg_num, f_subargs);223 complete = complete + get("Farg" + arg_num, f_subargs); 325 224 } 326 225 } … … 332 231 } 333 232 catch (Exception ex) { 334 System.err.println(" Something bad." + ex);233 System.err.println("Exception: " + ex); 335 234 Gatherer.printStackTrace(ex); 336 235 return initial; … … 340 239 catch (Exception e) { 341 240 System.err.println("Missing value for key: " + key); 342 // Gatherer.printStackTrace(e);241 // Gatherer.printStackTrace(e); 343 242 return key; 344 243 } 345 244 } 346 245 347 /** Retrieve the two letter code of the current language we are using, according to the stored locale. 348 * @return A <strong>String</strong> containing the two letter ISO639 language code. 349 */ 350 public String getLanguage() { 351 return locale.getLanguage(); 246 247 static public void registerBoth(Component component, String text_key, String tooltip_key) 248 { 249 setText(component, text_key); 250 setTooltip(component, tooltip_key); 251 } 252 253 254 static public void registerText(Component component, String text_key) 255 { 256 setText(component, text_key); 257 } 258 259 260 static public void registerText(Component component, String text_key, String[] args) 261 { 262 setText(component, text_key, args); 263 } 264 265 266 static public void registerTooltip(Component component, String tooltip_key) { 267 setTooltip(component, tooltip_key); 268 } 269 270 271 static public void registerTooltipText(Component component, String tooltip) { 272 setTooltipText(component, tooltip); 352 273 } 353 274 … … 371 292 // Update the component using the AWTEvent queue 372 293 ComponentDetails details = new ComponentDetails(true, text_key, args, false, null); 373 ComponentUpdateTask task = new ComponentUpdateTask(component, details , true);294 ComponentUpdateTask task = new ComponentUpdateTask(component, details); 374 295 SwingUtilities.invokeLater(task); 375 296 } … … 382 303 // Update the component using the AWTEvent queue 383 304 ComponentDetails details = new ComponentDetails(false, null, null, true, tooltip_key); 384 ComponentUpdateTask task = new ComponentUpdateTask(component, details , true);305 ComponentUpdateTask task = new ComponentUpdateTask(component, details); 385 306 SwingUtilities.invokeLater(task); 386 307 } … … 388 309 389 310 390 static public void registerBoth(Component component, String text_key, String tooltip_key) 391 { 392 registerText(component, text_key, null); 393 registerTooltip(component, tooltip_key); 394 } 395 396 397 static public void registerText(Component component, String text_key) 398 { 399 registerText(component, text_key, null); 400 } 401 402 403 static public void registerText(Component component, String text_key, String[] args) 311 static public void setTooltipText(Component component, String tooltip) 404 312 { 405 313 if (component != null) { 406 // Update the component's details 407 ComponentDetails details = (ComponentDetails) self.remove(component); 408 if (details == null) { 409 details = new ComponentDetails(true, text_key, args, false, null); 410 } 411 else { 412 details.has_text = true; 413 details.text_key = text_key; 414 details.text_args = args; 415 } 416 self.put(component, details); 417 418 // Update the component using the AWTEvent queue 419 ComponentUpdateTask task = new ComponentUpdateTask(component, details, true); 314 ComponentUpdateTask task = new ComponentUpdateTask(component, null, tooltip); 420 315 SwingUtilities.invokeLater(task); 421 316 } 422 }423 424 static public void registerTooltip(Component component, String tooltip_key) {425 self.registerTooltip(component, tooltip_key, true);426 }427 428 static public void registerTooltip(Component component, String tooltip_key, boolean is_key)429 {430 if (component != null) {431 // Update the component's details432 ComponentDetails details = (ComponentDetails) self.remove(component);433 if (details == null) {434 details = new ComponentDetails(false, null, null, true, tooltip_key);435 }436 else {437 details.has_tooltip = true;438 details.tooltip_key = tooltip_key;439 }440 self.put(component, details);441 442 // Update the component using the AWTEvent queue443 ComponentUpdateTask task = new ComponentUpdateTask(component, details, is_key);444 SwingUtilities.invokeLater(task);445 }446 }447 448 static public void unregister(Component component) {449 self.remove(component);450 317 } 451 318 … … 478 345 private String tooltip = null; 479 346 480 public ComponentUpdateTask(Component component, ComponentDetails details, boolean is_key) 347 348 public ComponentUpdateTask(Component component, ComponentDetails details) 481 349 { 482 350 this.component = component; 483 351 484 352 if (details.has_text) { 485 text = self.oldget(details.text_key, details.text_args);353 text = get(details.text_key, details.text_args); 486 354 } 487 355 if (details.has_tooltip) { 488 if(is_key) { 489 tooltip = self.oldget(details.tooltip_key, (String[]) null); 490 } 491 else { 492 tooltip = details.tooltip_key; 493 } 494 } 356 tooltip = get(details.tooltip_key, (String[]) null); 357 } 358 } 359 360 361 public ComponentUpdateTask(Component component, String text, String tooltip) 362 { 363 this.component = component; 364 this.text = text; 365 this.tooltip = tooltip; 495 366 } 496 367 … … 539 410 { 540 411 if (component != null) { 541 String[] args = new String[component.getTabCount()];542 543 // Iterate through the tabbed panes tabs, updating values and recording the original key of each item in args.412 String[] args = new String[component.getTabCount()]; 413 414 // Iterate through the tabbed panes tabs, updating values and recording the original key of each item in args. 544 415 for (int i = 0; i < args.length; i++) { 545 if (args[i] == null) {546 args[i] = component.getTitleAt(i);547 }548 String value = self.oldget(args[i], (String[]) null);549 String tooltip = self.oldget(args[i] + "_Tooltip", (String[]) null);550 JTabbedPaneChangeTask task = new JTabbedPaneChangeTask(component, i, value, tooltip);551 SwingUtilities.invokeLater(task);552 }553 }416 if (args[i] == null) { 417 args[i] = component.getTitleAt(i); 418 } 419 String value = get(args[i], (String[]) null); 420 String tooltip = get(args[i] + "_Tooltip", (String[]) null); 421 JTabbedPaneChangeTask task = new JTabbedPaneChangeTask(component, i, value, tooltip); 422 SwingUtilities.invokeLater(task); 423 } 424 } 554 425 } 555 426 … … 557 428 /** Updates a tabbed panes tab title and tooltip. */ 558 429 static private class JTabbedPaneChangeTask 559 implements Runnable {560 561 private JTabbedPane component;562 private int index;563 private String value;564 private String tooltip;565 566 public JTabbedPaneChangeTask(JTabbedPane component, int index, String value, String tooltip) {430 implements Runnable { 431 432 private JTabbedPane component; 433 private int index; 434 private String value; 435 private String tooltip; 436 437 public JTabbedPaneChangeTask(JTabbedPane component, int index, String value, String tooltip) { 567 438 this.component = component; 568 this.index = index;569 this.value = value;570 this.tooltip = tooltip;571 }572 573 public void run() {574 component.setTitleAt(index, value);575 component.setToolTipTextAt(index, tooltip);576 }439 this.index = index; 440 this.value = value; 441 this.tooltip = tooltip; 442 } 443 444 public void run() { 445 component.setTitleAt(index, value); 446 component.setToolTipTextAt(index, tooltip); 447 } 577 448 } 578 449 }
Note:
See TracChangeset
for help on using the changeset viewer.