1. /*
  2. * @(#)LabelView.java 1.56 00/02/02
  3. *
  4. * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.swing.text;
  11. import java.awt.*;
  12. import javax.swing.event.*;
  13. /**
  14. * A LabelView is a styled chunk of text that represents a view
  15. * mapped over an element in the text model. It caches the
  16. * character level attributes used for rendering.
  17. *
  18. * @author Timothy Prinzing
  19. * @version 1.56 02/02/00
  20. */
  21. public class LabelView extends GlyphView implements TabableView {
  22. /**
  23. * Constructs a new view wrapped on an element.
  24. *
  25. * @param elem the element
  26. */
  27. public LabelView(Element elem) {
  28. super(elem);
  29. }
  30. /**
  31. * Synchronize the view's cached values with the model.
  32. * This causes the font, metrics, color, etc to be
  33. * recached if the cache has been invalidated.
  34. */
  35. final void sync() {
  36. if (font == null) {
  37. setPropertiesFromAttributes();
  38. }
  39. }
  40. /**
  41. * Set whether or not the view is underlined.
  42. */
  43. protected void setUnderline(boolean u) {
  44. underline = u;
  45. }
  46. /**
  47. * Set whether or not the view has a strike/line
  48. * through it.
  49. */
  50. protected void setStrikeThrough(boolean s) {
  51. strike = s;
  52. }
  53. /**
  54. * Set whether or not the view represents a
  55. * superscript.
  56. */
  57. protected void setSuperscript(boolean s) {
  58. superscript = s;
  59. }
  60. /**
  61. * Set whether or not the view represents a
  62. * subscript.
  63. */
  64. protected void setSubscript(boolean s) {
  65. subscript = s;
  66. }
  67. /**
  68. * Set the cached properties from the attributes.
  69. */
  70. protected void setPropertiesFromAttributes() {
  71. AttributeSet attr = getAttributes();
  72. if (attr != null) {
  73. Document d = getDocument();
  74. if (d instanceof StyledDocument) {
  75. StyledDocument doc = (StyledDocument) d;
  76. font = doc.getFont(attr);
  77. fg = doc.getForeground(attr);
  78. if (attr.isDefined(StyleConstants.Background)) {
  79. bg = doc.getBackground(attr);
  80. } else {
  81. bg = null;
  82. }
  83. setUnderline(StyleConstants.isUnderline(attr));
  84. setStrikeThrough(StyleConstants.isStrikeThrough(attr));
  85. setSuperscript(StyleConstants.isSuperscript(attr));
  86. setSubscript(StyleConstants.isSubscript(attr));
  87. } else {
  88. throw new StateInvariantError("LabelView needs StyledDocument");
  89. }
  90. }
  91. }
  92. /**
  93. * Fetch the FontMetrics used for this view.
  94. * @deprecated FontMetrics are not used for glyph rendering
  95. * when running in the Java2 SDK.
  96. */
  97. protected FontMetrics getFontMetrics() {
  98. sync();
  99. return Toolkit.getDefaultToolkit().getFontMetrics(font);
  100. }
  101. /**
  102. * Fetch the background color to use to render the
  103. * glyphs. If there is no background color, null should
  104. * be returned. This is implemented to return a cached
  105. * background color.
  106. */
  107. public Color getBackground() {
  108. sync();
  109. return bg;
  110. }
  111. /**
  112. * Fetch the foreground color to use to render the
  113. * glyphs. If there is no foreground color, null should
  114. * be returned. This is implemented to return a cached
  115. * foreground color.
  116. */
  117. public Color getForeground() {
  118. sync();
  119. return fg;
  120. }
  121. /**
  122. * Fetch the font that the glyphs should be based
  123. * upon. This is implemented to return a cached
  124. * font.
  125. */
  126. public Font getFont() {
  127. sync();
  128. return font;
  129. }
  130. /**
  131. * Determine if the glyphs should be underlined. If true,
  132. * an underline should be drawn through the baseline.
  133. */
  134. public boolean isUnderline() {
  135. sync();
  136. return underline;
  137. }
  138. /**
  139. * Determine if the glyphs should have a strikethrough
  140. * line. If true, a line should be drawn through the center
  141. * of the glyphs.
  142. */
  143. public boolean isStrikeThrough() {
  144. sync();
  145. return strike;
  146. }
  147. /**
  148. * Determine if the glyphs should be rendered as superscript.
  149. */
  150. public boolean isSubscript() {
  151. sync();
  152. return subscript;
  153. }
  154. /**
  155. * Determine if the glyphs should be rendered as subscript.
  156. */
  157. public boolean isSuperscript() {
  158. sync();
  159. return superscript;
  160. }
  161. // --- View methods ---------------------------------------------
  162. /**
  163. * Gives notification from the document that attributes were changed
  164. * in a location that this view is responsible for.
  165. *
  166. * @param e the change information from the associated document
  167. * @param a the current allocation of the view
  168. * @param f the factory to use to rebuild if the view has children
  169. * @see View#changedUpdate
  170. */
  171. public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
  172. font = null;
  173. }
  174. // --- variables ------------------------------------------------
  175. private Font font;
  176. private Color fg;
  177. private Color bg;
  178. private boolean underline;
  179. private boolean strike;
  180. private boolean superscript;
  181. private boolean subscript;
  182. }