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