1. /*
  2. * @(#)BasicEditorPaneUI.java 1.24 00/04/06
  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 java.net.URL;
  15. import java.net.MalformedURLException;
  16. import javax.swing.*;
  17. import javax.swing.text.*;
  18. import javax.swing.plaf.*;
  19. import javax.swing.border.*;
  20. /**
  21. * Provides the look and feel for a JEditorPane.
  22. * <p>
  23. * <strong>Warning:</strong>
  24. * Serialized objects of this class will not be compatible with
  25. * future Swing releases. The current serialization support is appropriate
  26. * for short term storage or RMI between applications running the same
  27. * version of Swing. A future release of Swing will provide support for
  28. * long term persistence.
  29. *
  30. * @author Timothy Prinzing
  31. * @version 1.24 04/06/00
  32. */
  33. public class BasicEditorPaneUI extends BasicTextUI {
  34. /**
  35. * Creates a UI for the JTextPane.
  36. *
  37. * @param c the JTextPane component
  38. * @return the UI
  39. */
  40. public static ComponentUI createUI(JComponent c) {
  41. return new BasicEditorPaneUI();
  42. }
  43. /**
  44. * Creates a new BasicEditorPaneUI.
  45. */
  46. public BasicEditorPaneUI() {
  47. super();
  48. }
  49. protected void installKeyboardActions() {
  50. super.installKeyboardActions();
  51. EditorKit editorKit = getEditorKit(getComponent());
  52. if (editorKit != null) {
  53. Action[] actions = editorKit.getActions();
  54. if (actions != null) {
  55. addActions(getComponent().getActionMap(), actions);
  56. }
  57. }
  58. }
  59. /**
  60. * Fetches the name used as a key to lookup properties through the
  61. * UIManager. This is used as a prefix to all the standard
  62. * text properties.
  63. *
  64. * @return the name ("EditorPane")
  65. */
  66. protected String getPropertyPrefix() {
  67. return "EditorPane";
  68. }
  69. /**
  70. * Fetches the EditorKit for the UI. This is whatever is
  71. * currently set in the associated JEditorPane.
  72. *
  73. * @return the editor capabilities
  74. * @see TextUI#getEditorKit
  75. */
  76. public EditorKit getEditorKit(JTextComponent tc) {
  77. JEditorPane pane = (JEditorPane) getComponent();
  78. return pane.getEditorKit();
  79. }
  80. /**
  81. * Fetch an action map to use. The map for a JEditorPane
  82. * is not shared because it changes with the EditorKit.
  83. */
  84. ActionMap getActionMap() {
  85. ActionMap am = new ActionMapUIResource();
  86. am.put("requestFocus", new FocusAction());
  87. return am;
  88. }
  89. /**
  90. * This method gets called when a bound property is changed
  91. * on the associated JTextComponent. This is a hook
  92. * which UI implementations may change to reflect how the
  93. * UI displays bound properties of JTextComponent subclasses.
  94. * This is implemented to rebuild the ActionMap based upon an
  95. * EditorKit change.
  96. *
  97. * @param evt the property change event
  98. */
  99. protected void propertyChange(PropertyChangeEvent evt) {
  100. if (evt.getPropertyName().equals("editorKit")) {
  101. ActionMap map = getComponent().getActionMap();
  102. if (map != null) {
  103. Object oldValue = evt.getOldValue();
  104. if (oldValue instanceof EditorKit) {
  105. Action[] actions = ((EditorKit)oldValue).getActions();
  106. if (actions != null) {
  107. removeActions(map, actions);
  108. }
  109. }
  110. Object newValue = evt.getNewValue();
  111. if (newValue instanceof EditorKit) {
  112. Action[] actions = ((EditorKit)newValue).getActions();
  113. if (actions != null) {
  114. addActions(map, actions);
  115. }
  116. }
  117. }
  118. }
  119. }
  120. void removeActions(ActionMap map, Action[] actions) {
  121. int n = actions.length;
  122. for (int i = 0; i < n; i++) {
  123. Action a = actions[i];
  124. map.remove(a.getValue(Action.NAME));
  125. }
  126. }
  127. void addActions(ActionMap map, Action[] actions) {
  128. int n = actions.length;
  129. for (int i = 0; i < n; i++) {
  130. Action a = actions[i];
  131. map.put(a.getValue(Action.NAME), a);
  132. }
  133. }
  134. }