1. /*
  2. * @(#)BasicTextPaneUI.java 1.67 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.plaf.basic;
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import java.beans.*;
  11. import javax.swing.*;
  12. import javax.swing.text.*;
  13. import javax.swing.plaf.*;
  14. import javax.swing.border.*;
  15. /**
  16. * Provides the look and feel for a styled text editor.
  17. * <p>
  18. * <strong>Warning:</strong>
  19. * Serialized objects of this class will not be compatible with
  20. * future Swing releases. The current serialization support is
  21. * appropriate for short term storage or RMI between applications running
  22. * the same version of Swing. As of 1.4, support for long term storage
  23. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  24. * has been added to the <code>java.beans</code> package.
  25. * Please see {@link java.beans.XMLEncoder}.
  26. *
  27. * @author Timothy Prinzing
  28. * @version 1.67 01/23/03
  29. */
  30. public class BasicTextPaneUI extends BasicEditorPaneUI {
  31. /**
  32. * Creates a UI for the JTextPane.
  33. *
  34. * @param c the JTextPane object
  35. * @return the UI
  36. */
  37. public static ComponentUI createUI(JComponent c) {
  38. return new BasicTextPaneUI();
  39. }
  40. /**
  41. * Creates a new BasicTextPaneUI.
  42. */
  43. public BasicTextPaneUI() {
  44. super();
  45. }
  46. /**
  47. * Fetches the name used as a key to lookup properties through the
  48. * UIManager. This is used as a prefix to all the standard
  49. * text properties.
  50. *
  51. * @return the name ("TextPane")
  52. */
  53. protected String getPropertyPrefix() {
  54. return "TextPane";
  55. }
  56. public void installUI(JComponent c) {
  57. super.installUI(c);
  58. updateForeground(c.getForeground());
  59. updateFont(c.getFont());
  60. }
  61. /**
  62. * This method gets called when a bound property is changed
  63. * on the associated JTextComponent. This is a hook
  64. * which UI implementations may change to reflect how the
  65. * UI displays bound properties of JTextComponent subclasses.
  66. * If the font, foreground or document has changed, the
  67. * the appropriate property is set in the default style of
  68. * the document.
  69. *
  70. * @param evt the property change event
  71. */
  72. protected void propertyChange(PropertyChangeEvent evt) {
  73. super.propertyChange(evt);
  74. String name = evt.getPropertyName();
  75. if (name.equals("foreground")) {
  76. updateForeground((Color)evt.getNewValue());
  77. } else if (name.equals("font")) {
  78. updateFont((Font)evt.getNewValue());
  79. } else if (name.equals("document")) {
  80. JComponent comp = getComponent();
  81. updateForeground(comp.getForeground());
  82. updateFont(comp.getFont());
  83. }
  84. }
  85. /**
  86. * Update the color in the default style of the document.
  87. *
  88. * @param color the new color to use or null to remove the color attribute
  89. * from the document's style
  90. */
  91. private void updateForeground(Color color) {
  92. StyledDocument doc = (StyledDocument)getComponent().getDocument();
  93. Style style = doc.getStyle(StyleContext.DEFAULT_STYLE);
  94. if (style == null) {
  95. return;
  96. }
  97. if (color == null) {
  98. style.removeAttribute(StyleConstants.Foreground);
  99. } else {
  100. StyleConstants.setForeground(style, color);
  101. }
  102. }
  103. /**
  104. * Update the font in the default style of the document.
  105. *
  106. * @param font the new font to use or null to remove the font attribute
  107. * from the document's style
  108. */
  109. private void updateFont(Font font) {
  110. StyledDocument doc = (StyledDocument)getComponent().getDocument();
  111. Style style = doc.getStyle(StyleContext.DEFAULT_STYLE);
  112. if (style == null) {
  113. return;
  114. }
  115. if (font == null) {
  116. style.removeAttribute(StyleConstants.FontFamily);
  117. style.removeAttribute(StyleConstants.FontSize);
  118. style.removeAttribute(StyleConstants.Bold);
  119. style.removeAttribute(StyleConstants.Italic);
  120. } else {
  121. StyleConstants.setFontFamily(style, font.getName());
  122. StyleConstants.setFontSize(style, font.getSize());
  123. StyleConstants.setBold(style, font.isBold());
  124. StyleConstants.setItalic(style, font.isItalic());
  125. }
  126. }
  127. }