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