下面列出了java.awt.event.MouseWheelEvent#getScrollType() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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 );
}
};
}
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;
}
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;
}
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;
}
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;
}
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);
}
}
}
/**
* 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;
}
}
@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());
}
}
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());
}
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;
}
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));
}
@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);
}
}
/**
* 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());
}
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);
}
}
}
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;
}
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();
}
}
private void handleMouseWheelEvent(MouseWheelEvent evt, MouseWheelListener delegate) {
if (scrollpane.isWheelScrollingEnabled() &&
evt.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL)
{
mouseWheelMoved(evt);
evt.consume();
} else {
delegate.mouseWheelMoved(evt);
}
}
@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);
}
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);
}
/**
* 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());
}