KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > lib2 > KeyEventBlocker


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.editor.lib2;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.KeyboardFocusManager JavaDoc;
24 import java.awt.event.KeyListener JavaDoc;
25 import java.awt.event.KeyEvent JavaDoc;
26 import java.util.LinkedList JavaDoc;
27 import javax.swing.KeyStroke JavaDoc;
28 import javax.swing.text.JTextComponent JavaDoc;
29
30 /**
31 *
32 * @author Dusan Balek
33 */

34 public class KeyEventBlocker implements KeyListener JavaDoc {
35
36     private LinkedList JavaDoc<KeyEvent JavaDoc> blockedEvents = new LinkedList JavaDoc<KeyEvent JavaDoc>();
37     private JTextComponent JavaDoc component;
38     private boolean discardKeyTyped = true;
39     private static final boolean debugBlockEvent
40     = Boolean.getBoolean("netbeans.debug.editor.blocker"); // NOI18N
41

42
43     public KeyEventBlocker(JTextComponent JavaDoc component, boolean discardFirstKeyTypedEvent) {
44         this.component = component;
45         this.discardKeyTyped = discardFirstKeyTypedEvent;
46         if (debugBlockEvent){
47             System.out.println(""); //NOI18N
48
System.out.println("attaching listener"+this.component.getClass()+" - "+this.component.hashCode()); //NOI18N
49
}
50         this.component.addKeyListener(this);
51     }
52
53     /** Has to be called from AWT event thread to be properly synchronized */
54     public void stopBlocking(boolean dispatchBlockedEvents) {
55         if (debugBlockEvent){
56             System.out.println("removing listener from "+this.component.getClass()+" - "+this.component.hashCode()); //NOI18N
57
}
58         this.component.removeKeyListener(this);
59         if (dispatchBlockedEvents){
60             KeyboardFocusManager JavaDoc kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
61             while(!blockedEvents.isEmpty()) {
62                 KeyEvent JavaDoc e = blockedEvents.removeFirst();
63                 e = new KeyEvent JavaDoc((Component JavaDoc)e.getSource(), e.getID(), e.getWhen(), e.getModifiers(), e.getKeyCode(), e.getKeyChar(), e.getKeyLocation());
64                 kfm.dispatchEvent(e);
65             }
66         }
67     }
68     
69     public void stopBlocking() {
70         stopBlocking(true);
71     }
72
73     public void keyPressed(KeyEvent JavaDoc e) {
74         if (debugBlockEvent){
75             System.out.println("consuming keyPressed event:"+KeyEvent.getKeyModifiersText(e.getModifiers())+" + "+KeyEvent.getKeyText(e.getKeyCode())); //NOI18N
76
}
77         e.consume();
78         blockedEvents.add(e);
79     }
80
81     public void keyReleased(KeyEvent JavaDoc e) {
82         if (debugBlockEvent){
83             System.out.println("consuming keyReleased event:"+KeyEvent.getKeyModifiersText(e.getModifiers())+" + "+KeyEvent.getKeyText(e.getKeyCode())); //NOI18N
84
}
85         e.consume();
86         blockedEvents.add(e);
87     }
88
89     public void keyTyped(KeyEvent JavaDoc e) {
90         if (debugBlockEvent){
91             System.out.println("consuming keyTyped event:"+KeyEvent.getKeyModifiersText(e.getModifiers())+" + "+KeyEvent.getKeyText(e.getKeyCode())); //NOI18N
92
}
93         e.consume();
94         if (discardKeyTyped) {
95             discardKeyTyped = false;
96         } else {
97             blockedEvents.add(e);
98         }
99     }
100 }
101
Popular Tags