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: * LabelBlock.java
029: * ---------------
030: * (C) Copyright 2004-2007, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): Pierre-Marie Le Biot;
034: *
035: * $Id: LabelBlock.java,v 1.8.2.5 2007/03/16 11:28:16 mungady Exp $
036: *
037: * Changes:
038: * --------
039: * 22-Oct-2004 : Version 1 (DG);
040: * 19-Apr-2005 : Added optional tooltip and URL text items,
041: * draw() method now returns entities if
042: * requested (DG);
043: * 13-May-2005 : Added methods to set the font (DG);
044: * 01-Sep-2005 : Added paint management (PMLB);
045: * Implemented equals() and clone() (PublicCloneable) (DG);
046: * ------------- JFREECHART 1.0.x ---------------------------------------------
047: * 20-Jul-2006 : Fixed entity area in draw() method (DG);
048: * 16-Mar-2007 : Fixed serialization when using GradientPaint (DG);
049: *
050: */
051:
052: package org.jfree.chart.block;
053:
054: import java.awt.Color;
055: import java.awt.Font;
056: import java.awt.Graphics2D;
057: import java.awt.Paint;
058: import java.awt.Shape;
059: import java.awt.geom.Rectangle2D;
060: import java.io.IOException;
061: import java.io.ObjectInputStream;
062: import java.io.ObjectOutputStream;
063:
064: import org.jfree.chart.entity.ChartEntity;
065: import org.jfree.chart.entity.StandardEntityCollection;
066: import org.jfree.io.SerialUtilities;
067: import org.jfree.text.TextBlock;
068: import org.jfree.text.TextBlockAnchor;
069: import org.jfree.text.TextUtilities;
070: import org.jfree.ui.Size2D;
071: import org.jfree.util.ObjectUtilities;
072: import org.jfree.util.PaintUtilities;
073: import org.jfree.util.PublicCloneable;
074:
075: /**
076: * A block containing a label.
077: */
078: public class LabelBlock extends AbstractBlock implements Block,
079: PublicCloneable {
080:
081: /**
082: * The text for the label - retained in case the label needs
083: * regenerating (for example, to change the font).
084: */
085: private String text;
086:
087: /** The label. */
088: private TextBlock label;
089:
090: /** The font. */
091: private Font font;
092:
093: /** The tool tip text (can be <code>null</code>). */
094: private String toolTipText;
095:
096: /** The URL text (can be <code>null</code>). */
097: private String urlText;
098:
099: /** The default color. */
100: public static final Paint DEFAULT_PAINT = Color.black;
101:
102: /** The paint. */
103: private transient Paint paint;
104:
105: /**
106: * Creates a new label block.
107: *
108: * @param label the label (<code>null</code> not permitted).
109: */
110: public LabelBlock(String label) {
111: this (label, new Font("SansSerif", Font.PLAIN, 10),
112: DEFAULT_PAINT);
113: }
114:
115: /**
116: * Creates a new label block.
117: *
118: * @param text the text for the label (<code>null</code> not permitted).
119: * @param font the font (<code>null</code> not permitted).
120: */
121: public LabelBlock(String text, Font font) {
122: this (text, font, DEFAULT_PAINT);
123: }
124:
125: /**
126: * Creates a new label block.
127: *
128: * @param text the text for the label (<code>null</code> not permitted).
129: * @param font the font (<code>null</code> not permitted).
130: * @param paint the paint (<code>null</code> not permitted).
131: */
132: public LabelBlock(String text, Font font, Paint paint) {
133: this .text = text;
134: this .paint = paint;
135: this .label = TextUtilities.createTextBlock(text, font,
136: this .paint);
137: this .font = font;
138: this .toolTipText = null;
139: this .urlText = null;
140: }
141:
142: /**
143: * Returns the font.
144: *
145: * @return The font (never <code>null</code>).
146: *
147: * @see #setFont(Font)
148: */
149: public Font getFont() {
150: return this .font;
151: }
152:
153: /**
154: * Sets the font and regenerates the label.
155: *
156: * @param font the font (<code>null</code> not permitted).
157: *
158: * @see #getFont()
159: */
160: public void setFont(Font font) {
161: if (font == null) {
162: throw new IllegalArgumentException("Null 'font' argument.");
163: }
164: this .font = font;
165: this .label = TextUtilities.createTextBlock(this .text, font,
166: this .paint);
167: }
168:
169: /**
170: * Returns the paint.
171: *
172: * @return The paint (never <code>null</code>).
173: *
174: * @see #setPaint(Paint)
175: */
176: public Paint getPaint() {
177: return this .paint;
178: }
179:
180: /**
181: * Sets the paint and regenerates the label.
182: *
183: * @param paint the paint (<code>null</code> not permitted).
184: *
185: * @see #getPaint()
186: */
187: public void setPaint(Paint paint) {
188: if (paint == null) {
189: throw new IllegalArgumentException("Null 'paint' argument.");
190: }
191: this .paint = paint;
192: this .label = TextUtilities.createTextBlock(this .text,
193: this .font, this .paint);
194: }
195:
196: /**
197: * Returns the tool tip text.
198: *
199: * @return The tool tip text (possibly <code>null</code>).
200: *
201: * @see #setToolTipText(String)
202: */
203: public String getToolTipText() {
204: return this .toolTipText;
205: }
206:
207: /**
208: * Sets the tool tip text.
209: *
210: * @param text the text (<code>null</code> permitted).
211: *
212: * @see #getToolTipText()
213: */
214: public void setToolTipText(String text) {
215: this .toolTipText = text;
216: }
217:
218: /**
219: * Returns the URL text.
220: *
221: * @return The URL text (possibly <code>null</code>).
222: *
223: * @see #setURLText(String)
224: */
225: public String getURLText() {
226: return this .urlText;
227: }
228:
229: /**
230: * Sets the URL text.
231: *
232: * @param text the text (<code>null</code> permitted).
233: *
234: * @see #getURLText()
235: */
236: public void setURLText(String text) {
237: this .urlText = text;
238: }
239:
240: /**
241: * Arranges the contents of the block, within the given constraints, and
242: * returns the block size.
243: *
244: * @param g2 the graphics device.
245: * @param constraint the constraint (<code>null</code> not permitted).
246: *
247: * @return The block size (in Java2D units, never <code>null</code>).
248: */
249: public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) {
250: g2.setFont(this .font);
251: Size2D s = this .label.calculateDimensions(g2);
252: return new Size2D(calculateTotalWidth(s.getWidth()),
253: calculateTotalHeight(s.getHeight()));
254: }
255:
256: /**
257: * Draws the block.
258: *
259: * @param g2 the graphics device.
260: * @param area the area.
261: */
262: public void draw(Graphics2D g2, Rectangle2D area) {
263: draw(g2, area, null);
264: }
265:
266: /**
267: * Draws the block within the specified area.
268: *
269: * @param g2 the graphics device.
270: * @param area the area.
271: * @param params ignored (<code>null</code> permitted).
272: *
273: * @return Always <code>null</code>.
274: */
275: public Object draw(Graphics2D g2, Rectangle2D area, Object params) {
276: area = trimMargin(area);
277: drawBorder(g2, area);
278: area = trimBorder(area);
279: area = trimPadding(area);
280:
281: // check if we need to collect chart entities from the container
282: EntityBlockParams ebp = null;
283: StandardEntityCollection sec = null;
284: Shape entityArea = null;
285: if (params instanceof EntityBlockParams) {
286: ebp = (EntityBlockParams) params;
287: if (ebp.getGenerateEntities()) {
288: sec = new StandardEntityCollection();
289: entityArea = (Shape) area.clone();
290: }
291: }
292: g2.setPaint(this .paint);
293: g2.setFont(this .font);
294: this .label.draw(g2, (float) area.getX(), (float) area.getY(),
295: TextBlockAnchor.TOP_LEFT);
296: BlockResult result = null;
297: if (ebp != null && sec != null) {
298: if (this .toolTipText != null || this .urlText != null) {
299: ChartEntity entity = new ChartEntity(entityArea,
300: this .toolTipText, this .urlText);
301: sec.add(entity);
302: result = new BlockResult();
303: result.setEntityCollection(sec);
304: }
305: }
306: return result;
307: }
308:
309: /**
310: * Tests this <code>LabelBlock</code> for equality with an arbitrary
311: * object.
312: *
313: * @param obj the object (<code>null</code> permitted).
314: *
315: * @return A boolean.
316: */
317: public boolean equals(Object obj) {
318: if (!(obj instanceof LabelBlock)) {
319: return false;
320: }
321: LabelBlock that = (LabelBlock) obj;
322: if (!this .text.equals(that.text)) {
323: return false;
324: }
325: if (!this .font.equals(that.font)) {
326: return false;
327: }
328: if (!PaintUtilities.equal(this .paint, that.paint)) {
329: return false;
330: }
331: if (!ObjectUtilities.equal(this .toolTipText, that.toolTipText)) {
332: return false;
333: }
334: if (!ObjectUtilities.equal(this .urlText, that.urlText)) {
335: return false;
336: }
337: return super .equals(obj);
338: }
339:
340: /**
341: * Returns a clone of this <code>LabelBlock</code> instance.
342: *
343: * @return A clone.
344: *
345: * @throws CloneNotSupportedException if there is a problem cloning.
346: */
347: public Object clone() throws CloneNotSupportedException {
348: return super .clone();
349: }
350:
351: /**
352: * Provides serialization support.
353: *
354: * @param stream the output stream.
355: *
356: * @throws IOException if there is an I/O error.
357: */
358: private void writeObject(ObjectOutputStream stream)
359: throws IOException {
360: stream.defaultWriteObject();
361: SerialUtilities.writePaint(this .paint, stream);
362: }
363:
364: /**
365: * Provides serialization support.
366: *
367: * @param stream the input stream.
368: *
369: * @throws IOException if there is an I/O error.
370: * @throws ClassNotFoundException if there is a classpath problem.
371: */
372: private void readObject(ObjectInputStream stream)
373: throws IOException, ClassNotFoundException {
374: stream.defaultReadObject();
375: this.paint = SerialUtilities.readPaint(stream);
376: }
377:
378: }
|