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 079 implements Block, 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), 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 }