KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.swt.SWT;
15 import org.eclipse.swt.custom.StyledText;
16 import org.eclipse.swt.custom.VerifyKeyListener;
17 import org.eclipse.swt.events.VerifyEvent;
18 import org.eclipse.swt.widgets.Widget;
19
20 /**
21  * A listener that makes sure that out-of-order processing occurs if no other
22  * verify listeners do any work.
23  *
24  * @since 3.0
25  */

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

34     private int active = Integer.MIN_VALUE;
35
36     /**
37      * The listener that will be chained in if the verify event has not been
38      * eaten yet.
39      */

40     private OutOfOrderListener chainedListener;
41
42     /**
43      * Constructs a new instance of <code>OutOfOrderVerifyListener</code> with
44      * the listener that will be chained in.
45      *
46      * @param outOfOrderListener
47      * The listener that should be attached to the widget if the
48      * verify event is not eaten; must not be <code>null</code>.
49      */

50     OutOfOrderVerifyListener(OutOfOrderListener outOfOrderListener) {
51         chainedListener = outOfOrderListener;
52     }
53
54     /**
55      * Returns whether this listener has been hooked by this event already.
56      *
57      * @param timeRegistered
58      * The <code>event.time</code> for the current event.
59      * @return <code>true</code> if this listener is registered for a
60      * different event; <code>false</code> otherwise.
61      *
62      * @since 3.1
63      */

64     final boolean isActive(final int timeRegistered) {
65         return (active == timeRegistered);
66     }
67
68     /**
69      * Sets the event time at which this listener was last registered with a
70      * widget.
71      *
72      * @param timeRegistered
73      * The time at which this listener was last registered with a
74      * widget.
75      *
76      * @since 3.1
77      */

78     final void setActive(final int timeRegistered) {
79         active = timeRegistered;
80     }
81
82     /**
83      * Checks whether any other verify listeners have triggered. If not, then it
84      * sets up the top-level out-of-order listener.
85      *
86      * @param event
87      * The verify event after it has been processed by all other
88      * verify listeners; must not be <code>null</code>.
89      */

90     public void verifyKey(VerifyEvent event) {
91         // Always remove the listener.
92
final Widget widget = event.widget;
93         if ((widget instanceof StyledText) && (!widget.isDisposed())) {
94             ((StyledText) widget).removeVerifyKeyListener(this);
95         }
96
97         /*
98          * If the event is still up for grabs, then re-route through the global
99          * key filter.
100          */

101         if (event.doit) {
102             widget.addListener(SWT.Modify, new CancelOnModifyListener(
103                     chainedListener));
104             widget.addListener(SWT.KeyDown, chainedListener);
105         }
106     }
107 }
108
Popular Tags