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 JComponentActionSetCustomBackgroundSingleton 53 extends AJComponentAction { 54 55 58 private static final long serialVersionUID = 3904680491952451890L; 59 60 64 private Color m_lastChosenColor; 65 66 80 private JComponentActionSetCustomBackgroundSingleton(final JComponent component, 81 final String description) { 82 super(component, description); 83 component.addPropertyChangeListener( 84 Chart2D.PROPERTY_BACKGROUND_COLOR, this); 85 } 86 87 90 private static Map instances = new HashMap (); 91 92 93 private static String key(final JComponent component) { 94 return component.getClass().getName() + component.hashCode(); 95 } 96 97 115 public static JComponentActionSetCustomBackgroundSingleton getInstance( 116 final JComponent component, final String description) { 117 JComponentActionSetCustomBackgroundSingleton result = 118 (JComponentActionSetCustomBackgroundSingleton) JComponentActionSetCustomBackgroundSingleton.instances 119 .get(key(component)); 120 if (result == null) { 121 result = new JComponentActionSetCustomBackgroundSingleton(component, description); 122 JComponentActionSetCustomBackgroundSingleton.instances.put( 123 key(component), result); 124 } 125 return result; 126 } 127 128 131 public void actionPerformed(final ActionEvent e) { 132 Color chosen = JColorChooser.showDialog( 133 this.m_component, "choose background color for " + this.m_component.getName(), 134 this.m_component.getBackground()); 135 if (chosen != null) { 136 this.m_lastChosenColor = chosen; 137 this.m_component.setBackground(chosen); 138 } 139 } 140 141 144 public void propertyChange(final PropertyChangeEvent evt) { 145 String property = evt.getPropertyName(); 146 if (property.equals(Chart2D.PROPERTY_BACKGROUND_COLOR)) { 147 Color newColor = (Color ) evt.getNewValue(); 148 if (newColor.equals(this.m_lastChosenColor)) { 149 this 150 .firePropertyChange( 151 PropertyChangeCheckBoxMenuItem.PROPERTY_SELECTED, new Boolean (false), new Boolean ( 152 true)); 153 154 } else { 155 this 156 .firePropertyChange( 157 PropertyChangeCheckBoxMenuItem.PROPERTY_SELECTED, new Boolean (true), new Boolean ( 158 false)); 159 } 160 } 161 } 162 } 163 | Popular Tags |