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