| LookAndFeelUtil.java |
1 /**
2 * Distribution License:
3 * JSword is free software; you can redistribute it and/or modify it under
4 * the terms of the GNU Lesser General Public License, version 2.1 as published by
5 * the Free Software Foundation. This program is distributed in the hope
6 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8 * See the GNU Lesser General Public License for more details.
9 *
10 * The License is available on the internet at:
11 * http://www.gnu.org/copyleft/lgpl.html
12 * or by writing to:
13 * Free Software Foundation, Inc.
14 * 59 Temple Place - Suite 330
15 * Boston, MA 02111-1307, USA
16 *
17 * Copyright: 2005
18 * The copyright to this program is held by it's authors.
19 *
20 * ID: $Id: LookAndFeelUtil.java 2092 2011-03-07 12:20:11Z dmsmith $
21 */
22 package org.crosswire.common.swing;
23
24 import java.util.Enumeration;
25
26 import javax.swing.LookAndFeel;
27 import javax.swing.UIManager;
28 import javax.swing.UnsupportedLookAndFeelException;
29 import javax.swing.plaf.FontUIResource;
30 import javax.swing.plaf.metal.MetalLookAndFeel;
31
32 import org.crosswire.common.swing.plaf.MetalLFCustoms;
33 import org.crosswire.common.swing.plaf.OtherLFCustoms;
34 import org.crosswire.common.swing.plaf.WindowsLFCustoms;
35 import org.crosswire.common.util.ClassUtil;
36
37 /**
38 * LookAndFeelUtil declares the Choices and actions needed to dynamically change
39 * the look and feel (PLAF) and to add new PLAFs without needing to restart.
40 *
41 * @see gnu.lgpl.License for license details.<br>
42 * The copyright to this program is held by it's authors.
43 * @author Joe Walker [joe at eireneh dot com]
44 * @author Mark Goodwin [mark at thorubio dot org]
45 * @author DM Smith [dmsmith555 at yahoo dot com]
46 * @author Willie Thean [williethean at yahoo dot com]
47 */
48 public final class LookAndFeelUtil {
49 /**
50 * Prevent instantiation
51 */
52 private LookAndFeelUtil() {
53 }
54
55 /**
56 * Establish the system look and feel
57 */
58 public static void initialize() {
59 // Calling any method in this package will force the
60 // static initializer to be called.
61 }
62
63 /**
64 * The Options customization
65 */
66 public static Class<?> getLookAndFeel() {
67 if (currentLAF == null) {
68 return defaultLAF;
69 }
70 return currentLAF;
71 }
72
73 /**
74 * Set the look and feel to a new class.
75 */
76 public static void setLookAndFeel(Class<LookAndFeel> newLaFClass) throws InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
77 LookAndFeel laf = newLaFClass.newInstance();
78
79 // newLaFClass is null if the user enters a bogus value
80 if (currentLAF != null && !currentLAF.equals(newLaFClass)) {
81 CWOptionPane.showMessageDialog(null, CWOtherMsg.lookupText("The Look and Feel will change on the next startup."));
82 } else {
83 UIManager.setLookAndFeel(laf);
84 }
85
86 currentLAF = newLaFClass;
87 }
88
89 /**
90 * Accessor for the stylesheet we are transforming using
91 */
92 public static String getFont() {
93 return font;
94 }
95
96 /**
97 * Converts the font spec to something useful.
98 */
99 public static FontUIResource toFontUIResource() {
100 return new FontUIResource(GuiConvert.string2Font(LookAndFeelUtil.font));
101 }
102
103 /**
104 * Accessor for the stylesheet we are transforming using
105 */
106 public static void setFont(String font) {
107 LookAndFeelUtil.font = font;
108 setUIFont(toFontUIResource());
109 }
110
111 /**
112 * Set the default font for all Swing components. E.g.
113 * <code>setUIFont(new FontUIResource("Serif", Font.ITALIC, 12));</code> <br/>
114 * Note: a single resources can be changed with:
115 * <code>UIManager.put("Label.font", new Font("Serif", Font.ITALIC, 12));</code>
116 *
117 * @param f
118 * the font to use
119 */
120 public static void setUIFont(FontUIResource f) {
121 Enumeration<Object> keys = UIManager.getDefaults().keys();
122 while (keys.hasMoreElements()) {
123 Object key = keys.nextElement();
124 Object value = UIManager.get(key);
125
126 if (value instanceof FontUIResource) {
127 // System.err.println(key + " = " + value);
128 UIManager.put(key, f);
129 }
130 }
131 }
132
133 /**
134 * The current PLAF
135 */
136 private static Class<LookAndFeel> currentLAF;
137
138 /**
139 * The default PLAF (and the default value)
140 */
141 private static Class<?> defaultLAF;
142
143 /**
144 * The font to be used for the application
145 */
146 private static String font = "Dialog-PLAIN-12";
147
148 /**
149 * Setup the default PLAF
150 */
151 static {
152 defaultLAF = MetalLookAndFeel.class;
153 String systemLAF = UIManager.getSystemLookAndFeelClassName();
154 try {
155 // Note: GTK looks good under Java 1.5, but is broken.
156 // Motif still does not look good.
157 // systemLAF.indexOf("GTKLookAndFeel") != -1 ||
158 if (systemLAF.indexOf("WindowsLookAndFeel") != -1 || systemLAF.indexOf("AquaLookAndfeel") != -1) {
159 UIManager.setLookAndFeel(systemLAF);
160 // MetalLookAndFeel.setCurrentTheme(new DefaultMetalTheme());
161 // UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
162 // UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
163 // UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
164 defaultLAF = ClassUtil.forName(systemLAF);
165 }
166 } catch (ClassNotFoundException e) {
167 assert false;
168 } catch (InstantiationException e) {
169 assert false;
170 } catch (IllegalAccessException e) {
171 assert false;
172 } catch (UnsupportedLookAndFeelException e) {
173 assert false;
174 }
175
176 customizeBDLookandFeel();
177 }
178
179 private static void customizeBDLookandFeel() {
180 String currentLF = UIManager.getLookAndFeel().getClass().getName();
181
182 if (currentLF.indexOf("MetalLookAndFeel") != -1) {
183 new MetalLFCustoms().initUIDefaults();
184 } else if (currentLF.indexOf("WindowsLookAndFeel") != -1) {
185 new WindowsLFCustoms().initUIDefaults();
186 } else {
187 new OtherLFCustoms().initUIDefaults();
188 }
189 }
190 }
191