1. /*
  2. * @(#)BasicTextPaneUI.java 1.60 01/11/29
  3. *
  4. * Copyright 2002 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 appropriate
  21. * for short term storage or RMI between applications running the same
  22. * version of Swing. A future release of Swing will provide support for
  23. * long term persistence.
  24. *
  25. * @author Timothy Prinzing
  26. * @version 1.60 11/29/01
  27. */
  28. public class BasicTextPaneUI extends BasicEditorPaneUI {
  29. /**
  30. * Creates a UI for the JTextPane.
  31. *
  32. * @param c the JTextPane object
  33. * @return the UI
  34. */
  35. public static ComponentUI createUI(JComponent c) {
  36. return new BasicTextPaneUI();
  37. }
  38. /**
  39. * Creates a new BasicTextPaneUI.
  40. */
  41. public BasicTextPaneUI() {
  42. super();
  43. }
  44. /**
  45. * Fetches the name used as a key to lookup properties through the
  46. * UIManager. This is used as a prefix to all the standard
  47. * text properties.
  48. *
  49. * @return the name ("TextPane")
  50. */
  51. protected String getPropertyPrefix() {
  52. return "TextPane";
  53. }
  54. /**
  55. * Fetches the EditorKit for the UI. This is whatever is
  56. * currently set in the associated JEditorPane.
  57. *
  58. * @param tc the text component for which this UI is installed
  59. * @return the editor capabilities
  60. * @see TextUI#getEditorKit
  61. */
  62. public EditorKit getEditorKit(JTextComponent tc) {
  63. JEditorPane pane = (JEditorPane) getComponent();
  64. return pane.getEditorKit();
  65. }
  66. /**
  67. * This method gets called when a bound property is changed
  68. * on the associated JTextComponent. This is a hook
  69. * which UI implementations may change to reflect how the
  70. * UI displays bound properties of JTextComponent subclasses.
  71. * If the font or foreground has changed, and the
  72. * Document is a StyledDocument, the appropriate property
  73. * is set in the default style.
  74. *
  75. * @param evt the property change event
  76. */
  77. protected void propertyChange(PropertyChangeEvent evt) {
  78. super.propertyChange(evt);
  79. StyledDocument doc = (StyledDocument)getComponent().getDocument();
  80. Style style = doc.getStyle(StyleContext.DEFAULT_STYLE);
  81. if (style == null)
  82. return;
  83. String name = evt.getPropertyName();
  84. // foreground
  85. if (name.equals("foreground")) {
  86. Color color = (Color)evt.getNewValue();
  87. if (color != null) {
  88. StyleConstants.setForeground(style, color);
  89. }
  90. else {
  91. style.removeAttribute(StyleConstants.Foreground);
  92. }
  93. }
  94. // font
  95. else if (name.equals("font")) {
  96. Font font = (Font)evt.getNewValue();
  97. if (font != null) {
  98. StyleConstants.setFontFamily(style, font.getName());
  99. StyleConstants.setFontSize(style, font.getSize());
  100. StyleConstants.setBold(style, font.isBold());
  101. StyleConstants.setItalic(style, font.isItalic());
  102. }
  103. else {
  104. style.removeAttribute(StyleConstants.FontFamily);
  105. style.removeAttribute(StyleConstants.FontSize);
  106. style.removeAttribute(StyleConstants.Bold);
  107. style.removeAttribute(StyleConstants.Italic);
  108. }
  109. }
  110. }
  111. }