001: /*
002: * Beryl - A web platform based on XML, XSLT and Java
003: * This file is part of the Beryl XML GUI
004: *
005: * Copyright (C) 2004 Wenzel Jakob <wazlaf@tigris.org>
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) 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 GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-3107 USA
020: */
021:
022: package org.beryl.gui.builder;
023:
024: import java.lang.reflect.Constructor;
025: import java.lang.reflect.Method;
026:
027: import javax.swing.LookAndFeel;
028: import javax.swing.UIManager;
029: import javax.swing.plaf.metal.MetalLookAndFeel;
030: import javax.swing.plaf.metal.MetalTheme;
031:
032: import org.beryl.gui.Controller;
033: import org.beryl.gui.GUIEvent;
034: import org.beryl.gui.GUIException;
035: import org.beryl.gui.GUIUtils;
036: import org.beryl.gui.MessageDialog;
037: import org.beryl.gui.model.ListDataModel;
038: import org.beryl.gui.model.MapChangeEvent;
039: import org.beryl.gui.model.MapDataModel;
040: import org.beryl.gui.model.ModelChangeEvent;
041: import org.beryl.gui.model.ModelChangeListener;
042: import org.beryl.gui.widgets.Button;
043: import org.beryl.gui.widgets.ComboBox;
044: import org.beryl.gui.widgets.Frame;
045: import org.beryl.gui.widgets.TextField;
046:
047: public class LookAndFeelChooser extends Controller implements
048: ModelChangeListener {
049: private MapDataModel dataModel = null;
050: private Frame frame = null;
051: private ComboBox themeCombo = null;
052: private Button okButton = null;
053: private TextField licenseField = null;
054: private ListDataModel lnfModel = null;
055: private ListDataModel emptyModel = null;
056:
057: private class Theme {
058: public String clazz = null;
059:
060: public Theme(String clazz) {
061: this .clazz = clazz;
062: }
063:
064: public String toString() {
065: return getString("builder.lookandfeel." + clazz);
066: }
067: };
068:
069: /**
070: * General Look And Feel
071: */
072: private class LnF {
073: public String clazz = null;
074: public ListDataModel themeModel = null;
075: public boolean supported = false;
076:
077: public LnF(String clazz) {
078: this .clazz = clazz;
079: try {
080: Class lnf = Class.forName(clazz);
081: supported = !(System.getProperty("os.name").equals(
082: "Mac OS X") && lnf.getName().equals(
083: GUIUtils.KUNSTSTOFF_LNF));
084: } catch (Exception e) {
085: supported = false;
086: }
087: }
088:
089: protected void addTheme(String clazz) throws GUIException {
090: if (themeModel == null)
091: themeModel = new ListDataModel();
092: themeModel.addValue(new Theme(clazz));
093: }
094:
095: public String toString() {
096: if (isSupported())
097: return getString("builder.lookandfeel." + clazz);
098: else
099: return "[" + getString("builder.lookandfeel." + clazz)
100: + "]";
101: }
102:
103: public boolean isSupported() {
104: return supported;
105: }
106:
107: public boolean needsLicenseCode() {
108: return false;
109: }
110:
111: public LookAndFeel create(String themeClass, String licenseCode)
112: throws GUIException {
113: try {
114: Class lnfClass = Class.forName(clazz);
115: LookAndFeel lnfInstance = (LookAndFeel) lnfClass
116: .newInstance();
117: return lnfInstance;
118: } catch (Exception e) {
119: throw new GUIException(
120: "Error while creating Look And Feel", e);
121: }
122: }
123: };
124:
125: /**
126: * Metal based Look And Feel
127: */
128: private class MetalLnF extends LnF {
129: public MetalLnF(String clazz) {
130: super (clazz);
131: }
132:
133: public LookAndFeel create(String themeClass, String licenseCode)
134: throws GUIException {
135: MetalLookAndFeel lnf = (MetalLookAndFeel) super .create(
136: themeClass, licenseCode);
137: try {
138: MetalLookAndFeel.setCurrentTheme((MetalTheme) Class
139: .forName(themeClass).newInstance());
140: return lnf;
141: } catch (Exception e) {
142: throw new GUIException("Error while setting theme", e);
143: }
144: }
145: };
146:
147: /**
148: * Proprietary "Alloy" Look And Feel from INCORS
149: */
150: private class AlloyLnF extends LnF {
151: public AlloyLnF() throws GUIException {
152: super ("com.incors.plaf.alloy.AlloyLookAndFeel");
153: addTheme("com.incors.plaf.alloy.DefaultAlloyTheme");
154: addTheme("com.incors.plaf.alloy.themes.acid.AcidTheme");
155: addTheme("com.incors.plaf.alloy.themes.bedouin.BedouinTheme");
156: addTheme("com.incors.plaf.alloy.themes.glass.GlassTheme");
157: }
158:
159: public boolean needsLicenseCode() {
160: return true;
161: }
162:
163: public LookAndFeel create(String themeClass, String licenseCode)
164: throws GUIException {
165: try {
166: Class lnfClass = Class.forName(clazz);
167:
168: Method method = lnfClass.getMethod("setProperty",
169: new Class[] { String.class, String.class });
170: method.invoke(null, new Object[] { "alloy.licenseCode",
171: licenseCode });
172:
173: Constructor constructor = lnfClass
174: .getConstructor(new Class[] { Class
175: .forName("com.incors.plaf.alloy.AlloyTheme") });
176: LookAndFeel lnfInstance = (LookAndFeel) constructor
177: .newInstance(new Object[] { Class.forName(
178: themeClass).newInstance() });
179:
180: return lnfInstance;
181: } catch (Exception e) {
182: throw new GUIException("Error while setting theme", e);
183: }
184: }
185: };
186:
187: /**
188: * JGoodies Plastic Look And Feel
189: */
190: private class PlasticLnF extends MetalLnF {
191: public PlasticLnF(String clazz) throws GUIException {
192: super (clazz);
193: addTheme("com.jgoodies.plaf.plastic.theme.DesertBlue");
194: addTheme("com.jgoodies.plaf.plastic.theme.BrownSugar");
195: addTheme("com.jgoodies.plaf.plastic.theme.DarkStar");
196: addTheme("com.jgoodies.plaf.plastic.theme.DesertBluer");
197: addTheme("com.jgoodies.plaf.plastic.theme.DesertGreen");
198: addTheme("com.jgoodies.plaf.plastic.theme.DesertRed");
199: addTheme("com.jgoodies.plaf.plastic.theme.DesertYellow");
200: addTheme("com.jgoodies.plaf.plastic.theme.ExperienceBlue");
201: addTheme("com.jgoodies.plaf.plastic.theme.ExperienceGreen");
202: addTheme("com.jgoodies.plaf.plastic.theme.Silver");
203: addTheme("com.jgoodies.plaf.plastic.theme.SkyBlue");
204: addTheme("com.jgoodies.plaf.plastic.theme.SkyBluer");
205: addTheme("com.jgoodies.plaf.plastic.theme.SkyBluerTahoma");
206: addTheme("com.jgoodies.plaf.plastic.theme.SkyGreen");
207: addTheme("com.jgoodies.plaf.plastic.theme.SkyKrupp");
208: addTheme("com.jgoodies.plaf.plastic.theme.SkyPink");
209: addTheme("com.jgoodies.plaf.plastic.theme.SkyRed");
210: addTheme("com.jgoodies.plaf.plastic.theme.SkyYellow");
211: }
212: };
213:
214: public LookAndFeelChooser() throws GUIException {
215: emptyModel = new ListDataModel();
216: lnfModel = new ListDataModel();
217:
218: LnF lnf = new MetalLnF(
219: "javax.swing.plaf.metal.MetalLookAndFeel");
220: lnf.addTheme("javax.swing.plaf.metal.DefaultMetalTheme");
221: lnfModel.addValue(lnf);
222: lnfModel.addValue(new LnF(
223: "com.sun.java.swing.plaf.windows.WindowsLookAndFeel") {
224: public boolean isSupported() {
225: return super .isSupported()
226: && (System.getProperty("os.name").toLowerCase()
227: .indexOf("windows") != -1);
228: }
229: });
230: lnfModel.addValue(new LnF(
231: "com.sun.java.swing.plaf.motif.MotifLookAndFeel"));
232: lnf = new MetalLnF(
233: "com.incors.plaf.kunststoff.KunststoffLookAndFeel");
234: lnf.addTheme("com.incors.plaf.kunststoff.KunststoffTheme");
235: lnfModel.addValue(lnf);
236: lnfModel.addValue(new AlloyLnF());
237:
238: lnfModel.addValue(new LnF(
239: "com.jgoodies.plaf.windows.ExtWindowsLookAndFeel") {
240: public boolean isSupported() {
241: return super .isSupported()
242: && (System.getProperty("os.name").toLowerCase()
243: .indexOf("windows") != -1);
244: }
245: });
246:
247: lnfModel.addValue(new PlasticLnF(
248: "com.jgoodies.plaf.plastic.PlasticLookAndFeel"));
249: lnfModel.addValue(new PlasticLnF(
250: "com.jgoodies.plaf.plastic.Plastic3DLookAndFeel"));
251: lnfModel.addValue(new PlasticLnF(
252: "com.jgoodies.plaf.plastic.PlasticXPLookAndFeel"));
253:
254: dataModel = new MapDataModel();
255: frame = constructFrame("LookAndFeelChooser", dataModel);
256: ((ComboBox) frame.getWidget("LnfCombo"))
257: .setListDataModel(lnfModel);
258: themeCombo = (ComboBox) frame.getWidget("ThemeCombo");
259: licenseField = (TextField) frame.getWidget("LicenseField");
260: okButton = (Button) frame.getWidget("OKButton");
261:
262: dataModel.addModelChangeListener(this );
263: dataModel.setValue("lnf.index", new Integer(0));
264:
265: frame.show();
266: }
267:
268: public boolean isVisible() {
269: return frame.isVisible();
270: }
271:
272: public void eventOccured(GUIEvent event) {
273: String name = event.getName();
274: try {
275: if (name.equals("apply")) {
276: LnF lnf = (LnF) dataModel.getValue("lnf.value");
277: Theme theme = (Theme) dataModel.getValue("theme.value");
278: String licenseCode = (String) dataModel
279: .getValue("license");
280:
281: LookAndFeel lnfInstance = lnf
282: .create(theme == null ? null : theme.clazz,
283: licenseCode);
284: UIManager.setLookAndFeel(lnfInstance);
285: GUIUtils.updateAllWidgets();
286: } else if (name.equals("close")) {
287: frame.dispose();
288: }
289: } catch (Exception e) {
290: new MessageDialog(e);
291: }
292: }
293:
294: public void modelChanged(ModelChangeEvent e) throws GUIException {
295: if (e instanceof MapChangeEvent) {
296: MapChangeEvent event = (MapChangeEvent) e;
297:
298: if (event.getKey().equals("lnf.value")) {
299: LnF lnf = (LnF) dataModel.getValue("lnf.value");
300:
301: dataModel.setValue("license", "");
302: licenseField.setEnabled(lnf.needsLicenseCode());
303: okButton.setEnabled(lnf.isSupported());
304:
305: if (lnf.themeModel == null) {
306: themeCombo.setListDataModel(emptyModel);
307: themeCombo.setEnabled(false);
308: } else {
309: themeCombo.setListDataModel(lnf.themeModel);
310: dataModel.setValue("theme.index", new Integer(0));
311:
312: themeCombo.setEnabled(true);
313: }
314: }
315: }
316: }
317: }
|