KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > util > OpenStrategy


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
private static final int TIME = 500;
89
90     /* SINGLE_CLICK or DOUBLE_CLICK;
91      * In case of SINGLE_CLICK, the bits SELECT_ON_HOVER and ARROW_KEYS_OPEN
92      * my be set as well. */

93     private static int CURRENT_METHOD = DOUBLE_CLICK;
94
95     private Listener eventHandler;
96
97     private ListenerList openEventListeners = new ListenerList();
98
99     private ListenerList selectionEventListeners = new ListenerList();
100
101     private ListenerList postSelectionEventListeners = new ListenerList();
102
103     /**
104      * @param control the control the strategy is applied to
105      */

106     public OpenStrategy(Control control) {
107         initializeHandler(control.getDisplay());
108         addListener(control);
109     }
110
111     /**
112      * Adds an IOpenEventListener to the collection of openEventListeners
113      * @param listener the listener to add
114      */

115     public void addOpenListener(IOpenEventListener listener) {
116         openEventListeners.add(listener);
117     }
118
119     /**
120      * Removes an IOpenEventListener to the collection of openEventListeners
121      * @param listener the listener to remove
122      */

123     public void removeOpenListener(IOpenEventListener listener) {
124         openEventListeners.remove(listener);
125     }
126
127     /**
128      * Adds an SelectionListener to the collection of selectionEventListeners
129      * @param listener the listener to add
130      */

131     public void addSelectionListener(SelectionListener listener) {
132         selectionEventListeners.add(listener);
133     }
134
135     /**
136      * Removes an SelectionListener to the collection of selectionEventListeners
137      * @param listener the listener to remove
138      */

139     public void removeSelectionListener(SelectionListener listener) {
140         selectionEventListeners.remove(listener);
141     }
142
143     /**
144      * Adds an SelectionListener to the collection of selectionEventListeners
145      * @param listener the listener to add
146      */

147     public void addPostSelectionListener(SelectionListener listener) {
148         postSelectionEventListeners.add(listener);
149     }
150
151     /**
152      * Removes an SelectionListener to the collection of selectionEventListeners
153      * @param listener the listener to remove
154      */

155     public void removePostSelectionListener(SelectionListener listener) {
156         postSelectionEventListeners.remove(listener);
157     }
158
159     /**
160      * This method is internal to the framework; it should not be implemented outside
161      * the framework.
162      * @return the current used single/double-click method
163      *
164      */

165     public static int getOpenMethod() {
166         return CURRENT_METHOD;
167     }
168
169     /**
170      * Set the current used single/double-click method.
171      *
172      * This method is internal to the framework; it should not be implemented outside
173      * the framework.
174      * @param method the method to be used
175      * @see OpenStrategy#DOUBLE_CLICK
176      * @see OpenStrategy#SINGLE_CLICK
177      * @see OpenStrategy#SELECT_ON_HOVER
178      * @see OpenStrategy#ARROW_KEYS_OPEN
179      */

180     public static void setOpenMethod(int method) {
181         if (method == DOUBLE_CLICK) {
182             CURRENT_METHOD = method;
183             return;
184         }
185         if ((method & SINGLE_CLICK) == 0) {
186             throw new IllegalArgumentException JavaDoc("Invalid open mode"); //$NON-NLS-1$
187
}
188         if ((method & (SINGLE_CLICK | SELECT_ON_HOVER | ARROW_KEYS_OPEN)) == 0) {
189             throw new IllegalArgumentException JavaDoc("Invalid open mode"); //$NON-NLS-1$
190
}
191         CURRENT_METHOD = method;
192     }
193
194     /**
195      * @return true if editors should be activated when opened.
196      */

197     public static boolean activateOnOpen() {
198         return getOpenMethod() == DOUBLE_CLICK;
199     }
200
201     /*
202      * Adds all needed listener to the control in order to implement
203      * single-click/double-click strategies.
204      */

205     private void addListener(Control c) {
206         c.addListener(SWT.MouseEnter, eventHandler);
207         c.addListener(SWT.MouseExit, eventHandler);
208         c.addListener(SWT.MouseMove, eventHandler);
209         c.addListener(SWT.MouseDown, eventHandler);
210         c.addListener(SWT.MouseUp, eventHandler);
211         c.addListener(SWT.KeyDown, eventHandler);
212         c.addListener(SWT.Selection, eventHandler);
213         c.addListener(SWT.DefaultSelection, eventHandler);
214         c.addListener(SWT.Collapse, eventHandler);
215         c.addListener(SWT.Expand, eventHandler);
216     }
217
218     /*
219      * Fire the selection event to all selectionEventListeners
220      */

221     private void fireSelectionEvent(SelectionEvent e) {
222         if (e.item != null && e.item.isDisposed()) {
223             return;
224         }
225         Object JavaDoc l[] = selectionEventListeners.getListeners();
226         for (int i = 0; i < l.length; i++) {
227             ((SelectionListener) l[i]).widgetSelected(e);
228         }
229     }
230
231     /*
232      * Fire the default selection event to all selectionEventListeners
233      */

234     private void fireDefaultSelectionEvent(SelectionEvent e) {
235         Object JavaDoc l[] = selectionEventListeners.getListeners();
236         for (int i = 0; i < l.length; i++) {
237             ((SelectionListener) l[i]).widgetDefaultSelected(e);
238         }
239     }
240
241     /*
242      * Fire the post selection event to all postSelectionEventListeners
243      */

244     private void firePostSelectionEvent(SelectionEvent e) {
245         if (e.item != null && e.item.isDisposed()) {
246             return;
247         }
248         Object JavaDoc l[] = postSelectionEventListeners.getListeners();
249         for (int i = 0; i < l.length; i++) {
250             ((SelectionListener) l[i]).widgetSelected(e);
251         }
252     }
253
254     /*
255      * Fire the open event to all openEventListeners
256      */

257     private void fireOpenEvent(SelectionEvent e) {
258         if (e.item != null && e.item.isDisposed()) {
259             return;
260         }
261         Object JavaDoc l[] = openEventListeners.getListeners();
262         for (int i = 0; i < l.length; i++) {
263             ((IOpenEventListener) l[i]).handleOpen(e);
264         }
265     }
266
267     //Initialize event handler.
268
private void initializeHandler(final Display display) {
269         eventHandler = new Listener() {
270             boolean timerStarted = false;
271
272             Event mouseUpEvent = null;
273
274             Event mouseMoveEvent = null;
275
276             SelectionEvent selectionPendent = null;
277
278             boolean enterKeyDown = false;
279
280             SelectionEvent defaultSelectionPendent = null;
281
282             boolean arrowKeyDown = false;
283
284             final int[] count = new int[1];
285
286             long startTime = System.currentTimeMillis();
287
288             boolean collapseOccurred = false;
289
290             boolean expandOccurred = false;
291
292             public void handleEvent(final Event e) {
293                 if (e.type == SWT.DefaultSelection) {
294                     SelectionEvent event = new SelectionEvent(e);
295                     fireDefaultSelectionEvent(event);
296                     if (CURRENT_METHOD == DOUBLE_CLICK) {
297                         fireOpenEvent(event);
298                     } else {
299                         if (enterKeyDown) {
300                             fireOpenEvent(event);
301                             enterKeyDown = false;
302                             defaultSelectionPendent = null;
303                         } else {
304                             defaultSelectionPendent = event;
305                         }
306                     }
307                     return;
308                 }
309
310                 switch (e.type) {
311                 case SWT.MouseEnter:
312                 case SWT.MouseExit:
313                     mouseUpEvent = null;
314                     mouseMoveEvent = null;
315                     selectionPendent = null;
316                     break;
317                 case SWT.MouseMove:
318                     if ((CURRENT_METHOD & SELECT_ON_HOVER) == 0) {
319                         return;
320                     }
321                     if (e.stateMask != 0) {
322                         return;
323                     }
324                     if (e.widget.getDisplay().getFocusControl() != e.widget) {
325                         return;
326                     }
327                     mouseMoveEvent = e;
328                     final Runnable JavaDoc runnable[] = new Runnable JavaDoc[1];
329                     runnable[0] = new Runnable JavaDoc() {
330                         public void run() {
331                             long time = System.currentTimeMillis();
332                             int diff = (int) (time - startTime);
333                             if (diff <= TIME) {
334                                 display.timerExec(diff * 2 / 3, runnable[0]);
335                             } else {
336                                 timerStarted = false;
337                                 setSelection(mouseMoveEvent);
338                             }
339                         }
340                     };
341                     startTime = System.currentTimeMillis();
342                     if (!timerStarted) {
343                         timerStarted = true;
344                         display.timerExec(TIME * 2 / 3, runnable[0]);
345                     }
346                     break;
347                 case SWT.MouseDown:
348                     mouseUpEvent = null;
349                     arrowKeyDown = false;
350                     break;
351                 case SWT.Expand:
352                     expandOccurred = true;
353                     break;
354                 case SWT.Collapse:
355                     collapseOccurred = true;
356                     break;
357                 case SWT.MouseUp:
358                     mouseMoveEvent = null;
359                     if ((e.button != 1) || ((e.stateMask & ~SWT.BUTTON1) != 0)) {
360                         return;
361                     }
362                     if (selectionPendent != null
363                             && !(collapseOccurred || expandOccurred)) {
364                         mouseSelectItem(selectionPendent);
365                     } else {
366                         mouseUpEvent = e;
367                         collapseOccurred = false;
368                         expandOccurred = false;
369                     }
370                     break;
371                 case SWT.KeyDown:
372                     mouseMoveEvent = null;
373                     mouseUpEvent = null;
374                     arrowKeyDown = ((e.keyCode == SWT.ARROW_UP) || (e.keyCode == SWT.ARROW_DOWN))
375                             && e.stateMask == 0;
376                     if (e.character == SWT.CR) {
377                         if (defaultSelectionPendent != null) {
378                             fireOpenEvent(new SelectionEvent(e));
379                             enterKeyDown = false;
380                             defaultSelectionPendent = null;
381                         } else {
382                             enterKeyDown = true;
383                         }
384                     }
385                     break;
386                 case SWT.Selection:
387                     SelectionEvent event = new SelectionEvent(e);
388                     fireSelectionEvent(event);
389                     mouseMoveEvent = null;
390                     if (mouseUpEvent != null) {
391                         mouseSelectItem(event);
392                     } else {
393                         selectionPendent = event;
394                     }
395                     count[0]++;
396                     final int id = count[0];
397                     // In the case of arrowUp/arrowDown when in the arrowKeysOpen mode, we
398
// want to delay any selection until the last arrowDown/Up occurs. This
399
// handles the case where the user presses arrowDown/Up successively.
400
// We only want to open an editor for the last selected item.
401
display.asyncExec(new Runnable JavaDoc() {
402                         public void run() {
403                             if (arrowKeyDown) {
404                                 display.timerExec(TIME, new Runnable JavaDoc() {
405
406                                     public void run() {
407                                         if (id == count[0]) {
408                                             firePostSelectionEvent(new SelectionEvent(
409                                                     e));
410                                             if ((CURRENT_METHOD & ARROW_KEYS_OPEN) != 0) {
411                                                 fireOpenEvent(new SelectionEvent(
412                                                         e));
413                                             }
414                                         }
415                                     }
416                                 });
417                             } else {
418                                 firePostSelectionEvent(new SelectionEvent(e));
419                             }
420                         }
421                     });
422                     break;
423                 }
424             }
425
426             void mouseSelectItem(SelectionEvent e) {
427                 if ((CURRENT_METHOD & SINGLE_CLICK) != 0) {
428                     fireOpenEvent(e);
429                 }
430                 mouseUpEvent = null;
431                 selectionPendent = null;
432             }
433
434             void setSelection(Event e) {
435                 if (e == null) {
436                     return;
437                 }
438                 Widget w = e.widget;
439                 if (w.isDisposed()) {
440                     return;
441                 }
442
443                 SelectionEvent selEvent = new SelectionEvent(e);
444
445                 /*ISSUE: May have to create a interface with method:
446                  setSelection(Point p) so that user's custom widgets
447                  can use this class. If we keep this option. */

448                 if (w instanceof Tree) {
449                     Tree tree = (Tree) w;
450                     TreeItem item = tree.getItem(new Point(e.x, e.y));
451                     if (item != null) {
452                         tree.setSelection(new TreeItem[] { item });
453                     }
454                     selEvent.item = item;
455                 } else if (w instanceof Table) {
456                     Table table = (Table) w;
457                     TableItem item = table.getItem(new Point(e.x, e.y));
458                     if (item != null) {
459                         table.setSelection(new TableItem[] { item });
460                     }
461                     selEvent.item = item;
462                 } else if (w instanceof TableTree) {
463                     TableTree table = (TableTree) w;
464                     TableTreeItem item = table.getItem(new Point(e.x, e.y));
465                     if (item != null) {
466                         table.setSelection(new TableTreeItem[] { item });
467                     }
468                     selEvent.item = item;
469                 } else {
470                     return;
471                 }
472                 if (selEvent.item == null) {
473                     return;
474                 }
475                 fireSelectionEvent(selEvent);
476                 firePostSelectionEvent(selEvent);
477             }
478         };
479     }
480 }
481
Popular Tags