KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > text > LabelView


1 /*
2  * @(#)LabelView.java 1.68 04/06/28
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing.text;
8
9 import java.awt.*;
10 import javax.swing.event.*;
11
12 /**
13  * A <code>LabelView</code> is a styled chunk of text
14  * that represents a view mapped over an element in the
15  * text model. It caches the character level attributes
16  * used for rendering.
17  *
18  * @author Timothy Prinzing
19  * @version 1.68 06/28/04
20  */

21 public class LabelView extends GlyphView JavaDoc implements TabableView JavaDoc {
22
23     /**
24      * Constructs a new view wrapped on an element.
25      *
26      * @param elem the element
27      */

28     public LabelView(Element JavaDoc elem) {
29     super(elem);
30     }
31
32     /**
33      * Synchronize the view's cached values with the model.
34      * This causes the font, metrics, color, etc to be
35      * re-cached if the cache has been invalidated.
36      */

37     final void sync() {
38     if (font == null) {
39         setPropertiesFromAttributes();
40     }
41     }
42     
43     /**
44      * Sets whether or not the view is underlined.
45      * Note that this setter is protected and is really
46      * only meant if you need to update some additional
47      * state when set.
48      *
49      * @param u true if the view is underlined, otherwise
50      * false
51      * @see #isUnderline
52      */

53     protected void setUnderline(boolean u) {
54     underline = u;
55     }
56
57     /**
58      * Sets whether or not the view has a strike/line
59      * through it.
60      * Note that this setter is protected and is really
61      * only meant if you need to update some additional
62      * state when set.
63      *
64      * @param s true if the view has a strike/line
65      * through it, otherwise false
66      * @see #isStrikeThrough
67      */

68     protected void setStrikeThrough(boolean s) {
69     strike = s;
70     }
71
72
73     /**
74      * Sets whether or not the view represents a
75      * superscript.
76      * Note that this setter is protected and is really
77      * only meant if you need to update some additional
78      * state when set.
79      *
80      * @param s true if the view represents a
81      * superscript, otherwise false
82      * @see #isSuperscript
83      */

84     protected void setSuperscript(boolean s) {
85     superscript = s;
86     }
87
88     /**
89      * Sets whether or not the view represents a
90      * subscript.
91      * Note that this setter is protected and is really
92      * only meant if you need to update some additional
93      * state when set.
94      *
95      * @param s true if the view represents a
96      * subscript, otherwise false
97      * @see #isSubscript
98      */

99     protected void setSubscript(boolean s) {
100     subscript = s;
101     }
102
103     /**
104      * Sets the background color for the view. This method is typically
105      * invoked as part of configuring this <code>View</code>. If you need
106      * to customize the background color you should override
107      * <code>setPropertiesFromAttributes</code> and invoke this method. A
108      * value of null indicates no background should be rendered, so that the
109      * background of the parent <code>View</code> will show through.
110      *
111      * @param bg background color, or null
112      * @see #setPropertiesFromAttributes
113      * @since 1.5
114      */

115     protected void setBackground(Color bg) {
116         this.bg = bg;
117     }
118     
119     /**
120      * Sets the cached properties from the attributes.
121      */

122     protected void setPropertiesFromAttributes() {
123     AttributeSet JavaDoc attr = getAttributes();
124     if (attr != null) {
125             Document JavaDoc d = getDocument();
126         if (d instanceof StyledDocument JavaDoc) {
127         StyledDocument JavaDoc doc = (StyledDocument JavaDoc) d;
128         font = doc.getFont(attr);
129         fg = doc.getForeground(attr);
130         if (attr.isDefined(StyleConstants.Background)) {
131             bg = doc.getBackground(attr);
132         } else {
133             bg = null;
134         }
135         setUnderline(StyleConstants.isUnderline(attr));
136         setStrikeThrough(StyleConstants.isStrikeThrough(attr));
137         setSuperscript(StyleConstants.isSuperscript(attr));
138         setSubscript(StyleConstants.isSubscript(attr));
139         } else {
140         throw new StateInvariantError JavaDoc("LabelView needs StyledDocument");
141         }
142     }
143      }
144
145     /**
146      * Fetches the <code>FontMetrics</code> used for this view.
147      * @deprecated FontMetrics are not used for glyph rendering
148      * when running in the JDK.
149      */

150     @Deprecated JavaDoc
151     protected FontMetrics getFontMetrics() {
152     sync();
153         Container c = getContainer();
154         return (c != null) ? c.getFontMetrics(font) :
155             Toolkit.getDefaultToolkit().getFontMetrics(font);
156     }
157
158     /**
159      * Fetches the background color to use to render the glyphs.
160      * This is implemented to return a cached background color,
161      * which defaults to <code>null</code>.
162      *
163      * @return the cached background color
164      */

165     public Color getBackground() {
166     sync();
167     return bg;
168     }
169
170     /**
171      * Fetches the foreground color to use to render the glyphs.
172      * This is implemented to return a cached foreground color,
173      * which defaults to <code>null</code>.
174      *
175      * @return the cached foreground color
176      */

177     public Color getForeground() {
178     sync();
179     return fg;
180     }
181
182     /**
183      * Fetches the font that the glyphs should be based upon.
184      * This is implemented to return a cached font.
185      *
186      * @return the cached font
187      */

188      public Font getFont() {
189     sync();
190     return font;
191     }
192
193     /**
194      * Determines if the glyphs should be underlined. If true,
195      * an underline should be drawn through the baseline. This
196      * is implemented to return the cached underline property.
197      *
198      * <p>When you request this property, <code>LabelView</code>
199      * re-syncs its state with the properties of the
200      * <code>Element</code>'s <code>AttributeSet</code>.
201      * If <code>Element</code>'s <code>AttributeSet</code>
202      * does not have this property set, it will revert to false.
203      *
204      * @return the value of the cached
205      * <code>underline</code> property
206      */

207     public boolean isUnderline() {
208     sync();
209     return underline;
210     }
211
212     /**
213      * Determines if the glyphs should have a strikethrough
214      * line. If true, a line should be drawn through the center
215      * of the glyphs. This is implemented to return the
216      * cached <code>strikeThrough</code> 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>strikeThrough</code> property
226      */

227     public boolean isStrikeThrough() {
228     sync();
229     return strike;
230     }
231
232     /**
233      * Determines if the glyphs should be rendered as superscript.
234      * @return the value of the cached subscript property
235      *
236      * <p>When you request this property, <code>LabelView</code>
237      * re-syncs its state with the properties of the
238      * <code>Element</code>'s <code>AttributeSet</code>.
239      * If <code>Element</code>'s <code>AttributeSet</code>
240      * does not have this property set, it will revert to false.
241      *
242      * @return the value of the cached
243      * <code>subscript</code> property
244      */

245     public boolean isSubscript() {
246     sync();
247     return subscript;
248     }
249
250     /**
251      * Determines if the glyphs should be rendered as subscript.
252      *
253      * <p>When you request this property, <code>LabelView</code>
254      * re-syncs its state with the properties of the
255      * <code>Element</code>'s <code>AttributeSet</code>.
256      * If <code>Element</code>'s <code>AttributeSet</code>
257      * does not have this property set, it will revert to false.
258      *
259      * @return the value of the cached
260      * <code>superscript</code> property
261      */

262     public boolean isSuperscript() {
263     sync();
264     return superscript;
265     }
266
267     // --- View methods ---------------------------------------------
268

269     /**
270      * Gives notification from the document that attributes were changed
271      * in a location that this view is responsible for.
272      *
273      * @param e the change information from the associated document
274      * @param a the current allocation of the view
275      * @param f the factory to use to rebuild if the view has children
276      * @see View#changedUpdate
277      */

278     public void changedUpdate(DocumentEvent e, Shape a, ViewFactory JavaDoc f) {
279     font = null;
280         super.changedUpdate(e, a, f);
281     }
282
283     // --- variables ------------------------------------------------
284

285     private Font font;
286     private Color fg;
287     private Color bg;
288     private boolean underline;
289     private boolean strike;
290     private boolean superscript;
291     private boolean subscript;
292
293 }
294
295
Popular Tags