1:
50:
51: package ;
52:
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63:
64: import ;
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:
78:
81: public class LegendGraphic extends AbstractBlock
82: implements Block, PublicCloneable {
83:
84:
85: static final long serialVersionUID = -1338791523854985009L;
86:
87:
91: private boolean shapeVisible;
92:
93:
97: private transient Shape shape;
98:
99:
102: private RectangleAnchor shapeLocation;
103:
104:
108: private RectangleAnchor shapeAnchor;
109:
110:
111: private boolean shapeFilled;
112:
113:
114: private transient Paint fillPaint;
115:
116:
122: private GradientPaintTransformer fillPaintTransformer;
123:
124:
125: private boolean shapeOutlineVisible;
126:
127:
128: private transient Paint outlinePaint;
129:
130:
131: private transient Stroke outlineStroke;
132:
133:
137: private boolean lineVisible;
138:
139:
140: private transient Shape line;
141:
142:
143: private transient Stroke lineStroke;
144:
145:
146: private transient Paint linePaint;
147:
148:
154: public LegendGraphic(Shape shape, Paint fillPaint) {
155: if (shape == null) {
156: throw new IllegalArgumentException("Null 'shape' argument.");
157: }
158: if (fillPaint == null) {
159: throw new IllegalArgumentException("Null 'fillPaint' argument.");
160: }
161: this.shapeVisible = true;
162: this.shape = shape;
163: this.shapeAnchor = RectangleAnchor.CENTER;
164: this.shapeLocation = RectangleAnchor.CENTER;
165: this.shapeFilled = true;
166: this.fillPaint = fillPaint;
167: this.fillPaintTransformer = new StandardGradientPaintTransformer();
168: setPadding(2.0, 2.0, 2.0, 2.0);
169: }
170:
171:
179: public boolean isShapeVisible() {
180: return this.shapeVisible;
181: }
182:
183:
191: public void setShapeVisible(boolean visible) {
192: this.shapeVisible = visible;
193: }
194:
195:
202: public Shape getShape() {
203: return this.shape;
204: }
205:
206:
213: public void setShape(Shape shape) {
214: this.shape = shape;
215: }
216:
217:
225: public boolean isShapeFilled() {
226: return this.shapeFilled;
227: }
228:
229:
237: public void setShapeFilled(boolean filled) {
238: this.shapeFilled = filled;
239: }
240:
241:
248: public Paint getFillPaint() {
249: return this.fillPaint;
250: }
251:
252:
259: public void setFillPaint(Paint paint) {
260: this.fillPaint = paint;
261: }
262:
263:
273: public GradientPaintTransformer getFillPaintTransformer() {
274: return this.fillPaintTransformer;
275: }
276:
277:
287: public void setFillPaintTransformer(GradientPaintTransformer transformer) {
288: if (transformer == null) {
289: throw new IllegalArgumentException("Null 'transformer' argument.");
290: }
291: this.fillPaintTransformer = transformer;
292: }
293:
294:
301: public boolean isShapeOutlineVisible() {
302: return this.shapeOutlineVisible;
303: }
304:
305:
313: public void setShapeOutlineVisible(boolean visible) {
314: this.shapeOutlineVisible = visible;
315: }
316:
317:
324: public Paint getOutlinePaint() {
325: return this.outlinePaint;
326: }
327:
328:
335: public void setOutlinePaint(Paint paint) {
336: this.outlinePaint = paint;
337: }
338:
339:
346: public Stroke getOutlineStroke() {
347: return this.outlineStroke;
348: }
349:
350:
357: public void setOutlineStroke(Stroke stroke) {
358: this.outlineStroke = stroke;
359: }
360:
361:
368: public RectangleAnchor getShapeAnchor() {
369: return this.shapeAnchor;
370: }
371:
372:
380: public void setShapeAnchor(RectangleAnchor anchor) {
381: if (anchor == null) {
382: throw new IllegalArgumentException("Null 'anchor' argument.");
383: }
384: this.shapeAnchor = anchor;
385: }
386:
387:
394: public RectangleAnchor getShapeLocation() {
395: return this.shapeLocation;
396: }
397:
398:
406: public void setShapeLocation(RectangleAnchor location) {
407: if (location == null) {
408: throw new IllegalArgumentException("Null 'location' argument.");
409: }
410: this.shapeLocation = location;
411: }
412:
413:
420: public boolean isLineVisible() {
421: return this.lineVisible;
422: }
423:
424:
431: public void setLineVisible(boolean visible) {
432: this.lineVisible = visible;
433: }
434:
435:
442: public Shape getLine() {
443: return this.line;
444: }
445:
446:
454: public void setLine(Shape line) {
455: this.line = line;
456: }
457:
458:
465: public Paint getLinePaint() {
466: return this.linePaint;
467: }
468:
469:
476: public void setLinePaint(Paint paint) {
477: this.linePaint = paint;
478: }
479:
480:
487: public Stroke getLineStroke() {
488: return this.lineStroke;
489: }
490:
491:
498: public void setLineStroke(Stroke stroke) {
499: this.lineStroke = stroke;
500: }
501:
502:
511: public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) {
512: RectangleConstraint contentConstraint = toContentConstraint(constraint);
513: LengthConstraintType w = contentConstraint.getWidthConstraintType();
514: LengthConstraintType h = contentConstraint.getHeightConstraintType();
515: Size2D contentSize = null;
516: if (w == LengthConstraintType.NONE) {
517: if (h == LengthConstraintType.NONE) {
518: contentSize = arrangeNN(g2);
519: }
520: else if (h == LengthConstraintType.RANGE) {
521: throw new RuntimeException("Not yet implemented.");
522: }
523: else if (h == LengthConstraintType.FIXED) {
524: throw new RuntimeException("Not yet implemented.");
525: }
526: }
527: else if (w == LengthConstraintType.RANGE) {
528: if (h == LengthConstraintType.NONE) {
529: throw new RuntimeException("Not yet implemented.");
530: }
531: else if (h == LengthConstraintType.RANGE) {
532: throw new RuntimeException("Not yet implemented.");
533: }
534: else if (h == LengthConstraintType.FIXED) {
535: throw new RuntimeException("Not yet implemented.");
536: }
537: }
538: else if (w == LengthConstraintType.FIXED) {
539: if (h == LengthConstraintType.NONE) {
540: throw new RuntimeException("Not yet implemented.");
541: }
542: else if (h == LengthConstraintType.RANGE) {
543: throw new RuntimeException("Not yet implemented.");
544: }
545: else if (h == LengthConstraintType.FIXED) {
546: contentSize = new Size2D(
547: contentConstraint.getWidth(),
548: contentConstraint.getHeight()
549: );
550: }
551: }
552: return new Size2D(
553: calculateTotalWidth(contentSize.getWidth()),
554: calculateTotalHeight(contentSize.getHeight())
555: );
556: }
557:
558:
567: protected Size2D arrangeNN(Graphics2D g2) {
568: Rectangle2D contentSize = new Rectangle2D.Double();
569: if (this.line != null) {
570: contentSize.setRect(this.line.getBounds2D());
571: }
572: if (this.shape != null) {
573: contentSize = contentSize.createUnion(this.shape.getBounds2D());
574: }
575: return new Size2D(contentSize.getWidth(), contentSize.getHeight());
576: }
577:
578:
584: public void draw(Graphics2D g2, Rectangle2D area) {
585:
586: area = trimMargin(area);
587: drawBorder(g2, area);
588: area = trimBorder(area);
589: area = trimPadding(area);
590:
591: if (this.lineVisible) {
592: Point2D location = RectangleAnchor.coordinates(area,
593: this.shapeLocation);
594: Shape aLine = ShapeUtilities.createTranslatedShape(getLine(),
595: this.shapeAnchor, location.getX(), location.getY());
596: g2.setPaint(this.linePaint);
597: g2.setStroke(this.lineStroke);
598: g2.draw(aLine);
599: }
600:
601: if (this.shapeVisible) {
602: Point2D location = RectangleAnchor.coordinates(area,
603: this.shapeLocation);
604:
605: Shape s = ShapeUtilities.createTranslatedShape(this.shape,
606: this.shapeAnchor, location.getX(), location.getY());
607: if (this.shapeFilled) {
608: Paint p = this.fillPaint;
609: if (p instanceof GradientPaint) {
610: GradientPaint gp = (GradientPaint) this.fillPaint;
611: p = this.fillPaintTransformer.transform(gp, s);
612: }
613: g2.setPaint(p);
614: g2.fill(s);
615: }
616: if (this.shapeOutlineVisible) {
617: g2.setPaint(this.outlinePaint);
618: g2.setStroke(this.outlineStroke);
619: g2.draw(s);
620: }
621: }
622:
623: }
624:
625:
634: public Object draw(Graphics2D g2, Rectangle2D area, Object params) {
635: draw(g2, area);
636: return null;
637: }
638:
639:
647: public boolean equals(Object obj) {
648: if (!(obj instanceof LegendGraphic)) {
649: return false;
650: }
651: LegendGraphic that = (LegendGraphic) obj;
652: if (this.shapeVisible != that.shapeVisible) {
653: return false;
654: }
655: if (!ShapeUtilities.equal(this.shape, that.shape)) {
656: return false;
657: }
658: if (this.shapeFilled != that.shapeFilled) {
659: return false;
660: }
661: if (!PaintUtilities.equal(this.fillPaint, that.fillPaint)) {
662: return false;
663: }
664: if (!ObjectUtilities.equal(this.fillPaintTransformer,
665: that.fillPaintTransformer)) {
666: return false;
667: }
668: if (this.shapeOutlineVisible != that.shapeOutlineVisible) {
669: return false;
670: }
671: if (!PaintUtilities.equal(this.outlinePaint, that.outlinePaint)) {
672: return false;
673: }
674: if (!ObjectUtilities.equal(this.outlineStroke, that.outlineStroke)) {
675: return false;
676: }
677: if (this.shapeAnchor != that.shapeAnchor) {
678: return false;
679: }
680: if (this.shapeLocation != that.shapeLocation) {
681: return false;
682: }
683: if (this.lineVisible != that.lineVisible) {
684: return false;
685: }
686: if (!ShapeUtilities.equal(this.line, that.line)) {
687: return false;
688: }
689: if (!PaintUtilities.equal(this.linePaint, that.linePaint)) {
690: return false;
691: }
692: if (!ObjectUtilities.equal(this.lineStroke, that.lineStroke)) {
693: return false;
694: }
695: return super.equals(obj);
696: }
697:
698:
703: public int hashCode() {
704: int result = 193;
705: result = 37 * result + ObjectUtilities.hashCode(this.fillPaint);
706:
707: return result;
708: }
709:
710:
717: public Object clone() throws CloneNotSupportedException {
718: LegendGraphic clone = (LegendGraphic) super.clone();
719: clone.shape = ShapeUtilities.clone(this.shape);
720: clone.line = ShapeUtilities.clone(this.line);
721: return clone;
722: }
723:
724:
731: private void writeObject(ObjectOutputStream stream) throws IOException {
732: stream.defaultWriteObject();
733: SerialUtilities.writeShape(this.shape, stream);
734: SerialUtilities.writePaint(this.fillPaint, stream);
735: SerialUtilities.writePaint(this.outlinePaint, stream);
736: SerialUtilities.writeStroke(this.outlineStroke, stream);
737: SerialUtilities.writeShape(this.line, stream);
738: SerialUtilities.writePaint(this.linePaint, stream);
739: SerialUtilities.writeStroke(this.lineStroke, stream);
740: }
741:
742:
750: private void readObject(ObjectInputStream stream)
751: throws IOException, ClassNotFoundException {
752: stream.defaultReadObject();
753: this.shape = SerialUtilities.readShape(stream);
754: this.fillPaint = SerialUtilities.readPaint(stream);
755: this.outlinePaint = SerialUtilities.readPaint(stream);
756: this.outlineStroke = SerialUtilities.readStroke(stream);
757: this.line = SerialUtilities.readShape(stream);
758: this.linePaint = SerialUtilities.readPaint(stream);
759: this.lineStroke = SerialUtilities.readStroke(stream);
760: }
761:
762: }