KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JComponentActionSetCustomBackgroundSingleton,
3  * singleton action to set a custom background 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 background 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.JComponentActionSetCustomBackground
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 JComponentActionSetCustomBackgroundSingleton
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 JComponentActionSetCustomBackgroundSingleton(final JComponent JavaDoc component,
81       final String JavaDoc description) {
82     super(component, description);
83     component.addPropertyChangeListener(
84         Chart2D.PROPERTY_BACKGROUND_COLOR, this);
85   }
86
87   /**
88    * Map for instances.
89    */

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

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

131   public void actionPerformed(final ActionEvent JavaDoc e) {
132     Color JavaDoc 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   /**
142    * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
143    */

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