001 /*
002 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025 package javax.swing.text;
026
027 import java.awt.*;
028 import javax.swing.event.*;
029
030 /**
031 * A <code>LabelView</code> is a styled chunk of text
032 * that represents a view mapped over an element in the
033 * text model. It caches the character level attributes
034 * used for rendering.
035 *
036 * @author Timothy Prinzing
037 * @version 1.76 05/05/07
038 */
039 public class LabelView extends GlyphView implements TabableView {
040
041 /**
042 * Constructs a new view wrapped on an element.
043 *
044 * @param elem the element
045 */
046 public LabelView(Element elem) {
047 super (elem);
048 }
049
050 /**
051 * Synchronize the view's cached values with the model.
052 * This causes the font, metrics, color, etc to be
053 * re-cached if the cache has been invalidated.
054 */
055 final void sync() {
056 if (font == null) {
057 setPropertiesFromAttributes();
058 }
059 }
060
061 /**
062 * Sets whether or not the view is underlined.
063 * Note that this setter is protected and is really
064 * only meant if you need to update some additional
065 * state when set.
066 *
067 * @param u true if the view is underlined, otherwise
068 * false
069 * @see #isUnderline
070 */
071 protected void setUnderline(boolean u) {
072 underline = u;
073 }
074
075 /**
076 * Sets whether or not the view has a strike/line
077 * through it.
078 * Note that this setter is protected and is really
079 * only meant if you need to update some additional
080 * state when set.
081 *
082 * @param s true if the view has a strike/line
083 * through it, otherwise false
084 * @see #isStrikeThrough
085 */
086 protected void setStrikeThrough(boolean s) {
087 strike = s;
088 }
089
090 /**
091 * Sets whether or not the view represents a
092 * superscript.
093 * Note that this setter is protected and is really
094 * only meant if you need to update some additional
095 * state when set.
096 *
097 * @param s true if the view represents a
098 * superscript, otherwise false
099 * @see #isSuperscript
100 */
101 protected void setSuperscript(boolean s) {
102 super script = s;
103 }
104
105 /**
106 * Sets whether or not the view represents a
107 * subscript.
108 * Note that this setter is protected and is really
109 * only meant if you need to update some additional
110 * state when set.
111 *
112 * @param s true if the view represents a
113 * subscript, otherwise false
114 * @see #isSubscript
115 */
116 protected void setSubscript(boolean s) {
117 subscript = s;
118 }
119
120 /**
121 * Sets the background color for the view. This method is typically
122 * invoked as part of configuring this <code>View</code>. If you need
123 * to customize the background color you should override
124 * <code>setPropertiesFromAttributes</code> and invoke this method. A
125 * value of null indicates no background should be rendered, so that the
126 * background of the parent <code>View</code> will show through.
127 *
128 * @param bg background color, or null
129 * @see #setPropertiesFromAttributes
130 * @since 1.5
131 */
132 protected void setBackground(Color bg) {
133 this .bg = bg;
134 }
135
136 /**
137 * Sets the cached properties from the attributes.
138 */
139 protected void setPropertiesFromAttributes() {
140 AttributeSet attr = getAttributes();
141 if (attr != null) {
142 Document d = getDocument();
143 if (d instanceof StyledDocument) {
144 StyledDocument doc = (StyledDocument) d;
145 font = doc.getFont(attr);
146 fg = doc.getForeground(attr);
147 if (attr.isDefined(StyleConstants.Background)) {
148 bg = doc.getBackground(attr);
149 } else {
150 bg = null;
151 }
152 setUnderline(StyleConstants.isUnderline(attr));
153 setStrikeThrough(StyleConstants.isStrikeThrough(attr));
154 setSuperscript(StyleConstants.isSuperscript(attr));
155 setSubscript(StyleConstants.isSubscript(attr));
156 } else {
157 throw new StateInvariantError(
158 "LabelView needs StyledDocument");
159 }
160 }
161 }
162
163 /**
164 * Fetches the <code>FontMetrics</code> used for this view.
165 * @deprecated FontMetrics are not used for glyph rendering
166 * when running in the JDK.
167 */
168 @Deprecated
169 protected FontMetrics getFontMetrics() {
170 sync();
171 Container c = getContainer();
172 return (c != null) ? c.getFontMetrics(font) : Toolkit
173 .getDefaultToolkit().getFontMetrics(font);
174 }
175
176 /**
177 * Fetches the background color to use to render the glyphs.
178 * This is implemented to return a cached background color,
179 * which defaults to <code>null</code>.
180 *
181 * @return the cached background color
182 * @since 1.3
183 */
184 public Color getBackground() {
185 sync();
186 return bg;
187 }
188
189 /**
190 * Fetches the foreground color to use to render the glyphs.
191 * This is implemented to return a cached foreground color,
192 * which defaults to <code>null</code>.
193 *
194 * @return the cached foreground color
195 * @since 1.3
196 */
197 public Color getForeground() {
198 sync();
199 return fg;
200 }
201
202 /**
203 * Fetches the font that the glyphs should be based upon.
204 * This is implemented to return a cached font.
205 *
206 * @return the cached font
207 */
208 public Font getFont() {
209 sync();
210 return font;
211 }
212
213 /**
214 * Determines if the glyphs should be underlined. If true,
215 * an underline should be drawn through the baseline. This
216 * is implemented to return the cached underline property.
217 *
218 * <p>When you request this property, <code>LabelView</code>
219 * re-syncs its state with the properties of the
220 * <code>Element</code>'s <code>AttributeSet</code>.
221 * If <code>Element</code>'s <code>AttributeSet</code>
222 * does not have this property set, it will revert to false.
223 *
224 * @return the value of the cached
225 * <code>underline</code> property
226 * @since 1.3
227 */
228 public boolean isUnderline() {
229 sync();
230 return underline;
231 }
232
233 /**
234 * Determines if the glyphs should have a strikethrough
235 * line. If true, a line should be drawn through the center
236 * of the glyphs. This is implemented to return the
237 * cached <code>strikeThrough</code> property.
238 *
239 * <p>When you request this property, <code>LabelView</code>
240 * re-syncs its state with the properties of the
241 * <code>Element</code>'s <code>AttributeSet</code>.
242 * If <code>Element</code>'s <code>AttributeSet</code>
243 * does not have this property set, it will revert to false.
244 *
245 * @return the value of the cached
246 * <code>strikeThrough</code> property
247 * @since 1.3
248 */
249 public boolean isStrikeThrough() {
250 sync();
251 return strike;
252 }
253
254 /**
255 * Determines if the glyphs should be rendered as superscript.
256 * @return the value of the cached subscript property
257 *
258 * <p>When you request this property, <code>LabelView</code>
259 * re-syncs its state with the properties of the
260 * <code>Element</code>'s <code>AttributeSet</code>.
261 * If <code>Element</code>'s <code>AttributeSet</code>
262 * does not have this property set, it will revert to false.
263 *
264 * @return the value of the cached
265 * <code>subscript</code> property
266 * @since 1.3
267 */
268 public boolean isSubscript() {
269 sync();
270 return subscript;
271 }
272
273 /**
274 * Determines if the glyphs should be rendered as subscript.
275 *
276 * <p>When you request this property, <code>LabelView</code>
277 * re-syncs its state with the properties of the
278 * <code>Element</code>'s <code>AttributeSet</code>.
279 * If <code>Element</code>'s <code>AttributeSet</code>
280 * does not have this property set, it will revert to false.
281 *
282 * @return the value of the cached
283 * <code>superscript</code> property
284 * @since 1.3
285 */
286 public boolean isSuperscript() {
287 sync();
288 return super script;
289 }
290
291 // --- View methods ---------------------------------------------
292
293 /**
294 * Gives notification from the document that attributes were changed
295 * in a location that this view is responsible for.
296 *
297 * @param e the change information from the associated document
298 * @param a the current allocation of the view
299 * @param f the factory to use to rebuild if the view has children
300 * @see View#changedUpdate
301 */
302 public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
303 font = null;
304 super .changedUpdate(e, a, f);
305 }
306
307 // --- variables ------------------------------------------------
308
309 private Font font;
310 private Color fg;
311 private Color bg;
312 private boolean underline;
313 private boolean strike;
314 private boolean super script;
315 private boolean subscript;
316
317 }
|