1:
58:
59: package ;
60:
61: import ;
62: import ;
63: import ;
64: import ;
65: import ;
66: import ;
67: import ;
68: import ;
69: import ;
70:
71: import ;
72: import ;
73: import ;
74: import ;
75: import ;
76: import ;
77: import ;
78: import ;
79: import ;
80: import ;
81: import ;
82: import ;
83: import ;
84: import ;
85: import ;
86: import ;
87: import ;
88: import ;
89: import ;
90: import ;
91: import ;
92:
93:
100: public class LegendTitle extends Title
101: implements Cloneable, PublicCloneable, Serializable {
102:
103:
104: private static final long serialVersionUID = 2644010518533854633L;
105:
106:
107: public static final Font DEFAULT_ITEM_FONT
108: = new Font("SansSerif", Font.PLAIN, 12);
109:
110:
111: public static final Paint DEFAULT_ITEM_PAINT = Color.black;
112:
113:
114: private LegendItemSource[] sources;
115:
116:
117: private transient Paint backgroundPaint;
118:
119:
120: private RectangleEdge legendItemGraphicEdge;
121:
122:
123: private RectangleAnchor legendItemGraphicAnchor;
124:
125:
126: private RectangleAnchor legendItemGraphicLocation;
127:
128:
129: private RectangleInsets legendItemGraphicPadding;
130:
131:
132: private Font itemFont;
133:
134:
135: private transient Paint itemPaint;
136:
137:
138: private RectangleInsets itemLabelPadding;
139:
140:
143: private BlockContainer items;
144:
145:
149: private Arrangement hLayout;
150:
151:
155: private Arrangement vLayout;
156:
157:
161: private BlockContainer wrapper;
162:
163:
168: public LegendTitle(LegendItemSource source) {
169: this(source, new FlowArrangement(), new ColumnArrangement());
170: }
171:
172:
181: public LegendTitle(LegendItemSource source,
182: Arrangement hLayout, Arrangement vLayout) {
183: this.sources = new LegendItemSource[] {source};
184: this.items = new BlockContainer(hLayout);
185: this.hLayout = hLayout;
186: this.vLayout = vLayout;
187: this.backgroundPaint = null;
188: this.legendItemGraphicEdge = RectangleEdge.LEFT;
189: this.legendItemGraphicAnchor = RectangleAnchor.CENTER;
190: this.legendItemGraphicLocation = RectangleAnchor.CENTER;
191: this.legendItemGraphicPadding = new RectangleInsets(2.0, 2.0, 2.0, 2.0);
192: this.itemFont = DEFAULT_ITEM_FONT;
193: this.itemPaint = DEFAULT_ITEM_PAINT;
194: this.itemLabelPadding = new RectangleInsets(2.0, 2.0, 2.0, 2.0);
195: }
196:
197:
202: public LegendItemSource[] getSources() {
203: return this.sources;
204: }
205:
206:
212: public void setSources(LegendItemSource[] sources) {
213: if (sources == null) {
214: throw new IllegalArgumentException("Null 'sources' argument.");
215: }
216: this.sources = sources;
217: notifyListeners(new TitleChangeEvent(this));
218: }
219:
220:
225: public Paint getBackgroundPaint() {
226: return this.backgroundPaint;
227: }
228:
229:
235: public void setBackgroundPaint(Paint paint) {
236: this.backgroundPaint = paint;
237: notifyListeners(new TitleChangeEvent(this));
238: }
239:
240:
245: public RectangleEdge getLegendItemGraphicEdge() {
246: return this.legendItemGraphicEdge;
247: }
248:
249:
254: public void setLegendItemGraphicEdge(RectangleEdge edge) {
255: if (edge == null) {
256: throw new IllegalArgumentException("Null 'edge' argument.");
257: }
258: this.legendItemGraphicEdge = edge;
259: notifyListeners(new TitleChangeEvent(this));
260: }
261:
262:
267: public RectangleAnchor getLegendItemGraphicAnchor() {
268: return this.legendItemGraphicAnchor;
269: }
270:
271:
276: public void setLegendItemGraphicAnchor(RectangleAnchor anchor) {
277: if (anchor == null) {
278: throw new IllegalArgumentException("Null 'anchor' point.");
279: }
280: this.legendItemGraphicAnchor = anchor;
281: }
282:
283:
288: public RectangleAnchor getLegendItemGraphicLocation() {
289: return this.legendItemGraphicLocation;
290: }
291:
292:
297: public void setLegendItemGraphicLocation(RectangleAnchor anchor) {
298: this.legendItemGraphicLocation = anchor;
299: }
300:
301:
306: public RectangleInsets getLegendItemGraphicPadding() {
307: return this.legendItemGraphicPadding;
308: }
309:
310:
316: public void setLegendItemGraphicPadding(RectangleInsets padding) {
317: if (padding == null) {
318: throw new IllegalArgumentException("Null 'padding' argument.");
319: }
320: this.legendItemGraphicPadding = padding;
321: notifyListeners(new TitleChangeEvent(this));
322: }
323:
324:
329: public Font getItemFont() {
330: return this.itemFont;
331: }
332:
333:
339: public void setItemFont(Font font) {
340: if (font == null) {
341: throw new IllegalArgumentException("Null 'font' argument.");
342: }
343: this.itemFont = font;
344: notifyListeners(new TitleChangeEvent(this));
345: }
346:
347:
352: public Paint getItemPaint() {
353: return this.itemPaint;
354: }
355:
356:
361: public void setItemPaint(Paint paint) {
362: if (paint == null) {
363: throw new IllegalArgumentException("Null 'paint' argument.");
364: }
365: this.itemPaint = paint;
366: notifyListeners(new TitleChangeEvent(this));
367: }
368:
369:
374: public RectangleInsets getItemLabelPadding() {
375: return this.itemLabelPadding;
376: }
377:
378:
383: public void setItemLabelPadding(RectangleInsets padding) {
384: if (padding == null) {
385: throw new IllegalArgumentException("Null 'padding' argument.");
386: }
387: this.itemLabelPadding = padding;
388: notifyListeners(new TitleChangeEvent(this));
389: }
390:
391:
394: protected void fetchLegendItems() {
395: this.items.clear();
396: RectangleEdge p = getPosition();
397: if (RectangleEdge.isTopOrBottom(p)) {
398: this.items.setArrangement(this.hLayout);
399: }
400: else {
401: this.items.setArrangement(this.vLayout);
402: }
403: for (int s = 0; s < this.sources.length; s++) {
404: LegendItemCollection legendItems = this.sources[s].getLegendItems();
405: if (legendItems != null) {
406: for (int i = 0; i < legendItems.getItemCount(); i++) {
407: LegendItem item = legendItems.get(i);
408: Block block = createLegendItemBlock(item);
409: this.items.add(block);
410: }
411: }
412: }
413: }
414:
415:
422: protected Block createLegendItemBlock(LegendItem item) {
423: BlockContainer result = null;
424: LegendGraphic lg = new LegendGraphic(item.getShape(),
425: item.getFillPaint());
426: lg.setFillPaintTransformer(item.getFillPaintTransformer());
427: lg.setShapeFilled(item.isShapeFilled());
428: lg.setLine(item.getLine());
429: lg.setLineStroke(item.getLineStroke());
430: lg.setLinePaint(item.getLinePaint());
431: lg.setLineVisible(item.isLineVisible());
432: lg.setShapeVisible(item.isShapeVisible());
433: lg.setShapeOutlineVisible(item.isShapeOutlineVisible());
434: lg.setOutlinePaint(item.getOutlinePaint());
435: lg.setOutlineStroke(item.getOutlineStroke());
436: lg.setPadding(this.legendItemGraphicPadding);
437:
438: LegendItemBlockContainer legendItem = new LegendItemBlockContainer(
439: new BorderArrangement(), item.getDataset(),
440: item.getSeriesKey());
441: lg.setShapeAnchor(getLegendItemGraphicAnchor());
442: lg.setShapeLocation(getLegendItemGraphicLocation());
443: legendItem.add(lg, this.legendItemGraphicEdge);
444: LabelBlock labelBlock = new LabelBlock(item.getLabel(), this.itemFont,
445: this.itemPaint);
446: labelBlock.setPadding(this.itemLabelPadding);
447: legendItem.add(labelBlock);
448: legendItem.setToolTipText(item.getToolTipText());
449: legendItem.setURLText(item.getURLText());
450:
451: result = new BlockContainer(new CenterArrangement());
452: result.add(legendItem);
453:
454: return result;
455: }
456:
457:
462: public BlockContainer getItemContainer() {
463: return this.items;
464: }
465:
466:
475: public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) {
476: Size2D result = new Size2D();
477: fetchLegendItems();
478: if (this.items.isEmpty()) {
479: return result;
480: }
481: BlockContainer container = this.wrapper;
482: if (container == null) {
483: container = this.items;
484: }
485: RectangleConstraint c = toContentConstraint(constraint);
486: Size2D size = container.arrange(g2, c);
487: result.height = calculateTotalHeight(size.height);
488: result.width = calculateTotalWidth(size.width);
489: return result;
490: }
491:
492:
499: public void draw(Graphics2D g2, Rectangle2D area) {
500: draw(g2, area, null);
501: }
502:
503:
513: public Object draw(Graphics2D g2, Rectangle2D area, Object params) {
514: Rectangle2D target = (Rectangle2D) area.clone();
515: target = trimMargin(target);
516: if (this.backgroundPaint != null) {
517: g2.setPaint(this.backgroundPaint);
518: g2.fill(target);
519: }
520: BlockFrame border = getFrame();
521: border.draw(g2, target);
522: border.getInsets().trim(target);
523: BlockContainer container = this.wrapper;
524: if (container == null) {
525: container = this.items;
526: }
527: target = trimPadding(target);
528: return container.draw(g2, target, params);
529: }
530:
531:
536: public void setWrapper(BlockContainer wrapper) {
537: this.wrapper = wrapper;
538: }
539:
540:
547: public boolean equals(Object obj) {
548: if (obj == this) {
549: return true;
550: }
551: if (!(obj instanceof LegendTitle)) {
552: return false;
553: }
554: if (!super.equals(obj)) {
555: return false;
556: }
557: LegendTitle that = (LegendTitle) obj;
558: if (!PaintUtilities.equal(this.backgroundPaint, that.backgroundPaint)) {
559: return false;
560: }
561: if (this.legendItemGraphicEdge != that.legendItemGraphicEdge) {
562: return false;
563: }
564: if (this.legendItemGraphicAnchor != that.legendItemGraphicAnchor) {
565: return false;
566: }
567: if (this.legendItemGraphicLocation != that.legendItemGraphicLocation) {
568: return false;
569: }
570: if (!this.itemFont.equals(that.itemFont)) {
571: return false;
572: }
573: if (!this.itemPaint.equals(that.itemPaint)) {
574: return false;
575: }
576: if (!this.hLayout.equals(that.hLayout)) {
577: return false;
578: }
579: if (!this.vLayout.equals(that.vLayout)) {
580: return false;
581: }
582: return true;
583: }
584:
585:
592: private void writeObject(ObjectOutputStream stream) throws IOException {
593: stream.defaultWriteObject();
594: SerialUtilities.writePaint(this.backgroundPaint, stream);
595: SerialUtilities.writePaint(this.itemPaint, stream);
596: }
597:
598:
606: private void readObject(ObjectInputStream stream)
607: throws IOException, ClassNotFoundException {
608: stream.defaultReadObject();
609: this.backgroundPaint = SerialUtilities.readPaint(stream);
610: this.itemPaint = SerialUtilities.readPaint(stream);
611: }
612:
613: }