jdk/src/share/classes/java/awt/event/InputEvent.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 440 a3dac373f62d
child 1962 6c293d33645b
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 440
diff changeset
     2
 * Copyright 1996-2008 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.awt.event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.GraphicsEnvironment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.Toolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.logging.Logger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.logging.Level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * The root event class for all component-level input events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Input events are delivered to listeners before they are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * processed normally by the source where they originated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * This allows listeners and component subclasses to "consume"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * the event so that the source will not process them in their
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * default manner.  For example, consuming mousePressed events
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * on a Button component will prevent the Button from being
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * activated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author Carl Quinn
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @see KeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @see KeyAdapter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @see MouseEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @see MouseAdapter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @see MouseMotionAdapter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @since 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
public abstract class InputEvent extends ComponentEvent {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private static final Logger log = Logger.getLogger("java.awt.event.InputEvent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * The Shift key modifier constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * It is recommended that SHIFT_DOWN_MASK be used instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public static final int SHIFT_MASK = Event.SHIFT_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * The Control key modifier constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * It is recommended that CTRL_DOWN_MASK be used instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public static final int CTRL_MASK = Event.CTRL_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * The Meta key modifier constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * It is recommended that META_DOWN_MASK be used instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public static final int META_MASK = Event.META_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * The Alt key modifier constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * It is recommended that ALT_DOWN_MASK be used instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public static final int ALT_MASK = Event.ALT_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * The AltGraph key modifier constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public static final int ALT_GRAPH_MASK = 1 << 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * The Mouse Button1 modifier constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * It is recommended that BUTTON1_DOWN_MASK be used instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public static final int BUTTON1_MASK = 1 << 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * The Mouse Button2 modifier constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * It is recommended that BUTTON2_DOWN_MASK be used instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Note that BUTTON2_MASK has the same value as ALT_MASK.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public static final int BUTTON2_MASK = Event.ALT_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * The Mouse Button3 modifier constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * It is recommended that BUTTON3_DOWN_MASK be used instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Note that BUTTON3_MASK has the same value as META_MASK.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public static final int BUTTON3_MASK = Event.META_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * The Shift key extended modifier constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public static final int SHIFT_DOWN_MASK = 1 << 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * The Control key extended modifier constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public static final int CTRL_DOWN_MASK = 1 << 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * The Meta key extended modifier constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public static final int META_DOWN_MASK = 1 << 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * The Alt key extended modifier constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public static final int ALT_DOWN_MASK = 1 << 9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * The Mouse Button1 extended modifier constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public static final int BUTTON1_DOWN_MASK = 1 << 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * The Mouse Button2 extended modifier constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public static final int BUTTON2_DOWN_MASK = 1 << 11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * The Mouse Button3 extended modifier constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public static final int BUTTON3_DOWN_MASK = 1 << 12;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * The AltGraph key extended modifier constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public static final int ALT_GRAPH_DOWN_MASK = 1 << 13;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    // the constant below MUST be updated if any extra modifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    // bits are to be added!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    // in fact, it is undesirable to add modifier bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    // to the same field as this may break applications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    // see bug# 5066958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    static final int FIRST_HIGH_BIT = 1 << 14;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    static final int JDK_1_3_MODIFIERS = SHIFT_DOWN_MASK - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    static final int HIGH_MODIFIERS = ~( FIRST_HIGH_BIT - 1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * The input event's Time stamp in UTC format.  The time stamp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * indicates when the input event was created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @see #getWhen()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    long when;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * The state of the modifier mask at the time the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * event was fired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @see #getModifiers()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @see #getModifiersEx()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @see java.awt.event.KeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @see java.awt.event.MouseEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    int modifiers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * A flag that indicates that this instance can be used to access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * the system clipboard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    private transient boolean canAccessSystemClipboard;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        /* ensure that the necessary native libraries are loaded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        NativeLibLoader.loadLibraries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (!GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Initialize JNI field and method IDs for fields that may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
       accessed from C.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Constructs an InputEvent object with the specified source component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * modifiers, and type.
440
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   211
     * <p> This method throws an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * <code>IllegalArgumentException</code> if <code>source</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @param source the object where the event originated
440
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   216
     * @param id           the integer that identifies the event type.
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   217
     *                     It is allowed to pass as parameter any value that
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   218
     *                     allowed for some subclass of {@code InputEvent} class.
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   219
     *                     Passing in the value different from those values result
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   220
     *                     in unspecified behavior
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   221
     * @param when         a long int that gives the time the event occurred.
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   222
     *                     Passing negative or zero value
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   223
     *                     is not recommended
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   224
     * @param modifiers    the modifier keys down during event (e.g. shift, ctrl,
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   225
     *                     alt, meta)
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   226
     *                     Passing negative parameter is not recommended.
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   227
     *                     Zero value means no modifiers.
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   228
     *                     Either extended _DOWN_MASK or old _MASK modifiers
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   229
     *                     should be used, but both models should not be mixed
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   230
     *                     in one event. Use of the extended modifiers is
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   231
     *                     preferred
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @throws IllegalArgumentException if <code>source</code> is null
440
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   233
     * @see #getSource()
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   234
     * @see #getID()
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   235
     * @see #getWhen()
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   236
     * @see #getModifiers()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    InputEvent(Component source, int id, long when, int modifiers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        super(source, id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        this.when = when;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        this.modifiers = modifiers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        canAccessSystemClipboard = canAccessSystemClipboard();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    private boolean canAccessSystemClipboard() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        boolean b = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if (!GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    sm.checkSystemClipboardAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    b = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    if (log.isLoggable(Level.FINE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                        log.log(Level.FINE, "InputEvent.canAccessSystemClipboard() got SecurityException ", se);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                b = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * Returns whether or not the Shift modifier is down on this event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public boolean isShiftDown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        return (modifiers & SHIFT_MASK) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * Returns whether or not the Control modifier is down on this event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public boolean isControlDown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return (modifiers & CTRL_MASK) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Returns whether or not the Meta modifier is down on this event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    public boolean isMetaDown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        return (modifiers & META_MASK) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * Returns whether or not the Alt modifier is down on this event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public boolean isAltDown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        return (modifiers & ALT_MASK) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * Returns whether or not the AltGraph modifier is down on this event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public boolean isAltGraphDown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        return (modifiers & ALT_GRAPH_MASK) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    /**
440
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   303
     * Returns the difference in milliseconds between the timestamp of when this event occurred and
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   304
     * midnight, January 1, 1970 UTC.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    public long getWhen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        return when;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * Returns the modifier mask for this event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    public int getModifiers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        return modifiers & (JDK_1_3_MODIFIERS | HIGH_MODIFIERS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * Returns the extended modifier mask for this event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * Extended modifiers represent the state of all modal keys,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * such as ALT, CTRL, META, and the mouse buttons just after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * the event occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * For example, if the user presses <b>button 1</b> followed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * <b>button 2</b>, and then releases them in the same order,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * the following sequence of events is generated:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * <PRE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *    <code>MOUSE_PRESSED</code>:  <code>BUTTON1_DOWN_MASK</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *    <code>MOUSE_PRESSED</code>:  <code>BUTTON1_DOWN_MASK | BUTTON2_DOWN_MASK</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *    <code>MOUSE_RELEASED</code>: <code>BUTTON2_DOWN_MASK</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *    <code>MOUSE_CLICKED</code>:  <code>BUTTON2_DOWN_MASK</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *    <code>MOUSE_RELEASED</code>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *    <code>MOUSE_CLICKED</code>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * </PRE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * It is not recommended to compare the return value of this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * using <code>==</code> because new modifiers can be added in the future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * For example, the appropriate way to check that SHIFT and BUTTON1 are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * down, but CTRL is up is demonstrated by the following code:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * <PRE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *    int onmask = SHIFT_DOWN_MASK | BUTTON1_DOWN_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *    int offmask = CTRL_DOWN_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *    if ((event.getModifiersEx() & (onmask | offmask)) == onmask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *        ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * </PRE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * The above code will work even if new modifiers are added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    public int getModifiersEx() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        return modifiers & ~JDK_1_3_MODIFIERS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * Consumes this event so that it will not be processed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * in the default manner by the source which originated it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    public void consume() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        consumed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * Returns whether or not this event has been consumed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @see #consume
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    public boolean isConsumed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        return consumed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    // state serialization compatibility with JDK 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    static final long serialVersionUID = -2482525981698309786L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * Returns a String describing the extended modifier keys and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * mouse buttons, such as "Shift", "Button1", or "Ctrl+Shift".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * These strings can be localized by changing the
440
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   377
     * <code>awt.properties</code> file.
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   378
     * <p>
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   379
     * Note that passing negative parameter is incorrect,
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   380
     * and will cause the returning an unspecified string.
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   381
     * Zero parameter means that no modifiers were passed and will
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   382
     * cause the returning an empty string.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @param modifiers a modifier mask describing the extended
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
       *                modifier keys and mouse buttons for the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @return a text description of the combination of extended
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *         modifier keys and mouse buttons that were held down
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *         during the event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    public static String getModifiersExText(int modifiers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        StringBuilder buf = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if ((modifiers & InputEvent.META_DOWN_MASK) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            buf.append(Toolkit.getProperty("AWT.meta", "Meta"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            buf.append("+");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if ((modifiers & InputEvent.CTRL_DOWN_MASK) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            buf.append(Toolkit.getProperty("AWT.control", "Ctrl"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            buf.append("+");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        if ((modifiers & InputEvent.ALT_DOWN_MASK) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            buf.append(Toolkit.getProperty("AWT.alt", "Alt"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            buf.append("+");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        if ((modifiers & InputEvent.SHIFT_DOWN_MASK) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            buf.append(Toolkit.getProperty("AWT.shift", "Shift"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            buf.append("+");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        if ((modifiers & InputEvent.ALT_GRAPH_DOWN_MASK) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            buf.append(Toolkit.getProperty("AWT.altGraph", "Alt Graph"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            buf.append("+");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        if ((modifiers & InputEvent.BUTTON1_DOWN_MASK) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            buf.append(Toolkit.getProperty("AWT.button1", "Button1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            buf.append("+");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        if ((modifiers & InputEvent.BUTTON2_DOWN_MASK) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            buf.append(Toolkit.getProperty("AWT.button2", "Button2"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            buf.append("+");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        if ((modifiers & InputEvent.BUTTON3_DOWN_MASK) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            buf.append(Toolkit.getProperty("AWT.button3", "Button3"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            buf.append("+");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        if (buf.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            buf.setLength(buf.length()-1); // remove trailing '+'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
}