001    /* MouseWheelEvent.java -- a mouse wheel event
002       Copyright (C) 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.Adjustable;
042    import java.awt.Component;
043    import java.awt.Rectangle;
044    import java.awt.ScrollPane;
045    
046    import javax.swing.JScrollPane;
047    import javax.swing.Scrollable;
048    
049    /**
050     * This event is generated for a mouse wheel rotation. The wheel (the middle
051     * mouse button on most modern mice) can be rotated towards or away from the
052     * user, and is often used for scrolling.
053     *
054     * <p>Because of the special use for scrolling components, MouseWheelEvents
055     * often affect a different component than the one located at the point of
056     * the event. If the component under the mouse cursor does not accept wheel
057     * events, the event is passed to the first ancestor container which does. This
058     * is often a ScrollPane, which knows how to scroll. If an AWT component is
059     * built from a native widget that knows how to use mouse wheel events, that
060     * component will consume the event.
061     *
062     * <p>The two most common scroll types are "units" (lines at a time) or
063     * "blocks" (pages at a time). The initial setting is taken from the platform,
064     * although the user can adjust the setting at any time.
065     *
066     * @author Eric Blake (ebb9@email.byu.edu)
067     * @see MouseWheelListener
068     * @see ScrollPane
069     * @see ScrollPane#setWheelScrollingEnabled(boolean)
070     * @see JScrollPane
071     * @see JScrollPane#setWheelScrollingEnabled(boolean)
072     * @since 1.4
073     * @status updated to 1.4
074     */
075    public class MouseWheelEvent extends MouseEvent
076    {
077      /**
078       * Compatible with JDK 1.4+.
079       */
080      private static final long serialVersionUID = 6459879390515399677L;
081    
082      /**
083       * Indicates scrolling by units (lines).
084       *
085       * @see #getScrollType()
086       */
087      public static final int WHEEL_UNIT_SCROLL = 0;
088    
089      /**
090       * Indicates scrolling by blocks (pages).
091       *
092       * @see #getScrollType()
093       */
094      public static final int WHEEL_BLOCK_SCROLL = 1;
095    
096      /**
097       * Indicates what scroll type should take place. This should be limited
098       * to {@link #WHEEL_UNIT_SCROLL} and {@link #WHEEL_BLOCK_SCROLL}.
099       *
100       * @serial the scroll type
101       */
102      private final int scrollType;
103    
104      /**
105       * Indicates the scroll amount. This is only meaningful if scrollType is
106       * WHEEL_UNIT_SCROLL.
107       *
108       * @serial the number of lines to scroll
109       */
110      private final int scrollAmount;
111    
112      /**
113       * Indicates how far the mouse wheel was rotated.
114       *
115       * @serial the rotation amount
116       */
117      private final int wheelRotation;
118    
119      /**
120       * Initializes a new instance of <code>MouseWheelEvent</code> with the
121       * specified information. Note that an invalid id leads to unspecified
122       * results.
123       *
124       * @param source the source of the event
125       * @param id the event id
126       * @param when the timestamp of when the event occurred
127       * @param modifiers any modifier bits for this event
128       * @param x the X coordinate of the mouse point
129       * @param y the Y coordinate of the mouse point
130       * @param clickCount the number of mouse clicks for this event
131       * @param popupTrigger true if this event triggers a popup menu
132       * @param scrollType one of {@link #WHEEL_UNIT_SCROLL},
133       *        {@link #WHEEL_BLOCK_SCROLL}
134       * @param scrollAmount the number of units to scroll, ignored for block type
135       * @param wheelRotation the number of rotation "clicks"
136       * @throws IllegalArgumentException if source is null
137       * @see MouseEvent#MouseEvent(Component, int, long, int, int, int, int,
138       *      boolean)
139       */
140      public MouseWheelEvent(Component source, int id, long when, int modifiers,
141                             int x, int y, int clickCount, boolean popupTrigger,
142                             int scrollType, int scrollAmount, int wheelRotation)
143      {
144        super(source, id, when, modifiers, x, y, clickCount, popupTrigger);
145        this.scrollType = scrollType;
146        this.scrollAmount = scrollAmount;
147        this.wheelRotation = wheelRotation;
148      }
149    
150      /**
151       * This method returns the scrolling pattern this event requests. Legal
152       * values are {@link #WHEEL_UNIT_SCROLL} and {@link #WHEEL_BLOCK_SCROLL}.
153       *
154       * @return the scroll type
155       * @see Adjustable#getUnitIncrement()
156       * @see Adjustable#getBlockIncrement()
157       * @see Scrollable#getScrollableUnitIncrement(Rectangle, int, int)
158       * @see Scrollable#getScrollableBlockIncrement(Rectangle, int, int)
159       */
160      public int getScrollType()
161      {
162        return scrollType;
163      }
164    
165      /**
166       * Returns the number of units to scroll in response to this event. This
167       * only makes sense when the scroll type is WHEEL_UNIT_SCROLL.
168       *
169       * @return the number of scroll units, if defined
170       * @see #getScrollType()
171       */
172      public int getScrollAmount()
173      {
174        return scrollAmount;
175      }
176    
177      /**
178       * Gets the number of "clicks" the wheel was rotated. Negative values move
179       * up (away) from the user, positive values move down (towards) the user.
180       *
181       * @return the number of rotation clicks
182       */
183      public int getWheelRotation()
184      {
185        return wheelRotation;
186      }
187    
188      /**
189       * This is a convenience method which aids in a common listener for scrolling
190       * a scrollpane (although this is already built into ScrollPane and
191       * JScrollPane). This method only makes sense when getScrollType() returns
192       * WHEEL_UNIT_SCROLL.
193       *
194       * <p>This accounts for direction of scroll and amount of wheel movement, as
195       * interpreted by the platform settings.
196       *
197       * @return the number of units to scroll
198       * @see #getScrollType()
199       * @see #getScrollAmount()
200       * @see MouseWheelListener
201       * @see Adjustable
202       * @see Adjustable#getUnitIncrement()
203       * @see Scrollable
204       * @see Scrollable#getScrollableUnitIncrement(Rectangle, int, int)
205       * @see ScrollPane
206       * @see ScrollPane#setWheelScrollingEnabled(boolean)
207       * @see JScrollPane
208       * @see JScrollPane#setWheelScrollingEnabled(boolean)
209       */
210      public int getUnitsToScroll()
211      {
212        return wheelRotation * scrollAmount;
213      }
214    
215      /**
216       * Returns a string identifying this event. For mouse wheel events, this
217       * is <code>super.paramString() + ",scrollType=WHEEL_" +
218       * (getScrollType() == WHEEL_UNIT_SCROLL ? "UNIT" : "BLOCK")
219       * + "_SCROLL,scrollAmount=" + getScrollAmount() + ",wheelRotation="
220       * + getWheelRotation()</code>.
221       *
222       * @return a string identifying this event
223       */
224      public String paramString()
225      {
226        return super.paramString() + ",scrollType="
227          + (scrollType == WHEEL_UNIT_SCROLL ? "WHEEL_UNIT_SCROLL"
228             : scrollType == WHEEL_BLOCK_SCROLL ? "WHEEL_BLOCK_SCROLL"
229             : "unknown scroll type")
230          + ",scrollAmount=" + scrollAmount + ",wheelRotation=" + wheelRotation;
231      }
232    } // class MouseWheelEvent