0N/A/*
2362N/A * Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.awt.event;
0N/A
0N/Aimport java.awt.Component;
0N/A
0N/A/**
0N/A * An event which indicates that the mouse wheel was rotated in a component.
0N/A * <P>
0N/A * A wheel mouse is a mouse which has a wheel in place of the middle button.
0N/A * This wheel can be rotated towards or away from the user. Mouse wheels are
0N/A * most often used for scrolling, though other uses are possible.
0N/A * <P>
0N/A * A MouseWheelEvent object is passed to every <code>MouseWheelListener</code>
0N/A * object which registered to receive the "interesting" mouse events using the
0N/A * component's <code>addMouseWheelListener</code> method. Each such listener
0N/A * object gets a <code>MouseEvent</code> containing the mouse event.
0N/A * <P>
0N/A * Due to the mouse wheel's special relationship to scrolling Components,
0N/A * MouseWheelEvents are delivered somewhat differently than other MouseEvents.
0N/A * This is because while other MouseEvents usually affect a change on
0N/A * the Component directly under the mouse
0N/A * cursor (for instance, when clicking a button), MouseWheelEvents often have
0N/A * an effect away from the mouse cursor (moving the wheel while
0N/A * over a Component inside a ScrollPane should scroll one of the
0N/A * Scrollbars on the ScrollPane).
0N/A * <P>
0N/A * MouseWheelEvents start delivery from the Component underneath the
0N/A * mouse cursor. If MouseWheelEvents are not enabled on the
0N/A * Component, the event is delivered to the first ancestor
0N/A * Container with MouseWheelEvents enabled. This will usually be
0N/A * a ScrollPane with wheel scrolling enabled. The source
0N/A * Component and x,y coordinates will be relative to the event's
0N/A * final destination (the ScrollPane). This allows a complex
0N/A * GUI to be installed without modification into a ScrollPane, and
0N/A * for all MouseWheelEvents to be delivered to the ScrollPane for
0N/A * scrolling.
0N/A * <P>
0N/A * Some AWT Components are implemented using native widgets which
0N/A * display their own scrollbars and handle their own scrolling.
0N/A * The particular Components for which this is true will vary from
0N/A * platform to platform. When the mouse wheel is
0N/A * moved over one of these Components, the event is delivered straight to
0N/A * the native widget, and not propagated to ancestors.
0N/A * <P>
0N/A * Platforms offer customization of the amount of scrolling that
0N/A * should take place when the mouse wheel is moved. The two most
0N/A * common settings are to scroll a certain number of "units"
0N/A * (commonly lines of text in a text-based component) or an entire "block"
0N/A * (similar to page-up/page-down). The MouseWheelEvent offers
0N/A * methods for conforming to the underlying platform settings. These
0N/A * platform settings can be changed at any time by the user. MouseWheelEvents
0N/A * reflect the most recent settings.
98N/A * <P>
98N/A * The <code>MouseWheelEvent</code> class includes methods for
98N/A * getting the number of "clicks" by which the mouse wheel is rotated.
98N/A * The {@link #getWheelRotation} method returns the integer number
98N/A * of "clicks" corresponding to the number of notches by which the wheel was
98N/A * rotated. In addition to this method, the <code>MouseWheelEvent</code>
98N/A * class provides the {@link #getPreciseWheelRotation} method which returns
98N/A * a double number of "clicks" in case a partial rotation occurred.
98N/A * The {@link #getPreciseWheelRotation} method is useful if a mouse supports
98N/A * a high-resolution wheel, such as a freely rotating wheel with no
98N/A * notches. Applications can benefit by using this method to process
98N/A * mouse wheel events more precisely, and thus, making visual perception
98N/A * smoother.
0N/A *
0N/A * @author Brent Christian
0N/A * @see MouseWheelListener
0N/A * @see java.awt.ScrollPane
0N/A * @see java.awt.ScrollPane#setWheelScrollingEnabled(boolean)
0N/A * @see javax.swing.JScrollPane
0N/A * @see javax.swing.JScrollPane#setWheelScrollingEnabled(boolean)
0N/A * @since 1.4
0N/A */
0N/A
0N/Apublic class MouseWheelEvent extends MouseEvent {
0N/A
0N/A /**
0N/A * Constant representing scrolling by "units" (like scrolling with the
0N/A * arrow keys)
0N/A *
0N/A * @see #getScrollType
0N/A */
0N/A public static final int WHEEL_UNIT_SCROLL = 0;
0N/A
0N/A /**
0N/A * Constant representing scrolling by a "block" (like scrolling
0N/A * with page-up, page-down keys)
0N/A *
0N/A * @see #getScrollType
0N/A */
0N/A public static final int WHEEL_BLOCK_SCROLL = 1;
0N/A
0N/A /**
0N/A * Indicates what sort of scrolling should take place in response to this
0N/A * event, based on platform settings. Legal values are:
0N/A * <ul>
0N/A * <li> WHEEL_UNIT_SCROLL
0N/A * <li> WHEEL_BLOCK_SCROLL
0N/A * </ul>
0N/A *
0N/A * @see #getScrollType
0N/A */
0N/A int scrollType;
0N/A
0N/A /**
0N/A * Only valid for scrollType WHEEL_UNIT_SCROLL.
0N/A * Indicates number of units that should be scrolled per
0N/A * click of mouse wheel rotation, based on platform settings.
0N/A *
0N/A * @see #getScrollAmount
0N/A * @see #getScrollType
0N/A */
0N/A int scrollAmount;
0N/A
0N/A /**
0N/A * Indicates how far the mouse wheel was rotated.
0N/A *
0N/A * @see #getWheelRotation
0N/A */
0N/A int wheelRotation;
0N/A
98N/A /**
98N/A * Indicates how far the mouse wheel was rotated.
98N/A *
98N/A * @see #getPreciseWheelRotation
98N/A */
98N/A double preciseWheelRotation;
98N/A
0N/A /*
0N/A * serialVersionUID
0N/A */
0N/A
0N/A private static final long serialVersionUID = 6459879390515399677L;
0N/A
0N/A /**
0N/A * Constructs a <code>MouseWheelEvent</code> object with the
0N/A * specified source component, type, modifiers, coordinates,
0N/A * scroll type, scroll amount, and wheel rotation.
0N/A * <p>Absolute coordinates xAbs and yAbs are set to source's location on screen plus
0N/A * relative coordinates x and y. xAbs and yAbs are set to zero if the source is not showing.
0N/A * <p>Note that passing in an invalid <code>id</code> results in
0N/A * unspecified behavior. This method throws an
0N/A * <code>IllegalArgumentException</code> if <code>source</code>
0N/A * is <code>null</code>.
0N/A *
0N/A * @param source the <code>Component</code> that originated
0N/A * the event
0N/A * @param id the integer that identifies the event
0N/A * @param when a long that gives the time the event occurred
0N/A * @param modifiers the modifier keys down during event
0N/A * (shift, ctrl, alt, meta)
0N/A * @param x the horizontal x coordinate for the mouse location
0N/A * @param y the vertical y coordinate for the mouse location
0N/A * @param clickCount the number of mouse clicks associated with event
0N/A * @param popupTrigger a boolean, true if this event is a trigger for a
0N/A * popup-menu
0N/A * @param scrollType the type of scrolling which should take place in
0N/A * response to this event; valid values are
0N/A * <code>WHEEL_UNIT_SCROLL</code> and
0N/A * <code>WHEEL_BLOCK_SCROLL</code>
0N/A * @param scrollAmount for scrollType <code>WHEEL_UNIT_SCROLL</code>,
0N/A * the number of units to be scrolled
98N/A * @param wheelRotation the integer number of "clicks" by which the mouse
98N/A * wheel was rotated
0N/A *
0N/A * @throws IllegalArgumentException if <code>source</code> is null
0N/A * @see MouseEvent#MouseEvent(java.awt.Component, int, long, int, int, int, int, boolean)
0N/A * @see MouseEvent#MouseEvent(java.awt.Component, int, long, int, int, int, int, int, int, boolean, int)
0N/A */
0N/A public MouseWheelEvent (Component source, int id, long when, int modifiers,
0N/A int x, int y, int clickCount, boolean popupTrigger,
0N/A int scrollType, int scrollAmount, int wheelRotation) {
0N/A
0N/A this(source, id, when, modifiers, x, y, 0, 0, clickCount,
0N/A popupTrigger, scrollType, scrollAmount, wheelRotation);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a <code>MouseWheelEvent</code> object with the
0N/A * specified source component, type, modifiers, coordinates,
0N/A * absolute coordinates, scroll type, scroll amount, and wheel rotation.
0N/A * <p>Note that passing in an invalid <code>id</code> results in
0N/A * unspecified behavior. This method throws an
0N/A * <code>IllegalArgumentException</code> if <code>source</code>
0N/A * is <code>null</code>.<p>
0N/A * Even if inconsistent values for relative and absolute coordinates are
0N/A * passed to the constructor, the MouseWheelEvent instance is still
0N/A * created and no exception is thrown.
0N/A *
0N/A * @param source the <code>Component</code> that originated
0N/A * the event
0N/A * @param id the integer that identifies the event
0N/A * @param when a long that gives the time the event occurred
0N/A * @param modifiers the modifier keys down during event
0N/A * (shift, ctrl, alt, meta)
0N/A * @param x the horizontal x coordinate for the mouse location
0N/A * @param y the vertical y coordinate for the mouse location
0N/A * @param xAbs the absolute horizontal x coordinate for the mouse location
0N/A * @param yAbs the absolute vertical y coordinate for the mouse location
0N/A * @param clickCount the number of mouse clicks associated with event
0N/A * @param popupTrigger a boolean, true if this event is a trigger for a
0N/A * popup-menu
0N/A * @param scrollType the type of scrolling which should take place in
0N/A * response to this event; valid values are
0N/A * <code>WHEEL_UNIT_SCROLL</code> and
0N/A * <code>WHEEL_BLOCK_SCROLL</code>
0N/A * @param scrollAmount for scrollType <code>WHEEL_UNIT_SCROLL</code>,
0N/A * the number of units to be scrolled
98N/A * @param wheelRotation the integer number of "clicks" by which the mouse
98N/A * wheel was rotated
0N/A *
0N/A * @throws IllegalArgumentException if <code>source</code> is null
0N/A * @see MouseEvent#MouseEvent(java.awt.Component, int, long, int, int, int, int, boolean)
0N/A * @see MouseEvent#MouseEvent(java.awt.Component, int, long, int, int, int, int, int, int, boolean, int)
0N/A * @since 1.6
0N/A */
0N/A public MouseWheelEvent (Component source, int id, long when, int modifiers,
0N/A int x, int y, int xAbs, int yAbs, int clickCount, boolean popupTrigger,
0N/A int scrollType, int scrollAmount, int wheelRotation) {
0N/A
98N/A this(source, id, when, modifiers, x, y, xAbs, yAbs, clickCount, popupTrigger,
98N/A scrollType, scrollAmount, wheelRotation, wheelRotation);
98N/A
98N/A }
98N/A
98N/A
98N/A /**
98N/A * Constructs a <code>MouseWheelEvent</code> object with the specified
98N/A * source component, type, modifiers, coordinates, absolute coordinates,
98N/A * scroll type, scroll amount, and wheel rotation.
98N/A * <p>Note that passing in an invalid <code>id</code> parameter results
98N/A * in unspecified behavior. This method throws an
98N/A * <code>IllegalArgumentException</code> if <code>source</code> equals
98N/A * <code>null</code>.
98N/A * <p>Even if inconsistent values for relative and absolute coordinates
98N/A * are passed to the constructor, a <code>MouseWheelEvent</code> instance
98N/A * is still created and no exception is thrown.
98N/A *
98N/A * @param source the <code>Component</code> that originated the event
98N/A * @param id the integer value that identifies the event
98N/A * @param when a long value that gives the time when the event occurred
98N/A * @param modifiers the modifier keys down during event
98N/A * (shift, ctrl, alt, meta)
98N/A * @param x the horizontal <code>x</code> coordinate for the
98N/A * mouse location
98N/A * @param y the vertical <code>y</code> coordinate for the
98N/A * mouse location
98N/A * @param xAbs the absolute horizontal <code>x</code> coordinate for
98N/A * the mouse location
98N/A * @param yAbs the absolute vertical <code>y</code> coordinate for
98N/A * the mouse location
98N/A * @param clickCount the number of mouse clicks associated with the event
98N/A * @param popupTrigger a boolean value, <code>true</code> if this event is a trigger
98N/A * for a popup-menu
98N/A * @param scrollType the type of scrolling which should take place in
98N/A * response to this event; valid values are
98N/A * <code>WHEEL_UNIT_SCROLL</code> and
98N/A * <code>WHEEL_BLOCK_SCROLL</code>
98N/A * @param scrollAmount for scrollType <code>WHEEL_UNIT_SCROLL</code>,
98N/A * the number of units to be scrolled
98N/A * @param wheelRotation the integer number of "clicks" by which the mouse wheel
98N/A * was rotated
98N/A * @param preciseWheelRotation the double number of "clicks" by which the mouse wheel
98N/A * was rotated
98N/A *
98N/A * @throws IllegalArgumentException if <code>source</code> is null
98N/A * @see MouseEvent#MouseEvent(java.awt.Component, int, long, int, int, int, int, boolean)
98N/A * @see MouseEvent#MouseEvent(java.awt.Component, int, long, int, int, int, int, int, int, boolean, int)
98N/A * @since 1.7
98N/A */
98N/A public MouseWheelEvent (Component source, int id, long when, int modifiers,
98N/A int x, int y, int xAbs, int yAbs, int clickCount, boolean popupTrigger,
98N/A int scrollType, int scrollAmount, int wheelRotation, double preciseWheelRotation) {
98N/A
0N/A super(source, id, when, modifiers, x, y, xAbs, yAbs, clickCount,
0N/A popupTrigger, MouseEvent.NOBUTTON);
0N/A
0N/A this.scrollType = scrollType;
0N/A this.scrollAmount = scrollAmount;
0N/A this.wheelRotation = wheelRotation;
98N/A this.preciseWheelRotation = preciseWheelRotation;
98N/A
0N/A }
0N/A
0N/A /**
0N/A * Returns the type of scrolling that should take place in response to this
0N/A * event. This is determined by the native platform. Legal values are:
0N/A * <ul>
0N/A * <li> MouseWheelEvent.WHEEL_UNIT_SCROLL
0N/A * <li> MouseWheelEvent.WHEEL_BLOCK_SCROLL
0N/A * </ul>
0N/A *
0N/A * @return either MouseWheelEvent.WHEEL_UNIT_SCROLL or
0N/A * MouseWheelEvent.WHEEL_BLOCK_SCROLL, depending on the configuration of
0N/A * the native platform.
0N/A * @see java.awt.Adjustable#getUnitIncrement
0N/A * @see java.awt.Adjustable#getBlockIncrement
0N/A * @see javax.swing.Scrollable#getScrollableUnitIncrement
0N/A * @see javax.swing.Scrollable#getScrollableBlockIncrement
0N/A */
0N/A public int getScrollType() {
0N/A return scrollType;
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of units that should be scrolled per
0N/A * click of mouse wheel rotation.
0N/A * Only valid if <code>getScrollType</code> returns
0N/A * <code>MouseWheelEvent.WHEEL_UNIT_SCROLL</code>
0N/A *
0N/A * @return number of units to scroll, or an undefined value if
0N/A * <code>getScrollType</code> returns
0N/A * <code>MouseWheelEvent.WHEEL_BLOCK_SCROLL</code>
0N/A * @see #getScrollType
0N/A */
0N/A public int getScrollAmount() {
0N/A return scrollAmount;
0N/A }
0N/A
0N/A /**
98N/A * Returns the number of "clicks" the mouse wheel was rotated, as an integer.
98N/A * A partial rotation may occur if the mouse supports a high-resolution wheel.
98N/A * In this case, the method returns zero until a full "click" has been accumulated.
0N/A *
0N/A * @return negative values if the mouse wheel was rotated up/away from
0N/A * the user, and positive values if the mouse wheel was rotated down/
0N/A * towards the user
98N/A * @see #getPreciseWheelRotation
0N/A */
0N/A public int getWheelRotation() {
0N/A return wheelRotation;
0N/A }
0N/A
0N/A /**
98N/A * Returns the number of "clicks" the mouse wheel was rotated, as a double.
98N/A * A partial rotation may occur if the mouse supports a high-resolution wheel.
98N/A * In this case, the return value will include a fractional "click".
98N/A *
98N/A * @return negative values if the mouse wheel was rotated up or away from
98N/A * the user, and positive values if the mouse wheel was rotated down or
98N/A * towards the user
98N/A * @see #getWheelRotation
98N/A * @since 1.7
98N/A */
98N/A public double getPreciseWheelRotation() {
98N/A return preciseWheelRotation;
98N/A }
98N/A
98N/A /**
0N/A * This is a convenience method to aid in the implementation of
0N/A * the common-case MouseWheelListener - to scroll a ScrollPane or
0N/A * JScrollPane by an amount which conforms to the platform settings.
0N/A * (Note, however, that <code>ScrollPane</code> and
0N/A * <code>JScrollPane</code> already have this functionality built in.)
0N/A * <P>
0N/A * This method returns the number of units to scroll when scroll type is
0N/A * MouseWheelEvent.WHEEL_UNIT_SCROLL, and should only be called if
0N/A * <code>getScrollType</code> returns MouseWheelEvent.WHEEL_UNIT_SCROLL.
0N/A * <P>
0N/A * Direction of scroll, amount of wheel movement,
0N/A * and platform settings for wheel scrolling are all accounted for.
0N/A * This method does not and cannot take into account value of the
0N/A * Adjustable/Scrollable unit increment, as this will vary among
0N/A * scrolling components.
0N/A * <P>
0N/A * A simplified example of how this method might be used in a
0N/A * listener:
0N/A * <pre>
0N/A * mouseWheelMoved(MouseWheelEvent event) {
0N/A * ScrollPane sp = getScrollPaneFromSomewhere();
0N/A * Adjustable adj = sp.getVAdjustable()
0N/A * if (MouseWheelEvent.getScrollType() == WHEEL_UNIT_SCROLL) {
0N/A * int totalScrollAmount =
0N/A * event.getUnitsToScroll() *
0N/A * adj.getUnitIncrement();
0N/A * adj.setValue(adj.getValue() + totalScrollAmount);
0N/A * }
0N/A * }
0N/A * </pre>
0N/A *
0N/A * @return the number of units to scroll based on the direction and amount
0N/A * of mouse wheel rotation, and on the wheel scrolling settings of the
0N/A * native platform
0N/A * @see #getScrollType
0N/A * @see #getScrollAmount
0N/A * @see MouseWheelListener
0N/A * @see java.awt.Adjustable
0N/A * @see java.awt.Adjustable#getUnitIncrement
0N/A * @see javax.swing.Scrollable
0N/A * @see javax.swing.Scrollable#getScrollableUnitIncrement
0N/A * @see java.awt.ScrollPane
0N/A * @see java.awt.ScrollPane#setWheelScrollingEnabled
0N/A * @see javax.swing.JScrollPane
0N/A * @see javax.swing.JScrollPane#setWheelScrollingEnabled
0N/A */
0N/A public int getUnitsToScroll() {
0N/A return scrollAmount * wheelRotation;
0N/A }
0N/A
0N/A /**
0N/A * Returns a parameter string identifying this event.
0N/A * This method is useful for event-logging and for debugging.
0N/A *
0N/A * @return a string identifying the event and its attributes
0N/A */
0N/A public String paramString() {
0N/A String scrollTypeStr = null;
0N/A
0N/A if (getScrollType() == WHEEL_UNIT_SCROLL) {
0N/A scrollTypeStr = "WHEEL_UNIT_SCROLL";
0N/A }
0N/A else if (getScrollType() == WHEEL_BLOCK_SCROLL) {
0N/A scrollTypeStr = "WHEEL_BLOCK_SCROLL";
0N/A }
0N/A else {
0N/A scrollTypeStr = "unknown scroll type";
0N/A }
0N/A return super.paramString()+",scrollType="+scrollTypeStr+
0N/A ",scrollAmount="+getScrollAmount()+",wheelRotation="+
98N/A getWheelRotation()+",preciseWheelRotation="+getPreciseWheelRotation();
0N/A }
0N/A}