001: package net.sourceforge.squirrel_sql.plugins.laf;
002:
003: /*
004: * Copyright (C) 2003-2006 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or any later version.
011: *
012: * This program 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
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021: import java.util.Iterator;
022:
023: import javax.swing.LookAndFeel;
024: import javax.swing.plaf.metal.MetalLookAndFeel;
025: import javax.swing.plaf.metal.MetalTheme;
026:
027: import net.sourceforge.squirrel_sql.fw.util.BaseException;
028: import net.sourceforge.squirrel_sql.fw.util.DuplicateObjectException;
029: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
030: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
031: import net.sourceforge.squirrel_sql.fw.xml.XMLObjectCache;
032:
033: /**
034: * Behaviour for the jGoodies Plastic Look and Feel.
035: *
036: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
037: */
038: class PlasticLookAndFeelController extends AbstractPlasticController {
039: /** Logger for this class. */
040: private static ILogger s_log = LoggerController
041: .createLogger(PlasticLookAndFeelController.class);
042:
043: /**
044: * Look and Feel class names that this controller is responsible for.
045: */
046: static final String[] LAF_CLASS_NAMES = new String[] {
047: "com.jgoodies.looks.plastic.PlasticLookAndFeel",
048: "com.jgoodies.looks.plastic.Plastic3DLookAndFeel",
049: "com.jgoodies.looks.plastic.PlasticXPLookAndFeel", };
050:
051: public static final String DEFAULT_LOOK_AND_FEEL_CLASS_NAME = LAF_CLASS_NAMES[1];
052:
053: /** Base class for all Plastic themes. */
054: private static final String THEME_BASE_CLASS = "com.jgoodies.looks.plastic.PlasticTheme";
055:
056: /** Preferences for this LAF. */
057: private PlasticThemePreferences _prefs;
058:
059: /**
060: * Ctor specifying the Look and Feel plugin and register.
061: *
062: * @param plugin The plugin that this controller is a part of.
063: * @param lafRegister LAF register.
064: */
065: PlasticLookAndFeelController(LAFPlugin plugin,
066: LAFRegister lafRegister) {
067: super (plugin, lafRegister);
068: try {
069:
070: XMLObjectCache cache = plugin.getSettingsCache();
071: Iterator<?> it = cache
072: .getAllForClass(PlasticThemePreferences.class);
073: if (it.hasNext()) {
074: _prefs = (PlasticThemePreferences) it.next();
075: } else {
076: _prefs = new PlasticThemePreferences();
077:
078: ClassLoader cl = getLAFRegister()
079: .getLookAndFeelClassLoader();
080: Class<?> clazz = Class
081: .forName(
082: AbstractPlasticController.DEFAULT_PLASTIC_THEME_CLASS_NAME,
083: false, cl);
084: MetalTheme theme = (MetalTheme) clazz.newInstance();
085: _prefs.setThemeName(theme.getName());
086:
087: try {
088: cache.add(_prefs);
089: } catch (DuplicateObjectException ex) {
090: s_log
091: .error(
092: "PlasticThemePreferences object already in XMLObjectCache",
093: ex);
094: }
095: }
096: } catch (Exception e) {
097: throw new RuntimeException(e);
098: }
099: }
100:
101: /**
102: * Retrieve the name of the current theme.
103: *
104: * @return Name of the current theme.
105: */
106: String getCurrentThemeName() {
107: return _prefs.getThemeName();
108: }
109:
110: /**
111: * Set the current theme name.
112: *
113: * @param name name of the current theme.
114: */
115: void setCurrentThemeName(String name) {
116: _prefs.setThemeName(name);
117: }
118:
119: void installCurrentTheme(LookAndFeel laf, MetalTheme theme)
120: throws BaseException {
121: try {
122: ClassLoader cl = getLAFRegister()
123: .getLookAndFeelClassLoader();
124: Class<?> themeBaseClass;
125: try {
126: themeBaseClass = Class.forName(THEME_BASE_CLASS, false,
127: cl);
128: } catch (Throwable th) {
129: s_log.error("Error loading theme base class "
130: + THEME_BASE_CLASS, th);
131: throw new BaseException(th);
132: }
133:
134: // Ensure that this is a Plastic Theme.
135: if (!themeBaseClass.isAssignableFrom(theme.getClass())) {
136: throw new BaseException("NonPlastic Theme passed in");
137: }
138:
139: // Note: which jar is used is specified in LAFPluginResources.properties
140: // try
141: // {
142: // // This works for the old looks-1.3.1.jar
143: // Method method = laf.getClass().getMethod("setMyCurrentTheme", new Class[] { themeBaseClass });
144: // Object[] parms = new Object[] { theme };
145: // method.invoke(laf, parms);
146: // }
147: // catch (NoSuchMethodException e)
148: // {
149: // // This works for the newer looks.jar
150: // MetalLookAndFeel.setCurrentTheme(theme);
151: // }
152: MetalLookAndFeel.setCurrentTheme(theme);
153: } catch (Throwable th) {
154: throw new BaseException(th);
155: }
156: }
157:
158: /**
159: * Preferences for the Plastic LAFs. Subclassed purely to give it a
160: * different data type when stored in the plugin preferences so that it
161: * doesn't get mixed up with preferences for other subclasses of
162: * AbstractPlasticController
163: */
164: public static final class PlasticThemePreferences extends
165: AbstractPlasticController.ThemePreferences {
166: }
167: }
|