001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.editor.ext;
043:
044: import java.awt.Component;
045: import java.awt.KeyboardFocusManager;
046: import java.awt.event.KeyListener;
047: import java.awt.event.KeyEvent;
048: import java.util.LinkedList;
049: import javax.swing.KeyStroke;
050: import javax.swing.text.JTextComponent;
051:
052: /**
053: *
054: * @author Dusan Balek
055: */
056: public class KeyEventBlocker implements KeyListener {
057:
058: private LinkedList blockedEvents = new LinkedList();
059: private JTextComponent component;
060: private boolean discardKeyTyped = true;
061: private static final boolean debugBlockEvent = Boolean
062: .getBoolean("netbeans.debug.editor.blocker"); // NOI18N
063:
064: public KeyEventBlocker(JTextComponent component,
065: boolean discardFirstKeyTypedEvent) {
066: this .component = component;
067: this .discardKeyTyped = discardFirstKeyTypedEvent;
068: if (debugBlockEvent) {
069: System.out.println(""); //NOI18N
070: System.out.println("attaching listener"
071: + this .component.getClass() + " - "
072: + this .component.hashCode()); //NOI18N
073: }
074: this .component.addKeyListener(this );
075: }
076:
077: /** Has to be called from AWT event thread to be properly synchronized */
078: public void stopBlocking(boolean dispatchBlockedEvents) {
079: if (debugBlockEvent) {
080: System.out.println("removing listener from "
081: + this .component.getClass() + " - "
082: + this .component.hashCode()); //NOI18N
083: }
084: this .component.removeKeyListener(this );
085: if (dispatchBlockedEvents) {
086: KeyboardFocusManager kfm = KeyboardFocusManager
087: .getCurrentKeyboardFocusManager();
088: while (!blockedEvents.isEmpty()) {
089: KeyEvent e = (KeyEvent) blockedEvents.removeFirst();
090: e = new KeyEvent((Component) e.getSource(), e.getID(),
091: e.getWhen(), e.getModifiers(), e.getKeyCode(),
092: e.getKeyChar(), e.getKeyLocation());
093: kfm.dispatchEvent(e);
094: }
095: }
096: }
097:
098: public void stopBlocking() {
099: stopBlocking(true);
100: }
101:
102: public void keyPressed(KeyEvent e) {
103: if (debugBlockEvent) {
104: System.out.println("consuming keyPressed event:"
105: + KeyEvent.getKeyModifiersText(e.getModifiers())
106: + " + " + KeyEvent.getKeyText(e.getKeyCode())); //NOI18N
107: }
108: e.consume();
109: blockedEvents.add(e);
110: }
111:
112: public void keyReleased(KeyEvent e) {
113: if (debugBlockEvent) {
114: System.out.println("consuming keyReleased event:"
115: + KeyEvent.getKeyModifiersText(e.getModifiers())
116: + " + " + KeyEvent.getKeyText(e.getKeyCode())); //NOI18N
117: }
118: e.consume();
119: blockedEvents.add(e);
120: }
121:
122: public void keyTyped(KeyEvent e) {
123: if (debugBlockEvent) {
124: System.out.println("consuming keyTyped event:"
125: + KeyEvent.getKeyModifiersText(e.getModifiers())
126: + " + " + KeyEvent.getKeyText(e.getKeyCode())); //NOI18N
127: }
128: e.consume();
129: if (discardKeyTyped) {
130: discardKeyTyped = false;
131: } else {
132: blockedEvents.add(e);
133: }
134: }
135: }
|