1 11 12 package org.eclipse.jface.text; 13 14 15 import org.eclipse.swt.SWT; 16 import org.eclipse.swt.events.ControlEvent; 17 import org.eclipse.swt.events.ControlListener; 18 import org.eclipse.swt.events.KeyEvent; 19 import org.eclipse.swt.events.KeyListener; 20 import org.eclipse.swt.events.MouseEvent; 21 import org.eclipse.swt.events.MouseListener; 22 import org.eclipse.swt.events.MouseMoveListener; 23 import org.eclipse.swt.events.MouseTrackAdapter; 24 import org.eclipse.swt.events.MouseTrackListener; 25 import org.eclipse.swt.events.ShellAdapter; 26 import org.eclipse.swt.events.ShellEvent; 27 import org.eclipse.swt.events.ShellListener; 28 import org.eclipse.swt.graphics.Point; 29 import org.eclipse.swt.graphics.Rectangle; 30 import org.eclipse.swt.widgets.Control; 31 import org.eclipse.swt.widgets.Display; 32 import org.eclipse.swt.widgets.Event; 33 import org.eclipse.swt.widgets.Listener; 34 35 import org.eclipse.core.runtime.Assert; 36 37 38 54 abstract public class AbstractHoverInformationControlManager extends AbstractInformationControlManager { 55 56 57 62 class Closer extends MouseTrackAdapter 63 implements IInformationControlCloser, MouseListener, MouseMoveListener, ControlListener, KeyListener, ShellListener, Listener { 64 65 66 private Control fSubjectControl; 67 68 private Rectangle fSubjectArea; 69 70 private boolean fIsActive= false; 71 75 private Display fDisplay; 76 77 78 81 public Closer() { 82 } 83 84 87 public void setSubjectControl(Control control) { 88 fSubjectControl= control; 89 } 90 91 94 public void setInformationControl(IInformationControl control) { 95 } 96 97 100 public void start(Rectangle subjectArea) { 101 102 if (fIsActive) return; 103 fIsActive= true; 104 105 fSubjectArea= subjectArea; 106 107 if (fSubjectControl != null && !fSubjectControl.isDisposed()) { 108 fSubjectControl.addMouseListener(this); 109 fSubjectControl.addMouseMoveListener(this); 110 fSubjectControl.addMouseTrackListener(this); 111 fSubjectControl.addControlListener(this); 112 fSubjectControl.addKeyListener(this); 113 fSubjectControl.getShell().addShellListener(this); 114 115 fDisplay= fSubjectControl.getDisplay(); 116 if (!fDisplay.isDisposed()) { 117 fDisplay.addFilter(SWT.Show, this); 118 fDisplay.addFilter(SWT.Activate, this); 119 fDisplay.addFilter(SWT.MouseWheel, this); 120 } 121 } 122 } 123 124 127 public void stop() { 128 stop(false); 129 } 130 131 137 protected void stop(boolean delayRestart) { 138 139 if (!fIsActive) 140 return; 141 142 fIsActive= false; 143 144 hideInformationControl(); 145 146 if (fSubjectControl != null && !fSubjectControl.isDisposed()) { 147 fSubjectControl.removeMouseListener(this); 148 fSubjectControl.removeMouseMoveListener(this); 149 fSubjectControl.removeMouseTrackListener(this); 150 fSubjectControl.removeControlListener(this); 151 fSubjectControl.removeKeyListener(this); 152 fSubjectControl.getShell().removeShellListener(this); 153 } 154 155 if (fDisplay != null && !fDisplay.isDisposed()) { 156 fDisplay.removeFilter(SWT.Show, this); 157 fDisplay.removeFilter(SWT.Activate, this); 158 fDisplay.removeFilter(SWT.MouseWheel, this); 159 } 160 fDisplay= null; 161 } 162 163 166 public void mouseMove(MouseEvent event) { 167 if (!fSubjectArea.contains(event.x, event.y)) 168 stop(); 169 } 170 171 174 public void mouseUp(MouseEvent event) { 175 } 176 177 180 public void mouseDown(MouseEvent event) { 181 stop(); 182 } 183 184 187 public void mouseDoubleClick(MouseEvent event) { 188 stop(); 189 } 190 191 194 public void mouseExit(MouseEvent event) { 195 stop(); 196 } 197 198 201 public void controlResized(ControlEvent event) { 202 stop(); 203 } 204 205 208 public void controlMoved(ControlEvent event) { 209 stop(); 210 } 211 212 215 public void keyReleased(KeyEvent event) { 216 } 217 218 221 public void keyPressed(KeyEvent event) { 222 stop(true); 223 } 224 225 229 public void shellActivated(ShellEvent e) { 230 } 231 232 236 public void shellClosed(ShellEvent e) { 237 } 238 239 243 public void shellDeactivated(ShellEvent e) { 244 stop(); 245 } 246 247 251 public void shellDeiconified(ShellEvent e) { 252 } 253 254 258 public void shellIconified(ShellEvent e) { 259 } 260 261 265 public void handleEvent(Event event) { 266 if (event.type == SWT.Activate || event.type == SWT.Show || event.type == SWT.MouseWheel) 267 stop(); 268 } 269 } 270 271 272 281 class MouseTracker extends ShellAdapter implements MouseTrackListener, MouseMoveListener { 282 283 284 private final static int EPSILON= 3; 285 286 287 private Rectangle fHoverArea; 288 289 private Rectangle fSubjectArea; 290 291 private Control fSubjectControl; 292 293 294 private boolean fIsInRestartMode= false; 295 296 private boolean fIsComputing= false; 297 298 private boolean fMouseLostWhileComputing= false; 299 300 private boolean fShellDeactivatedWhileComputing= false; 301 302 305 public MouseTracker() { 306 } 307 308 314 public void setSubjectArea(Rectangle subjectArea) { 315 Assert.isNotNull(subjectArea); 316 fSubjectArea= subjectArea; 317 } 318 319 325 public void start(Control subjectControl) { 326 fSubjectControl= subjectControl; 327 if (fSubjectControl != null && !fSubjectControl.isDisposed()) 328 fSubjectControl.addMouseTrackListener(this); 329 330 fIsInRestartMode= false; 331 fIsComputing= false; 332 fMouseLostWhileComputing= false; 333 fShellDeactivatedWhileComputing= false; 334 } 335 336 340 public void stop() { 341 if (fSubjectControl != null && !fSubjectControl.isDisposed()) { 342 fSubjectControl.removeMouseTrackListener(this); 343 fSubjectControl.removeMouseMoveListener(this); 344 fSubjectControl.getShell().removeShellListener(this); 345 } 346 } 347 348 356 public void mouseHover(MouseEvent event) { 357 358 if (fIsComputing || fIsInRestartMode) return; 359 360 fIsInRestartMode= true; 361 fIsComputing= true; 362 fMouseLostWhileComputing= false; 363 fShellDeactivatedWhileComputing= false; 364 365 fHoverEventStateMask= event.stateMask; 366 fHoverEvent= event; 367 fHoverArea= new Rectangle(event.x - EPSILON, event.y - EPSILON, 2 * EPSILON, 2 * EPSILON ); 368 if (fHoverArea.x < 0) fHoverArea.x= 0; 369 if (fHoverArea.y < 0) fHoverArea.y= 0; 370 setSubjectArea(fHoverArea); 371 372 if (fSubjectControl != null && !fSubjectControl.isDisposed()) { 373 fSubjectControl.addMouseMoveListener(this); 374 fSubjectControl.getShell().addShellListener(this); 375 } 376 doShowInformation(); 377 } 378 379 384 protected void deactivate() { 385 if (fIsComputing) return; 386 fIsInRestartMode= false; 387 if (fSubjectControl != null && !fSubjectControl.isDisposed()) { 388 fSubjectControl.removeMouseMoveListener(this); 389 fSubjectControl.getShell().removeShellListener(this); 390 } 391 } 392 393 396 public void mouseEnter(MouseEvent e) { 397 } 398 399 402 public void mouseExit(MouseEvent e) { 403 fMouseLostWhileComputing= true; 404 deactivate(); 405 } 406 407 410 public void mouseMove(MouseEvent event) { 411 if (!fSubjectArea.contains(event.x, event.y)) 412 deactivate(); 413 } 414 415 418 public void shellDeactivated(ShellEvent e) { 419 fShellDeactivatedWhileComputing= true; 420 deactivate(); 421 } 422 423 426 public void shellIconified(ShellEvent e) { 427 fShellDeactivatedWhileComputing= true; 428 deactivate(); 429 } 430 431 434 public void computationCompleted() { 435 fIsComputing= false; 436 fMouseLostWhileComputing= false; 437 fShellDeactivatedWhileComputing= false; 438 } 439 440 448 public boolean isMouseLost() { 449 450 if (fMouseLostWhileComputing || fShellDeactivatedWhileComputing) 451 return true; 452 453 if (fSubjectControl != null && !fSubjectControl.isDisposed()) { 454 Display display= fSubjectControl.getDisplay(); 455 Point p= display.getCursorLocation(); 456 p= fSubjectControl.toControl(p); 457 if (!fSubjectArea.contains(p) && !fHoverArea.contains(p)) 458 return true; 459 } 460 461 return false; 462 } 463 } 464 465 466 private MouseTracker fMouseTracker= new MouseTracker(); 467 471 private MouseEvent fHoverEvent= null; 472 473 private int fHoverEventStateMask= 0; 474 475 481 protected AbstractHoverInformationControlManager(IInformationControlCreator creator) { 482 super(creator); 483 setCloser(new Closer()); 484 } 485 486 489 protected void presentInformation() { 490 if (fMouseTracker == null) { 491 super.presentInformation(); 492 return; 493 } 494 495 Rectangle area= getSubjectArea(); 496 if (area != null) 497 fMouseTracker.setSubjectArea(area); 498 499 if (fMouseTracker.isMouseLost()) { 500 fMouseTracker.computationCompleted(); 501 fMouseTracker.deactivate(); 502 } else { 503 fMouseTracker.computationCompleted(); 504 super.presentInformation(); 505 } 506 } 507 508 512 public void setEnabled(boolean enabled) { 513 514 boolean was= isEnabled(); 515 super.setEnabled(enabled); 516 boolean is= isEnabled(); 517 518 if (was != is && fMouseTracker != null) { 519 if (is) 520 fMouseTracker.start(getSubjectControl()); 521 else 522 fMouseTracker.stop(); 523 } 524 } 525 526 529 public void dispose() { 530 if (fMouseTracker != null) { 531 fMouseTracker.stop(); 532 fMouseTracker.fSubjectControl= null; 533 fMouseTracker= null; 534 } 535 super.dispose(); 536 } 537 538 544 protected Point getHoverEventLocation() { 545 return fHoverEvent != null ? new Point(fHoverEvent.x, fHoverEvent.y) : new Point(-1, -1); 546 } 547 548 554 protected MouseEvent getHoverEvent() { 555 return fHoverEvent; 556 } 557 558 563 protected int getHoverEventStateMask() { 564 return fHoverEventStateMask; 565 } 566 567 } 568 | Popular Tags |