| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2000, 2006 IBM Corporation and others. |
| 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | *******************************************************************************/ |
| 11 | package org.eclipse.jface.util; |
| 12 | |
| 13 | import org.eclipse.core.runtime.ListenerList; |
| 14 | import org.eclipse.swt.SWT; |
| 15 | import org.eclipse.swt.custom.TableTree; |
| 16 | import org.eclipse.swt.custom.TableTreeItem; |
| 17 | import org.eclipse.swt.events.SelectionEvent; |
| 18 | import org.eclipse.swt.events.SelectionListener; |
| 19 | import org.eclipse.swt.graphics.Point; |
| 20 | import org.eclipse.swt.widgets.Control; |
| 21 | import org.eclipse.swt.widgets.Display; |
| 22 | import org.eclipse.swt.widgets.Event; |
| 23 | import org.eclipse.swt.widgets.Listener; |
| 24 | import org.eclipse.swt.widgets.Table; |
| 25 | import org.eclipse.swt.widgets.TableItem; |
| 26 | import org.eclipse.swt.widgets.Tree; |
| 27 | import org.eclipse.swt.widgets.TreeItem; |
| 28 | import org.eclipse.swt.widgets.Widget; |
| 29 | |
| 30 | /** |
| 31 | * Implementation of single-click and double-click strategies. |
| 32 | * <p> |
| 33 | * Usage: |
| 34 | * <pre> |
| 35 | * OpenStrategy handler = new OpenStrategy(control); |
| 36 | * handler.addOpenListener(new IOpenEventListener() { |
| 37 | * public void handleOpen(SelectionEvent e) { |
| 38 | * ... // code to handle the open event. |
| 39 | * } |
| 40 | * }); |
| 41 | * </pre> |
| 42 | * </p> |
| 43 | */ |
| 44 | public class OpenStrategy { |
| 45 | /** |
| 46 | * Default behavior. Double click to open the item. |
| 47 | */ |
| 48 | public static final int DOUBLE_CLICK = 0; |
| 49 | |
| 50 | /** |
| 51 | * Single click will open the item. |
| 52 | */ |
| 53 | public static final int SINGLE_CLICK = 1; |
| 54 | |
| 55 | /** |
| 56 | * Hover will select the item. |
| 57 | */ |
| 58 | public static final int SELECT_ON_HOVER = 1 << 1; |
| 59 | |
| 60 | /** |
| 61 | * Open item when using arrow keys |
| 62 | */ |
| 63 | public static final int ARROW_KEYS_OPEN = 1 << 2; |
| 64 | |
| 65 | /** A single click will generate |
| 66 | * an open event but key arrows will not do anything. |
| 67 | * |
| 68 | * @deprecated |
| 69 | */ |
| 70 | public static final int NO_TIMER = SINGLE_CLICK; |
| 71 | |
| 72 | /** A single click will generate an open |
| 73 | * event and key arrows will generate an open event after a |
| 74 | * small time. |
| 75 | * |
| 76 | * @deprecated |
| 77 | */ |
| 78 | public static final int FILE_EXPLORER = SINGLE_CLICK | ARROW_KEYS_OPEN; |
| 79 | |
| 80 | /** Pointing to an item will change the selection |
| 81 | * and a single click will gererate an open event |
| 82 | * |
| 83 | * @deprecated |
| 84 | */ |
| 85 | public static final int ACTIVE_DESKTOP = SINGLE_CLICK | SELECT_ON_HOVER; |
| 86 | |
| 87 | // Time used in FILE_EXPLORER and ACTIVE_DESKTOP |
| 88 | // Not declared final, see bug 246209 |
| 89 | private static int TIME = 500; |
| 90 | |
| 91 | /* SINGLE_CLICK or DOUBLE_CLICK; |
| 92 | * In case of SINGLE_CLICK, the bits SELECT_ON_HOVER and ARROW_KEYS_OPEN |
| 93 | * my be set as well. */ |
| 94 | private static int CURRENT_METHOD = DOUBLE_CLICK; |
| 95 | |
| 96 | private Listener eventHandler; |
| 97 | |
| 98 | private ListenerList openEventListeners = new ListenerList(); |
| 99 | |
| 100 | private ListenerList selectionEventListeners = new ListenerList(); |
| 101 | |
| 102 | private ListenerList postSelectionEventListeners = new ListenerList(); |
| 103 | |
| 104 | /** |
| 105 | * @param control the control the strategy is applied to |
| 106 | */ |
| 107 | public OpenStrategy(Control control) { |
| 108 | initializeHandler(control.getDisplay()); |
| 109 | addListener(control); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Adds an IOpenEventListener to the collection of openEventListeners |
| 114 | * @param listener the listener to add |
| 115 | */ |
| 116 | public void addOpenListener(IOpenEventListener listener) { |
| 117 | openEventListeners.add(listener); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Removes an IOpenEventListener to the collection of openEventListeners |
| 122 | * @param listener the listener to remove |
| 123 | */ |
| 124 | public void removeOpenListener(IOpenEventListener listener) { |
| 125 | openEventListeners.remove(listener); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Adds an SelectionListener to the collection of selectionEventListeners |
| 130 | * @param listener the listener to add |
| 131 | */ |
| 132 | public void addSelectionListener(SelectionListener listener) { |
| 133 | selectionEventListeners.add(listener); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Removes an SelectionListener to the collection of selectionEventListeners |
| 138 | * @param listener the listener to remove |
| 139 | */ |
| 140 | public void removeSelectionListener(SelectionListener listener) { |
| 141 | selectionEventListeners.remove(listener); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Adds an SelectionListener to the collection of selectionEventListeners |
| 146 | * @param listener the listener to add |
| 147 | */ |
| 148 | public void addPostSelectionListener(SelectionListener listener) { |
| 149 | postSelectionEventListeners.add(listener); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Removes an SelectionListener to the collection of selectionEventListeners |
| 154 | * @param listener the listener to remove |
| 155 | */ |
| 156 | public void removePostSelectionListener(SelectionListener listener) { |
| 157 | postSelectionEventListeners.remove(listener); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * This method is internal to the framework; it should not be implemented outside |
| 162 | * the framework. |
| 163 | * @return the current used single/double-click method |
| 164 | * |
| 165 | */ |
| 166 | public static int getOpenMethod() { |
| 167 | return CURRENT_METHOD; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Set the current used single/double-click method. |
| 172 | * |
| 173 | * This method is internal to the framework; it should not be implemented outside |
| 174 | * the framework. |
| 175 | * @param method the method to be used |
| 176 | * @see OpenStrategy#DOUBLE_CLICK |
| 177 | * @see OpenStrategy#SINGLE_CLICK |
| 178 | * @see OpenStrategy#SELECT_ON_HOVER |
| 179 | * @see OpenStrategy#ARROW_KEYS_OPEN |
| 180 | */ |
| 181 | public static void setOpenMethod(int method) { |
| 182 | if (method == DOUBLE_CLICK) { |
| 183 | CURRENT_METHOD = method; |
| 184 | return; |
| 185 | } |
| 186 | if ((method & SINGLE_CLICK) == 0) { |
| 187 | throw new IllegalArgumentException("Invalid open mode"); //$NON-NLS-1$ |
| 188 | } |
| 189 | if ((method & (SINGLE_CLICK | SELECT_ON_HOVER | ARROW_KEYS_OPEN)) == 0) { |
| 190 | throw new IllegalArgumentException("Invalid open mode"); //$NON-NLS-1$ |
| 191 | } |
| 192 | CURRENT_METHOD = method; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * @return true if editors should be activated when opened. |
| 197 | */ |
| 198 | public static boolean activateOnOpen() { |
| 199 | return getOpenMethod() == DOUBLE_CLICK; |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * Adds all needed listener to the control in order to implement |
| 204 | * single-click/double-click strategies. |
| 205 | */ |
| 206 | private void addListener(Control c) { |
| 207 | c.addListener(SWT.MouseEnter, eventHandler); |
| 208 | c.addListener(SWT.MouseExit, eventHandler); |
| 209 | c.addListener(SWT.MouseMove, eventHandler); |
| 210 | c.addListener(SWT.MouseDown, eventHandler); |
| 211 | c.addListener(SWT.MouseUp, eventHandler); |
| 212 | c.addListener(SWT.KeyDown, eventHandler); |
| 213 | c.addListener(SWT.Selection, eventHandler); |
| 214 | c.addListener(SWT.DefaultSelection, eventHandler); |
| 215 | c.addListener(SWT.Collapse, eventHandler); |
| 216 | c.addListener(SWT.Expand, eventHandler); |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * Fire the selection event to all selectionEventListeners |
| 221 | */ |
| 222 | private void fireSelectionEvent(SelectionEvent e) { |
| 223 | if (e.item != null && e.item.isDisposed()) { |
| 224 | return; |
| 225 | } |
| 226 | Object l[] = selectionEventListeners.getListeners(); |
| 227 | for (int i = 0; i < l.length; i++) { |
| 228 | ((SelectionListener) l[i]).widgetSelected(e); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | * Fire the default selection event to all selectionEventListeners |
| 234 | */ |
| 235 | private void fireDefaultSelectionEvent(SelectionEvent e) { |
| 236 | Object l[] = selectionEventListeners.getListeners(); |
| 237 | for (int i = 0; i < l.length; i++) { |
| 238 | ((SelectionListener) l[i]).widgetDefaultSelected(e); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /* |
| 243 | * Fire the post selection event to all postSelectionEventListeners |
| 244 | */ |
| 245 | private void firePostSelectionEvent(SelectionEvent e) { |
| 246 | if (e.item != null && e.item.isDisposed()) { |
| 247 | return; |
| 248 | } |
| 249 | Object l[] = postSelectionEventListeners.getListeners(); |
| 250 | for (int i = 0; i < l.length; i++) { |
| 251 | ((SelectionListener) l[i]).widgetSelected(e); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | * Fire the open event to all openEventListeners |
| 257 | */ |
| 258 | private void fireOpenEvent(SelectionEvent e) { |
| 259 | if (e.item != null && e.item.isDisposed()) { |
| 260 | return; |
| 261 | } |
| 262 | Object l[] = openEventListeners.getListeners(); |
| 263 | for (int i = 0; i < l.length; i++) { |
| 264 | ((IOpenEventListener) l[i]).handleOpen(e); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | //Initialize event handler. |
| 269 | private void initializeHandler(final Display display) { |
| 270 | eventHandler = new Listener() { |
| 271 | boolean timerStarted = false; |
| 272 | |
| 273 | Event mouseUpEvent = null; |
| 274 | |
| 275 | Event mouseMoveEvent = null; |
| 276 | |
| 277 | SelectionEvent selectionPendent = null; |
| 278 | |
| 279 | boolean enterKeyDown = false; |
| 280 | |
| 281 | SelectionEvent defaultSelectionPendent = null; |
| 282 | |
| 283 | boolean arrowKeyDown = false; |
| 284 | |
| 285 | final int[] count = new int[1]; |
| 286 | |
| 287 | long startTime = System.currentTimeMillis(); |
| 288 | |
| 289 | boolean collapseOccurred = false; |
| 290 | |
| 291 | boolean expandOccurred = false; |
| 292 | |
| 293 | public void handleEvent(final Event e) { |
| 294 | if (e.type == SWT.DefaultSelection) { |
| 295 | SelectionEvent event = new SelectionEvent(e); |
| 296 | fireDefaultSelectionEvent(event); |
| 297 | if (CURRENT_METHOD == DOUBLE_CLICK) { |
| 298 | fireOpenEvent(event); |
| 299 | } else { |
| 300 | if (enterKeyDown) { |
| 301 | fireOpenEvent(event); |
| 302 | enterKeyDown = false; |
| 303 | defaultSelectionPendent = null; |
| 304 | } else { |
| 305 | defaultSelectionPendent = event; |
| 306 | } |
| 307 | } |
| 308 | return; |
| 309 | } |
| 310 | |
| 311 | switch (e.type) { |
| 312 | case SWT.MouseEnter: |
| 313 | case SWT.MouseExit: |
| 314 | mouseUpEvent = null; |
| 315 | mouseMoveEvent = null; |
| 316 | selectionPendent = null; |
| 317 | break; |
| 318 | case SWT.MouseMove: |
| 319 | if ((CURRENT_METHOD & SELECT_ON_HOVER) == 0) { |
| 320 | return; |
| 321 | } |
| 322 | if (e.stateMask != 0) { |
| 323 | return; |
| 324 | } |
| 325 | if (e.widget.getDisplay().getFocusControl() != e.widget) { |
| 326 | return; |
| 327 | } |
| 328 | mouseMoveEvent = e; |
| 329 | final Runnable runnable[] = new Runnable[1]; |
| 330 | runnable[0] = new Runnable() { |
| 331 | public void run() { |
| 332 | long time = System.currentTimeMillis(); |
| 333 | int diff = (int) (time - startTime); |
| 334 | if (diff <= TIME) { |
| 335 | display.timerExec(diff * 2 / 3, runnable[0]); |
| 336 | } else { |
| 337 | timerStarted = false; |
| 338 | setSelection(mouseMoveEvent); |
| 339 | } |
| 340 | } |
| 341 | }; |
| 342 | startTime = System.currentTimeMillis(); |
| 343 | if (!timerStarted) { |
| 344 | timerStarted = true; |
| 345 | display.timerExec(TIME * 2 / 3, runnable[0]); |
| 346 | } |
| 347 | break; |
| 348 | case SWT.MouseDown: |
| 349 | mouseUpEvent = null; |
| 350 | arrowKeyDown = false; |
| 351 | break; |
| 352 | case SWT.Expand: |
| 353 | expandOccurred = true; |
| 354 | break; |
| 355 | case SWT.Collapse: |
| 356 | collapseOccurred = true; |
| 357 | break; |
| 358 | case SWT.MouseUp: |
| 359 | mouseMoveEvent = null; |
| 360 | if ((e.button != 1) || ((e.stateMask & ~SWT.BUTTON1) != 0)) { |
| 361 | return; |
| 362 | } |
| 363 | if (selectionPendent != null |
| 364 | && !(collapseOccurred || expandOccurred)) { |
| 365 | mouseSelectItem(selectionPendent); |
| 366 | } else { |
| 367 | mouseUpEvent = e; |
| 368 | collapseOccurred = false; |
| 369 | expandOccurred = false; |
| 370 | } |
| 371 | break; |
| 372 | case SWT.KeyDown: |
| 373 | mouseMoveEvent = null; |
| 374 | mouseUpEvent = null; |
| 375 | arrowKeyDown = ((e.keyCode == SWT.ARROW_UP) || (e.keyCode == SWT.ARROW_DOWN)) |
| 376 | && e.stateMask == 0; |
| 377 | if (e.character == SWT.CR) { |
| 378 | if (defaultSelectionPendent != null) { |
| 379 | fireOpenEvent(new SelectionEvent(e)); |
| 380 | enterKeyDown = false; |
| 381 | defaultSelectionPendent = null; |
| 382 | } else { |
| 383 | enterKeyDown = true; |
| 384 | } |
| 385 | } |
| 386 | break; |
| 387 | case SWT.Selection: |
| 388 | SelectionEvent event = new SelectionEvent(e); |
| 389 | fireSelectionEvent(event); |
| 390 | mouseMoveEvent = null; |
| 391 | if (mouseUpEvent != null) { |
| 392 | mouseSelectItem(event); |
| 393 | } else { |
| 394 | selectionPendent = event; |
| 395 | } |
| 396 | count[0]++; |
| 397 | final int id = count[0]; |
| 398 | // In the case of arrowUp/arrowDown when in the arrowKeysOpen mode, we |
| 399 | // want to delay any selection until the last arrowDown/Up occurs. This |
| 400 | // handles the case where the user presses arrowDown/Up successively. |
| 401 | // We only want to open an editor for the last selected item. |
| 402 | display.asyncExec(new Runnable() { |
| 403 | public void run() { |
| 404 | if (arrowKeyDown) { |
| 405 | display.timerExec(TIME, new Runnable() { |
| 406 | |
| 407 | public void run() { |
| 408 | if (id == count[0]) { |
| 409 | firePostSelectionEvent(new SelectionEvent( |
| 410 | e)); |
| 411 | if ((CURRENT_METHOD & ARROW_KEYS_OPEN) != 0) { |
| 412 | fireOpenEvent(new SelectionEvent( |
| 413 | e)); |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | }); |
| 418 | } else { |
| 419 | firePostSelectionEvent(new SelectionEvent(e)); |
| 420 | } |
| 421 | } |
| 422 | }); |
| 423 | break; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | void mouseSelectItem(SelectionEvent e) { |
| 428 | if ((CURRENT_METHOD & SINGLE_CLICK) != 0) { |
| 429 | fireOpenEvent(e); |
| 430 | } |
| 431 | mouseUpEvent = null; |
| 432 | selectionPendent = null; |
| 433 | } |
| 434 | |
| 435 | void setSelection(Event e) { |
| 436 | if (e == null) { |
| 437 | return; |
| 438 | } |
| 439 | Widget w = e.widget; |
| 440 | if (w.isDisposed()) { |
| 441 | return; |
| 442 | } |
| 443 | |
| 444 | SelectionEvent selEvent = new SelectionEvent(e); |
| 445 | |
| 446 | /*ISSUE: May have to create a interface with method: |
| 447 | setSelection(Point p) so that user's custom widgets |
| 448 | can use this class. If we keep this option. */ |
| 449 | if (w instanceof Tree) { |
| 450 | Tree tree = (Tree) w; |
| 451 | TreeItem item = tree.getItem(new Point(e.x, e.y)); |
| 452 | if (item != null) { |
| 453 | tree.setSelection(new TreeItem[] { item }); |
| 454 | } |
| 455 | selEvent.item = item; |
| 456 | } else if (w instanceof Table) { |
| 457 | Table table = (Table) w; |
| 458 | TableItem item = table.getItem(new Point(e.x, e.y)); |
| 459 | if (item != null) { |
| 460 | table.setSelection(new TableItem[] { item }); |
| 461 | } |
| 462 | selEvent.item = item; |
| 463 | } else if (w instanceof TableTree) { |
| 464 | TableTree table = (TableTree) w; |
| 465 | TableTreeItem item = table.getItem(new Point(e.x, e.y)); |
| 466 | if (item != null) { |
| 467 | table.setSelection(new TableTreeItem[] { item }); |
| 468 | } |
| 469 | selEvent.item = item; |
| 470 | } else { |
| 471 | return; |
| 472 | } |
| 473 | if (selEvent.item == null) { |
| 474 | return; |
| 475 | } |
| 476 | fireSelectionEvent(selEvent); |
| 477 | firePostSelectionEvent(selEvent); |
| 478 | } |
| 479 | }; |
| 480 | } |
| 481 | } |