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