-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiLineString.java
More file actions
251 lines (220 loc) · 7.24 KB
/
MultiLineString.java
File metadata and controls
251 lines (220 loc) · 7.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
This class was originally written by Cay S. Horstmann in Violet
The following is the copyright information originally written by Cay
*/
/*
Violet - A program for editing UML diagrams.
Copyright (C) 2002 Cay S. Horstmann (http://horstmann.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
//package com.horstmann.violet.framework;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
import javax.swing.JLabel;
/**
* A string that can extend over multiple lines.
*/
public class MultiLineString implements Cloneable, Serializable {
/**
* Constructs an empty, centered, normal size multiline
* string that is not underlined.
*/
public MultiLineString() {
text = "";
justification = CENTER;
size = NORMAL;
underlined = false;
}
/**
* Sets the value of the text property.
*
* @param newValue the text of the multiline string
*/
public void setText(String newValue) {
text = newValue;
setLabelText();
}
/**
* Gets the value of the text property.
*
* @return the text of the multiline string
*/
public String getText() {
return text;
}
/**
* Sets the value of the justification property.
*
* @param newValue the justification, one of LEFT, CENTER,
* RIGHT
*/
public void setJustification(int newValue) {
justification = newValue;
setLabelText();
}
/**
* Gets the value of the justification property.
*
* @return the justification, one of LEFT, CENTER,
* RIGHT
*/
public int getJustification() {
return justification;
}
/**
* Gets the value of the underlined property.
*
* @return true if the text is underlined
*/
public boolean isUnderlined() {
return underlined;
}
/**
* Sets the value of the underlined property.
*
* @param newValue true to underline the text
*/
public void setUnderlined(boolean newValue) {
underlined = newValue;
setLabelText();
}
/**
* Sets the value of the size property.
*
* @param newValue the size, one of SMALL, NORMAL, LARGE
*/
public void setSize(int newValue) {
size = newValue;
setLabelText();
}
/**
* Gets the value of the size property.
*
* @return the size, one of SMALL, NORMAL, LARGE
*/
public int getSize() {
return size;
}
/**
* @return a toString representation of this object
*/
public String toString() {
return text.replace('\n', '|');
}
/**
* Internal method for setting the text with the way
* the parameters are currently set
*/
private void setLabelText() {
StringBuffer prefix = new StringBuffer();
StringBuffer suffix = new StringBuffer();
StringBuffer htmlText = new StringBuffer();
prefix.append(" ");
suffix.insert(0, " ");
if (underlined) {
prefix.append("<u>");
suffix.insert(0, "</u>");
}
if (size == LARGE) {
prefix.append("<font size=\"+1\">");
suffix.insert(0, "</font>");
}
if (size == SMALL) {
prefix.append("<font size=\"-2\">");
suffix.insert(0, "</font>");
}
htmlText.append("<html>");
StringTokenizer tokenizer = new StringTokenizer(text, "\n");
boolean first = true;
while (tokenizer.hasMoreTokens()) {
if (first) first = false;
else htmlText.append("<br>");
htmlText.append(prefix);
htmlText.append(tokenizer.nextToken());
htmlText.append(suffix);
}
htmlText.append("</html>");
// replace any < that are not followed by {u, i, b, tt, font, br} with <
List dontReplace = Arrays.asList(new String[]{"u", "i", "b", "tt", "font", "br"});
int ltpos = 0;
while (ltpos != -1) {
ltpos = htmlText.indexOf("<", ltpos + 1);
if (ltpos != -1 && !(ltpos + 1 < htmlText.length() && htmlText.charAt(ltpos + 1) == '/')) {
int end = ltpos + 1;
while (end < htmlText.length() && Character.isLetter(htmlText.charAt(end))) end++;
if (!dontReplace.contains(htmlText.substring(ltpos + 1, end)))
htmlText.replace(ltpos, ltpos + 1, "<");
}
}
label.setText(htmlText.toString());
if (justification == LEFT) label.setHorizontalAlignment(JLabel.LEFT);
else if (justification == CENTER) label.setHorizontalAlignment(JLabel.CENTER);
else if (justification == RIGHT) label.setHorizontalAlignment(JLabel.RIGHT);
}
/**
* Gets the bounding rectangle for this multiline string.
*
* @param g2 the graphics context
* @return the bounding rectangle (with top left corner (0,0))
*/
public Rectangle2D getBounds(Graphics2D g2) {
if (text.length() == 0) return new Rectangle2D.Double();
// setLabelText();
Dimension dim = label.getPreferredSize();
return new Rectangle2D.Double(0, 0, dim.getWidth(), dim.getHeight());
}
/**
* Draws this multiline string inside a given rectangle
*
* @param g2 the graphics context
* @param r the rectangle into which to place this multiline string
*/
public void draw(Graphics2D g2, Rectangle2D r) {
// setLabelText();
label.setFont(g2.getFont());
label.setBounds(0, 0, (int) r.getWidth(), (int) r.getHeight());
g2.translate(r.getX(), r.getY());
label.paint(g2);
g2.translate(-r.getX(), -r.getY());
}
/**
* @return a new MultiLineString with the same parameters as this
*/
public Object clone() {
try {
MultiLineString cloned = (MultiLineString) super.clone();
cloned.label = new JLabel();
cloned.setLabelText();
return cloned;
} catch (CloneNotSupportedException exception) {
return null;
}
}
public static final int LEFT = 0;
public static final int CENTER = 1;
public static final int RIGHT = 2;
public static final int LARGE = 3;
public static final int NORMAL = 4;
public static final int SMALL = 5;
private static final int GAP = 3;
private String text;
private int justification;
private int size;
private boolean underlined;
private transient JLabel label = new JLabel();
}