001: package net.sourceforge.squirrel_sql.plugins.laf;
002:
003: /*
004: * Copyright (C) 2002-2003 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.awt.GridBagConstraints;
022: import java.awt.GridBagLayout;
023: import java.awt.Insets;
024: import java.io.File;
025: import java.io.IOException;
026: import java.lang.reflect.Method;
027: import java.util.Iterator;
028:
029: import javax.swing.JLabel;
030: import javax.swing.LookAndFeel;
031: import javax.swing.SwingConstants;
032:
033: import net.sourceforge.squirrel_sql.fw.gui.DirectoryListComboBox;
034: import net.sourceforge.squirrel_sql.fw.gui.OutputLabel;
035: import net.sourceforge.squirrel_sql.fw.id.IHasIdentifier;
036: import net.sourceforge.squirrel_sql.fw.id.IIdentifier;
037: import net.sourceforge.squirrel_sql.fw.id.IntegerIdentifier;
038: import net.sourceforge.squirrel_sql.fw.util.DuplicateObjectException;
039: import net.sourceforge.squirrel_sql.fw.util.FileExtensionFilter;
040: import net.sourceforge.squirrel_sql.fw.util.StringManager;
041: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
042: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
043: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
044: import net.sourceforge.squirrel_sql.fw.xml.XMLObjectCache;
045:
046: /**
047: * Behaviour for the Skin Look and Feel.
048: *
049: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
050: */
051: public class SkinLookAndFeelController extends
052: DefaultLookAndFeelController {
053: private static final StringManager s_stringMgr = StringManagerFactory
054: .getStringManager(SkinLookAndFeelController.class);
055:
056: /** Logger for this class. */
057: private static final ILogger s_log = LoggerController
058: .createLogger(SkinLookAndFeelController.class);
059:
060: /** Class name of the Skin Look and Feel. */
061: public static final String SKINNABLE_LAF_CLASS_NAME = "com.l2fprod.gui.plaf.skin.SkinLookAndFeel";
062:
063: /** Class name of the Skin class. */
064: public static final String SKIN_CLASS_NAME = "com.l2fprod.gui.plaf.skin.Skin";
065:
066: /** Preferences for this LAF. */
067: private SkinPreferences _prefs;
068:
069: /**
070: * Ctor specifying the Look and Feel plugin.
071: *
072: * @param plugin The plugin that this controller is a part of.
073: */
074: SkinLookAndFeelController(LAFPlugin plugin) throws IOException {
075: super ();
076:
077: XMLObjectCache cache = plugin.getSettingsCache();
078: Iterator<?> it = cache.getAllForClass(SkinPreferences.class);
079: if (it.hasNext()) {
080: _prefs = (SkinPreferences) it.next();
081: } else {
082: _prefs = new SkinPreferences();
083: try {
084: cache.add(_prefs);
085: } catch (DuplicateObjectException ex) {
086: s_log
087: .error(
088: "SkinPreferences object already in XMLObjectCache",
089: ex);
090: }
091: }
092:
093: // Folder that stores themepacks for this LAF.
094: File themePackDir = new File(plugin
095: .getPluginAppSettingsFolder(), "skinlf-theme-packs");
096: _prefs.setThemePackDirectory(themePackDir.getAbsolutePath());
097: if (!themePackDir.exists()) {
098: themePackDir.mkdirs();
099: }
100: }
101:
102: /**
103: * This Look and Feel is about to be installed. Load the selected
104: * themepack.
105: */
106: public void aboutToBeInstalled(LAFRegister lafRegister,
107: LookAndFeel laf) {
108: try {
109: final String dir = _prefs.getThemePackDirectory();
110: final String name = _prefs.getThemePackName();
111: if (dir != null && name != null) {
112: File themePackFile = new File(dir, name);
113: if (themePackFile.exists()) {
114: ClassLoader cl = lafRegister
115: .getLookAndFeelClassLoader();
116: Class<?> skinLafClass = Class.forName(
117: SKINNABLE_LAF_CLASS_NAME, false, cl);
118: Class<?> skinClass = Class.forName(SKIN_CLASS_NAME,
119: false, cl);
120:
121: Method loadThemePack = skinLafClass.getMethod(
122: "loadThemePack",
123: new Class[] { String.class });
124: Method setSkin = skinLafClass.getMethod("setSkin",
125: new Class[] { skinClass });
126:
127: Object[] parms = new Object[] { dir + "/" + name };
128: Object skin = loadThemePack.invoke(skinLafClass,
129: parms);
130: setSkin.invoke(skinLafClass, new Object[] { skin });
131: }
132: }
133: } catch (Throwable th) {
134: s_log.error("Error loading a Skinnable Look and Feel", th);
135: }
136:
137: }
138:
139: /**
140: * This Look and Feel has just been installed.
141: */
142: public void hasBeenInstalled(LAFRegister lafRegister,
143: LookAndFeel laf) {
144: }
145:
146: /**
147: * @see ILookAndFeelController#getPreferencesComponent()
148: */
149: public BaseLAFPreferencesPanelComponent getPreferencesComponent() {
150: return new SkinPrefsPanel(this );
151: }
152:
153: private static final class SkinPrefsPanel extends
154: BaseLAFPreferencesPanelComponent {
155: private static final long serialVersionUID = 1L;
156:
157: /**
158: * This interface defines locale specific strings. This should be
159: * replaced with a property file.
160: */
161: interface SkinPrefsPanelI18n {
162: // i18n[laf.skinThemPack=Theme Pack:]
163: String THEME_PACK = s_stringMgr
164: .getString("laf.skinThemPack");
165: // i18n[laf.skinThemePackDir=Theme Pack Directory:]
166: String THEMEPACK_LOC = s_stringMgr
167: .getString("laf.skinThemePackDir");
168: }
169:
170: private SkinLookAndFeelController _ctrl;
171: private DirectoryListComboBox _themePackCmb = new DirectoryListComboBox();
172:
173: SkinPrefsPanel(SkinLookAndFeelController ctrl) {
174: super (new GridBagLayout());
175: _ctrl = ctrl;
176: createUserInterface();
177: }
178:
179: private void createUserInterface() {
180: final GridBagConstraints gbc = new GridBagConstraints();
181: gbc.anchor = GridBagConstraints.WEST;
182: gbc.fill = GridBagConstraints.HORIZONTAL;
183: gbc.insets = new Insets(4, 4, 4, 4);
184:
185: gbc.gridx = 0;
186: gbc.gridy = 0;
187: add(new JLabel(SkinPrefsPanelI18n.THEME_PACK,
188: SwingConstants.RIGHT), gbc);
189:
190: ++gbc.gridx;
191: add(_themePackCmb, gbc);
192:
193: gbc.gridx = 0;
194: ++gbc.gridy;
195: add(new JLabel(SkinPrefsPanelI18n.THEMEPACK_LOC,
196: SwingConstants.RIGHT), gbc);
197:
198: ++gbc.gridx;
199: final String themePackDir = _ctrl._prefs
200: .getThemePackDirectory();
201: add(new OutputLabel(themePackDir), gbc);
202: }
203:
204: /**
205: * @see BaseLAFPreferencesPanelComponent#loadPreferencesPanel()
206: */
207: public void loadPreferencesPanel() {
208: super .loadPreferencesPanel();
209: final String themePackDir = _ctrl._prefs
210: .getThemePackDirectory();
211: // i18n[laf.jarZip=JAR/Zip files]
212: final FileExtensionFilter filter = new FileExtensionFilter(
213: s_stringMgr.getString("laf.jarZip"), new String[] {
214: ".jar", ".zip" });
215: _themePackCmb.load(new File(themePackDir), filter);
216: _themePackCmb.setSelectedItem(_ctrl._prefs
217: .getThemePackName());
218: if (_themePackCmb.getSelectedIndex() == -1
219: && _themePackCmb.getModel().getSize() > 0) {
220: _themePackCmb.setSelectedIndex(0);
221: }
222: }
223:
224: /**
225: * @see BaseLAFPreferencesPanelComponent#applyChanges()
226: */
227: public boolean applyChanges() {
228: super .applyChanges();
229: _ctrl._prefs.setThemePackName((String) _themePackCmb
230: .getSelectedItem());
231:
232: // Force the LAF to be set even if Skin is the current one. This
233: // allows a change in theme to take affect.
234: return true;
235: }
236: }
237:
238: public static final class SkinPreferences implements IHasIdentifier {
239: private String _themePackDir;
240: private String _themePackName;
241: private IntegerIdentifier _id = new IntegerIdentifier(1);
242:
243: public String getThemePackDirectory() {
244: return _themePackDir;
245: }
246:
247: public void setThemePackDirectory(String value) {
248: _themePackDir = value;
249: }
250:
251: public String getThemePackName() {
252: return _themePackName;
253: }
254:
255: public void setThemePackName(String value) {
256: _themePackName = value;
257: }
258:
259: /**
260: * @return The unique identifier for this object.
261: */
262: public IIdentifier getIdentifier() {
263: return _id;
264: }
265: }
266: }
|