001: /*
002: * AbstractInputHandler.java - Manages key bindings and executes actions
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2006 Matthieu Casanova
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022: package org.gjt.sp.jedit.input;
023:
024: import org.gjt.sp.jedit.gui.KeyEventTranslator;
025: import org.gjt.sp.jedit.Debug;
026: import org.gjt.sp.util.Log;
027:
028: import java.awt.event.KeyListener;
029: import java.awt.event.KeyEvent;
030:
031: /**
032: * The abstract input handler manage the keyboard handling.
033: * The entry point is
034: * {@link #processKeyEvent(java.awt.event.KeyEvent, int, boolean)}
035: *
036: * @author Matthieu Casanova
037: * @version $Id: FoldHandler.java 5568 2006-07-10 20:52:23Z kpouer $
038: */
039: public abstract class AbstractInputHandler {
040: protected int lastActionCount;
041: /** This listener will receive keyboard events if it is not null. */
042: protected KeyListener keyEventInterceptor;
043: protected String readNextChar;
044: protected int repeatCount;
045:
046: protected static final int REPEAT_COUNT_THRESHOLD = 20;
047:
048: //{{{ AbstractInputHandler constructor
049: public AbstractInputHandler() {
050: repeatCount = 1;
051: } //}}}
052:
053: //{{{ getLastActionCount() method
054: /**
055: * Returns the number of times the last action was executed.
056: * It can be used with smartHome and smartEnd
057: * @return the number of times the last action was executed
058: * @since jEdit 2.5pre5
059: */
060: public int getLastActionCount() {
061: return lastActionCount;
062: } //}}}
063:
064: //{{{ resetLastActionCount() method
065: /**
066: * Resets the last action count. This should be called when an
067: * editing operation that is not an action is invoked, for example
068: * a mouse click.
069: * @since jEdit 4.0pre1
070: */
071: public void resetLastActionCount() {
072: lastActionCount = 0;
073: } //}}}
074:
075: //{{{ getKeyEventInterceptor() method
076: public KeyListener getKeyEventInterceptor() {
077: return keyEventInterceptor;
078: } //}}}
079:
080: //{{{ setKeyEventInterceptor() method
081: /**
082: * Sets the listener that will handle all key events in this
083: * view. For example, the complete word command uses this so
084: * that all key events are passed to the word list popup while
085: * it is visible.
086: * @param keyEventInterceptor the KeyListener that will receive the events
087: */
088: public void setKeyEventInterceptor(KeyListener keyEventInterceptor) {
089: this .keyEventInterceptor = keyEventInterceptor;
090: } //}}}
091:
092: //{{{ isPrefixActive() method
093: /**
094: * Returns if a prefix key has been pressed.
095: */
096: public boolean isPrefixActive() {
097: return readNextChar != null;
098: } //}}}
099:
100: //{{{ handleKey() method
101: /**
102: * Handles a keystroke.
103: * @param keyStroke The key stroke.
104: * @param dryRun only calculate the return value, do not have any other effect
105: * @return true if the input could be handled.
106: * @since jEdit 4.3pre7
107: */
108: public abstract boolean handleKey(KeyEventTranslator.Key keyStroke,
109: boolean dryRun);
110:
111: //}}}
112:
113: //{{{ processKeyEvent() method
114:
115: /**
116: * Process a keyboard event.
117: * This is the entry point of the keyboard handling
118: *
119: * @param evt the keyboard event
120: * @param from the source, it can be {@link org.gjt.sp.jedit.View#VIEW},
121: * {@link org.gjt.sp.jedit.View#ACTION_BAR} or {@link org.gjt.sp.jedit.View#TEXT_AREA}
122: * @param global tell if the event comes from the DefaultKeyboardFocusManager or not
123: */
124: public abstract void processKeyEvent(KeyEvent evt, int from,
125: boolean global);
126:
127: //}}}
128:
129: //{{{ processKeyEventKeyStrokeHandling() method
130:
131: /**
132: *
133: * @param evt the keyboard event
134: * @param from the source, it can be {@link org.gjt.sp.jedit.View#VIEW},
135: * {@link org.gjt.sp.jedit.View#ACTION_BAR} or {@link org.gjt.sp.jedit.View#TEXT_AREA}
136: * @param mode the mode is "press" or "type" and is used for debug only
137: * @param global tell if the event comes from the DefaultKeyboardFocusManager or not
138: */
139: protected void processKeyEventKeyStrokeHandling(KeyEvent evt,
140: int from, String mode, boolean global) {
141: KeyEventTranslator.Key keyStroke = KeyEventTranslator
142: .translateKeyEvent2(evt);
143:
144: if (keyStroke != null) {
145: keyStroke.setIsFromGlobalContext(global);
146: if (Debug.DUMP_KEY_EVENTS) {
147: Log.log(Log.DEBUG, this , "Translated (key " + mode
148: + "): " + keyStroke + " from " + from);
149: }
150: boolean consumed = false;
151: if (handleKey(keyStroke, keyStroke.isPhantom())) {
152: evt.consume();
153:
154: consumed = true;
155: }
156: if (Debug.DUMP_KEY_EVENTS) {
157: Log.log(Log.DEBUG, this , "Translated (key " + mode
158: + "): " + keyStroke + " from " + from
159: + ": consumed=" + consumed + '.');
160: }
161: }
162: } //}}}
163: }
|