1 24 package info.monitorenter.gui.chart.events; 25 26 import info.monitorenter.gui.chart.Chart2D; 27 import info.monitorenter.gui.chart.layout.LayoutFactory.PropertyChangeCheckBoxMenuItem; 28 29 import java.awt.Color ; 30 import java.awt.event.ActionEvent ; 31 import java.beans.PropertyChangeEvent ; 32 import java.util.HashMap ; 33 import java.util.Map ; 34 35 import javax.swing.JColorChooser ; 36 import javax.swing.JComponent ; 37 38 52 public final class JComponentActionSetCustomForegroundSingleton 53 extends AJComponentAction { 54 55 58 private static final long serialVersionUID = 3904680491952451890L; 59 60 64 private Color m_lastChosenColor; 65 66 80 private JComponentActionSetCustomForegroundSingleton(final JComponent component, 81 final String description) { 82 super(component, description); 83 component.addPropertyChangeListener(Chart2D.PROPERTY_FOREGROUND_COLOR, this); 84 } 85 86 89 private static Map instances = new HashMap (); 90 91 92 private static String key(final JComponent component) { 93 return component.getClass().getName() + component.hashCode(); 94 } 95 96 114 public static JComponentActionSetCustomForegroundSingleton getInstance( 115 final JComponent component, final String description) { 116 JComponentActionSetCustomForegroundSingleton result = 117 (JComponentActionSetCustomForegroundSingleton) JComponentActionSetCustomForegroundSingleton.instances 118 .get(key(component)); 119 if (result == null) { 120 result = new JComponentActionSetCustomForegroundSingleton(component, description); 121 JComponentActionSetCustomForegroundSingleton.instances.put(key(component), result); 122 } 123 return result; 124 } 125 126 129 public void actionPerformed(final ActionEvent e) { 130 Color chosen = JColorChooser.showDialog(this.m_component, "choose background color for " 131 + this.m_component.getName(), this.m_component.getBackground()); 132 if (chosen != null) { 133 this.m_lastChosenColor = chosen; 134 this.m_component.setForeground(chosen); 135 } 136 } 137 138 141 public void propertyChange(final PropertyChangeEvent evt) { 142 String property = evt.getPropertyName(); 143 if (property.equals(Chart2D.PROPERTY_FOREGROUND_COLOR)) { 144 Color newColor = (Color ) evt.getNewValue(); 145 if (newColor.equals(this.m_lastChosenColor)) { 146 this.firePropertyChange(PropertyChangeCheckBoxMenuItem.PROPERTY_SELECTED, 147 new Boolean (false), new Boolean (true)); 148 149 } else { 150 this.firePropertyChange(PropertyChangeCheckBoxMenuItem.PROPERTY_SELECTED, 151 new Boolean (true), new Boolean (false)); 152 } 153 } 154 } 155 } 156 | Popular Tags |