KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > monitorenter > gui > chart > rangepolicies > ARangePolicy


1 /*
2  * AbstractRangePolicy.java of project jchart2d, A default superclass for chart viewport
3  * implementations that adds support for setting and getting ranges.
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 package info.monitorenter.gui.chart.rangepolicies;
23
24 import info.monitorenter.gui.chart.Chart2D;
25 import info.monitorenter.gui.chart.IRangePolicy;
26 import info.monitorenter.util.Range;
27
28 import java.beans.PropertyChangeListener JavaDoc;
29 import java.beans.PropertyChangeSupport JavaDoc;
30
31 import javax.swing.event.SwingPropertyChangeSupport JavaDoc;
32
33 /**
34  * <p>
35  * A default superclass for IRangePolicy implementations that adds support for
36  * setting and getting ranges.
37  * </p>
38  * <p>
39  * Should be used by any implementation that really works on the data of ranges
40  * (not unbounded ranges). Subclasses should access the internal member range or
41  * use {@link #getRange()}.
42  * </p>
43  * <h3>Property Change events</h3>
44  * <p>
45  * <table border="0">
46  * <tr>
47  * <th><code>property</code></th>
48  * <th><code>oldValue</code></th>
49  * <th><code>newValue</code></th>
50  * <th>occurance</th>
51  * </tr>
52  * <tr>
53  * <td><code>{@link #PROPERTY_RANGE}</code></td>
54  * <td><code>{@link info.monitorenter.util.Range}</code> that changed</td>
55  * <td><code>{@link info.monitorenter.util.Range}</code>, the new value</td>
56  * <td>Fired if any bound of the range changed (min or max).</td>
57  * </tr>
58  * <tr>
59  * <tr>
60  * <td><code>{@link #PROPERTY_RANGE_MAX}</code></td>
61  * <td><code>{@link java.lang.Double}</code>, the old max value of the
62  * range. </td>
63  * <td><code>{@link info.monitorenter.util.Range}</code>, the new max value
64  * of the range. </td>
65  * <td></td>
66  * </tr>
67  * <tr>
68  * <td><code>{@link #PROPERTY_RANGE_MIN}</code></td>
69  * <td><code>{@link java.lang.Double}</code>, the old min value of the
70  * range. </td>
71  * <td><code>{@link info.monitorenter.util.Range}</code>, the new min value
72  * of the range. </td>
73  * <td></td>
74  * </tr>
75  * </table>
76  * <p>
77  *
78  * @author <a HREF="mailto:Achim.Westermann@gmx.de">Achim Westermann </a>
79  * @version $Revision: 1.2 $
80  */

81 public abstract class ARangePolicy implements IRangePolicy {
82   /**
83    * The property key defining the <code>max</code> property.
84    * <p>
85    * Use in combination with
86    * {@link #addPropertyChangeListener(String, PropertyChangeListener)}.
87    * <p>
88    */

89   public static final String JavaDoc PROPERTY_RANGE_MAX = "rangepolicy.rangemax";
90
91   /**
92    * The property key defining a change of the <code>min</code> or the
93    * <code>max</code> property.
94    * <p>
95    * Use in combination with
96    * {@link #addPropertyChangeListener(String, PropertyChangeListener)}.
97    * <p>
98    */

99   public static final String JavaDoc PROPERTY_RANGE = "rangepolicy.range";
100
101   /**
102    * The property key defining the <code>min</code> property.
103    * <p>
104    * Use in combination with
105    * {@link #addPropertyChangeListener(String, PropertyChangeListener)}.
106    * <p>
107    */

108   public static final String JavaDoc PROPERTY_RANGE_MIN = "rangepolicy.rangemin";
109
110   /**
111    * The instance that add support for firing <code>PropertyChangeEvents</code>
112    * and maintaining <code>PropertyChangeListeners</code>.
113    * {@link PropertyChangeListener} instances.
114    */

115
116   protected PropertyChangeSupport JavaDoc m_propertyChangeSupport = new SwingPropertyChangeSupport JavaDoc(this);
117
118   /**
119    * The internal range that may be taken into account for returning bounds from
120    * {@link IRangePolicy#getMax(double, double)} and
121    * {@link IRangePolicy#getMax(double, double)}.
122    * <p>
123    */

124   private Range m_range;
125
126   /**
127    * Creates a range policy with an unconfigured range ({@link Range#RANGE_UNBOUNDED}).
128    * <p>
129    *
130    */

131   public ARangePolicy() {
132     this.m_range = Range.RANGE_UNBOUNDED;
133   }
134
135   /**
136    * Creates a range policy backed by the given range.
137    * <p>
138    *
139    * @param range
140    * the range that may be used to decide about the policy of
141    * displaying the range.
142    */

143   public ARangePolicy(final Range range) {
144     this.m_range = range;
145   }
146
147   /**
148    * Adds a property change listener.
149    * <p>
150    *
151    * @param propertyName
152    * The name of the property to listen on.
153    * @param listener
154    * The PropertyChangeListener to be added.
155    * @see info.monitorenter.gui.chart.ITrace2D#addPropertyChangeListener(java.lang.String,
156    * java.beans.PropertyChangeListener)
157    */

158   public final void addPropertyChangeListener(final String JavaDoc propertyName,
159       final PropertyChangeListener JavaDoc listener) {
160     this.m_propertyChangeSupport.addPropertyChangeListener(propertyName, listener);
161   }
162
163   /**
164    * <p>
165    * Fires a property change event to the registered listeners.
166    * </p>
167    *
168    * @param property
169    * one of the <code>PROPERTY_XXX</code> constants defined in
170    * <code>{@link info.monitorenter.gui.chart.ITrace2D}</code>.
171    *
172    * @param oldvalue
173    * the old value of the property.
174    *
175    * @param newvalue
176    * the new value of the property.
177    */

178   protected final void firePropertyChange(final String JavaDoc property, final Object JavaDoc oldvalue,
179       final Object JavaDoc newvalue) {
180     if (Chart2D.DEBUG_THREADING) {
181       System.out.println("AbstractRangePolicy.firePropertyChange (" + property + "), 0 locks");
182     }
183     this.m_propertyChangeSupport.firePropertyChange(property, oldvalue, newvalue);
184   }
185
186   /**
187    * @see info.monitorenter.gui.chart.ITrace2D#getPropertyChangeListeners(String)
188    */

189   public PropertyChangeListener JavaDoc[] getPropertyChangeListeners(final String JavaDoc property) {
190     return this.m_propertyChangeSupport.getPropertyChangeListeners(property);
191   }
192
193   /**
194    * Returns the internal range that is used to decide about the policy of
195    * displaying the chart.
196    * <p>
197    *
198    * @return the internal range that may be taken into account for returning
199    * bounds from {@link IRangePolicy#getMax(double, double)} and
200    * {@link IRangePolicy#getMax(double, double)}.
201    */

202   public final Range getRange() {
203     return this.m_range;
204   }
205
206   /**
207    * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
208    */

209   public void removePropertyChangeListener(final PropertyChangeListener JavaDoc listener) {
210     this.m_propertyChangeSupport.removePropertyChangeListener(listener);
211   }
212
213   /**
214    * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.lang.String,
215    * java.beans.PropertyChangeListener)
216    */

217   public void removePropertyChangeListener(final String JavaDoc property,
218       final PropertyChangeListener JavaDoc listener) {
219     this.m_propertyChangeSupport.removePropertyChangeListener(property, listener);
220   }
221
222   /**
223    * Sets the internal range that is used to decide about the policy of
224    * displaying the chart.
225    * <p>
226    *
227    * @param range
228    * the internal range that may be taken into account for returning
229    * bounds from {@link IRangePolicy#getMax(double, double)} and
230    * {@link IRangePolicy#getMax(double, double)}.
231    */

232   public void setRange(final Range range) {
233     double oldMin = this.m_range.getMin();
234     double oldMax = this.m_range.getMax();
235     Range oldRange = this.m_range;
236     boolean minchanged = range.getMin() != oldMin;
237     boolean maxchanged = range.getMax() != oldMax;
238     this.m_range = range;
239     if (minchanged && maxchanged) {
240       this.firePropertyChange(ARangePolicy.PROPERTY_RANGE, oldRange, this.m_range);
241     } else if (minchanged) {
242       this.firePropertyChange(ARangePolicy.PROPERTY_RANGE_MIN, new Double JavaDoc(oldMin), new Double JavaDoc(range
243           .getMin()));
244     } else if (maxchanged) {
245       this.firePropertyChange(ARangePolicy.PROPERTY_RANGE_MAX, new Double JavaDoc(oldMax), new Double JavaDoc(range
246           .getMax()));
247     }
248
249   }
250
251 }
252
Popular Tags