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: * XYTitleAnnotation.java
029: * ----------------------
030: * (C) Copyright 2007, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: XYTitleAnnotation.java,v 1.1.2.2 2007/04/30 20:54:34 mungady Exp $
036: *
037: * Changes:
038: * --------
039: * 02-Feb-2007 : Version 1 (DG);
040: * 30-Apr-2007 : Fixed equals() method (DG);
041: *
042: */
043:
044: package org.jfree.experimental.chart.annotations;
045:
046: import java.awt.Graphics2D;
047: import java.awt.geom.Point2D;
048: import java.awt.geom.Rectangle2D;
049: import java.io.Serializable;
050:
051: import org.jfree.chart.annotations.AbstractXYAnnotation;
052: import org.jfree.chart.axis.AxisLocation;
053: import org.jfree.chart.axis.ValueAxis;
054: import org.jfree.chart.block.BlockParams;
055: import org.jfree.chart.block.EntityBlockResult;
056: import org.jfree.chart.block.RectangleConstraint;
057: import org.jfree.chart.plot.Plot;
058: import org.jfree.chart.plot.PlotOrientation;
059: import org.jfree.chart.plot.PlotRenderingInfo;
060: import org.jfree.chart.plot.XYPlot;
061: import org.jfree.chart.title.Title;
062: import org.jfree.data.Range;
063: import org.jfree.experimental.chart.util.XYCoordinateType;
064: import org.jfree.ui.RectangleAnchor;
065: import org.jfree.ui.RectangleEdge;
066: import org.jfree.ui.Size2D;
067: import org.jfree.util.ObjectUtilities;
068: import org.jfree.util.PublicCloneable;
069:
070: /**
071: * An annotation that allows any {@link Title} to be placed at a location on
072: * an {@link XYPlot}.
073: */
074: public class XYTitleAnnotation extends AbstractXYAnnotation implements
075: Cloneable, PublicCloneable, Serializable {
076:
077: /** For serialization. */
078: private static final long serialVersionUID = -4364694501921559958L;
079:
080: /** The coordinate type. */
081: private XYCoordinateType coordinateType;
082:
083: /** The x-coordinate (in data space). */
084: private double x;
085:
086: /** The y-coordinate (in data space). */
087: private double y;
088:
089: private double maxWidth;
090:
091: private double maxHeight;
092:
093: /** The title. */
094: private Title title;
095:
096: /**
097: * The title anchor point.
098: */
099: private RectangleAnchor anchor;
100:
101: /**
102: * Creates a new annotation to be displayed at the specified (x, y)
103: * location.
104: *
105: * @param x the x-coordinate (in data space).
106: * @param y the y-coordinate (in data space).
107: * @param title the title (<code>null</code> not permitted).
108: */
109: public XYTitleAnnotation(double x, double y, Title title) {
110: this (x, y, title, RectangleAnchor.CENTER);
111: }
112:
113: /**
114: * Creates a new annotation to be displayed at the specified (x, y)
115: * location.
116: *
117: * @param x the x-coordinate (in data space).
118: * @param y the y-coordinate (in data space).
119: * @param title the title (<code>null</code> not permitted).
120: * @param anchor the title anchor (<code>null</code> not permitted).
121: */
122: public XYTitleAnnotation(double x, double y, Title title,
123: RectangleAnchor anchor) {
124: if (title == null) {
125: throw new IllegalArgumentException("Null 'title' argument.");
126: }
127: if (anchor == null) {
128: throw new IllegalArgumentException(
129: "Null 'anchor' argument.");
130: }
131: this .coordinateType = XYCoordinateType.RELATIVE;
132: this .x = x;
133: this .y = y;
134: this .maxWidth = 0.0;
135: this .maxHeight = 0.0;
136: this .title = title;
137: this .anchor = anchor;
138: }
139:
140: /**
141: * Returns the coordinate type (set in the constructor).
142: *
143: * @return The coordinate type (never <code>null</code>).
144: */
145: public XYCoordinateType getCoordinateType() {
146: return this .coordinateType;
147: }
148:
149: /**
150: * Returns the x-coordinate for the annotation.
151: *
152: * @return The x-coordinate.
153: */
154: public double getX() {
155: return this .x;
156: }
157:
158: /**
159: * Returns the y-coordinate for the annotation.
160: *
161: * @return The y-coordinate.
162: */
163: public double getY() {
164: return this .y;
165: }
166:
167: /**
168: * Returns the title for the annotation.
169: *
170: * @return The title.
171: */
172: public Title getTitle() {
173: return this .title;
174: }
175:
176: /**
177: * Returns the title anchor for the annotation.
178: *
179: * @return The title anchor.
180: */
181: public RectangleAnchor getTitleAnchor() {
182: return this .anchor;
183: }
184:
185: /**
186: * Returns the maximum width.
187: *
188: * @return The maximum width.
189: */
190: public double getMaxWidth() {
191: return this .maxWidth;
192: }
193:
194: /**
195: * Sets the maximum width.
196: *
197: * @param max the maximum width (0.0 or less means no maximum).
198: */
199: public void setMaxWidth(double max) {
200: this .maxWidth = max;
201: }
202:
203: /**
204: * Returns the maximum height.
205: *
206: * @return The maximum height.
207: */
208: public double getMaxHeight() {
209: return this .maxHeight;
210: }
211:
212: /**
213: * Sets the maximum height.
214: *
215: * @param max the maximum height.
216: */
217: public void setMaxHeight(double max) {
218: this .maxHeight = max;
219: }
220:
221: /**
222: * Draws the annotation. This method is called by the drawing code in the
223: * {@link XYPlot} class, you don't normally need to call this method
224: * directly.
225: *
226: * @param g2 the graphics device.
227: * @param plot the plot.
228: * @param dataArea the data area.
229: * @param domainAxis the domain axis.
230: * @param rangeAxis the range axis.
231: * @param rendererIndex the renderer index.
232: * @param info if supplied, this info object will be populated with
233: * entity information.
234: */
235: public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea,
236: ValueAxis domainAxis, ValueAxis rangeAxis,
237: int rendererIndex, PlotRenderingInfo info) {
238:
239: PlotOrientation orientation = plot.getOrientation();
240: AxisLocation domainAxisLocation = plot.getDomainAxisLocation();
241: AxisLocation rangeAxisLocation = plot.getRangeAxisLocation();
242: RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(
243: domainAxisLocation, orientation);
244: RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(
245: rangeAxisLocation, orientation);
246: Range xRange = domainAxis.getRange();
247: Range yRange = rangeAxis.getRange();
248: double anchorX = 0.0;
249: double anchorY = 0.0;
250: if (this .coordinateType == XYCoordinateType.RELATIVE) {
251: anchorX = xRange.getLowerBound()
252: + (this .x * xRange.getLength());
253: anchorY = yRange.getLowerBound()
254: + (this .y * yRange.getLength());
255: } else {
256: anchorX = domainAxis.valueToJava2D(this .x, dataArea,
257: domainEdge);
258: anchorY = rangeAxis.valueToJava2D(this .y, dataArea,
259: rangeEdge);
260: }
261:
262: float j2DX = (float) domainAxis.valueToJava2D(anchorX,
263: dataArea, domainEdge);
264: float j2DY = (float) rangeAxis.valueToJava2D(anchorY, dataArea,
265: rangeEdge);
266: float xx = 0.0f;
267: float yy = 0.0f;
268: if (orientation == PlotOrientation.HORIZONTAL) {
269: xx = j2DY;
270: yy = j2DX;
271: } else if (orientation == PlotOrientation.VERTICAL) {
272: xx = j2DX;
273: yy = j2DY;
274: }
275:
276: double maxW = dataArea.getWidth();
277: double maxH = dataArea.getHeight();
278: if (this .coordinateType == XYCoordinateType.RELATIVE) {
279: if (this .maxWidth > 0.0) {
280: maxW = maxW * this .maxWidth;
281: }
282: if (this .maxHeight > 0.0) {
283: maxH = maxH * this .maxHeight;
284: }
285: }
286: if (this .coordinateType == XYCoordinateType.DATA) {
287: maxW = this .maxWidth;
288: maxH = this .maxHeight;
289: }
290: RectangleConstraint rc = new RectangleConstraint(new Range(0,
291: maxW), new Range(0, maxH));
292:
293: Size2D size = this .title.arrange(g2, rc);
294: Rectangle2D titleRect = new Rectangle2D.Double(0, 0,
295: size.width, size.height);
296: Point2D anchorPoint = (Point2D) RectangleAnchor.coordinates(
297: titleRect, this .anchor);
298: xx = xx - (float) anchorPoint.getX();
299: yy = yy - (float) anchorPoint.getY();
300: titleRect.setRect(xx, yy, titleRect.getWidth(), titleRect
301: .getHeight());
302: BlockParams p = new BlockParams();
303: if (info != null) {
304: if (info.getOwner().getEntityCollection() != null) {
305: p.setGenerateEntities(true);
306: }
307: }
308: Object result = this .title.draw(g2, titleRect, p);
309: if (result instanceof EntityBlockResult) {
310: EntityBlockResult ebr = (EntityBlockResult) result;
311: info.getOwner().getEntityCollection().addAll(
312: ebr.getEntityCollection());
313: }
314: String toolTip = getToolTipText();
315: String url = getURL();
316: if (toolTip != null || url != null) {
317: addEntity(info, new Rectangle2D.Float(xx, yy,
318: (float) size.width, (float) size.height),
319: rendererIndex, toolTip, url);
320: }
321: }
322:
323: /**
324: * Tests this object for equality with an arbitrary object.
325: *
326: * @param obj the object (<code>null</code> permitted).
327: *
328: * @return A boolean.
329: */
330: public boolean equals(Object obj) {
331: if (obj == this ) {
332: return true;
333: }
334: if (!(obj instanceof XYTitleAnnotation)) {
335: return false;
336: }
337: XYTitleAnnotation that = (XYTitleAnnotation) obj;
338: if (this .coordinateType != that.coordinateType) {
339: return false;
340: }
341: if (this .x != that.x) {
342: return false;
343: }
344: if (this .y != that.y) {
345: return false;
346: }
347: if (this .maxWidth != that.maxWidth) {
348: return false;
349: }
350: if (this .maxHeight != that.maxHeight) {
351: return false;
352: }
353: if (!ObjectUtilities.equal(this .title, that.title)) {
354: return false;
355: }
356: if (!this .anchor.equals(that.anchor)) {
357: return false;
358: }
359: return super .equals(obj);
360: }
361:
362: /**
363: * Returns a hash code for this object.
364: *
365: * @return A hash code.
366: */
367: public int hashCode() {
368: // FIXME: do better than this
369: return this .title.hashCode();
370: }
371:
372: /**
373: * Returns a clone of the annotation.
374: *
375: * @return A clone.
376: *
377: * @throws CloneNotSupportedException if the annotation can't be cloned.
378: */
379: public Object clone() throws CloneNotSupportedException {
380: return super.clone();
381: }
382:
383: }
|