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.skin.SkinInfo;
007: import javax.swing.AbstractAction;
008: import javax.swing.Action;
009: import javax.swing.JMenu;
010: import javax.swing.JMenuItem;
011: import javax.swing.LookAndFeel;
012: import javax.swing.UIManager;
013: import org.jvnet.substance.SubstanceLookAndFeel;
014: import org.openide.util.NbBundle;
015: import org.openide.util.WeakSet;
016: import org.openide.util.actions.Presenter;
017:
018: /**
019: * Adds a submenu to the View menu to let the user select themes.
020: *
021: * @author Timothy Boudreau
022: */
023: public class SkinSelector extends AbstractAction implements
024: Presenter.Menu, Presenter.Popup {
025: public void actionPerformed(ActionEvent e) {
026: throw new UnsupportedOperationException();
027: }
028:
029: public JMenuItem getPopupPresenter() {
030: return getMenuPresenter();
031: }
032:
033: public JMenuItem getMenuPresenter() {
034: LookAndFeel lf = UIManager.getLookAndFeel();
035: JMenu result = new JMenu(NbBundle.getMessage(getClass(),
036: "LBL_Skins")); //NOI18N
037:
038: if (lf instanceof SubstanceLookAndFeel) {
039: SubstanceLookAndFeel slf = (SubstanceLookAndFeel) lf;
040: Map<String, SkinInfo> skins = slf.getAllSkins();
041: for (SkinInfo skinInfo : skins.values()) {
042: JMenuItem item = new JMenuItem(new ChangeSkinAction(
043: skinInfo.getDisplayName()));
044: result.add(item);
045: }
046: } else {
047: result.setText(NbBundle.getMessage(getClass(),
048: "LBL_NotRunning")); //NOI18N
049: result.setEnabled(false);
050: }
051: return result;
052: }
053:
054: private static final WeakSet allActions = new WeakSet();
055:
056: private static void updateActions() {
057: for (Iterator i = allActions.iterator(); i.hasNext();) {
058: Object skinAction = i.next();
059: if (skinAction instanceof ChangeSkinAction) {
060: ChangeSkinAction a = (ChangeSkinAction) skinAction;
061: if (a != null) {
062: a.update();
063: }
064: }
065: }
066: }
067:
068: private static final class ChangeSkinAction extends AbstractAction {
069: private final String skin;
070:
071: public ChangeSkinAction(String skin) {
072: this .skin = skin;
073: allActions.add(this );
074: update();
075: }
076:
077: private void update() {
078: String nm = skin;
079: putValue(Action.NAME, skin);
080: }
081:
082: public void actionPerformed(ActionEvent e) {
083: SkinInfo info = (SkinInfo) SubstanceLookAndFeel
084: .getAllSkins().get(skin);
085:
086: assert info != null;
087: String skinClass = info.getClassName();
088: setSkin(skinClass);
089: ButtonShapeSelector.updateUIs();
090: persist(skinClass);
091: updateActions();
092: }
093: }
094:
095: static void setSkin(String s) {
096: boolean status = SubstanceLookAndFeel.setSkin(s);
097: }
098:
099: static void persist(String value) {
100: NbPrefs p = new NbPrefs();
101: p.put("skinClassName", value); //NOI18N
102: p.put("themeClassName", null); //NOI18N
103: p.put("watermarkClassName", null); //NOI18N
104: p.put("shaperClassName", null); //NOI18N
105: p.put("painterClassName", null); //NOI18N
106: }
107:
108: public static boolean restore() {
109: NbPrefs p = new NbPrefs();
110: String preferred = p.get("skinClassName", null); //NOI18N
111: if (preferred != null) {
112: setSkin(preferred);
113: }
114: return preferred != null;
115: }
116: }
|