KickJava   Java API By Example, From Geeks To Geeks.

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


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

23 package info.monitorenter.gui.chart.events;
24
25 import info.monitorenter.gui.chart.Chart2D;
26 import info.monitorenter.gui.chart.layout.LayoutFactory.PropertyChangeCheckBoxMenuItem;
27
28 import java.awt.Color JavaDoc;
29 import java.awt.event.ActionEvent JavaDoc;
30 import java.beans.PropertyChangeEvent JavaDoc;
31
32 import javax.swing.JColorChooser JavaDoc;
33 import javax.swing.JComponent JavaDoc;
34
35 /**
36  * <p>
37  * <code>Action</code> that sets a custom foreground color of the
38  * corresponding <code>JComponent</code> by 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 JComponentActionSetCustomForeground extends AJComponentAction {
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_lastChosenColor;
57
58   /**
59    * Create an <code>Action</code> that accesses the trace and identifies
60    * itself with the given action String.
61    * <p>
62    *
63    * @param component
64    * the target the action will work on.
65    *
66    * @param description
67    * the descriptive <code>String</code> that will be displayed by
68    * {@link javax.swing.AbstractButton} subclasses that get this
69    * <code>Action</code> assigned (
70    * {@link javax.swing.AbstractButton#setAction(javax.swing.Action)}).
71    */

72   public JComponentActionSetCustomForeground(final JComponent JavaDoc component, final String JavaDoc description) {
73     super(component, description);
74     component.addPropertyChangeListener(Chart2D.PROPERTY_FOREGROUND_COLOR, this);
75   }
76
77   /**
78    * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
79    */

80   public void actionPerformed(final ActionEvent JavaDoc e) {
81     Color JavaDoc chosen = JColorChooser.showDialog(this.m_component, "choose foreground color for "
82         + this.m_component.getName(), this.m_component.getForeground());
83     this.m_component.setForeground(chosen);
84   }
85
86   /**
87    * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
88    */

89   public void propertyChange(final PropertyChangeEvent JavaDoc evt) {
90     String JavaDoc property = evt.getPropertyName();
91     if (property.equals(Chart2D.PROPERTY_FOREGROUND_COLOR)) {
92       Color JavaDoc newColor = (Color JavaDoc) evt.getNewValue();
93       if (newColor.equals(this.m_lastChosenColor)) {
94         this.firePropertyChange(PropertyChangeCheckBoxMenuItem.PROPERTY_SELECTED,
95             new Boolean JavaDoc(false), new Boolean JavaDoc(true));
96
97       } else {
98         this.firePropertyChange(PropertyChangeCheckBoxMenuItem.PROPERTY_SELECTED,
99             new Boolean JavaDoc(true), new Boolean JavaDoc(false));
100       }
101     }
102   }
103 }
104
Popular Tags