Source for org.jfree.chart.block.LabelBlock

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