KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > themes > ThemeSwitcher


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18
// 02111-1307, USA.
19
package org.columba.core.gui.themes;
20
21 import javax.swing.JFrame JavaDoc;
22 import javax.swing.JOptionPane JavaDoc;
23 import javax.swing.SwingUtilities JavaDoc;
24 import javax.swing.UIManager JavaDoc;
25
26 import org.columba.api.plugin.IExtension;
27 import org.columba.api.plugin.IExtensionHandler;
28 import org.columba.api.plugin.IExtensionHandlerKeys;
29 import org.columba.core.base.OSInfo;
30 import org.columba.core.config.Config;
31 import org.columba.core.gui.frame.FrameManager;
32 import org.columba.core.gui.themes.plugin.AbstractThemePlugin;
33 import org.columba.core.logging.Logging;
34 import org.columba.core.plugin.PluginManager;
35 import org.columba.core.xml.XmlElement;
36
37 /**
38  * Switch between Look and Feels.
39  * <p>
40  * L&F and feels are loaded as plugins.
41  * <p>
42  *
43  * @see org.columba.core.gui.themes.plugin.AbstractThemePlugin
44  *
45  * @author fdietz
46  *
47  */

48 public class ThemeSwitcher {
49
50     private static final String JavaDoc QUAQUA_LF = "Quaqua";
51     private static final String JavaDoc DEFAULT_LF = "Plastic";
52
53     public static void setTheme() {
54         // get configuration
55
XmlElement themeConfig = Config.getInstance().get("options")
56                 .getElement("/options/gui/theme");
57
58         if (themeConfig == null) {
59             XmlElement themeParent = Config.getInstance().get("options")
60                     .getElement("/options/gui");
61             themeConfig = new XmlElement("theme");
62             themeParent.addElement(themeConfig);
63         }
64
65         String JavaDoc pluginName = null;
66         try {
67             // get plugin-handler
68
IExtensionHandler handler = PluginManager
69                     .getInstance().getExtensionHandler(IExtensionHandlerKeys.ORG_COLUMBA_CORE_THEME);
70
71             pluginName = themeConfig.getAttribute("name");
72
73             // if no theme available -> set "Plastic" as default
74
if (pluginName == null) {
75                 pluginName = ThemeSwitcher.getPlatformDefaultTheme();
76                 themeConfig.addAttribute("name", DEFAULT_LF);
77                 themeConfig.addAttribute("theme", "Experience Blue");
78             }
79
80             AbstractThemePlugin theme = null;
81
82             IExtension extension = handler.getExtension(pluginName);
83
84             // instanciate theme
85
theme = (AbstractThemePlugin) extension.instanciateExtension(null);
86
87             // apply theme
88
theme.setLookAndFeel();
89         } catch (Exception JavaDoc ex) {
90
91             if (Logging.DEBUG)
92                 ex.printStackTrace();
93
94             JOptionPane.showMessageDialog(FrameManager.getInstance()
95                     .getActiveFrame(), "Error while trying to load "
96                     + pluginName
97                     + " Look and Feel.\nSwitching back to default.");
98
99             try {
100                 // fall-back
101
UIManager.setLookAndFeel(UIManager
102                         .getCrossPlatformLookAndFeelClassName());
103             } catch (Exception JavaDoc e) {
104                 e.printStackTrace();
105             }
106         }
107     } /*
108          * Gets the platform specific default theme. This is in all cases but
109          * MacOS X the Plastic theme. On MacOs X we use the System L&F.
110          *
111          */

112
113     public static String JavaDoc getPlatformDefaultTheme() {
114         if (OSInfo.isMac()) {
115             return QUAQUA_LF;
116         } else {
117             return DEFAULT_LF;
118         }
119     }
120
121     public static void updateFrame(JFrame JavaDoc frame) {
122         final JFrame JavaDoc f = frame;
123
124         SwingUtilities.invokeLater(new Runnable JavaDoc() {
125
126             public void run() {
127                 SwingUtilities.updateComponentTreeUI(f);
128             }
129         });
130     }
131 }
Popular Tags