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