KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Chart2DActionSetCustomGridColorSingleton,
3  * singleton action that sets a custom grid color to the chart.
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
37 /**
38  * Singleton <code>Action</code> that sets a custom grid color to the corresponding
39  * chart ({@link Chart2D#setGridColor(Color)}) by showing a modal color
40  * chooser.
41  * <p>
42  * Only one instance per target component may exist.
43  * <p>
44  *
45  * @see info.monitorenter.gui.chart.events.Chart2DActionSetCustomGridColor
46  *
47  * @author <a HREF="mailto:Achim.Westermann@gmx.de">Achim Westermann </a>
48  *
49  * @version $Revision: 1.1 $
50  */

51 public final class Chart2DActionSetCustomGridColorSingleton extends AChart2DAction {
52   /**
53    * Generated <code>serialVersionUID</code>.
54    */

55   private static final long serialVersionUID = 3691034370412916788L;
56   /**
57    * Map for instances.
58    */

59   private static Map JavaDoc instances = new HashMap JavaDoc();
60
61   /** Creates a key for the component for internal storage. */
62   private static String JavaDoc key(final Chart2D chart) {
63     return chart.getClass().getName() + chart.hashCode();
64   }
65
66   /**
67    * Returns the single instance for the given component, potentially creating
68    * it.
69    * <p>
70    *
71    * If an instance for the given component had been created the description
72    * String is ignored.
73    * <p>
74    *
75     * @param chart
76    * the target the action will work on
77    * @param colorName
78    * the descriptive <code>String</code> that will be displayed by
79    * {@link javax.swing.AbstractButton} subclasses that get this
80    * <code>Action</code> assigned (
81    * {@link javax.swing.AbstractButton#setAction(javax.swing.Action)}).
82    *
83    * @return the single instance for the given component.
84    */

85   public static Chart2DActionSetCustomGridColorSingleton getInstance(
86       final Chart2D chart, final String JavaDoc colorName) {
87     Chart2DActionSetCustomGridColorSingleton result =
88       (Chart2DActionSetCustomGridColorSingleton) Chart2DActionSetCustomGridColorSingleton.instances
89         .get(key(chart));
90     if (result == null) {
91       result = new Chart2DActionSetCustomGridColorSingleton(chart, colorName);
92       Chart2DActionSetCustomGridColorSingleton.instances.put(key(chart), result);
93     }
94     return result;
95   }
96   
97   /**
98    * Reference to the last custom color chosen to check wether the corresponding
99    * menu is selected.
100    */

101   private Color JavaDoc m_lastChosenColor;
102
103   /**
104    * Create an <code>Action</code> that accesses the trace and identifies
105    * itself with the given action String.
106    * <p>
107    *
108    * @param chart
109    * the target the action will work on
110    * @param colorName
111    * the descriptive <code>String</code> that will be displayed by
112    * {@link javax.swing.AbstractButton} subclasses that get this
113    * <code>Action</code> assigned (
114    * {@link javax.swing.AbstractButton#setAction(javax.swing.Action)}).
115    */

116   private Chart2DActionSetCustomGridColorSingleton(final Chart2D chart, final String JavaDoc colorName) {
117     super(chart, colorName);
118     chart.addPropertyChangeListener(Chart2D.PROPERTY_GRID_COLOR, this);
119   }
120
121   /**
122    * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
123    */

124   public void actionPerformed(final ActionEvent JavaDoc e) {
125     Color JavaDoc chosen = JColorChooser.showDialog(this.m_chart, "choose color for "
126         + this.m_chart.getName(), this.m_chart.getGridColor());
127     this.m_lastChosenColor = chosen;
128     this.m_chart.setGridColor(chosen);
129   }
130
131   /**
132    * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
133    */

134   public void propertyChange(final PropertyChangeEvent JavaDoc evt) {
135     String JavaDoc property = evt.getPropertyName();
136     if (property.equals(Chart2D.PROPERTY_GRID_COLOR)) {
137       Color JavaDoc newColor = (Color JavaDoc) evt.getNewValue();
138       if (newColor.equals(this.m_lastChosenColor)) {
139         this.firePropertyChange(PropertyChangeCheckBoxMenuItem.PROPERTY_SELECTED,
140             new Boolean JavaDoc(false), new Boolean JavaDoc(true));
141
142       } else {
143         this.firePropertyChange(PropertyChangeCheckBoxMenuItem.PROPERTY_SELECTED,
144             new Boolean JavaDoc(true), new Boolean JavaDoc(false));
145       }
146     }
147   }
148 }
149
Popular Tags