KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > keys > OutOfOrderListener


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
12 package org.eclipse.ui.internal.keys;
13
14 import java.util.List JavaDoc;
15
16 import org.eclipse.swt.widgets.Event;
17 import org.eclipse.swt.widgets.Listener;
18 import org.eclipse.swt.widgets.Widget;
19
20 /**
21  * A listener that makes sure that global key bindings are processed if no other
22  * listeners do any useful work.
23  *
24  * @since 3.0
25  */

26 public class OutOfOrderListener implements Listener {
27     /**
28      * The time at which this listener was last registered to an event. This is
29      * the <code>event.time</code> value.
30      *
31      * @since 3.1
32      */

33     private int active = Integer.MIN_VALUE;
34
35     /**
36      * The keyboard interface to which the event should be passed if it is not
37      * eaten.
38      */

39     private final WorkbenchKeyboard keyboard;
40
41     /**
42      * Constructs a new instance of <code>OutOfOrderListener</code> with a
43      * reference to the keyboard interface which should be allowed to process
44      * uneaten events.
45      *
46      * @param workbenchKeyboard
47      * The keyboard interface for the workbench capable of processing
48      * key bindings; must not be <code>null</code>.
49      */

50     public OutOfOrderListener(WorkbenchKeyboard workbenchKeyboard) {
51         keyboard = workbenchKeyboard;
52     }
53
54     /**
55      * Handles the key down event on a widget by passing uneaten events to the
56      * key binding architecture. This is used to allow special keys to reach the
57      * widget first -- before being processed by the key binding architecture.
58      *
59      * @param event
60      * The event to process; must not be <code>null</code>
61      */

62     public void handleEvent(Event event) {
63         // Always remove myself as a listener.
64
final Widget widget = event.widget;
65         if ((widget != null) && (!widget.isDisposed())) {
66             widget.removeListener(event.type, this);
67         }
68
69         /*
70          * If the event is still up for grabs, then re-route through the global
71          * key filter.
72          */

73         if (event.doit) {
74             List JavaDoc keyStrokes = WorkbenchKeyboard
75                     .generatePossibleKeyStrokes(event);
76             keyboard.processKeyEvent(keyStrokes, event);
77         }
78     }
79
80     /**
81      * Returns whether this listener has been hooked by this event already.
82      *
83      * @param timeRegistered
84      * The <code>event.time</code> for the current event.
85      * @return <code>true</code> if this listener is registered for a
86      * different event; <code>false</code> otherwise.
87      *
88      * @since 3.1
89      */

90     final boolean isActive(final int timeRegistered) {
91         return (active == timeRegistered);
92     }
93
94     /**
95      * Sets the event time at which this listener was last registered with a
96      * widget.
97      *
98      * @param timeRegistered
99      * The time at which this listener was last registered with a
100      * widget.
101      *
102      * @since 3.1
103      */

104     final void setActive(final int timeRegistered) {
105         active = timeRegistered;
106     }
107 }
108
Popular Tags