001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * -------------------
028: * TextAnnotation.java
029: * -------------------
030: * (C) Copyright 2002-2007, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: TextAnnotation.java,v 1.6.2.3 2007/01/16 09:43:46 mungady Exp $
036: *
037: * Changes:
038: * --------
039: * 28-Aug-2002 : Version 1 (DG);
040: * 07-Nov-2002 : Fixed errors reported by Checkstyle, added accessor
041: * methods (DG);
042: * 13-Jan-2003 : Reviewed Javadocs (DG);
043: * 26-Mar-2003 : Implemented Serializable (DG);
044: * 02-Jun-2003 : Added anchor and rotation settings (DG);
045: * 19-Aug-2003 : Added equals() method and implemented Cloneable (DG);
046: * 29-Sep-2004 : Updated equals() method (DG);
047: * 06-Jun-2005 : Fixed equals() method to work with GradientPaint (DG);
048: * ------------- JFREECHART 1.0.x ---------------------------------------------
049: * 16-Jan-2007 : Added argument checks, fixed hashCode() method and updated
050: * API docs (DG);
051: *
052: */
053:
054: package org.jfree.chart.annotations;
055:
056: import java.awt.Color;
057: import java.awt.Font;
058: import java.awt.Paint;
059: import java.io.IOException;
060: import java.io.ObjectInputStream;
061: import java.io.ObjectOutputStream;
062: import java.io.Serializable;
063:
064: import org.jfree.chart.HashUtilities;
065: import org.jfree.io.SerialUtilities;
066: import org.jfree.ui.TextAnchor;
067: import org.jfree.util.ObjectUtilities;
068: import org.jfree.util.PaintUtilities;
069:
070: /**
071: * A base class for text annotations. This class records the content but not
072: * the location of the annotation.
073: */
074: public class TextAnnotation implements Serializable {
075:
076: /** For serialization. */
077: private static final long serialVersionUID = 7008912287533127432L;
078:
079: /** The default font. */
080: public static final Font DEFAULT_FONT = new Font("SansSerif",
081: Font.PLAIN, 10);
082:
083: /** The default paint. */
084: public static final Paint DEFAULT_PAINT = Color.black;
085:
086: /** The default text anchor. */
087: public static final TextAnchor DEFAULT_TEXT_ANCHOR = TextAnchor.CENTER;
088:
089: /** The default rotation anchor. */
090: public static final TextAnchor DEFAULT_ROTATION_ANCHOR = TextAnchor.CENTER;
091:
092: /** The default rotation angle. */
093: public static final double DEFAULT_ROTATION_ANGLE = 0.0;
094:
095: /** The text. */
096: private String text;
097:
098: /** The font. */
099: private Font font;
100:
101: /** The paint. */
102: private transient Paint paint;
103:
104: /** The text anchor. */
105: private TextAnchor textAnchor;
106:
107: /** The rotation anchor. */
108: private TextAnchor rotationAnchor;
109:
110: /** The rotation angle. */
111: private double rotationAngle;
112:
113: /**
114: * Creates a text annotation with default settings.
115: *
116: * @param text the text (<code>null</code> not permitted).
117: */
118: protected TextAnnotation(String text) {
119: if (text == null) {
120: throw new IllegalArgumentException("Null 'text' argument.");
121: }
122: this .text = text;
123: this .font = DEFAULT_FONT;
124: this .paint = DEFAULT_PAINT;
125: this .textAnchor = DEFAULT_TEXT_ANCHOR;
126: this .rotationAnchor = DEFAULT_ROTATION_ANCHOR;
127: this .rotationAngle = DEFAULT_ROTATION_ANGLE;
128: }
129:
130: /**
131: * Returns the text for the annotation.
132: *
133: * @return The text (never <code>null</code>).
134: *
135: * @see #setText(String)
136: */
137: public String getText() {
138: return this .text;
139: }
140:
141: /**
142: * Sets the text for the annotation.
143: *
144: * @param text the text (<code>null</code> not permitted).
145: *
146: * @see #getText()
147: */
148: public void setText(String text) {
149: if (text == null) {
150: throw new IllegalArgumentException("Null 'text' argument.");
151: }
152: this .text = text;
153: }
154:
155: /**
156: * Returns the font for the annotation.
157: *
158: * @return The font (never <code>null</code>).
159: *
160: * @see #setFont(Font)
161: */
162: public Font getFont() {
163: return this .font;
164: }
165:
166: /**
167: * Sets the font for the annotation.
168: *
169: * @param font the font (<code>null</code> not permitted).
170: *
171: * @see #getFont()
172: */
173: public void setFont(Font font) {
174: if (font == null) {
175: throw new IllegalArgumentException("Null 'font' argument.");
176: }
177: this .font = font;
178: }
179:
180: /**
181: * Returns the paint for the annotation.
182: *
183: * @return The paint (never <code>null</code>).
184: *
185: * @see #setPaint(Paint)
186: */
187: public Paint getPaint() {
188: return this .paint;
189: }
190:
191: /**
192: * Sets the paint for the annotation.
193: *
194: * @param paint the paint (<code>null</code> not permitted).
195: *
196: * @see #getPaint()
197: */
198: public void setPaint(Paint paint) {
199: if (paint == null) {
200: throw new IllegalArgumentException("Null 'paint' argument.");
201: }
202: this .paint = paint;
203: }
204:
205: /**
206: * Returns the text anchor.
207: *
208: * @return The text anchor.
209: *
210: * @see #setTextAnchor(TextAnchor)
211: */
212: public TextAnchor getTextAnchor() {
213: return this .textAnchor;
214: }
215:
216: /**
217: * Sets the text anchor (the point on the text bounding rectangle that is
218: * aligned to the (x, y) coordinate of the annotation).
219: *
220: * @param anchor the anchor point (<code>null</code> not permitted).
221: *
222: * @see #getTextAnchor()
223: */
224: public void setTextAnchor(TextAnchor anchor) {
225: if (anchor == null) {
226: throw new IllegalArgumentException(
227: "Null 'anchor' argument.");
228: }
229: this .textAnchor = anchor;
230: }
231:
232: /**
233: * Returns the rotation anchor.
234: *
235: * @return The rotation anchor point (never <code>null</code>).
236: *
237: * @see #setRotationAnchor(TextAnchor)
238: */
239: public TextAnchor getRotationAnchor() {
240: return this .rotationAnchor;
241: }
242:
243: /**
244: * Sets the rotation anchor point.
245: *
246: * @param anchor the anchor (<code>null</code> not permitted).
247: *
248: * @see #getRotationAnchor()
249: */
250: public void setRotationAnchor(TextAnchor anchor) {
251: this .rotationAnchor = anchor;
252: }
253:
254: /**
255: * Returns the rotation angle in radians.
256: *
257: * @return The rotation angle.
258: *
259: * @see #setRotationAngle(double)
260: */
261: public double getRotationAngle() {
262: return this .rotationAngle;
263: }
264:
265: /**
266: * Sets the rotation angle. The angle is measured clockwise in radians.
267: *
268: * @param angle the angle (in radians).
269: *
270: * @see #getRotationAngle()
271: */
272: public void setRotationAngle(double angle) {
273: this .rotationAngle = angle;
274: }
275:
276: /**
277: * Tests this object for equality with an arbitrary object.
278: *
279: * @param obj the object (<code>null</code> permitted).
280: *
281: * @return <code>true</code> or <code>false</code>.
282: */
283: public boolean equals(Object obj) {
284: if (obj == this ) {
285: return true;
286: }
287: // now try to reject equality...
288: if (!(obj instanceof TextAnnotation)) {
289: return false;
290: }
291: TextAnnotation that = (TextAnnotation) obj;
292: if (!ObjectUtilities.equal(this .text, that.getText())) {
293: return false;
294: }
295: if (!ObjectUtilities.equal(this .font, that.getFont())) {
296: return false;
297: }
298: if (!PaintUtilities.equal(this .paint, that.getPaint())) {
299: return false;
300: }
301: if (!ObjectUtilities.equal(this .textAnchor, that
302: .getTextAnchor())) {
303: return false;
304: }
305: if (!ObjectUtilities.equal(this .rotationAnchor, that
306: .getRotationAnchor())) {
307: return false;
308: }
309: if (this .rotationAngle != that.getRotationAngle()) {
310: return false;
311: }
312:
313: // seem to be the same...
314: return true;
315:
316: }
317:
318: /**
319: * Returns a hash code for this instance.
320: *
321: * @return A hash code.
322: */
323: public int hashCode() {
324: int result = 193;
325: result = 37 * result + this .font.hashCode();
326: result = 37 * result
327: + HashUtilities.hashCodeForPaint(this .paint);
328: result = 37 * result + this .rotationAnchor.hashCode();
329: long temp = Double.doubleToLongBits(this .rotationAngle);
330: result = 37 * result + (int) (temp ^ (temp >>> 32));
331: result = 37 * result + this .text.hashCode();
332: result = 37 * result + this .textAnchor.hashCode();
333: return result;
334: }
335:
336: /**
337: * Provides serialization support.
338: *
339: * @param stream the output stream.
340: *
341: * @throws IOException if there is an I/O error.
342: */
343: private void writeObject(ObjectOutputStream stream)
344: throws IOException {
345: stream.defaultWriteObject();
346: SerialUtilities.writePaint(this .paint, stream);
347: }
348:
349: /**
350: * Provides serialization support.
351: *
352: * @param stream the input stream.
353: *
354: * @throws IOException if there is an I/O error.
355: * @throws ClassNotFoundException if there is a classpath problem.
356: */
357: private void readObject(ObjectInputStream stream)
358: throws IOException, ClassNotFoundException {
359: stream.defaultReadObject();
360: this.paint = SerialUtilities.readPaint(stream);
361: }
362:
363: }
|