/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ---------------
* LabelBlock.java
* ---------------
* (C) Copyright 2004-2007, by Object Refinery Limited.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): Pierre-Marie Le Biot;
*
* Changes:
* --------
* 22-Oct-2004 : Version 1 (DG);
* 19-Apr-2005 : Added optional tooltip and URL text items,
* draw() method now returns entities if
* requested (DG);
* 13-May-2005 : Added methods to set the font (DG);
* 01-Sep-2005 : Added paint management (PMLB);
* Implemented equals() and clone() (PublicCloneable) (DG);
* ------------- JFREECHART 1.0.x ---------------------------------------------
* 20-Jul-2006 : Fixed entity area in draw() method (DG);
* 16-Mar-2007 : Fixed serialization when using GradientPaint (DG);
*
*/
package com.positive.charts.block;
import java.awt.Paint;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.GCData;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import com.positive.charts.data.util.ObjectUtilities;
import com.positive.charts.data.util.PublicCloneable;
import com.positive.charts.entity.ChartEntity;
import com.positive.charts.entity.StandardEntityCollection;
import com.positive.charts.util.Size2D;
import com.positive.charts.util.TextBlock;
import com.positive.charts.util.TextBlockAnchor;
import com.positive.charts.util.TextUtilities;
import com.positive.colorchecker.StaticColorChecker;
/**
* A block containing a label.
*/
public class LabelBlock extends AbstractBlock implements Block, PublicCloneable {
/** For serialization. */
static final long serialVersionUID = 249626098864178017L;
/**
* The text for the label - retained in case the label needs regenerating
* (for example, to change the font).
*/
private final String text;
/** The label. */
private TextBlock label;
/** The font. */
private Font font;
/** The tool tip text (can be <code>null</code>). */
private String toolTipText;
/** The URL text (can be <code>null</code>). */
private String urlText;
/** The default color. */
public static final Color DEFAULT_PAINT = StaticColorChecker.dublicateColor(SWT.COLOR_BLACK);
//Display.getDefault()
//.getSystemColor(SWT.COLOR_BLACK);
/** The default color. */
public static final Font DEFAULT_FONT = Display.getDefault()
.getSystemFont();
/** The paint. */
private transient Color paint;
/**
* Creates a new label block.
*
* @param label
* the label (<code>null</code> not permitted).
*/
public LabelBlock(final String label) {
this(label, DEFAULT_FONT, DEFAULT_PAINT);
}
/**
* Creates a new label block.
*
* @param text
* the text for the label (<code>null</code> not permitted).
* @param font
* the font (<code>null</code> not permitted).
*/
public LabelBlock(final String text, final Font font) {
this(text, font, DEFAULT_PAINT);
}
/**
* Creates a new label block.
*
* @param text
* the text for the label (<code>null</code> not permitted).
* @param font
* the font (<code>null</code> not permitted).
* @param paint
* the paint (<code>null</code> not permitted).
*/
public LabelBlock(final String text, final Font font, final Color paint) {
this.text = text;
this.paint = paint;
this.label = TextUtilities.createTextBlock(text, font, this.paint);
this.font = font;
this.toolTipText = null;
this.urlText = null;
this.setMargin(1, 1, 1, 1);
}
/**
* Arranges the contents of the block, within the given constraints, and
* returns the block size.
*
* @param g2
* the graphics device.
* @param constraint
* the constraint (<code>null</code> not permitted).
*
* @return The block size (in Java2D units, never <code>null</code>).
*/
public Size2D arrange(final GC g2, final RectangleConstraint constraint) {
g2.setFont(this.font);
final Size2D s = this.label.calculateDimensions(g2);
return new Size2D(this.calculateTotalWidth(s.getWidth()), this
.calculateTotalHeight(s.getHeight()));
}
/**
* Returns a clone of this <code>LabelBlock</code> instance.
*
* @return A clone.
*
* @throws CloneNotSupportedException
* if there is a problem cloning.
*/
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
/**
* Draws the block.
*
* @param g2
* the graphics device.
* @param area
* the area.
*/
public void draw(final GC g2, final Rectangle area) {
this.draw(g2, area, null);
}
/**
* Draws the block within the specified area.
*
* @param g2
* the graphics device.
* @param area
* the area.
* @param params
* ignored (<code>null</code> permitted).
*
* @return Always <code>null</code>.
*/
public Object draw(final GC g2, Rectangle area, final Object params) {
area = this.trimMargin(area);
this.drawBorder(g2, area);
area = this.trimBorder(area);
area = this.trimPadding(area);
// check if we need to collect chart entities from the container
EntityBlockParams ebp = null;
StandardEntityCollection sec = null;
Rectangle entityArea = null;
if (params instanceof EntityBlockParams) {
ebp = (EntityBlockParams) params;
if (ebp.getGenerateEntities()) {
sec = new StandardEntityCollection();
entityArea = new Rectangle(area.x, area.y, area.width,
area.height);
}
}
g2.setForeground(this.paint);
g2.setFont(this.font);
this.label.draw(g2, (float) area.x + 1, (float) area.y + 1,
TextBlockAnchor.TOP_LEFT);
BlockResult result = null;
if ((ebp != null) && (sec != null)) {
if ((this.toolTipText != null) || (this.urlText != null)) {
final ChartEntity entity = new ChartEntity(entityArea,
this.toolTipText, this.urlText);
sec.add(entity);
result = new BlockResult();
result.setEntityCollection(sec);
}
}
return result;
}
/**
* Tests this <code>LabelBlock</code> for equality with an arbitrary object.
*
* @param obj
* the object (<code>null</code> permitted).
*
* @return A boolean.
*/
public boolean equals(final Object obj) {
if (!(obj instanceof LabelBlock)) {
return false;
}
final LabelBlock that = (LabelBlock) obj;
if (!this.text.equals(that.text)) {
return false;
}
if (!this.font.equals(that.font)) {
return false;
}
// if (!PaintUtilities.equal(this.paint, that.paint)) {
// return false;
// }
if (!ObjectUtilities.equal(this.toolTipText, that.toolTipText)) {
return false;
}
if (!ObjectUtilities.equal(this.urlText, that.urlText)) {
return false;
}
return super.equals(obj);
}
/**
* Returns the font.
*
* @return The font (never <code>null</code>).
*
* @see #setFont(Font)
*/
public Font getFont() {
return this.font;
}
/**
* Returns the paint.
*
* @return The paint (never <code>null</code>).
*
* @see #setPaint(Paint)
*/
public Color getPaint() {
return this.paint;
}
/**
* Returns the tool tip text.
*
* @return The tool tip text (possibly <code>null</code>).
*
* @see #setToolTipText(String)
*/
public String getToolTipText() {
return this.toolTipText;
}
/**
* Returns the URL text.
*
* @return The URL text (possibly <code>null</code>).
*
* @see #setURLText(String)
*/
public String getURLText() {
return this.urlText;
}
public void internal_dispose_GC(final int handle, final GCData data) {
}
/**
* Provides serialization support.
*
* @param stream
* the input stream.
*
* @throws IOException
* if there is an I/O error.
* @throws ClassNotFoundException
* if there is a classpath problem.
*/
private void readObject(final ObjectInputStream stream) throws IOException,
ClassNotFoundException {
stream.defaultReadObject();
// this.paint = SerialUtilities.readPaint(stream);
}
/**
* Sets the font and regenerates the label.
*
* @param font
* the font (<code>null</code> not permitted).
*
* @see #getFont()
*/
public void setFont(final Font font) {
if (font == null) {
throw new IllegalArgumentException("Null 'font' argument.");
}
this.font = font;
this.label = TextUtilities.createTextBlock(this.text, font, this.paint);
}
/**
* Sets the paint and regenerates the label.
*
* @param paint
* the paint (<code>null</code> not permitted).
*
* @see #getPaint()
*/
public void setPaint(final Color paint) {
if (paint == null) {
throw new IllegalArgumentException("Null 'paint' argument.");
}
this.paint = paint;
this.label = TextUtilities.createTextBlock(this.text, this.font,
this.paint);
}
/**
* Sets the tool tip text.
*
* @param text
* the text (<code>null</code> permitted).
*
* @see #getToolTipText()
*/
public void setToolTipText(final String text) {
this.toolTipText = text;
}
/**
* Sets the URL text.
*
* @param text
* the text (<code>null</code> permitted).
*
* @see #getURLText()
*/
public void setURLText(final String text) {
this.urlText = text;
}
/**
* Provides serialization support.
*
* @param stream
* the output stream.
*
* @throws IOException
* if there is an I/O error.
*/
private void writeObject(final ObjectOutputStream stream)
throws IOException {
stream.defaultWriteObject();
// SerialUtilities.writePaint(this.paint, stream);
}
}