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: * CategoryTextAnnotation.java
029: * ---------------------------
030: * (C) Copyright 2003-2007, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: CategoryTextAnnotation.java,v 1.6.2.3 2007/03/06 16:12:18 mungady Exp $
036: *
037: * Changes:
038: * --------
039: * 02-Apr-2003 : Version 1 (DG);
040: * 02-Jul-2003 : Added new text alignment and rotation options (DG);
041: * 04-Jul-2003 : Added a category anchor option (DG);
042: * 19-Aug-2003 : Added equals() method and implemented Cloneable (DG);
043: * 21-Jan-2004 : Update for renamed method in ValueAxis (DG);
044: * 30-Sep-2004 : Moved drawRotatedString() from RefineryUtilities
045: * --> TextUtilities (DG);
046: * ------------- JFREECHART 1.0.x -------------------------------------------
047: * 06-Mar-2007 : Implemented hashCode() (DG);
048: *
049: */
050:
051: package org.jfree.chart.annotations;
052:
053: import java.awt.Graphics2D;
054: import java.awt.geom.Rectangle2D;
055: import java.io.Serializable;
056:
057: import org.jfree.chart.axis.CategoryAnchor;
058: import org.jfree.chart.axis.CategoryAxis;
059: import org.jfree.chart.axis.ValueAxis;
060: import org.jfree.chart.plot.CategoryPlot;
061: import org.jfree.chart.plot.Plot;
062: import org.jfree.chart.plot.PlotOrientation;
063: import org.jfree.data.category.CategoryDataset;
064: import org.jfree.text.TextUtilities;
065: import org.jfree.ui.RectangleEdge;
066:
067: /**
068: * A text annotation that can be placed on a {@link CategoryPlot}.
069: */
070: public class CategoryTextAnnotation extends TextAnnotation implements
071: CategoryAnnotation, Cloneable, Serializable {
072:
073: /** For serialization. */
074: private static final long serialVersionUID = 3333360090781320147L;
075:
076: /** The category. */
077: private Comparable category;
078:
079: /** The category anchor (START, MIDDLE, or END). */
080: private CategoryAnchor categoryAnchor;
081:
082: /** The value. */
083: private double value;
084:
085: /**
086: * Creates a new annotation to be displayed at the given location.
087: *
088: * @param text the text (<code>null</code> not permitted).
089: * @param category the category (<code>null</code> not permitted).
090: * @param value the value.
091: */
092: public CategoryTextAnnotation(String text, Comparable category,
093: double value) {
094: super (text);
095: if (category == null) {
096: throw new IllegalArgumentException(
097: "Null 'category' argument.");
098: }
099: this .category = category;
100: this .value = value;
101: this .categoryAnchor = CategoryAnchor.MIDDLE;
102: }
103:
104: /**
105: * Returns the category.
106: *
107: * @return The category (never <code>null</code>).
108: *
109: * @see #setCategory(Comparable)
110: */
111: public Comparable getCategory() {
112: return this .category;
113: }
114:
115: /**
116: * Sets the category that the annotation attaches to.
117: *
118: * @param category the category (<code>null</code> not permitted).
119: *
120: * @see #getCategory()
121: */
122: public void setCategory(Comparable category) {
123: if (category == null) {
124: throw new IllegalArgumentException(
125: "Null 'category' argument.");
126: }
127: this .category = category;
128: }
129:
130: /**
131: * Returns the category anchor point.
132: *
133: * @return The category anchor point.
134: *
135: * @see #setCategoryAnchor(CategoryAnchor)
136: */
137: public CategoryAnchor getCategoryAnchor() {
138: return this .categoryAnchor;
139: }
140:
141: /**
142: * Sets the category anchor point.
143: *
144: * @param anchor the anchor point (<code>null</code> not permitted).
145: *
146: * @see #getCategoryAnchor()
147: */
148: public void setCategoryAnchor(CategoryAnchor anchor) {
149: if (anchor == null) {
150: throw new IllegalArgumentException(
151: "Null 'anchor' argument.");
152: }
153: this .categoryAnchor = anchor;
154: }
155:
156: /**
157: * Returns the value that the annotation attaches to.
158: *
159: * @return The value.
160: *
161: * @see #setValue(double)
162: */
163: public double getValue() {
164: return this .value;
165: }
166:
167: /**
168: * Sets the value.
169: *
170: * @param value the value.
171: *
172: * @see #getValue()
173: */
174: public void setValue(double value) {
175: this .value = value;
176: }
177:
178: /**
179: * Draws the annotation.
180: *
181: * @param g2 the graphics device.
182: * @param plot the plot.
183: * @param dataArea the data area.
184: * @param domainAxis the domain axis.
185: * @param rangeAxis the range axis.
186: */
187: public void draw(Graphics2D g2, CategoryPlot plot,
188: Rectangle2D dataArea, CategoryAxis domainAxis,
189: ValueAxis rangeAxis) {
190:
191: CategoryDataset dataset = plot.getDataset();
192: int catIndex = dataset.getColumnIndex(this .category);
193: int catCount = dataset.getColumnCount();
194:
195: float anchorX = 0.0f;
196: float anchorY = 0.0f;
197: PlotOrientation orientation = plot.getOrientation();
198: RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(plot
199: .getDomainAxisLocation(), orientation);
200: RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(plot
201: .getRangeAxisLocation(), orientation);
202:
203: if (orientation == PlotOrientation.HORIZONTAL) {
204: anchorY = (float) domainAxis.getCategoryJava2DCoordinate(
205: this .categoryAnchor, catIndex, catCount, dataArea,
206: domainEdge);
207: anchorX = (float) rangeAxis.valueToJava2D(this .value,
208: dataArea, rangeEdge);
209: } else if (orientation == PlotOrientation.VERTICAL) {
210: anchorX = (float) domainAxis.getCategoryJava2DCoordinate(
211: this .categoryAnchor, catIndex, catCount, dataArea,
212: domainEdge);
213: anchorY = (float) rangeAxis.valueToJava2D(this .value,
214: dataArea, rangeEdge);
215: }
216: g2.setFont(getFont());
217: g2.setPaint(getPaint());
218: TextUtilities.drawRotatedString(getText(), g2, anchorX,
219: anchorY, getTextAnchor(), getRotationAngle(),
220: getRotationAnchor());
221:
222: }
223:
224: /**
225: * Tests this object for equality with another.
226: *
227: * @param obj the object (<code>null</code> permitted).
228: *
229: * @return <code>true</code> or <code>false</code>.
230: */
231: public boolean equals(Object obj) {
232: if (obj == this ) {
233: return true;
234: }
235: if (!(obj instanceof CategoryTextAnnotation)) {
236: return false;
237: }
238: CategoryTextAnnotation that = (CategoryTextAnnotation) obj;
239: if (!super .equals(obj)) {
240: return false;
241: }
242: if (!this .category.equals(that.getCategory())) {
243: return false;
244: }
245: if (!this .categoryAnchor.equals(that.getCategoryAnchor())) {
246: return false;
247: }
248: if (this .value != that.getValue()) {
249: return false;
250: }
251: return true;
252: }
253:
254: /**
255: * Returns a hash code for this instance.
256: *
257: * @return A hash code.
258: */
259: public int hashCode() {
260: int result = super .hashCode();
261: result = 37 * result + this .category.hashCode();
262: result = 37 * result + this .categoryAnchor.hashCode();
263: long temp = Double.doubleToLongBits(this .value);
264: result = 37 * result + (int) (temp ^ (temp >>> 32));
265: return result;
266: }
267:
268: /**
269: * Returns a clone of the annotation.
270: *
271: * @return A clone.
272: *
273: * @throws CloneNotSupportedException this class will not throw this
274: * exception, but subclasses (if any) might.
275: */
276: public Object clone() throws CloneNotSupportedException {
277: return super.clone();
278: }
279:
280: }
|