KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Trace2DActionSetCustomColor, action to set a custom (runtime chosen)
3  * color to a ITrace2D.
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.ITrace2D;
27 import info.monitorenter.gui.chart.layout.LayoutFactory.PropertyChangeCheckBoxMenuItem;
28
29 import java.awt.Color JavaDoc;
30 import java.awt.Component JavaDoc;
31 import java.awt.event.ActionEvent JavaDoc;
32 import java.beans.PropertyChangeEvent JavaDoc;
33
34 import javax.swing.JColorChooser JavaDoc;
35
36 /**
37  * <code>Action</code> that sets a custom color of the corresponding trace by
38  * showing a modal color chooser.
39  * <p>
40  *
41  * @author <a HREF="mailto:Achim.Westermann@gmx.de">Achim Westermann </a>
42  *
43  * @version $Revision: 1.2 $
44  */

45 public final class Trace2DActionSetCustomColor extends ATrace2DAction {
46
47   /**
48    * Generated <code>serialVersionUID</code>.
49    */

50   private static final long serialVersionUID = 3904680491952451890L;
51
52   /**
53    * Reference to the last custom color chosen to check wether the corresponding
54    * menu is selected.
55    */

56   private Color JavaDoc m_lastChosen;
57
58   /**
59    * The component this instance will be registered to as a listener.
60    * <p>
61    *
62    * @see Component#addMouseListener(java.awt.event.MouseListener)
63    *
64    */

65   private Component JavaDoc m_trigger;
66
67   /**
68    * Create an <code>Action</code> that accesses the trace and identifies
69    * itself with the given action String.
70    * <p>
71    *
72    * @param trace
73    * the target the action will work on.
74    *
75    * @param description
76    * the descriptive <code>String</code> that will be displayed by
77    * {@link javax.swing.AbstractButton} subclasses that get this
78    * <code>Action</code> assigned (
79    * {@link javax.swing.AbstractButton#setAction(javax.swing.Action)}).
80    *
81    * @param trigger
82    * the <code>Component</code> the modal color chooser dialog will
83    * be related to.
84    * <p>
85    */

86   public Trace2DActionSetCustomColor(final ITrace2D trace, final String JavaDoc description,
87       final Component JavaDoc trigger) {
88     super(trace, description);
89     this.m_trigger = trigger;
90     trace.addPropertyChangeListener(ITrace2D.PROPERTY_COLOR, this);
91   }
92
93   /**
94    * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
95    */

96   public void actionPerformed(final ActionEvent JavaDoc e) {
97     Color JavaDoc chosen = JColorChooser.showDialog(this.m_trigger, "choose color for "
98         + this.m_trace.getName(), this.m_trace.getColor());
99     this.m_lastChosen = chosen;
100     this.m_trace.setColor(chosen);
101   }
102
103   /**
104    * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
105    */

106   public void propertyChange(final PropertyChangeEvent JavaDoc evt) {
107     String JavaDoc property = evt.getPropertyName();
108     if (property.equals(ITrace2D.PROPERTY_COLOR)) {
109       Color JavaDoc newValue = (Color JavaDoc) evt.getNewValue();
110       if (newValue.equals(this.m_lastChosen)) {
111         this.firePropertyChange(PropertyChangeCheckBoxMenuItem.PROPERTY_SELECTED,
112             new Boolean JavaDoc(false), new Boolean JavaDoc(true));
113       } else {
114         this.firePropertyChange(PropertyChangeCheckBoxMenuItem.PROPERTY_SELECTED,
115             new Boolean JavaDoc(true), new Boolean JavaDoc(false));
116       }
117     }
118   }
119 }
120
Popular Tags