001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018: // 02111-1307, USA.
019: package org.columba.core.gui.themes;
020:
021: import javax.swing.JFrame;
022: import javax.swing.JOptionPane;
023: import javax.swing.SwingUtilities;
024: import javax.swing.UIManager;
025:
026: import org.columba.api.plugin.IExtension;
027: import org.columba.api.plugin.IExtensionHandler;
028: import org.columba.api.plugin.IExtensionHandlerKeys;
029: import org.columba.core.base.OSInfo;
030: import org.columba.core.config.Config;
031: import org.columba.core.gui.frame.FrameManager;
032: import org.columba.core.gui.themes.plugin.AbstractThemePlugin;
033: import org.columba.core.logging.Logging;
034: import org.columba.core.plugin.PluginManager;
035: import org.columba.core.xml.XmlElement;
036:
037: /**
038: * Switch between Look and Feels.
039: * <p>
040: * L&F and feels are loaded as plugins.
041: * <p>
042: *
043: * @see org.columba.core.gui.themes.plugin.AbstractThemePlugin
044: *
045: * @author fdietz
046: *
047: */
048: public class ThemeSwitcher {
049:
050: private static final String QUAQUA_LF = "Quaqua";
051: private static final String DEFAULT_LF = "Plastic";
052:
053: public static void setTheme() {
054: // get configuration
055: XmlElement themeConfig = Config.getInstance().get("options")
056: .getElement("/options/gui/theme");
057:
058: if (themeConfig == null) {
059: XmlElement themeParent = Config.getInstance()
060: .get("options").getElement("/options/gui");
061: themeConfig = new XmlElement("theme");
062: themeParent.addElement(themeConfig);
063: }
064:
065: String pluginName = null;
066: try {
067: // get plugin-handler
068: IExtensionHandler handler = PluginManager
069: .getInstance()
070: .getExtensionHandler(
071: IExtensionHandlerKeys.ORG_COLUMBA_CORE_THEME);
072:
073: pluginName = themeConfig.getAttribute("name");
074:
075: // if no theme available -> set "Plastic" as default
076: if (pluginName == null) {
077: pluginName = ThemeSwitcher.getPlatformDefaultTheme();
078: themeConfig.addAttribute("name", DEFAULT_LF);
079: themeConfig.addAttribute("theme", "Experience Blue");
080: }
081:
082: AbstractThemePlugin theme = null;
083:
084: IExtension extension = handler.getExtension(pluginName);
085:
086: // instanciate theme
087: theme = (AbstractThemePlugin) extension
088: .instanciateExtension(null);
089:
090: // apply theme
091: theme.setLookAndFeel();
092: } catch (Exception ex) {
093:
094: if (Logging.DEBUG)
095: ex.printStackTrace();
096:
097: JOptionPane.showMessageDialog(FrameManager.getInstance()
098: .getActiveFrame(), "Error while trying to load "
099: + pluginName
100: + " Look and Feel.\nSwitching back to default.");
101:
102: try {
103: // fall-back
104: UIManager.setLookAndFeel(UIManager
105: .getCrossPlatformLookAndFeelClassName());
106: } catch (Exception e) {
107: e.printStackTrace();
108: }
109: }
110: } /*
111: * Gets the platform specific default theme. This is in all cases but
112: * MacOS X the Plastic theme. On MacOs X we use the System L&F.
113: *
114: */
115:
116: public static String getPlatformDefaultTheme() {
117: if (OSInfo.isMac()) {
118: return QUAQUA_LF;
119: } else {
120: return DEFAULT_LF;
121: }
122: }
123:
124: public static void updateFrame(JFrame frame) {
125: final JFrame f = frame;
126:
127: SwingUtilities.invokeLater(new Runnable() {
128:
129: public void run() {
130: SwingUtilities.updateComponentTreeUI(f);
131: }
132: });
133: }
134: }
|