1:
42:
43: package ;
44:
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61:
62: import ;
63: import ;
64: import ;
65: import ;
66: import ;
67: import ;
68: import ;
69: import ;
70: import ;
71:
72:
75: public class DialValueIndicator extends AbstractDialLayer implements DialLayer,
76: Cloneable, PublicCloneable, Serializable {
77:
78:
79: static final long serialVersionUID = 803094354130942585L;
80:
81:
82: private int datasetIndex;
83:
84:
85: private double angle;
86:
87:
88: private double radius;
89:
90:
91: private RectangleAnchor frameAnchor;
92:
93:
94: private Number templateValue;
95:
96:
97: private NumberFormat formatter;
98:
99:
100: private Font font;
101:
102:
103: private transient Paint paint;
104:
105:
106: private transient Paint backgroundPaint;
107:
108:
109: private transient Stroke outlineStroke;
110:
111:
112: private transient Paint outlinePaint;
113:
114:
115: private RectangleInsets insets;
116:
117:
118: private RectangleAnchor valueAnchor;
119:
120:
121: private TextAnchor textAnchor;
122:
123:
126: public DialValueIndicator() {
127: this(0);
128: }
129:
130:
135: public DialValueIndicator(int datasetIndex) {
136: this.datasetIndex = datasetIndex;
137: this.angle = -90.0;
138: this.radius = 0.3;
139: this.frameAnchor = RectangleAnchor.CENTER;
140: this.templateValue = new Double(100.0);
141: this.formatter = new DecimalFormat("0.0");
142: this.font = new Font("Dialog", Font.BOLD, 14);
143: this.paint = Color.black;
144: this.backgroundPaint = Color.white;
145: this.outlineStroke = new BasicStroke(1.0f);
146: this.outlinePaint = Color.blue;
147: this.insets = new RectangleInsets(4, 4, 4, 4);
148: this.valueAnchor = RectangleAnchor.RIGHT;
149: this.textAnchor = TextAnchor.CENTER_RIGHT;
150: }
151:
152:
160: public int getDatasetIndex() {
161: return this.datasetIndex;
162: }
163:
164:
172: public void setDatasetIndex(int index) {
173: this.datasetIndex = index;
174: notifyListeners(new DialLayerChangeEvent(this));
175: }
176:
177:
185: public double getAngle() {
186: return this.angle;
187: }
188:
189:
197: public void setAngle(double angle) {
198: this.angle = angle;
199: notifyListeners(new DialLayerChangeEvent(this));
200: }
201:
202:
209: public double getRadius() {
210: return this.radius;
211: }
212:
213:
221: public void setRadius(double radius) {
222: this.radius = radius;
223: notifyListeners(new DialLayerChangeEvent(this));
224: }
225:
226:
233: public RectangleAnchor getFrameAnchor() {
234: return this.frameAnchor;
235: }
236:
237:
245: public void setFrameAnchor(RectangleAnchor anchor) {
246: if (anchor == null) {
247: throw new IllegalArgumentException("Null 'anchor' argument.");
248: }
249: this.frameAnchor = anchor;
250: notifyListeners(new DialLayerChangeEvent(this));
251: }
252:
253:
260: public Number getTemplateValue() {
261: return this.templateValue;
262: }
263:
264:
272: public void setTemplateValue(Number value) {
273: if (value == null) {
274: throw new IllegalArgumentException("Null 'value' argument.");
275: }
276: this.templateValue = value;
277: notifyListeners(new DialLayerChangeEvent(this));
278: }
279:
280:
287: public NumberFormat getNumberFormat() {
288: return this.formatter;
289: }
290:
291:
299: public void setNumberFormat(NumberFormat formatter) {
300: if (formatter == null) {
301: throw new IllegalArgumentException("Null 'formatter' argument.");
302: }
303: this.formatter = formatter;
304: notifyListeners(new DialLayerChangeEvent(this));
305: }
306:
307:
314: public Font getFont() {
315: return this.font;
316: }
317:
318:
324: public void setFont(Font font) {
325: if (font == null) {
326: throw new IllegalArgumentException("Null 'font' argument.");
327: }
328: this.font = font;
329: notifyListeners(new DialLayerChangeEvent(this));
330: }
331:
332:
339: public Paint getPaint() {
340: return this.paint;
341: }
342:
343:
351: public void setPaint(Paint paint) {
352: if (paint == null) {
353: throw new IllegalArgumentException("Null 'paint' argument.");
354: }
355: this.paint = paint;
356: notifyListeners(new DialLayerChangeEvent(this));
357: }
358:
359:
366: public Paint getBackgroundPaint() {
367: return this.backgroundPaint;
368: }
369:
370:
378: public void setBackgroundPaint(Paint paint) {
379: if (paint == null) {
380: throw new IllegalArgumentException("Null 'paint' argument.");
381: }
382: this.backgroundPaint = paint;
383: notifyListeners(new DialLayerChangeEvent(this));
384: }
385:
386:
393: public Stroke getOutlineStroke() {
394: return this.outlineStroke;
395: }
396:
397:
405: public void setOutlineStroke(Stroke stroke) {
406: if (stroke == null) {
407: throw new IllegalArgumentException("Null 'stroke' argument.");
408: }
409: this.outlineStroke = stroke;
410: notifyListeners(new DialLayerChangeEvent(this));
411: }
412:
413:
420: public Paint getOutlinePaint() {
421: return this.outlinePaint;
422: }
423:
424:
432: public void setOutlinePaint(Paint paint) {
433: if (paint == null) {
434: throw new IllegalArgumentException("Null 'paint' argument.");
435: }
436: this.outlinePaint = paint;
437: notifyListeners(new DialLayerChangeEvent(this));
438: }
439:
440:
447: public RectangleInsets getInsets() {
448: return this.insets;
449: }
450:
451:
459: public void setInsets(RectangleInsets insets) {
460: if (insets == null) {
461: throw new IllegalArgumentException("Null 'insets' argument.");
462: }
463: this.insets = insets;
464: notifyListeners(new DialLayerChangeEvent(this));
465: }
466:
467:
474: public RectangleAnchor getValueAnchor() {
475: return this.valueAnchor;
476: }
477:
478:
486: public void setValueAnchor(RectangleAnchor anchor) {
487: if (anchor == null) {
488: throw new IllegalArgumentException("Null 'anchor' argument.");
489: }
490: this.valueAnchor = anchor;
491: notifyListeners(new DialLayerChangeEvent(this));
492: }
493:
494:
501: public TextAnchor getTextAnchor() {
502: return this.textAnchor;
503: }
504:
505:
513: public void setTextAnchor(TextAnchor anchor) {
514: if (anchor == null) {
515: throw new IllegalArgumentException("Null 'anchor' argument.");
516: }
517: this.textAnchor = anchor;
518: notifyListeners(new DialLayerChangeEvent(this));
519: }
520:
521:
527: public boolean isClippedToWindow() {
528: return true;
529: }
530:
531:
541: public void draw(Graphics2D g2, DialPlot plot, Rectangle2D frame,
542: Rectangle2D view) {
543:
544:
545: Rectangle2D f = DialPlot.rectangleByRadius(frame, this.radius,
546: this.radius);
547: Arc2D arc = new Arc2D.Double(f, this.angle, 0.0, Arc2D.OPEN);
548: Point2D pt = arc.getStartPoint();
549:
550:
551: FontMetrics fm = g2.getFontMetrics(this.font);
552: String s = this.formatter.format(this.templateValue);
553: Rectangle2D tb = TextUtilities.getTextBounds(s, g2, fm);
554:
555:
556: Rectangle2D bounds = RectangleAnchor.createRectangle(new Size2D(
557: tb.getWidth(), tb.getHeight()), pt.getX(), pt.getY(),
558: this.frameAnchor);
559:
560:
561: Rectangle2D fb = this.insets.createOutsetRectangle(bounds);
562:
563:
564: g2.setPaint(this.backgroundPaint);
565: g2.fill(fb);
566:
567:
568: g2.setStroke(this.outlineStroke);
569: g2.setPaint(this.outlinePaint);
570: g2.draw(fb);
571:
572:
573:
574: double value = plot.getValue(this.datasetIndex);
575: String valueStr = this.formatter.format(value);
576: Point2D pt2 = RectangleAnchor.coordinates(bounds, this.valueAnchor);
577: g2.setPaint(this.paint);
578: g2.setFont(this.font);
579: TextUtilities.drawAlignedString(valueStr, g2, (float) pt2.getX(),
580: (float) pt2.getY(), this.textAnchor);
581:
582: }
583:
584:
591: public boolean equals(Object obj) {
592: if (obj == this) {
593: return true;
594: }
595: if (!(obj instanceof DialValueIndicator)) {
596: return false;
597: }
598: DialValueIndicator that = (DialValueIndicator) obj;
599: if (this.datasetIndex != that.datasetIndex) {
600: return false;
601: }
602: if (this.angle != that.angle) {
603: return false;
604: }
605: if (this.radius != that.radius) {
606: return false;
607: }
608: if (!this.frameAnchor.equals(that.frameAnchor)) {
609: return false;
610: }
611: if (!this.templateValue.equals(that.templateValue)) {
612: return false;
613: }
614: if (!this.font.equals(that.font)) {
615: return false;
616: }
617: if (!PaintUtilities.equal(this.paint, that.paint)) {
618: return false;
619: }
620: if (!PaintUtilities.equal(this.backgroundPaint, that.backgroundPaint)) {
621: return false;
622: }
623: if (!this.outlineStroke.equals(that.outlineStroke)) {
624: return false;
625: }
626: if (!PaintUtilities.equal(this.outlinePaint, that.outlinePaint)) {
627: return false;
628: }
629: if (!this.insets.equals(that.insets)) {
630: return false;
631: }
632: if (!this.valueAnchor.equals(that.valueAnchor)) {
633: return false;
634: }
635: if (!this.textAnchor.equals(that.textAnchor)) {
636: return false;
637: }
638:
639: return super.equals(obj);
640: }
641:
642:
647: public int hashCode() {
648: int result = 193;
649: result = 37 * result + HashUtilities.hashCodeForPaint(this.paint);
650: result = 37 * result + HashUtilities.hashCodeForPaint(
651: this.backgroundPaint);
652: result = 37 * result + HashUtilities.hashCodeForPaint(
653: this.outlinePaint);
654: result = 37 * result + this.outlineStroke.hashCode();
655: return result;
656: }
657:
658:
666: public Object clone() throws CloneNotSupportedException {
667: return super.clone();
668: }
669:
670:
677: private void writeObject(ObjectOutputStream stream) throws IOException {
678: stream.defaultWriteObject();
679: SerialUtilities.writePaint(this.paint, stream);
680: SerialUtilities.writePaint(this.backgroundPaint, stream);
681: SerialUtilities.writePaint(this.outlinePaint, stream);
682: SerialUtilities.writeStroke(this.outlineStroke, stream);
683: }
684:
685:
693: private void readObject(ObjectInputStream stream)
694: throws IOException, ClassNotFoundException {
695: stream.defaultReadObject();
696: this.paint = SerialUtilities.readPaint(stream);
697: this.backgroundPaint = SerialUtilities.readPaint(stream);
698: this.outlinePaint = SerialUtilities.readPaint(stream);
699: this.outlineStroke = SerialUtilities.readStroke(stream);
700: }
701:
702: }