1:
51:
52: package ;
53:
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63: import ;
64:
65: import ;
66: import ;
67: import ;
68: import ;
69: import ;
70: import ;
71: import ;
72: import ;
73: import ;
74: import ;
75: import ;
76: import ;
77: import ;
78:
79:
82: public class LegendGraphic extends AbstractBlock
83: implements Block, PublicCloneable {
84:
85:
89: private boolean shapeVisible;
90:
91:
95: private transient Shape shape;
96:
97:
100: private RectangleAnchor shapeLocation;
101:
102:
106: private RectangleAnchor shapeAnchor;
107:
108:
109: private boolean shapeFilled;
110:
111:
112: private transient Paint fillPaint;
113:
114:
120: private GradientPaintTransformer fillPaintTransformer;
121:
122:
123: private boolean shapeOutlineVisible;
124:
125:
126: private transient Paint outlinePaint;
127:
128:
129: private transient Stroke outlineStroke;
130:
131:
135: private boolean lineVisible;
136:
137:
138: private transient Shape line;
139:
140:
141: private transient Stroke lineStroke;
142:
143:
144: private transient Paint linePaint;
145:
146:
152: public LegendGraphic(Shape shape, Paint fillPaint) {
153: if (shape == null) {
154: throw new IllegalArgumentException("Null 'shape' argument.");
155: }
156: if (fillPaint == null) {
157: throw new IllegalArgumentException("Null 'fillPaint' argument.");
158: }
159: this.shapeVisible = true;
160: this.shape = shape;
161: this.shapeAnchor = RectangleAnchor.CENTER;
162: this.shapeLocation = RectangleAnchor.CENTER;
163: this.shapeFilled = true;
164: this.fillPaint = fillPaint;
165: this.fillPaintTransformer = new StandardGradientPaintTransformer();
166: setPadding(2.0, 2.0, 2.0, 2.0);
167: }
168:
169:
175: public boolean isShapeVisible() {
176: return this.shapeVisible;
177: }
178:
179:
185: public void setShapeVisible(boolean visible) {
186: this.shapeVisible = visible;
187: }
188:
189:
194: public Shape getShape() {
195: return this.shape;
196: }
197:
198:
203: public void setShape(Shape shape) {
204: this.shape = shape;
205: }
206:
207:
213: public boolean isShapeFilled() {
214: return this.shapeFilled;
215: }
216:
217:
223: public void setShapeFilled(boolean filled) {
224: this.shapeFilled = filled;
225: }
226:
227:
232: public Paint getFillPaint() {
233: return this.fillPaint;
234: }
235:
236:
241: public void setFillPaint(Paint paint) {
242: this.fillPaint = paint;
243: }
244:
245:
253: public GradientPaintTransformer getFillPaintTransformer() {
254: return this.fillPaintTransformer;
255: }
256:
257:
265: public void setFillPaintTransformer(GradientPaintTransformer transformer) {
266: if (transformer == null) {
267: throw new IllegalArgumentException("Null 'transformer' argument.");
268: }
269: this.fillPaintTransformer = transformer;
270: }
271:
272:
277: public boolean isShapeOutlineVisible() {
278: return this.shapeOutlineVisible;
279: }
280:
281:
287: public void setShapeOutlineVisible(boolean visible) {
288: this.shapeOutlineVisible = visible;
289: }
290:
291:
296: public Paint getOutlinePaint() {
297: return this.outlinePaint;
298: }
299:
300:
305: public void setOutlinePaint(Paint paint) {
306: this.outlinePaint = paint;
307: }
308:
309:
314: public Stroke getOutlineStroke() {
315: return this.outlineStroke;
316: }
317:
318:
323: public void setOutlineStroke(Stroke stroke) {
324: this.outlineStroke = stroke;
325: }
326:
327:
332: public RectangleAnchor getShapeAnchor() {
333: return this.shapeAnchor;
334: }
335:
336:
342: public void setShapeAnchor(RectangleAnchor anchor) {
343: if (anchor == null) {
344: throw new IllegalArgumentException("Null 'anchor' argument.");
345: }
346: this.shapeAnchor = anchor;
347: }
348:
349:
354: public RectangleAnchor getShapeLocation() {
355: return this.shapeLocation;
356: }
357:
358:
364: public void setShapeLocation(RectangleAnchor location) {
365: if (location == null) {
366: throw new IllegalArgumentException("Null 'location' argument.");
367: }
368: this.shapeLocation = location;
369: }
370:
371:
376: public boolean isLineVisible() {
377: return this.lineVisible;
378: }
379:
380:
385: public void setLineVisible(boolean visible) {
386: this.lineVisible = visible;
387: }
388:
389:
394: public Shape getLine() {
395: return this.line;
396: }
397:
398:
404: public void setLine(Shape line) {
405: this.line = line;
406: }
407:
408:
413: public Paint getLinePaint() {
414: return this.linePaint;
415: }
416:
417:
422: public void setLinePaint(Paint paint) {
423: this.linePaint = paint;
424: }
425:
426:
431: public Stroke getLineStroke() {
432: return this.lineStroke;
433: }
434:
435:
440: public void setLineStroke(Stroke stroke) {
441: this.lineStroke = stroke;
442: }
443:
444:
453: public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) {
454: RectangleConstraint contentConstraint = toContentConstraint(constraint);
455: LengthConstraintType w = contentConstraint.getWidthConstraintType();
456: LengthConstraintType h = contentConstraint.getHeightConstraintType();
457: Size2D contentSize = null;
458: if (w == LengthConstraintType.NONE) {
459: if (h == LengthConstraintType.NONE) {
460: contentSize = arrangeNN(g2);
461: }
462: else if (h == LengthConstraintType.RANGE) {
463: throw new RuntimeException("Not yet implemented.");
464: }
465: else if (h == LengthConstraintType.FIXED) {
466: throw new RuntimeException("Not yet implemented.");
467: }
468: }
469: else if (w == LengthConstraintType.RANGE) {
470: if (h == LengthConstraintType.NONE) {
471: throw new RuntimeException("Not yet implemented.");
472: }
473: else if (h == LengthConstraintType.RANGE) {
474: throw new RuntimeException("Not yet implemented.");
475: }
476: else if (h == LengthConstraintType.FIXED) {
477: throw new RuntimeException("Not yet implemented.");
478: }
479: }
480: else if (w == LengthConstraintType.FIXED) {
481: if (h == LengthConstraintType.NONE) {
482: throw new RuntimeException("Not yet implemented.");
483: }
484: else if (h == LengthConstraintType.RANGE) {
485: throw new RuntimeException("Not yet implemented.");
486: }
487: else if (h == LengthConstraintType.FIXED) {
488: contentSize = new Size2D(
489: contentConstraint.getWidth(),
490: contentConstraint.getHeight()
491: );
492: }
493: }
494: return new Size2D(
495: calculateTotalWidth(contentSize.getWidth()),
496: calculateTotalHeight(contentSize.getHeight())
497: );
498: }
499:
500:
509: protected Size2D arrangeNN(Graphics2D g2) {
510: Rectangle2D contentSize = new Rectangle2D.Double();
511: if (this.line != null) {
512: contentSize.setRect(this.line.getBounds2D());
513: }
514: if (this.shape != null) {
515: contentSize = contentSize.createUnion(this.shape.getBounds2D());
516: }
517: return new Size2D(contentSize.getWidth(), contentSize.getHeight());
518: }
519:
520:
526: public void draw(Graphics2D g2, Rectangle2D area) {
527:
528: area = trimMargin(area);
529: drawBorder(g2, area);
530: area = trimBorder(area);
531: area = trimPadding(area);
532:
533: if (this.lineVisible) {
534: Point2D location = RectangleAnchor.coordinates(
535: area, this.shapeLocation
536: );
537: Shape aLine = ShapeUtilities.createTranslatedShape(
538: getLine(), this.shapeAnchor, location.getX(), location.getY()
539: );
540: g2.setPaint(this.linePaint);
541: g2.setStroke(this.lineStroke);
542: g2.draw(aLine);
543: }
544:
545: if (this.shapeVisible) {
546: Point2D location = RectangleAnchor.coordinates(
547: area, this.shapeLocation
548: );
549:
550: Shape s = ShapeUtilities.createTranslatedShape(
551: this.shape, this.shapeAnchor, location.getX(), location.getY()
552: );
553: if (this.shapeFilled) {
554: Paint p = this.fillPaint;
555: if (p instanceof GradientPaint) {
556: GradientPaint gp = (GradientPaint) this.fillPaint;
557: p = this.fillPaintTransformer.transform(gp, s);
558: }
559: g2.setPaint(p);
560: g2.fill(s);
561: }
562: if (this.shapeOutlineVisible) {
563: g2.setPaint(this.outlinePaint);
564: g2.setStroke(this.outlineStroke);
565: g2.draw(s);
566: }
567: }
568:
569: }
570:
571:
580: public Object draw(Graphics2D g2, Rectangle2D area, Object params) {
581: draw(g2, area);
582: return null;
583: }
584:
585:
593: public boolean equals(Object obj) {
594: if (!(obj instanceof LegendGraphic)) {
595: return false;
596: }
597: LegendGraphic that = (LegendGraphic) obj;
598: if (this.shapeVisible != that.shapeVisible) {
599: return false;
600: }
601: if (!ShapeUtilities.equal(this.shape, that.shape)) {
602: return false;
603: }
604: if (this.shapeFilled != that.shapeFilled) {
605: return false;
606: }
607: if (!PaintUtilities.equal(this.fillPaint, that.fillPaint)) {
608: return false;
609: }
610: if (!ObjectUtilities.equal(this.fillPaintTransformer,
611: that.fillPaintTransformer)) {
612: return false;
613: }
614: if (this.shapeOutlineVisible != that.shapeOutlineVisible) {
615: return false;
616: }
617: if (!PaintUtilities.equal(this.outlinePaint, that.outlinePaint)) {
618: return false;
619: }
620: if (!ObjectUtilities.equal(this.outlineStroke, that.outlineStroke)) {
621: return false;
622: }
623: if (this.shapeAnchor != that.shapeAnchor) {
624: return false;
625: }
626: if (this.shapeLocation != that.shapeLocation) {
627: return false;
628: }
629: if (this.lineVisible != that.lineVisible) {
630: return false;
631: }
632: if (!ShapeUtilities.equal(this.line, that.line)) {
633: return false;
634: }
635: if (!PaintUtilities.equal(this.linePaint, that.linePaint)) {
636: return false;
637: }
638: if (!ObjectUtilities.equal(this.lineStroke, that.lineStroke)) {
639: return false;
640: }
641: return super.equals(obj);
642: }
643:
644:
649: public int hashCode() {
650: int result = 193;
651: result = 37 * result + ObjectUtilities.hashCode(this.fillPaint);
652:
653: return result;
654: }
655:
656:
663: public Object clone() throws CloneNotSupportedException {
664: LegendGraphic clone = (LegendGraphic) super.clone();
665: clone.shape = ShapeUtilities.clone(this.shape);
666: clone.line = ShapeUtilities.clone(this.line);
667: return clone;
668: }
669:
670:
677: private void writeObject(ObjectOutputStream stream) throws IOException {
678: stream.defaultWriteObject();
679: SerialUtilities.writeShape(this.shape, stream);
680: SerialUtilities.writePaint(this.fillPaint, stream);
681: SerialUtilities.writePaint(this.outlinePaint, stream);
682: SerialUtilities.writeStroke(this.outlineStroke, stream);
683: SerialUtilities.writeShape(this.line, stream);
684: SerialUtilities.writePaint(this.linePaint, stream);
685: SerialUtilities.writeStroke(this.lineStroke, stream);
686: }
687:
688:
696: private void readObject(ObjectInputStream stream)
697: throws IOException, ClassNotFoundException
698: {
699: stream.defaultReadObject();
700: this.shape = SerialUtilities.readShape(stream);
701: this.fillPaint = SerialUtilities.readPaint(stream);
702: this.outlinePaint = SerialUtilities.readPaint(stream);
703: this.outlineStroke = SerialUtilities.readStroke(stream);
704: this.line = SerialUtilities.readShape(stream);
705: this.linePaint = SerialUtilities.readPaint(stream);
706: this.lineStroke = SerialUtilities.readStroke(stream);
707: }
708:
709: }