1. /*
  2. * @(#)LabelView.java 1.62 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.text;
  8. import java.awt.*;
  9. import javax.swing.event.*;
  10. /**
  11. * A <code>LabelView</code> is a styled chunk of text
  12. * that represents a view mapped over an element in the
  13. * text model. It caches the character level attributes
  14. * used for rendering.
  15. *
  16. * @author Timothy Prinzing
  17. * @version 1.62 01/23/03
  18. */
  19. public class LabelView extends GlyphView implements TabableView {
  20. /**
  21. * Constructs a new view wrapped on an element.
  22. *
  23. * @param elem the element
  24. */
  25. public LabelView(Element elem) {
  26. super(elem);
  27. }
  28. /**
  29. * Synchronize the view's cached values with the model.
  30. * This causes the font, metrics, color, etc to be
  31. * re-cached if the cache has been invalidated.
  32. */
  33. final void sync() {
  34. if (font == null) {
  35. setPropertiesFromAttributes();
  36. }
  37. }
  38. /**
  39. * Sets whether or not the view is underlined.
  40. * Note that this setter is protected and is really
  41. * only meant if you need to update some additional
  42. * state when set.
  43. *
  44. * @param u true if the view is underlined, otherwise
  45. * false
  46. * @see #isUnderline
  47. */
  48. protected void setUnderline(boolean u) {
  49. underline = u;
  50. }
  51. /**
  52. * Sets whether or not the view has a strike/line
  53. * through it.
  54. * Note that this setter is protected and is really
  55. * only meant if you need to update some additional
  56. * state when set.
  57. *
  58. * @param s true if the view has a strike/line
  59. * through it, otherwise false
  60. * @see #isStrikeThrough
  61. */
  62. protected void setStrikeThrough(boolean s) {
  63. strike = s;
  64. }
  65. /**
  66. * Sets whether or not the view represents a
  67. * superscript.
  68. * Note that this setter is protected and is really
  69. * only meant if you need to update some additional
  70. * state when set.
  71. *
  72. * @param s true if the view represents a
  73. * superscript, otherwise false
  74. * @see #isSuperscript
  75. */
  76. protected void setSuperscript(boolean s) {
  77. superscript = s;
  78. }
  79. /**
  80. * Sets whether or not the view represents a
  81. * subscript.
  82. * Note that this setter is protected and is really
  83. * only meant if you need to update some additional
  84. * state when set.
  85. *
  86. * @param s true if the view represents a
  87. * subscript, otherwise false
  88. * @see #isSubscript
  89. */
  90. protected void setSubscript(boolean s) {
  91. subscript = s;
  92. }
  93. /**
  94. * Sets the cached properties from the attributes.
  95. */
  96. protected void setPropertiesFromAttributes() {
  97. AttributeSet attr = getAttributes();
  98. if (attr != null) {
  99. Document d = getDocument();
  100. if (d instanceof StyledDocument) {
  101. StyledDocument doc = (StyledDocument) d;
  102. font = doc.getFont(attr);
  103. fg = doc.getForeground(attr);
  104. if (attr.isDefined(StyleConstants.Background)) {
  105. bg = doc.getBackground(attr);
  106. } else {
  107. bg = null;
  108. }
  109. setUnderline(StyleConstants.isUnderline(attr));
  110. setStrikeThrough(StyleConstants.isStrikeThrough(attr));
  111. setSuperscript(StyleConstants.isSuperscript(attr));
  112. setSubscript(StyleConstants.isSubscript(attr));
  113. } else {
  114. throw new StateInvariantError("LabelView needs StyledDocument");
  115. }
  116. }
  117. }
  118. /**
  119. * Fetches the <code>FontMetrics</code> used for this view.
  120. * @deprecated FontMetrics are not used for glyph rendering
  121. * when running in the Java2 SDK.
  122. */
  123. protected FontMetrics getFontMetrics() {
  124. sync();
  125. return Toolkit.getDefaultToolkit().getFontMetrics(font);
  126. }
  127. /**
  128. * Fetches the background color to use to render the glyphs.
  129. * This is implemented to return a cached background color,
  130. * which defaults to <code>null</code>.
  131. *
  132. * @return the cached background color
  133. */
  134. public Color getBackground() {
  135. sync();
  136. return bg;
  137. }
  138. /**
  139. * Fetches the foreground color to use to render the glyphs.
  140. * This is implemented to return a cached foreground color,
  141. * which defaults to <code>null</code>.
  142. *
  143. * @return the cached foreground color
  144. */
  145. public Color getForeground() {
  146. sync();
  147. return fg;
  148. }
  149. /**
  150. * Fetches the font that the glyphs should be based upon.
  151. * This is implemented to return a cached font.
  152. *
  153. * @return the cached font
  154. */
  155. public Font getFont() {
  156. sync();
  157. return font;
  158. }
  159. /**
  160. * Determines if the glyphs should be underlined. If true,
  161. * an underline should be drawn through the baseline. This
  162. * is implemented to return the cached underline property.
  163. *
  164. * <p>When you request this property, <code>LabelView</code>
  165. * re-syncs its state with the properties of the
  166. * <code>Element</code>'s <code>AttributeSet</code>.
  167. * If <code>Element</code>'s <code>AttributeSet</code>
  168. * does not have this property set, it will revert to false.
  169. *
  170. * @return the value of the cached
  171. * <code>underline</code> property
  172. */
  173. public boolean isUnderline() {
  174. sync();
  175. return underline;
  176. }
  177. /**
  178. * Determines if the glyphs should have a strikethrough
  179. * line. If true, a line should be drawn through the center
  180. * of the glyphs. This is implemented to return the
  181. * cached <code>strikeThrough</code> property.
  182. *
  183. * <p>When you request this property, <code>LabelView</code>
  184. * re-syncs its state with the properties of the
  185. * <code>Element</code>'s <code>AttributeSet</code>.
  186. * If <code>Element</code>'s <code>AttributeSet</code>
  187. * does not have this property set, it will revert to false.
  188. *
  189. * @return the value of the cached
  190. * <code>strikeThrough</code> property
  191. */
  192. public boolean isStrikeThrough() {
  193. sync();
  194. return strike;
  195. }
  196. /**
  197. * Determines if the glyphs should be rendered as superscript.
  198. * @return the value of the cached subscript property
  199. *
  200. * <p>When you request this property, <code>LabelView</code>
  201. * re-syncs its state with the properties of the
  202. * <code>Element</code>'s <code>AttributeSet</code>.
  203. * If <code>Element</code>'s <code>AttributeSet</code>
  204. * does not have this property set, it will revert to false.
  205. *
  206. * @return the value of the cached
  207. * <code>subscript</code> property
  208. */
  209. public boolean isSubscript() {
  210. sync();
  211. return subscript;
  212. }
  213. /**
  214. * Determines if the glyphs should be rendered as subscript.
  215. *
  216. * <p>When you request this property, <code>LabelView</code>
  217. * re-syncs its state with the properties of the
  218. * <code>Element</code>'s <code>AttributeSet</code>.
  219. * If <code>Element</code>'s <code>AttributeSet</code>
  220. * does not have this property set, it will revert to false.
  221. *
  222. * @return the value of the cached
  223. * <code>superscript</code> property
  224. */
  225. public boolean isSuperscript() {
  226. sync();
  227. return superscript;
  228. }
  229. // --- View methods ---------------------------------------------
  230. /**
  231. * Gives notification from the document that attributes were changed
  232. * in a location that this view is responsible for.
  233. *
  234. * @param e the change information from the associated document
  235. * @param a the current allocation of the view
  236. * @param f the factory to use to rebuild if the view has children
  237. * @see View#changedUpdate
  238. */
  239. public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
  240. font = null;
  241. super.changedUpdate(e, a, f);
  242. }
  243. // --- variables ------------------------------------------------
  244. private Font font;
  245. private Color fg;
  246. private Color bg;
  247. private boolean underline;
  248. private boolean strike;
  249. private boolean superscript;
  250. private boolean subscript;
  251. }