38 |
|
|
39 |
package javax.swing.text; |
package javax.swing.text; |
40 |
|
|
41 |
// TODO: Implement this class. |
import java.awt.Color; |
42 |
public class LabelView |
import java.awt.Font; |
43 |
extends GlyphView |
import java.awt.FontMetrics; |
44 |
|
import java.awt.Shape; |
45 |
|
|
46 |
|
import javax.swing.event.DocumentEvent; |
47 |
|
|
48 |
|
/** |
49 |
|
* A {@link GlyphView} that caches the textattributes for most effective |
50 |
|
* rendering. |
51 |
|
* |
52 |
|
* @author Roman Kennke (kennke@aicas.com) |
53 |
|
*/ |
54 |
|
public class LabelView extends GlyphView |
55 |
{ |
{ |
56 |
|
|
57 |
|
/** |
58 |
|
* The background color. |
59 |
|
*/ |
60 |
|
Color background; |
61 |
|
|
62 |
|
/** |
63 |
|
* The foreground color. |
64 |
|
*/ |
65 |
|
Color foreground; |
66 |
|
|
67 |
|
/** |
68 |
|
* The background color. |
69 |
|
*/ |
70 |
|
Font font; |
71 |
|
|
72 |
|
/** |
73 |
|
* The strikethrough flag. |
74 |
|
*/ |
75 |
|
boolean strikeThrough; |
76 |
|
|
77 |
|
/** |
78 |
|
* The underline flag. |
79 |
|
*/ |
80 |
|
boolean underline; |
81 |
|
|
82 |
|
/** |
83 |
|
* The subscript flag. |
84 |
|
*/ |
85 |
|
boolean subscript; |
86 |
|
|
87 |
|
/** |
88 |
|
* The superscript flag. |
89 |
|
*/ |
90 |
|
boolean superscript; |
91 |
|
|
92 |
/** |
/** |
93 |
* Creates a new <code>GlyphView</code> for the given <code>Element</code>. |
* Creates a new <code>GlyphView</code> for the given <code>Element</code>. |
94 |
* |
* |
97 |
public LabelView(Element element) |
public LabelView(Element element) |
98 |
{ |
{ |
99 |
super(element); |
super(element); |
100 |
|
setPropertiesFromAttributes(); |
101 |
|
} |
102 |
|
|
103 |
|
/** |
104 |
|
* Loads the properties of this label view from the element's text |
105 |
|
* attributes. This method is called from the constructor and the |
106 |
|
* {@link #changedUpdate} method |
107 |
|
*/ |
108 |
|
protected void setPropertiesFromAttributes() |
109 |
|
{ |
110 |
|
Element el = getElement(); |
111 |
|
AttributeSet atts = el.getAttributes(); |
112 |
|
background = StyleConstants.getBackground(atts); |
113 |
|
foreground = StyleConstants.getForeground(atts); |
114 |
|
strikeThrough = StyleConstants.isStrikeThrough(atts); |
115 |
|
subscript = StyleConstants.isSubscript(atts); |
116 |
|
superscript = StyleConstants.isSuperscript(atts); |
117 |
|
underline = StyleConstants.isUnderline(atts); |
118 |
|
|
119 |
|
// Determine the font. |
120 |
|
String family = StyleConstants.getFontFamily(atts); |
121 |
|
int size = StyleConstants.getFontSize(atts); |
122 |
|
int style = Font.PLAIN; |
123 |
|
if (StyleConstants.isBold(atts)) |
124 |
|
style |= Font.BOLD; |
125 |
|
if (StyleConstants.isItalic(atts)) |
126 |
|
style |= Font.ITALIC; |
127 |
|
font = new Font(family, style, size); |
128 |
|
} |
129 |
|
|
130 |
|
/** |
131 |
|
* Receives notification when text attributes change in the chunk of |
132 |
|
* text that this view is responsible for. This simply calls |
133 |
|
* {@link #setPropertiesFromAttributes()}. |
134 |
|
* |
135 |
|
* @param e the document event |
136 |
|
* @param a the allocation of this view |
137 |
|
* @param vf the view factory to use for creating new views |
138 |
|
*/ |
139 |
|
public void changedUpdate(DocumentEvent e, Shape a, ViewFactory vf) |
140 |
|
{ |
141 |
|
setPropertiesFromAttributes(); |
142 |
|
} |
143 |
|
|
144 |
|
/** |
145 |
|
* Returns the background color for the glyphs. |
146 |
|
* |
147 |
|
* @return the background color for the glyphs |
148 |
|
*/ |
149 |
|
public Color getBackground() |
150 |
|
{ |
151 |
|
return background; |
152 |
|
} |
153 |
|
|
154 |
|
/** |
155 |
|
* Sets the background color for the glyphs. A value of <code>null</code> |
156 |
|
* means the background of the parent view should shine through. |
157 |
|
* |
158 |
|
* @param bg the background to set or <code>null</code> |
159 |
|
* |
160 |
|
* @since 1.5 |
161 |
|
*/ |
162 |
|
protected void setBackground(Color bg) |
163 |
|
{ |
164 |
|
background = bg; |
165 |
|
} |
166 |
|
|
167 |
|
/** |
168 |
|
* Returns the foreground color for the glyphs. |
169 |
|
* |
170 |
|
* @return the foreground color for the glyphs |
171 |
|
*/ |
172 |
|
public Color getForeground() |
173 |
|
{ |
174 |
|
return foreground; |
175 |
|
} |
176 |
|
|
177 |
|
/** |
178 |
|
* Returns the font for the glyphs. |
179 |
|
* |
180 |
|
* @return the font for the glyphs |
181 |
|
*/ |
182 |
|
public Font getFont() |
183 |
|
{ |
184 |
|
return font; |
185 |
|
} |
186 |
|
|
187 |
|
/** |
188 |
|
* Returns the font metrics of the current font. |
189 |
|
* |
190 |
|
* @return the font metrics of the current font |
191 |
|
* |
192 |
|
* @deprecated this is not used anymore |
193 |
|
*/ |
194 |
|
protected FontMetrics getFontMetrics() |
195 |
|
{ |
196 |
|
return getContainer().getGraphics().getFontMetrics(font); |
197 |
|
} |
198 |
|
|
199 |
|
/** |
200 |
|
* Returns <code>true</code> if the glyphs are rendered underlined, |
201 |
|
* <code>false</code> otherwise. |
202 |
|
* |
203 |
|
* @return <code>true</code> if the glyphs are rendered underlined, |
204 |
|
* <code>false</code> otherwise |
205 |
|
*/ |
206 |
|
public boolean isUnderline() |
207 |
|
{ |
208 |
|
return underline; |
209 |
|
} |
210 |
|
|
211 |
|
/** |
212 |
|
* Sets the underline flag. |
213 |
|
* |
214 |
|
* @param flag <code>true</code> if the glyphs are rendered underlined, |
215 |
|
* <code>false</code> otherwise |
216 |
|
*/ |
217 |
|
protected void setUnderline(boolean flag) |
218 |
|
{ |
219 |
|
underline = flag; |
220 |
|
} |
221 |
|
|
222 |
|
/** |
223 |
|
* Returns <code>true</code> if the glyphs are rendered as subscript, |
224 |
|
* <code>false</code> otherwise. |
225 |
|
* |
226 |
|
* @return <code>true</code> if the glyphs are rendered as subscript, |
227 |
|
* <code>false</code> otherwise |
228 |
|
*/ |
229 |
|
public boolean isSubscript() |
230 |
|
{ |
231 |
|
return subscript; |
232 |
|
} |
233 |
|
|
234 |
|
/** |
235 |
|
* Sets the subscript flag. |
236 |
|
* |
237 |
|
* @param flag <code>true</code> if the glyphs are rendered as subscript, |
238 |
|
* <code>false</code> otherwise |
239 |
|
*/ |
240 |
|
protected void setSubscript(boolean flag) |
241 |
|
{ |
242 |
|
subscript = flag; |
243 |
|
} |
244 |
|
|
245 |
|
/** |
246 |
|
* Returns <code>true</code> if the glyphs are rendered as superscript, |
247 |
|
* <code>false</code> otherwise. |
248 |
|
* |
249 |
|
* @return <code>true</code> if the glyphs are rendered as superscript, |
250 |
|
* <code>false</code> otherwise |
251 |
|
*/ |
252 |
|
public boolean isSuperscript() |
253 |
|
{ |
254 |
|
return superscript; |
255 |
|
} |
256 |
|
|
257 |
|
/** |
258 |
|
* Sets the superscript flag. |
259 |
|
* |
260 |
|
* @param flag <code>true</code> if the glyphs are rendered as superscript, |
261 |
|
* <code>false</code> otherwise |
262 |
|
*/ |
263 |
|
protected void setSuperscript(boolean flag) |
264 |
|
{ |
265 |
|
superscript = flag; |
266 |
|
} |
267 |
|
|
268 |
|
/** |
269 |
|
* Returns <code>true</code> if the glyphs are rendered strike-through, |
270 |
|
* <code>false</code> otherwise. |
271 |
|
* |
272 |
|
* @return <code>true</code> if the glyphs are rendered strike-through, |
273 |
|
* <code>false</code> otherwise |
274 |
|
*/ |
275 |
|
public boolean isStrikeThrough() |
276 |
|
{ |
277 |
|
return strikeThrough; |
278 |
|
} |
279 |
|
|
280 |
|
/** |
281 |
|
* Sets the strike-through flag. |
282 |
|
* |
283 |
|
* @param flag <code>true</code> if the glyphs are rendered strike-through, |
284 |
|
* <code>false</code> otherwise |
285 |
|
*/ |
286 |
|
protected void setStrikeThrough(boolean flag) |
287 |
|
{ |
288 |
|
strikeThrough = flag; |
289 |
} |
} |
290 |
} |
} |