KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > entity > ChartEntity


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  * ChartEntity.java
28  * ----------------
29  * (C) Copyright 2002-2005, by Object Refinery Limited and Contributors.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): Richard Atkinson;
33  * Xavier Poinsard;
34  * Robert Fuller;
35  *
36  * $Id: ChartEntity.java,v 1.8 2005/05/19 15:42:54 mungady Exp $
37  *
38  * Changes:
39  * --------
40  * 23-May-2002 : Version 1 (DG);
41  * 12-Jun-2002 : Added Javadoc comments (DG);
42  * 26-Jun-2002 : Added methods for image maps (DG);
43  * 05-Aug-2002 : Added constructor and accessors for URL support in image maps
44  * Added getImageMapAreaTag() - previously in subclasses (RA);
45  * 05-Sep-2002 : Added getImageMapAreaTag(boolean) to support OverLIB for
46  * tooltips http://www.bosrup.com/web/overlib (RA);
47  * 03-Oct-2002 : Fixed errors reported by Checkstyle (DG);
48  * 08-Oct-2002 : Changed getImageMapAreaTag to use title instead of alt
49  * attribute so HTML image maps now work in Mozilla and Opera as
50  * well as Internet Explorer (RA);
51  * 13-Mar-2003 : Change getImageMapAreaTag to only return a tag when there is a
52  * tooltip or URL, as suggested by Xavier Poinsard (see Feature
53  * Request 688079) (DG);
54  * 12-Aug-2003 : Added support for custom image maps using
55  * ToolTipTagFragmentGenerator and URLTagFragmentGenerator (RA);
56  * 02-Sep-2003 : Incorporated fix (791901) submitted by Robert Fuller (DG);
57  * 19-May-2004 : Added equals() method and implemented Cloneable and
58  * Serializable (DG);
59  * 29-Sep-2004 : Implemented PublicCloneable (DG);
60  * 13-Jan-2005 : Fixed for compliance with XHTML 1.0 (DG);
61  * 18-Apr-2005 : Use StringBuffer (DG);
62  * 20-Apr-2005 : Added toString() implementation (DG);
63  *
64  */

65
66 package org.jfree.chart.entity;
67
68 import java.awt.Shape JavaDoc;
69 import java.awt.geom.PathIterator JavaDoc;
70 import java.awt.geom.Rectangle2D JavaDoc;
71 import java.io.IOException JavaDoc;
72 import java.io.ObjectInputStream JavaDoc;
73 import java.io.ObjectOutputStream JavaDoc;
74 import java.io.Serializable JavaDoc;
75
76 import org.jfree.chart.imagemap.ToolTipTagFragmentGenerator;
77 import org.jfree.chart.imagemap.URLTagFragmentGenerator;
78 import org.jfree.io.SerialUtilities;
79 import org.jfree.util.ObjectUtilities;
80 import org.jfree.util.PublicCloneable;
81
82 /**
83  * A class that captures information about some component of a chart (a bar,
84  * line etc).
85  */

86 public class ChartEntity implements Cloneable JavaDoc, PublicCloneable, Serializable JavaDoc {
87
88     /** For serialization. */
89     private static final long serialVersionUID = -4445994133561919083L;
90     
91     /** The area occupied by the entity (in Java 2D space). */
92     private transient Shape JavaDoc area;
93
94     /** The tool tip text for the entity. */
95     private String JavaDoc toolTipText;
96
97     /** The URL text for the entity. */
98     private String JavaDoc urlText;
99
100     /**
101      * Creates a new chart entity.
102      *
103      * @param area the area (<code>null</code> not permitted).
104      */

105     public ChartEntity(Shape JavaDoc area) {
106         // defer argument checks...
107
this(area, null);
108     }
109
110     /**
111      * Creates a new chart entity.
112      *
113      * @param area the area (<code>null</code> not permitted).
114      * @param toolTipText the tool tip text (<code>null</code> permitted).
115      */

116     public ChartEntity(Shape JavaDoc area, String JavaDoc toolTipText) {
117         // defer argument checks...
118
this(area, toolTipText, null);
119     }
120
121     /**
122      * Creates a new entity.
123      *
124      * @param area the area (<code>null</code> not permitted).
125      * @param toolTipText the tool tip text (<code>null</code> permitted).
126      * @param urlText the URL text for HTML image maps (<code>null</code>
127      * permitted).
128      */

129     public ChartEntity(Shape JavaDoc area, String JavaDoc toolTipText, String JavaDoc urlText) {
130         if (area == null) {
131             throw new IllegalArgumentException JavaDoc("Null 'area' argument.");
132         }
133         this.area = area;
134         this.toolTipText = toolTipText;
135         this.urlText = urlText;
136     }
137
138     /**
139      * Returns the area occupied by the entity (in Java 2D space).
140      *
141      * @return The area (never <code>null</code>).
142      */

143     public Shape JavaDoc getArea() {
144         return this.area;
145     }
146
147     /**
148      * Sets the area for the entity.
149      * <P>
150      * This class conveys information about chart entities back to a client.
151      * Setting this area doesn't change the entity (which has already been
152      * drawn).
153      *
154      * @param area the area (<code>null</code> not permitted).
155      */

156     public void setArea(Shape JavaDoc area) {
157         if (area == null) {
158             throw new IllegalArgumentException JavaDoc("Null 'area' argument.");
159         }
160         this.area = area;
161     }
162
163     /**
164      * Returns the tool tip text for the entity.
165      *
166      * @return The tool tip text (possibly <code>null</code>).
167      */

168     public String JavaDoc getToolTipText() {
169         return this.toolTipText;
170     }
171
172     /**
173      * Sets the tool tip text.
174      *
175      * @param text the text (<code>null</code> permitted).
176      */

177     public void setToolTipText(String JavaDoc text) {
178         this.toolTipText = text;
179     }
180
181     /**
182      * Returns the URL text for the entity.
183      *
184      * @return The URL text (possibly <code>null</code>).
185      */

186     public String JavaDoc getURLText() {
187         return this.urlText;
188     }
189
190     /**
191      * Sets the URL text.
192      *
193      * @param text the text (<code>null</code> permitted).
194      */

195     public void setURLText(String JavaDoc text) {
196         this.urlText = text;
197     }
198
199     /**
200      * Returns a string describing the entity area. This string is intended
201      * for use in an AREA tag when generating an image map.
202      *
203      * @return The shape type (never <code>null</code>).
204      */

205     public String JavaDoc getShapeType() {
206         if (this.area instanceof Rectangle2D JavaDoc) {
207             return "rect";
208         }
209         else {
210             return "poly";
211         }
212     }
213
214     /**
215      * Returns the shape coordinates as a string.
216      *
217      * @return The shape coordinates (never <code>null</code>).
218      */

219     public String JavaDoc getShapeCoords() {
220         if (this.area instanceof Rectangle2D JavaDoc) {
221             return getRectCoords((Rectangle2D JavaDoc) this.area);
222         }
223         else {
224             return getPolyCoords(this.area);
225         }
226     }
227
228     /**
229      * Returns a string containing the coordinates (x1, y1, x2, y2) for a given
230      * rectangle. This string is intended for use in an image map.
231      *
232      * @param rectangle the rectangle (<code>null</code> not permitted).
233      *
234      * @return Upper left and lower right corner of a rectangle.
235      */

236     private String JavaDoc getRectCoords(Rectangle2D JavaDoc rectangle) {
237         if (rectangle == null) {
238             throw new IllegalArgumentException JavaDoc("Null 'rectangle' argument.");
239         }
240         int x1 = (int) rectangle.getX();
241         int y1 = (int) rectangle.getY();
242         int x2 = x1 + (int) rectangle.getWidth();
243         int y2 = y1 + (int) rectangle.getHeight();
244         // fix by rfuller
245
if (x2 == x1) {
246             x2++;
247         }
248         if (y2 == y1) {
249             y2++;
250         }
251         // end fix by rfuller
252
return x1 + "," + y1 + "," + x2 + "," + y2;
253     }
254
255     /**
256      * Returns a string containing the coordinates for a given shape. This
257      * string is intended for use in an image map.
258      *
259      * @param shape the shape (<code>null</code> not permitted).
260      *
261      * @return The coordinates for a given shape as string.
262      */

263     private String JavaDoc getPolyCoords(Shape JavaDoc shape) {
264         if (shape == null) {
265             throw new IllegalArgumentException JavaDoc("Null 'shape' argument.");
266         }
267         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
268         boolean first = true;
269         float[] coords = new float[6];
270         PathIterator JavaDoc pi = shape.getPathIterator(null, 1.0);
271         while (!pi.isDone()) {
272             pi.currentSegment(coords);
273             if (first) {
274                 first = false;
275                 result.append((int) coords[0]);
276                 result.append(",").append((int) coords[1]);
277             }
278             else {
279                 result.append(",");
280                 result.append((int) coords[0]);
281                 result.append(",");
282                 result.append((int) coords[1]);
283             }
284             pi.next();
285         }
286         return result.toString();
287     }
288
289     /**
290      * Returns an HTML image map tag for this entity. The returned fragment
291      * should be <code>XHTML 1.0</code> compliant.
292      *
293      * @param toolTipTagFragmentGenerator the generator for tooltip fragment.
294      * @param urlTagFragmentGenerator the generator for the URL fragment.
295      *
296      * @return The HTML tag.
297      */

298     public String JavaDoc getImageMapAreaTag(
299             ToolTipTagFragmentGenerator toolTipTagFragmentGenerator,
300             URLTagFragmentGenerator urlTagFragmentGenerator) {
301
302         StringBuffer JavaDoc tag = new StringBuffer JavaDoc();
303         boolean hasURL
304             = (this.urlText == null ? false : !this.urlText.equals(""));
305         boolean hasToolTip
306             = (this.toolTipText == null ? false : !this.toolTipText.equals(""));
307         if (hasURL || hasToolTip) {
308             tag.append(
309                 "<area shape=\"" + getShapeType() + "\"" + " coords=\""
310                 + getShapeCoords() + "\""
311             );
312             if (hasToolTip) {
313                 tag.append(toolTipTagFragmentGenerator.generateToolTipFragment(
314                     this.toolTipText
315                 ));
316             }
317             if (hasURL) {
318                 tag.append(
319                     urlTagFragmentGenerator.generateURLFragment(this.urlText)
320                 );
321             }
322             // if there is a tool tip, we expect it to generate the title and
323
// alt values, so we only add an empty alt if there is no tooltip
324
if (!hasToolTip) {
325                 tag.append(" alt=\"\"");
326             }
327             tag.append("/>");
328         }
329         return tag.toString();
330     }
331     
332     /**
333      * Returns a string representation of the chart entity, useful for
334      * debugging.
335      *
336      * @return A string.
337      */

338     public String JavaDoc toString() {
339         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("ChartEntity: ");
340         buf.append("tooltip = ");
341         buf.append(this.toolTipText);
342         return buf.toString();
343     }
344     
345     /**
346      * Tests the entity for equality with an arbitrary object.
347      *
348      * @param obj the object to test against (<code>null</code> permitted).
349      *
350      * @return A boolean.
351      */

352     public boolean equals(Object JavaDoc obj) {
353         if (obj == this) {
354             return true;
355         }
356         if (obj instanceof ChartEntity) {
357             ChartEntity that = (ChartEntity) obj;
358             if (!this.area.equals(that.area)) {
359                 return false;
360             }
361             if (!ObjectUtilities.equal(this.toolTipText, that.toolTipText)) {
362                 return false;
363             }
364             if (!ObjectUtilities.equal(this.urlText, that.urlText)) {
365                 return false;
366             }
367             return true;
368         }
369         return false;
370     }
371     
372     /**
373      * Returns a clone of the entity.
374      *
375      * @return A clone.
376      *
377      * @throws CloneNotSupportedException if there is a problem cloning the
378      * entity.
379      */

380     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
381         return super.clone();
382     }
383     
384     /**
385      * Provides serialization support.
386      *
387      * @param stream the output stream.
388      *
389      * @throws IOException if there is an I/O error.
390      */

391     private void writeObject(ObjectOutputStream JavaDoc stream) throws IOException JavaDoc {
392         stream.defaultWriteObject();
393         SerialUtilities.writeShape(this.area, stream);
394      }
395
396     /**
397      * Provides serialization support.
398      *
399      * @param stream the input stream.
400      *
401      * @throws IOException if there is an I/O error.
402      * @throws ClassNotFoundException if there is a classpath problem.
403      */

404     private void readObject(ObjectInputStream JavaDoc stream)
405         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
406         stream.defaultReadObject();
407         this.area = SerialUtilities.readShape(stream);
408     }
409
410 }
411
Popular Tags