| 1 |
/** |
| 2 |
* ======================================== |
| 3 |
* JFreeReport : a free Java report library |
| 4 |
* ======================================== |
| 5 |
* |
| 6 |
* Project Info: http://reporting.pentaho.org/ |
| 7 |
* |
| 8 |
* (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors. |
| 9 |
* |
| 10 |
* This library is free software; you can redistribute it and/or modify it under the terms |
| 11 |
* of the GNU Lesser General Public License as published by the Free Software Foundation; |
| 12 |
* either version 2.1 of the License, or (at your option) any later version. |
| 13 |
* |
| 14 |
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 15 |
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 16 |
* See the GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License along with this |
| 19 |
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
| 20 |
* Boston, MA 02111-1307, USA. |
| 21 |
* |
| 22 |
* [Java is a trademark or registered trademark of Sun Microsystems, Inc. |
| 23 |
* in the United States and other countries.] |
| 24 |
* |
| 25 |
* ------------ |
| 26 |
* $Id: SurveyScale.java 10756 2009-12-02 15:58:24Z tmorgner $ |
| 27 |
* ------------ |
| 28 |
* (C) Copyright 2000-2005, by Object Refinery Limited. |
| 29 |
* (C) Copyright 2005-2007, by Pentaho Corporation. |
| 30 |
*/ |
| 31 |
|
| 32 |
package org.jfree.report.modules.misc.survey; |
| 33 |
|
| 34 |
import java.awt.BasicStroke; |
| 35 |
import java.awt.Color; |
| 36 |
import java.awt.Font; |
| 37 |
import java.awt.FontMetrics; |
| 38 |
import java.awt.Graphics2D; |
| 39 |
import java.awt.Paint; |
| 40 |
import java.awt.Shape; |
| 41 |
import java.awt.Stroke; |
| 42 |
import java.awt.font.FontRenderContext; |
| 43 |
import java.awt.font.LineMetrics; |
| 44 |
import java.awt.geom.Ellipse2D; |
| 45 |
import java.awt.geom.GeneralPath; |
| 46 |
import java.awt.geom.Line2D; |
| 47 |
import java.awt.geom.Rectangle2D; |
| 48 |
import java.io.IOException; |
| 49 |
import java.io.ObjectInputStream; |
| 50 |
import java.io.ObjectOutputStream; |
| 51 |
import java.io.Serializable; |
| 52 |
import java.util.ArrayList; |
| 53 |
|
| 54 |
import org.jfree.report.JFreeReportBoot; |
| 55 |
import org.pentaho.reporting.libraries.base.util.ObjectUtilities; |
| 56 |
import org.pentaho.reporting.libraries.serializer.SerializerHelper; |
| 57 |
|
| 58 |
/** |
| 59 |
* Draws a survey scale. By implementing the Drawable interface, instances can be displayed within a report using the |
| 60 |
* DrawableElement class. |
| 61 |
* |
| 62 |
* @author David Gilbert |
| 63 |
*/ |
| 64 |
public class SurveyScale implements Serializable |
| 65 |
{ |
| 66 |
private static final Number[] EMPTY_VALUES = new Number[0]; |
| 67 |
|
| 68 |
/** |
| 69 |
* The lowest response value on the scale. |
| 70 |
*/ |
| 71 |
private int lowest; |
| 72 |
|
| 73 |
/** |
| 74 |
* The highest response value on the scale. |
| 75 |
*/ |
| 76 |
private int highest; |
| 77 |
|
| 78 |
/** |
| 79 |
* The lower margin. |
| 80 |
*/ |
| 81 |
private double lowerMargin = 0.10; |
| 82 |
|
| 83 |
/** |
| 84 |
* The upper margin. |
| 85 |
*/ |
| 86 |
private double upperMargin = 0.10; |
| 87 |
|
| 88 |
/** |
| 89 |
* A list of flags that control whether or not the shapes are filled. |
| 90 |
*/ |
| 91 |
private ArrayList fillShapes; |
| 92 |
|
| 93 |
/** |
| 94 |
* The values to display. |
| 95 |
*/ |
| 96 |
private Number[] values; |
| 97 |
|
| 98 |
/** |
| 99 |
* The lower bound of the highlighted range. |
| 100 |
*/ |
| 101 |
private Number rangeLowerBound; |
| 102 |
|
| 103 |
/** |
| 104 |
* The upper bound of the highlighted range. |
| 105 |
*/ |
| 106 |
private Number rangeUpperBound; |
| 107 |
|
| 108 |
/** |
| 109 |
* Draw a border? |
| 110 |
*/ |
| 111 |
private boolean drawBorder; |
| 112 |
|
| 113 |
/** |
| 114 |
* Draw the tick marks? |
| 115 |
*/ |
| 116 |
private boolean drawTickMarks; |
| 117 |
|
| 118 |
/** |
| 119 |
* Draw the scale values. |
| 120 |
*/ |
| 121 |
private boolean drawScaleValues; |
| 122 |
|
| 123 |
/** |
| 124 |
* The font used to display the scale values. |
| 125 |
*/ |
| 126 |
private Font scaleValueFont; |
| 127 |
|
| 128 |
/** |
| 129 |
* The paint used to draw the scale values. |
| 130 |
*/ |
| 131 |
private transient Paint scaleValuePaint; |
| 132 |
|
| 133 |
/** |
| 134 |
* The range paint. |
| 135 |
*/ |
| 136 |
private transient Paint rangePaint; |
| 137 |
|
| 138 |
/** |
| 139 |
* The shapes to display. |
| 140 |
*/ |
| 141 |
private transient ArrayList shapes; |
| 142 |
|
| 143 |
/** |
| 144 |
* The fill paint. |
| 145 |
*/ |
| 146 |
private transient Paint fillPaint; |
| 147 |
|
| 148 |
/** |
| 149 |
* The outline stroke for the shapes. |
| 150 |
*/ |
| 151 |
private transient Stroke outlineStroke; |
| 152 |
|
| 153 |
/** |
| 154 |
* The default shape, if no shape is defined in the shapeList for the given value. |
| 155 |
*/ |
| 156 |
private transient Shape defaultShape; |
| 157 |
|
| 158 |
/** |
| 159 |
* The tick mark paint. |
| 160 |
*/ |
| 161 |
private transient Paint tickMarkPaint; |
| 162 |
|
| 163 |
private transient Paint borderPaint; |
| 164 |
|
| 165 |
private int range; |
| 166 |
private double lowerBound; |
| 167 |
private double upperBound; |
| 168 |
private boolean useFontMetricsGetStringBounds; |
| 169 |
private boolean autoConfigure; |
| 170 |
|
| 171 |
/** |
| 172 |
* Creates a new default instance. |
| 173 |
*/ |
| 174 |
public SurveyScale() |
| 175 |
{ |
| 176 |
this(1, 5, SurveyScale.EMPTY_VALUES); |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Creates a new instance. |
| 181 |
* |
| 182 |
* @param lowest the lowest response value on the scale. |
| 183 |
* @param highest the highest response value on the scale. |
| 184 |
* @param values the values to display. |
| 185 |
*/ |
| 186 |
public SurveyScale(final int lowest, final int highest, |
| 187 |
final Number[] values) |
| 188 |
{ |
| 189 |
final String configFontMetricsStringBounds = JFreeReportBoot.getInstance().getGlobalConfig().getConfigProperty |
| 190 |
("org.jfree.report.modules.misc.survey.UseFontMetricsGetStringBounds", "auto"); |
| 191 |
if ("auto".equals(configFontMetricsStringBounds)) |
| 192 |
{ |
| 193 |
useFontMetricsGetStringBounds = (ObjectUtilities.isJDK14() == true); |
| 194 |
} |
| 195 |
else |
| 196 |
{ |
| 197 |
useFontMetricsGetStringBounds = "true".equals(configFontMetricsStringBounds); |
| 198 |
} |
| 199 |
|
| 200 |
this.lowest = lowest; |
| 201 |
this.highest = highest; |
| 202 |
if (values == null) |
| 203 |
{ |
| 204 |
this.values = SurveyScale.EMPTY_VALUES; |
| 205 |
} |
| 206 |
else |
| 207 |
{ |
| 208 |
this.values = (Number[]) values.clone(); |
| 209 |
} |
| 210 |
|
| 211 |
this.drawTickMarks = true; |
| 212 |
this.tickMarkPaint = Color.gray; |
| 213 |
|
| 214 |
this.scaleValuePaint = Color.black; |
| 215 |
this.defaultShape = new Ellipse2D.Double(-3.0, -3.0, 6.0, 6.0); |
| 216 |
|
| 217 |
this.rangeLowerBound = null; |
| 218 |
this.rangeUpperBound = null; |
| 219 |
this.rangePaint = Color.lightGray; |
| 220 |
|
| 221 |
this.shapes = createShapeList(); |
| 222 |
this.fillShapes = new ArrayList(); |
| 223 |
this.fillShapes.add(0, Boolean.TRUE); |
| 224 |
this.fillPaint = Color.black; |
| 225 |
this.outlineStroke = new BasicStroke(0.5f); |
| 226 |
recompute(); |
| 227 |
} |
| 228 |
|
| 229 |
public boolean isAutoConfigure() |
| 230 |
{ |
| 231 |
return autoConfigure; |
| 232 |
} |
| 233 |
|
| 234 |
public void setAutoConfigure(final boolean autoConfigure) |
| 235 |
{ |
| 236 |
this.autoConfigure = autoConfigure; |
| 237 |
recompute(); |
| 238 |
} |
| 239 |
|
| 240 |
public int getLowest() |
| 241 |
{ |
| 242 |
return lowest; |
| 243 |
} |
| 244 |
|
| 245 |
public void setLowest(final int lowest) |
| 246 |
{ |
| 247 |
this.lowest = lowest; |
| 248 |
recompute(); |
| 249 |
} |
| 250 |
|
| 251 |
public int getHighest() |
| 252 |
{ |
| 253 |
return highest; |
| 254 |
} |
| 255 |
|
| 256 |
public void setHighest(final int highest) |
| 257 |
{ |
| 258 |
this.highest = highest; |
| 259 |
recompute(); |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* This method is called whenever lowest or highest has changed. It will recompute the range and upper and lower |
| 264 |
* bounds. |
| 265 |
*/ |
| 266 |
protected void recompute() |
| 267 |
{ |
| 268 |
this.range = Math.max(0, this.highest - this.lowest); |
| 269 |
this.lowerBound = this.lowest - (range * this.lowerMargin); |
| 270 |
this.upperBound = this.highest + (range * this.upperMargin); |
| 271 |
} |
| 272 |
|
| 273 |
protected int getRange() |
| 274 |
{ |
| 275 |
return range; |
| 276 |
} |
| 277 |
|
| 278 |
protected void setRange(final int range) |
| 279 |
{ |
| 280 |
this.range = range; |
| 281 |
} |
| 282 |
|
| 283 |
protected double getLowerBound() |
| 284 |
{ |
| 285 |
return lowerBound; |
| 286 |
} |
| 287 |
|
| 288 |
protected void setLowerBound(final double lowerBound) |
| 289 |
{ |
| 290 |
this.lowerBound = lowerBound; |
| 291 |
} |
| 292 |
|
| 293 |
protected double getUpperBound() |
| 294 |
{ |
| 295 |
return upperBound; |
| 296 |
} |
| 297 |
|
| 298 |
protected void setUpperBound(final double upperBound) |
| 299 |
{ |
| 300 |
this.upperBound = upperBound; |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* Creates the shape list used when drawing the scale. The list returned must contain exactly 6 elements. |
| 305 |
* |
| 306 |
* @return |
| 307 |
*/ |
| 308 |
protected ArrayList createShapeList() |
| 309 |
{ |
| 310 |
final ArrayList shapes = new ArrayList(); |
| 311 |
//this.shapes.setShape(0, createDiagonalCross(3.0f, 0.5f)); |
| 312 |
shapes.add(new Ellipse2D.Double(-3.0, -3.0, 6.0, 6.0)); |
| 313 |
shapes.add(SurveyScale.createDownTriangle(4.0f)); |
| 314 |
shapes.add(SurveyScale.createUpTriangle(4.0f)); |
| 315 |
shapes.add(SurveyScale.createDiamond(4.0f)); |
| 316 |
shapes.add(new Rectangle2D.Double(-4.0, -4.0, 8.0, 8.0)); |
| 317 |
shapes.add(new Ellipse2D.Double(-4.0, -4.0, 8.0, 8.0)); |
| 318 |
//this.shapes.setShape(5, createDiagonalCross(3.0f, 0.5f)); |
| 319 |
return shapes; |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
/** |
| 324 |
* Creates a diamond shape. |
| 325 |
* |
| 326 |
* @param s the size factor (equal to half the height of the diamond). |
| 327 |
* @return A diamond shape. |
| 328 |
*/ |
| 329 |
public static Shape createDiamond(final float s) |
| 330 |
{ |
| 331 |
final GeneralPath p0 = new GeneralPath(); |
| 332 |
p0.moveTo(0.0f, -s); |
| 333 |
p0.lineTo(s, 0.0f); |
| 334 |
p0.lineTo(0.0f, s); |
| 335 |
p0.lineTo(-s, 0.0f); |
| 336 |
p0.closePath(); |
| 337 |
return p0; |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* Creates a triangle shape that points upwards. |
| 342 |
* |
| 343 |
* @param s the size factor (equal to half the height of the triangle). |
| 344 |
* @return A triangle shape. |
| 345 |
*/ |
| 346 |
public static Shape createUpTriangle(final float s) |
| 347 |
{ |
| 348 |
final GeneralPath p0 = new GeneralPath(); |
| 349 |
p0.moveTo(0.0f, -s); |
| 350 |
p0.lineTo(s, s); |
| 351 |
p0.lineTo(-s, s); |
| 352 |
p0.closePath(); |
| 353 |
return p0; |
| 354 |
} |
| 355 |
|
| 356 |
/** |
| 357 |
* Creates a triangle shape that points downwards. |
| 358 |
* |
| 359 |
* @param s the size factor (equal to half the height of the triangle). |
| 360 |
* @return A triangle shape. |
| 361 |
*/ |
| 362 |
public static Shape createDownTriangle(final float s) |
| 363 |
{ |
| 364 |
final GeneralPath p0 = new GeneralPath(); |
| 365 |
p0.moveTo(0.0f, s); |
| 366 |
p0.lineTo(s, -s); |
| 367 |
p0.lineTo(-s, -s); |
| 368 |
p0.closePath(); |
| 369 |
return p0; |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
/** |
| 374 |
* Returns the lower bound of the highlighted range. A <code>null</code> value indicates that no range is set for |
| 375 |
* highlighting. |
| 376 |
* |
| 377 |
* @return The lower bound (possibly <code>null</code>). |
| 378 |
*/ |
| 379 |
public Number getRangeLowerBound() |
| 380 |
{ |
| 381 |
return this.rangeLowerBound; |
| 382 |
} |
| 383 |
|
| 384 |
/** |
| 385 |
* Sets the lower bound for the range that is highlighted on the scale. |
| 386 |
* |
| 387 |
* @param bound the lower bound (<code>null</code> permitted). |
| 388 |
*/ |
| 389 |
public void setRangeLowerBound(final Number bound) |
| 390 |
{ |
| 391 |
this.rangeLowerBound = bound; |
| 392 |
} |
| 393 |
|
| 394 |
/** |
| 395 |
* Returns the upper bound of the highlighted range. A <code>null</code> value indicates that no range is set for |
| 396 |
* highlighting. |
| 397 |
* |
| 398 |
* @return The upper bound (possibly <code>null</code>). |
| 399 |
*/ |
| 400 |
public Number getRangeUpperBound() |
| 401 |
{ |
| 402 |
return this.rangeUpperBound; |
| 403 |
} |
| 404 |
|
| 405 |
/** |
| 406 |
* Sets the upper bound for the range that is highlighted on the scale. |
| 407 |
* |
| 408 |
* @param bound the upper bound (<code>null</code> permitted). |
| 409 |
*/ |
| 410 |
public void setRangeUpperBound(final Number bound) |
| 411 |
{ |
| 412 |
this.rangeUpperBound = bound; |
| 413 |
} |
| 414 |
|
| 415 |
/** |
| 416 |
* Returns a flag that controls whether or not a border is drawn around the scale. |
| 417 |
* |
| 418 |
* @return A boolean. |
| 419 |
*/ |
| 420 |
public boolean isDrawBorder() |
| 421 |
{ |
| 422 |
return this.drawBorder; |
| 423 |
} |
| 424 |
|
| 425 |
/** |
| 426 |
* Sets a flag that controls whether or not a border is drawn around the scale. |
| 427 |
* |
| 428 |
* @param flag the flag. |
| 429 |
*/ |
| 430 |
public void setDrawBorder(final boolean flag) |
| 431 |
{ |
| 432 |
this.drawBorder = flag; |
| 433 |
} |
| 434 |
|
| 435 |
/** |
| 436 |
* Returns the flag that controls whether the tick marks are drawn. |
| 437 |
* |
| 438 |
* @return A boolean. |
| 439 |
*/ |
| 440 |
public boolean isDrawTickMarks() |
| 441 |
{ |
| 442 |
return this.drawTickMarks; |
| 443 |
} |
| 444 |
|
| 445 |
/** |
| 446 |
* Sets the flag that controls whether the tick marks are drawn. |
| 447 |
* |
| 448 |
* @param flag a boolean. |
| 449 |
*/ |
| 450 |
public void setDrawTickMarks(final boolean flag) |
| 451 |
{ |
| 452 |
this.drawTickMarks = flag; |
| 453 |
} |
| 454 |
|
| 455 |
/** |
| 456 |
* Returns a flag that controls whether or not scale values are drawn. |
| 457 |
* |
| 458 |
* @return a boolean. |
| 459 |
*/ |
| 460 |
public boolean isDrawScaleValues() |
| 461 |
{ |
| 462 |
return this.drawScaleValues; |
| 463 |
} |
| 464 |
|
| 465 |
/** |
| 466 |
* Sets a flag that controls whether or not scale values are drawn. |
| 467 |
* |
| 468 |
* @param flag the flag. |
| 469 |
*/ |
| 470 |
public void setDrawScaleValues(final boolean flag) |
| 471 |
{ |
| 472 |
this.drawScaleValues = flag; |
| 473 |
} |
| 474 |
|
| 475 |
/** |
| 476 |
* Returns the font used to display the scale values. |
| 477 |
* |
| 478 |
* @return A font (never <code>null</code>). |
| 479 |
*/ |
| 480 |
public Font getScaleValueFont() |
| 481 |
{ |
| 482 |
return this.scaleValueFont; |
| 483 |
} |
| 484 |
|
| 485 |
/** |
| 486 |
* Sets the font used to display the scale values. |
| 487 |
* |
| 488 |
* @param font the font (<code>null</code> not permitted). |
| 489 |
*/ |
| 490 |
public void setScaleValueFont(final Font font) |
| 491 |
{ |
| 492 |
this.scaleValueFont = font; |
| 493 |
} |
| 494 |
|
| 495 |
/** |
| 496 |
* Returns the color used to draw the scale values (if they are visible). |
| 497 |
* |
| 498 |
* @return A paint (never <code>null</code>). |
| 499 |
*/ |
| 500 |
public Paint getScaleValuePaint() |
| 501 |
{ |
| 502 |
return this.scaleValuePaint; |
| 503 |
} |
| 504 |
|
| 505 |
/** |
| 506 |
* Sets the color used to draw the scale values. |
| 507 |
* |
| 508 |
* @param paint the paint (<code>null</code> not permitted). |
| 509 |
*/ |
| 510 |
public void setScaleValuePaint(final Paint paint) |
| 511 |
{ |
| 512 |
if (paint == null) |
| 513 |
{ |
| 514 |
throw new IllegalArgumentException("Null 'paint' argument."); //$NON-NLS-1$ |
| 515 |
} |
| 516 |
this.scaleValuePaint = paint; |
| 517 |
} |
| 518 |
|
| 519 |
/** |
| 520 |
* Returns the shape used to indicate the value of a response. |
| 521 |
* |
| 522 |
* @param index the value index (zero-based). |
| 523 |
* @return The shape. |
| 524 |
*/ |
| 525 |
public Shape getShape(final int index) |
| 526 |
{ |
| 527 |
if (index < 0) |
| 528 |
{ |
| 529 |
throw new IndexOutOfBoundsException(); |
| 530 |
} |
| 531 |
if (index < shapes.size()) |
| 532 |
{ |
| 533 |
return (Shape) this.shapes.get(index); |
| 534 |
} |
| 535 |
return null; |
| 536 |
} |
| 537 |
|
| 538 |
/** |
| 539 |
* Sets the shape used to mark a particular value in the dataset. |
| 540 |
* |
| 541 |
* @param index the value index (zero-based). |
| 542 |
* @param shape the shape (<code>null</code> not permitted). |
| 543 |
*/ |
| 544 |
public void setShape(final int index, final Shape shape) |
| 545 |
{ |
| 546 |
if (index < 0) |
| 547 |
{ |
| 548 |
throw new IndexOutOfBoundsException(); |
| 549 |
} |
| 550 |
if (shapes.size() > index) |
| 551 |
{ |
| 552 |
this.shapes.set(index, shape); |
| 553 |
} |
| 554 |
else |
| 555 |
{ |
| 556 |
while (shapes.size() < index) |
| 557 |
{ |
| 558 |
shapes.ensureCapacity(index); |
| 559 |
shapes.add(null); |
| 560 |
shapes.add(shape); |
| 561 |
} |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
/** |
| 566 |
* Returns a flag that controls whether the shape for a particular value should be filled. |
| 567 |
* |
| 568 |
* @param index the value index (zero-based). |
| 569 |
* @return A boolean. |
| 570 |
*/ |
| 571 |
public boolean isShapeFilled(final int index) |
| 572 |
{ |
| 573 |
if (index < 0) |
| 574 |
{ |
| 575 |
throw new IndexOutOfBoundsException(); |
| 576 |
} |
| 577 |
if (index < fillShapes.size()) |
| 578 |
{ |
| 579 |
final Boolean b = (Boolean) this.fillShapes.get(index); |
| 580 |
if (b != null) |
| 581 |
{ |
| 582 |
return b.booleanValue(); |
| 583 |
} |
| 584 |
} |
| 585 |
return false; |
| 586 |
} |
| 587 |
|
| 588 |
/** |
| 589 |
* Sets the flag that controls whether the shape for a particular value should be filled. |
| 590 |
* |
| 591 |
* @param index the value index (zero-based). |
| 592 |
* @param fill the flag. |
| 593 |
*/ |
| 594 |
public void setShapeFilled(final int index, final boolean fill) |
| 595 |
{ |
| 596 |
//noinspection ConditionalExpression |
| 597 |
this.fillShapes.set(index, fill ? Boolean.TRUE : Boolean.FALSE); |
| 598 |
} |
| 599 |
|
| 600 |
/** |
| 601 |
* Returns the paint used to highlight the range. |
| 602 |
* |
| 603 |
* @return A {@link Paint} object (never <code>null</code>). |
| 604 |
*/ |
| 605 |
public Paint getRangePaint() |
| 606 |
{ |
| 607 |
return this.rangePaint; |
| 608 |
} |
| 609 |
|
| 610 |
/** |
| 611 |
* Sets the paint used to highlight the range (if one is specified). |
| 612 |
* |
| 613 |
* @param paint the paint (<code>null</code> not permitted). |
| 614 |
*/ |
| 615 |
public void setRangePaint(final Paint paint) |
| 616 |
{ |
| 617 |
if (paint == null) |
| 618 |
{ |
| 619 |
throw new IllegalArgumentException("Null 'paint' argument."); //$NON-NLS-1$ |
| 620 |
} |
| 621 |
this.rangePaint = paint; |
| 622 |
} |
| 623 |
|
| 624 |
public Paint getBorderPaint() |
| 625 |
{ |
| 626 |
return borderPaint; |
| 627 |
} |
| 628 |
|
| 629 |
public void setBorderPaint(final Paint borderPaint) |
| 630 |
{ |
| 631 |
if (borderPaint == null) |
| 632 |
{ |
| 633 |
throw new IllegalArgumentException("Null 'paint' argument."); //$NON-NLS-1$ |
| 634 |
} |
| 635 |
this.borderPaint = borderPaint; |
| 636 |
} |
| 637 |
|
| 638 |
/** |
| 639 |
* Returns the default shape, which is used, if a shape for a certain value is not defined. |
| 640 |
* |
| 641 |
* @return the default shape, never null. |
| 642 |
*/ |
| 643 |
public Shape getDefaultShape() |
| 644 |
{ |
| 645 |
return defaultShape; |
| 646 |
} |
| 647 |
|
| 648 |
/** |
| 649 |
* Redefines the default shape. |
| 650 |
* |
| 651 |
* @param defaultShape the default shape |
| 652 |
* @throws NullPointerException if the given shape is null. |
| 653 |
*/ |
| 654 |
public void setDefaultShape(final Shape defaultShape) |
| 655 |
{ |
| 656 |
if (defaultShape == null) |
| 657 |
{ |
| 658 |
throw new NullPointerException("The default shape must not be null."); //$NON-NLS-1$ |
| 659 |
} |
| 660 |
this.defaultShape = defaultShape; |
| 661 |
} |
| 662 |
|
| 663 |
public Paint getTickMarkPaint() |
| 664 |
{ |
| 665 |
return tickMarkPaint; |
| 666 |
} |
| 667 |
|
| 668 |
public void setTickMarkPaint(final Paint tickMarkPaint) |
| 669 |
{ |
| 670 |
if (tickMarkPaint == null) |
| 671 |
{ |
| 672 |
throw new NullPointerException(); |
| 673 |
} |
| 674 |
this.tickMarkPaint = tickMarkPaint; |
| 675 |
} |
| 676 |
|
| 677 |
public Number[] getValues() |
| 678 |
{ |
| 679 |
return (Number[]) values.clone(); |
| 680 |
} |
| 681 |
|
| 682 |
public Paint getFillPaint() |
| 683 |
{ |
| 684 |
return fillPaint; |
| 685 |
} |
| 686 |
|
| 687 |
public void setFillPaint(final Paint fillPaint) |
| 688 |
{ |
| 689 |
if (fillPaint == null) |
| 690 |
{ |
| 691 |
throw new NullPointerException(); |
| 692 |
} |
| 693 |
this.fillPaint = fillPaint; |
| 694 |
} |
| 695 |
|
| 696 |
public Stroke getOutlineStroke() |
| 697 |
{ |
| 698 |
return outlineStroke; |
| 699 |
} |
| 700 |
|
| 701 |
public void setOutlineStroke(final Stroke outlineStroke) |
| 702 |
{ |
| 703 |
if (outlineStroke == null) |
| 704 |
{ |
| 705 |
throw new NullPointerException(); |
| 706 |
} |
| 707 |
this.outlineStroke = outlineStroke; |
| 708 |
} |
| 709 |
|
| 710 |
public double getUpperMargin() |
| 711 |
{ |
| 712 |
return upperMargin; |
| 713 |
} |
| 714 |
|
| 715 |
public void setUpperMargin(final double upperMargin) |
| 716 |
{ |
| 717 |
this.upperMargin = upperMargin; |
| 718 |
} |
| 719 |
|
| 720 |
public double getLowerMargin() |
| 721 |
{ |
| 722 |
return lowerMargin; |
| 723 |
} |
| 724 |
|
| 725 |
public void setLowerMargin(final double lowerMargin) |
| 726 |
{ |
| 727 |
this.lowerMargin = lowerMargin; |
| 728 |
} |
| 729 |
|
| 730 |
/** |
| 731 |
* Draws the survey scale. |
| 732 |
* |
| 733 |
* @param g2 the graphics device. |
| 734 |
* @param area the area. |
| 735 |
*/ |
| 736 |
public void draw(final Graphics2D g2, final Rectangle2D area) |
| 737 |
{ |
| 738 |
|
| 739 |
if (isDrawBorder()) |
| 740 |
{ |
| 741 |
drawBorder(g2, area); |
| 742 |
} |
| 743 |
|
| 744 |
drawRangeArea(area, g2); |
| 745 |
|
| 746 |
// draw tick marks... |
| 747 |
if (isDrawTickMarks()) |
| 748 |
{ |
| 749 |
drawTickMarks(g2, area); |
| 750 |
} |
| 751 |
|
| 752 |
// draw scale values... |
| 753 |
if (isDrawScaleValues()) |
| 754 |
{ |
| 755 |
drawScaleValues(g2, area); |
| 756 |
} |
| 757 |
|
| 758 |
drawValues(g2, area); |
| 759 |
} |
| 760 |
|
| 761 |
protected void drawValues(final Graphics2D g2, |
| 762 |
final Rectangle2D area) |
| 763 |
{ |
| 764 |
|
| 765 |
// draw data values... |
| 766 |
final Number[] values = getValues(); |
| 767 |
if (values.length == 0) |
| 768 |
{ |
| 769 |
return; |
| 770 |
} |
| 771 |
|
| 772 |
final double y = area.getCenterY(); |
| 773 |
|
| 774 |
final Stroke outlineStroke = getOutlineStroke(); |
| 775 |
final Shape defaultShape = getDefaultShape(); |
| 776 |
|
| 777 |
g2.setPaint(getFillPaint()); |
| 778 |
for (int i = 0; i < values.length; i++) |
| 779 |
{ |
| 780 |
final Number n = values[i]; |
| 781 |
if (n == null) |
| 782 |
{ |
| 783 |
continue; |
| 784 |
} |
| 785 |
|
| 786 |
final double v = n.doubleValue(); |
| 787 |
final double x = valueToJava2D(v, area); |
| 788 |
Shape valueShape = getShape(i); |
| 789 |
if (valueShape == null) |
| 790 |
{ |
| 791 |
valueShape = defaultShape; |
| 792 |
} |
| 793 |
if (isShapeFilled(i)) |
| 794 |
{ |
| 795 |
g2.translate(x, y); |
| 796 |
g2.fill(valueShape); |
| 797 |
g2.translate(-x, -y); |
| 798 |
} |
| 799 |
else |
| 800 |
{ |
| 801 |
g2.setStroke(outlineStroke); |
| 802 |
g2.translate(x, y); |
| 803 |
g2.draw(valueShape); |
| 804 |
g2.translate(-x, -y); |
| 805 |
} |
| 806 |
} |
| 807 |
} |
| 808 |
|
| 809 |
protected void drawScaleValues(final Graphics2D g2, final Rectangle2D area) |
| 810 |
{ |
| 811 |
g2.setPaint(getScaleValuePaint()); |
| 812 |
final Font valueFont = getScaleValueFont(); |
| 813 |
if (valueFont != null) |
| 814 |
{ |
| 815 |
g2.setFont(valueFont); |
| 816 |
} |
| 817 |
|
| 818 |
final Font f = g2.getFont(); |
| 819 |
final FontMetrics fm = g2.getFontMetrics(f); |
| 820 |
final FontRenderContext frc = g2.getFontRenderContext(); |
| 821 |
final double y = area.getCenterY(); |
| 822 |
|
| 823 |
final int highest = getHighest(); |
| 824 |
for (int i = getLowest(); i <= highest; i++) |
| 825 |
{ |
| 826 |
final double x = valueToJava2D(i, area); |
| 827 |
final String text = String.valueOf(i); |
| 828 |
|
| 829 |
final float width; |
| 830 |
if (useFontMetricsGetStringBounds) |
| 831 |
{ |
| 832 |
final Rectangle2D bounds = fm.getStringBounds(text, g2); |
| 833 |
// getStringBounds() can return incorrect height for some Unicode |
| 834 |
// characters...see bug parade 6183356, let's replace it with |
| 835 |
// something correct |
| 836 |
width = (float) bounds.getWidth(); |
| 837 |
} |
| 838 |
else |
| 839 |
{ |
| 840 |
width = fm.stringWidth(text); |
| 841 |
} |
| 842 |
|
| 843 |
|
| 844 |
final LineMetrics metrics = f.getLineMetrics(text, frc); |
| 845 |
final float descent = metrics.getDescent(); |
| 846 |
final float leading = metrics.getLeading(); |
| 847 |
final float yAdj = -descent - leading + (float) (metrics.getHeight() / 2.0); |
| 848 |
final float xAdj = -width / 2.0f; |
| 849 |
g2.drawString(text, (float) (x + xAdj), (float) (y + yAdj)); |
| 850 |
} |
| 851 |
} |
| 852 |
|
| 853 |
protected void drawTickMarks(final Graphics2D g2, final Rectangle2D area) |
| 854 |
{ |
| 855 |
g2.setPaint(getTickMarkPaint()); |
| 856 |
g2.setStroke(new BasicStroke(0.1f)); |
| 857 |
|
| 858 |
final int highest = getHighest(); |
| 859 |
for (int i = getLowest(); i <= highest; i++) |
| 860 |
{ |
| 861 |
for (int j = 0; j < 10; j++) |
| 862 |
{ |
| 863 |
final double xx = valueToJava2D(i + j / 10.0, area); |
| 864 |
final Line2D mark = new Line2D.Double(xx, area.getCenterY() - 2.0, xx, |
| 865 |
area.getCenterY() + 2.0); |
| 866 |
g2.draw(mark); |
| 867 |
} |
| 868 |
} |
| 869 |
final double xx = valueToJava2D(highest, area); |
| 870 |
final Line2D mark = new Line2D.Double(xx, area.getCenterY() - 2.0, xx, |
| 871 |
area.getCenterY() + 2.0); |
| 872 |
g2.draw(mark); |
| 873 |
} |
| 874 |
|
| 875 |
protected void drawRangeArea(final Rectangle2D area, final Graphics2D g2) |
| 876 |
{ |
| 877 |
final Number rangeUpperBound = getRangeUpperBound(); |
| 878 |
final Number rangeLowerBound = getRangeLowerBound(); |
| 879 |
if (rangeLowerBound == null || rangeUpperBound == null) |
| 880 |
{ |
| 881 |
return; |
| 882 |
} |
| 883 |
final double x0 = valueToJava2D(rangeLowerBound.doubleValue(), area); |
| 884 |
final double x1 = valueToJava2D(rangeUpperBound.doubleValue(), area); |
| 885 |
final Rectangle2D rangeArea = new Rectangle2D.Double(x0, area.getY(), |
| 886 |
(x1 - x0), area.getHeight()); |
| 887 |
g2.setPaint(getRangePaint()); |
| 888 |
g2.fill(rangeArea); |
| 889 |
} |
| 890 |
|
| 891 |
protected void drawBorder(final Graphics2D g2, final Rectangle2D area) |
| 892 |
{ |
| 893 |
g2.setStroke(getOutlineStroke()); |
| 894 |
g2.setPaint(getBorderPaint()); |
| 895 |
g2.draw(area); |
| 896 |
} |
| 897 |
|
| 898 |
/** |
| 899 |
* Translates a data value to Java2D coordinates. |
| 900 |
* |
| 901 |
* @param value the value. |
| 902 |
* @param area the area. |
| 903 |
* @return The Java2D coordinate. |
| 904 |
*/ |
| 905 |
private double valueToJava2D(final double value, |
| 906 |
final Rectangle2D area) |
| 907 |
{ |
| 908 |
|
| 909 |
final double upperBound = getUpperBound(); |
| 910 |
final double lowerBound = getLowerBound(); |
| 911 |
return area.getMinX() + ((value - lowerBound) / |
| 912 |
(upperBound - lowerBound) * area.getWidth()); |
| 913 |
|
| 914 |
} |
| 915 |
|
| 916 |
private void writeObject(final ObjectOutputStream out) |
| 917 |
throws IOException |
| 918 |
{ |
| 919 |
out.defaultWriteObject(); |
| 920 |
final SerializerHelper helper = SerializerHelper.getInstance(); |
| 921 |
helper.writeObject(scaleValuePaint, out); |
| 922 |
helper.writeObject(rangePaint, out); |
| 923 |
helper.writeObject(fillPaint, out); |
| 924 |
helper.writeObject(outlineStroke, out); |
| 925 |
helper.writeObject(defaultShape, out); |
| 926 |
helper.writeObject(tickMarkPaint, out); |
| 927 |
helper.writeObject(borderPaint, out); |
| 928 |
final int size = shapes.size(); |
| 929 |
out.writeInt(size); |
| 930 |
for (int i = 0; i < size; i++) |
| 931 |
{ |
| 932 |
final Shape s = (Shape) shapes.get(i); |
| 933 |
helper.writeObject(s, out); |
| 934 |
} |
| 935 |
} |
| 936 |
|
| 937 |
private void readObject(final ObjectInputStream in) |
| 938 |
throws IOException, ClassNotFoundException |
| 939 |
{ |
| 940 |
in.defaultReadObject(); |
| 941 |
final SerializerHelper helper = SerializerHelper.getInstance(); |
| 942 |
scaleValuePaint = (Paint) helper.readObject(in); |
| 943 |
rangePaint = (Paint) helper.readObject(in); |
| 944 |
fillPaint = (Paint) helper.readObject(in); |
| 945 |
outlineStroke = (Stroke) helper.readObject(in); |
| 946 |
defaultShape = (Shape) helper.readObject(in); |
| 947 |
tickMarkPaint = (Paint) helper.readObject(in); |
| 948 |
borderPaint = (Paint) helper.readObject(in); |
| 949 |
shapes = new ArrayList(); |
| 950 |
|
| 951 |
final int size = in.readInt(); |
| 952 |
for (int i = 0; i < size; i++) |
| 953 |
{ |
| 954 |
final Shape s = (Shape) helper.readObject(in); |
| 955 |
shapes.add(s); |
| 956 |
} |
| 957 |
|
| 958 |
} |
| 959 |
} |