public class MultiLineString implements Cloneable
1: import java.awt.Dimension;
2: import java.awt.Graphics2D;
3: import java.awt.geom.Rectangle2D;
4: import java.io.Serializable;
5: import java.util.StringTokenizer;
7: import javax.swing.JLabel;
9: /**
10: A string that can extend over multiple lines.
11: */
12: public class MultiLineString implements Cloneable, Serializable
13: {
14: /**
15: Constructs an empty, centered, normal size multiline
16: string that is not underlined.
17: */
18: public MultiLineString()
19: {
20: text = "";
21: justification = CENTER;
22: size = NORMAL;
23: underlined = false;
24: }
25: /**
26: Sets the value of the text property.
27: @param newValue the text of the multiline string
28: */
29: public void setText(String newValue) { text = newValue; }
30: /**
31: Gets the value of the text property.
32: @return the text of the multiline string
33: */
34: public String getText() { return text; }
35: /**
36: Sets the value of the justification property.
37: @param newValue the justification, one of LEFT, CENTER,
38: RIGHT
39: */
40: public void setJustification(int newValue) { justification = newValue; }
41: /**
42: Gets the value of the justification property.
43: @return the justification, one of LEFT, CENTER,
44: RIGHT
45: */
46: public int getJustification() { return justification; }
47: /**
48: Gets the value of the underlined property.
49: @return true if the text is underlined
50: */
51: public boolean isUnderlined() { return underlined; }
52: /**
53: Sets the value of the underlined property.
54: @param newValue true to underline the text
55: */
56: public void setUnderlined(boolean newValue) { underlined = newValue; }
57: /**
58: Sets the value of the size property.
59: @param newValue the size, one of SMALL, NORMAL, LARGE
60: */
61: public void setSize(int newValue) { size = newValue; }
62: /**
63: Gets the value of the size property.
64: @return the size, one of SMALL, NORMAL, LARGE
65: */
66: public int getSize() { return size; }
67:
68: public String toString()
69: {
70: return text.replace('\n', '|');
71: }
73: private void setLabelText()
74: {
75: StringBuffer prefix = new StringBuffer();
76: StringBuffer suffix = new StringBuffer();
77: StringBuffer htmlText = new StringBuffer();
78: prefix.append(" ");
79: suffix.insert(0, " ");
80: if (underlined)
81: {
82: prefix.append("<u>");
83: suffix.insert(0, "</u>");
84: }
85: if (size == LARGE)
86: {
87: prefix.append("<font size=\"+1\">");
88: suffix.insert(0, "</font>");
89: }
90: if (size == SMALL)
91: {
92: prefix.append("<font size=\"-1\">");
93: suffix.insert(0, "</font>");
94: }
95: htmlText.append("<html>");
96: StringTokenizer tokenizer = new StringTokenizer(text, "\n");
97: boolean first = true;
98: while (tokenizer.hasMoreTokens())
99: {
100: if (first) first = false; else htmlText.append("<br>");
101: htmlText.append(prefix);
102: htmlText.append(tokenizer.nextToken());
103: htmlText.append(suffix);
104: }
105: htmlText.append("</html>");
106: label.setText(htmlText.toString());
107: if (justification == LEFT) label.setHorizontalAlignment(JLabel.LEFT);
108: else if (justification == CENTER) label.setHorizontalAlignment(JLabel.CENTER);
109: else if (justification == RIGHT) label.setHorizontalAlignment(JLabel.RIGHT);
110: }
111:
112: /**
113: Gets the bounding rectangle for this multiline string.
114: @param g2 the graphics context
115: @return the bounding rectangle (with top left corner (0,0))
116: */
117: public Rectangle2D getBounds(Graphics2D g2)
118: {
119: if (text.length() == 0) return new Rectangle2D.Double();
120: setLabelText();
121: Dimension dim = label.getPreferredSize();
122: return new Rectangle2D.Double(0, 0, dim.getWidth(), dim.getHeight());
123: }
125: /**
126: Draws this multiline string inside a given rectangle
127: @param g2 the graphics context
128: @param r the rectangle into which to place this multiline string
129: */
130: public void draw(Graphics2D g2, Rectangle2D r)
131: {
132: setLabelText();
133: label.setFont(g2.getFont());
134: label.setBounds(0, 0, (int) r.getWidth(), (int) r.getHeight());
135: g2.translate(r.getX(), r.getY());
136: label.paint(g2);
137: g2.translate(-r.getX(), -r.getY());
138: }
140: public Object clone()
141: {
142: try
143: {
144: return super.clone();
145: }
146: catch (CloneNotSupportedException exception)
147: {
148: return null;
149: }
150: }
152: public static final int LEFT = 0;
153: public static final int CENTER = 1;
154: public static final int RIGHT = 2;
155: public static final int LARGE = 3;
156: public static final int NORMAL = 4;
157: public static final int SMALL = 5;
159: private static final int GAP = 3;
161: private String text;
162: private int justification;
163: private int size;
164: private boolean underlined;
165:
166: private static JLabel label = new JLabel();
167: }