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. 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.68 06/28/04
  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 background color for the view. This method is typically
  95. * invoked as part of configuring this <code>View</code>. If you need
  96. * to customize the background color you should override
  97. * <code>setPropertiesFromAttributes</code> and invoke this method. A
  98. * value of null indicates no background should be rendered, so that the
  99. * background of the parent <code>View</code> will show through.
  100. *
  101. * @param bg background color, or null
  102. * @see #setPropertiesFromAttributes
  103. * @since 1.5
  104. */
  105. protected void setBackground(Color bg) {
  106. this.bg = bg;
  107. }
  108. /**
  109. * Sets the cached properties from the attributes.
  110. */
  111. protected void setPropertiesFromAttributes() {
  112. AttributeSet attr = getAttributes();
  113. if (attr != null) {
  114. Document d = getDocument();
  115. if (d instanceof StyledDocument) {
  116. StyledDocument doc = (StyledDocument) d;
  117. font = doc.getFont(attr);
  118. fg = doc.getForeground(attr);
  119. if (attr.isDefined(StyleConstants.Background)) {
  120. bg = doc.getBackground(attr);
  121. } else {
  122. bg = null;
  123. }
  124. setUnderline(StyleConstants.isUnderline(attr));
  125. setStrikeThrough(StyleConstants.isStrikeThrough(attr));
  126. setSuperscript(StyleConstants.isSuperscript(attr));
  127. setSubscript(StyleConstants.isSubscript(attr));
  128. } else {
  129. throw new StateInvariantError("LabelView needs StyledDocument");
  130. }
  131. }
  132. }
  133. /**
  134. * Fetches the <code>FontMetrics</code> used for this view.
  135. * @deprecated FontMetrics are not used for glyph rendering
  136. * when running in the JDK.
  137. */
  138. @Deprecated
  139. protected FontMetrics getFontMetrics() {
  140. sync();
  141. Container c = getContainer();
  142. return (c != null) ? c.getFontMetrics(font) :
  143. Toolkit.getDefaultToolkit().getFontMetrics(font);
  144. }
  145. /**
  146. * Fetches the background color to use to render the glyphs.
  147. * This is implemented to return a cached background color,
  148. * which defaults to <code>null</code>.
  149. *
  150. * @return the cached background color
  151. */
  152. public Color getBackground() {
  153. sync();
  154. return bg;
  155. }
  156. /**
  157. * Fetches the foreground color to use to render the glyphs.
  158. * This is implemented to return a cached foreground color,
  159. * which defaults to <code>null</code>.
  160. *
  161. * @return the cached foreground color
  162. */
  163. public Color getForeground() {
  164. sync();
  165. return fg;
  166. }
  167. /**
  168. * Fetches the font that the glyphs should be based upon.
  169. * This is implemented to return a cached font.
  170. *
  171. * @return the cached font
  172. */
  173. public Font getFont() {
  174. sync();
  175. return font;
  176. }
  177. /**
  178. * Determines if the glyphs should be underlined. If true,
  179. * an underline should be drawn through the baseline. This
  180. * is implemented to return the cached underline property.
  181. *
  182. * <p>When you request this property, <code>LabelView</code>
  183. * re-syncs its state with the properties of the
  184. * <code>Element</code>'s <code>AttributeSet</code>.
  185. * If <code>Element</code>'s <code>AttributeSet</code>
  186. * does not have this property set, it will revert to false.
  187. *
  188. * @return the value of the cached
  189. * <code>underline</code> property
  190. */
  191. public boolean isUnderline() {
  192. sync();
  193. return underline;
  194. }
  195. /**
  196. * Determines if the glyphs should have a strikethrough
  197. * line. If true, a line should be drawn through the center
  198. * of the glyphs. This is implemented to return the
  199. * cached <code>strikeThrough</code> property.
  200. *
  201. * <p>When you request this property, <code>LabelView</code>
  202. * re-syncs its state with the properties of the
  203. * <code>Element</code>'s <code>AttributeSet</code>.
  204. * If <code>Element</code>'s <code>AttributeSet</code>
  205. * does not have this property set, it will revert to false.
  206. *
  207. * @return the value of the cached
  208. * <code>strikeThrough</code> property
  209. */
  210. public boolean isStrikeThrough() {
  211. sync();
  212. return strike;
  213. }
  214. /**
  215. * Determines if the glyphs should be rendered as superscript.
  216. * @return the value of the cached subscript 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>subscript</code> property
  226. */
  227. public boolean isSubscript() {
  228. sync();
  229. return subscript;
  230. }
  231. /**
  232. * Determines if the glyphs should be rendered as subscript.
  233. *
  234. * <p>When you request this property, <code>LabelView</code>
  235. * re-syncs its state with the properties of the
  236. * <code>Element</code>'s <code>AttributeSet</code>.
  237. * If <code>Element</code>'s <code>AttributeSet</code>
  238. * does not have this property set, it will revert to false.
  239. *
  240. * @return the value of the cached
  241. * <code>superscript</code> property
  242. */
  243. public boolean isSuperscript() {
  244. sync();
  245. return superscript;
  246. }
  247. // --- View methods ---------------------------------------------
  248. /**
  249. * Gives notification from the document that attributes were changed
  250. * in a location that this view is responsible for.
  251. *
  252. * @param e the change information from the associated document
  253. * @param a the current allocation of the view
  254. * @param f the factory to use to rebuild if the view has children
  255. * @see View#changedUpdate
  256. */
  257. public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
  258. font = null;
  259. super.changedUpdate(e, a, f);
  260. }
  261. // --- variables ------------------------------------------------
  262. private Font font;
  263. private Color fg;
  264. private Color bg;
  265. private boolean underline;
  266. private boolean strike;
  267. private boolean superscript;
  268. private boolean subscript;
  269. }