src/java.desktop/share/classes/java/awt/event/ItemEvent.java
author darcy
Tue, 24 Sep 2019 18:25:54 -0700
changeset 58309 c6f8b2c3dc66
parent 52752 5e7dba63836d
permissions -rw-r--r--
8231334: Suppress warnings on non-serializable instance fields in client libs serializable classes Reviewed-by: prr
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: 20455
diff changeset
     2
 * Copyright (c) 1996, 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: 715
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: 715
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: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
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.AWTEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.ItemSelectable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * A semantic event which indicates that an item was selected or deselected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * This high-level event is generated by an ItemSelectable object (such as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * List) when an item is selected or deselected by the user.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    35
 * The event is passed to every {@code ItemListener} object which
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * registered to receive such events using the component's
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    37
 * {@code addItemListener} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <P>
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    39
 * The object that implements the {@code ItemListener} interface gets
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    40
 * this {@code ItemEvent} when the event occurs. The listener is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * spared the details of processing individual mouse movements and mouse
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * clicks, and can instead process a "meaningful" (semantic) event like
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * "item selected" or "item deselected".
440
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
    44
 * <p>
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
    45
 * An unspecified behavior will be caused if the {@code id} parameter
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
    46
 * of any particular {@code ItemEvent} instance is not
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
    47
 * in the range from {@code ITEM_FIRST} to {@code ITEM_LAST}.
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
    48
 * <p>
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
    49
 * The {@code stateChange} of any {@code ItemEvent} instance takes one of the following
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
    50
 * values:
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
    51
 *                     <ul>
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
    52
 *                     <li> {@code ItemEvent.SELECTED}
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
    53
 *                     <li> {@code ItemEvent.DESELECTED}
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
    54
 *                     </ul>
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
    55
 * Assigning the value different from listed above will cause an unspecified behavior.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @author Carl Quinn
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @see java.awt.ItemSelectable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @see ItemListener
52752
5e7dba63836d 8212882: links to tutorial should be updated to use https:
kaddepalli
parents: 47216
diff changeset
    61
 * @see <a href="https://docs.oracle.com/javase/tutorial/uiswing/events/itemlistener.html">Tutorial: Writing an Item Listener</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @since 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
public class ItemEvent extends AWTEvent {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * The first number in the range of ids used for item events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public static final int ITEM_FIRST          = 701;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * The last number in the range of ids used for item events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public static final int ITEM_LAST           = 701;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * This event id indicates that an item's state changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public static final int ITEM_STATE_CHANGED  = ITEM_FIRST; //Event.LIST_SELECT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * This state-change value indicates that an item was selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public static final int SELECTED = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * This state-change-value indicates that a selected item was deselected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public static final int DESELECTED  = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * The item whose selection state has changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @see #getItem()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
58309
c6f8b2c3dc66 8231334: Suppress warnings on non-serializable instance fields in client libs serializable classes
darcy
parents: 52752
diff changeset
    98
    @SuppressWarnings("serial") // Not statically typed as Serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    Object item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   102
     * {@code stateChange} indicates whether the {@code item}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * was selected or deselected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @see #getStateChange()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    int stateChange;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * JDK 1.1 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private static final long serialVersionUID = -608708132447206933L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   116
     * Constructs an {@code ItemEvent} object.
440
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   117
     * <p> This method throws an
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   118
     * {@code IllegalArgumentException} if {@code source}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   119
     * is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   121
     * @param source The {@code ItemSelectable} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *               that originated the event
440
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   123
     * @param id           The integer that identifies the event type.
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   124
     *                     For information on allowable values, see
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   125
     *                     the class description for {@link ItemEvent}
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   126
     * @param item   An object -- the item affected by the event
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   127
     * @param stateChange  An integer that indicates whether the item was
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   128
     *               selected or deselected.
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   129
     *                     For information on allowable values, see
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   130
     *                     the class description for {@link ItemEvent}
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   131
     * @throws IllegalArgumentException if {@code source} is null
440
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   132
     * @see #getItemSelectable()
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   133
     * @see #getID()
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   134
     * @see #getStateChange()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public ItemEvent(ItemSelectable source, int id, Object item, int stateChange) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        super(source, id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        this.item = item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        this.stateChange = stateChange;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Returns the originator of the event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @return the ItemSelectable object that originated the event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public ItemSelectable getItemSelectable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return (ItemSelectable)source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    * Returns the item affected by the event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    * @return the item (object) that was affected by the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public Object getItem() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        return item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    * Returns the type of state change (selected or deselected).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    * @return an integer that indicates whether the item was selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    *         or deselected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    * @see #SELECTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    * @see #DESELECTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public int getStateChange() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return stateChange;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Returns a parameter string identifying this item event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * This method is useful for event-logging and for debugging.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @return a string identifying the event and its attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public String paramString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        String typeStr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        switch(id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
          case ITEM_STATE_CHANGED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
              typeStr = "ITEM_STATE_CHANGED";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
          default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
              typeStr = "unknown type";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        String stateStr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        switch(stateChange) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
          case SELECTED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
              stateStr = "SELECTED";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
          case DESELECTED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
              stateStr = "DESELECTED";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
          default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
              stateStr = "unknown type";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        return typeStr + ",item="+item + ",stateChange="+stateStr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
}