001: package org.jvnet.substance.netbeans.ui;
002:
003: import java.awt.event.ActionEvent;
004: import java.util.Iterator;
005: import java.util.Map;
006: import org.jvnet.substance.theme.*;
007: import org.jvnet.substance.themepack.theme.*;
008: import org.openide.ErrorManager;
009: import javax.swing.AbstractAction;
010: import javax.swing.Action;
011: import javax.swing.JMenu;
012: import javax.swing.JMenuItem;
013: import javax.swing.LookAndFeel;
014: import javax.swing.UIManager;
015: import javax.swing.UnsupportedLookAndFeelException;
016: import org.jvnet.substance.SubstanceImageCreator;
017: import org.jvnet.substance.SubstanceLookAndFeel;
018: import org.jvnet.substance.skin.SubstanceSkin;
019: import org.jvnet.substance.theme.SubstanceTheme;
020: import org.jvnet.substance.theme.ThemeInfo;
021: import org.openide.util.NbBundle;
022: import org.openide.util.WeakSet;
023: import org.openide.util.actions.Presenter;
024:
025: /**
026: * Adds a submenu to the View menu to let the user select themes.
027: *
028: * @author Timothy Boudreau
029: */
030: public class ThemeSelector extends AbstractAction implements
031: Presenter.Menu, Presenter.Popup {
032: public void actionPerformed(ActionEvent e) {
033: throw new UnsupportedOperationException();
034: }
035:
036: public JMenuItem getPopupPresenter() {
037: return getMenuPresenter();
038: }
039:
040: public JMenuItem getMenuPresenter() {
041: LookAndFeel lf = UIManager.getLookAndFeel();
042: JMenu result = new JMenu(NbBundle.getMessage(getClass(),
043: "LBL_Themes")); //NOI18N
044: JMenu bright = new JMenu(NbBundle.getMessage(getClass(),
045: "LBL_Bright_Themes")); //NOI18N
046: JMenu cold = new JMenu(NbBundle.getMessage(getClass(),
047: "LBL_Cold_Themes")); //NOI18N
048: JMenu dark = new JMenu(NbBundle.getMessage(getClass(),
049: "LBL_Dark_Themes")); //NOI18N
050: JMenu mixed_bright = new JMenu(NbBundle.getMessage(getClass(),
051: "LBL_Mixed_Themes")); //NOI18N
052: JMenu mixed_cold = new JMenu(NbBundle.getMessage(getClass(),
053: "LBL_Mixed_Themes")); //NOI18N
054:
055: if (lf instanceof SubstanceLookAndFeel) {
056: SubstanceLookAndFeel slf = (SubstanceLookAndFeel) lf;
057: Map<String, ThemeInfo> themes = slf.getAllThemes();
058: for (ThemeInfo themeInfo : themes.values()) {
059: JMenuItem item = new JMenuItem(new ChangeThemeAction(
060: themeInfo.getDisplayName()));
061: switch (themeInfo.getThemeKind()) {
062: case BRIGHT:
063: bright.add(item);
064: break;
065: case COLD:
066: cold.add(item);
067: break;
068: case DARK:
069: dark.add(item);
070: break;
071: }
072: // set icon
073: try {
074: Class themeClass = Class.forName(themeInfo
075: .getClassName());
076: SubstanceTheme theme = (SubstanceTheme) themeClass
077: .newInstance();
078: item.setIcon(SubstanceImageCreator
079: .getThemeIcon(theme));
080: } catch (Exception exc) {
081: continue;
082: }
083: }
084:
085: mixed_bright.add(getMenuItem(new SubstanceAquaTheme(),
086: new SubstanceLightAquaTheme()));
087: mixed_bright.add(getMenuItem(new ExtraDesertMarsTheme(),
088: new SubstanceOrangeTheme()));
089: mixed_bright.add(getMenuItem(new SubstanceBarbyPinkTheme(),
090: new ExtraPlacidPinkTheme()));
091: mixed_bright.add(getMenuItem(new ExtraBloodyMoonTheme(),
092: new ExtraBrickWallTheme()));
093: mixed_bright.add(getMenuItem(new SubstanceAquaTheme(),
094: new ExtraYellowMarineTheme()));
095: mixed_bright.add(getMenuItem(new ExtraBrickWallTheme(),
096: new SubstanceOrangeTheme()));
097: mixed_bright.add(getMenuItem(new ExtraEarthFrescoTheme(),
098: new SubstanceOrangeTheme()));
099: mixed_bright.add(getMenuItem(new SubstanceBarbyPinkTheme(),
100: new SubstancePurpleTheme()));
101: mixed_bright.add(getMenuItem(new SubstanceSunsetTheme(),
102: new ExtraEarthFrescoTheme()));
103: mixed_bright.add(getMenuItem(new ExtraMahoganyTheme(),
104: new SubstanceRaspberryTheme()));
105: mixed_bright.add(getMenuItem(new ExtraBloodyMoonTheme(),
106: new SubstanceOrangeTheme()));
107: mixed_bright.add(getMenuItem(new SubstanceBrownTheme(),
108: new ExtraEarthFrescoTheme()));
109: mixed_bright.add(getMenuItem(new ExtraPlacidPinkTheme(),
110: new ExtraBrickWallTheme()));
111: mixed_bright.add(getMenuItem(new SubstanceRaspberryTheme(),
112: new ExtraPlacidPinkTheme()));
113: mixed_bright.add(getMenuItem(new ExtraPlacidPinkTheme(),
114: new SubstancePurpleTheme()));
115: mixed_bright.add(getMenuItem(new ExtraBloodyMoonTheme(),
116: new ExtraEarthFrescoTheme()));
117: mixed_bright.add(getMenuItem(new ExtraBrickWallTheme(),
118: new ExtraEarthFrescoTheme()));
119: mixed_bright.add(getMenuItem(new SubstanceLimeGreenTheme(),
120: new ExtraWildPineTheme()));
121: mixed_bright.add(getMenuItem(new SubstanceRaspberryTheme(),
122: new SubstanceBarbyPinkTheme()));
123: mixed_bright.add(getMenuItem(new SubstanceBrownTheme(),
124: new SubstanceSunGlareTheme()));
125: mixed_bright.add(getMenuItem(new SubstanceSunsetTheme(),
126: new SubstanceSunGlareTheme()));
127: mixed_bright.add(getMenuItem(
128: new SubstanceBottleGreenTheme(),
129: new SubstanceLimeGreenTheme()));
130:
131: mixed_cold.add(getMenuItem(new SubstanceSteelBlueTheme(),
132: new ExtraCobaltSteelTheme()));
133: mixed_cold.add(getMenuItem(new ExtraBrownVelvetTheme(),
134: new SubstanceSepiaTheme()));
135: mixed_cold.add(getMenuItem(
136: new ExtraGooseberryJungleTheme(),
137: new ExtraCobaltSteelTheme()));
138: mixed_cold.add(getMenuItem(new SubstanceTerracottaTheme(),
139: new SubstanceSepiaTheme()));
140: mixed_cold.add(getMenuItem(new ExtraOrchidAlloyTheme(),
141: new ExtraBlueYonderTheme()));
142: mixed_cold.add(getMenuItem(new ExtraGreenPearlTheme(),
143: new ExtraSpringLeafTheme()));
144: mixed_cold.add(getMenuItem(new ExtraPeachTheme(),
145: new SubstanceTerracottaTheme()));
146: mixed_cold.add(getMenuItem(new ExtraCobaltSteelTheme(),
147: new ExtraGreenPearlTheme()));
148: mixed_cold.add(getMenuItem(new ExtraSpringLeafTheme(),
149: new SubstanceOliveTheme()));
150: mixed_cold.add(getMenuItem(new ExtraBrownVelvetTheme(),
151: new SubstanceOliveTheme()));
152: mixed_cold.add(getMenuItem(new ExtraBrownVelvetTheme(),
153: new SubstanceTerracottaTheme()));
154: mixed_cold.add(getMenuItem(new ExtraTurquoiseLakeTheme(),
155: new SubstanceSteelBlueTheme()));
156: mixed_cold.add(getMenuItem(new ExtraSkyHighTheme(),
157: new ExtraTurquoiseLakeTheme()));
158: mixed_cold.add(getMenuItem(new ExtraFauveMauveTheme(),
159: new ExtraGooseberryJungleTheme()));
160:
161: bright.add(mixed_bright);
162: cold.add(mixed_cold);
163:
164: result.add(bright);
165: result.add(cold);
166: result.add(dark);
167: result.setIcon(SubstanceImageCreator.getThemeIcon(null));
168: } else {
169: result.setText(NbBundle.getMessage(getClass(),
170: "LBL_NotRunning")); //NOI18N
171: result.setEnabled(false);
172: }
173: return result;
174: }
175:
176: private static JMenuItem getMenuItem(SubstanceTheme theme1,
177: SubstanceTheme theme2) {
178: SubstanceTheme mixedTheme = new SubstanceMixTheme(theme1,
179: theme2);
180: JMenuItem item = new JMenuItem(new ChangeThemeObjectAction(
181: mixedTheme));
182: item.setIcon(SubstanceImageCreator.getThemeIcon(mixedTheme));
183: return item;
184: }
185:
186: private static JMenuItem getMenuItem(SubstanceTheme theme) {
187: JMenuItem item = new JMenuItem(new ChangeThemeObjectAction(
188: theme));
189: item.setIcon(SubstanceImageCreator.getThemeIcon(theme));
190: return item;
191: }
192:
193: private static final WeakSet allActions = new WeakSet();
194:
195: private static void updateActions() {
196: for (Iterator i = allActions.iterator(); i.hasNext();) {
197: Object themeAction = i.next();
198: if (themeAction instanceof ChangeThemeAction) {
199: ChangeThemeAction a = (ChangeThemeAction) themeAction;
200: if (a != null) {
201: a.update();
202: }
203: }
204: if (themeAction instanceof ChangeThemeObjectAction) {
205: ChangeThemeObjectAction a = (ChangeThemeObjectAction) themeAction;
206: if (a != null) {
207: a.update();
208: }
209: }
210:
211: }
212: }
213:
214: private static final class ChangeThemeAction extends AbstractAction {
215: private final String theme;
216:
217: public ChangeThemeAction(String theme) {
218: this .theme = theme;
219: allActions.add(this );
220: update();
221: }
222:
223: private void update() {
224: String nm = theme;
225: if (nm.equals(SubstanceLookAndFeel.getCurrentThemeName())) {
226: nm = "<html><b>" + nm + "</b></html>"; //NOI18N
227: }
228: putValue(Action.NAME, nm);
229: }
230:
231: public void actionPerformed(ActionEvent e) {
232: ThemeInfo info = (ThemeInfo) SubstanceLookAndFeel
233: .getAllThemes().get(theme);
234:
235: assert info != null;
236: String themeClass = info.getClassName();
237: setTheme(themeClass);
238: ButtonShapeSelector.updateUIs();
239: persist(themeClass);
240: updateActions();
241: }
242: }
243:
244: private static final class ChangeThemeObjectAction extends
245: AbstractAction {
246: private final SubstanceTheme theme;
247:
248: public ChangeThemeObjectAction(SubstanceTheme theme) {
249: this .theme = theme;
250: allActions.add(this );
251: update();
252: }
253:
254: private void update() {
255: String nm = theme.getDisplayName();
256: if (nm.equals(SubstanceLookAndFeel.getCurrentThemeName())) {
257: nm = "<html><b>" + nm + "</b></html>"; //NOI18N
258: }
259: putValue(Action.NAME, nm);
260: }
261:
262: public void actionPerformed(ActionEvent e) {
263: setTheme(theme);
264: ButtonShapeSelector.updateUIs();
265: persist(theme);
266: updateActions();
267: }
268: }
269:
270: static void setTheme(String s) {
271: try {
272: SubstanceLookAndFeel.setCurrentTheme(s);
273: SubstanceLookAndFeel slf = new SubstanceLookAndFeel();
274: UIManager.setLookAndFeel(slf);
275: } catch (UnsupportedLookAndFeelException ulafe) {
276: ErrorManager.getDefault().notify(ulafe);
277: throw new RuntimeException(ulafe);
278: }
279: }
280:
281: static void setTheme(SubstanceTheme theme) {
282: try {
283: SubstanceLookAndFeel.setCurrentTheme(theme);
284: SubstanceLookAndFeel slf = new SubstanceLookAndFeel();
285: UIManager.setLookAndFeel(slf);
286: } catch (UnsupportedLookAndFeelException ulafe) {
287: ErrorManager.getDefault().notify(ulafe);
288: throw new RuntimeException(ulafe);
289: }
290: }
291:
292: static void persist(String value) {
293: NbPrefs p = new NbPrefs();
294: p.put("themeClassName", value); //NOI18N
295: // p.put("skinClassName", null);
296: }
297:
298: static void persist(SubstanceTheme theme) {
299: persist(theme.getClass().getName());
300: if (theme instanceof SubstanceMixTheme) {
301: NbPrefs p = new NbPrefs();
302: SubstanceMixTheme mixTheme = (SubstanceMixTheme) theme;
303: SubstanceTheme[] themes = mixTheme.getOriginalThemes();
304: p.put("themeCount", "" + themes.length);
305: for (int i = 0; i < themes.length; i++)
306: p.put("themeParam" + i, themes[i].getClass().getName()); //NOI18N
307: }
308: }
309:
310: public static boolean restore() {
311: NbPrefs p = new NbPrefs();
312: String preferred = p.get("themeClassName", null); //NOI18N
313: if (preferred != null) {
314: if (preferred.equals(SubstanceMixTheme.class.getName())) {
315: try {
316: int count = Integer.parseInt(p.get("themeCount",
317: null));
318: SubstanceTheme[] themes = new SubstanceTheme[count];
319: for (int i = 0; i < count; i++) {
320: String param = p.get("themeParam" + i, null); // NOI18N
321: themes[i] = (SubstanceTheme) Class.forName(
322: param).newInstance();
323: }
324: SubstanceMixTheme mixTheme = new SubstanceMixTheme(
325: themes);
326: setTheme(mixTheme);
327: } catch (Exception exc) {
328: return false;
329: }
330: } else {
331: setTheme(preferred);
332: }
333: }
334: return preferred != null;
335: }
336: }
|