001: package net.sourceforge.squirrel_sql.plugins.laf;
002:
003: /*
004: * Copyright (C) 2003-20066 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or 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
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021: import java.awt.GridBagConstraints;
022: import java.awt.GridBagLayout;
023: import java.awt.Insets;
024: import java.util.Iterator;
025: import java.util.Map;
026: import java.util.TreeMap;
027:
028: import javax.swing.JComboBox;
029: import javax.swing.JLabel;
030: import javax.swing.LookAndFeel;
031: import javax.swing.SwingConstants;
032: import javax.swing.plaf.metal.MetalTheme;
033:
034: import net.sourceforge.squirrel_sql.fw.id.IHasIdentifier;
035: import net.sourceforge.squirrel_sql.fw.id.IIdentifier;
036: import net.sourceforge.squirrel_sql.fw.id.IntegerIdentifier;
037: import net.sourceforge.squirrel_sql.fw.util.BaseException;
038: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
039: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
040:
041: /**
042: * Base class for the LAF controllers for Plastic and Metal.
043: *
044: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
045: */
046: abstract class AbstractPlasticController extends
047: DefaultLookAndFeelController {
048: /** Logger for this class. */
049: private static ILogger s_log = LoggerController
050: .createLogger(AbstractPlasticController.class);
051:
052: /**
053: * This interface defines locale specific strings. This should be
054: * replaced with a property file.
055: */
056: protected interface i18n {
057: String THEME = "Theme:";
058: }
059:
060: /** Class names of all the Plastic themes. */
061: protected static final String[] PLASTIC_THEME_CLASS_NAMES = new String[] {
062: "com.jgoodies.looks.plastic.theme.BrownSugar",
063: "com.jgoodies.looks.plastic.theme.DarkStar",
064: "com.jgoodies.looks.plastic.theme.DesertBlue",
065: "com.jgoodies.looks.plastic.theme.DesertBluer",
066: "com.jgoodies.looks.plastic.theme.DesertGreen",
067: "com.jgoodies.looks.plastic.theme.DesertRed",
068: "com.jgoodies.looks.plastic.theme.DesertYellow",
069: "com.jgoodies.looks.plastic.theme.ExperienceBlue",
070: "com.jgoodies.looks.plastic.theme.ExperienceGreen",
071: "com.jgoodies.looks.plastic.theme.ExperienceRoyale",
072: "com.jgoodies.looks.plastic.theme.LightGray",
073: "com.jgoodies.looks.plastic.theme.Silver",
074: "com.jgoodies.looks.plastic.theme.SkyBlue",
075: "com.jgoodies.looks.plastic.theme.SkyBluer",
076: "com.jgoodies.looks.plastic.theme.SkyGreen",
077: "com.jgoodies.looks.plastic.theme.SkyKrupp",
078: "com.jgoodies.looks.plastic.theme.SkyPink",
079: "com.jgoodies.looks.plastic.theme.SkyRed",
080: "com.jgoodies.looks.plastic.theme.SkyYellow", };
081:
082: public static final String DEFAULT_PLASTIC_THEME_CLASS_NAME = PLASTIC_THEME_CLASS_NAMES[11]; // SkyBluer
083:
084: /** Look and Feel Plugin. */
085: private final LAFPlugin _lafPlugin;
086:
087: /** Look and Feel Register. */
088: private final LAFRegister _lafRegister;
089:
090: /** The Plastic themes keyed by the theme name. */
091: private Map<String, MetalTheme> _themes = new TreeMap<String, MetalTheme>();
092:
093: /**
094: * Ctor specifying the Look and Feel plugin and register.
095: *
096: * @param plugin The plugin that this controller is a part of.
097: * @param lafRegister LAF register.
098: */
099: AbstractPlasticController(LAFPlugin plugin, LAFRegister lafRegister) {
100: super ();
101: _lafPlugin = plugin;
102: _lafRegister = lafRegister;
103: }
104:
105: /**
106: * Initialization.
107: */
108: public void initialize() {
109: _themes.clear();
110:
111: ClassLoader cl = getLAFRegister().getLookAndFeelClassLoader();
112: MetalTheme[] extras = getExtraThemes();
113: if (extras == null) {
114: extras = new MetalTheme[0];
115: }
116:
117: for (int i = 0; i < extras.length; ++i) {
118: _themes.put(extras[i].getName(), extras[i]);
119: }
120:
121: for (int i = 0; i < PLASTIC_THEME_CLASS_NAMES.length; ++i) {
122: try {
123:
124: Class<?> clazz = Class.forName(
125: PLASTIC_THEME_CLASS_NAMES[i], false, cl);
126: MetalTheme theme = (MetalTheme) clazz.newInstance();
127: _themes.put(theme.getName(), theme);
128: } catch (Throwable th) {
129: s_log.error("Error loading theme "
130: + PLASTIC_THEME_CLASS_NAMES[i], th);
131: }
132: }
133: }
134:
135: /**
136: * A Look and Feel is about to be installed. Apply the selected
137: * theme.
138: *
139: * @param lafRegister The LAF Register.
140: * @param laf The Look and Feel being installed.
141: */
142: public void aboutToBeInstalled(LAFRegister lafRegister,
143: LookAndFeel laf) {
144: final String name = getCurrentThemeName();
145: if (name != null) {
146: MetalTheme theme = getThemeForName(name);
147: if (theme != null) {
148: try {
149: installCurrentTheme(laf, theme);
150: } catch (BaseException ex) {
151: s_log.error("Error installing a Theme", ex);
152: }
153: }
154: }
155: }
156:
157: /**
158: * Retrieve the extra panel to be displayed in the LAF preferences
159: * panel for the current LAF.
160: *
161: * @return LAF preferences extra panel
162: */
163: public BaseLAFPreferencesPanelComponent getPreferencesComponent() {
164: return new PrefsPanel(this );
165: }
166:
167: /**
168: * Retrieve the name of the current theme.
169: *
170: * @return Name of the current theme.
171: */
172: abstract String getCurrentThemeName();
173:
174: /**
175: * Set the current theme name.
176: *
177: * @param name name of the current theme.
178: */
179: abstract void setCurrentThemeName(String name);
180:
181: /**
182: * Override in subclasses to actually set the current theme.
183: *
184: * @param name
185: * @return
186: */
187: abstract void installCurrentTheme(LookAndFeel laf, MetalTheme theme)
188: throws BaseException;
189:
190: final MetalTheme getThemeForName(String name) {
191: return _themes.get(name);
192: }
193:
194: /**
195: * Override in subclasses to retrieve the extra themes for the Look and
196: * Feel classes that the subclass is responsible for.
197: * @return
198: */
199: MetalTheme[] getExtraThemes() {
200: return new MetalTheme[0];
201: }
202:
203: /**
204: * Retrieve the LAF register.
205: *
206: * @return <TT>LAFRegister</TT>.
207: */
208: LAFRegister getLAFRegister() {
209: return _lafRegister;
210: }
211:
212: /**
213: * Retrieve the LAF plugin.
214: *
215: * @return <TT>LAFPlugin</TT>.
216: */
217: LAFPlugin getLAFPlugin() {
218: return _lafPlugin;
219: }
220:
221: Iterator<MetalTheme> themesIterator() {
222: return _themes.values().iterator();
223: }
224:
225: /**
226: * Preferences panel. Show a dropdown of themes.
227: */
228: final static class PrefsPanel extends
229: BaseLAFPreferencesPanelComponent {
230: private static final long serialVersionUID = 1L;
231: private AbstractPlasticController _ctrl;
232: private JComboBox _themeCmb;
233: private int _origSelThemeIdx;
234:
235: PrefsPanel(AbstractPlasticController ctrl) {
236: super ();
237: _ctrl = ctrl;
238: createUserInterface();
239: }
240:
241: private void createUserInterface() {
242: setLayout(new GridBagLayout());
243: _themeCmb = new JComboBox();
244:
245: final GridBagConstraints gbc = new GridBagConstraints();
246: gbc.anchor = GridBagConstraints.WEST;
247: gbc.fill = GridBagConstraints.HORIZONTAL;
248: gbc.insets = new Insets(4, 4, 4, 4);
249:
250: gbc.gridx = 0;
251: gbc.gridy = 0;
252: add(new JLabel(i18n.THEME, SwingConstants.RIGHT), gbc);
253:
254: ++gbc.gridx;
255: add(_themeCmb, gbc);
256: }
257:
258: /**
259: * @see BaseLAFPreferencesPanelComponent#loadPreferencesPanel()
260: */
261: public void loadPreferencesPanel() {
262: super .loadPreferencesPanel();
263: loadThemesCombo();
264: }
265:
266: /**
267: * @see BaseLAFPreferencesPanelComponent#applyChanges()
268: */
269: public boolean applyChanges() {
270: super .applyChanges();
271: if (_origSelThemeIdx != _themeCmb.getSelectedIndex()) {
272: _ctrl.setCurrentThemeName((String) _themeCmb
273: .getSelectedItem());
274: return true;
275: }
276: return false;
277: }
278:
279: private void loadThemesCombo() {
280: _themeCmb.removeAllItems();
281:
282: for (Iterator<MetalTheme> it = _ctrl.themesIterator(); it
283: .hasNext();) {
284: _themeCmb.addItem((it.next()).getName());
285: }
286:
287: if (_themeCmb.getModel().getSize() > 0) {
288: String selThemeName = _ctrl.getCurrentThemeName();
289: if (selThemeName != null && selThemeName.length() > 0) {
290: _themeCmb.setSelectedItem(selThemeName);
291: }
292: if (_themeCmb.getSelectedIndex() == -1) {
293: _themeCmb.setSelectedIndex(0);
294: }
295: }
296: _origSelThemeIdx = _themeCmb.getSelectedIndex();
297: }
298: }
299:
300: public static abstract class ThemePreferences implements
301: IHasIdentifier {
302: private String _themeName;
303: private IntegerIdentifier _id = new IntegerIdentifier(1);
304:
305: public String getThemeName() {
306: return _themeName;
307: }
308:
309: public void setThemeName(String value) {
310: _themeName = value;
311: }
312:
313: /**
314: * @return The unique identifier for this object.
315: */
316: public IIdentifier getIdentifier() {
317: return _id;
318: }
319: }
320: }
|