jdk/src/share/classes/javax/swing/text/TextAction.java
author malenkov
Fri, 04 Apr 2014 20:18:53 +0400
changeset 24158 6afb40c4e9f8
parent 23010 6dadb192ad81
child 25193 187a455af8f8
permissions -rw-r--r--
8039137: KSS: JTextComponent.isProcessInputMethodEventOverridden Reviewed-by: alexsch, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 22574
diff changeset
     2
 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.event.ActionEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.KeyboardFocusManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.Action;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.AbstractAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.KeyStroke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * An Action implementation useful for key bindings that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * shared across a number of different text components.  Because
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * the action is shared, it must have a way of getting it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * target to act upon.  This class provides support to try and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * find a text component to operate on.  The preferred way of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * getting the component to act upon is through the ActionEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * that is received.  If the Object returned by getSource can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * be narrowed to a text component, it will be used.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * action event is null or can't be narrowed, the last focused
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * text component is tried.  This is determined by being
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * used in conjunction with a JTextController which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * arranges to share that information with a TextAction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 5506
diff changeset
    55
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
    61
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
public abstract class TextAction extends AbstractAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Creates a new JTextAction object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @param name the name of the action
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public TextAction(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Determines the component to use for the action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * This if fetched from the source of the ActionEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * if it's not null and can be narrowed.  Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * the last focused component is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param e the ActionEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @return the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    protected final JTextComponent getTextComponent(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            Object o = e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            if (o instanceof JTextComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                return (JTextComponent) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        return getFocusedComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Takes one list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * commands and augments it with another list
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * of commands.  The second list takes precedence
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * over the first list; that is, when both lists
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * contain a command with the same name, the command
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * from the second list is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @param list1 the first list, may be empty but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *              <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @param list2 the second list, may be empty but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *              <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @return the augmented list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public static final Action[] augmentList(Action[] list1, Action[] list2) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   107
        Hashtable<String, Action> h = new Hashtable<String, Action>();
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   108
        for (Action a : list1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            String value = (String)a.getValue(Action.NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            h.put((value!=null ? value:""), a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   112
        for (Action a : list2) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            String value = (String)a.getValue(Action.NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            h.put((value!=null ? value:""), a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        Action[] actions = new Action[h.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        for (Enumeration e = h.elements() ; e.hasMoreElements() ;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            actions[index++] = (Action) e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return actions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Fetches the text component that currently has focus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * This allows actions to be shared across text components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * which is useful for key-bindings where a large set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * actions are defined, but generally used the same way
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * across many different components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @return the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    protected final JTextComponent getFocusedComponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        return JTextComponent.getFocusedComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
}