01: /*
02: * @(#)SpinnerWheelSupport.java 7/28/2006
03: *
04: * Copyright 2002 - 2006 JIDE Software Inc. All rights reserved.
05: */
06:
07: package com.jidesoft.spinner;
08:
09: import javax.swing.*;
10: import java.awt.event.ActionEvent;
11: import java.awt.event.MouseWheelEvent;
12: import java.awt.event.MouseWheelListener;
13:
14: /**
15: * A helper class to add mouse wheel support to JSpinner.
16: * You can call {@link #installMouseWheelSupport(javax.swing.JSpinner)} to add the support
17: * and {@link #uninstallMouseWheelSupport(javax.swing.JSpinner)} to remove the support.
18: */
19: public class SpinnerWheelSupport {
20:
21: public static final String CLIENT_PROPERTY_MOUSE_WHEEL_LISTENER = "mouseWheelListener";
22: protected static final String ACTION_NAME_INCREMENT = "increment";
23: protected static final String ACTION_NAME_DECREMENT = "decrement";
24:
25: public static void installMouseWheelSupport(final JSpinner spinner) {
26: MouseWheelListener l = new MouseWheelListener() {
27: public void mouseWheelMoved(MouseWheelEvent e) {
28: int rotation = e.getWheelRotation();
29: if (rotation > 0) {
30: Action action = spinner.getActionMap().get(
31: ACTION_NAME_INCREMENT);
32: if (action != null) {
33: action
34: .actionPerformed(new ActionEvent(e
35: .getSource(), 0,
36: ACTION_NAME_INCREMENT));
37: }
38: } else if (rotation < 0) {
39: Action action = spinner.getActionMap().get(
40: ACTION_NAME_DECREMENT);
41: if (action != null) {
42: action
43: .actionPerformed(new ActionEvent(e
44: .getSource(), 0,
45: ACTION_NAME_DECREMENT));
46: }
47: }
48: }
49: };
50: spinner.addMouseWheelListener(l);
51: spinner.putClientProperty(CLIENT_PROPERTY_MOUSE_WHEEL_LISTENER,
52: l);
53: }
54:
55: public static void uninstallMouseWheelSupport(final JSpinner spinner) {
56: MouseWheelListener l = (MouseWheelListener) spinner
57: .getClientProperty(CLIENT_PROPERTY_MOUSE_WHEEL_LISTENER);
58: if (l != null) {
59: spinner.removeMouseWheelListener(l);
60: }
61: }
62: }
|