1 19 20 package org.netbeans.core.windows.view.ui.slides; 21 22 import java.awt.AWTEvent ; 23 import java.awt.Component ; 24 import java.awt.Point ; 25 import java.awt.Rectangle ; 26 import java.awt.Toolkit ; 27 import java.awt.Window ; 28 import java.awt.event.AWTEventListener ; 29 import java.awt.event.ActionEvent ; 30 import java.awt.event.ActionListener ; 31 import java.awt.event.MouseAdapter ; 32 import java.awt.event.MouseEvent ; 33 import java.awt.event.MouseListener ; 34 import java.awt.event.MouseMotionListener ; 35 import java.beans.PropertyChangeEvent ; 36 import java.beans.PropertyChangeListener ; 37 import java.util.List ; 38 import javax.swing.AbstractButton ; 39 import javax.swing.SwingUtilities ; 40 import javax.swing.Timer ; 41 import org.netbeans.swing.tabcontrol.SlideBarDataModel; 42 import org.openide.util.WeakListeners; 43 import org.openide.windows.TopComponent; 44 45 50 final class SlideGestureRecognizer implements ActionListener , MouseListener , MouseMotionListener { 51 52 private SlideBar slideBar; 53 54 private Component mouseInButton = null; 55 56 private int curMouseLocX, curMouseLocY; 57 58 59 private AutoSlideTrigger autoSlideTrigger = new AutoSlideTrigger(); 60 private ResizeGestureRecognizer resizer; 61 private boolean pressingButton = false; 62 63 SlideGestureRecognizer(SlideBar slideBar, ResizeGestureRecognizer resize) { 64 this.slideBar = slideBar; 65 resizer = resize; 66 } 67 68 71 public void attachButton (AbstractButton button) { 72 button.addActionListener(this); 73 button.addMouseListener(this); 74 button.addMouseMotionListener(this); 75 } 76 77 80 public void detachButton (AbstractButton button) { 81 button.removeActionListener(this); 82 button.removeMouseListener(this); 83 button.addMouseMotionListener(this); 84 } 85 86 87 public void actionPerformed(ActionEvent e) { 88 slideBar.userClickedSlidingButton((Component )e.getSource()); 89 } 90 91 92 public void mouseMoved(MouseEvent e) { 93 if (autoSlideTrigger.isEnabled()) { 94 curMouseLocX = e.getX(); 95 curMouseLocY = e.getY(); 96 } 97 if (pressingButton && (e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) == 0) { 99 pressingButton = false; 100 autoSlideTrigger.activateAutoSlideInGesture(); 101 } 102 } 104 105 106 public void mouseEntered(MouseEvent e) { 107 if (!slideBar.isHoveringAllowed()) { 108 return; 110 } 111 mouseInButton = (Component )e.getSource(); 112 curMouseLocX = e.getX(); 113 curMouseLocY = e.getY(); 114 pressingButton =false; 115 if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) == MouseEvent.BUTTON1_DOWN_MASK) { 117 pressingButton = true; 118 return; 119 } 120 autoSlideTrigger.activateAutoSlideInGesture(); 122 } 123 124 125 public void mouseExited(MouseEvent e) { 126 mouseInButton = null; 127 pressingButton = false; 128 autoSlideTrigger.deactivateAutoSlideInGesture(); 129 } 130 131 132 public void mousePressed(MouseEvent e) { 133 autoSlideTrigger.deactivateAutoSlideInGesture(); 134 handlePopupRequests(e); 135 } 136 137 138 public void mouseReleased(MouseEvent e) { 139 autoSlideTrigger.deactivateAutoSlideInGesture(); 140 handlePopupRequests(e); 141 } 142 143 public void mouseDragged(MouseEvent e) { 144 } 146 147 public void mouseClicked(MouseEvent e) { 148 } 150 151 152 private void handlePopupRequests (MouseEvent e) { 153 if (e.getSource().equals(slideBar)) { 155 return; 156 } 157 158 if (e.isPopupTrigger()) { 159 slideBar.userTriggeredPopup(e, (Component )e.getSource()); 160 } 161 } 162 163 165 private final class AutoSlideTrigger implements ActionListener , AWTEventListener { 166 167 168 private Timer slideInTimer; 169 170 private int initialX, initialY; 171 172 private boolean autoSlideActive = false; 173 175 private Rectangle activeArea; 176 177 AutoSlideTrigger() { 178 super(); 179 slideInTimer = new Timer (200, this); 180 slideInTimer.setRepeats(true); 181 slideInTimer.setCoalesce(true); 182 } 183 184 185 public void activateAutoSlideInGesture() { 186 initialX = curMouseLocX; 187 initialY = curMouseLocY; 188 slideInTimer.start(); 189 } 190 191 192 public void deactivateAutoSlideInGesture() { 193 slideInTimer.stop(); 194 } 195 196 197 public boolean isEnabled() { 198 return autoSlideActive || slideInTimer.isRunning(); 199 } 200 201 204 public void actionPerformed(ActionEvent evt) { 205 if (isSlideInGesture()) { 206 slideInTimer.stop(); 207 if (autoSlideActive) { 209 autoSlideOut(); 210 } 211 autoSlideActive = true; 212 if (slideBar.userTriggeredAutoSlideIn(mouseInButton)) { 215 Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.MOUSE_MOTION_EVENT_MASK); 216 } else { 217 autoSlideActive = false; 218 } 219 } else { 220 initialX = curMouseLocX; 221 initialY = curMouseLocY; 222 } 223 } 224 225 228 public void eventDispatched(AWTEvent event) { 229 autoSlideOutIfNeeded((MouseEvent )event); 230 } 231 232 234 private void autoSlideOutIfNeeded(MouseEvent evt) { 235 if (!autoSlideActive) { 236 return; 238 } 239 if (slideBar.isActive()) { 240 cleanup(); 242 return; 243 } 244 if (isSlideOutGesture(evt)) { 245 cleanup(); 246 autoSlideOut(); 247 } 248 } 249 250 251 private void autoSlideOut() { 252 slideBar.userTriggeredAutoSlideOut(); 253 } 254 255 257 private void cleanup() { 258 Toolkit.getDefaultToolkit().removeAWTEventListener(this); 259 autoSlideActive = false; 260 activeArea = null; 261 } 262 263 264 private boolean isSlideInGesture () { 265 if (mouseInButton == null) { 266 return false; 267 } 268 269 int diffX = Math.abs(initialX - curMouseLocX); 270 int diffY = Math.abs(initialY - curMouseLocY); 271 272 return (diffX <= 2) && (diffY <= 2); 273 } 274 275 276 private boolean isSlideOutGesture(MouseEvent evt) { 277 if (resizer.isDragging()) { 278 activeArea = null; 279 return false; 280 } 281 if (activeArea == null) { 282 activeArea = computeActiveArea(); 283 if (activeArea == null) { 285 return false; 286 } 287 } 288 Point mouseLoc = evt.getPoint(); 289 SwingUtilities.convertPointToScreen(mouseLoc, (Component )evt.getSource()); 290 291 return !activeArea.contains(mouseLoc); 292 } 293 294 298 private Rectangle computeActiveArea() { 299 Component slidedComp = slideBar.getSlidedComp(); 300 if (slidedComp == null || !slidedComp.isShowing()) { 301 return null; 302 } 303 304 Point slideBarLoc = slideBar.getLocationOnScreen(); 305 Rectangle actArea = new Rectangle (slideBarLoc.x - 1, slideBarLoc.y - 1, 306 slideBar.getWidth() - 1, slideBar.getHeight() - 1); 307 308 Point slidedCompLoc = slidedComp.getLocationOnScreen(); 309 310 int slidex = slidedCompLoc.x; 311 int slidey = slidedCompLoc.y; 312 int slideh = slidedComp.getHeight(); 313 int slidew = slidedComp.getWidth(); 314 int orientation = slideBar.getModel().getOrientation(); 315 if (orientation == SlideBarDataModel.WEST) { 316 slidew = slidew + ResizeGestureRecognizer.RESIZE_BUFFER; 317 } 318 if (orientation == SlideBarDataModel.EAST) { 319 slidew = slidew + ResizeGestureRecognizer.RESIZE_BUFFER; 320 slidex = slidex - ResizeGestureRecognizer.RESIZE_BUFFER; 321 } 322 if (orientation == SlideBarDataModel.SOUTH) { 323 slideh = slideh + ResizeGestureRecognizer.RESIZE_BUFFER; 324 slidey = slidey - ResizeGestureRecognizer.RESIZE_BUFFER; 325 } 326 actArea = SwingUtilities.computeUnion( 327 slidex, slidey, slidew, 328 slideh, actArea); 329 330 return actArea; 331 } 332 333 334 } 336 } | Popular Tags |