1. /*
  2. * @(#)TextAction.java 1.29 03/12/19
  3. *
  4. * Copyright 2004 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.29 12/19/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 takes precedence
  73. * over the first list; that is, when both lists
  74. * contain a command with the same name, the command
  75. * from the second list is used.
  76. *
  77. * @param list1 the first list, may be empty but not
  78. * <code>null</code>
  79. * @param list2 the second list, may be empty but not
  80. * <code>null</code>
  81. * @return the augmented list
  82. */
  83. public static final Action[] augmentList(Action[] list1, Action[] list2) {
  84. Hashtable h = new Hashtable();
  85. for (int i = 0; i < list1.length; i++) {
  86. Action a = list1[i];
  87. String value = (String)a.getValue(Action.NAME);
  88. h.put((value!=null ? value:""), a);
  89. }
  90. for (int i = 0; i < list2.length; i++) {
  91. Action a = list2[i];
  92. String value = (String)a.getValue(Action.NAME);
  93. h.put((value!=null ? value:""), a);
  94. }
  95. Action[] actions = new Action[h.size()];
  96. int index = 0;
  97. for (Enumeration e = h.elements() ; e.hasMoreElements() ;) {
  98. actions[index++] = (Action) e.nextElement();
  99. }
  100. return actions;
  101. }
  102. /**
  103. * Fetches the text component that currently has focus.
  104. * This allows actions to be shared across text components
  105. * which is useful for key-bindings where a large set of
  106. * actions are defined, but generally used the same way
  107. * across many different components.
  108. *
  109. * @return the component
  110. */
  111. protected final JTextComponent getFocusedComponent() {
  112. return JTextComponent.getFocusedComponent();
  113. }
  114. }