001: /*
002: * $Id: AbstractComponentAddon.java,v 1.2 2006/03/25 14:56:14 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: package com.l2fprod.common.swing.plaf;
008:
009: import com.l2fprod.common.swing.plaf.aqua.AquaLookAndFeelAddons;
010: import com.l2fprod.common.swing.plaf.metal.MetalLookAndFeelAddons;
011: import com.l2fprod.common.swing.plaf.motif.MotifLookAndFeelAddons;
012: import com.l2fprod.common.swing.plaf.windows.WindowsLookAndFeelAddons;
013:
014: import java.awt.Color;
015: import java.awt.Font;
016: import java.util.ArrayList;
017: import java.util.Enumeration;
018: import java.util.List;
019: import java.util.ResourceBundle;
020:
021: import javax.swing.UIManager;
022:
023: /**
024: * Ease the work of creating an addon for a component.<br>
025: *
026: * @author Frederic Lavigne
027: */
028: public abstract class AbstractComponentAddon implements ComponentAddon {
029:
030: private String name;
031:
032: protected AbstractComponentAddon(String name) {
033: this .name = name;
034: }
035:
036: public final String getName() {
037: return name;
038: }
039:
040: public void initialize(LookAndFeelAddons addon) {
041: addon.loadDefaults(getDefaults(addon));
042: }
043:
044: public void uninitialize(LookAndFeelAddons addon) {
045: addon.unloadDefaults(getDefaults(addon));
046: }
047:
048: /**
049: * Adds default key/value pairs to the given list.
050: *
051: * @param addon
052: * @param defaults
053: */
054: protected void addBasicDefaults(LookAndFeelAddons addon,
055: List defaults) {
056: }
057:
058: /**
059: * Default implementation calls {@link #addBasicDefaults(LookAndFeelAddons, List)}
060: *
061: * @param addon
062: * @param defaults
063: */
064: protected void addMacDefaults(LookAndFeelAddons addon, List defaults) {
065: addBasicDefaults(addon, defaults);
066: }
067:
068: /**
069: * Default implementation calls {@link #addBasicDefaults(LookAndFeelAddons, List)}
070: *
071: * @param addon
072: * @param defaults
073: */
074: protected void addMetalDefaults(LookAndFeelAddons addon,
075: List 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 addMotifDefaults(LookAndFeelAddons addon,
086: List defaults) {
087: addBasicDefaults(addon, defaults);
088: }
089:
090: /**
091: * Default implementation calls {@link #addBasicDefaults(LookAndFeelAddons, List)}
092: *
093: * @param addon
094: * @param defaults
095: */
096: protected void addWindowsDefaults(LookAndFeelAddons addon,
097: List defaults) {
098: addBasicDefaults(addon, defaults);
099: }
100:
101: /**
102: * Gets the defaults for the given addon.
103: *
104: * Based on the addon, it calls
105: * {@link #addMacDefaults(LookAndFeelAddons, List)} if isMac()
106: * or
107: * {@link #addMetalDefaults(LookAndFeelAddons, List)} if isMetal()
108: * or
109: * {@link #addMotifDefaults(LookAndFeelAddons, List)} if isMotif()
110: * or
111: * {@link #addWindowsDefaults(LookAndFeelAddons, List)} if isWindows()
112: * or
113: * {@link #addBasicDefaults(LookAndFeelAddons, List)} if none of the above was called.
114: * @param addon
115: * @return an array of key/value pairs. For example:
116: * <pre>
117: * Object[] uiDefaults = {
118: * "Font", new Font("Dialog", Font.BOLD, 12),
119: * "Color", Color.red,
120: * "five", new Integer(5)
121: * };
122: * </pre>
123: */
124: private Object[] getDefaults(LookAndFeelAddons addon) {
125: List defaults = new ArrayList();
126: if (isWindows(addon)) {
127: addWindowsDefaults(addon, defaults);
128: } else if (isMetal(addon)) {
129: addMetalDefaults(addon, defaults);
130: } else if (isMac(addon)) {
131: addMacDefaults(addon, defaults);
132: } else if (isMotif(addon)) {
133: addMotifDefaults(addon, defaults);
134: } else {
135: // at least add basic defaults
136: addBasicDefaults(addon, defaults);
137: }
138: return defaults.toArray();
139: }
140:
141: //
142: // Helper methods to make ComponentAddon developer life easier
143: //
144:
145: /**
146: * Adds the all keys/values from the given named resource bundle to the
147: * defaults
148: */
149: protected void addResource(List defaults, String bundleName) {
150: ResourceBundle bundle = ResourceBundle.getBundle(bundleName);
151: for (Enumeration keys = bundle.getKeys(); keys
152: .hasMoreElements();) {
153: String key = (String) keys.nextElement();
154: defaults.add(key);
155: defaults.add(bundle.getObject(key));
156: }
157: }
158:
159: /**
160: * @return true if the addon is the Windows addon or its subclasses
161: */
162: protected boolean isWindows(LookAndFeelAddons addon) {
163: return addon instanceof WindowsLookAndFeelAddons;
164: }
165:
166: /**
167: * @return true if the addon is the Metal addon or its subclasses
168: */
169: protected boolean isMetal(LookAndFeelAddons addon) {
170: return addon instanceof MetalLookAndFeelAddons;
171: }
172:
173: /**
174: * @return true if the addon is the Aqua addon or its subclasses
175: */
176: protected boolean isMac(LookAndFeelAddons addon) {
177: return addon instanceof AquaLookAndFeelAddons;
178: }
179:
180: /**
181: * @return true if the addon is the Motif addon or its subclasses
182: */
183: protected boolean isMotif(LookAndFeelAddons addon) {
184: return addon instanceof MotifLookAndFeelAddons;
185: }
186:
187: /**
188: * @return true if the current look and feel is one of JGoodies Plastic l&fs
189: */
190: protected boolean isPlastic() {
191: return UIManager.getLookAndFeel().getClass().getName().indexOf(
192: "Plastic") != -1;
193: }
194:
195: /**
196: * @return true if the current look and feel is Synth l&f
197: */
198: protected boolean isSynth() {
199: return UIManager.getLookAndFeel().getClass().getName().indexOf(
200: "ynth") != -1;
201: }
202:
203: protected Font getFont(String key, Font defaultFont) {
204: Font result = UIManager.getFont(key);
205: return result != null ? result : defaultFont;
206: }
207:
208: protected Color getColor(String key, Color defaultColor) {
209: Color result = UIManager.getColor(key);
210: return result != null ? result : defaultColor;
211: }
212:
213: }
|