< prev index next >

src/java.desktop/share/classes/javax/swing/text/TextAction.java

Print this page




  36 /**
  37  * An Action implementation useful for key bindings that are
  38  * shared across a number of different text components.  Because
  39  * the action is shared, it must have a way of getting it's
  40  * target to act upon.  This class provides support to try and
  41  * find a text component to operate on.  The preferred way of
  42  * getting the component to act upon is through the ActionEvent
  43  * that is received.  If the Object returned by getSource can
  44  * be narrowed to a text component, it will be used.  If the
  45  * action event is null or can't be narrowed, the last focused
  46  * text component is tried.  This is determined by being
  47  * used in conjunction with a JTextController which
  48  * arranges to share that information with a TextAction.
  49  * <p>
  50  * <strong>Warning:</strong>
  51  * Serialized objects of this class will not be compatible with
  52  * future Swing releases. The current serialization support is
  53  * appropriate for short term storage or RMI between applications running
  54  * the same version of Swing.  As of 1.4, support for long term storage
  55  * of all JavaBeans&trade;
  56  * has been added to the <code>java.beans</code> package.
  57  * Please see {@link java.beans.XMLEncoder}.
  58  *
  59  * @author  Timothy Prinzing
  60  */
  61 @SuppressWarnings("serial") // Same-version serialization only
  62 public abstract class TextAction extends AbstractAction {
  63 
  64     /**
  65      * Creates a new JTextAction object.
  66      *
  67      * @param name the name of the action
  68      */
  69     public TextAction(String name) {
  70         super(name);
  71     }
  72 
  73     /**
  74      * Determines the component to use for the action.
  75      * This if fetched from the source of the ActionEvent
  76      * if it's not null and can be narrowed.  Otherwise,


  81      */
  82     protected final JTextComponent getTextComponent(ActionEvent e) {
  83         if (e != null) {
  84             Object o = e.getSource();
  85             if (o instanceof JTextComponent) {
  86                 return (JTextComponent) o;
  87             }
  88         }
  89         return getFocusedComponent();
  90     }
  91 
  92     /**
  93      * Takes one list of
  94      * commands and augments it with another list
  95      * of commands.  The second list takes precedence
  96      * over the first list; that is, when both lists
  97      * contain a command with the same name, the command
  98      * from the second list is used.
  99      *
 100      * @param list1 the first list, may be empty but not
 101      *              <code>null</code>
 102      * @param list2 the second list, may be empty but not
 103      *              <code>null</code>
 104      * @return the augmented list
 105      */
 106     public static final Action[] augmentList(Action[] list1, Action[] list2) {
 107         Hashtable<String, Action> h = new Hashtable<String, Action>();
 108         for (Action a : list1) {
 109             String value = (String)a.getValue(Action.NAME);
 110             h.put((value!=null ? value:""), a);
 111         }
 112         for (Action a : list2) {
 113             String value = (String)a.getValue(Action.NAME);
 114             h.put((value!=null ? value:""), a);
 115         }
 116         Action[] actions = new Action[h.size()];
 117         int index = 0;
 118         for (Enumeration<Action> e = h.elements() ; e.hasMoreElements() ;) {
 119             actions[index++] = e.nextElement();
 120         }
 121         return actions;
 122     }
 123 


  36 /**
  37  * An Action implementation useful for key bindings that are
  38  * shared across a number of different text components.  Because
  39  * the action is shared, it must have a way of getting it's
  40  * target to act upon.  This class provides support to try and
  41  * find a text component to operate on.  The preferred way of
  42  * getting the component to act upon is through the ActionEvent
  43  * that is received.  If the Object returned by getSource can
  44  * be narrowed to a text component, it will be used.  If the
  45  * action event is null or can't be narrowed, the last focused
  46  * text component is tried.  This is determined by being
  47  * used in conjunction with a JTextController which
  48  * arranges to share that information with a TextAction.
  49  * <p>
  50  * <strong>Warning:</strong>
  51  * Serialized objects of this class will not be compatible with
  52  * future Swing releases. The current serialization support is
  53  * appropriate for short term storage or RMI between applications running
  54  * the same version of Swing.  As of 1.4, support for long term storage
  55  * of all JavaBeans&trade;
  56  * has been added to the {@code java.beans} package.
  57  * Please see {@link java.beans.XMLEncoder}.
  58  *
  59  * @author  Timothy Prinzing
  60  */
  61 @SuppressWarnings("serial") // Same-version serialization only
  62 public abstract class TextAction extends AbstractAction {
  63 
  64     /**
  65      * Creates a new JTextAction object.
  66      *
  67      * @param name the name of the action
  68      */
  69     public TextAction(String name) {
  70         super(name);
  71     }
  72 
  73     /**
  74      * Determines the component to use for the action.
  75      * This if fetched from the source of the ActionEvent
  76      * if it's not null and can be narrowed.  Otherwise,


  81      */
  82     protected final JTextComponent getTextComponent(ActionEvent e) {
  83         if (e != null) {
  84             Object o = e.getSource();
  85             if (o instanceof JTextComponent) {
  86                 return (JTextComponent) o;
  87             }
  88         }
  89         return getFocusedComponent();
  90     }
  91 
  92     /**
  93      * Takes one list of
  94      * commands and augments it with another list
  95      * of commands.  The second list takes precedence
  96      * over the first list; that is, when both lists
  97      * contain a command with the same name, the command
  98      * from the second list is used.
  99      *
 100      * @param list1 the first list, may be empty but not
 101      *              {@code null}
 102      * @param list2 the second list, may be empty but not
 103      *              {@code null}
 104      * @return the augmented list
 105      */
 106     public static final Action[] augmentList(Action[] list1, Action[] list2) {
 107         Hashtable<String, Action> h = new Hashtable<String, Action>();
 108         for (Action a : list1) {
 109             String value = (String)a.getValue(Action.NAME);
 110             h.put((value!=null ? value:""), a);
 111         }
 112         for (Action a : list2) {
 113             String value = (String)a.getValue(Action.NAME);
 114             h.put((value!=null ? value:""), a);
 115         }
 116         Action[] actions = new Action[h.size()];
 117         int index = 0;
 118         for (Enumeration<Action> e = h.elements() ; e.hasMoreElements() ;) {
 119             actions[index++] = e.nextElement();
 120         }
 121         return actions;
 122     }
 123 
< prev index next >