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