Frames | No Frames |
1: /* =========================================================== 2: * JFreeChart : a free chart library for the Java(tm) platform 3: * =========================================================== 4: * 5: * (C) Copyright 2000-2007, 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 20: * License along with this library; if not, write to the Free Software 21: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 22: * USA. 23: * 24: * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 25: * in the United States and other countries.] 26: * 27: * --------------- 28: * LabelBlock.java 29: * --------------- 30: * (C) Copyright 2004-2007, by Object Refinery Limited. 31: * 32: * Original Author: David Gilbert (for Object Refinery Limited); 33: * Contributor(s): Pierre-Marie Le Biot; 34: * 35: * $Id: LabelBlock.java,v 1.8.2.5 2007/03/16 11:28:16 mungady Exp $ 36: * 37: * Changes: 38: * -------- 39: * 22-Oct-2004 : Version 1 (DG); 40: * 19-Apr-2005 : Added optional tooltip and URL text items, 41: * draw() method now returns entities if 42: * requested (DG); 43: * 13-May-2005 : Added methods to set the font (DG); 44: * 01-Sep-2005 : Added paint management (PMLB); 45: * Implemented equals() and clone() (PublicCloneable) (DG); 46: * ------------- JFREECHART 1.0.x --------------------------------------------- 47: * 20-Jul-2006 : Fixed entity area in draw() method (DG); 48: * 16-Mar-2007 : Fixed serialization when using GradientPaint (DG); 49: * 50: */ 51: 52: package org.jfree.chart.block; 53: 54: import java.awt.Color; 55: import java.awt.Font; 56: import java.awt.Graphics2D; 57: import java.awt.Paint; 58: import java.awt.Shape; 59: import java.awt.geom.Rectangle2D; 60: import java.io.IOException; 61: import java.io.ObjectInputStream; 62: import java.io.ObjectOutputStream; 63: 64: import org.jfree.chart.entity.ChartEntity; 65: import org.jfree.chart.entity.StandardEntityCollection; 66: import org.jfree.io.SerialUtilities; 67: import org.jfree.text.TextBlock; 68: import org.jfree.text.TextBlockAnchor; 69: import org.jfree.text.TextUtilities; 70: import org.jfree.ui.Size2D; 71: import org.jfree.util.ObjectUtilities; 72: import org.jfree.util.PaintUtilities; 73: import org.jfree.util.PublicCloneable; 74: 75: /** 76: * A block containing a label. 77: */ 78: public class LabelBlock extends AbstractBlock 79: implements Block, PublicCloneable { 80: 81: /** 82: * The text for the label - retained in case the label needs 83: * regenerating (for example, to change the font). 84: */ 85: private String text; 86: 87: /** The label. */ 88: private TextBlock label; 89: 90: /** The font. */ 91: private Font font; 92: 93: /** The tool tip text (can be <code>null</code>). */ 94: private String toolTipText; 95: 96: /** The URL text (can be <code>null</code>). */ 97: private String urlText; 98: 99: /** 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), DEFAULT_PAINT); 112: } 113: 114: /** 115: * Creates a new label block. 116: * 117: * @param text the text for the label (<code>null</code> not permitted). 118: * @param font the font (<code>null</code> not permitted). 119: */ 120: public LabelBlock(String text, Font font) { 121: this(text, font, DEFAULT_PAINT); 122: } 123: 124: /** 125: * Creates a new label block. 126: * 127: * @param text the text for the label (<code>null</code> not permitted). 128: * @param font the font (<code>null</code> not permitted). 129: * @param paint the paint (<code>null</code> not permitted). 130: */ 131: public LabelBlock(String text, Font font, Paint paint) { 132: this.text = text; 133: this.paint = paint; 134: this.label = TextUtilities.createTextBlock(text, font, this.paint); 135: this.font = font; 136: this.toolTipText = null; 137: this.urlText = null; 138: } 139: 140: /** 141: * Returns the font. 142: * 143: * @return The font (never <code>null</code>). 144: * 145: * @see #setFont(Font) 146: */ 147: public Font getFont() { 148: return this.font; 149: } 150: 151: /** 152: * Sets the font and regenerates the label. 153: * 154: * @param font the font (<code>null</code> not permitted). 155: * 156: * @see #getFont() 157: */ 158: public void setFont(Font font) { 159: if (font == null) { 160: throw new IllegalArgumentException("Null 'font' argument."); 161: } 162: this.font = font; 163: this.label = TextUtilities.createTextBlock(this.text, font, this.paint); 164: } 165: 166: /** 167: * Returns the paint. 168: * 169: * @return The paint (never <code>null</code>). 170: * 171: * @see #setPaint(Paint) 172: */ 173: public Paint getPaint() { 174: return this.paint; 175: } 176: 177: /** 178: * Sets the paint and regenerates the label. 179: * 180: * @param paint the paint (<code>null</code> not permitted). 181: * 182: * @see #getPaint() 183: */ 184: public void setPaint(Paint paint) { 185: if (paint == null) { 186: throw new IllegalArgumentException("Null 'paint' argument."); 187: } 188: this.paint = paint; 189: this.label = TextUtilities.createTextBlock(this.text, this.font, 190: this.paint); 191: } 192: 193: /** 194: * Returns the tool tip text. 195: * 196: * @return The tool tip text (possibly <code>null</code>). 197: * 198: * @see #setToolTipText(String) 199: */ 200: public String getToolTipText() { 201: return this.toolTipText; 202: } 203: 204: /** 205: * Sets the tool tip text. 206: * 207: * @param text the text (<code>null</code> permitted). 208: * 209: * @see #getToolTipText() 210: */ 211: public void setToolTipText(String text) { 212: this.toolTipText = text; 213: } 214: 215: /** 216: * Returns the URL text. 217: * 218: * @return The URL text (possibly <code>null</code>). 219: * 220: * @see #setURLText(String) 221: */ 222: public String getURLText() { 223: return this.urlText; 224: } 225: 226: /** 227: * Sets the URL text. 228: * 229: * @param text the text (<code>null</code> permitted). 230: * 231: * @see #getURLText() 232: */ 233: public void setURLText(String text) { 234: this.urlText = text; 235: } 236: 237: /** 238: * Arranges the contents of the block, within the given constraints, and 239: * returns the block size. 240: * 241: * @param g2 the graphics device. 242: * @param constraint the constraint (<code>null</code> not permitted). 243: * 244: * @return The block size (in Java2D units, never <code>null</code>). 245: */ 246: public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) { 247: g2.setFont(this.font); 248: Size2D s = this.label.calculateDimensions(g2); 249: return new Size2D(calculateTotalWidth(s.getWidth()), 250: calculateTotalHeight(s.getHeight())); 251: } 252: 253: /** 254: * Draws the block. 255: * 256: * @param g2 the graphics device. 257: * @param area the area. 258: */ 259: public void draw(Graphics2D g2, Rectangle2D area) { 260: draw(g2, area, null); 261: } 262: 263: /** 264: * Draws the block within the specified area. 265: * 266: * @param g2 the graphics device. 267: * @param area the area. 268: * @param params ignored (<code>null</code> permitted). 269: * 270: * @return Always <code>null</code>. 271: */ 272: public Object draw(Graphics2D g2, Rectangle2D area, Object params) { 273: area = trimMargin(area); 274: drawBorder(g2, area); 275: area = trimBorder(area); 276: area = trimPadding(area); 277: 278: // check if we need to collect chart entities from the container 279: EntityBlockParams ebp = null; 280: StandardEntityCollection sec = null; 281: Shape entityArea = null; 282: if (params instanceof EntityBlockParams) { 283: ebp = (EntityBlockParams) params; 284: if (ebp.getGenerateEntities()) { 285: sec = new StandardEntityCollection(); 286: entityArea = (Shape) area.clone(); 287: } 288: } 289: g2.setPaint(this.paint); 290: g2.setFont(this.font); 291: this.label.draw(g2, (float) area.getX(), (float) area.getY(), 292: TextBlockAnchor.TOP_LEFT); 293: BlockResult result = null; 294: if (ebp != null && sec != null) { 295: if (this.toolTipText != null || this.urlText != null) { 296: ChartEntity entity = new ChartEntity(entityArea, 297: this.toolTipText, this.urlText); 298: sec.add(entity); 299: result = new BlockResult(); 300: result.setEntityCollection(sec); 301: } 302: } 303: return result; 304: } 305: 306: /** 307: * Tests this <code>LabelBlock</code> for equality with an arbitrary 308: * object. 309: * 310: * @param obj the object (<code>null</code> permitted). 311: * 312: * @return A boolean. 313: */ 314: public boolean equals(Object obj) { 315: if (!(obj instanceof LabelBlock)) { 316: return false; 317: } 318: LabelBlock that = (LabelBlock) obj; 319: if (!this.text.equals(that.text)) { 320: return false; 321: } 322: if (!this.font.equals(that.font)) { 323: return false; 324: } 325: if (!PaintUtilities.equal(this.paint, that.paint)) { 326: return false; 327: } 328: if (!ObjectUtilities.equal(this.toolTipText, that.toolTipText)) { 329: return false; 330: } 331: if (!ObjectUtilities.equal(this.urlText, that.urlText)) { 332: return false; 333: } 334: return super.equals(obj); 335: } 336: 337: /** 338: * Returns a clone of this <code>LabelBlock</code> instance. 339: * 340: * @return A clone. 341: * 342: * @throws CloneNotSupportedException if there is a problem cloning. 343: */ 344: public Object clone() throws CloneNotSupportedException { 345: return super.clone(); 346: } 347: 348: /** 349: * Provides serialization support. 350: * 351: * @param stream the output stream. 352: * 353: * @throws IOException if there is an I/O error. 354: */ 355: private void writeObject(ObjectOutputStream stream) throws IOException { 356: stream.defaultWriteObject(); 357: SerialUtilities.writePaint(this.paint, stream); 358: } 359: 360: /** 361: * Provides serialization support. 362: * 363: * @param stream the input stream. 364: * 365: * @throws IOException if there is an I/O error. 366: * @throws ClassNotFoundException if there is a classpath problem. 367: */ 368: private void readObject(ObjectInputStream stream) 369: throws IOException, ClassNotFoundException { 370: stream.defaultReadObject(); 371: this.paint = SerialUtilities.readPaint(stream); 372: } 373: 374: }