001: /*
002: * $Id: AbstractComponentAddon.java,v 1.6 2007/02/07 15:09:51 l2fprod 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.macosx.MacOSXLookAndFeelAddons;
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: // commented after Issue 446. Maybe addon should keep track of its
058: // added defaults to correctly remove them on uninitialize
059: // addon.unloadDefaults(getDefaults(addon));
060: }
061:
062: /**
063: * Adds default key/value pairs to the given list.
064: *
065: * @param addon
066: * @param defaults
067: */
068: protected void addBasicDefaults(LookAndFeelAddons addon,
069: List<Object> defaults) {
070: }
071:
072: /**
073: * Default implementation calls {@link #addBasicDefaults(LookAndFeelAddons, List)}
074: *
075: * @param addon
076: * @param defaults
077: */
078: protected void addMacDefaults(LookAndFeelAddons addon,
079: List<Object> defaults) {
080: addBasicDefaults(addon, defaults);
081: }
082:
083: /**
084: * Default implementation calls {@link #addBasicDefaults(LookAndFeelAddons, List)}
085: *
086: * @param addon
087: * @param defaults
088: */
089: protected void addMetalDefaults(LookAndFeelAddons addon,
090: List<Object> defaults) {
091: addBasicDefaults(addon, defaults);
092: }
093:
094: /**
095: * Default implementation calls {@link #addBasicDefaults(LookAndFeelAddons, List)}
096: *
097: * @param addon
098: * @param defaults
099: */
100: protected void addMotifDefaults(LookAndFeelAddons addon,
101: List<Object> defaults) {
102: addBasicDefaults(addon, defaults);
103: }
104:
105: /**
106: * Default implementation calls {@link #addBasicDefaults(LookAndFeelAddons, List)}
107: *
108: * @param addon
109: * @param defaults
110: */
111: protected void addWindowsDefaults(LookAndFeelAddons addon,
112: List<Object> defaults) {
113: addBasicDefaults(addon, defaults);
114: }
115:
116: /**
117: * Gets the defaults for the given addon.
118: *
119: * Based on the addon, it calls
120: * {@link #addMacDefaults(LookAndFeelAddons, List)} if isMac()
121: * or
122: * {@link #addMetalDefaults(LookAndFeelAddons, List)} if isMetal()
123: * or
124: * {@link #addMotifDefaults(LookAndFeelAddons, List)} if isMotif()
125: * or
126: * {@link #addWindowsDefaults(LookAndFeelAddons, List)} if isWindows()
127: * or
128: * {@link #addBasicDefaults(LookAndFeelAddons, List)} if none of the above was called.
129: * @param addon
130: * @return an array of key/value pairs. For example:
131: * <pre>
132: * Object[] uiDefaults = {
133: * "Font", new Font("Dialog", Font.BOLD, 12),
134: * "Color", Color.red,
135: * "five", new Integer(5)
136: * };
137: * </pre>
138: */
139: private Object[] getDefaults(LookAndFeelAddons addon) {
140: List<Object> defaults = new ArrayList<Object>();
141: if (isWindows(addon)) {
142: addWindowsDefaults(addon, defaults);
143: } else if (isMetal(addon)) {
144: addMetalDefaults(addon, defaults);
145: } else if (isMac(addon)) {
146: addMacDefaults(addon, defaults);
147: } else if (isMotif(addon)) {
148: addMotifDefaults(addon, defaults);
149: } else {
150: // at least add basic defaults
151: addBasicDefaults(addon, defaults);
152: }
153: return defaults.toArray();
154: }
155:
156: //
157: // Helper methods to make ComponentAddon developer life easier
158: //
159:
160: /**
161: * Adds the all keys/values from the given named resource bundle to the
162: * defaults
163: */
164: protected void addResource(List<Object> defaults, String bundleName) {
165: ResourceBundle bundle = ResourceBundle.getBundle(bundleName);
166: for (Enumeration<String> keys = bundle.getKeys(); keys
167: .hasMoreElements();) {
168: String key = keys.nextElement();
169: defaults.add(key);
170: defaults.add(bundle.getObject(key));
171: }
172: }
173:
174: /**
175: * @return true if the addon is the Windows addon or its subclasses
176: */
177: protected boolean isWindows(LookAndFeelAddons addon) {
178: return addon instanceof WindowsLookAndFeelAddons;
179: }
180:
181: /**
182: * @return true if the addon is the Metal addon or its subclasses
183: */
184: protected boolean isMetal(LookAndFeelAddons addon) {
185: return addon instanceof MetalLookAndFeelAddons;
186: }
187:
188: /**
189: * @return true if the addon is the Mac OS X addon or its subclasses
190: */
191: protected boolean isMac(LookAndFeelAddons addon) {
192: return addon instanceof MacOSXLookAndFeelAddons;
193: }
194:
195: /**
196: * @return true if the addon is the Motif addon or its subclasses
197: */
198: protected boolean isMotif(LookAndFeelAddons addon) {
199: return addon instanceof MotifLookAndFeelAddons;
200: }
201:
202: /**
203: * @return true if the current look and feel is one of JGoodies Plastic l&fs
204: */
205: protected boolean isPlastic() {
206: return UIManager.getLookAndFeel().getClass().getName()
207: .contains("Plastic");
208: }
209:
210: /**
211: * @return true if the current look and feel is Synth l&f
212: */
213: protected boolean isSynth() {
214: return UIManager.getLookAndFeel().getClass().getName()
215: .contains("ynth");
216: }
217:
218: }
|