KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Trace2DActionAddRemoveTracePainter, action to set a ITracePainter on an ITrace2D.
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.ITrace2D;
26 import info.monitorenter.gui.chart.ITracePainter;
27 import info.monitorenter.gui.chart.layout.LayoutFactory.PropertyChangeCheckBoxMenuItem;
28
29 import java.awt.event.ActionEvent JavaDoc;
30 import java.beans.PropertyChangeEvent JavaDoc;
31
32 import javax.swing.JCheckBoxMenuItem JavaDoc;
33
34 /**
35  * <code>Action</code> that adds or removes constructor-given
36  * {@link info.monitorenter.gui.chart.ITracePainter} to the corresponding trace.
37  * <p>
38  * This action only works in combination with {@link java.awt.CheckboxMenuItem}
39  * instances that send themselve as the event object to the
40  * {@link java.awt.event.ActionEvent}(
41  * {@link java.util.EventObject#getSource()}).
42  * <p>
43  *
44  *
45  * @author <a HREF="mailto:Achim.Westermann@gmx.de">Achim Westermann </a>
46  *
47  * @version $Revision: 1.3 $
48  */

49 public final class Trace2DActionAddRemoveTracePainter extends ATrace2DAction {
50
51   /**
52    * Generated <code>serialVersionUID</code>.
53    */

54   private static final long serialVersionUID = 3978986583057707570L;
55
56   /**
57    * The stroke to set.
58    */

59   private ITracePainter m_tracePainter;
60
61   /**
62    * Create an <code>Action</code> that accesses the trace and identifies
63    * itself with the given action String.
64    * <p>
65    *
66    * @param trace
67    * the target the action will work on.
68    *
69    * @param description
70    * the descriptive <code>String</code> that will be displayed by
71    * {@link javax.swing.AbstractButton} subclasses that get this
72    * <code>Action</code> assigned (
73    * {@link javax.swing.AbstractButton#setAction(javax.swing.Action)}).
74    *
75    * @param painter
76    * the painter to add / remove from the trace.
77    */

78   public Trace2DActionAddRemoveTracePainter(final ITrace2D trace, final String JavaDoc description,
79       final ITracePainter painter) {
80     super(trace, description);
81     this.m_tracePainter = painter;
82     trace.addPropertyChangeListener(ITrace2D.PROPERTY_PAINTERS, this);
83   }
84
85   /**
86    * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
87    */

88   public void actionPerformed(final ActionEvent JavaDoc e) {
89     JCheckBoxMenuItem JavaDoc item = (JCheckBoxMenuItem JavaDoc) e.getSource();
90     boolean state = item.getState();
91     if (state) {
92       this.m_trace.addTracePainter(this.m_tracePainter);
93     } else {
94       boolean success = this.m_trace.removeTracePainter(this.m_tracePainter);
95       if (success) {
96         // nop
97
} else {
98         // rewind state as this could not be done:
99
// contract for ITrace2D is, that at least one renderer should be
100
// defined!
101
item.setState(true);
102         item.invalidate();
103         item.repaint();
104       }
105     }
106   }
107
108   /**
109    * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
110    */

111   public void propertyChange(final PropertyChangeEvent JavaDoc evt) {
112     String JavaDoc property = evt.getPropertyName();
113     if (property.equals(ITrace2D.PROPERTY_PAINTERS)) {
114       ITracePainter oldValue = (ITracePainter) evt.getOldValue();
115       ITracePainter newValue = (ITracePainter) evt.getNewValue();
116       // added or removed?
117
if (oldValue == null) {
118         // added
119
if (newValue.getClass() == this.m_tracePainter.getClass()) {
120           this.firePropertyChange(PropertyChangeCheckBoxMenuItem.PROPERTY_SELECTED, new Boolean JavaDoc(
121               false), new Boolean JavaDoc(true));
122         }
123       } else {
124         // removed
125
if (oldValue.getClass() == this.m_tracePainter.getClass()) {
126           this.firePropertyChange(PropertyChangeCheckBoxMenuItem.PROPERTY_SELECTED, new Boolean JavaDoc(
127               true), new Boolean JavaDoc(false));
128         }
129       }
130     }
131   }
132 }
133
Popular Tags