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:
77: public class DialValueIndicator extends AbstractDialLayer implements DialLayer,
78: Cloneable, PublicCloneable, Serializable {
79:
80:
81: static final long serialVersionUID = 803094354130942585L;
82:
83:
84: private int datasetIndex;
85:
86:
87: private double angle;
88:
89:
90: private double radius;
91:
92:
93: private RectangleAnchor frameAnchor;
94:
95:
96: private Number templateValue;
97:
98:
99: private NumberFormat formatter;
100:
101:
102: private Font font;
103:
104:
105: private transient Paint paint;
106:
107:
108: private transient Paint backgroundPaint;
109:
110:
111: private transient Stroke outlineStroke;
112:
113:
114: private transient Paint outlinePaint;
115:
116:
117: private RectangleInsets insets;
118:
119:
120: private RectangleAnchor valueAnchor;
121:
122:
123: private TextAnchor textAnchor;
124:
125:
128: public DialValueIndicator() {
129: this(0);
130: }
131:
132:
137: public DialValueIndicator(int datasetIndex) {
138: this.datasetIndex = datasetIndex;
139: this.angle = -90.0;
140: this.radius = 0.3;
141: this.frameAnchor = RectangleAnchor.CENTER;
142: this.templateValue = new Double(100.0);
143: this.formatter = new DecimalFormat("0.0");
144: this.font = new Font("Dialog", Font.BOLD, 14);
145: this.paint = Color.black;
146: this.backgroundPaint = Color.white;
147: this.outlineStroke = new BasicStroke(1.0f);
148: this.outlinePaint = Color.blue;
149: this.insets = new RectangleInsets(4, 4, 4, 4);
150: this.valueAnchor = RectangleAnchor.RIGHT;
151: this.textAnchor = TextAnchor.CENTER_RIGHT;
152: }
153:
154:
162: public int getDatasetIndex() {
163: return this.datasetIndex;
164: }
165:
166:
174: public void setDatasetIndex(int index) {
175: this.datasetIndex = index;
176: notifyListeners(new DialLayerChangeEvent(this));
177: }
178:
179:
187: public double getAngle() {
188: return this.angle;
189: }
190:
191:
199: public void setAngle(double angle) {
200: this.angle = angle;
201: notifyListeners(new DialLayerChangeEvent(this));
202: }
203:
204:
211: public double getRadius() {
212: return this.radius;
213: }
214:
215:
223: public void setRadius(double radius) {
224: this.radius = radius;
225: notifyListeners(new DialLayerChangeEvent(this));
226: }
227:
228:
235: public RectangleAnchor getFrameAnchor() {
236: return this.frameAnchor;
237: }
238:
239:
247: public void setFrameAnchor(RectangleAnchor anchor) {
248: if (anchor == null) {
249: throw new IllegalArgumentException("Null 'anchor' argument.");
250: }
251: this.frameAnchor = anchor;
252: notifyListeners(new DialLayerChangeEvent(this));
253: }
254:
255:
262: public Number getTemplateValue() {
263: return this.templateValue;
264: }
265:
266:
274: public void setTemplateValue(Number value) {
275: if (value == null) {
276: throw new IllegalArgumentException("Null 'value' argument.");
277: }
278: this.templateValue = value;
279: notifyListeners(new DialLayerChangeEvent(this));
280: }
281:
282:
289: public NumberFormat getNumberFormat() {
290: return this.formatter;
291: }
292:
293:
301: public void setNumberFormat(NumberFormat formatter) {
302: if (formatter == null) {
303: throw new IllegalArgumentException("Null 'formatter' argument.");
304: }
305: this.formatter = formatter;
306: notifyListeners(new DialLayerChangeEvent(this));
307: }
308:
309:
316: public Font getFont() {
317: return this.font;
318: }
319:
320:
326: public void setFont(Font font) {
327: if (font == null) {
328: throw new IllegalArgumentException("Null 'font' argument.");
329: }
330: this.font = font;
331: notifyListeners(new DialLayerChangeEvent(this));
332: }
333:
334:
341: public Paint getPaint() {
342: return this.paint;
343: }
344:
345:
353: public void setPaint(Paint paint) {
354: if (paint == null) {
355: throw new IllegalArgumentException("Null 'paint' argument.");
356: }
357: this.paint = paint;
358: notifyListeners(new DialLayerChangeEvent(this));
359: }
360:
361:
368: public Paint getBackgroundPaint() {
369: return this.backgroundPaint;
370: }
371:
372:
380: public void setBackgroundPaint(Paint paint) {
381: if (paint == null) {
382: throw new IllegalArgumentException("Null 'paint' argument.");
383: }
384: this.backgroundPaint = paint;
385: notifyListeners(new DialLayerChangeEvent(this));
386: }
387:
388:
395: public Stroke getOutlineStroke() {
396: return this.outlineStroke;
397: }
398:
399:
407: public void setOutlineStroke(Stroke stroke) {
408: if (stroke == null) {
409: throw new IllegalArgumentException("Null 'stroke' argument.");
410: }
411: this.outlineStroke = stroke;
412: notifyListeners(new DialLayerChangeEvent(this));
413: }
414:
415:
422: public Paint getOutlinePaint() {
423: return this.outlinePaint;
424: }
425:
426:
434: public void setOutlinePaint(Paint paint) {
435: if (paint == null) {
436: throw new IllegalArgumentException("Null 'paint' argument.");
437: }
438: this.outlinePaint = paint;
439: notifyListeners(new DialLayerChangeEvent(this));
440: }
441:
442:
449: public RectangleInsets getInsets() {
450: return this.insets;
451: }
452:
453:
461: public void setInsets(RectangleInsets insets) {
462: if (insets == null) {
463: throw new IllegalArgumentException("Null 'insets' argument.");
464: }
465: this.insets = insets;
466: notifyListeners(new DialLayerChangeEvent(this));
467: }
468:
469:
476: public RectangleAnchor getValueAnchor() {
477: return this.valueAnchor;
478: }
479:
480:
488: public void setValueAnchor(RectangleAnchor anchor) {
489: if (anchor == null) {
490: throw new IllegalArgumentException("Null 'anchor' argument.");
491: }
492: this.valueAnchor = anchor;
493: notifyListeners(new DialLayerChangeEvent(this));
494: }
495:
496:
503: public TextAnchor getTextAnchor() {
504: return this.textAnchor;
505: }
506:
507:
515: public void setTextAnchor(TextAnchor anchor) {
516: if (anchor == null) {
517: throw new IllegalArgumentException("Null 'anchor' argument.");
518: }
519: this.textAnchor = anchor;
520: notifyListeners(new DialLayerChangeEvent(this));
521: }
522:
523:
529: public boolean isClippedToWindow() {
530: return true;
531: }
532:
533:
543: public void draw(Graphics2D g2, DialPlot plot, Rectangle2D frame,
544: Rectangle2D view) {
545:
546:
547: Rectangle2D f = DialPlot.rectangleByRadius(frame, this.radius,
548: this.radius);
549: Arc2D arc = new Arc2D.Double(f, this.angle, 0.0, Arc2D.OPEN);
550: Point2D pt = arc.getStartPoint();
551:
552:
553: FontMetrics fm = g2.getFontMetrics(this.font);
554: String s = this.formatter.format(this.templateValue);
555: Rectangle2D tb = TextUtilities.getTextBounds(s, g2, fm);
556:
557:
558: Rectangle2D bounds = RectangleAnchor.createRectangle(new Size2D(
559: tb.getWidth(), tb.getHeight()), pt.getX(), pt.getY(),
560: this.frameAnchor);
561:
562:
563: Rectangle2D fb = this.insets.createOutsetRectangle(bounds);
564:
565:
566: g2.setPaint(this.backgroundPaint);
567: g2.fill(fb);
568:
569:
570: g2.setStroke(this.outlineStroke);
571: g2.setPaint(this.outlinePaint);
572: g2.draw(fb);
573:
574:
575:
576: double value = plot.getValue(this.datasetIndex);
577: String valueStr = this.formatter.format(value);
578: Point2D pt2 = RectangleAnchor.coordinates(bounds, this.valueAnchor);
579: g2.setPaint(this.paint);
580: g2.setFont(this.font);
581: TextUtilities.drawAlignedString(valueStr, g2, (float) pt2.getX(),
582: (float) pt2.getY(), this.textAnchor);
583:
584: }
585:
586:
593: public boolean equals(Object obj) {
594: if (obj == this) {
595: return true;
596: }
597: if (!(obj instanceof DialValueIndicator)) {
598: return false;
599: }
600: DialValueIndicator that = (DialValueIndicator) obj;
601: if (this.datasetIndex != that.datasetIndex) {
602: return false;
603: }
604: if (this.angle != that.angle) {
605: return false;
606: }
607: if (this.radius != that.radius) {
608: return false;
609: }
610: if (!this.frameAnchor.equals(that.frameAnchor)) {
611: return false;
612: }
613: if (!this.templateValue.equals(that.templateValue)) {
614: return false;
615: }
616: if (!this.font.equals(that.font)) {
617: return false;
618: }
619: if (!PaintUtilities.equal(this.paint, that.paint)) {
620: return false;
621: }
622: if (!PaintUtilities.equal(this.backgroundPaint, that.backgroundPaint)) {
623: return false;
624: }
625: if (!this.outlineStroke.equals(that.outlineStroke)) {
626: return false;
627: }
628: if (!PaintUtilities.equal(this.outlinePaint, that.outlinePaint)) {
629: return false;
630: }
631: if (!this.insets.equals(that.insets)) {
632: return false;
633: }
634: if (!this.valueAnchor.equals(that.valueAnchor)) {
635: return false;
636: }
637: if (!this.textAnchor.equals(that.textAnchor)) {
638: return false;
639: }
640:
641: return super.equals(obj);
642: }
643:
644:
649: public int hashCode() {
650: int result = 193;
651: result = 37 * result + HashUtilities.hashCodeForPaint(this.paint);
652: result = 37 * result + HashUtilities.hashCodeForPaint(
653: this.backgroundPaint);
654: result = 37 * result + HashUtilities.hashCodeForPaint(
655: this.outlinePaint);
656: result = 37 * result + this.outlineStroke.hashCode();
657: return result;
658: }
659:
660:
668: public Object clone() throws CloneNotSupportedException {
669: return super.clone();
670: }
671:
672:
679: private void writeObject(ObjectOutputStream stream) throws IOException {
680: stream.defaultWriteObject();
681: SerialUtilities.writePaint(this.paint, stream);
682: SerialUtilities.writePaint(this.backgroundPaint, stream);
683: SerialUtilities.writePaint(this.outlinePaint, stream);
684: SerialUtilities.writeStroke(this.outlineStroke, stream);
685: }
686:
687:
695: private void readObject(ObjectInputStream stream)
696: throws IOException, ClassNotFoundException {
697: stream.defaultReadObject();
698: this.paint = SerialUtilities.readPaint(stream);
699: this.backgroundPaint = SerialUtilities.readPaint(stream);
700: this.outlinePaint = SerialUtilities.readPaint(stream);
701: this.outlineStroke = SerialUtilities.readStroke(stream);
702: }
703:
704: }