Java Code Examples for java.awt.event.MouseWheelEvent#getScrollType()

The following examples show how to use java.awt.event.MouseWheelEvent#getScrollType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InteractiveCanvasComponent.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void mouseWheelMoved(MouseWheelEvent e) {
    // Mouse wheel zooming takes precedence over scrolling
    if (isMouseZoomingEnabled() &&
        e.getSource() == InteractiveCanvasComponent.this) return;

    // Change the ScrollBar value
    if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
        int unitsToScroll = e.getUnitsToScroll();
        int direction = unitsToScroll < 0 ? -1 : 1;
        if (unitsToScroll != 0) {
            int increment = scrollBar.getUnitIncrement(direction);
            int oldValue = scrollBar.getValue();
            int newValue = oldValue + increment * unitsToScroll;
            newValue = Math.max(Math.min(newValue, scrollBar.getMaximum() -
                    scrollBar.getVisibleAmount()), scrollBar.getMinimum());
            if (oldValue != newValue) scrollBar.setValue(newValue);
        }
    }
}
 
Example 2
Source File: FlatScrollPaneUI.java    From FlatLaf with Apache License 2.0 6 votes vote down vote up
@Override
protected MouseWheelListener createMouseWheelListener() {
	return new BasicScrollPaneUI.MouseWheelHandler() {
		@Override
		public void mouseWheelMoved( MouseWheelEvent e ) {
			// Note: Getting UI value "ScrollPane.smoothScrolling" here to allow
			// applications to turn smooth scrolling on or off at any time
			// (e.g. in application options dialog).
			if( UIManager.getBoolean( "ScrollPane.smoothScrolling" ) &&
				scrollpane.isWheelScrollingEnabled() &&
				e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL &&
				e.getPreciseWheelRotation() != 0 &&
				e.getPreciseWheelRotation() != e.getWheelRotation() )
			{
				mouseWheelMovedSmooth( e );
			} else
				super.mouseWheelMoved( e );
		}
	};
}
 
Example 3
Source File: ScrollPaneWheelScroller.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public static int getIncrementFromAdjustable(Adjustable adj,
                                             MouseWheelEvent e) {
    if (log.isLoggable(PlatformLogger.Level.FINE)) {
        if (adj == null) {
            log.fine("Assertion (adj != null) failed");
        }
    }

    int increment = 0;

    if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
        increment = e.getUnitsToScroll() * adj.getUnitIncrement();
    }
    else if (e.getScrollType() == MouseWheelEvent.WHEEL_BLOCK_SCROLL) {
        increment = adj.getBlockIncrement() * e.getWheelRotation();
    }
    return increment;
}
 
Example 4
Source File: ScrollPaneWheelScroller.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static int getIncrementFromAdjustable(Adjustable adj,
                                             MouseWheelEvent e) {
    if (log.isLoggable(PlatformLogger.Level.FINE)) {
        if (adj == null) {
            log.fine("Assertion (adj != null) failed");
        }
    }

    int increment = 0;

    if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
        increment = e.getUnitsToScroll() * adj.getUnitIncrement();
    }
    else if (e.getScrollType() == MouseWheelEvent.WHEEL_BLOCK_SCROLL) {
        increment = adj.getBlockIncrement() * e.getWheelRotation();
    }
    return increment;
}
 
Example 5
Source File: ScrollPaneWheelScroller.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static int getIncrementFromAdjustable(Adjustable adj,
                                             MouseWheelEvent e) {
    if (log.isLoggable(PlatformLogger.Level.FINE)) {
        if (adj == null) {
            log.fine("Assertion (adj != null) failed");
        }
    }

    int increment = 0;

    if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
        increment = e.getUnitsToScroll() * adj.getUnitIncrement();
    }
    else if (e.getScrollType() == MouseWheelEvent.WHEEL_BLOCK_SCROLL) {
        increment = adj.getBlockIncrement() * e.getWheelRotation();
    }
    return increment;
}
 
Example 6
Source File: ScrollPaneWheelScroller.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public static int getIncrementFromAdjustable(Adjustable adj,
                                             MouseWheelEvent e) {
    if (log.isLoggable(PlatformLogger.Level.FINE)) {
        if (adj == null) {
            log.fine("Assertion (adj != null) failed");
        }
    }

    int increment = 0;

    if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
        increment = e.getUnitsToScroll() * adj.getUnitIncrement();
    }
    else if (e.getScrollType() == MouseWheelEvent.WHEEL_BLOCK_SCROLL) {
        increment = adj.getBlockIncrement() * e.getWheelRotation();
    }
    return increment;
}
 
Example 7
Source File: WebViewPanel.java    From wandora with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void processMouseWheelEvent(MouseWheelEvent e) {
    MouseWheelEvent ee = new MouseWheelEvent(
            (Component) e.getSource(), e.getID(), e.getWhen(),
            e.getModifiers(), e.getX(), e.getY(), e.getXOnScreen(),
            e.getYOnScreen(), e.getClickCount(),
            e.isPopupTrigger(), e.getScrollType(), e.getScrollAmount(),
            e.getWheelRotation(), e.getPreciseWheelRotation());
    super.processMouseWheelEvent(ee);
}
 
Example 8
Source File: CharMap4Grid.java    From sldeditor with GNU General Public License v3.0 5 votes vote down vote up
/**
 * mouseWheelMoved
 *
 * <p>This is the mouse wheel listener, for the scroll wheel on some mice. The "unit" scroll
 * uses the local system preferences for how many lines/rows per click of the mouse. The "unit"
 * scroll may be too big if there are only one or two lines in the display.
 *
 * <p>The mouse wheel listener has no interaction with the other mouse listeners above.
 */
public void mouseWheelMoved(MouseWheelEvent event) {
    switch (event.getScrollType()) // different mice scroll differently
    {
        case (MouseWheelEvent.WHEEL_BLOCK_SCROLL):
            {
                charMap4.getGridScroll()
                        .setValue(
                                charMap4.getGridScroll().getValue()
                                        + (event.getWheelRotation()
                                                * charMap4.getGridScroll()
                                                        .getBlockIncrement()));
            }
            break;

        case (MouseWheelEvent.WHEEL_UNIT_SCROLL):
            {
                int i = charMap4.getGridScroll().getBlockIncrement(); // maximum scroll rows
                i = Math.max((-i), Math.min(i, event.getUnitsToScroll())); // limits
                charMap4.getGridScroll().setValue(charMap4.getGridScroll().getValue() + i);
                // scroll using limited local preferences
            }
            break;

        default: // ignore anything that we don't recognize
            break;
    }
}
 
Example 9
Source File: SmoothScrollPaneUI.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void handleMouseWheelEvent(MouseWheelEvent evt, MouseWheelListener delegate) {
    if (scrollpane.isWheelScrollingEnabled() &&
        evt.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL)
    {
        mouseWheelMoved(evt);
        evt.consume();
    } else {
        delegate.mouseWheelMoved(evt);
    }
}
 
Example 10
Source File: ProfilerTableContainer.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void scroll(JScrollBar scroller, MouseWheelEvent event) {
    if (event.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
        int direction = event.getUnitsToScroll() < 0 ? -1 : 1;
        int increment = scroller.getUnitIncrement(direction);
        int amount = event.getScrollAmount();
        int oldValue = scroller.getValue();
        int newValue = oldValue + increment * amount * direction;
        if (oldValue != newValue) scroller.setValue(newValue);
        event.consume();
    }
}
 
Example 11
Source File: ListHelper.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
static boolean doWheelScroll(XVerticalScrollbar vsb,
                                 XHorizontalScrollbar hsb,
                                 MouseWheelEvent e) {
    XScrollbar scroll = null;
    int wheelRotation;

    // Determine which, if any, sb to scroll
    if (vsb != null) {
        scroll = vsb;
    }
    else if (hsb != null) {
        scroll = hsb;
    }
    else { // Neither scrollbar is showing
        return false;
    }

    wheelRotation = e.getWheelRotation();

    // Check if scroll is necessary
    if ((wheelRotation < 0 && scroll.getValue() > scroll.getMinimum()) ||
        (wheelRotation > 0 && scroll.getValue() < scroll.getMaximum()) ||
        wheelRotation != 0) {

        int type = e.getScrollType();
        int incr;
        if (type == MouseWheelEvent.WHEEL_BLOCK_SCROLL) {
            incr = wheelRotation * scroll.getBlockIncrement();
        }
        else { // type is WHEEL_UNIT_SCROLL
            incr = e.getUnitsToScroll() * scroll.getUnitIncrement();
        }
        scroll.setValue(scroll.getValue() + incr);
        return true;
    }
    return false;
}
 
Example 12
Source File: ChartPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void scroll(JScrollBar scrollBar, MouseWheelEvent e) {
    if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
        int unitsToScroll = e.getUnitsToScroll();
        int direction = unitsToScroll < 0 ? -1 : 1;
        if (unitsToScroll != 0) {
            int increment = scrollBar.getUnitIncrement(direction);
            int oldValue = scrollBar.getValue();
            int newValue = oldValue + increment * unitsToScroll;
            newValue = Math.max(Math.min(newValue, scrollBar.getMaximum() -
                    scrollBar.getVisibleAmount()), scrollBar.getMinimum());
            if (oldValue != newValue) scrollBar.setValue(newValue);
        }
    }
}
 
Example 13
Source File: UIUtilities.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Clones a {@link MouseEvent}.
 *
 * @param event       The event to clone.
 * @param refreshTime Pass in {@code true} to generate a new time stamp.
 * @return The new {@link MouseEvent}.
 */
public static final MouseEvent cloneMouseEvent(MouseEvent event, boolean refreshTime) {
    if (event instanceof MouseWheelEvent) {
        MouseWheelEvent old = (MouseWheelEvent) event;
        return new MouseWheelEvent((Component) old.getSource(), old.getID(), refreshTime ? System.currentTimeMillis() : event.getWhen(), old.getModifiersEx(), old.getX(), old.getY(), old.getClickCount(), old.isPopupTrigger(), old.getScrollType(), old.getScrollAmount(), old.getWheelRotation());
    }
    return new MouseEvent((Component) event.getSource(), event.getID(), refreshTime ? System.currentTimeMillis() : event.getWhen(), event.getModifiersEx(), event.getX(), event.getY(), event.getClickCount(), event.isPopupTrigger());
}
 
Example 14
Source File: GanttPanel.java    From openvisualtraceroute with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void mouseWheelMoved(final MouseWheelEvent e) {
	if (e.getScrollType() != MouseWheelEvent.WHEEL_UNIT_SCROLL) {
		return;
	}
	if (e.getWheelRotation() < 0) {
		zoomChartAxis(e.getPoint(), true);
	} else {
		zoomChartAxis(e.getPoint(), false);
	}
}
 
Example 15
Source File: PrintPreviewComponent.java    From ramus with GNU General Public License v3.0 5 votes vote down vote up
public PrintPreviewComponent(RamusPrintable printable, int columnCount,
                             GUIFramework framework) {
    this.printable = printable;
    this.columnCount = columnCount;
    this.framework = framework;
    MouseWheelListener l = new MouseWheelListener() {

        @Override
        public void mouseWheelMoved(MouseWheelEvent e) {
            if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
                if (e.getModifiers() == KeyEvent.CTRL_MASK) {
                    double r = e.getWheelRotation();
                    double zoom = getZoom() - 0.2 * r;
                    setCurrentZoom(zoom);
                } else {
                    Rectangle rect = getVisibleRect();
                    scrollRectToVisible(new Rectangle(rect.x, rect.y
                            + e.getWheelRotation() * 150, rect.width,
                            rect.height));
                }
            }
        }
    };
    this.addMouseWheelListener(l);
    layout = Options.getInteger("PREVIW_LAYOUT", PREV_LAYOUT_GRID);
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            setCurrentZoom(Options.getDouble("PREV_ZOOM", 1d));
        }
    });
    setCurrentZoom(Options.getDouble("PREV_ZOOM", 1d));
}
 
Example 16
Source File: ListHelper.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
static boolean doWheelScroll(XVerticalScrollbar vsb,
                                 XHorizontalScrollbar hsb,
                                 MouseWheelEvent e) {
    XScrollbar scroll = null;
    int wheelRotation;

    // Determine which, if any, sb to scroll
    if (vsb != null) {
        scroll = vsb;
    }
    else if (hsb != null) {
        scroll = hsb;
    }
    else { // Neither scrollbar is showing
        return false;
    }

    wheelRotation = e.getWheelRotation();

    // Check if scroll is necessary
    if ((wheelRotation < 0 && scroll.getValue() > scroll.getMinimum()) ||
        (wheelRotation > 0 && scroll.getValue() < scroll.getMaximum()) ||
        wheelRotation != 0) {

        int type = e.getScrollType();
        int incr;
        if (type == MouseWheelEvent.WHEEL_BLOCK_SCROLL) {
            incr = wheelRotation * scroll.getBlockIncrement();
        }
        else { // type is WHEEL_UNIT_SCROLL
            incr = e.getUnitsToScroll() * scroll.getUnitIncrement();
        }
        scroll.setValue(scroll.getValue() + incr);
        return true;
    }
    return false;
}
 
Example 17
Source File: MouseEventUI.java    From darklaf with MIT License 5 votes vote down vote up
private MouseWheelEvent createMouseWheelEvent(final MouseWheelEvent mouseWheelEvent, final Point point,
                                              final Component target) {
    return new MouseWheelEvent(target,
                               mouseWheelEvent.getID(),
                               mouseWheelEvent.getWhen(),
                               mouseWheelEvent.getModifiersEx(),
                               point.x,
                               point.y,
                               mouseWheelEvent.getClickCount(),
                               mouseWheelEvent.isPopupTrigger(),
                               mouseWheelEvent.getScrollType(),
                               mouseWheelEvent.getScrollAmount(),
                               mouseWheelEvent.getWheelRotation());
}
 
Example 18
Source File: DirectScrollPanel.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void mouseWheelMoved(MouseWheelEvent event) {
    if (event.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
        int        amt = event.getUnitsToScroll();
        JScrollBar bar = event.isShiftDown() ? mHSB : mVSB;
        bar.setValue(bar.getValue() + amt * bar.getUnitIncrement());
    }
}
 
Example 19
Source File: VisualGraphScrollWheelPanningPlugin.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private void pan(MouseWheelEvent e) {
	GraphViewer<V, E> viewer = getGraphViewer(e);

	//
	// Number of 'units' by which to scroll.  This is defined by the OS and is usually
	// something like 'lines of text'.
	//
	int scrollAmount = 1;
	if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
		scrollAmount = e.getScrollAmount();
	}

	//
	// The amount the mouse wheel has been rotated.  By default this is usually 1, but 
	// users can change how the OS accelerates mouse scrolling.
	//
	int wheelRotation = -e.getWheelRotation();

	//
	// A magic magnification amount.  This was chosen by testing on a few platforms.
	//
	int arbitraryAcceleration = 10;

	//
	// The scale of the current graph.  We need to change the scroll amount when scaled out
	// so that we don't end up with tiny scrolling when zoomed out.
	//		
	Double scale = GraphViewerUtils.getGraphScale(viewer);
	int unscaledOffset = wheelRotation * scrollAmount * arbitraryAcceleration;
	int offset = (int) (unscaledOffset * (1 / scale));

	Point newPoint = new Point(0, offset);

	if (e.isAltDown()) {
		// control-alt is a horizontal pan
		newPoint.setLocation(offset, 0);
	}

	VisualGraphViewUpdater<V, E> updater = viewer.getViewUpdater();
	updater.moveViewerLocationWithoutAnimation(newPoint);
}
 
Example 20
Source File: UIUtilities.java    From gcs with Mozilla Public License 2.0 3 votes vote down vote up
/**
 * Clones a {@link MouseEvent}.
 *
 * @param event       The event to clone.
 * @param source      Pass in a new source.
 * @param where       Pass in a new location.
 * @param refreshTime Pass in {@code true} to generate a new time stamp.
 * @return The new {@link MouseEvent}.
 */
public static final MouseEvent cloneMouseEvent(MouseEvent event, Component source, Point where, boolean refreshTime) {
    if (event instanceof MouseWheelEvent) {
        MouseWheelEvent old = (MouseWheelEvent) event;
        return new MouseWheelEvent(source, old.getID(), refreshTime ? System.currentTimeMillis() : event.getWhen(), old.getModifiersEx(), where.x, where.y, old.getClickCount(), old.isPopupTrigger(), old.getScrollType(), old.getScrollAmount(), old.getWheelRotation());
    }
    return new MouseEvent(source, event.getID(), refreshTime ? System.currentTimeMillis() : event.getWhen(), event.getModifiersEx(), where.x, where.y, event.getClickCount(), event.isPopupTrigger());
}