1:
59:
60: package ;
61:
62: import ;
63: import ;
64: import ;
65: import ;
66: import ;
67: import ;
68: import ;
69: import ;
70: import ;
71:
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: import ;
93:
94:
101: public class LegendTitle extends Title
102: implements Cloneable, PublicCloneable, Serializable {
103:
104:
105: private static final long serialVersionUID = 2644010518533854633L;
106:
107:
108: public static final Font DEFAULT_ITEM_FONT
109: = new Font("SansSerif", Font.PLAIN, 12);
110:
111:
112: public static final Paint DEFAULT_ITEM_PAINT = Color.black;
113:
114:
115: private LegendItemSource[] sources;
116:
117:
118: private transient Paint backgroundPaint;
119:
120:
121: private RectangleEdge legendItemGraphicEdge;
122:
123:
124: private RectangleAnchor legendItemGraphicAnchor;
125:
126:
127: private RectangleAnchor legendItemGraphicLocation;
128:
129:
130: private RectangleInsets legendItemGraphicPadding;
131:
132:
133: private Font itemFont;
134:
135:
136: private transient Paint itemPaint;
137:
138:
139: private RectangleInsets itemLabelPadding;
140:
141:
144: private BlockContainer items;
145:
146: private Arrangement hLayout;
147:
148: private Arrangement vLayout;
149:
150:
154: private BlockContainer wrapper;
155:
156:
161: public LegendTitle(LegendItemSource source) {
162: this(source, new FlowArrangement(), new ColumnArrangement());
163: }
164:
165:
174: public LegendTitle(LegendItemSource source,
175: Arrangement hLayout, Arrangement vLayout) {
176: this.sources = new LegendItemSource[] {source};
177: this.items = new BlockContainer(hLayout);
178: this.hLayout = hLayout;
179: this.vLayout = vLayout;
180: this.backgroundPaint = null;
181: this.legendItemGraphicEdge = RectangleEdge.LEFT;
182: this.legendItemGraphicAnchor = RectangleAnchor.CENTER;
183: this.legendItemGraphicLocation = RectangleAnchor.CENTER;
184: this.legendItemGraphicPadding = new RectangleInsets(2.0, 2.0, 2.0, 2.0);
185: this.itemFont = DEFAULT_ITEM_FONT;
186: this.itemPaint = DEFAULT_ITEM_PAINT;
187: this.itemLabelPadding = new RectangleInsets(2.0, 2.0, 2.0, 2.0);
188: }
189:
190:
195: public LegendItemSource[] getSources() {
196: return this.sources;
197: }
198:
199:
205: public void setSources(LegendItemSource[] sources) {
206: if (sources == null) {
207: throw new IllegalArgumentException("Null 'sources' argument.");
208: }
209: this.sources = sources;
210: notifyListeners(new TitleChangeEvent(this));
211: }
212:
213:
218: public Paint getBackgroundPaint() {
219: return this.backgroundPaint;
220: }
221:
222:
228: public void setBackgroundPaint(Paint paint) {
229: this.backgroundPaint = paint;
230: notifyListeners(new TitleChangeEvent(this));
231: }
232:
233:
238: public RectangleEdge getLegendItemGraphicEdge() {
239: return this.legendItemGraphicEdge;
240: }
241:
242:
247: public void setLegendItemGraphicEdge(RectangleEdge edge) {
248: if (edge == null) {
249: throw new IllegalArgumentException("Null 'edge' argument.");
250: }
251: this.legendItemGraphicEdge = edge;
252: notifyListeners(new TitleChangeEvent(this));
253: }
254:
255:
260: public RectangleAnchor getLegendItemGraphicAnchor() {
261: return this.legendItemGraphicAnchor;
262: }
263:
264:
269: public void setLegendItemGraphicAnchor(RectangleAnchor anchor) {
270: if (anchor == null) {
271: throw new IllegalArgumentException("Null 'anchor' point.");
272: }
273: this.legendItemGraphicAnchor = anchor;
274: }
275:
276:
281: public RectangleAnchor getLegendItemGraphicLocation() {
282: return this.legendItemGraphicLocation;
283: }
284:
285:
290: public void setLegendItemGraphicLocation(RectangleAnchor anchor) {
291: this.legendItemGraphicLocation = anchor;
292: }
293:
294:
299: public RectangleInsets getLegendItemGraphicPadding() {
300: return this.legendItemGraphicPadding;
301: }
302:
303:
309: public void setLegendItemGraphicPadding(RectangleInsets padding) {
310: if (padding == null) {
311: throw new IllegalArgumentException("Null 'padding' argument.");
312: }
313: this.legendItemGraphicPadding = padding;
314: notifyListeners(new TitleChangeEvent(this));
315: }
316:
317:
322: public Font getItemFont() {
323: return this.itemFont;
324: }
325:
326:
332: public void setItemFont(Font font) {
333: if (font == null) {
334: throw new IllegalArgumentException("Null 'font' argument.");
335: }
336: this.itemFont = font;
337: notifyListeners(new TitleChangeEvent(this));
338: }
339:
340:
345: public Paint getItemPaint() {
346: return this.itemPaint;
347: }
348:
349:
354: public void setItemPaint(Paint paint) {
355: if (paint == null) {
356: throw new IllegalArgumentException("Null 'paint' argument.");
357: }
358: this.itemPaint = paint;
359: notifyListeners(new TitleChangeEvent(this));
360: }
361:
362:
367: public RectangleInsets getItemLabelPadding() {
368: return this.itemLabelPadding;
369: }
370:
371:
376: public void setItemLabelPadding(RectangleInsets padding) {
377: if (padding == null) {
378: throw new IllegalArgumentException("Null 'padding' argument.");
379: }
380: this.itemLabelPadding = padding;
381: notifyListeners(new TitleChangeEvent(this));
382: }
383:
384:
387: protected void fetchLegendItems() {
388: this.items.clear();
389: RectangleEdge p = getPosition();
390: if (RectangleEdge.isTopOrBottom(p)) {
391: this.items.setArrangement(this.hLayout);
392: }
393: else {
394: this.items.setArrangement(this.vLayout);
395: }
396: for (int s = 0; s < this.sources.length; s++) {
397: LegendItemCollection legendItems = this.sources[s].getLegendItems();
398: if (legendItems != null) {
399: for (int i = 0; i < legendItems.getItemCount(); i++) {
400: LegendItem item = legendItems.get(i);
401: Block block = createLegendItemBlock(item);
402: this.items.add(block);
403: }
404: }
405: }
406: }
407:
408:
415: protected Block createLegendItemBlock(LegendItem item) {
416: BlockContainer result = null;
417: LegendGraphic lg = new LegendGraphic(item.getShape(),
418: item.getFillPaint());
419: lg.setFillPaintTransformer(item.getFillPaintTransformer());
420: lg.setShapeFilled(item.isShapeFilled());
421: lg.setLine(item.getLine());
422: lg.setLineStroke(item.getLineStroke());
423: lg.setLinePaint(item.getLinePaint());
424: lg.setLineVisible(item.isLineVisible());
425: lg.setShapeVisible(item.isShapeVisible());
426: lg.setShapeOutlineVisible(item.isShapeOutlineVisible());
427: lg.setOutlinePaint(item.getOutlinePaint());
428: lg.setOutlineStroke(item.getOutlineStroke());
429: lg.setPadding(this.legendItemGraphicPadding);
430:
431: LegendItemBlockContainer legendItem = new LegendItemBlockContainer(
432: new BorderArrangement(), item.getDatasetIndex(),
433: item.getSeriesIndex());
434: lg.setShapeAnchor(getLegendItemGraphicAnchor());
435: lg.setShapeLocation(getLegendItemGraphicLocation());
436: legendItem.add(lg, this.legendItemGraphicEdge);
437: LabelBlock labelBlock = new LabelBlock(item.getLabel(), this.itemFont,
438: this.itemPaint);
439: labelBlock.setPadding(this.itemLabelPadding);
440: legendItem.add(labelBlock);
441: legendItem.setToolTipText(item.getToolTipText());
442: legendItem.setURLText(item.getURLText());
443:
444: result = new BlockContainer(new CenterArrangement());
445: result.add(legendItem);
446:
447: return result;
448: }
449:
450:
455: public BlockContainer getItemContainer() {
456: return this.items;
457: }
458:
459:
468: public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) {
469: Size2D result = new Size2D();
470: fetchLegendItems();
471: if (this.items.isEmpty()) {
472: return result;
473: }
474: BlockContainer container = this.wrapper;
475: if (container == null) {
476: container = this.items;
477: }
478: RectangleConstraint c = toContentConstraint(constraint);
479: Size2D size = container.arrange(g2, c);
480: result.height = calculateTotalHeight(size.height);
481: result.width = calculateTotalWidth(size.width);
482: return result;
483: }
484:
485:
492: public void draw(Graphics2D g2, Rectangle2D area) {
493: draw(g2, area, null);
494: }
495:
496:
506: public Object draw(Graphics2D g2, Rectangle2D area, Object params) {
507: Rectangle2D target = (Rectangle2D) area.clone();
508: target = trimMargin(target);
509: if (this.backgroundPaint != null) {
510: g2.setPaint(this.backgroundPaint);
511: g2.fill(target);
512: }
513: BlockFrame border = getFrame();
514: border.draw(g2, target);
515: border.getInsets().trim(target);
516: BlockContainer container = this.wrapper;
517: if (container == null) {
518: container = this.items;
519: }
520: target = trimPadding(target);
521: return container.draw(g2, target, params);
522: }
523:
524:
529: public void setWrapper(BlockContainer wrapper) {
530: this.wrapper = wrapper;
531: }
532:
533:
540: public boolean equals(Object obj) {
541: if (obj == this) {
542: return true;
543: }
544: if (!(obj instanceof LegendTitle)) {
545: return false;
546: }
547: if (!super.equals(obj)) {
548: return false;
549: }
550: LegendTitle that = (LegendTitle) obj;
551: if (!PaintUtilities.equal(this.backgroundPaint, that.backgroundPaint)) {
552: return false;
553: }
554: if (this.legendItemGraphicEdge != that.legendItemGraphicEdge) {
555: return false;
556: }
557: if (this.legendItemGraphicAnchor != that.legendItemGraphicAnchor) {
558: return false;
559: }
560: if (this.legendItemGraphicLocation != that.legendItemGraphicLocation) {
561: return false;
562: }
563: if (!this.itemFont.equals(that.itemFont)) {
564: return false;
565: }
566: if (!this.itemPaint.equals(that.itemPaint)) {
567: return false;
568: }
569: if (!this.hLayout.equals(that.hLayout)) {
570: return false;
571: }
572: if (!this.vLayout.equals(that.vLayout)) {
573: return false;
574: }
575: return true;
576: }
577:
578:
585: private void writeObject(ObjectOutputStream stream) throws IOException {
586: stream.defaultWriteObject();
587: SerialUtilities.writePaint(this.backgroundPaint, stream);
588: SerialUtilities.writePaint(this.itemPaint, stream);
589: }
590:
591:
599: private void readObject(ObjectInputStream stream)
600: throws IOException, ClassNotFoundException {
601: stream.defaultReadObject();
602: this.backgroundPaint = SerialUtilities.readPaint(stream);
603: this.itemPaint = SerialUtilities.readPaint(stream);
604: }
605:
606: }