KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > monitorenter > gui > chart > events > JComponentActionSetCustomForegroundSingleton


1 /*
2  * JComponentActionSetCustomForegroundSingleton,
3  * singleton action to set a custom foreground color of a JComponent.
4  * Copyright (C) Achim Westermann, created on 10.12.2004, 13:48:55
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  *
20  * If you modify or optimize the code in a useful way please let me know.
21  * Achim.Westermann@gmx.de
22  *
23  */

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 JavaDoc;
30 import java.awt.event.ActionEvent JavaDoc;
31 import java.beans.PropertyChangeEvent JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import javax.swing.JColorChooser JavaDoc;
36 import javax.swing.JComponent JavaDoc;
37
38 /**
39  * Singleton <code>Action</code> that sets a custom foreground color of the
40  * corresponding <code>JComponent</code> by showing a modal color chooser.
41  * <p>
42  * Only one instance per target component may exist.
43  * <p>
44  *
45  * @see info.monitorenter.gui.chart.events.JComponentActionSetCustomForeground
46  *
47  *
48  * @author <a HREF="mailto:Achim.Westermann@gmx.de">Achim Westermann </a>
49  *
50  * @version $Revision: 1.1 $
51  */

52 public final class JComponentActionSetCustomForegroundSingleton
53     extends AJComponentAction {
54
55   /**
56    * Generated serial version ID.
57    */

58   private static final long serialVersionUID = 3904680491952451890L;
59
60   /**
61    * Reference to the last custom color chosen to check wether the corresponding
62    * menu is selected.
63    */

64   private Color JavaDoc m_lastChosenColor;
65
66   /**
67    * Create an <code>Action</code> that accesses the trace and identifies
68    * itself with the given action String.
69    * <p>
70    *
71    * @param component
72    * the target the action will work on.
73    *
74    * @param description
75    * the descriptive <code>String</code> that will be displayed by
76    * {@link javax.swing.AbstractButton} subclasses that get this
77    * <code>Action</code> assigned (
78    * {@link javax.swing.AbstractButton#setAction(javax.swing.Action)}).
79    */

80   private JComponentActionSetCustomForegroundSingleton(final JComponent JavaDoc component,
81       final String JavaDoc description) {
82     super(component, description);
83     component.addPropertyChangeListener(Chart2D.PROPERTY_FOREGROUND_COLOR, this);
84   }
85
86   /**
87    * Map for instances.
88    */

89   private static Map JavaDoc instances = new HashMap JavaDoc();
90
91   /** Creates a key for the component for internal storage. */
92   private static String JavaDoc key(final JComponent JavaDoc component) {
93     return component.getClass().getName() + component.hashCode();
94   }
95
96   /**
97    * Returns the single instance for the given component, potentially creating
98    * it.
99    * <p>
100    *
101    * If an instance for the given component had been created the description
102    * String is ignored.
103    * <p>
104    *
105    * @param component
106    * the component to get the instance for (works as key).
107    *
108    * @param description
109    * the description to use (ignored if instance for component has been
110    * created before).
111    *
112    * @return the single instance for the given component.
113    */

114   public static JComponentActionSetCustomForegroundSingleton getInstance(
115       final JComponent JavaDoc component, final String JavaDoc 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   /**
127    * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
128    */

129   public void actionPerformed(final ActionEvent JavaDoc e) {
130     Color JavaDoc 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   /**
139    * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
140    */

141   public void propertyChange(final PropertyChangeEvent JavaDoc evt) {
142     String JavaDoc property = evt.getPropertyName();
143     if (property.equals(Chart2D.PROPERTY_FOREGROUND_COLOR)) {
144       Color JavaDoc newColor = (Color JavaDoc) evt.getNewValue();
145       if (newColor.equals(this.m_lastChosenColor)) {
146         this.firePropertyChange(PropertyChangeCheckBoxMenuItem.PROPERTY_SELECTED,
147             new Boolean JavaDoc(false), new Boolean JavaDoc(true));
148
149       } else {
150         this.firePropertyChange(PropertyChangeCheckBoxMenuItem.PROPERTY_SELECTED,
151             new Boolean JavaDoc(true), new Boolean JavaDoc(false));
152       }
153     }
154   }
155 }
156
Popular Tags