KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > experimental > chart > plot > dial > StandardDialRange


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * ----------------------
28  * StandardDialRange.java
29  * ----------------------
30  * (C) Copyright 2006, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: StandardDialRange.java,v 1.1.2.2 2006/11/06 16:26:07 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 03-Nov-2006 : Version 1 (DG);
40  *
41  */

42
43 package org.jfree.experimental.chart.plot.dial;
44
45 import java.awt.BasicStroke JavaDoc;
46 import java.awt.Color JavaDoc;
47 import java.awt.Graphics2D JavaDoc;
48 import java.awt.Paint JavaDoc;
49 import java.awt.geom.Arc2D JavaDoc;
50 import java.awt.geom.Rectangle2D JavaDoc;
51 import java.io.IOException JavaDoc;
52 import java.io.ObjectInputStream JavaDoc;
53 import java.io.ObjectOutputStream JavaDoc;
54 import java.io.Serializable JavaDoc;
55
56 import org.jfree.chart.HashUtilities;
57 import org.jfree.io.SerialUtilities;
58 import org.jfree.util.PaintUtilities;
59 import org.jfree.util.PublicCloneable;
60
61 /**
62  * A layer that draws a range highlight on a dial plot.
63  */

64 public class StandardDialRange extends AbstractDialLayer implements DialLayer,
65         Cloneable JavaDoc, PublicCloneable, Serializable JavaDoc {
66     
67     /** The minimum data value for the scale. */
68     private double lowerBound;
69     
70     /** The maximum data value for the scale. */
71     private double upperBound;
72     
73     /**
74      * The increment (in data units) for each section.
75      */

76     private double increment;
77
78     /**
79      * The paint used to draw the range highlight. This field is transient
80      * because it requires special handling for serialization.
81      */

82     private transient Paint JavaDoc paint;
83     
84     /**
85      * The factor (in the range 0.0 to 1.0) that determines the inside limit
86      * of the range highlight.
87      */

88     private double innerRadius;
89
90     /**
91      * The factor (in the range 0.0 to 1.0) that determines the outside limit
92      * of the range highlight.
93      */

94     private double outerRadius;
95     
96     /**
97      * Creates a new instance of <code>StandardDialRange</code>.
98      */

99     public StandardDialRange() {
100         this(0.0, 100.0, Color.white);
101     }
102     
103     /**
104      * Creates a new instance of <code>StandardDialRange</code>.
105      *
106      * @param lower the lower bound.
107      * @param upper the upper bound.
108      * @param paint the paint (<code>null</code> not permitted).
109      */

110     public StandardDialRange(double lower, double upper, Paint JavaDoc paint) {
111         if (paint == null) {
112             throw new IllegalArgumentException JavaDoc("Null 'paint' argument.");
113         }
114         this.lowerBound = lower;
115         this.upperBound = upper;
116         this.paint = paint;
117     }
118     
119     /**
120      * Returns the lower bound (a data value) of the dial range.
121      *
122      * @return The lower bound of the dial range.
123      *
124      * @see #setLowerBound(double)
125      */

126     public double getLowerBound() {
127         return this.lowerBound;
128     }
129     
130     /**
131      * Sets the lower bound of the dial range.
132      *
133      * @param bound the lower bound.
134      *
135      * @see #getLowerBound()
136      */

137     public void setLowerBound(double bound) {
138         // FIXME: check
139
this.lowerBound = bound;
140         notifyListeners(new DialLayerChangeEvent(this));
141     }
142     
143     /**
144      * Returns the upper bound of the dial range.
145      *
146      * @return The upper bound.
147      *
148      * @see #setUpperBound(double)
149      */

150     public double getUpperBound() {
151         return this.upperBound;
152     }
153     
154     /**
155      * Sets the upper bound of the dial range.
156      *
157      * @param bound the upper bound.
158      *
159      * @see #getUpperBound()
160      */

161     public void setUpperBound(double bound) {
162         // FIXME: check
163
this.upperBound = bound;
164         notifyListeners(new DialLayerChangeEvent(this));
165     }
166     
167     /**
168      * Returns the increment between tick marks.
169      *
170      * @return The increment.
171      *
172      * @see #setIncrement(double)
173      */

174     public double getIncrement() {
175         return this.increment;
176     }
177     
178     /**
179      * Sets the increment.
180      *
181      * @param increment the increment.
182      *
183      * @see #getIncrement()
184      */

185     public void setIncrement(double increment) {
186         // FIXME: check
187
this.increment = increment;
188         notifyListeners(new DialLayerChangeEvent(this));
189     }
190     
191     /**
192      * Returns the paint used to highlight the range.
193      *
194      * @return The paint (never <code>null</code>).
195      *
196      * @see #setPaint(Paint)
197      */

198     public Paint JavaDoc getPaint() {
199         return this.paint;
200     }
201     
202     /**
203      * Sets the paint used to highlight the range.
204      *
205      * @param paint the paint (<code>null</code> not permitted).
206      *
207      * @see #getPaint()
208      */

209     public void setPaint(Paint JavaDoc paint) {
210         if (paint == null) {
211             throw new IllegalArgumentException JavaDoc("Null 'paint' argument.");
212         }
213         this.paint = paint;
214         notifyListeners(new DialLayerChangeEvent(this));
215     }
216     
217     /**
218      * Returns the inner radius.
219      *
220      * @return The inner radius.
221      *
222      * @see #setInnerRadius(double)
223      */

224     public double getInnerRadius() {
225         return this.innerRadius;
226     }
227     
228     /**
229      * Sets the inner radius.
230      *
231      * @param radius the radius.
232      *
233      * @see #getInnerRadius()
234      */

235     public void setInnerRadius(double radius) {
236         this.innerRadius = radius;
237         notifyListeners(new DialLayerChangeEvent(this));
238     }
239     
240     /**
241      * Returns the outer radius.
242      *
243      * @return The outer radius.
244      *
245      * @see #setOuterRadius(double)
246      */

247     public double getOuterRadius() {
248         return this.outerRadius;
249     }
250     
251     /**
252      * Sets the outer radius.
253      *
254      * @param radius the radius.
255      *
256      * @see #getOuterRadius()
257      */

258     public void setOuterRadius(double radius) {
259         this.outerRadius = radius;
260         notifyListeners(new DialLayerChangeEvent(this));
261     }
262     
263     /**
264      * Returns <code>true</code> to indicate that this layer should be
265      * clipped within the dial window.
266      *
267      * @return <code>true</code>.
268      */

269     public boolean isClippedToWindow() {
270         return true;
271     }
272     
273     /**
274      * Draws the range.
275      *
276      * @param g2 the graphics target.
277      * @param plot the plot.
278      * @param frame the dial's reference frame (in Java2D space).
279      * @param view the dial's view rectangle (in Java2D space).
280      */

281     public void draw(Graphics2D JavaDoc g2, DialPlot plot, Rectangle2D JavaDoc frame,
282             Rectangle2D JavaDoc view) {
283         
284         Rectangle2D JavaDoc arcRectInner = DialPlot.rectangleByRadius(frame,
285                 this.innerRadius, this.innerRadius);
286         Rectangle2D JavaDoc arcRectOuter = DialPlot.rectangleByRadius(frame,
287                 this.outerRadius, this.outerRadius);
288         //double range = this.upperBound - this.lowerBound;
289

290         DialScale scale = plot.getScaleForDataset(0);
291         double angleMin = scale.valueToAngle(this.lowerBound);
292         double angleMax = scale.valueToAngle(this.upperBound);
293
294         Arc2D JavaDoc arcInner = new Arc2D.Double JavaDoc(arcRectInner, angleMin,
295                 angleMax - angleMin, Arc2D.OPEN);
296         Arc2D JavaDoc arcOuter = new Arc2D.Double JavaDoc(arcRectOuter, angleMax,
297                 angleMin - angleMax, Arc2D.OPEN);
298         
299         g2.setPaint(this.paint);
300         g2.setStroke(new BasicStroke JavaDoc(2.0f));
301         g2.draw(arcInner);
302         g2.draw(arcOuter);
303     }
304     
305     /**
306      * Tests this instance for equality with an arbitrary object.
307      *
308      * @param obj the object (<code>null</code> permitted).
309      *
310      * @return A boolean.
311      */

312     public boolean equals(Object JavaDoc obj) {
313         if (obj == this) {
314             return true;
315         }
316         if (!(obj instanceof StandardDialRange)) {
317             return false;
318         }
319         StandardDialRange that = (StandardDialRange) obj;
320         if (this.lowerBound != that.lowerBound) {
321             return false;
322         }
323         if (this.upperBound != that.upperBound) {
324             return false;
325         }
326         if (this.increment != that.increment) {
327             return false;
328         }
329         if (!PaintUtilities.equal(this.paint, that.paint)) {
330             return false;
331         }
332         if (this.innerRadius != that.innerRadius) {
333             return false;
334         }
335         if (this.outerRadius != that.outerRadius) {
336             return false;
337         }
338         return true;
339     }
340
341     /**
342      * Returns a hash code for this instance.
343      *
344      * @return The hash code.
345      */

346     public int hashCode() {
347         int result = 193;
348         long temp = Double.doubleToLongBits(this.increment);
349         result = (int) (temp ^ (temp >>> 32));
350         temp = Double.doubleToLongBits(this.lowerBound);
351         result = (int) (temp ^ (temp >>> 32));
352         temp = Double.doubleToLongBits(this.upperBound);
353         result = (int) (temp ^ (temp >>> 32));
354         temp = Double.doubleToLongBits(this.innerRadius);
355         result = (int) (temp ^ (temp >>> 32));
356         temp = Double.doubleToLongBits(this.outerRadius);
357         result = (int) (temp ^ (temp >>> 32));
358         result = 37 * result + HashUtilities.hashCodeForPaint(this.paint);
359         return result;
360     }
361     
362     /**
363      * Returns a clone of this instance.
364      *
365      * @return A clone.
366      *
367      * @throws CloneNotSupportedException if any of the attributes of this
368      * instance cannot be cloned.
369      */

370     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
371         return super.clone();
372     }
373     
374     /**
375      * Provides serialization support.
376      *
377      * @param stream the output stream.
378      *
379      * @throws IOException if there is an I/O error.
380      */

381     private void writeObject(ObjectOutputStream JavaDoc stream) throws IOException JavaDoc {
382         stream.defaultWriteObject();
383         SerialUtilities.writePaint(this.paint, stream);
384     }
385
386     /**
387      * Provides serialization support.
388      *
389      * @param stream the input stream.
390      *
391      * @throws IOException if there is an I/O error.
392      * @throws ClassNotFoundException if there is a classpath problem.
393      */

394     private void readObject(ObjectInputStream JavaDoc stream)
395             throws IOException JavaDoc, ClassNotFoundException JavaDoc {
396         stream.defaultReadObject();
397         this.paint = SerialUtilities.readPaint(stream);
398     }
399
400 }
401
Popular Tags