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