001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2017, 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * ---------------
028 * LabelBlock.java
029 * ---------------
030 * (C) Copyright 2004-2017, by Object Refinery Limited.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   Pierre-Marie Le Biot;
034 *
035 * Changes:
036 * --------
037 * 22-Oct-2004 : Version 1 (DG);
038 * 19-Apr-2005 : Added optional tooltip and URL text items,
039 *               draw() method now returns entities if
040 *               requested (DG);
041 * 13-May-2005 : Added methods to set the font (DG);
042 * 01-Sep-2005 : Added paint management (PMLB);
043 *               Implemented equals() and clone() (PublicCloneable) (DG);
044 * ------------- JFREECHART 1.0.x ---------------------------------------------
045 * 20-Jul-2006 : Fixed entity area in draw() method (DG);
046 * 16-Mar-2007 : Fixed serialization when using GradientPaint (DG);
047 * 10-Feb-2009 : Added alignment fields (DG);
048 * 02-Jul-2013 : Use ParamChecks (DG);
049 *
050 */
051
052package org.jfree.chart.block;
053
054import java.awt.Color;
055import java.awt.Font;
056import java.awt.Graphics2D;
057import java.awt.Paint;
058import java.awt.Shape;
059import java.awt.geom.Point2D;
060import java.awt.geom.Rectangle2D;
061import java.io.IOException;
062import java.io.ObjectInputStream;
063import java.io.ObjectOutputStream;
064
065import org.jfree.chart.entity.ChartEntity;
066import org.jfree.chart.entity.StandardEntityCollection;
067import org.jfree.chart.text.TextBlock;
068import org.jfree.chart.text.TextBlockAnchor;
069import org.jfree.chart.text.TextUtils;
070import org.jfree.chart.ui.RectangleAnchor;
071import org.jfree.chart.ui.Size2D;
072import org.jfree.chart.util.ObjectUtils;
073import org.jfree.chart.util.PaintUtils;
074import org.jfree.chart.util.Args;
075import org.jfree.chart.util.PublicCloneable;
076import org.jfree.chart.util.SerialUtils;
077
078/**
079 * A block containing a label.
080 */
081public class LabelBlock extends AbstractBlock
082        implements Block, PublicCloneable {
083
084    /** For serialization. */
085    static final long serialVersionUID = 249626098864178017L;
086
087    /**
088     * The text for the label - retained in case the label needs
089     * regenerating (for example, to change the font).
090     */
091    private String text;
092
093    /** The label. */
094    private TextBlock label;
095
096    /** The font. */
097    private Font font;
098
099    /** The tool tip text (can be {@code null}). */
100    private String toolTipText;
101
102    /** The URL text (can be {@code null}). */
103    private String urlText;
104
105    /** The default color. */
106    public static final Paint DEFAULT_PAINT = Color.BLACK;
107
108    /** The paint. */
109    private transient Paint paint;
110
111    /**
112     * The content alignment point.
113     *
114     * @since 1.0.13
115     */
116    private TextBlockAnchor contentAlignmentPoint;
117
118    /**
119     * The anchor point for the text.
120     *
121     * @since 1.0.13
122     */
123    private RectangleAnchor textAnchor;
124
125    /**
126     * Creates a new label block.
127     *
128     * @param label  the label ({@code null} not permitted).
129     */
130    public LabelBlock(String label) {
131        this(label, new Font("SansSerif", Font.PLAIN, 10), DEFAULT_PAINT);
132    }
133
134    /**
135     * Creates a new label block.
136     *
137     * @param text  the text for the label ({@code null} not permitted).
138     * @param font  the font ({@code null} not permitted).
139     */
140    public LabelBlock(String text, Font font) {
141        this(text, font, DEFAULT_PAINT);
142    }
143
144    /**
145     * Creates a new label block.
146     *
147     * @param text  the text for the label ({@code null} not permitted).
148     * @param font  the font ({@code null} not permitted).
149     * @param paint the paint ({@code null} not permitted).
150     */
151    public LabelBlock(String text, Font font, Paint paint) {
152        this.text = text;
153        this.paint = paint;
154        this.label = TextUtils.createTextBlock(text, font, this.paint);
155        this.font = font;
156        this.toolTipText = null;
157        this.urlText = null;
158        this.contentAlignmentPoint = TextBlockAnchor.CENTER;
159        this.textAnchor = RectangleAnchor.CENTER;
160    }
161
162    /**
163     * Returns the font.
164     *
165     * @return The font (never {@code null}).
166     *
167     * @see #setFont(Font)
168     */
169    public Font getFont() {
170        return this.font;
171    }
172
173    /**
174     * Sets the font and regenerates the label.
175     *
176     * @param font  the font ({@code null} not permitted).
177     *
178     * @see #getFont()
179     */
180    public void setFont(Font font) {
181        Args.nullNotPermitted(font, "font");
182        this.font = font;
183        this.label = TextUtils.createTextBlock(this.text, font, this.paint);
184    }
185
186    /**
187     * Returns the paint.
188     *
189     * @return The paint (never {@code null}).
190     *
191     * @see #setPaint(Paint)
192     */
193    public Paint getPaint() {
194        return this.paint;
195    }
196
197    /**
198     * Sets the paint and regenerates the label.
199     *
200     * @param paint  the paint ({@code null} not permitted).
201     *
202     * @see #getPaint()
203     */
204    public void setPaint(Paint paint) {
205        Args.nullNotPermitted(paint, "paint");
206        this.paint = paint;
207        this.label = TextUtils.createTextBlock(this.text, this.font,
208                this.paint);
209    }
210
211    /**
212     * Returns the tool tip text.
213     *
214     * @return The tool tip text (possibly {@code null}).
215     *
216     * @see #setToolTipText(String)
217     */
218    public String getToolTipText() {
219        return this.toolTipText;
220    }
221
222    /**
223     * Sets the tool tip text.
224     *
225     * @param text  the text ({@code null} permitted).
226     *
227     * @see #getToolTipText()
228     */
229    public void setToolTipText(String text) {
230        this.toolTipText = text;
231    }
232
233    /**
234     * Returns the URL text.
235     *
236     * @return The URL text (possibly {@code null}).
237     *
238     * @see #setURLText(String)
239     */
240    public String getURLText() {
241        return this.urlText;
242    }
243
244    /**
245     * Sets the URL text.
246     *
247     * @param text  the text ({@code null} permitted).
248     *
249     * @see #getURLText()
250     */
251    public void setURLText(String text) {
252        this.urlText = text;
253    }
254
255    /**
256     * Returns the content alignment point.
257     *
258     * @return The content alignment point (never {@code null}).
259     *
260     * @since 1.0.13
261     */
262    public TextBlockAnchor getContentAlignmentPoint() {
263        return this.contentAlignmentPoint;
264    }
265
266    /**
267     * Sets the content alignment point.
268     *
269     * @param anchor  the anchor used to determine the alignment point (never
270     *         {@code null}).
271     *
272     * @since 1.0.13
273     */
274    public void setContentAlignmentPoint(TextBlockAnchor anchor) {
275        Args.nullNotPermitted(anchor, "anchor");
276        this.contentAlignmentPoint = anchor;
277    }
278
279    /**
280     * Returns the text anchor (never {@code null}).
281     *
282     * @return The text anchor.
283     *
284     * @since 1.0.13
285     */
286    public RectangleAnchor getTextAnchor() {
287        return this.textAnchor;
288    }
289
290    /**
291     * Sets the text anchor.
292     *
293     * @param anchor  the anchor ({@code null} not permitted).
294     *
295     * @since 1.0.13
296     */
297    public void setTextAnchor(RectangleAnchor anchor) {
298        this.textAnchor = anchor;
299    }
300
301    /**
302     * Arranges the contents of the block, within the given constraints, and
303     * returns the block size.
304     *
305     * @param g2  the graphics device.
306     * @param constraint  the constraint ({@code null} not permitted).
307     *
308     * @return The block size (in Java2D units, never {@code null}).
309     */
310    @Override
311    public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) {
312        g2.setFont(this.font);
313        Size2D s = this.label.calculateDimensions(g2);
314        return new Size2D(calculateTotalWidth(s.getWidth()),
315                calculateTotalHeight(s.getHeight()));
316    }
317
318    /**
319     * Draws the block.
320     *
321     * @param g2  the graphics device.
322     * @param area  the area.
323     */
324    @Override
325    public void draw(Graphics2D g2, Rectangle2D area) {
326        draw(g2, area, null);
327    }
328
329    /**
330     * Draws the block within the specified area.
331     *
332     * @param g2  the graphics device.
333     * @param area  the area.
334     * @param params  ignored ({@code null} permitted).
335     *
336     * @return Always {@code null}.
337     */
338    @Override
339    public Object draw(Graphics2D g2, Rectangle2D area, Object params) {
340        area = trimMargin(area);
341        drawBorder(g2, area);
342        area = trimBorder(area);
343        area = trimPadding(area);
344
345        // check if we need to collect chart entities from the container
346        EntityBlockParams ebp = null;
347        StandardEntityCollection sec = null;
348        Shape entityArea = null;
349        if (params instanceof EntityBlockParams) {
350            ebp = (EntityBlockParams) params;
351            if (ebp.getGenerateEntities()) {
352                sec = new StandardEntityCollection();
353                entityArea = (Shape) area.clone();
354            }
355        }
356        g2.setPaint(this.paint);
357        g2.setFont(this.font);
358        Point2D pt = this.textAnchor.getAnchorPoint(area);
359        this.label.draw(g2, (float) pt.getX(), (float) pt.getY(),
360                this.contentAlignmentPoint);
361        BlockResult result = null;
362        if (ebp != null && sec != null) {
363            if (this.toolTipText != null || this.urlText != null) {
364                ChartEntity entity = new ChartEntity(entityArea,
365                        this.toolTipText, this.urlText);
366                sec.add(entity);
367                result = new BlockResult();
368                result.setEntityCollection(sec);
369            }
370        }
371        return result;
372    }
373
374    /**
375     * Tests this {@code LabelBlock} for equality with an arbitrary object.
376     *
377     * @param obj  the object ({@code null} permitted).
378     *
379     * @return A boolean.
380     */
381    @Override
382    public boolean equals(Object obj) {
383        if (!(obj instanceof LabelBlock)) {
384            return false;
385        }
386        LabelBlock that = (LabelBlock) obj;
387        if (!this.text.equals(that.text)) {
388            return false;
389        }
390        if (!this.font.equals(that.font)) {
391            return false;
392        }
393        if (!PaintUtils.equal(this.paint, that.paint)) {
394            return false;
395        }
396        if (!ObjectUtils.equal(this.toolTipText, that.toolTipText)) {
397            return false;
398        }
399        if (!ObjectUtils.equal(this.urlText, that.urlText)) {
400            return false;
401        }
402        if (!this.contentAlignmentPoint.equals(that.contentAlignmentPoint)) {
403            return false;
404        }
405        if (!this.textAnchor.equals(that.textAnchor)) {
406            return false;
407        }
408        return super.equals(obj);
409    }
410
411    /**
412     * Returns a clone of this {@code LabelBlock} instance.
413     *
414     * @return A clone.
415     *
416     * @throws CloneNotSupportedException if there is a problem cloning.
417     */
418    @Override
419    public Object clone() throws CloneNotSupportedException {
420        return super.clone();
421    }
422
423    /**
424     * Provides serialization support.
425     *
426     * @param stream  the output stream.
427     *
428     * @throws IOException if there is an I/O error.
429     */
430    private void writeObject(ObjectOutputStream stream) throws IOException {
431        stream.defaultWriteObject();
432        SerialUtils.writePaint(this.paint, stream);
433    }
434
435    /**
436     * Provides serialization support.
437     *
438     * @param stream  the input stream.
439     *
440     * @throws IOException  if there is an I/O error.
441     * @throws ClassNotFoundException  if there is a classpath problem.
442     */
443    private void readObject(ObjectInputStream stream)
444        throws IOException, ClassNotFoundException {
445        stream.defaultReadObject();
446        this.paint = SerialUtils.readPaint(stream);
447    }
448
449}