1:
78:
79: package ;
80:
81: import ;
82: import ;
83: import ;
84: import ;
85: import ;
86: import ;
87: import ;
88: import ;
89: import ;
90:
91: import ;
92: import ;
93: import ;
94: import ;
95: import ;
96: import ;
97: import ;
98: import ;
99: import ;
100: import ;
101: import ;
102: import ;
103: import ;
104: import ;
105: import ;
106: import ;
107: import ;
108: import ;
109: import ;
110: import ;
111: import ;
112: import ;
113:
114:
118: public class TextTitle extends Title
119: implements Serializable, Cloneable, PublicCloneable {
120:
121:
122: private static final long serialVersionUID = 8372008692127477443L;
123:
124:
125: public static final Font DEFAULT_FONT = new Font("SansSerif", Font.BOLD,
126: 12);
127:
128:
129: public static final Paint DEFAULT_TEXT_PAINT = Color.black;
130:
131:
132: private String text;
133:
134:
135: private Font font;
136:
137:
138: private HorizontalAlignment textAlignment;
139:
140:
141: private transient Paint paint;
142:
143:
144: private transient Paint backgroundPaint;
145:
146:
147: private String toolTipText;
148:
149:
150: private String urlText;
151:
152:
153: private TextBlock content;
154:
155:
159: private boolean expandToFitSpace = false;
160:
161:
164: public TextTitle() {
165: this("");
166: }
167:
168:
173: public TextTitle(String text) {
174: this(text, TextTitle.DEFAULT_FONT, TextTitle.DEFAULT_TEXT_PAINT,
175: Title.DEFAULT_POSITION, Title.DEFAULT_HORIZONTAL_ALIGNMENT,
176: Title.DEFAULT_VERTICAL_ALIGNMENT, Title.DEFAULT_PADDING);
177: }
178:
179:
185: public TextTitle(String text, Font font) {
186: this(text, font, TextTitle.DEFAULT_TEXT_PAINT, Title.DEFAULT_POSITION,
187: Title.DEFAULT_HORIZONTAL_ALIGNMENT,
188: Title.DEFAULT_VERTICAL_ALIGNMENT, Title.DEFAULT_PADDING);
189: }
190:
191:
204: public TextTitle(String text, Font font, Paint paint,
205: RectangleEdge position,
206: HorizontalAlignment horizontalAlignment,
207: VerticalAlignment verticalAlignment,
208: RectangleInsets padding) {
209:
210: super(position, horizontalAlignment, verticalAlignment, padding);
211:
212: if (text == null) {
213: throw new NullPointerException("Null 'text' argument.");
214: }
215: if (font == null) {
216: throw new NullPointerException("Null 'font' argument.");
217: }
218: if (paint == null) {
219: throw new NullPointerException("Null 'paint' argument.");
220: }
221: this.text = text;
222: this.font = font;
223: this.paint = paint;
224:
225:
226:
227: this.textAlignment = horizontalAlignment;
228: this.backgroundPaint = null;
229: this.content = null;
230: this.toolTipText = null;
231: this.urlText = null;
232:
233: }
234:
235:
242: public String getText() {
243: return this.text;
244: }
245:
246:
252: public void setText(String text) {
253: if (text == null) {
254: throw new IllegalArgumentException("Null 'text' argument.");
255: }
256: if (!this.text.equals(text)) {
257: this.text = text;
258: notifyListeners(new TitleChangeEvent(this));
259: }
260: }
261:
262:
270: public HorizontalAlignment getTextAlignment() {
271: return this.textAlignment;
272: }
273:
274:
279: public void setTextAlignment(HorizontalAlignment alignment) {
280: if (alignment == null) {
281: throw new IllegalArgumentException("Null 'alignment' argument.");
282: }
283: this.textAlignment = alignment;
284: notifyListeners(new TitleChangeEvent(this));
285: }
286:
287:
294: public Font getFont() {
295: return this.font;
296: }
297:
298:
306: public void setFont(Font font) {
307: if (font == null) {
308: throw new IllegalArgumentException("Null 'font' argument.");
309: }
310: if (!this.font.equals(font)) {
311: this.font = font;
312: notifyListeners(new TitleChangeEvent(this));
313: }
314: }
315:
316:
323: public Paint getPaint() {
324: return this.paint;
325: }
326:
327:
335: public void setPaint(Paint paint) {
336: if (paint == null) {
337: throw new IllegalArgumentException("Null 'paint' argument.");
338: }
339: if (!this.paint.equals(paint)) {
340: this.paint = paint;
341: notifyListeners(new TitleChangeEvent(this));
342: }
343: }
344:
345:
350: public Paint getBackgroundPaint() {
351: return this.backgroundPaint;
352: }
353:
354:
361: public void setBackgroundPaint(Paint paint) {
362: this.backgroundPaint = paint;
363: notifyListeners(new TitleChangeEvent(this));
364: }
365:
366:
371: public String getToolTipText() {
372: return this.toolTipText;
373: }
374:
375:
381: public void setToolTipText(String text) {
382: this.toolTipText = text;
383: notifyListeners(new TitleChangeEvent(this));
384: }
385:
386:
391: public String getURLText() {
392: return this.urlText;
393: }
394:
395:
401: public void setURLText(String text) {
402: this.urlText = text;
403: notifyListeners(new TitleChangeEvent(this));
404: }
405:
406:
412: public boolean getExpandToFitSpace() {
413: return this.expandToFitSpace;
414: }
415:
416:
423: public void setExpandToFitSpace(boolean expand) {
424: this.expandToFitSpace = expand;
425: notifyListeners(new TitleChangeEvent(this));
426: }
427:
428:
437: public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) {
438: RectangleConstraint cc = toContentConstraint(constraint);
439: LengthConstraintType w = cc.getWidthConstraintType();
440: LengthConstraintType h = cc.getHeightConstraintType();
441: Size2D contentSize = null;
442: if (w == LengthConstraintType.NONE) {
443: if (h == LengthConstraintType.NONE) {
444: throw new RuntimeException("Not yet implemented.");
445: }
446: else if (h == LengthConstraintType.RANGE) {
447: throw new RuntimeException("Not yet implemented.");
448: }
449: else if (h == LengthConstraintType.FIXED) {
450: throw new RuntimeException("Not yet implemented.");
451: }
452: }
453: else if (w == LengthConstraintType.RANGE) {
454: if (h == LengthConstraintType.NONE) {
455: throw new RuntimeException("Not yet implemented.");
456: }
457: else if (h == LengthConstraintType.RANGE) {
458: contentSize = arrangeRR(g2, cc.getWidthRange(),
459: cc.getHeightRange());
460: }
461: else if (h == LengthConstraintType.FIXED) {
462: throw new RuntimeException("Not yet implemented.");
463: }
464: }
465: else if (w == LengthConstraintType.FIXED) {
466: if (h == LengthConstraintType.NONE) {
467: throw new RuntimeException("Not yet implemented.");
468: }
469: else if (h == LengthConstraintType.RANGE) {
470: throw new RuntimeException("Not yet implemented.");
471: }
472: else if (h == LengthConstraintType.FIXED) {
473: throw new RuntimeException("Not yet implemented.");
474: }
475: }
476: return new Size2D(calculateTotalWidth(contentSize.getWidth()),
477: calculateTotalHeight(contentSize.getHeight()));
478: }
479:
480:
491: protected Size2D arrangeRR(Graphics2D g2, Range widthRange,
492: Range heightRange) {
493: RectangleEdge position = getPosition();
494: if (position == RectangleEdge.TOP || position == RectangleEdge.BOTTOM) {
495: float maxWidth = (float) widthRange.getUpperBound();
496: g2.setFont(this.font);
497: this.content = TextUtilities.createTextBlock(this.text, this.font,
498: this.paint, maxWidth, new G2TextMeasurer(g2));
499: this.content.setLineAlignment(this.textAlignment);
500: Size2D contentSize = this.content.calculateDimensions(g2);
501: if (this.expandToFitSpace) {
502: return new Size2D(maxWidth, contentSize.getHeight());
503: }
504: else {
505: return contentSize;
506: }
507: }
508: else if (position == RectangleEdge.LEFT || position
509: == RectangleEdge.RIGHT) {
510: float maxWidth = (float) heightRange.getUpperBound();
511: g2.setFont(this.font);
512: this.content = TextUtilities.createTextBlock(this.text, this.font,
513: this.paint, maxWidth, new G2TextMeasurer(g2));
514: this.content.setLineAlignment(this.textAlignment);
515: Size2D contentSize = this.content.calculateDimensions(g2);
516:
517:
518: if (this.expandToFitSpace) {
519: return new Size2D(contentSize.getHeight(), maxWidth);
520: }
521: else {
522: return new Size2D(contentSize.height, contentSize.width);
523: }
524: }
525: else {
526: throw new RuntimeException("Unrecognised exception.");
527: }
528: }
529:
530:
537: public void draw(Graphics2D g2, Rectangle2D area) {
538: draw(g2, area, null);
539: }
540:
541:
553: public Object draw(Graphics2D g2, Rectangle2D area, Object params) {
554: if (this.content == null) {
555: return null;
556: }
557: area = trimMargin(area);
558: drawBorder(g2, area);
559: if (this.text.equals("")) {
560: return null;
561: }
562: ChartEntity entity = null;
563: if (params instanceof EntityBlockParams) {
564: EntityBlockParams p = (EntityBlockParams) params;
565: if (p.getGenerateEntities()) {
566: entity = new ChartEntity(area, this.toolTipText, this.urlText);
567: }
568: }
569: area = trimBorder(area);
570: if (this.backgroundPaint != null) {
571: g2.setPaint(this.backgroundPaint);
572: g2.fill(area);
573: }
574: area = trimPadding(area);
575: RectangleEdge position = getPosition();
576: if (position == RectangleEdge.TOP || position == RectangleEdge.BOTTOM) {
577: drawHorizontal(g2, area);
578: }
579: else if (position == RectangleEdge.LEFT
580: || position == RectangleEdge.RIGHT) {
581: drawVertical(g2, area);
582: }
583: BlockResult result = new BlockResult();
584: if (entity != null) {
585: StandardEntityCollection sec = new StandardEntityCollection();
586: sec.add(entity);
587: result.setEntityCollection(sec);
588: }
589: return result;
590: }
591:
592:
600: protected void drawHorizontal(Graphics2D g2, Rectangle2D area) {
601: Rectangle2D titleArea = (Rectangle2D) area.clone();
602: g2.setFont(this.font);
603: g2.setPaint(this.paint);
604: TextBlockAnchor anchor = null;
605: float x = 0.0f;
606: HorizontalAlignment horizontalAlignment = getHorizontalAlignment();
607: if (horizontalAlignment == HorizontalAlignment.LEFT) {
608: x = (float) titleArea.getX();
609: anchor = TextBlockAnchor.TOP_LEFT;
610: }
611: else if (horizontalAlignment == HorizontalAlignment.RIGHT) {
612: x = (float) titleArea.getMaxX();
613: anchor = TextBlockAnchor.TOP_RIGHT;
614: }
615: else if (horizontalAlignment == HorizontalAlignment.CENTER) {
616: x = (float) titleArea.getCenterX();
617: anchor = TextBlockAnchor.TOP_CENTER;
618: }
619: float y = 0.0f;
620: RectangleEdge position = getPosition();
621: if (position == RectangleEdge.TOP) {
622: y = (float) titleArea.getY();
623: }
624: else if (position == RectangleEdge.BOTTOM) {
625: y = (float) titleArea.getMaxY();
626: if (horizontalAlignment == HorizontalAlignment.LEFT) {
627: anchor = TextBlockAnchor.BOTTOM_LEFT;
628: }
629: else if (horizontalAlignment == HorizontalAlignment.CENTER) {
630: anchor = TextBlockAnchor.BOTTOM_CENTER;
631: }
632: else if (horizontalAlignment == HorizontalAlignment.RIGHT) {
633: anchor = TextBlockAnchor.BOTTOM_RIGHT;
634: }
635: }
636: this.content.draw(g2, x, y, anchor);
637: }
638:
639:
647: protected void drawVertical(Graphics2D g2, Rectangle2D area) {
648: Rectangle2D titleArea = (Rectangle2D) area.clone();
649: g2.setFont(this.font);
650: g2.setPaint(this.paint);
651: TextBlockAnchor anchor = null;
652: float y = 0.0f;
653: VerticalAlignment verticalAlignment = getVerticalAlignment();
654: if (verticalAlignment == VerticalAlignment.TOP) {
655: y = (float) titleArea.getY();
656: anchor = TextBlockAnchor.TOP_RIGHT;
657: }
658: else if (verticalAlignment == VerticalAlignment.BOTTOM) {
659: y = (float) titleArea.getMaxY();
660: anchor = TextBlockAnchor.TOP_LEFT;
661: }
662: else if (verticalAlignment == VerticalAlignment.CENTER) {
663: y = (float) titleArea.getCenterY();
664: anchor = TextBlockAnchor.TOP_CENTER;
665: }
666: float x = 0.0f;
667: RectangleEdge position = getPosition();
668: if (position == RectangleEdge.LEFT) {
669: x = (float) titleArea.getX();
670: }
671: else if (position == RectangleEdge.RIGHT) {
672: x = (float) titleArea.getMaxX();
673: if (verticalAlignment == VerticalAlignment.TOP) {
674: anchor = TextBlockAnchor.BOTTOM_RIGHT;
675: }
676: else if (verticalAlignment == VerticalAlignment.CENTER) {
677: anchor = TextBlockAnchor.BOTTOM_CENTER;
678: }
679: else if (verticalAlignment == VerticalAlignment.BOTTOM) {
680: anchor = TextBlockAnchor.BOTTOM_LEFT;
681: }
682: }
683: this.content.draw(g2, x, y, anchor, x, y, -Math.PI / 2.0);
684: }
685:
686:
693: public boolean equals(Object obj) {
694: if (obj == this) {
695: return true;
696: }
697: if (!(obj instanceof TextTitle)) {
698: return false;
699: }
700: if (!super.equals(obj)) {
701: return false;
702: }
703: TextTitle that = (TextTitle) obj;
704: if (!ObjectUtilities.equal(this.text, that.text)) {
705: return false;
706: }
707: if (!ObjectUtilities.equal(this.font, that.font)) {
708: return false;
709: }
710: if (!PaintUtilities.equal(this.paint, that.paint)) {
711: return false;
712: }
713: if (this.textAlignment != that.textAlignment) {
714: return false;
715: }
716: if (!PaintUtilities.equal(this.backgroundPaint, that.backgroundPaint)) {
717: return false;
718: }
719: return true;
720: }
721:
722:
727: public int hashCode() {
728: int result = super.hashCode();
729: result = 29 * result + (this.text != null ? this.text.hashCode() : 0);
730: result = 29 * result + (this.font != null ? this.font.hashCode() : 0);
731: result = 29 * result + (this.paint != null ? this.paint.hashCode() : 0);
732: result = 29 * result + (this.backgroundPaint != null
733: ? this.backgroundPaint.hashCode() : 0);
734: return result;
735: }
736:
737:
744: public Object clone() throws CloneNotSupportedException {
745: return super.clone();
746: }
747:
748:
755: private void writeObject(ObjectOutputStream stream) throws IOException {
756: stream.defaultWriteObject();
757: SerialUtilities.writePaint(this.paint, stream);
758: SerialUtilities.writePaint(this.backgroundPaint, stream);
759: }
760:
761:
769: private void readObject(ObjectInputStream stream)
770: throws IOException, ClassNotFoundException
771: {
772: stream.defaultReadObject();
773: this.paint = SerialUtilities.readPaint(stream);
774: this.backgroundPaint = SerialUtilities.readPaint(stream);
775: }
776:
777: }
778: