AbstractComponentAddon.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:6k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: AbstractComponentAddon.java,v 1.3 2005/10/10 18:02:09 rbair Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.swingx.plaf;
  22. import java.util.ArrayList;
  23. import java.util.Enumeration;
  24. import java.util.List;
  25. import java.util.ResourceBundle;
  26. import javax.swing.UIManager;
  27. import org.jdesktop.swingx.plaf.aqua.AquaLookAndFeelAddons;
  28. import org.jdesktop.swingx.plaf.metal.MetalLookAndFeelAddons;
  29. import org.jdesktop.swingx.plaf.motif.MotifLookAndFeelAddons;
  30. import org.jdesktop.swingx.plaf.windows.WindowsLookAndFeelAddons;
  31. /**
  32.  * Ease the work of creating an addon for a component.<br>
  33.  * 
  34.  * @author <a href="mailto:fred@L2FProd.com">Frederic Lavigne</a>
  35.  */
  36. public abstract class AbstractComponentAddon implements ComponentAddon {
  37.   private String name;
  38.   
  39.   protected AbstractComponentAddon(String name) {
  40.     this.name = name;
  41.   }
  42.   
  43.   public final String getName() {
  44.     return name;
  45.   }
  46.   public void initialize(LookAndFeelAddons addon) {
  47.     addon.loadDefaults(getDefaults(addon));
  48.   }
  49.   public void uninitialize(LookAndFeelAddons addon) {
  50.     addon.unloadDefaults(getDefaults(addon));
  51.   }
  52.   /**
  53.    * Adds default key/value pairs to the given list.
  54.    * 
  55.    * @param addon
  56.    * @param defaults
  57.    */
  58.   protected void addBasicDefaults(LookAndFeelAddons addon, List<Object> defaults) {
  59.   }
  60.   /**
  61.    * Default implementation calls {@link #addBasicDefaults(LookAndFeelAddons, List)}
  62.    * 
  63.    * @param addon
  64.    * @param defaults
  65.    */
  66.   protected void addMacDefaults(LookAndFeelAddons addon, List<Object> defaults) {
  67.     addBasicDefaults(addon, defaults);
  68.   }
  69.   /**
  70.    * Default implementation calls {@link #addBasicDefaults(LookAndFeelAddons, List)}
  71.    * 
  72.    * @param addon
  73.    * @param defaults
  74.    */
  75.   protected void addMetalDefaults(LookAndFeelAddons addon, List<Object> defaults) {
  76.     addBasicDefaults(addon, defaults);
  77.   }
  78.   
  79.   /**
  80.    * Default implementation calls {@link #addBasicDefaults(LookAndFeelAddons, List)}
  81.    * 
  82.    * @param addon
  83.    * @param defaults
  84.    */
  85.   protected void addMotifDefaults(LookAndFeelAddons addon, List<Object> defaults) {
  86.     addBasicDefaults(addon, defaults);
  87.   }
  88.   /**
  89.    * Default implementation calls {@link #addBasicDefaults(LookAndFeelAddons, List)}
  90.    * 
  91.    * @param addon
  92.    * @param defaults
  93.    */
  94.   protected void addWindowsDefaults(LookAndFeelAddons addon, List<Object> defaults) {
  95.     addBasicDefaults(addon, defaults);
  96.   }
  97.     
  98.   /**
  99.    * Gets the defaults for the given addon.
  100.    * 
  101.    * Based on the addon, it calls
  102.    * {@link #addMacDefaults(LookAndFeelAddons, List)} if isMac()
  103.    * or
  104.    * {@link #addMetalDefaults(LookAndFeelAddons, List)} if isMetal()
  105.    * or
  106.    * {@link #addMotifDefaults(LookAndFeelAddons, List)} if isMotif()
  107.    * or
  108.    * {@link #addWindowsDefaults(LookAndFeelAddons, List)} if isWindows()
  109.    * or
  110.    * {@link #addBasicDefaults(LookAndFeelAddons, List)} if none of the above was called.
  111.    * @param addon
  112.    * @return an array of key/value pairs. For example:
  113.    * <pre>
  114.    * Object[] uiDefaults = {
  115.    *   "Font", new Font("Dialog", Font.BOLD, 12),
  116.    *   "Color", Color.red,
  117.    *   "five", new Integer(5)
  118.    * };
  119.    * </pre>
  120.    */
  121.   private Object[] getDefaults(LookAndFeelAddons addon) {
  122.     List<Object> defaults = new ArrayList<Object>();
  123.     if (isWindows(addon)) {
  124.       addWindowsDefaults(addon, defaults);
  125.     } else if (isMetal(addon)) {
  126.       addMetalDefaults(addon, defaults);
  127.     } else if (isMac(addon)) {
  128.       addMacDefaults(addon, defaults);
  129.     } else if (isMotif(addon)) {
  130.       addMotifDefaults(addon, defaults);
  131.     } else {
  132.       // at least add basic defaults
  133.       addBasicDefaults(addon, defaults);
  134.     }
  135.     return defaults.toArray();
  136.   }
  137.   //
  138.   // Helper methods to make ComponentAddon developer life easier
  139.   //
  140.   /**
  141.    * Adds the all keys/values from the given named resource bundle to the
  142.    * defaults
  143.    */
  144.   protected void addResource(List<Object> defaults, String bundleName) {
  145.     ResourceBundle bundle = ResourceBundle.getBundle(bundleName);
  146.     for (Enumeration<String> keys = bundle.getKeys(); keys.hasMoreElements(); ) {
  147.       String key = keys.nextElement();      
  148.       defaults.add(key);
  149.       defaults.add(bundle.getObject(key));
  150.     }
  151.   }
  152.   
  153.   /**
  154.    * @return true if the addon is the Windows addon or its subclasses
  155.    */
  156.   protected boolean isWindows(LookAndFeelAddons addon) {
  157.     return addon instanceof WindowsLookAndFeelAddons;
  158.   }
  159.   
  160.   /**
  161.    * @return true if the addon is the Metal addon or its subclasses
  162.    */
  163.   protected boolean isMetal(LookAndFeelAddons addon) {
  164.     return addon instanceof MetalLookAndFeelAddons;
  165.   }
  166.   
  167.   /**
  168.    * @return true if the addon is the Aqua addon or its subclasses
  169.    */
  170.   protected boolean isMac(LookAndFeelAddons addon) {
  171.     return addon instanceof AquaLookAndFeelAddons;
  172.   }
  173.   
  174.   /**
  175.    * @return true if the addon is the Motif addon or its subclasses
  176.    */
  177.   protected boolean isMotif(LookAndFeelAddons addon) {
  178.     return addon instanceof MotifLookAndFeelAddons;
  179.   }
  180.   /**
  181.    * @return true if the current look and feel is one of JGoodies Plastic l&fs
  182.    */
  183.   protected boolean isPlastic() {
  184.     return UIManager.getLookAndFeel().getClass().getName().contains("Plastic");
  185.   }
  186.   /**
  187.    * @return true if the current look and feel is Synth l&f
  188.    */
  189.   protected boolean isSynth() {
  190.     return UIManager.getLookAndFeel().getClass().getName().contains("ynth");    
  191.   }
  192.   
  193. }