~josejuan-sanchez/+junk/original-jhv-experimental-version

« back to all changes in this revision

Viewing changes to src/jhv/src/org/helioviewer/jhv/gui/components/WheelSupport.java

  • Committer: José Juan Sánchez Hernández
  • Date: 2013-02-05 13:32:08 UTC
  • Revision ID: josejuan.sanchez@gmail.com-20130205133208-dfz1sh1uge5pjkny
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.helioviewer.jhv.gui.components;
 
2
 
 
3
/*
 
4
 * @(#)SpinnerWheelSupport.java 7/28/2006
 
5
 *
 
6
 * Copyright 2002 - 2006 JIDE Software Inc. All rights reserved.
 
7
 */
 
8
 
 
9
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. You can call
 
16
 * {@link #installMouseWheelSupport(javax.swing.JSpinner)} to add the support
 
17
 * and {@link #uninstallMouseWheelSupport(JComponent)} to remove the support.
 
18
 */
 
19
public class WheelSupport {
 
20
 
 
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";
 
26
 
 
27
    public static void installMouseWheelSupport(final JSpinner spinner) {
 
28
        MouseWheelListener l = new MouseWheelListener() {
 
29
            public void mouseWheelMoved(MouseWheelEvent e) {
 
30
                if (!spinner.isEnabled()) {
 
31
                    return;
 
32
                }
 
33
 
 
34
                int rotation = e.getWheelRotation();
 
35
                if (rotation < 0) {
 
36
                    Action action = spinner.getActionMap().get(SPINNER_ACTION_NAME_INCREMENT);
 
37
                    if (action != null) {
 
38
                        action.actionPerformed(new ActionEvent(e.getSource(), 0, SPINNER_ACTION_NAME_INCREMENT));
 
39
                    }
 
40
                } else if (rotation > 0) {
 
41
                    Action action = spinner.getActionMap().get(SPINNER_ACTION_NAME_DECREMENT);
 
42
                    if (action != null) {
 
43
                        action.actionPerformed(new ActionEvent(e.getSource(), 0, SPINNER_ACTION_NAME_DECREMENT));
 
44
                    }
 
45
                }
 
46
            }
 
47
        };
 
48
        spinner.addMouseWheelListener(l);
 
49
        spinner.putClientProperty(CLIENT_PROPERTY_MOUSE_WHEEL_LISTENER, l);
 
50
    }
 
51
 
 
52
    public static void installMouseWheelSupport(final JSlider slider) {
 
53
        MouseWheelListener l = new MouseWheelListener() {
 
54
            public void mouseWheelMoved(MouseWheelEvent e) {
 
55
                if (!slider.isEnabled()) {
 
56
                    return;
 
57
                }
 
58
 
 
59
                int rotation = e.getWheelRotation();
 
60
                if (rotation < 0) {
 
61
                    Action action = slider.getActionMap().get(SLIDER_ACTION_NAME_INCREMENT);
 
62
                    if (action != null) {
 
63
                        action.actionPerformed(new ActionEvent(e.getSource(), 0, SLIDER_ACTION_NAME_INCREMENT));
 
64
                    }
 
65
                } else if (rotation > 0) {
 
66
                    Action action = slider.getActionMap().get(SLIDER_ACTION_NAME_DECREMENT);
 
67
                    if (action != null) {
 
68
                        action.actionPerformed(new ActionEvent(e.getSource(), 0, SLIDER_ACTION_NAME_DECREMENT));
 
69
                    }
 
70
                }
 
71
            }
 
72
        };
 
73
        slider.addMouseWheelListener(l);
 
74
        slider.putClientProperty(CLIENT_PROPERTY_MOUSE_WHEEL_LISTENER, l);
 
75
    }
 
76
 
 
77
    public static <T extends JComponent> void uninstallMouseWheelSupport(final T component) {
 
78
        MouseWheelListener l = (MouseWheelListener) component.getClientProperty(CLIENT_PROPERTY_MOUSE_WHEEL_LISTENER);
 
79
        if (l != null) {
 
80
            component.removeMouseWheelListener(l);
 
81
        }
 
82
    }
 
83
}