-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathMouseWheelController.java
More file actions
150 lines (125 loc) · 3.52 KB
/
MouseWheelController.java
File metadata and controls
150 lines (125 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import java.awt.event.*;
import javax.swing.*;
/**
* This class allows you to control the units scrolled for each mouse wheel
* rotation relative to the unit increment value of the scroll bar. Specifying
* a scroll amount of 1, is equivalent to clicking the unit scroll button of
* the scroll bar once.
*/
public class MouseWheelController
implements MouseWheelListener
{
private JScrollPane scrollPane;
private int scrollAmount = 0;
private MouseWheelListener[] realListeners;
/**
* Convenience constructor to create the class with a scroll amount of 1.
*
* @param scrollPane the scroll pane being used by the mouse wheel
*/
public MouseWheelController(JScrollPane scrollPane)
{
this(scrollPane, 1);
}
/**
* Create the class with the specified scroll amount.
*
* @param scrollAmount the scroll amount to by used for this scroll pane
* @param scrollPane the scroll pane being used by the mouse wheel
*/
public MouseWheelController(JScrollPane scrollPane, int scrollAmount)
{
this.scrollPane = scrollPane;
setScrollAmount( scrollAmount );
install();
}
/**
* Get the scroll amount
*
* @returns the scroll amount.
*/
public int getScrollAmount()
{
return scrollAmount;
}
/**
* Set the scroll amount. Controls the amount the scrollpane will scroll
* for each mouse wheel rotation. The amount is relative to the unit
* increment value of the scrollbar being scrolled.
*
* @param scrollAmount an integer value. A value of zero will use the
* default scroll amount for your OS.
*/
public void setScrollAmount(int scrollAmount)
{
this.scrollAmount = scrollAmount;
}
/**
* Install this class as the default listener for MouseWheel events.
*/
public void install()
{
if (realListeners != null) return;
// Keep track of original listeners so we can use them to
// redispatch an altered MouseWheelEvent
realListeners = scrollPane.getMouseWheelListeners();
for (MouseWheelListener mwl : realListeners)
{
scrollPane.removeMouseWheelListener(mwl);
}
// Intercept events so they can be redispatched
scrollPane.addMouseWheelListener(this);
}
/**
* Remove the class as the default listener and reinstall the original
* listeners.
*/
public void uninstall()
{
if (realListeners == null) return;
// Remove this class as the default listener
scrollPane.removeMouseWheelListener( this );
// Install the default listeners
for (MouseWheelListener mwl : realListeners)
{
scrollPane.addMouseWheelListener( mwl );
}
realListeners = null;
}
// Implement MouseWheelListener interface
/**
* Redispatch a MouseWheelEvent to the real MouseWheelListeners
*/
public void mouseWheelMoved(MouseWheelEvent e)
{
// Create an altered event to redispatch
if (scrollAmount != 0)
{
e = createScrollAmountEvent( e );
}
// Redispatch the event to original MouseWheelListener
for (MouseWheelListener mwl : realListeners)
{
mwl.mouseWheelMoved( e );
}
}
private MouseWheelEvent createScrollAmountEvent(MouseWheelEvent e)
{
// Reset the scroll amount
MouseWheelEvent mwe = new MouseWheelEvent(
e.getComponent(),
e.getID(),
e.getWhen(),
e.getModifiers(),
e.getX(),
e.getY(),
e.getXOnScreen(),
e.getYOnScreen(),
e.getClickCount(),
e.isPopupTrigger(),
e.getScrollType(),
scrollAmount,
e.getWheelRotation());
return mwe;
}
}