KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DialTextAnnotation.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: DialTextAnnotation.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.Color JavaDoc;
46 import java.awt.Font 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.Point2D JavaDoc;
51 import java.awt.geom.Rectangle2D JavaDoc;
52 import java.io.IOException JavaDoc;
53 import java.io.ObjectInputStream JavaDoc;
54 import java.io.ObjectOutputStream JavaDoc;
55 import java.io.Serializable JavaDoc;
56
57 import org.jfree.chart.HashUtilities;
58 import org.jfree.io.SerialUtilities;
59 import org.jfree.text.TextUtilities;
60 import org.jfree.ui.TextAnchor;
61 import org.jfree.util.PaintUtilities;
62 import org.jfree.util.PublicCloneable;
63
64 /**
65  * A text annotation for a {@link DialPlot}.
66  */

67 public class DialTextAnnotation extends AbstractDialLayer implements DialLayer,
68         Cloneable JavaDoc, PublicCloneable, Serializable JavaDoc {
69     
70     /** The angle that defines the anchor point for the annotation. */
71     private double angle;
72     
73     /** The radius that defines the anchor point for the annotation. */
74     private double radius;
75     
76     /** The font. */
77     private Font JavaDoc font;
78     
79     /**
80      * The paint for the label. This field is transient because it requires
81      * special handling for serialization.
82      */

83     private transient Paint JavaDoc paint;
84     
85     /** The label text. */
86     private String JavaDoc label;
87     
88     /** The text anchor to be aligned to the annotation's anchor point. */
89     private TextAnchor anchor;
90     
91     /**
92      * Creates a new instance of <code>DialTextAnnotation</code>.
93      *
94      * @param label the label (<code>null</code> not permitted).
95      */

96     public DialTextAnnotation(String JavaDoc label) {
97         if (label == null) {
98             throw new IllegalArgumentException JavaDoc("Null 'label' argument.");
99         }
100         this.angle = -90.0;
101         this.radius = 0.3;
102         this.font = new Font JavaDoc("Dialog", Font.BOLD, 14);
103         this.paint = Color.black;
104         this.label = label;
105         this.anchor = TextAnchor.TOP_CENTER;
106     }
107     
108     /**
109      * Returns the angle used to calculate the anchor point.
110      *
111      * @return The angle used to calculate the anchor point.
112      *
113      * @see #setAngle(double)
114      * @see #getRadius()
115      */

116     public double getAngle() {
117         return this.angle;
118     }
119     
120     /**
121      * Sets the angle used to calculate the anchor point.
122      *
123      * @param angle the angle.
124      *
125      * @see #getAngle()
126      * @see #setRadius(double)
127      */

128     public void setAngle(double angle) {
129         this.angle = angle;
130         notifyListeners(new DialLayerChangeEvent(this));
131     }
132     
133     /**
134      * Returns the radius used to calculate the anchor point.
135      *
136      * @return The radius.
137      *
138      * @see #setRadius(double)
139      * @see #getAngle()
140      */

141     public double getRadius() {
142         return this.radius;
143     }
144     
145     /**
146      * Sets the radius used to calculate the anchor point.
147      *
148      * @param radius the radius.
149      *
150      * @see #getRadius()
151      * @see #setAngle(double)
152      */

153     public void setRadius(double radius) {
154         // TODO: validation
155
this.radius = radius;
156         notifyListeners(new DialLayerChangeEvent(this));
157     }
158     
159     /**
160      * Returns the font used to display the label.
161      *
162      * @return The font (never <code>null</code>).
163      *
164      * @see #setFont(Font)
165      */

166     public Font JavaDoc getFont() {
167         return this.font;
168     }
169     
170     /**
171      * Sets the font used to display the label.
172      *
173      * @param font the font (<code>null</code> not permitted).
174      *
175      * @see #getFont()
176      */

177     public void setFont(Font JavaDoc font) {
178         if (font == null) {
179             throw new IllegalArgumentException JavaDoc("Null 'font' argument.");
180         }
181         this.font = font;
182         notifyListeners(new DialLayerChangeEvent(this));
183     }
184     
185     /**
186      * Returns the paint used to display the label.
187      *
188      * @return The paint (never <code>null</code>).
189      *
190      * @see #setPaint(Paint)
191      */

192     public Paint JavaDoc getPaint() {
193         return this.paint;
194     }
195     
196     /**
197      * Sets the paint used to display the label.
198      *
199      * @param paint the paint (<code>null</code> not permitted).
200      *
201      * @see #getPaint()
202      */

203     public void setPaint(Paint JavaDoc paint) {
204         if (paint == null) {
205             throw new IllegalArgumentException JavaDoc("Null 'paint' argument.");
206         }
207         this.paint = paint;
208         notifyListeners(new DialLayerChangeEvent(this));
209     }
210     
211     /**
212      * Returns the label text.
213      *
214      * @return The label text (never <code>null</code).
215      *
216      * @see #setLabel(String)
217      */

218     public String JavaDoc getLabel() {
219         return this.label;
220     }
221     
222     /**
223      * Sets the label.
224      *
225      * @param label the label (<code>null</code> not permitted).
226      *
227      * @see #getLabel()
228      */

229     public void setLabel(String JavaDoc label) {
230         if (label == null) {
231             throw new IllegalArgumentException JavaDoc("Null 'label' argument.");
232         }
233         this.label = label;
234         notifyListeners(new DialLayerChangeEvent(this));
235     }
236     
237     /**
238      * Returns <code>true</code> to indicate that this layer should be
239      * clipped within the dial window.
240      *
241      * @return <code>true</code>.
242      */

243     public boolean isClippedToWindow() {
244         return true;
245     }
246     
247     /**
248      * Draws the background to the specified graphics device. If the dial
249      * frame specifies a window, the clipping region will already have been
250      * set to this window before this method is called.
251      *
252      * @param g2 the graphics device (<code>null</code> not permitted).
253      * @param plot the plot (ignored here).
254      * @param frame the dial frame (ignored here).
255      * @param view the view rectangle (<code>null</code> not permitted).
256      */

257     public void draw(Graphics2D JavaDoc g2, DialPlot plot, Rectangle2D JavaDoc frame,
258             Rectangle2D JavaDoc view) {
259
260         // work out the anchor point
261
Rectangle2D JavaDoc f = DialPlot.rectangleByRadius(frame, this.radius,
262                 this.radius);
263         Arc2D JavaDoc arc = new Arc2D.Double JavaDoc(f, this.angle, 0.0, Arc2D.OPEN);
264         Point2D JavaDoc pt = arc.getStartPoint();
265         g2.setPaint(this.paint);
266         g2.setFont(this.font);
267         TextUtilities.drawAlignedString(this.label, g2, (float) pt.getX(),
268                 (float) pt.getY(), this.anchor);
269         
270     }
271     
272     /**
273      * Tests this instance for equality with an arbitrary object.
274      *
275      * @param obj the object (<code>null</code> permitted).
276      *
277      * @return A boolean.
278      */

279     public boolean equals(Object JavaDoc obj) {
280         if (obj == this) {
281             return true;
282         }
283         if (!(obj instanceof DialTextAnnotation)) {
284             return false;
285         }
286         DialTextAnnotation that = (DialTextAnnotation) obj;
287         if (!this.label.equals(that.label)) {
288             return false;
289         }
290         if (!this.font.equals(that.font)) {
291             return false;
292         }
293         if (!PaintUtilities.equal(this.paint, that.paint)) {
294             return false;
295         }
296         if (this.radius != that.radius) {
297             return false;
298         }
299         if (this.angle != that.angle) {
300             return false;
301         }
302         if (!this.anchor.equals(that.anchor)) {
303             return false;
304         }
305         return true;
306     }
307     
308     /**
309      * Returns a hash code for this instance.
310      *
311      * @return The hash code.
312      */

313     public int hashCode() {
314         int result = 193;
315         result = 37 * result + HashUtilities.hashCodeForPaint(this.paint);
316         result = 37 * result + this.font.hashCode();
317         result = 37 * result + this.label.hashCode();
318         result = 37 * result + this.anchor.hashCode();
319         long temp = Double.doubleToLongBits(this.angle);
320         result = (int) (temp ^ (temp >>> 32));
321         temp = Double.doubleToLongBits(this.radius);
322         result = (int) (temp ^ (temp >>> 32));
323         return result;
324     }
325     
326     /**
327      * Returns a clone of this instance.
328      *
329      * @return The clone.
330      *
331      * @throws CloneNotSupportedException if some attribute of this instance
332      * cannot be cloned.
333      */

334     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
335         return super.clone();
336     }
337     
338     /**
339      * Provides serialization support.
340      *
341      * @param stream the output stream.
342      *
343      * @throws IOException if there is an I/O error.
344      */

345     private void writeObject(ObjectOutputStream JavaDoc stream) throws IOException JavaDoc {
346         stream.defaultWriteObject();
347         SerialUtilities.writePaint(this.paint, stream);
348     }
349
350     /**
351      * Provides serialization support.
352      *
353      * @param stream the input stream.
354      *
355      * @throws IOException if there is an I/O error.
356      * @throws ClassNotFoundException if there is a classpath problem.
357      */

358     private void readObject(ObjectInputStream JavaDoc stream)
359             throws IOException JavaDoc, ClassNotFoundException JavaDoc {
360         stream.defaultReadObject();
361         this.paint = SerialUtilities.readPaint(stream);
362     }
363     
364 }
365
Popular Tags