001: /**
002: * L2FProd.com Common Components 7.3 License.
003: *
004: * Copyright 2005-2007 L2FProd.com
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package com.l2fprod.common.swing.plaf;
018:
019: import com.l2fprod.common.swing.plaf.aqua.AquaLookAndFeelAddons;
020: import com.l2fprod.common.swing.plaf.metal.MetalLookAndFeelAddons;
021: import com.l2fprod.common.swing.plaf.windows.WindowsClassicLookAndFeelAddons;
022: import com.l2fprod.common.swing.plaf.windows.WindowsLookAndFeelAddons;
023: import com.l2fprod.common.util.OS;
024:
025: import java.beans.PropertyChangeEvent;
026: import java.beans.PropertyChangeListener;
027: import java.lang.reflect.Method;
028: import java.util.ArrayList;
029: import java.util.Iterator;
030: import java.util.List;
031:
032: import javax.swing.JComponent;
033: import javax.swing.UIManager;
034: import javax.swing.plaf.ComponentUI;
035: import javax.swing.plaf.metal.MetalLookAndFeel;
036:
037: /**
038: * Provides additional pluggable UI for new components added by the
039: * library. By default, the library uses the pluggable UI returned by
040: * {@link #getBestMatchAddonClassName()}.
041: * <p>
042: * The default addon can be configured using the
043: * <code>swing.addon</code> system property as follow:
044: * <ul>
045: * <li>on the command line,
046: * <code>java -Dswing.addon=ADDONCLASSNAME ...</code></li>
047: * <li>at runtime and before using the library components
048: * <code>System.getProperties().put("swing.addon", ADDONCLASSNAME);</code>
049: * </li>
050: * </ul>
051: * <p>
052: * The addon can also be installed directly by calling the
053: * {@link #setAddon(String)}method. For example, to install the
054: * Windows addons, add the following statement
055: * <code>LookAndFeelAddons.setAddon("com.l2fprod.common.swing.plaf.windows.WindowsLookAndFeelAddons");</code>.
056: *
057: * @author <a href="mailto:fred@L2FProd.com">Frederic Lavigne</a>
058: */
059: public class LookAndFeelAddons {
060:
061: private static List contributedComponents = new ArrayList();
062:
063: /**
064: * Key used to ensure the current UIManager has been populated by the
065: * LookAndFeelAddons.
066: */
067: private static final Object APPCONTEXT_INITIALIZED = new Object();
068:
069: private static boolean trackingChanges = false;
070: private static PropertyChangeListener changeListener;
071:
072: static {
073: // load the default addon
074: String addonClassname = getBestMatchAddonClassName();
075: try {
076: addonClassname = System.getProperty("swing.addon",
077: addonClassname);
078: } catch (SecurityException e) {
079: // security exception may arise in Java Web Start
080: }
081:
082: try {
083: setAddon(addonClassname);
084: setTrackingLookAndFeelChanges(true);
085: } catch (InstantiationException e) {
086: e.printStackTrace();
087: } catch (IllegalAccessException e) {
088: e.printStackTrace();
089: } catch (ClassNotFoundException e) {
090: e.printStackTrace();
091: }
092: }
093:
094: private static LookAndFeelAddons currentAddon;
095:
096: public void initialize() {
097: for (Iterator iter = contributedComponents.iterator(); iter
098: .hasNext();) {
099: ComponentAddon addon = (ComponentAddon) iter.next();
100: addon.initialize(this );
101: }
102: }
103:
104: public void uninitialize() {
105: for (Iterator iter = contributedComponents.iterator(); iter
106: .hasNext();) {
107: ComponentAddon addon = (ComponentAddon) iter.next();
108: addon.uninitialize(this );
109: }
110: }
111:
112: /**
113: * Adds the given defaults in UIManager.
114: *
115: * Note: the values are added only if they do not exist in the existing look
116: * and feel defaults. This makes it possible for look and feel implementors to
117: * override library defaults.
118: *
119: * Note: the array is traversed in reverse order. If a key is found twice in
120: * the array, the key/value with the highest position in the array gets
121: * precedence over the other key in the array
122: *
123: * @param keysAndValues
124: */
125: public void loadDefaults(Object[] keysAndValues) {
126: // Go in reverse order so the most recent keys get added first...
127: for (int i = keysAndValues.length - 2; i >= 0; i = i - 2) {
128: if (UIManager.getLookAndFeelDefaults()
129: .get(keysAndValues[i]) == null) {
130: UIManager.getLookAndFeelDefaults().put(
131: keysAndValues[i], keysAndValues[i + 1]);
132: }
133: }
134: }
135:
136: public void unloadDefaults(Object[] keysAndValues) {
137: for (int i = 0, c = keysAndValues.length; i < c; i = i + 2) {
138: UIManager.getLookAndFeelDefaults().put(keysAndValues[i],
139: null);
140: }
141: }
142:
143: public static void setAddon(String addonClassName)
144: throws InstantiationException, IllegalAccessException,
145: ClassNotFoundException {
146: setAddon(Class.forName(addonClassName));
147: }
148:
149: public static void setAddon(Class addonClass)
150: throws InstantiationException, IllegalAccessException {
151: LookAndFeelAddons addon = (LookAndFeelAddons) addonClass
152: .newInstance();
153: setAddon(addon);
154: }
155:
156: public static void setAddon(LookAndFeelAddons addon) {
157: if (currentAddon != null) {
158: currentAddon.uninitialize();
159: }
160:
161: addon.initialize();
162: currentAddon = addon;
163: UIManager.put(APPCONTEXT_INITIALIZED, Boolean.TRUE);
164: }
165:
166: public static LookAndFeelAddons getAddon() {
167: return currentAddon;
168: }
169:
170: /**
171: * Based on the current look and feel (as returned by
172: * <code>UIManager.getLookAndFeel()</code>), this method returns
173: * the name of the closest <code>LookAndFeelAddons</code> to use.
174: *
175: * @return the addon matching the currently installed look and feel
176: */
177: public static String getBestMatchAddonClassName() {
178: String lnf = UIManager.getLookAndFeel().getClass().getName();
179: String addon;
180: if (UIManager.getCrossPlatformLookAndFeelClassName()
181: .equals(lnf)) {
182: addon = MetalLookAndFeelAddons.class.getName();
183: } else if (UIManager.getSystemLookAndFeelClassName()
184: .equals(lnf)) {
185: addon = getSystemAddonClassName();
186: } else if ("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"
187: .equals(lnf)
188: || "com.jgoodies.looks.windows.WindowsLookAndFeel"
189: .equals(lnf)) {
190: if (OS.isUsingWindowsVisualStyles()) {
191: addon = WindowsLookAndFeelAddons.class.getName();
192: } else {
193: addon = WindowsClassicLookAndFeelAddons.class.getName();
194: }
195: } else if ("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel"
196: .equals(lnf)) {
197: addon = WindowsClassicLookAndFeelAddons.class.getName();
198: } else if (UIManager.getLookAndFeel() instanceof MetalLookAndFeel) {
199: // for JGoodies and other sub-l&fs of Metal
200: addon = MetalLookAndFeelAddons.class.getName();
201: } else {
202: addon = getSystemAddonClassName();
203: }
204: return addon;
205: }
206:
207: /**
208: * Gets the addon best suited for the operating system where the
209: * virtual machine is running.
210: *
211: * @return the addon matching the native operating system platform.
212: */
213: public static String getSystemAddonClassName() {
214: String addon = WindowsClassicLookAndFeelAddons.class.getName();
215:
216: if (OS.isMacOSX()) {
217: addon = AquaLookAndFeelAddons.class.getName();
218: } else if (OS.isWindows()) {
219: // see whether of not visual styles are used
220: if (OS.isUsingWindowsVisualStyles()) {
221: addon = WindowsLookAndFeelAddons.class.getName();
222: } else {
223: addon = WindowsClassicLookAndFeelAddons.class.getName();
224: }
225: }
226:
227: return addon;
228: }
229:
230: /**
231: * Each new component added by the library will contribute its
232: * default UI classes, colors and fonts to the LookAndFeelAddons.
233: * See {@link ComponentAddon}.
234: *
235: * @param component
236: */
237: public static void contribute(ComponentAddon component) {
238: contributedComponents.add(component);
239:
240: if (currentAddon != null) {
241: // make sure to initialize any addons added after the
242: // LookAndFeelAddons has been installed
243: component.initialize(currentAddon);
244: }
245: }
246:
247: /**
248: * Removes the contribution of the given addon
249: *
250: * @param component
251: */
252: public static void uncontribute(ComponentAddon component) {
253: contributedComponents.remove(component);
254:
255: if (currentAddon != null) {
256: component.uninitialize(currentAddon);
257: }
258: }
259:
260: /**
261: * Workaround for IDE mixing up with classloaders and Applets environments.
262: * Consider this method as API private. It must not be called directly.
263: *
264: * @param component
265: * @param expectedUIClass
266: * @return an instance of expectedUIClass
267: */
268: public static ComponentUI getUI(JComponent component,
269: Class expectedUIClass) {
270: maybeInitialize();
271:
272: // solve issue with ClassLoader not able to find classes
273: String uiClassname = (String) UIManager.get(component
274: .getUIClassID());
275: try {
276: Class uiClass = Class.forName(uiClassname);
277: UIManager.put(uiClassname, uiClass);
278: } catch (Exception e) {
279: // we ignore the ClassNotFoundException
280: }
281:
282: ComponentUI ui = UIManager.getUI(component);
283:
284: if (expectedUIClass.isInstance(ui)) {
285: return ui;
286: } else {
287: String realUI = ui.getClass().getName();
288: Class realUIClass;
289: try {
290: realUIClass = expectedUIClass.getClassLoader()
291: .loadClass(realUI);
292: } catch (ClassNotFoundException e) {
293: throw new RuntimeException("Failed to load class "
294: + realUI, e);
295: }
296: Method createUIMethod = null;
297: try {
298: createUIMethod = realUIClass.getMethod("createUI",
299: new Class[] { JComponent.class });
300: } catch (NoSuchMethodException e1) {
301: throw new RuntimeException("Class " + realUI
302: + " has no method createUI(JComponent)");
303: }
304: try {
305: return (ComponentUI) createUIMethod.invoke(null,
306: new Object[] { component });
307: } catch (Exception e2) {
308: throw new RuntimeException("Failed to invoke " + realUI
309: + "#createUI(JComponent)");
310: }
311: }
312: }
313:
314: /**
315: * With applets, if you reload the current applet, the UIManager will be
316: * reinitialized (entries previously added by LookAndFeelAddons will be
317: * removed) but the addon will not reinitialize because addon initialize
318: * itself through the static block in components and the classes do not get
319: * reloaded. This means component.updateUI will fail because it will not find
320: * its UI.
321: *
322: * This method ensures LookAndFeelAddons get re-initialized if needed. It must
323: * be called in every component updateUI methods.
324: */
325: private static synchronized void maybeInitialize() {
326: if (currentAddon != null) {
327: // this is to ensure "UIManager#maybeInitialize" gets called and the
328: // LAFState initialized
329: UIManager.getLookAndFeelDefaults();
330:
331: if (!UIManager.getBoolean(APPCONTEXT_INITIALIZED)) {
332: setAddon(currentAddon);
333: }
334: }
335: }
336:
337: //
338: // TRACKING OF THE CURRENT LOOK AND FEEL
339: //
340: private static class UpdateAddon implements PropertyChangeListener {
341: public void propertyChange(PropertyChangeEvent evt) {
342: try {
343: setAddon(getBestMatchAddonClassName());
344: } catch (Exception e) {
345: // should not happen
346: throw new RuntimeException(e);
347: }
348: }
349: }
350:
351: /**
352: * If true, everytime the Swing look and feel is changed, the addon which
353: * best matches the current look and feel will be automatically selected.
354: *
355: * @param tracking
356: * true to automatically update the addon, false to not automatically
357: * track the addon. Defaults to false.
358: * @see #getBestMatchAddonClassName()
359: */
360: public static synchronized void setTrackingLookAndFeelChanges(
361: boolean tracking) {
362: if (trackingChanges != tracking) {
363: if (tracking) {
364: if (changeListener == null) {
365: changeListener = new UpdateAddon();
366: }
367: UIManager.addPropertyChangeListener(changeListener);
368: } else {
369: if (changeListener != null) {
370: UIManager
371: .removePropertyChangeListener(changeListener);
372: }
373: changeListener = null;
374: }
375: trackingChanges = tracking;
376: }
377: }
378:
379: /**
380: * @return true if the addon will be automatically change to match the current
381: * look and feel
382: * @see #setTrackingLookAndFeelChanges(boolean)
383: */
384: public static synchronized boolean isTrackingLookAndFeelChanges() {
385: return trackingChanges;
386: }
387:
388: }
|