KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > text > TextAction


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
9 import java.awt.event.ActionEvent JavaDoc;
10 import java.awt.KeyboardFocusManager JavaDoc;
11 import java.awt.Component JavaDoc;
12 import java.util.Hashtable JavaDoc;
13 import java.util.Enumeration JavaDoc;
14 import javax.swing.Action JavaDoc;
15 import javax.swing.AbstractAction JavaDoc;
16 import javax.swing.KeyStroke JavaDoc;
17
18 /**
19  * An Action implementation useful for key bindings that are
20  * shared across a number of different text components. Because
21  * the action is shared, it must have a way of getting it's
22  * target to act upon. This class provides support to try and
23  * find a text component to operate on. The preferred way of
24  * getting the component to act upon is through the ActionEvent
25  * that is received. If the Object returned by getSource can
26  * be narrowed to a text component, it will be used. If the
27  * action event is null or can't be narrowed, the last focused
28  * text component is tried. This is determined by being
29  * used in conjunction with a JTextController which
30  * arranges to share that information with a TextAction.
31  * <p>
32  * <strong>Warning:</strong>
33  * Serialized objects of this class will not be compatible with
34  * future Swing releases. The current serialization support is
35  * appropriate for short term storage or RMI between applications running
36  * the same version of Swing. As of 1.4, support for long term storage
37  * of all JavaBeans<sup><font size="-2">TM</font></sup>
38  * has been added to the <code>java.beans</code> package.
39  * Please see {@link java.beans.XMLEncoder}.
40  *
41  * @author Timothy Prinzing
42  * @version 1.29 12/19/03
43  */

44 public abstract class TextAction extends AbstractAction JavaDoc {
45
46     /**
47      * Creates a new JTextAction object.
48      *
49      * @param name the name of the action
50      */

51     public TextAction(String JavaDoc name) {
52     super(name);
53     }
54
55     /**
56      * Determines the component to use for the action.
57      * This if fetched from the source of the ActionEvent
58      * if it's not null and can be narrowed. Otherwise,
59      * the last focused component is used.
60      *
61      * @param e the ActionEvent
62      * @return the component
63      */

64     protected final JTextComponent JavaDoc getTextComponent(ActionEvent JavaDoc e) {
65     if (e != null) {
66         Object JavaDoc o = e.getSource();
67         if (o instanceof JTextComponent JavaDoc) {
68         return (JTextComponent JavaDoc) o;
69         }
70     }
71     return getFocusedComponent();
72     }
73     
74     /**
75      * Takes one list of
76      * commands and augments it with another list
77      * of commands. The second list takes precedence
78      * over the first list; that is, when both lists
79      * contain a command with the same name, the command
80      * from the second list is used.
81      *
82      * @param list1 the first list, may be empty but not
83      * <code>null</code>
84      * @param list2 the second list, may be empty but not
85      * <code>null</code>
86      * @return the augmented list
87      */

88     public static final Action JavaDoc[] augmentList(Action JavaDoc[] list1, Action JavaDoc[] list2) {
89     Hashtable JavaDoc h = new Hashtable JavaDoc();
90     for (int i = 0; i < list1.length; i++) {
91         Action JavaDoc a = list1[i];
92         String JavaDoc value = (String JavaDoc)a.getValue(Action.NAME);
93         h.put((value!=null ? value:""), a);
94     }
95     for (int i = 0; i < list2.length; i++) {
96         Action JavaDoc a = list2[i];
97         String JavaDoc value = (String JavaDoc)a.getValue(Action.NAME);
98         h.put((value!=null ? value:""), a);
99     }
100     Action JavaDoc[] actions = new Action JavaDoc[h.size()];
101     int index = 0;
102         for (Enumeration JavaDoc e = h.elements() ; e.hasMoreElements() ;) {
103             actions[index++] = (Action JavaDoc) e.nextElement();
104         }
105     return actions;
106     }
107
108     /**
109      * Fetches the text component that currently has focus.
110      * This allows actions to be shared across text components
111      * which is useful for key-bindings where a large set of
112      * actions are defined, but generally used the same way
113      * across many different components.
114      *
115      * @return the component
116      */

117     protected final JTextComponent JavaDoc getFocusedComponent() {
118         return JTextComponent.getFocusedComponent();
119     }
120 }
121
Popular Tags