001    /*
002     * $Id: AbstractComponentAddon.java,v 1.3 2005/10/10 18:02:09 rbair Exp $
003     *
004     * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005     * Santa Clara, California 95054, U.S.A. All rights reserved.
006     *
007     * This library is free software; you can redistribute it and/or
008     * modify it under the terms of the GNU Lesser General Public
009     * License as published by the Free Software Foundation; either
010     * version 2.1 of the License, or (at your option) any later version.
011     * 
012     * This library is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015     * Lesser General Public License for more details.
016     * 
017     * You should have received a copy of the GNU Lesser General Public
018     * License along with this library; if not, write to the Free Software
019     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
020     */
021    package org.jdesktop.swingx.plaf;
022    
023    import java.util.ArrayList;
024    import java.util.Enumeration;
025    import java.util.List;
026    import java.util.ResourceBundle;
027    
028    import javax.swing.UIManager;
029    
030    import org.jdesktop.swingx.plaf.aqua.AquaLookAndFeelAddons;
031    import org.jdesktop.swingx.plaf.metal.MetalLookAndFeelAddons;
032    import org.jdesktop.swingx.plaf.motif.MotifLookAndFeelAddons;
033    import org.jdesktop.swingx.plaf.windows.WindowsLookAndFeelAddons;
034    
035    /**
036     * Ease the work of creating an addon for a component.<br>
037     * 
038     * @author <a href="mailto:fred@L2FProd.com">Frederic Lavigne</a>
039     */
040    public abstract class AbstractComponentAddon implements ComponentAddon {
041    
042      private String name;
043      
044      protected AbstractComponentAddon(String name) {
045        this.name = name;
046      }
047      
048      public final String getName() {
049        return name;
050      }
051    
052      public void initialize(LookAndFeelAddons addon) {
053        addon.loadDefaults(getDefaults(addon));
054      }
055    
056      public void uninitialize(LookAndFeelAddons addon) {
057        addon.unloadDefaults(getDefaults(addon));
058      }
059    
060      /**
061       * Adds default key/value pairs to the given list.
062       * 
063       * @param addon
064       * @param defaults
065       */
066      protected void addBasicDefaults(LookAndFeelAddons addon, List<Object> defaults) {
067      }
068    
069      /**
070       * Default implementation calls {@link #addBasicDefaults(LookAndFeelAddons, List)}
071       * 
072       * @param addon
073       * @param defaults
074       */
075      protected void addMacDefaults(LookAndFeelAddons addon, List<Object> defaults) {
076        addBasicDefaults(addon, defaults);
077      }
078    
079      /**
080       * Default implementation calls {@link #addBasicDefaults(LookAndFeelAddons, List)}
081       * 
082       * @param addon
083       * @param defaults
084       */
085      protected void addMetalDefaults(LookAndFeelAddons addon, List<Object> defaults) {
086        addBasicDefaults(addon, defaults);
087      }
088      
089      /**
090       * Default implementation calls {@link #addBasicDefaults(LookAndFeelAddons, List)}
091       * 
092       * @param addon
093       * @param defaults
094       */
095      protected void addMotifDefaults(LookAndFeelAddons addon, List<Object> defaults) {
096        addBasicDefaults(addon, defaults);
097      }
098    
099      /**
100       * Default implementation calls {@link #addBasicDefaults(LookAndFeelAddons, List)}
101       * 
102       * @param addon
103       * @param defaults
104       */
105      protected void addWindowsDefaults(LookAndFeelAddons addon, List<Object> defaults) {
106        addBasicDefaults(addon, defaults);
107      }
108        
109      /**
110       * Gets the defaults for the given addon.
111       * 
112       * Based on the addon, it calls
113       * {@link #addMacDefaults(LookAndFeelAddons, List)} if isMac()
114       * or
115       * {@link #addMetalDefaults(LookAndFeelAddons, List)} if isMetal()
116       * or
117       * {@link #addMotifDefaults(LookAndFeelAddons, List)} if isMotif()
118       * or
119       * {@link #addWindowsDefaults(LookAndFeelAddons, List)} if isWindows()
120       * or
121       * {@link #addBasicDefaults(LookAndFeelAddons, List)} if none of the above was called.
122       * @param addon
123       * @return an array of key/value pairs. For example:
124       * <pre>
125       * Object[] uiDefaults = {
126       *   "Font", new Font("Dialog", Font.BOLD, 12),
127       *   "Color", Color.red,
128       *   "five", new Integer(5)
129       * };
130       * </pre>
131       */
132      private Object[] getDefaults(LookAndFeelAddons addon) {
133        List<Object> defaults = new ArrayList<Object>();
134        if (isWindows(addon)) {
135          addWindowsDefaults(addon, defaults);
136        } else if (isMetal(addon)) {
137          addMetalDefaults(addon, defaults);
138        } else if (isMac(addon)) {
139          addMacDefaults(addon, defaults);
140        } else if (isMotif(addon)) {
141          addMotifDefaults(addon, defaults);
142        } else {
143          // at least add basic defaults
144          addBasicDefaults(addon, defaults);
145        }
146        return defaults.toArray();
147      }
148    
149      //
150      // Helper methods to make ComponentAddon developer life easier
151      //
152    
153      /**
154       * Adds the all keys/values from the given named resource bundle to the
155       * defaults
156       */
157      protected void addResource(List<Object> defaults, String bundleName) {
158        ResourceBundle bundle = ResourceBundle.getBundle(bundleName);
159        for (Enumeration<String> keys = bundle.getKeys(); keys.hasMoreElements(); ) {
160          String key = keys.nextElement();      
161          defaults.add(key);
162          defaults.add(bundle.getObject(key));
163        }
164      }
165      
166      /**
167       * @return true if the addon is the Windows addon or its subclasses
168       */
169      protected boolean isWindows(LookAndFeelAddons addon) {
170        return addon instanceof WindowsLookAndFeelAddons;
171      }
172      
173      /**
174       * @return true if the addon is the Metal addon or its subclasses
175       */
176      protected boolean isMetal(LookAndFeelAddons addon) {
177        return addon instanceof MetalLookAndFeelAddons;
178      }
179      
180      /**
181       * @return true if the addon is the Aqua addon or its subclasses
182       */
183      protected boolean isMac(LookAndFeelAddons addon) {
184        return addon instanceof AquaLookAndFeelAddons;
185      }
186      
187      /**
188       * @return true if the addon is the Motif addon or its subclasses
189       */
190      protected boolean isMotif(LookAndFeelAddons addon) {
191        return addon instanceof MotifLookAndFeelAddons;
192      }
193    
194      /**
195       * @return true if the current look and feel is one of JGoodies Plastic l&fs
196       */
197      protected boolean isPlastic() {
198        return UIManager.getLookAndFeel().getClass().getName().contains("Plastic");
199      }
200    
201      /**
202       * @return true if the current look and feel is Synth l&f
203       */
204      protected boolean isSynth() {
205        return UIManager.getLookAndFeel().getClass().getName().contains("ynth");    
206      }
207      
208    }