1 50 51 package org.jfree.chart.block; 52 53 import java.awt.Color ; 54 import java.awt.Font ; 55 import java.awt.Graphics2D ; 56 import java.awt.Paint ; 57 import java.awt.Shape ; 58 import java.awt.geom.Rectangle2D ; 59 60 import org.jfree.chart.entity.ChartEntity; 61 import org.jfree.chart.entity.StandardEntityCollection; 62 import org.jfree.text.TextBlock; 63 import org.jfree.text.TextBlockAnchor; 64 import org.jfree.text.TextUtilities; 65 import org.jfree.ui.Size2D; 66 import org.jfree.util.ObjectUtilities; 67 import org.jfree.util.PaintUtilities; 68 import org.jfree.util.PublicCloneable; 69 70 73 public class LabelBlock extends AbstractBlock 74 implements Block, PublicCloneable { 75 76 80 private String text; 81 82 83 private TextBlock label; 84 85 86 private Font font; 87 88 89 private String toolTipText; 90 91 92 private String urlText; 93 94 95 public static final Paint DEFAULT_PAINT = Color.black; 96 97 98 private Paint paint; 99 100 105 public LabelBlock(String label) { 106 this(label, new Font ("SansSerif", Font.PLAIN, 10), DEFAULT_PAINT); 107 } 108 109 115 public LabelBlock(String text, Font font) { 116 this(text, font, DEFAULT_PAINT); 117 } 118 119 126 public LabelBlock(String text, Font font, Paint paint) { 127 this.text = text; 128 this.paint = paint; 129 this.label = TextUtilities.createTextBlock(text, font, this.paint); 130 this.font = font; 131 this.toolTipText = null; 132 this.urlText = null; 133 } 134 135 140 public Font getFont() { 141 return this.font; 142 } 143 144 149 public void setFont(Font font) { 150 if (font == null) { 151 throw new IllegalArgumentException ("Null 'font' argument."); 152 } 153 this.font = font; 154 this.label = TextUtilities.createTextBlock(this.text, font, this.paint); 155 } 156 157 162 public Paint getPaint() { 163 return this.paint; 164 } 165 166 171 public void setPaint(Paint paint) { 172 if (paint == null) { 173 throw new IllegalArgumentException ("Null 'paint' argument."); 174 } 175 this.paint = paint; 176 this.label = TextUtilities.createTextBlock(this.text, font, this.paint); 177 } 178 179 184 public String getToolTipText() { 185 return this.toolTipText; 186 } 187 188 193 public void setToolTipText(String text) { 194 this.toolTipText = text; 195 } 196 197 202 public String getURLText() { 203 return this.urlText; 204 } 205 206 211 public void setURLText(String text) { 212 this.urlText = text; 213 } 214 215 224 public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) { 225 g2.setFont(this.font); 226 Size2D s = this.label.calculateDimensions(g2); 227 return new Size2D(calculateTotalWidth(s.getWidth()), 228 calculateTotalHeight(s.getHeight())); 229 } 230 231 237 public void draw(Graphics2D g2, Rectangle2D area) { 238 draw(g2, area, null); 239 } 240 241 250 public Object draw(Graphics2D g2, Rectangle2D area, Object params) { 251 area = trimMargin(area); 252 drawBorder(g2, area); 253 area = trimBorder(area); 254 area = trimPadding(area); 255 256 EntityBlockParams ebp = null; 258 StandardEntityCollection sec = null; 259 Shape entityArea = null; 260 if (params instanceof EntityBlockParams) { 261 ebp = (EntityBlockParams) params; 262 if (ebp.getGenerateEntities()) { 263 sec = new StandardEntityCollection(); 264 entityArea = (Shape ) area.clone(); 265 } 266 } 267 g2.setPaint(this.paint); 268 g2.setFont(this.font); 269 this.label.draw(g2, (float) area.getX(), (float) area.getY(), 270 TextBlockAnchor.TOP_LEFT); 271 BlockResult result = null; 272 if (ebp != null && sec != null) { 273 if (this.toolTipText != null || this.urlText != null) { 274 ChartEntity entity = new ChartEntity(entityArea, 275 this.toolTipText, this.urlText); 276 sec.add(entity); 277 result = new BlockResult(); 278 result.setEntityCollection(sec); 279 } 280 } 281 return result; 282 } 283 284 292 public boolean equals(Object obj) { 293 if (!(obj instanceof LabelBlock)) { 294 return false; 295 } 296 LabelBlock that = (LabelBlock) obj; 297 if (!this.text.equals(that.text)) { 298 return false; 299 } 300 if (!this.font.equals(that.font)) { 301 return false; 302 } 303 if (!PaintUtilities.equal(this.paint, that.paint)) { 304 return false; 305 } 306 if (!ObjectUtilities.equal(this.toolTipText, that.toolTipText)) { 307 return false; 308 } 309 if (!ObjectUtilities.equal(this.urlText, that.urlText)) { 310 return false; 311 } 312 if (!super.equals(obj)) { 313 return false; 314 } 315 return true; 316 } 317 318 325 public Object clone() throws CloneNotSupportedException { 326 return super.clone(); 327 } 328 } 329 | Popular Tags |