KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > LegendItem


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  * LegendItem.java
28  * ---------------
29  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): Andrzej Porebski;
33  * David Li;
34  * Wolfgang Irler;
35  * Luke Quinane;
36  *
37  * $Id: LegendItem.java,v 1.9 2005/05/19 15:40:55 mungady Exp $
38  *
39  * Changes (from 2-Oct-2002)
40  * -------------------------
41  * 02-Oct-2002 : Fixed errors reported by Checkstyle (DG);
42  * 17-Jan-2003 : Dropped outlineStroke attribute (DG);
43  * 08-Oct-2003 : Applied patch for displaying series line style, contributed by
44  * Luke Quinane (DG);
45  * 21-Jan-2004 : Added the shapeFilled flag (DG);
46  * 04-Jun-2004 : Added equals() method, implemented Serializable (DG);
47  * 25-Nov-2004 : Changes required by new LegendTitle implementation (DG);
48  * 11-Jan-2005 : Removed deprecated code in preparation for the 1.0.0
49  * release (DG);
50  * 20-Apr-2005 : Added tooltip and URL text (DG);
51  *
52  */

53
54 package org.jfree.chart;
55
56 import java.awt.BasicStroke JavaDoc;
57 import java.awt.Color JavaDoc;
58 import java.awt.Paint JavaDoc;
59 import java.awt.Shape JavaDoc;
60 import java.awt.Stroke JavaDoc;
61 import java.awt.geom.Line2D JavaDoc;
62 import java.io.IOException JavaDoc;
63 import java.io.ObjectInputStream JavaDoc;
64 import java.io.ObjectOutputStream JavaDoc;
65 import java.io.Serializable JavaDoc;
66
67 import org.jfree.io.SerialUtilities;
68 import org.jfree.util.ObjectUtilities;
69 import org.jfree.util.ShapeUtilities;
70
71 /**
72  * A storage object for recording the properties of a legend item, without any
73  * consideration for layout issues. Instances of this class are immutable.
74  */

75 public class LegendItem implements Serializable JavaDoc {
76
77     // TODO: keeping this class immutable is becoming a lot of overhead, need
78
// to look at the consequences of dropping immutability
79

80     /** For serialization. */
81     private static final long serialVersionUID = -797214582948827144L;
82     
83     /** The label. */
84     private String JavaDoc label;
85     
86     /**
87      * The description (not currently used - could be displayed as a tool tip).
88      */

89     private String JavaDoc description;
90     
91     /** The tool tip text. */
92     private String JavaDoc toolTipText;
93     
94     /** The url text. */
95     private String JavaDoc urlText;
96
97     /** A flag that controls whether or not the shape is visible. */
98     private boolean shapeVisible;
99     
100     /** The shape. */
101     private transient Shape JavaDoc shape;
102     
103     /** A flag that controls whether or not the shape is filled. */
104     private boolean shapeFilled;
105
106     /** The paint. */
107     private transient Paint JavaDoc fillPaint;
108     
109     /** A flag that controls whether or not the shape outline is visible. */
110     private boolean shapeOutlineVisible;
111     
112     /** The outline paint. */
113     private transient Paint JavaDoc outlinePaint;
114     
115     /** The outline stroke. */
116     private transient Stroke JavaDoc outlineStroke;
117
118     /** A flag that controls whether or not the line is visible. */
119     private boolean lineVisible;
120     
121     /** The line. */
122     private transient Shape JavaDoc line;
123     
124     /** The stroke. */
125     private transient Stroke JavaDoc lineStroke;
126     
127     /** The line paint. */
128     private transient Paint JavaDoc linePaint;
129
130     /**
131      * The shape must be non-null for a LegendItem - if no shape is required,
132      * use this.
133      */

134     private static final Shape JavaDoc UNUSED_SHAPE = new Line2D.Float JavaDoc();
135     
136     /**
137      * The stroke must be non-null for a LegendItem - if no stroke is required,
138      * use this.
139      */

140     private static final Stroke JavaDoc UNUSED_STROKE = new BasicStroke JavaDoc(0.0f);
141     
142     /**
143      * Creates a legend item with a filled shape. The shape is not outlined,
144      * and no line is visible.
145      *
146      * @param label the label (<code>null</code> not permitted).
147      * @param description the description (<code>null</code> permitted).
148      * @param toolTipText the tool tip text (<code>null</code> permitted).
149      * @param urlText the URL text (<code>null</code> permitted).
150      * @param shape the shape (<code>null</code> not permitted).
151      * @param fillPaint the paint used to fill the shape (<code>null</code>
152      * not permitted).
153      */

154     public LegendItem(String JavaDoc label, String JavaDoc description,
155                       String JavaDoc toolTipText, String JavaDoc urlText,
156                       Shape JavaDoc shape, Paint JavaDoc fillPaint) {
157         this(
158             label,
159             description,
160             toolTipText,
161             urlText,
162             true, // shape visible
163
shape,
164             true, // shape filled
165
fillPaint,
166             false, // shape not outlined
167
Color.black,
168             UNUSED_STROKE,
169             false, // line not visible
170
UNUSED_SHAPE,
171             UNUSED_STROKE,
172             Color.black
173         );
174     }
175     
176     /**
177      * Creates a legend item with a filled and outlined shape.
178      *
179      * @param label the label (<code>null</code> not permitted).
180      * @param description the description (<code>null</code> permitted).
181      * @param toolTipText the tool tip text (<code>null</code> permitted).
182      * @param urlText the URL text (<code>null</code> permitted).
183      * @param shape the shape (<code>null</code> not permitted).
184      * @param fillPaint the paint used to fill the shape (<code>null</code>
185      * not permitted).
186      * @param outlineStroke the outline stroke (<code>null</code> not
187      * permitted).
188      * @param outlinePaint the outline paint (<code>null</code> not
189      * permitted).
190      */

191     public LegendItem(String JavaDoc label, String JavaDoc description,
192                       String JavaDoc toolTipText, String JavaDoc urlText,
193                       Shape JavaDoc shape, Paint JavaDoc fillPaint,
194                       Stroke JavaDoc outlineStroke, Paint JavaDoc outlinePaint) {
195         this(
196             label,
197             description,
198             toolTipText,
199             urlText,
200             true, // shape visible
201
shape,
202             true, // shape filled
203
fillPaint,
204             true, // shape outlined
205
outlinePaint,
206             outlineStroke,
207             false, // line not visible
208
UNUSED_SHAPE,
209             UNUSED_STROKE,
210             Color.black
211         );
212     }
213     
214     /**
215      * Creates a legend item using a line.
216      *
217      * @param label the label (<code>null</code> not permitted).
218      * @param description the description (<code>null</code> permitted).
219      * @param toolTipText the tool tip text (<code>null</code> permitted).
220      * @param urlText the URL text (<code>null</code> permitted).
221      * @param line the line (<code>null</code> not permitted).
222      * @param lineStroke the line stroke (<code>null</code> not permitted).
223      * @param linePaint the line paint (<code>null</code> not permitted).
224      */

225     public LegendItem(String JavaDoc label, String JavaDoc description,
226                       String JavaDoc toolTipText, String JavaDoc urlText,
227                       Shape JavaDoc line, Stroke JavaDoc lineStroke, Paint JavaDoc linePaint) {
228         this(
229             label,
230             description,
231             toolTipText,
232             urlText,
233             false, // shape not visible
234
UNUSED_SHAPE,
235             false, // shape not filled
236
Color.black,
237             false, // shape not outlined
238
Color.black,
239             UNUSED_STROKE,
240             true, // line visible
241
line,
242             lineStroke,
243             linePaint
244         );
245     }
246     
247     /**
248      * Creates a new legend item.
249      *
250      * @param label the label (<code>null</code> not permitted).
251      * @param description the description (not currently used,
252      * <code>null</code> permitted).
253      * @param toolTipText the tool tip text (<code>null</code> permitted).
254      * @param urlText the URL text (<code>null</code> permitted).
255      * @param shapeVisible a flag that controls whether or not the shape is
256      * displayed.
257      * @param shape the shape (<code>null</code> permitted).
258      * @param shapeFilled a flag that controls whether or not the shape is
259      * filled.
260      * @param fillPaint the fill paint (<code>null</code> not permitted).
261      * @param shapeOutlineVisible a flag that controls whether or not the
262      * shape is outlined.
263      * @param outlinePaint the outline paint (<code>null</code> not permitted).
264      * @param outlineStroke the outline stroke (<code>null</code> not
265      * permitted).
266      * @param lineVisible a flag that controls whether or not the line is
267      * visible.
268      * @param line the line.
269      * @param lineStroke the stroke (<code>null</code> not permitted).
270      * @param linePaint the line paint (<code>null</code> not permitted).
271      */

272     public LegendItem(String JavaDoc label,
273                       String JavaDoc description,
274                       String JavaDoc toolTipText,
275                       String JavaDoc urlText,
276                       boolean shapeVisible,
277                       Shape JavaDoc shape,
278                       boolean shapeFilled,
279                       Paint JavaDoc fillPaint,
280                       boolean shapeOutlineVisible,
281                       Paint JavaDoc outlinePaint,
282                       Stroke JavaDoc outlineStroke,
283                       boolean lineVisible,
284                       Shape JavaDoc line,
285                       Stroke JavaDoc lineStroke,
286                       Paint JavaDoc linePaint) {
287         
288         if (label == null) {
289             throw new IllegalArgumentException JavaDoc("Null 'label' argument.");
290         }
291         if (fillPaint == null) {
292             throw new IllegalArgumentException JavaDoc("Null 'fillPaint' argument.");
293         }
294         if (lineStroke == null) {
295             throw new IllegalArgumentException JavaDoc("Null 'lineStroke' argument.");
296         }
297         if (outlinePaint == null) {
298             throw new IllegalArgumentException JavaDoc("Null 'outlinePaint' argument.");
299         }
300         if (outlineStroke == null) {
301             throw new IllegalArgumentException JavaDoc(
302                 "Null 'outlineStroke' argument."
303             );
304         }
305         this.label = label;
306         this.description = description;
307         this.shapeVisible = shapeVisible;
308         this.shape = shape;
309         this.shapeFilled = shapeFilled;
310         this.fillPaint = fillPaint;
311         this.shapeOutlineVisible = shapeOutlineVisible;
312         this.outlinePaint = outlinePaint;
313         this.outlineStroke = outlineStroke;
314         this.lineVisible = lineVisible;
315         this.line = line;
316         this.lineStroke = lineStroke;
317         this.linePaint = linePaint;
318         this.toolTipText = toolTipText;
319         this.urlText = urlText;
320     }
321
322     /**
323      * Returns the label.
324      *
325      * @return The label (never <code>null</code>).
326      */

327     public String JavaDoc getLabel() {
328         return this.label;
329     }
330
331     /**
332      * Returns the description for the legend item.
333      *
334      * @return The description.
335      */

336     public String JavaDoc getDescription() {
337         return this.description;
338     }
339     
340     /**
341      * Returns the tool tip text.
342      *
343      * @return The tool tip text (possibly <code>null</code>).
344      */

345     public String JavaDoc getToolTipText() {
346         return this.toolTipText;
347     }
348     
349     /**
350      * Returns the URL text.
351      *
352      * @return The URL text (possibly <code>null</code>).
353      */

354     public String JavaDoc getURLText() {
355         return this.urlText;
356     }
357     
358     /**
359      * Returns a flag that indicates whether or not the shape is visible.
360      *
361      * @return A boolean.
362      */

363     public boolean isShapeVisible() {
364         return this.shapeVisible;
365     }
366     
367     /**
368      * Returns the shape used to label the series represented by this legend
369      * item.
370      *
371      * @return The shape (never <code>null</code>).
372      */

373     public Shape JavaDoc getShape() {
374         return this.shape;
375     }
376     
377     /**
378      * Returns a flag that controls whether or not the shape is filled.
379      *
380      * @return A boolean.
381      */

382     public boolean isShapeFilled() {
383         return this.shapeFilled;
384     }
385
386     /**
387      * Returns the fill paint.
388      *
389      * @return The fill paint (never <code>null</code>).
390      */

391     public Paint JavaDoc getFillPaint() {
392         return this.fillPaint;
393     }
394
395     /**
396      * Returns the flag that controls whether or not the shape outline
397      * is visible.
398      *
399      * @return A boolean.
400      */

401     public boolean isShapeOutlineVisible() {
402         return this.shapeOutlineVisible;
403     }
404     
405     /**
406      * Returns the line stroke for the series.
407      *
408      * @return The stroke (never <code>null</code>).
409      */

410     public Stroke JavaDoc getLineStroke() {
411         return this.lineStroke;
412     }
413     
414     /**
415      * Returns the paint used for lines.
416      *
417      * @return The paint.
418      */

419     public Paint JavaDoc getLinePaint() {
420         return this.linePaint;
421     }
422     
423     /**
424      * Returns the outline paint.
425      *
426      * @return The outline paint (never <code>null</code>).
427      */

428     public Paint JavaDoc getOutlinePaint() {
429         return this.outlinePaint;
430     }
431
432     /**
433      * Returns the outline stroke.
434      *
435      * @return The outline stroke (never <code>null</code>).
436      */

437     public Stroke JavaDoc getOutlineStroke() {
438         return this.outlineStroke;
439     }
440     
441     /**
442      * Returns a flag that indicates whether or not the line is visible.
443      *
444      * @return A boolean.
445      */

446     public boolean isLineVisible() {
447         return this.lineVisible;
448     }
449     
450     /**
451      * Returns the line.
452      *
453      * @return The line.
454      */

455     public Shape JavaDoc getLine() {
456         return this.line;
457     }
458     
459     /**
460      * Tests this item for equality with an arbitrary object.
461      *
462      * @param obj the object (<code>null</code> permitted).
463      *
464      * @return A boolean.
465      */

466     public boolean equals(Object JavaDoc obj) {
467         if (obj == this) {
468             return true;
469         }
470         if (!(obj instanceof LegendItem)) {
471                 return false;
472         }
473         LegendItem that = (LegendItem) obj;
474         if (!this.label.equals(that.label)) {
475             return false;
476         }
477         if (!ObjectUtilities.equal(this.description, that.description)) {
478             return false;
479         }
480         if (this.shapeVisible != that.shapeVisible) {
481             return false;
482         }
483         if (!ShapeUtilities.equal(this.shape, that.shape)) {
484             return false;
485         }
486         if (this.shapeFilled != that.shapeFilled) {
487             return false;
488         }
489         if (!this.fillPaint.equals(that.fillPaint)) {
490             return false;
491         }
492         if (this.shapeOutlineVisible != that.shapeOutlineVisible) {
493             return false;
494         }
495         if (!this.outlineStroke.equals(that.outlineStroke)) {
496             return false;
497         }
498         if (!this.outlinePaint.equals(that.outlinePaint)) {
499             return false;
500         }
501         if (!this.lineVisible == that.lineVisible) {
502             return false;
503         }
504         if (!ShapeUtilities.equal(this.line, that.line)) {
505             return false;
506         }
507         if (!this.lineStroke.equals(that.lineStroke)) {
508             return false;
509         }
510         if (!this.linePaint.equals(that.linePaint)) {
511             return false;
512         }
513         return true;
514     }
515     
516     /**
517      * Provides serialization support.
518      *
519      * @param stream the output stream (<code>null</code> not permitted).
520      *
521      * @throws IOException if there is an I/O error.
522      */

523     private void writeObject(ObjectOutputStream JavaDoc stream) throws IOException JavaDoc {
524         stream.defaultWriteObject();
525         SerialUtilities.writeShape(this.shape, stream);
526         SerialUtilities.writePaint(this.fillPaint, stream);
527         SerialUtilities.writeStroke(this.outlineStroke, stream);
528         SerialUtilities.writePaint(this.outlinePaint, stream);
529         SerialUtilities.writeShape(this.line, stream);
530         SerialUtilities.writeStroke(this.lineStroke, stream);
531         SerialUtilities.writePaint(this.linePaint, stream);
532     }
533
534     /**
535      * Provides serialization support.
536      *
537      * @param stream the input stream (<code>null</code> not permitted).
538      *
539      * @throws IOException if there is an I/O error.
540      * @throws ClassNotFoundException if there is a classpath problem.
541      */

542     private void readObject(ObjectInputStream JavaDoc stream)
543         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
544         stream.defaultReadObject();
545         this.shape = SerialUtilities.readShape(stream);
546         this.fillPaint = SerialUtilities.readPaint(stream);
547         this.outlineStroke = SerialUtilities.readStroke(stream);
548         this.outlinePaint = SerialUtilities.readPaint(stream);
549         this.line = SerialUtilities.readShape(stream);
550         this.lineStroke = SerialUtilities.readStroke(stream);
551         this.linePaint = SerialUtilities.readPaint(stream);
552     }
553     
554 }
555
Popular Tags