001    /* ItemEvent.java -- event for item state changes
002       Copyright (C) 1999, 2002, 2005  Free Software Foundation, Inc.
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010    
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    
039    package java.awt.event;
040    
041    import java.awt.AWTEvent;
042    import java.awt.ItemSelectable;
043    
044    /**
045     * This event is generated when a selection item changes state. This is an
046     * abstraction that distills a large number of individual mouse or keyboard
047     * events into a simpler "item selected" and "item deselected" events.
048     *
049     * @author Aaron M. Renn (arenn@urbanophile.com)
050     * @see ItemSelectable
051     * @see ItemListener
052     * @since 1.1
053     * @status updated to 1.4
054     */
055    public class ItemEvent extends AWTEvent
056    {
057      /**
058       * Compatible with JDK 1.1+.
059       */
060      private static final long serialVersionUID = -608708132447206933L;
061    
062      /** This is the first id in the event id range used by this class. */
063      public static final int ITEM_FIRST = 701;
064    
065      /** This is the last id in the event id range used by this class. */
066      public static final int ITEM_LAST = 701;
067    
068      /** This event id indicates a state change occurred. */
069      public static final int ITEM_STATE_CHANGED = 701;
070    
071      /** This type indicates that the item was selected. */
072      public static final int SELECTED = 1;
073    
074      /** This type indicates that the item was deselected. */
075      public static final int DESELECTED = 2;
076    
077      /**
078       * The item affected by this event.
079       *
080       * @serial the item of the selection
081       */
082      private final Object item;
083    
084      /**
085       * The state change direction, one of {@link #SELECTED} or
086       * {@link #DESELECTED}.
087       *
088       * @serial the selection state
089       */
090      private final int stateChange;
091    
092      /**
093       * Initializes a new instance of <code>ItemEvent</code> with the specified
094       * source, id, and state change constant. Note that an invalid id leads to
095       * unspecified results.
096       *
097       * @param source the source of the event
098       * @param id the event id
099       * @param item the item affected by the state change
100       * @param stateChange one of {@link #SELECTED} or {@link #DESELECTED}
101       */
102      public ItemEvent(ItemSelectable source, int id, Object item, int stateChange)
103      {
104        super(source, id);
105        this.item = item;
106        this.stateChange = stateChange;
107      }
108    
109      /**
110       * This method returns the event source as an <code>ItemSelectable</code>.
111       *
112       * @return the event source as an <code>ItemSelected</code>
113       * @throws ClassCastException if source is changed to a non-ItemSelectable
114       */
115      public ItemSelectable getItemSelectable()
116      {
117        return (ItemSelectable) source;
118      }
119    
120      /**
121       * Returns the item affected by this state change.
122       *
123       * @return the item affected by this state change
124       */
125      public Object getItem()
126      {
127        return item;
128      }
129    
130      /**
131       * Returns the type of state change, either {@link #SELECTED} or
132       * {@link #DESELECTED}.
133       *
134       * @return the type of state change
135       */
136      public int getStateChange()
137      {
138        return stateChange;
139      }
140    
141      /**
142       * Returns a string identifying this event. This is in the format:
143       * <code>"ITEM_STATE_CHANGED,item=" + item + ",stateChange="
144       * + (getStateChange() == DESELECTED ? "DESELECTED" : "SELECTED")</code>.
145       *
146       * @return a string identifying this event
147       */
148      public String paramString()
149      {
150        return (id == ITEM_STATE_CHANGED ? "ITEM_STATE_CHANGED,item="
151                : "unknown type,item=") + item + ",stateChange="
152          + (stateChange == SELECTED ? "SELECTED"
153             : stateChange == DESELECTED ? "DESELECTED" : "unknown type");
154      }
155    } // class ItemEvent