1 7 package javax.swing.text; 8 9 import java.awt.*; 10 import javax.swing.event.*; 11 12 21 public class LabelView extends GlyphView implements TabableView { 22 23 28 public LabelView(Element elem) { 29 super(elem); 30 } 31 32 37 final void sync() { 38 if (font == null) { 39 setPropertiesFromAttributes(); 40 } 41 } 42 43 53 protected void setUnderline(boolean u) { 54 underline = u; 55 } 56 57 68 protected void setStrikeThrough(boolean s) { 69 strike = s; 70 } 71 72 73 84 protected void setSuperscript(boolean s) { 85 superscript = s; 86 } 87 88 99 protected void setSubscript(boolean s) { 100 subscript = s; 101 } 102 103 115 protected void setBackground(Color bg) { 116 this.bg = bg; 117 } 118 119 122 protected void setPropertiesFromAttributes() { 123 AttributeSet attr = getAttributes(); 124 if (attr != null) { 125 Document d = getDocument(); 126 if (d instanceof StyledDocument ) { 127 StyledDocument doc = (StyledDocument ) 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 ("LabelView needs StyledDocument"); 141 } 142 } 143 } 144 145 150 @Deprecated 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 165 public Color getBackground() { 166 sync(); 167 return bg; 168 } 169 170 177 public Color getForeground() { 178 sync(); 179 return fg; 180 } 181 182 188 public Font getFont() { 189 sync(); 190 return font; 191 } 192 193 207 public boolean isUnderline() { 208 sync(); 209 return underline; 210 } 211 212 227 public boolean isStrikeThrough() { 228 sync(); 229 return strike; 230 } 231 232 245 public boolean isSubscript() { 246 sync(); 247 return subscript; 248 } 249 250 262 public boolean isSuperscript() { 263 sync(); 264 return superscript; 265 } 266 267 269 278 public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) { 279 font = null; 280 super.changedUpdate(e, a, f); 281 } 282 283 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 |