1. /*
  2. * @(#)TextAction.java 1.27 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.text;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.KeyboardFocusManager;
  10. import java.awt.Component;
  11. import java.util.Hashtable;
  12. import java.util.Enumeration;
  13. import javax.swing.Action;
  14. import javax.swing.AbstractAction;
  15. import javax.swing.KeyStroke;
  16. /**
  17. * An Action implementation useful for key bindings that are
  18. * shared across a number of different text components. Because
  19. * the action is shared, it must have a way of getting it's
  20. * target to act upon. This class provides support to try and
  21. * find a text component to operate on. The preferred way of
  22. * getting the component to act upon is through the ActionEvent
  23. * that is received. If the Object returned by getSource can
  24. * be narrowed to a text component, it will be used. If the
  25. * action event is null or can't be narrowed, the last focused
  26. * text component is tried. This is determined by being
  27. * used in conjunction with a JTextController which
  28. * arranges to share that information with a TextAction.
  29. * <p>
  30. * <strong>Warning:</strong>
  31. * Serialized objects of this class will not be compatible with
  32. * future Swing releases. The current serialization support is
  33. * appropriate for short term storage or RMI between applications running
  34. * the same version of Swing. As of 1.4, support for long term storage
  35. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  36. * has been added to the <code>java.beans</code> package.
  37. * Please see {@link java.beans.XMLEncoder}.
  38. *
  39. * @author Timothy Prinzing
  40. * @version 1.27 01/23/03
  41. */
  42. public abstract class TextAction extends AbstractAction {
  43. /**
  44. * Creates a new JTextAction object.
  45. *
  46. * @param name the name of the action
  47. */
  48. public TextAction(String name) {
  49. super(name);
  50. }
  51. /**
  52. * Determines the component to use for the action.
  53. * This if fetched from the source of the ActionEvent
  54. * if it's not null and can be narrowed. Otherwise,
  55. * the last focused component is used.
  56. *
  57. * @param e the ActionEvent
  58. * @return the component
  59. */
  60. protected final JTextComponent getTextComponent(ActionEvent e) {
  61. if (e != null) {
  62. Object o = e.getSource();
  63. if (o instanceof JTextComponent) {
  64. return (JTextComponent) o;
  65. }
  66. }
  67. return getFocusedComponent();
  68. }
  69. /**
  70. * Takes one list of
  71. * commands and augments it with another list
  72. * of commands. The second list is considered
  73. * to be higher priority than the first list
  74. * and commands with the same name will both lists
  75. * will only have the dominate command found in the
  76. * second list in the returned list.
  77. *
  78. * @param list1 the first list, may be empty but not null
  79. * @param list2 the second list, may be empty but not null
  80. * @return the augmented list
  81. */
  82. public static final Action[] augmentList(Action[] list1, Action[] list2) {
  83. Hashtable h = new Hashtable();
  84. for (int i = 0; i < list1.length; i++) {
  85. Action a = list1[i];
  86. String value = (String)a.getValue(Action.NAME);
  87. h.put((value!=null ? value:""), a);
  88. }
  89. for (int i = 0; i < list2.length; i++) {
  90. Action a = list2[i];
  91. String value = (String)a.getValue(Action.NAME);
  92. h.put((value!=null ? value:""), a);
  93. }
  94. Action[] actions = new Action[h.size()];
  95. int index = 0;
  96. for (Enumeration e = h.elements() ; e.hasMoreElements() ;) {
  97. actions[index++] = (Action) e.nextElement();
  98. }
  99. return actions;
  100. }
  101. /**
  102. * Fetches the text component that currently has focus.
  103. * This allows actions to be shared across text components
  104. * which is useful for key-bindings where a large set of
  105. * actions are defined, but generally used the same way
  106. * across many different components.
  107. *
  108. * @return the component
  109. */
  110. protected final JTextComponent getFocusedComponent() {
  111. return JTextComponent.getFocusedComponent();
  112. }
  113. }