1:
56:
57: package ;
58:
59: import ;
60: import ;
61: import ;
62: import ;
63: import ;
64: import ;
65: import ;
66: import ;
67: import ;
68:
69: import ;
70: import ;
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:
90:
97: public class LegendTitle extends Title
98: implements Cloneable, PublicCloneable, Serializable {
99:
100:
101: private static final long serialVersionUID = 2644010518533854633L;
102:
103:
104: public static final Font DEFAULT_ITEM_FONT
105: = new Font("SansSerif", Font.PLAIN, 12);
106:
107:
108: public static final Paint DEFAULT_ITEM_PAINT = Color.black;
109:
110:
111: private LegendItemSource[] sources;
112:
113:
114: private transient Paint backgroundPaint;
115:
116:
117: private RectangleEdge legendItemGraphicEdge;
118:
119:
120: private RectangleAnchor legendItemGraphicAnchor;
121:
122:
123: private RectangleAnchor legendItemGraphicLocation;
124:
125:
126: private RectangleInsets legendItemGraphicPadding;
127:
128:
129: private Font itemFont;
130:
131:
132: private transient Paint itemPaint;
133:
134:
135: private RectangleInsets itemLabelPadding;
136:
137:
140: private BlockContainer items;
141:
142: private Arrangement hLayout;
143:
144: private Arrangement vLayout;
145:
146:
150: private BlockContainer wrapper;
151:
152:
157: public LegendTitle(LegendItemSource source) {
158: this(source, new FlowArrangement(), new ColumnArrangement());
159: }
160:
161:
170: public LegendTitle(LegendItemSource source,
171: Arrangement hLayout, Arrangement vLayout) {
172: this.sources = new LegendItemSource[] {source};
173: this.items = new BlockContainer(hLayout);
174: this.hLayout = hLayout;
175: this.vLayout = vLayout;
176: this.backgroundPaint = null;
177: this.legendItemGraphicEdge = RectangleEdge.LEFT;
178: this.legendItemGraphicAnchor = RectangleAnchor.CENTER;
179: this.legendItemGraphicLocation = RectangleAnchor.CENTER;
180: this.legendItemGraphicPadding = new RectangleInsets(2.0, 2.0, 2.0, 2.0);
181: this.itemFont = DEFAULT_ITEM_FONT;
182: this.itemPaint = DEFAULT_ITEM_PAINT;
183: this.itemLabelPadding = new RectangleInsets(2.0, 2.0, 2.0, 2.0);
184: }
185:
186:
191: public LegendItemSource[] getSources() {
192: return this.sources;
193: }
194:
195:
201: public void setSources(LegendItemSource[] sources) {
202: if (sources == null) {
203: throw new IllegalArgumentException("Null 'sources' argument.");
204: }
205: this.sources = sources;
206: notifyListeners(new TitleChangeEvent(this));
207: }
208:
209:
214: public Paint getBackgroundPaint() {
215: return this.backgroundPaint;
216: }
217:
218:
224: public void setBackgroundPaint(Paint paint) {
225: this.backgroundPaint = paint;
226: notifyListeners(new TitleChangeEvent(this));
227: }
228:
229:
234: public RectangleEdge getLegendItemGraphicEdge() {
235: return this.legendItemGraphicEdge;
236: }
237:
238:
243: public void setLegendItemGraphicEdge(RectangleEdge edge) {
244: if (edge == null) {
245: throw new IllegalArgumentException("Null 'edge' argument.");
246: }
247: this.legendItemGraphicEdge = edge;
248: notifyListeners(new TitleChangeEvent(this));
249: }
250:
251:
256: public RectangleAnchor getLegendItemGraphicAnchor() {
257: return this.legendItemGraphicAnchor;
258: }
259:
260:
265: public void setLegendItemGraphicAnchor(RectangleAnchor anchor) {
266: if (anchor == null) {
267: throw new IllegalArgumentException("Null 'anchor' point.");
268: }
269: this.legendItemGraphicAnchor = anchor;
270: }
271:
272:
277: public RectangleAnchor getLegendItemGraphicLocation() {
278: return this.legendItemGraphicLocation;
279: }
280:
281:
286: public void setLegendItemGraphicLocation(RectangleAnchor anchor) {
287: this.legendItemGraphicLocation = anchor;
288: }
289:
290:
295: public RectangleInsets getLegendItemGraphicPadding() {
296: return this.legendItemGraphicPadding;
297: }
298:
299:
305: public void setLegendItemGraphicPadding(RectangleInsets padding) {
306: if (padding == null) {
307: throw new IllegalArgumentException("Null 'padding' argument.");
308: }
309: this.legendItemGraphicPadding = padding;
310: notifyListeners(new TitleChangeEvent(this));
311: }
312:
313:
318: public Font getItemFont() {
319: return this.itemFont;
320: }
321:
322:
328: public void setItemFont(Font font) {
329: if (font == null) {
330: throw new IllegalArgumentException("Null 'font' argument.");
331: }
332: this.itemFont = font;
333: notifyListeners(new TitleChangeEvent(this));
334: }
335:
336:
341: public Paint getItemPaint() {
342: return this.itemPaint;
343: }
344:
345:
350: public void setItemPaint(Paint paint) {
351: if (paint == null) {
352: throw new IllegalArgumentException("Null 'paint' argument.");
353: }
354: this.itemPaint = paint;
355: notifyListeners(new TitleChangeEvent(this));
356: }
357:
358:
363: public RectangleInsets getItemLabelPadding() {
364: return this.itemLabelPadding;
365: }
366:
367:
372: public void setItemLabelPadding(RectangleInsets padding) {
373: if (padding == null) {
374: throw new IllegalArgumentException("Null 'padding' argument.");
375: }
376: this.itemLabelPadding = padding;
377: notifyListeners(new TitleChangeEvent(this));
378: }
379:
380:
383: protected void fetchLegendItems() {
384: this.items.clear();
385: RectangleEdge p = getPosition();
386: if (RectangleEdge.isTopOrBottom(p)) {
387: this.items.setArrangement(this.hLayout);
388: }
389: else {
390: this.items.setArrangement(this.vLayout);
391: }
392: for (int s = 0; s < this.sources.length; s++) {
393: LegendItemCollection legendItems = this.sources[s].getLegendItems();
394: if (legendItems != null) {
395: for (int i = 0; i < legendItems.getItemCount(); i++) {
396: LegendItem item = legendItems.get(i);
397: Block block = createLegendItemBlock(item);
398: this.items.add(block);
399: }
400: }
401: }
402: }
403:
404:
411: protected Block createLegendItemBlock(LegendItem item) {
412: BlockContainer result = null;
413: LegendGraphic lg = new LegendGraphic(item.getShape(),
414: item.getFillPaint());
415: lg.setShapeFilled(item.isShapeFilled());
416: lg.setLine(item.getLine());
417: lg.setLineStroke(item.getLineStroke());
418: lg.setLinePaint(item.getLinePaint());
419: lg.setLineVisible(item.isLineVisible());
420: lg.setShapeVisible(item.isShapeVisible());
421: lg.setShapeOutlineVisible(item.isShapeOutlineVisible());
422: lg.setOutlinePaint(item.getOutlinePaint());
423: lg.setOutlineStroke(item.getOutlineStroke());
424: lg.setPadding(this.legendItemGraphicPadding);
425:
426: LegendItemBlockContainer legendItem = new LegendItemBlockContainer(
427: new BorderArrangement(), item.getDatasetIndex(),
428: item.getSeriesIndex());
429: lg.setShapeAnchor(getLegendItemGraphicAnchor());
430: lg.setShapeLocation(getLegendItemGraphicLocation());
431: legendItem.add(lg, this.legendItemGraphicEdge);
432: LabelBlock labelBlock = new LabelBlock(item.getLabel(), this.itemFont,
433: this.itemPaint);
434: labelBlock.setPadding(this.itemLabelPadding);
435: labelBlock.setToolTipText(item.getToolTipText());
436: legendItem.add(labelBlock);
437:
438: result = new BlockContainer(new CenterArrangement());
439: result.add(legendItem);
440:
441: return result;
442: }
443:
444:
449: public BlockContainer getItemContainer() {
450: return this.items;
451: }
452:
453:
462: public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) {
463: Size2D result = new Size2D();
464: fetchLegendItems();
465: if (this.items.isEmpty()) {
466: return result;
467: }
468: BlockContainer container = this.wrapper;
469: if (container == null) {
470: container = this.items;
471: }
472: RectangleConstraint c = toContentConstraint(constraint);
473: Size2D size = container.arrange(g2, c);
474: result.height = calculateTotalHeight(size.height);
475: result.width = calculateTotalWidth(size.width);
476: return result;
477: }
478:
479:
486: public void draw(Graphics2D g2, Rectangle2D area) {
487: draw(g2, area, null);
488: }
489:
490:
500: public Object draw(Graphics2D g2, Rectangle2D area, Object params) {
501: Rectangle2D target = (Rectangle2D) area.clone();
502: target = trimMargin(target);
503: if (this.backgroundPaint != null) {
504: g2.setPaint(this.backgroundPaint);
505: g2.fill(target);
506: }
507: getBorder().draw(g2, target);
508: getBorder().getInsets().trim(target);
509: BlockContainer container = this.wrapper;
510: if (container == null) {
511: container = this.items;
512: }
513: target = trimPadding(target);
514: return container.draw(g2, target, params);
515: }
516:
517:
522: public void setWrapper(BlockContainer wrapper) {
523: this.wrapper = wrapper;
524: }
525:
526:
533: public boolean equals(Object obj) {
534: if (obj == this) {
535: return true;
536: }
537: if (!(obj instanceof LegendTitle)) {
538: return false;
539: }
540: if (!super.equals(obj)) {
541: return false;
542: }
543: LegendTitle that = (LegendTitle) obj;
544: if (!PaintUtilities.equal(this.backgroundPaint, that.backgroundPaint)) {
545: return false;
546: }
547: if (this.legendItemGraphicEdge != that.legendItemGraphicEdge) {
548: return false;
549: }
550: if (this.legendItemGraphicAnchor != that.legendItemGraphicAnchor) {
551: return false;
552: }
553: if (this.legendItemGraphicLocation != that.legendItemGraphicLocation) {
554: return false;
555: }
556: if (!this.itemFont.equals(that.itemFont)) {
557: return false;
558: }
559: if (!this.itemPaint.equals(that.itemPaint)) {
560: return false;
561: }
562: if (!this.hLayout.equals(that.hLayout)) {
563: return false;
564: }
565: if (!this.vLayout.equals(that.vLayout)) {
566: return false;
567: }
568: return true;
569: }
570:
571:
578: private void writeObject(ObjectOutputStream stream) throws IOException {
579: stream.defaultWriteObject();
580: SerialUtilities.writePaint(this.backgroundPaint, stream);
581: SerialUtilities.writePaint(this.itemPaint, stream);
582: }
583:
584:
592: private void readObject(ObjectInputStream stream)
593: throws IOException, ClassNotFoundException {
594: stream.defaultReadObject();
595: this.backgroundPaint = SerialUtilities.readPaint(stream);
596: this.itemPaint = SerialUtilities.readPaint(stream);
597: }
598:
599: }