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.5 2005/02/28 16:11:34 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. package org.jfree.chart.annotations;
  49. import java.awt.Color;
  50. import java.awt.Font;
  51. import java.awt.Paint;
  52. import java.io.IOException;
  53. import java.io.ObjectInputStream;
  54. import java.io.ObjectOutputStream;
  55. import java.io.Serializable;
  56. import org.jfree.io.SerialUtilities;
  57. import org.jfree.ui.TextAnchor;
  58. import org.jfree.util.ObjectUtilities;
  59. /**
  60. * A base class for text annotations. This class records the content but not
  61. * the location of the annotation.
  62. */
  63. public class TextAnnotation implements Serializable {
  64. /** The default font. */
  65. public static final Font DEFAULT_FONT
  66. = new Font("SansSerif", Font.PLAIN, 10);
  67. /** The default paint. */
  68. public static final Paint DEFAULT_PAINT = Color.black;
  69. /** The default text anchor. */
  70. public static final TextAnchor DEFAULT_TEXT_ANCHOR = TextAnchor.CENTER;
  71. /** The default rotation anchor. */
  72. public static final TextAnchor DEFAULT_ROTATION_ANCHOR = TextAnchor.CENTER;
  73. /** The default rotation angle. */
  74. public static final double DEFAULT_ROTATION_ANGLE = 0.0;
  75. /** The text. */
  76. private String text;
  77. /** The font. */
  78. private Font font;
  79. /** The paint. */
  80. private transient Paint paint;
  81. /** The text anchor. */
  82. private TextAnchor textAnchor;
  83. /** The rotation anchor. */
  84. private TextAnchor rotationAnchor;
  85. /** The rotation angle. */
  86. private double rotationAngle;
  87. /**
  88. * Creates a text annotation with default settings.
  89. *
  90. * @param text the text (<code>null</code> not permitted).
  91. */
  92. protected TextAnnotation(String text) {
  93. if (text == null) {
  94. throw new IllegalArgumentException("Null 'text' argument.");
  95. }
  96. this.text = text;
  97. this.font = DEFAULT_FONT;
  98. this.paint = DEFAULT_PAINT;
  99. this.textAnchor = DEFAULT_TEXT_ANCHOR;
  100. this.rotationAnchor = DEFAULT_ROTATION_ANCHOR;
  101. this.rotationAngle = DEFAULT_ROTATION_ANGLE;
  102. }
  103. /**
  104. * Returns the text for the annotation.
  105. *
  106. * @return The text (never <code>null</code>).
  107. */
  108. public String getText() {
  109. return this.text;
  110. }
  111. /**
  112. * Sets the text for the annotation.
  113. *
  114. * @param text the text (<code>null</code> not permitted).
  115. */
  116. public void setText(String text) {
  117. this.text = text;
  118. }
  119. /**
  120. * Returns the font for the annotation.
  121. *
  122. * @return The font.
  123. */
  124. public Font getFont() {
  125. return this.font;
  126. }
  127. /**
  128. * Sets the font for the annotation.
  129. *
  130. * @param font the font.
  131. */
  132. public void setFont(Font font) {
  133. this.font = font;
  134. }
  135. /**
  136. * Returns the paint for the annotation.
  137. *
  138. * @return The paint.
  139. */
  140. public Paint getPaint() {
  141. return this.paint;
  142. }
  143. /**
  144. * Sets the paint for the annotation.
  145. *
  146. * @param paint the paint.
  147. */
  148. public void setPaint(Paint paint) {
  149. this.paint = paint;
  150. }
  151. /**
  152. * Returns the text anchor.
  153. *
  154. * @return The text anchor.
  155. */
  156. public TextAnchor getTextAnchor() {
  157. return this.textAnchor;
  158. }
  159. /**
  160. * Sets the text anchor (the point on the text bounding rectangle that is
  161. * aligned to the (x, y) coordinate of the annotation).
  162. *
  163. * @param anchor the anchor point.
  164. */
  165. public void setTextAnchor(TextAnchor anchor) {
  166. this.textAnchor = anchor;
  167. }
  168. /**
  169. * Returns the rotation anchor.
  170. *
  171. * @return The rotation anchor point.
  172. */
  173. public TextAnchor getRotationAnchor() {
  174. return this.rotationAnchor;
  175. }
  176. /**
  177. * Sets the rotation anchor point.
  178. *
  179. * @param anchor the anchor.
  180. */
  181. public void setRotationAnchor(TextAnchor anchor) {
  182. this.rotationAnchor = anchor;
  183. }
  184. /**
  185. * Returns the rotation angle.
  186. *
  187. * @return The rotation angle.
  188. */
  189. public double getRotationAngle() {
  190. return this.rotationAngle;
  191. }
  192. /**
  193. * Sets the rotation angle.
  194. * <p>
  195. * The angle is measured clockwise in radians.
  196. *
  197. * @param angle the angle (in radians).
  198. */
  199. public void setRotationAngle(double angle) {
  200. this.rotationAngle = angle;
  201. }
  202. /**
  203. * Tests this object for equality with an arbitrary object.
  204. *
  205. * @param obj the object (<code>null</code> permitted).
  206. *
  207. * @return <code>true</code> or <code>false</code>.
  208. */
  209. public boolean equals(Object obj) {
  210. if (obj == this) {
  211. return true;
  212. }
  213. // now try to reject equality...
  214. if (!(obj instanceof TextAnnotation)) {
  215. return false;
  216. }
  217. TextAnnotation that = (TextAnnotation) obj;
  218. if (!ObjectUtilities.equal(this.text, that.getText())) {
  219. return false;
  220. }
  221. if (!ObjectUtilities.equal(this.font, that.getFont())) {
  222. return false;
  223. }
  224. if (!ObjectUtilities.equal(this.paint, that.getPaint())) {
  225. return false;
  226. }
  227. if (!ObjectUtilities.equal(this.textAnchor, that.getTextAnchor())) {
  228. return false;
  229. }
  230. if (!ObjectUtilities.equal(
  231. this.rotationAnchor, that.getRotationAnchor()
  232. )) {
  233. return false;
  234. }
  235. if (this.rotationAngle != that.getRotationAngle()) {
  236. return false;
  237. }
  238. // seem to be the same...
  239. return true;
  240. }
  241. /**
  242. * Returns a hash code for this instance.
  243. *
  244. * @return A hash code.
  245. */
  246. public int hashCode() {
  247. // TODO: this needs work
  248. return this.text.hashCode();
  249. }
  250. /**
  251. * Provides serialization support.
  252. *
  253. * @param stream the output stream.
  254. *
  255. * @throws IOException if there is an I/O error.
  256. */
  257. private void writeObject(ObjectOutputStream stream) throws IOException {
  258. stream.defaultWriteObject();
  259. SerialUtilities.writePaint(this.paint, stream);
  260. }
  261. /**
  262. * Provides serialization support.
  263. *
  264. * @param stream the input stream.
  265. *
  266. * @throws IOException if there is an I/O error.
  267. * @throws ClassNotFoundException if there is a classpath problem.
  268. */
  269. private void readObject(ObjectInputStream stream)
  270. throws IOException, ClassNotFoundException {
  271. stream.defaultReadObject();
  272. this.paint = SerialUtilities.readPaint(stream);
  273. }
  274. }