001: package net.sourceforge.squirrel_sql.plugins.laf;
002:
003: /*
004: * Copyright (C) 2001-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.io.File;
022: import java.io.FileNotFoundException;
023: import java.io.IOException;
024: import java.util.Iterator;
025:
026: import javax.swing.JTabbedPane;
027:
028: import com.jgoodies.looks.Options;
029:
030: import net.sourceforge.squirrel_sql.fw.util.DuplicateObjectException;
031: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
032: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
033: import net.sourceforge.squirrel_sql.fw.xml.XMLBeanReader;
034: import net.sourceforge.squirrel_sql.fw.xml.XMLException;
035: import net.sourceforge.squirrel_sql.fw.xml.XMLObjectCache;
036:
037: import net.sourceforge.squirrel_sql.client.IApplication;
038: import net.sourceforge.squirrel_sql.client.gui.builders.UIFactory;
039: import net.sourceforge.squirrel_sql.client.gui.builders.UIFactoryAdapter;
040: import net.sourceforge.squirrel_sql.client.gui.builders.UIFactoryComponentCreatedEvent;
041: import net.sourceforge.squirrel_sql.client.plugin.DefaultPlugin;
042: import net.sourceforge.squirrel_sql.client.plugin.PluginException;
043: import net.sourceforge.squirrel_sql.client.plugin.PluginResources;
044: import net.sourceforge.squirrel_sql.client.preferences.IGlobalPreferencesPanel;
045: import net.sourceforge.squirrel_sql.client.util.IdentifierFactory;
046:
047: /**
048: * The Look and Feel plugin class.
049: *
050: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
051: */
052: public class LAFPlugin extends DefaultPlugin {
053: /** Logger for this class. */
054: private final static ILogger s_log = LoggerController
055: .createLogger(LAFPlugin.class);
056:
057: /** Old name of file to store user prefs in. Replaced by USER_PREFS_FILE_NAME. */
058: static final String OLD_USER_PREFS_FILE_NAME = "LAFPrefs.xml";
059:
060: /** Name of file to store user prefs in. */
061: static final String USER_PREFS_FILE_NAME = "LAFPreferences.xml";
062:
063: /** Resources for this plugin. */
064: private LAFPluginResources _resources;
065:
066: /** Plugin preferences. */
067: private LAFPreferences _lafPrefs;
068:
069: /** A register of Look and Feels. */
070: private LAFRegister _lafRegister;
071:
072: /** The folder that contains LAF jars. */
073: private File _lafFolder;
074:
075: /** Folder to store user settings in. */
076: private File _userSettingsFolder;
077:
078: /** Folder to store extra LAFs supplied by the user. */
079: private File _userExtraLAFFolder;
080:
081: /** Cache of settings for the plugin. */
082: private final XMLObjectCache<LAFPreferences> _settingsCache = new XMLObjectCache<LAFPreferences>();
083:
084: /**
085: * Return the internal name of this plugin.
086: *
087: * @return the internal name of this plugin.
088: */
089: public String getInternalName() {
090: return "laf";
091: }
092:
093: /**
094: * Return the descriptive name of this plugin.
095: *
096: * @return the descriptive name of this plugin.
097: */
098: public String getDescriptiveName() {
099: return "Look & Feel Plugin";
100: }
101:
102: /**
103: * Returns the current version of this plugin.
104: *
105: * @return the current version of this plugin.
106: */
107: public String getVersion() {
108: return "1.1";
109: }
110:
111: /**
112: * Returns the authors name.
113: *
114: * @return the authors name.
115: */
116: public String getAuthor() {
117: return "Colin Bell";
118: }
119:
120: /**
121: * Returns the name of the change log for the plugin. This should
122: * be a text or HTML file residing in the <TT>getPluginAppSettingsFolder</TT>
123: * directory.
124: *
125: * @return the changelog file name or <TT>null</TT> if plugin doesn't have
126: * a change log.
127: */
128: public String getChangeLogFileName() {
129: return "changes.txt";
130: }
131:
132: /**
133: * Returns the name of the Help file for the plugin. This should
134: * be a text or HTML file residing in the <TT>getPluginAppSettingsFolder</TT>
135: * directory.
136: *
137: * @return the Help file name or <TT>null</TT> if plugin doesn't have
138: * a help file.
139: */
140: public String getHelpFileName() {
141: return "readme.html";
142: }
143:
144: /**
145: * Returns the name of the Licence file for the plugin. This should
146: * be a text or HTML file residing in the <TT>getPluginAppSettingsFolder</TT>
147: * directory.
148: *
149: * @return the Licence file name or <TT>null</TT> if plugin doesn't have
150: * a licence file.
151: */
152: public String getLicenceFileName() {
153: return "licences.html";
154: }
155:
156: /**
157: * Load this plugin.
158: *
159: * @param app Application API.
160: */
161: public synchronized void load(IApplication app)
162: throws PluginException {
163: super .load(app);
164:
165: // Load resources.
166: _resources = new LAFPluginResources(this );
167:
168: // Folder within plugins folder that belongs to this
169: // plugin.
170: File pluginAppFolder = null;
171: try {
172: pluginAppFolder = getPluginAppSettingsFolder();
173: } catch (IOException ex) {
174: throw new PluginException(ex);
175: }
176:
177: // Folder that stores Look and Feel jars.
178: _lafFolder = new File(pluginAppFolder, "lafs");
179: if (!_lafFolder.exists()) {
180: _lafFolder.mkdir();
181: }
182:
183: // Folder to store user settings.
184: try {
185: _userSettingsFolder = getPluginUserSettingsFolder();
186: } catch (IOException ex) {
187: throw new PluginException(ex);
188: }
189:
190: // Folder to contain extra LAFs supplied by the user.
191: _userExtraLAFFolder = new File(_userSettingsFolder,
192: ILAFConstants.USER_EXTRA_LAFS_FOLDER);
193:
194: // Create empty required files in user settings directory.
195: createEmptyRequiredUserFiles();
196:
197: // Load plugin preferences.
198: loadPrefs();
199:
200: // Setup preferences to make jGoodies LAF look better.
201: // UIManager.put(Options.USE_SYSTEM_FONTS_APP_KEY, Boolean.TRUE);
202: // UIManager.put(Options.USE_NARROW_BUTTONS_KEY, Boolean.TRUE);
203:
204: // Create the Look and Feel register.
205: _lafRegister = new LAFRegister(app, this );
206:
207: // Listen for GUI components being created.
208: UIFactory.getInstance().addListener(new UIFactoryListener());
209:
210: // Update font used for status bars.
211: _lafRegister.updateStatusBarFont();
212: }
213:
214: /**
215: * Application is shutting down so save preferences.
216: */
217: public void unload() {
218: try {
219: savePrefs(new File(_userSettingsFolder,
220: USER_PREFS_FILE_NAME));
221: } catch (IOException ex) {
222: s_log.error("Error occured writing to preferences file: "
223: + USER_PREFS_FILE_NAME, ex);
224: } catch (XMLException ex) {
225: s_log.error("Error occured writing to preferences file: "
226: + USER_PREFS_FILE_NAME, ex);
227: }
228: super .unload();
229: }
230:
231: /**
232: * Create Look and Feel preferences panels for the Global Preferences dialog.
233: *
234: * @return Look and Feel preferences panels.
235: */
236: public IGlobalPreferencesPanel[] getGlobalPreferencePanels() {
237: return new IGlobalPreferencesPanel[] {
238: new LAFPreferencesTab(this , _lafRegister),
239: new LAFFontsTab(this , _lafRegister), };
240: }
241:
242: /**
243: * Return the folder that contains LAF jars.
244: *
245: * @return folder as <TT>File</TT> that contains LAF jars.
246: */
247: File getLookAndFeelFolder() {
248: return _lafFolder;
249: }
250:
251: /**
252: * Retrieve the directory that contains the extra LAFs supplied
253: * by the user.
254: *
255: * @return folder as <TT>File</TT> that contains the extra LAFs supplied
256: * by the user.
257: */
258: File getUsersExtraLAFFolder() {
259: return _userExtraLAFFolder;
260: }
261:
262: /**
263: * Get the preferences info object for this plugin.
264: *
265: * @return The preferences info object for this plugin.
266: */
267: LAFPreferences getLAFPreferences() {
268: return _lafPrefs;
269: }
270:
271: /**
272: * Retrieve plugins resources.
273: *
274: * @return Plugins resources.
275: */
276: PluginResources getResources() {
277: return _resources;
278: }
279:
280: XMLObjectCache<LAFPreferences> getSettingsCache() {
281: return _settingsCache;
282: }
283:
284: /**
285: * Load from preferences file.
286: */
287: private void loadPrefs() {
288: final File oldPrefsFile = new File(_userSettingsFolder,
289: OLD_USER_PREFS_FILE_NAME);
290: final File newPrefsFile = new File(_userSettingsFolder,
291: USER_PREFS_FILE_NAME);
292: final boolean oldExists = oldPrefsFile.exists();
293: final boolean newExists = newPrefsFile.exists();
294:
295: try {
296: if (oldExists) {
297: loadOldPrefs(oldPrefsFile);
298: try {
299: _settingsCache.add(_lafPrefs);
300: } catch (DuplicateObjectException ex) {
301: s_log.error(
302: "LAFPreferences object already in cache",
303: ex);
304: }
305: savePrefs(newPrefsFile);
306: if (!oldPrefsFile.delete()) {
307: s_log
308: .error("Unable to delete old LAF preferences file");
309: }
310:
311: } else if (newExists) {
312: loadNewPrefs(newPrefsFile);
313: }
314: } catch (IOException ex) {
315: s_log.error("Error occured in preferences file", ex);
316: } catch (XMLException ex) {
317: s_log.error("Error occured in preferences file", ex);
318: }
319:
320: if (_lafPrefs == null) {
321: _lafPrefs = new LAFPreferences(IdentifierFactory
322: .getInstance().createIdentifier());
323: _lafPrefs
324: .setLookAndFeelClassName(PlasticLookAndFeelController.DEFAULT_LOOK_AND_FEEL_CLASS_NAME);
325: try {
326: _settingsCache.add(_lafPrefs);
327: } catch (DuplicateObjectException ex) {
328: s_log.error("LAFPreferences object already in cache",
329: ex);
330: }
331: }
332: }
333:
334: /**
335: * Load preferences from the old file format.
336: *
337: * @param oldPrefsFile File containing the preferences info.
338: *
339: * @throws XMLException Thrown if an error occurs eradign the rpeferences data.
340: */
341: private void loadOldPrefs(File oldPrefsFile) throws XMLException {
342: try {
343: XMLBeanReader doc = new XMLBeanReader();
344: doc.load(oldPrefsFile, getClass().getClassLoader());
345: Iterator<?> it = doc.iterator();
346: if (it.hasNext()) {
347: _lafPrefs = (LAFPreferences) it.next();
348: }
349: } catch (FileNotFoundException ignore) {
350: // property file not found for user - first time user ran pgm.
351: }
352: }
353:
354: /**
355: * Load preferences from the new file format.
356: *
357: * @param newPerfsFile File containing the preferences information.
358: *
359: * @throws XMLException Thrown if error reading preferences file.
360: */
361: private void loadNewPrefs(File newPrefsFile) throws XMLException {
362: try {
363: try {
364: _settingsCache.load(newPrefsFile.getPath(), getClass()
365: .getClassLoader());
366: } catch (DuplicateObjectException ex) {
367: s_log.error("Cache should have been empty", ex);
368: }
369: Iterator<LAFPreferences> it = _settingsCache
370: .getAllForClass(LAFPreferences.class);
371: if (it.hasNext()) {
372: _lafPrefs = it.next();
373: } else {
374: s_log.error("LAFPreferences object not loaded");
375: }
376: } catch (FileNotFoundException ignore) {
377: // property file not found for user - first time user ran pgm.
378: }
379: }
380:
381: /**
382: * Save preferences to disk.
383: *
384: * @param prefsFile File to save preferences to.
385: */
386: private void savePrefs(File prefsFile) throws IOException,
387: XMLException {
388: _settingsCache.save(prefsFile.getPath());
389: }
390:
391: private void createEmptyRequiredUserFiles() {
392: _userExtraLAFFolder.mkdirs();
393:
394: File file = new File(_userExtraLAFFolder,
395: ILAFConstants.USER_EXTRA_LAFS_PROPS_FILE);
396: try {
397: file.createNewFile();
398: } catch (IOException ex) {
399: s_log
400: .error("Error creating file "
401: + file.getAbsolutePath(), ex);
402: }
403: }
404:
405: private static class UIFactoryListener extends UIFactoryAdapter {
406: /**
407: * A tabbed panel object has been created.
408: *
409: * @param evt event object.
410: */
411: public void tabbedPaneCreated(UIFactoryComponentCreatedEvent evt) {
412: final JTabbedPane pnl = (JTabbedPane) evt.getComponent();
413: pnl.putClientProperty(Options.NO_CONTENT_BORDER_KEY,
414: Boolean.TRUE);
415: }
416: }
417: }
|