1
package org.helioviewer.jhv.gui.components;
4
* @(#)SpinnerWheelSupport.java 7/28/2006
6
* Copyright 2002 - 2006 JIDE Software Inc. All rights reserved.
10
import java.awt.event.ActionEvent;
11
import java.awt.event.MouseWheelEvent;
12
import java.awt.event.MouseWheelListener;
15
* A helper class to add mouse wheel support to JSpinner. You can call
16
* {@link #installMouseWheelSupport(javax.swing.JSpinner)} to add the support
17
* and {@link #uninstallMouseWheelSupport(JComponent)} to remove the support.
19
public class WheelSupport {
21
public static final String CLIENT_PROPERTY_MOUSE_WHEEL_LISTENER = "mouseWheelListener";
22
protected static final String SPINNER_ACTION_NAME_INCREMENT = "increment";
23
protected static final String SPINNER_ACTION_NAME_DECREMENT = "decrement";
24
protected static final String SLIDER_ACTION_NAME_INCREMENT = "positiveUnitIncrement";
25
protected static final String SLIDER_ACTION_NAME_DECREMENT = "negativeUnitIncrement";
27
public static void installMouseWheelSupport(final JSpinner spinner) {
28
MouseWheelListener l = new MouseWheelListener() {
29
public void mouseWheelMoved(MouseWheelEvent e) {
30
if (!spinner.isEnabled()) {
34
int rotation = e.getWheelRotation();
36
Action action = spinner.getActionMap().get(SPINNER_ACTION_NAME_INCREMENT);
38
action.actionPerformed(new ActionEvent(e.getSource(), 0, SPINNER_ACTION_NAME_INCREMENT));
40
} else if (rotation > 0) {
41
Action action = spinner.getActionMap().get(SPINNER_ACTION_NAME_DECREMENT);
43
action.actionPerformed(new ActionEvent(e.getSource(), 0, SPINNER_ACTION_NAME_DECREMENT));
48
spinner.addMouseWheelListener(l);
49
spinner.putClientProperty(CLIENT_PROPERTY_MOUSE_WHEEL_LISTENER, l);
52
public static void installMouseWheelSupport(final JSlider slider) {
53
MouseWheelListener l = new MouseWheelListener() {
54
public void mouseWheelMoved(MouseWheelEvent e) {
55
if (!slider.isEnabled()) {
59
int rotation = e.getWheelRotation();
61
Action action = slider.getActionMap().get(SLIDER_ACTION_NAME_INCREMENT);
63
action.actionPerformed(new ActionEvent(e.getSource(), 0, SLIDER_ACTION_NAME_INCREMENT));
65
} else if (rotation > 0) {
66
Action action = slider.getActionMap().get(SLIDER_ACTION_NAME_DECREMENT);
68
action.actionPerformed(new ActionEvent(e.getSource(), 0, SLIDER_ACTION_NAME_DECREMENT));
73
slider.addMouseWheelListener(l);
74
slider.putClientProperty(CLIENT_PROPERTY_MOUSE_WHEEL_LISTENER, l);
77
public static <T extends JComponent> void uninstallMouseWheelSupport(final T component) {
78
MouseWheelListener l = (MouseWheelListener) component.getClientProperty(CLIENT_PROPERTY_MOUSE_WHEEL_LISTENER);
80
component.removeMouseWheelListener(l);