001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.gui.lookandfeel;
020:
021: import java.util.Collection;
022: import java.util.Iterator;
023: import java.util.LinkedHashMap;
024:
025: import javax.swing.LookAndFeel;
026: import javax.swing.UIManager;
027:
028: import com.jeta.forms.gui.common.URLClassLoaderHelper;
029: import com.jeta.forms.logger.FormsLogger;
030: import com.jeta.open.gui.utils.JETAToolbox;
031: import com.jeta.open.i18n.I18N;
032: import com.jeta.swingbuilder.interfaces.userprops.TSUserPropertiesUtils;
033:
034: /**
035: * The look and feel manager for the application.
036: *
037: * @author Jeff Tassin
038: */
039: public class DefaultLookAndFeelManager {
040: /**
041: * The class loader for all look and feels.
042: */
043: private URLClassLoaderHelper m_loader;
044:
045: /**
046: * A list of default look and feels. (We use LookAndFeelInfo objects for
047: * convenience here)
048: */
049: private LinkedHashMap m_lafs = new LinkedHashMap();
050:
051: // public static final String ALLOY = AlloyLoader.class.getName();
052: public static final String JGOODIES = JGoodiesLoader.class
053: .getName();
054:
055: public static final String DEFAULT = DefaultLoader.class.getName();
056:
057: public static final String COMPONENT_ID = "look.and.feel.manager";
058:
059: public static final String CURRENT_LOOK_AND_FEEL = "current.look.and.feel";
060:
061: /**
062: * ctor
063: */
064: public DefaultLookAndFeelManager() {
065: loadDefaults();
066: }
067:
068: private void createDefaultLookAndFeel(String lfLoader,
069: String lfClassName, String lfThemeName, String name) {
070: LookAndFeelInfo lf = new LookAndFeelInfo(lfLoader, lfClassName,
071: lfThemeName, name, lfClassName + "," + lfThemeName);
072: m_lafs.put(lf.getId(), lf);
073: }
074:
075: public LookAndFeelInfo findById(String id) {
076: return (LookAndFeelInfo) m_lafs.get(id);
077: }
078:
079: /**
080: * Locates a look and feel by the class name.
081: */
082: public LookAndFeelInfo findByClass(String className, String theme) {
083: Iterator iter = m_lafs.values().iterator();
084: while (iter.hasNext()) {
085: LookAndFeelInfo info = (LookAndFeelInfo) iter.next();
086: if (theme == null || theme.length() == 0) {
087: if (className.equals(info.getLookAndFeelClassName()))
088: return info;
089: } else {
090: if (className.equals(info.getLookAndFeelClassName())
091: && theme.equals(info.getTheme()))
092: return info;
093: }
094: }
095: return null;
096: }
097:
098: /**
099: * @return the default look and feel for the application. The following OSes
100: * will have the given default look and feel: Linux Java Metal
101: * Windows Windows Apple Apple Other Java Metal
102: */
103: public LookAndFeelInfo getDefaultLookAndFeel() {
104: String id = TSUserPropertiesUtils.getString(
105: CURRENT_LOOK_AND_FEEL, null);
106: if (id != null) {
107: LookAndFeelInfo info = findById(id);
108: if (info != null)
109: return info;
110: }
111:
112: if (JETAToolbox.isOSX()) {
113: return findByClass(LookAndFeelNames.ID_AQUA_LF, null);
114: } else {
115: return findByClass(
116: "com.jgoodies.looks.plastic.PlasticXPLookAndFeel",
117: "com.jgoodies.looks.plastic.theme.ExperienceBlue");
118: }
119: }
120:
121: public Collection getDefaultLookAndFeels() {
122: return m_lafs.values();
123: }
124:
125: /**
126: * @return the look and feel
127: */
128: public LookAndFeel getLookAndFeel(String lfId) {
129: try {
130: LookAndFeelInfo info = (LookAndFeelInfo) m_lafs.get(lfId);
131: if (info != null) {
132: return info.getLookAndFeel();
133: }
134: } catch (Exception e) {
135: e.printStackTrace();
136: // ignore here. null is returned on error
137: }
138: return null;
139: }
140:
141: private void loadDefaults() {
142: try {
143: /*
144: * implement later createDefaultLookAndFeel( ALLOY,
145: * "com.incors.plaf.alloy.AlloyLookAndFeel",
146: * "com.incors.plaf.alloy.DefaultAlloyTheme",
147: * I18N.getLocalizedMessage( "Alloy - Default" ) );
148: * createDefaultLookAndFeel( ALLOY,
149: * "com.incors.plaf.alloy.AlloyLookAndFeel",
150: * "com.incors.plaf.alloy.themes.glass.GlassTheme",
151: * I18N.getLocalizedMessage( "Alloy - Glass" ) );
152: * createDefaultLookAndFeel( ALLOY,
153: * "com.incors.plaf.alloy.AlloyLookAndFeel",
154: * "com.incors.plaf.alloy.themes.bedouin.BedouinTheme",
155: * I18N.getLocalizedMessage( "Alloy - Desert" ) );
156: * createDefaultLookAndFeel( ALLOY,
157: * "com.incors.plaf.alloy.AlloyLookAndFeel",
158: * "com.incors.plaf.alloy.themes.acid.AcidTheme",
159: * I18N.getLocalizedMessage( "Alloy - Acid" ) );
160: */
161:
162: createDefaultLookAndFeel(
163: JGOODIES,
164: "com.jgoodies.looks.plastic.PlasticXPLookAndFeel",
165: "com.jgoodies.looks.plastic.theme.ExperienceBlue",
166: I18N
167: .getLocalizedMessage("PlasticXP - Experience Blue"));
168:
169: createDefaultLookAndFeel(
170: JGOODIES,
171: "com.jgoodies.looks.plastic.PlasticXPLookAndFeel",
172: "com.jgoodies.looks.plastic.theme.ExperienceGreen",
173: I18N
174: .getLocalizedMessage("PlasticXP - Experience Green"));
175:
176: createDefaultLookAndFeel(JGOODIES,
177: "com.jgoodies.looks.plastic.PlasticXPLookAndFeel",
178: "com.jgoodies.looks.plastic.theme.SkyBlue",
179: I18N.getLocalizedMessage("PlasticXP - Sky Blue"));
180:
181: createDefaultLookAndFeel(DEFAULT,
182: "javax.swing.plaf.metal.MetalLookAndFeel", "", I18N
183: .getLocalizedMessage("Metal"));
184:
185: createDefaultLookAndFeel(DEFAULT,
186: "org.jvnet.substance.SubstanceLookAndFeel", "",
187: I18N.getLocalizedMessage("Substance"));
188:
189: UIManager.LookAndFeelInfo[] lfinfo = UIManager
190: .getInstalledLookAndFeels();
191: if (lfinfo != null) {
192: for (int index = 0; index < lfinfo.length; index++) {
193: UIManager.LookAndFeelInfo lfi = lfinfo[index];
194: createDefaultLookAndFeel(DEFAULT, lfi
195: .getClassName(), "", lfi.getName());
196: }
197: }
198:
199: } catch (Exception e) {
200:
201: }
202: }
203:
204: /** sets the look and feel for the given frame and look an feel class name */
205: public static void setLookAndFeel(LookAndFeelInfo lfinfo) {
206: try {
207: if (lfinfo != null) {
208: LookAndFeelLoader loader = lfinfo.getLoaderInstance();
209: if (loader != null) {
210: loader.setLookAndFeel(lfinfo);
211: }
212: }
213: TSUserPropertiesUtils.setString(CURRENT_LOOK_AND_FEEL,
214: lfinfo.getId());
215: } catch (Exception e) {
216: FormsLogger.debug(e);
217: }
218: }
219:
220: }
|