KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > annotations > TextAnnotation


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, 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 License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * -------------------
27  * TextAnnotation.java
28  * -------------------
29  * (C) Copyright 2002-2005, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: TextAnnotation.java,v 1.6 2005/05/19 15:41:53 mungady Exp $
35  *
36  * Changes:
37  * --------
38  * 28-Aug-2002 : Version 1 (DG);
39  * 07-Nov-2002 : Fixed errors reported by Checkstyle, added accessor
40  * methods (DG);
41  * 13-Jan-2003 : Reviewed Javadocs (DG);
42  * 26-Mar-2003 : Implemented Serializable (DG);
43  * 02-Jun-2003 : Added anchor and rotation settings (DG);
44  * 19-Aug-2003 : Added equals() method and implemented Cloneable (DG);
45  * 29-Sep-2004 : Updated equals() method (DG);
46  *
47  */

48
49 package org.jfree.chart.annotations;
50
51 import java.awt.Color JavaDoc;
52 import java.awt.Font JavaDoc;
53 import java.awt.Paint JavaDoc;
54 import java.io.IOException JavaDoc;
55 import java.io.ObjectInputStream JavaDoc;
56 import java.io.ObjectOutputStream JavaDoc;
57 import java.io.Serializable JavaDoc;
58
59 import org.jfree.io.SerialUtilities;
60 import org.jfree.ui.TextAnchor;
61 import org.jfree.util.ObjectUtilities;
62
63 /**
64  * A base class for text annotations. This class records the content but not
65  * the location of the annotation.
66  */

67 public class TextAnnotation implements Serializable JavaDoc {
68
69     /** For serialization. */
70     private static final long serialVersionUID = 7008912287533127432L;
71     
72     /** The default font. */
73     public static final Font JavaDoc DEFAULT_FONT
74         = new Font JavaDoc("SansSerif", Font.PLAIN, 10);
75
76     /** The default paint. */
77     public static final Paint JavaDoc DEFAULT_PAINT = Color.black;
78     
79     /** The default text anchor. */
80     public static final TextAnchor DEFAULT_TEXT_ANCHOR = TextAnchor.CENTER;
81
82     /** The default rotation anchor. */
83     public static final TextAnchor DEFAULT_ROTATION_ANCHOR = TextAnchor.CENTER;
84     
85     /** The default rotation angle. */
86     public static final double DEFAULT_ROTATION_ANGLE = 0.0;
87
88     /** The text. */
89     private String JavaDoc text;
90
91     /** The font. */
92     private Font JavaDoc font;
93
94     /** The paint. */
95     private transient Paint JavaDoc paint;
96     
97     /** The text anchor. */
98     private TextAnchor textAnchor;
99     
100     /** The rotation anchor. */
101     private TextAnchor rotationAnchor;
102     
103     /** The rotation angle. */
104     private double rotationAngle;
105
106     /**
107      * Creates a text annotation with default settings.
108      *
109      * @param text the text (<code>null</code> not permitted).
110      */

111     protected TextAnnotation(String JavaDoc text) {
112         if (text == null) {
113             throw new IllegalArgumentException JavaDoc("Null 'text' argument.");
114         }
115         this.text = text;
116         this.font = DEFAULT_FONT;
117         this.paint = DEFAULT_PAINT;
118         this.textAnchor = DEFAULT_TEXT_ANCHOR;
119         this.rotationAnchor = DEFAULT_ROTATION_ANCHOR;
120         this.rotationAngle = DEFAULT_ROTATION_ANGLE;
121     }
122
123     /**
124      * Returns the text for the annotation.
125      *
126      * @return The text (never <code>null</code>).
127      */

128     public String JavaDoc getText() {
129         return this.text;
130     }
131
132     /**
133      * Sets the text for the annotation.
134      *
135      * @param text the text (<code>null</code> not permitted).
136      */

137     public void setText(String JavaDoc text) {
138         this.text = text;
139     }
140     
141     /**
142      * Returns the font for the annotation.
143      *
144      * @return The font.
145      */

146     public Font JavaDoc getFont() {
147         return this.font;
148     }
149
150     /**
151      * Sets the font for the annotation.
152      *
153      * @param font the font.
154      */

155     public void setFont(Font JavaDoc font) {
156         this.font = font;
157     }
158     
159     /**
160      * Returns the paint for the annotation.
161      *
162      * @return The paint.
163      */

164     public Paint JavaDoc getPaint() {
165         return this.paint;
166     }
167     
168     /**
169      * Sets the paint for the annotation.
170      *
171      * @param paint the paint.
172      */

173     public void setPaint(Paint JavaDoc paint) {
174         this.paint = paint;
175     }
176
177     /**
178      * Returns the text anchor.
179      *
180      * @return The text anchor.
181      */

182     public TextAnchor getTextAnchor() {
183         return this.textAnchor;
184     }
185     
186     /**
187      * Sets the text anchor (the point on the text bounding rectangle that is
188      * aligned to the (x, y) coordinate of the annotation).
189      *
190      * @param anchor the anchor point.
191      */

192     public void setTextAnchor(TextAnchor anchor) {
193         this.textAnchor = anchor;
194     }
195     
196     /**
197      * Returns the rotation anchor.
198      *
199      * @return The rotation anchor point.
200      */

201     public TextAnchor getRotationAnchor() {
202         return this.rotationAnchor;
203     }
204     
205     /**
206      * Sets the rotation anchor point.
207      *
208      * @param anchor the anchor.
209      */

210     public void setRotationAnchor(TextAnchor anchor) {
211         this.rotationAnchor = anchor;
212     }
213     
214     /**
215      * Returns the rotation angle.
216      *
217      * @return The rotation angle.
218      */

219     public double getRotationAngle() {
220         return this.rotationAngle;
221     }
222     
223     /**
224      * Sets the rotation angle.
225      * <p>
226      * The angle is measured clockwise in radians.
227      *
228      * @param angle the angle (in radians).
229      */

230     public void setRotationAngle(double angle) {
231         this.rotationAngle = angle;
232     }
233     
234     /**
235      * Tests this object for equality with an arbitrary object.
236      *
237      * @param obj the object (<code>null</code> permitted).
238      *
239      * @return <code>true</code> or <code>false</code>.
240      */

241     public boolean equals(Object JavaDoc obj) {
242         
243         if (obj == this) {
244             return true;
245         }
246         
247         // now try to reject equality...
248
if (!(obj instanceof TextAnnotation)) {
249             return false;
250         }
251         TextAnnotation that = (TextAnnotation) obj;
252         if (!ObjectUtilities.equal(this.text, that.getText())) {
253             return false;
254         }
255         if (!ObjectUtilities.equal(this.font, that.getFont())) {
256             return false;
257         }
258         if (!ObjectUtilities.equal(this.paint, that.getPaint())) {
259             return false;
260         }
261         if (!ObjectUtilities.equal(this.textAnchor, that.getTextAnchor())) {
262             return false;
263         }
264         if (!ObjectUtilities.equal(
265             this.rotationAnchor, that.getRotationAnchor()
266         )) {
267             return false;
268         }
269         if (this.rotationAngle != that.getRotationAngle()) {
270             return false;
271         }
272         
273         // seem to be the same...
274
return true;
275             
276     }
277     
278     /**
279      * Returns a hash code for this instance.
280      *
281      * @return A hash code.
282      */

283     public int hashCode() {
284         // TODO: this needs work
285
return this.text.hashCode();
286     }
287     
288     /**
289      * Provides serialization support.
290      *
291      * @param stream the output stream.
292      *
293      * @throws IOException if there is an I/O error.
294      */

295     private void writeObject(ObjectOutputStream JavaDoc stream) throws IOException JavaDoc {
296         stream.defaultWriteObject();
297         SerialUtilities.writePaint(this.paint, stream);
298     }
299
300     /**
301      * Provides serialization support.
302      *
303      * @param stream the input stream.
304      *
305      * @throws IOException if there is an I/O error.
306      * @throws ClassNotFoundException if there is a classpath problem.
307      */

308     private void readObject(ObjectInputStream JavaDoc stream)
309         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
310         stream.defaultReadObject();
311         this.paint = SerialUtilities.readPaint(stream);
312     }
313
314 }
315
Popular Tags