001: package org.jvnet.substance.netbeans.ui;
002:
003: import java.awt.event.ActionEvent;
004: import java.beans.PropertyChangeListener;
005: import java.util.Iterator;
006: import java.util.Map;
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.jvnet.substance.painter.GradientPainterInfo;
015: import org.openide.util.NbBundle;
016: import org.openide.util.WeakListeners;
017: import org.openide.util.WeakSet;
018: import org.openide.util.actions.Presenter;
019:
020: /**
021: * Adds a submenu to the View menu to let the user select gradients.
022: *
023: * @author Timothy Boudreau
024: */
025: public class ColorSelector extends AbstractAction implements
026: Presenter.Menu, Presenter.Popup {
027:
028: public void actionPerformed(ActionEvent e) {
029: throw new UnsupportedOperationException();
030: }
031:
032: public JMenuItem getPopupPresenter() {
033: return getMenuPresenter();
034: }
035:
036: public JMenuItem getMenuPresenter() {
037: LookAndFeel lf = UIManager.getLookAndFeel();
038: JMenu result = new JMenu(NbBundle.getMessage(getClass(),
039: "LBL_Colors")); //NOI18N
040:
041: if (lf instanceof SubstanceLookAndFeel) {
042: SubstanceLookAndFeel slf = (SubstanceLookAndFeel) lf;
043: Map schemes = slf.getAllGradientPainters();
044: for (Iterator i = schemes.keySet().iterator(); i.hasNext();) {
045: String scheme = (String) i.next();
046: JMenuItem item = new JMenuItem(new ChangeSchemeAction(
047: scheme));
048: result.add(item);
049: }
050: } else {
051: result.setText(NbBundle.getMessage(getClass(),
052: "LBL_NotRunning")); //NOI18N
053: result.setEnabled(false);
054: }
055: return result;
056: }
057:
058: private static final WeakSet allActions = new WeakSet();
059:
060: private static void updateActions() {
061: for (Iterator i = allActions.iterator(); i.hasNext();) {
062: ChangeSchemeAction a = (ChangeSchemeAction) i.next();
063: if (a != null) {
064: a.update();
065: }
066: }
067: }
068:
069: private static final class ChangeSchemeAction extends
070: AbstractAction {
071: private final String painterName;
072:
073: public ChangeSchemeAction(String painterName) {
074: this .painterName = painterName;
075: update();
076: allActions.add(this );
077: }
078:
079: private void update() {
080: String nm = painterName;
081: if (nm.equals(SubstanceLookAndFeel
082: .getCurrentGradientPainterName())) {
083: nm = "<html><b>" + nm + "</b></html>"; //NOI18N
084: }
085: putValue(Action.NAME, nm);
086: }
087:
088: public void addPropertyChangeListener(PropertyChangeListener pcl) {
089: //ensure old menu items can be garbage collected
090: super .addPropertyChangeListener(WeakListeners
091: .propertyChange(pcl, this ));
092: }
093:
094: public void actionPerformed(ActionEvent e) {
095: GradientPainterInfo info = (GradientPainterInfo) SubstanceLookAndFeel
096: .getAllGradientPainters().get(painterName);
097:
098: assert info != null;
099: String painterClassName = info.getClassName();
100: setGradientPainter(painterClassName);
101: ButtonShapeSelector.updateUIs();
102: persist(painterClassName);
103: updateActions();
104: }
105: }
106:
107: static void setGradientPainter(String s) {
108: SubstanceLookAndFeel.setCurrentGradientPainter(s);
109: }
110:
111: static void persist(String value) {
112: NbPrefs p = new NbPrefs();
113: p.put("painterClassName", value); //NOI18N
114: }
115:
116: public static boolean restore() {
117: NbPrefs p = new NbPrefs();
118: String preferred = p.get("painterClassName", null); //NOI18N
119: if (preferred != null) {
120: setGradientPainter(preferred);
121: }
122: return preferred != null;
123: }
124: }
|