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