1:
81:
82: package ;
83:
84: import ;
85: import ;
86: import ;
87: import ;
88: import ;
89: import ;
90: import ;
91: import ;
92: import ;
93:
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: import ;
114: import ;
115: import ;
116:
117:
121: public class TextTitle extends Title
122: implements Serializable, Cloneable, PublicCloneable {
123:
124:
125: private static final long serialVersionUID = 8372008692127477443L;
126:
127:
128: public static final Font DEFAULT_FONT = new Font("SansSerif", Font.BOLD,
129: 12);
130:
131:
132: public static final Paint DEFAULT_TEXT_PAINT = Color.black;
133:
134:
135: private String text;
136:
137:
138: private Font font;
139:
140:
141: private HorizontalAlignment textAlignment;
142:
143:
144: private transient Paint paint;
145:
146:
147: private transient Paint backgroundPaint;
148:
149:
150: private String toolTipText;
151:
152:
153: private String urlText;
154:
155:
156: private TextBlock content;
157:
158:
162: private boolean expandToFitSpace = false;
163:
164:
169: private int maximumLinesToDisplay = Integer.MAX_VALUE;
170:
171:
174: public TextTitle() {
175: this("");
176: }
177:
178:
183: public TextTitle(String text) {
184: this(text, TextTitle.DEFAULT_FONT, TextTitle.DEFAULT_TEXT_PAINT,
185: Title.DEFAULT_POSITION, Title.DEFAULT_HORIZONTAL_ALIGNMENT,
186: Title.DEFAULT_VERTICAL_ALIGNMENT, Title.DEFAULT_PADDING);
187: }
188:
189:
195: public TextTitle(String text, Font font) {
196: this(text, font, TextTitle.DEFAULT_TEXT_PAINT, Title.DEFAULT_POSITION,
197: Title.DEFAULT_HORIZONTAL_ALIGNMENT,
198: Title.DEFAULT_VERTICAL_ALIGNMENT, Title.DEFAULT_PADDING);
199: }
200:
201:
214: public TextTitle(String text, Font font, Paint paint,
215: RectangleEdge position,
216: HorizontalAlignment horizontalAlignment,
217: VerticalAlignment verticalAlignment,
218: RectangleInsets padding) {
219:
220: super(position, horizontalAlignment, verticalAlignment, padding);
221:
222: if (text == null) {
223: throw new NullPointerException("Null 'text' argument.");
224: }
225: if (font == null) {
226: throw new NullPointerException("Null 'font' argument.");
227: }
228: if (paint == null) {
229: throw new NullPointerException("Null 'paint' argument.");
230: }
231: this.text = text;
232: this.font = font;
233: this.paint = paint;
234:
235:
236:
237: this.textAlignment = horizontalAlignment;
238: this.backgroundPaint = null;
239: this.content = null;
240: this.toolTipText = null;
241: this.urlText = null;
242:
243: }
244:
245:
252: public String getText() {
253: return this.text;
254: }
255:
256:
262: public void setText(String text) {
263: if (text == null) {
264: throw new IllegalArgumentException("Null 'text' argument.");
265: }
266: if (!this.text.equals(text)) {
267: this.text = text;
268: notifyListeners(new TitleChangeEvent(this));
269: }
270: }
271:
272:
280: public HorizontalAlignment getTextAlignment() {
281: return this.textAlignment;
282: }
283:
284:
290: public void setTextAlignment(HorizontalAlignment alignment) {
291: if (alignment == null) {
292: throw new IllegalArgumentException("Null 'alignment' argument.");
293: }
294: this.textAlignment = alignment;
295: notifyListeners(new TitleChangeEvent(this));
296: }
297:
298:
305: public Font getFont() {
306: return this.font;
307: }
308:
309:
317: public void setFont(Font font) {
318: if (font == null) {
319: throw new IllegalArgumentException("Null 'font' argument.");
320: }
321: if (!this.font.equals(font)) {
322: this.font = font;
323: notifyListeners(new TitleChangeEvent(this));
324: }
325: }
326:
327:
334: public Paint getPaint() {
335: return this.paint;
336: }
337:
338:
346: public void setPaint(Paint paint) {
347: if (paint == null) {
348: throw new IllegalArgumentException("Null 'paint' argument.");
349: }
350: if (!this.paint.equals(paint)) {
351: this.paint = paint;
352: notifyListeners(new TitleChangeEvent(this));
353: }
354: }
355:
356:
361: public Paint getBackgroundPaint() {
362: return this.backgroundPaint;
363: }
364:
365:
372: public void setBackgroundPaint(Paint paint) {
373: this.backgroundPaint = paint;
374: notifyListeners(new TitleChangeEvent(this));
375: }
376:
377:
382: public String getToolTipText() {
383: return this.toolTipText;
384: }
385:
386:
392: public void setToolTipText(String text) {
393: this.toolTipText = text;
394: notifyListeners(new TitleChangeEvent(this));
395: }
396:
397:
402: public String getURLText() {
403: return this.urlText;
404: }
405:
406:
412: public void setURLText(String text) {
413: this.urlText = text;
414: notifyListeners(new TitleChangeEvent(this));
415: }
416:
417:
423: public boolean getExpandToFitSpace() {
424: return this.expandToFitSpace;
425: }
426:
427:
434: public void setExpandToFitSpace(boolean expand) {
435: this.expandToFitSpace = expand;
436: notifyListeners(new TitleChangeEvent(this));
437: }
438:
439:
448: public int getMaximumLinesToDisplay() {
449: return this.maximumLinesToDisplay;
450: }
451:
452:
462: public void setMaximumLinesToDisplay(int max) {
463: this.maximumLinesToDisplay = max;
464: notifyListeners(new TitleChangeEvent(this));
465: }
466:
467:
476: public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) {
477: RectangleConstraint cc = toContentConstraint(constraint);
478: LengthConstraintType w = cc.getWidthConstraintType();
479: LengthConstraintType h = cc.getHeightConstraintType();
480: Size2D contentSize = null;
481: if (w == LengthConstraintType.NONE) {
482: if (h == LengthConstraintType.NONE) {
483: contentSize = arrangeNN(g2);
484: }
485: else if (h == LengthConstraintType.RANGE) {
486: throw new RuntimeException("Not yet implemented.");
487: }
488: else if (h == LengthConstraintType.FIXED) {
489: throw new RuntimeException("Not yet implemented.");
490: }
491: }
492: else if (w == LengthConstraintType.RANGE) {
493: if (h == LengthConstraintType.NONE) {
494: contentSize = arrangeRN(g2, cc.getWidthRange());
495: }
496: else if (h == LengthConstraintType.RANGE) {
497: contentSize = arrangeRR(g2, cc.getWidthRange(),
498: cc.getHeightRange());
499: }
500: else if (h == LengthConstraintType.FIXED) {
501: throw new RuntimeException("Not yet implemented.");
502: }
503: }
504: else if (w == LengthConstraintType.FIXED) {
505: if (h == LengthConstraintType.NONE) {
506: contentSize = arrangeFN(g2, cc.getWidth());
507: }
508: else if (h == LengthConstraintType.RANGE) {
509: throw new RuntimeException("Not yet implemented.");
510: }
511: else if (h == LengthConstraintType.FIXED) {
512: throw new RuntimeException("Not yet implemented.");
513: }
514: }
515: return new Size2D(calculateTotalWidth(contentSize.getWidth()),
516: calculateTotalHeight(contentSize.getHeight()));
517: }
518:
519:
531: protected Size2D arrangeNN(Graphics2D g2) {
532: Range max = new Range(0.0, Float.MAX_VALUE);
533: return arrangeRR(g2, max, max);
534: }
535:
536:
549: protected Size2D arrangeFN(Graphics2D g2, double w) {
550: RectangleEdge position = getPosition();
551: if (position == RectangleEdge.TOP || position == RectangleEdge.BOTTOM) {
552: float maxWidth = (float) w;
553: g2.setFont(this.font);
554: this.content = TextUtilities.createTextBlock(this.text, this.font,
555: this.paint, maxWidth, this.maximumLinesToDisplay,
556: new G2TextMeasurer(g2));
557: this.content.setLineAlignment(this.textAlignment);
558: Size2D contentSize = this.content.calculateDimensions(g2);
559: if (this.expandToFitSpace) {
560: return new Size2D(maxWidth, contentSize.getHeight());
561: }
562: else {
563: return contentSize;
564: }
565: }
566: else if (position == RectangleEdge.LEFT || position
567: == RectangleEdge.RIGHT) {
568: float maxWidth = Float.MAX_VALUE;
569: g2.setFont(this.font);
570: this.content = TextUtilities.createTextBlock(this.text, this.font,
571: this.paint, maxWidth, this.maximumLinesToDisplay,
572: new G2TextMeasurer(g2));
573: this.content.setLineAlignment(this.textAlignment);
574: Size2D contentSize = this.content.calculateDimensions(g2);
575:
576:
577: if (this.expandToFitSpace) {
578: return new Size2D(contentSize.getHeight(), maxWidth);
579: }
580: else {
581: return new Size2D(contentSize.height, contentSize.width);
582: }
583: }
584: else {
585: throw new RuntimeException("Unrecognised exception.");
586: }
587: }
588:
589:
602: protected Size2D arrangeRN(Graphics2D g2, Range widthRange) {
603: Size2D s = arrangeNN(g2);
604: if (widthRange.contains(s.getWidth())) {
605: return s;
606: }
607: double ww = widthRange.constrain(s.getWidth());
608: return arrangeFN(g2, ww);
609: }
610:
611:
622: protected Size2D arrangeRR(Graphics2D g2, Range widthRange,
623: Range heightRange) {
624: RectangleEdge position = getPosition();
625: if (position == RectangleEdge.TOP || position == RectangleEdge.BOTTOM) {
626: float maxWidth = (float) widthRange.getUpperBound();
627: g2.setFont(this.font);
628: this.content = TextUtilities.createTextBlock(this.text, this.font,
629: this.paint, maxWidth, this.maximumLinesToDisplay,
630: new G2TextMeasurer(g2));
631: this.content.setLineAlignment(this.textAlignment);
632: Size2D contentSize = this.content.calculateDimensions(g2);
633: if (this.expandToFitSpace) {
634: return new Size2D(maxWidth, contentSize.getHeight());
635: }
636: else {
637: return contentSize;
638: }
639: }
640: else if (position == RectangleEdge.LEFT || position
641: == RectangleEdge.RIGHT) {
642: float maxWidth = (float) heightRange.getUpperBound();
643: g2.setFont(this.font);
644: this.content = TextUtilities.createTextBlock(this.text, this.font,
645: this.paint, maxWidth, this.maximumLinesToDisplay,
646: new G2TextMeasurer(g2));
647: this.content.setLineAlignment(this.textAlignment);
648: Size2D contentSize = this.content.calculateDimensions(g2);
649:
650:
651: if (this.expandToFitSpace) {
652: return new Size2D(contentSize.getHeight(), maxWidth);
653: }
654: else {
655: return new Size2D(contentSize.height, contentSize.width);
656: }
657: }
658: else {
659: throw new RuntimeException("Unrecognised exception.");
660: }
661: }
662:
663:
670: public void draw(Graphics2D g2, Rectangle2D area) {
671: draw(g2, area, null);
672: }
673:
674:
686: public Object draw(Graphics2D g2, Rectangle2D area, Object params) {
687: if (this.content == null) {
688: return null;
689: }
690: area = trimMargin(area);
691: drawBorder(g2, area);
692: if (this.text.equals("")) {
693: return null;
694: }
695: ChartEntity entity = null;
696: if (params instanceof EntityBlockParams) {
697: EntityBlockParams p = (EntityBlockParams) params;
698: if (p.getGenerateEntities()) {
699: entity = new ChartEntity(area, this.toolTipText, this.urlText);
700: }
701: }
702: area = trimBorder(area);
703: if (this.backgroundPaint != null) {
704: g2.setPaint(this.backgroundPaint);
705: g2.fill(area);
706: }
707: area = trimPadding(area);
708: RectangleEdge position = getPosition();
709: if (position == RectangleEdge.TOP || position == RectangleEdge.BOTTOM) {
710: drawHorizontal(g2, area);
711: }
712: else if (position == RectangleEdge.LEFT
713: || position == RectangleEdge.RIGHT) {
714: drawVertical(g2, area);
715: }
716: BlockResult result = new BlockResult();
717: if (entity != null) {
718: StandardEntityCollection sec = new StandardEntityCollection();
719: sec.add(entity);
720: result.setEntityCollection(sec);
721: }
722: return result;
723: }
724:
725:
733: protected void drawHorizontal(Graphics2D g2, Rectangle2D area) {
734: Rectangle2D titleArea = (Rectangle2D) area.clone();
735: g2.setFont(this.font);
736: g2.setPaint(this.paint);
737: TextBlockAnchor anchor = null;
738: float x = 0.0f;
739: HorizontalAlignment horizontalAlignment = getHorizontalAlignment();
740: if (horizontalAlignment == HorizontalAlignment.LEFT) {
741: x = (float) titleArea.getX();
742: anchor = TextBlockAnchor.TOP_LEFT;
743: }
744: else if (horizontalAlignment == HorizontalAlignment.RIGHT) {
745: x = (float) titleArea.getMaxX();
746: anchor = TextBlockAnchor.TOP_RIGHT;
747: }
748: else if (horizontalAlignment == HorizontalAlignment.CENTER) {
749: x = (float) titleArea.getCenterX();
750: anchor = TextBlockAnchor.TOP_CENTER;
751: }
752: float y = 0.0f;
753: RectangleEdge position = getPosition();
754: if (position == RectangleEdge.TOP) {
755: y = (float) titleArea.getY();
756: }
757: else if (position == RectangleEdge.BOTTOM) {
758: y = (float) titleArea.getMaxY();
759: if (horizontalAlignment == HorizontalAlignment.LEFT) {
760: anchor = TextBlockAnchor.BOTTOM_LEFT;
761: }
762: else if (horizontalAlignment == HorizontalAlignment.CENTER) {
763: anchor = TextBlockAnchor.BOTTOM_CENTER;
764: }
765: else if (horizontalAlignment == HorizontalAlignment.RIGHT) {
766: anchor = TextBlockAnchor.BOTTOM_RIGHT;
767: }
768: }
769: this.content.draw(g2, x, y, anchor);
770: }
771:
772:
780: protected void drawVertical(Graphics2D g2, Rectangle2D area) {
781: Rectangle2D titleArea = (Rectangle2D) area.clone();
782: g2.setFont(this.font);
783: g2.setPaint(this.paint);
784: TextBlockAnchor anchor = null;
785: float y = 0.0f;
786: VerticalAlignment verticalAlignment = getVerticalAlignment();
787: if (verticalAlignment == VerticalAlignment.TOP) {
788: y = (float) titleArea.getY();
789: anchor = TextBlockAnchor.TOP_RIGHT;
790: }
791: else if (verticalAlignment == VerticalAlignment.BOTTOM) {
792: y = (float) titleArea.getMaxY();
793: anchor = TextBlockAnchor.TOP_LEFT;
794: }
795: else if (verticalAlignment == VerticalAlignment.CENTER) {
796: y = (float) titleArea.getCenterY();
797: anchor = TextBlockAnchor.TOP_CENTER;
798: }
799: float x = 0.0f;
800: RectangleEdge position = getPosition();
801: if (position == RectangleEdge.LEFT) {
802: x = (float) titleArea.getX();
803: }
804: else if (position == RectangleEdge.RIGHT) {
805: x = (float) titleArea.getMaxX();
806: if (verticalAlignment == VerticalAlignment.TOP) {
807: anchor = TextBlockAnchor.BOTTOM_RIGHT;
808: }
809: else if (verticalAlignment == VerticalAlignment.CENTER) {
810: anchor = TextBlockAnchor.BOTTOM_CENTER;
811: }
812: else if (verticalAlignment == VerticalAlignment.BOTTOM) {
813: anchor = TextBlockAnchor.BOTTOM_LEFT;
814: }
815: }
816: this.content.draw(g2, x, y, anchor, x, y, -Math.PI / 2.0);
817: }
818:
819:
826: public boolean equals(Object obj) {
827: if (obj == this) {
828: return true;
829: }
830: if (!(obj instanceof TextTitle)) {
831: return false;
832: }
833: TextTitle that = (TextTitle) obj;
834: if (!ObjectUtilities.equal(this.text, that.text)) {
835: return false;
836: }
837: if (!ObjectUtilities.equal(this.font, that.font)) {
838: return false;
839: }
840: if (!PaintUtilities.equal(this.paint, that.paint)) {
841: return false;
842: }
843: if (this.textAlignment != that.textAlignment) {
844: return false;
845: }
846: if (!PaintUtilities.equal(this.backgroundPaint, that.backgroundPaint)) {
847: return false;
848: }
849: if (this.maximumLinesToDisplay != that.maximumLinesToDisplay) {
850: return false;
851: }
852: if (this.expandToFitSpace != that.expandToFitSpace) {
853: return false;
854: }
855: if (!ObjectUtilities.equal(this.toolTipText, that.toolTipText)) {
856: return false;
857: }
858: if (!ObjectUtilities.equal(this.urlText, that.urlText)) {
859: return false;
860: }
861: return super.equals(obj);
862: }
863:
864:
869: public int hashCode() {
870: int result = super.hashCode();
871: result = 29 * result + (this.text != null ? this.text.hashCode() : 0);
872: result = 29 * result + (this.font != null ? this.font.hashCode() : 0);
873: result = 29 * result + (this.paint != null ? this.paint.hashCode() : 0);
874: result = 29 * result + (this.backgroundPaint != null
875: ? this.backgroundPaint.hashCode() : 0);
876: return result;
877: }
878:
879:
886: public Object clone() throws CloneNotSupportedException {
887: return super.clone();
888: }
889:
890:
897: private void writeObject(ObjectOutputStream stream) throws IOException {
898: stream.defaultWriteObject();
899: SerialUtilities.writePaint(this.paint, stream);
900: SerialUtilities.writePaint(this.backgroundPaint, stream);
901: }
902:
903:
911: private void readObject(ObjectInputStream stream)
912: throws IOException, ClassNotFoundException {
913: stream.defaultReadObject();
914: this.paint = SerialUtilities.readPaint(stream);
915: this.backgroundPaint = SerialUtilities.readPaint(stream);
916: }
917:
918: }
919: