Changeset 6821


Ignore:
Timestamp:
2004-02-18T10:15:38+13:00 (20 years ago)
Author:
mdewsnip
Message:

Tidied up the Dictionary a lot. Now that a restart is always required when changing dictionaries (excellent move), components do not have to be registered with the dictionary (to be updated when the dictionary changes). Registering is now the same as setting -- saving a fair bit of memory and a chunk of code.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/Dictionary.java

    r6633 r6821  
    7575    /** A String which more explicitly states the Locale of this dictionary. */
    7676    public String language = null;
    77     /** A static reference to ourself. */
    78     static public Dictionary self;
    7977    /** The font used when displaying various html text. */
    80     private FontUIResource font = null;
     78    static private FontUIResource font = null;
    8179    /** A reference to remind us of the current locale. */
    8280    private Locale locale = null;
    8381    /** 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;
    8583
    8684    private TreeSet key_list = null;
     
    9189    public Dictionary(Locale locale, FontUIResource font) {
    9290    super();
    93     this.self = this;
    9491
    9592    if (KEY_LIST_DEBUG) {
     
    126123
    127124
    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//          }
    146141//      }
    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();
    228150    }
    229151
     
    231153    static public String get(String key)
    232154    {
    233     return self.oldget(key);
     155    return get(key, (String[]) null);
    234156    }
    235157
     
    239161    String[] args = new String[1];
    240162    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
    269166
    270167    /** 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>
     
    274171     * @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.
    275172     */
    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//      }
    282180    try {
    283181        String initial = dictionary.getString(key);
     
    285183        // If the string contains arguments we have to insert them.
    286184        String complete = "";
     185
    287186        // While we still have initial string left.
    288187        while(initial.length() > 0 && initial.indexOf('{') != -1 && initial.indexOf('}') != -1) {
     
    322221            f_subargs[0] = "Arial";
    323222            }
    324             complete = complete + oldget("Farg" + arg_num, f_subargs);
     223            complete = complete + get("Farg" + arg_num, f_subargs);
    325224        }
    326225        }
     
    332231        }
    333232        catch (Exception ex) {
    334         System.err.println("Something bad. " + ex);
     233        System.err.println("Exception: " + ex);
    335234        Gatherer.printStackTrace(ex);
    336235        return initial;
     
    340239    catch (Exception e) {
    341240        System.err.println("Missing value for key: " + key);
    342         //Gatherer.printStackTrace(e);
     241        // Gatherer.printStackTrace(e);
    343242        return key;
    344243    }
    345244    }
    346245
    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);
    352273    }
    353274
     
    371292        // Update the component using the AWTEvent queue
    372293        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);
    374295        SwingUtilities.invokeLater(task);
    375296    }
     
    382303        // Update the component using the AWTEvent queue
    383304        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);
    385306        SwingUtilities.invokeLater(task);
    386307    }
     
    388309
    389310
    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)
    404312    {
    405313    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);
    420315        SwingUtilities.invokeLater(task);
    421316    }
    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 details
    432         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 queue
    443         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);
    450317    }
    451318
     
    478345    private String tooltip = null;
    479346
    480     public ComponentUpdateTask(Component component, ComponentDetails details, boolean is_key)
     347
     348    public ComponentUpdateTask(Component component, ComponentDetails details)
    481349    {
    482350        this.component = component;
    483351
    484352        if (details.has_text) {
    485         text = self.oldget(details.text_key, details.text_args);
     353        text = get(details.text_key, details.text_args);
    486354        }
    487355        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;
    495366    }
    496367
     
    539410    {
    540411    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.
    544415        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    }
    554425    }
    555426
     
    557428    /** Updates a tabbed panes tab title and tooltip. */
    558429    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) {
    567438        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        }
    577448    }
    578449}
Note: See TracChangeset for help on using the changeset viewer.