| 1 |
/* |
| 2 |
* Copyright (c) 2005-2010 Laf-Widget Kirill Grouchnikov. All Rights Reserved. |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions are met: |
| 6 |
* |
| 7 |
* o Redistributions of source code must retain the above copyright notice, |
| 8 |
* this list of conditions and the following disclaimer. |
| 9 |
* |
| 10 |
* o Redistributions in binary form must reproduce the above copyright notice, |
| 11 |
* this list of conditions and the following disclaimer in the documentation |
| 12 |
* and/or other materials provided with the distribution. |
| 13 |
* |
| 14 |
* o Neither the name of Laf-Widget Kirill Grouchnikov nor the names of |
| 15 |
* its contributors may be used to endorse or promote products derived |
| 16 |
* from this software without specific prior written permission. |
| 17 |
* |
| 18 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 20 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 21 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 22 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 23 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 25 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 26 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 27 |
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 28 |
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
*/ |
| 30 |
package org.pushingpixels.lafwidget.scroll; |
| 31 |
|
| 32 |
import java.awt.*; |
| 33 |
import java.awt.event.*; |
| 34 |
|
| 35 |
import javax.swing.*; |
| 36 |
import javax.swing.border.Border; |
| 37 |
|
| 38 |
/** |
| 39 |
* Christopher Deckers ([email protected]) http://www.nextencia.net |
| 40 |
* |
| 41 |
* @author Christopher Deckers |
| 42 |
*/ |
| 43 |
public class AutoScrollActivator { |
| 44 |
|
| 45 |
protected JScrollPane scrollPane; |
| 46 |
|
| 47 |
public AutoScrollActivator(JScrollPane scrollPane) { |
| 48 |
this.scrollPane = scrollPane; |
| 49 |
configureScrollPane(); |
| 50 |
} |
| 51 |
|
| 52 |
protected static class AutoScrollProperties { |
| 53 |
public Point startLocation; |
| 54 |
public Point currentLocation; |
| 55 |
public Timer timer; |
| 56 |
public AWTEventListener toolkitListener; |
| 57 |
public boolean isDragMode; |
| 58 |
public JPopupMenu iconPopupMenu; |
| 59 |
} |
| 60 |
|
| 61 |
protected AutoScrollProperties autoScrollProperties; |
| 62 |
|
| 63 |
protected void deactivateAutoScroll() { |
| 64 |
if (autoScrollProperties == null) |
| 65 |
return; |
| 66 |
autoScrollProperties.timer.stop(); |
| 67 |
Toolkit.getDefaultToolkit().removeAWTEventListener( |
| 68 |
autoScrollProperties.toolkitListener); |
| 69 |
autoScrollProperties.iconPopupMenu.setVisible(false); |
| 70 |
autoScrollProperties = null; |
| 71 |
} |
| 72 |
|
| 73 |
protected void activateAutoScroll(MouseEvent e) { |
| 74 |
autoScrollProperties = new AutoScrollProperties(); |
| 75 |
autoScrollProperties.isDragMode = false; |
| 76 |
JViewport viewport = scrollPane.getViewport(); |
| 77 |
PointerInfo pi = MouseInfo.getPointerInfo(); |
| 78 |
if (pi == null) return; // mouse not on any device |
| 79 |
|
| 80 |
autoScrollProperties.currentLocation = pi.getLocation(); |
| 81 |
SwingUtilities.convertPointFromScreen( |
| 82 |
autoScrollProperties.currentLocation, viewport); |
| 83 |
autoScrollProperties.startLocation = autoScrollProperties.currentLocation; |
| 84 |
// We use a popup menu so that it can be heavyweight or lightweight |
| 85 |
// depending on the context. |
| 86 |
// By default it is probably lightweight and thus uses alpha |
| 87 |
// transparency |
| 88 |
final JPopupMenu iconPopupMenu = new JPopupMenu() { |
| 89 |
@Override |
| 90 |
public void setBorder(Border border) { |
| 91 |
// Overriden to avoid having a border set by the L&F |
| 92 |
} |
| 93 |
}; |
| 94 |
iconPopupMenu.setFocusable(false); |
| 95 |
iconPopupMenu.setOpaque(false); |
| 96 |
JLabel iconLabel = new JLabel(getAutoScrollIcon()); |
| 97 |
iconLabel.addMouseWheelListener(new MouseWheelListener() { |
| 98 |
@Override |
| 99 |
public void mouseWheelMoved(MouseWheelEvent e) { |
| 100 |
deactivateAutoScroll(); |
| 101 |
} |
| 102 |
}); |
| 103 |
iconPopupMenu.add(iconLabel); |
| 104 |
iconPopupMenu.addMouseListener(new MouseAdapter() { |
| 105 |
@Override |
| 106 |
public void mousePressed(MouseEvent e) { |
| 107 |
iconPopupMenu.setVisible(false); |
| 108 |
} |
| 109 |
}); |
| 110 |
autoScrollProperties.iconPopupMenu = iconPopupMenu; |
| 111 |
Dimension iconPopupMenuSize = iconPopupMenu.getPreferredSize(); |
| 112 |
iconPopupMenu.show(viewport, autoScrollProperties.startLocation.x |
| 113 |
- iconPopupMenuSize.width / 2, |
| 114 |
autoScrollProperties.startLocation.y - iconPopupMenuSize.height |
| 115 |
/ 2); |
| 116 |
// Assumption: the popup menu has a parent that is itself added to the |
| 117 |
// glass pane or to a window. |
| 118 |
// Some L&F will create borders to that parent, and we don't want that. |
| 119 |
Container parent = iconPopupMenu.getParent(); |
| 120 |
if (parent instanceof JComponent) { |
| 121 |
((JComponent) parent).setBorder(null); |
| 122 |
} |
| 123 |
ActionListener actionListener = new ActionListener() { |
| 124 |
@Override |
| 125 |
public void actionPerformed(ActionEvent e) { |
| 126 |
JViewport viewport = scrollPane.getViewport(); |
| 127 |
Component view = viewport.getView(); |
| 128 |
if (view == null) { |
| 129 |
return; |
| 130 |
} |
| 131 |
Point viewPosition = viewport.getViewPosition(); |
| 132 |
int offsetX = autoScrollProperties.currentLocation.x |
| 133 |
- autoScrollProperties.startLocation.x; |
| 134 |
int offsetY = autoScrollProperties.currentLocation.y |
| 135 |
- autoScrollProperties.startLocation.y; |
| 136 |
offsetX = offsetX > 0 ? Math.max(0, offsetX - 4) : Math.min(0, |
| 137 |
offsetX + 4); |
| 138 |
offsetY = offsetY > 0 ? Math.max(0, offsetY - 4) : Math.min(0, |
| 139 |
offsetY + 4); |
| 140 |
viewPosition = new Point(viewPosition.x + offsetX, |
| 141 |
viewPosition.y + offsetY); |
| 142 |
Dimension extentSize = viewport.getExtentSize(); |
| 143 |
Dimension viewSize = view.getSize(); |
| 144 |
if (viewSize.width - viewPosition.x < extentSize.width) { |
| 145 |
viewPosition.x = viewSize.width - extentSize.width; |
| 146 |
} |
| 147 |
if (viewSize.height - viewPosition.y < extentSize.height) { |
| 148 |
viewPosition.y = viewSize.height - extentSize.height; |
| 149 |
} |
| 150 |
if (viewPosition.x < 0) { |
| 151 |
viewPosition.x = 0; |
| 152 |
} |
| 153 |
if (viewPosition.y < 0) { |
| 154 |
viewPosition.y = 0; |
| 155 |
} |
| 156 |
viewport.setViewPosition(viewPosition); |
| 157 |
} |
| 158 |
}; |
| 159 |
autoScrollProperties.timer = new Timer(50, actionListener); |
| 160 |
autoScrollProperties.timer.start(); |
| 161 |
autoScrollProperties.toolkitListener = new AWTEventListener() { |
| 162 |
@Override |
| 163 |
public void eventDispatched(AWTEvent e) { |
| 164 |
int eventID = e.getID(); |
| 165 |
switch (eventID) { |
| 166 |
case MouseEvent.MOUSE_MOVED: |
| 167 |
case MouseEvent.MOUSE_DRAGGED: |
| 168 |
JViewport viewport = scrollPane.getViewport(); |
| 169 |
PointerInfo pi = MouseInfo.getPointerInfo(); |
| 170 |
if (pi == null) break; // pointer not on any device |
| 171 |
|
| 172 |
autoScrollProperties.currentLocation = pi.getLocation(); |
| 173 |
SwingUtilities.convertPointFromScreen( |
| 174 |
autoScrollProperties.currentLocation, viewport); |
| 175 |
if (!autoScrollProperties.isDragMode |
| 176 |
&& eventID == MouseEvent.MOUSE_DRAGGED) { |
| 177 |
Dimension size = new Dimension( |
| 178 |
Math.abs(autoScrollProperties.currentLocation.x |
| 179 |
- autoScrollProperties.startLocation.x), |
| 180 |
Math.abs(autoScrollProperties.currentLocation.y |
| 181 |
- autoScrollProperties.startLocation.y)); |
| 182 |
autoScrollProperties.isDragMode = size.width > HV_IMAGE_ICON |
| 183 |
.getIconWidth() / 2 |
| 184 |
|| size.height > HV_IMAGE_ICON.getIconHeight() / 2; |
| 185 |
} |
| 186 |
break; |
| 187 |
case MouseEvent.MOUSE_PRESSED: |
| 188 |
case MouseEvent.MOUSE_WHEEL: |
| 189 |
deactivateAutoScroll(); |
| 190 |
break; |
| 191 |
case MouseEvent.MOUSE_RELEASED: |
| 192 |
if (autoScrollProperties.isDragMode |
| 193 |
&& ((MouseEvent) e).getButton() == 2) { |
| 194 |
deactivateAutoScroll(); |
| 195 |
} |
| 196 |
break; |
| 197 |
case WindowEvent.WINDOW_LOST_FOCUS: |
| 198 |
deactivateAutoScroll(); |
| 199 |
break; |
| 200 |
} |
| 201 |
} |
| 202 |
}; |
| 203 |
Toolkit.getDefaultToolkit().addAWTEventListener( |
| 204 |
autoScrollProperties.toolkitListener, |
| 205 |
AWTEvent.MOUSE_MOTION_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK |
| 206 |
| AWTEvent.MOUSE_WHEEL_EVENT_MASK |
| 207 |
| AWTEvent.WINDOW_FOCUS_EVENT_MASK); |
| 208 |
} |
| 209 |
|
| 210 |
protected static class AutoScrollMouseListener extends MouseAdapter { |
| 211 |
protected AutoScrollActivator autoScrollActivator; |
| 212 |
|
| 213 |
public AutoScrollMouseListener(AutoScrollActivator autoScrollActivator) { |
| 214 |
this.autoScrollActivator = autoScrollActivator; |
| 215 |
} |
| 216 |
|
| 217 |
@Override |
| 218 |
public void mousePressed(MouseEvent e) { |
| 219 |
if (e.getButton() != 2) { |
| 220 |
return; |
| 221 |
} |
| 222 |
autoScrollActivator.activateAutoScroll(e); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
protected void configureScrollPane() { |
| 227 |
for (MouseListener mouseListener : scrollPane.getMouseListeners()) { |
| 228 |
if (mouseListener instanceof AutoScrollMouseListener) { |
| 229 |
return; |
| 230 |
} |
| 231 |
} |
| 232 |
scrollPane.addMouseListener(new AutoScrollMouseListener(this)); |
| 233 |
} |
| 234 |
|
| 235 |
protected static final ImageIcon H_IMAGE_ICON = new ImageIcon( |
| 236 |
AutoScrollActivator.class.getResource("resource/autoscroll_h.png")); |
| 237 |
protected static final ImageIcon V_IMAGE_ICON = new ImageIcon( |
| 238 |
AutoScrollActivator.class.getResource("resource/autoscroll_v.png")); |
| 239 |
protected static final ImageIcon HV_IMAGE_ICON = new ImageIcon( |
| 240 |
AutoScrollActivator.class |
| 241 |
.getResource("resource/autoscroll_all.png")); |
| 242 |
|
| 243 |
protected ImageIcon getAutoScrollIcon() { |
| 244 |
ImageIcon icon; |
| 245 |
if (scrollPane.getHorizontalScrollBar().isVisible()) { |
| 246 |
if (scrollPane.getVerticalScrollBar().isVisible()) { |
| 247 |
icon = HV_IMAGE_ICON; |
| 248 |
} else { |
| 249 |
icon = H_IMAGE_ICON; |
| 250 |
} |
| 251 |
} else { |
| 252 |
if (scrollPane.getVerticalScrollBar().isVisible()) { |
| 253 |
icon = V_IMAGE_ICON; |
| 254 |
} else { |
| 255 |
icon = HV_IMAGE_ICON; |
| 256 |
} |
| 257 |
} |
| 258 |
return icon; |
| 259 |
} |
| 260 |
|
| 261 |
public static void setAutoScrollEnabled(final JScrollPane scrollPane, |
| 262 |
boolean isEnabled) { |
| 263 |
if (isEnabled) { |
| 264 |
new AutoScrollActivator(scrollPane); |
| 265 |
} else { |
| 266 |
for (MouseListener mouseListener : scrollPane.getMouseListeners()) { |
| 267 |
if (mouseListener instanceof AutoScrollMouseListener) { |
| 268 |
scrollPane.removeMouseListener(mouseListener); |
| 269 |
return; |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
} |