PageRenderTime 40ms CodeModel.GetById 28ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/jedit/gui/DefaultInputHandler.java

#
Java | 428 lines | 241 code | 35 blank | 152 comment | 45 complexity | 2bd4e99d9cea66c7a456470396e2a09c MD5 | raw file
  1/*
  2 * DefaultInputHandler.java - Default implementation of an input handler
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 1999, 2003 Slava Pestov
  7 *
  8 * This program is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU General Public License
 10 * as published by the Free Software Foundation; either version 2
 11 * of the License, or any later version.
 12 *
 13 * This program is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public License
 19 * along with this program; if not, write to the Free Software
 20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 21 */
 22
 23package org.gjt.sp.jedit.gui;
 24
 25//{{{ Imports
 26import javax.swing.KeyStroke;
 27import java.awt.event.KeyEvent;
 28import java.awt.event.InputEvent;
 29import java.awt.Toolkit;
 30import java.util.Hashtable;
 31import java.util.StringTokenizer;
 32import org.gjt.sp.jedit.*;
 33import org.gjt.sp.util.Log;
 34import org.gjt.sp.jedit.msg.*;
 35import javax.swing.event.*;
 36//}}}
 37
 38/**
 39 * The default input handler. It maps sequences of keystrokes into actions
 40 * and inserts key typed events into the text area.
 41 * @author Slava Pestov
 42 * @version $Id: DefaultInputHandler.java 5310 2005-12-19 02:29:26Z jchoyt $
 43 */
 44public class DefaultInputHandler extends InputHandler
 45{
 46	//{{{ DefaultInputHandler constructor
 47	/**
 48	 * Creates a new input handler with no key bindings defined.
 49	 * @param view The view
 50	 * @param bindings An explicitly-specified set of key bindings,
 51	 * must not be null.
 52	 * @since jEdit 4.3pre1
 53	 */
 54	public DefaultInputHandler(View view, Hashtable bindings)
 55	{
 56		super(view);
 57
 58		if(bindings == null)
 59			throw new NullPointerException();
 60		this.bindings = this.currentBindings = bindings;
 61	} //}}}
 62
 63	//{{{ DefaultInputHandler constructor
 64	/**
 65	 * Creates a new input handler with no key bindings defined.
 66	 * @param view The view
 67	 */
 68	public DefaultInputHandler(View view)
 69	{
 70		this(view,new Hashtable());
 71	} //}}}
 72
 73	//{{{ DefaultInputHandler constructor
 74	/**
 75	 * Creates a new input handler with the same set of key bindings
 76	 * as the one specified. Note that both input handlers share
 77	 * a pointer to exactly the same key binding table; so adding
 78	 * a key binding in one will also add it to the other.
 79	 * @param copy The input handler to copy key bindings from
 80	 * @param view The view
 81	 */
 82	public DefaultInputHandler(View view, DefaultInputHandler copy)
 83	{
 84		this(view,copy.bindings);
 85	} //}}}
 86
 87	//{{{ addKeyBinding() method
 88	/**
 89	 * Adds a key binding to this input handler. The key binding is
 90	 * a list of white space separated key strokes of the form
 91	 * <i>[modifiers+]key</i> where modifier is C for Control, A for Alt,
 92	 * or S for Shift, and key is either a character (a-z) or a field
 93	 * name in the KeyEvent class prefixed with VK_ (e.g., BACK_SPACE)
 94	 * @param keyBinding The key binding
 95	 * @param action The action
 96	 * @since jEdit 4.2pre1
 97	 */
 98	public void addKeyBinding(String keyBinding, String action)
 99	{
100		addKeyBinding(keyBinding,(Object)action);
101	} //}}}
102
103	//{{{ addKeyBinding() method
104	/**
105	 * Adds a key binding to this input handler. The key binding is
106	 * a list of white space separated key strokes of the form
107	 * <i>[modifiers+]key</i> where modifier is C for Control, A for Alt,
108	 * or S for Shift, and key is either a character (a-z) or a field
109	 * name in the KeyEvent class prefixed with VK_ (e.g., BACK_SPACE)
110	 * @param keyBinding The key binding
111	 * @param action The action
112	 */
113	public void addKeyBinding(String keyBinding, EditAction action)
114	{
115		addKeyBinding(keyBinding,(Object)action);
116	} //}}}
117
118	//{{{ addKeyBinding() method
119	/**
120	 * Adds a key binding to this input handler. The key binding is
121	 * a list of white space separated key strokes of the form
122	 * <i>[modifiers+]key</i> where modifier is C for Control, A for Alt,
123	 * or S for Shift, and key is either a character (a-z) or a field
124	 * name in the KeyEvent class prefixed with VK_ (e.g., BACK_SPACE)
125	 * @param keyBinding The key binding
126	 * @param action The action
127	 * @since jEdit 4.3pre1
128	 */
129	public void addKeyBinding(String keyBinding, Object action)
130	{
131		Hashtable current = bindings;
132
133		String prefixStr = null;
134
135		StringTokenizer st = new StringTokenizer(keyBinding);
136		while(st.hasMoreTokens())
137		{
138			String keyCodeStr = st.nextToken();
139			if(prefixStr == null)
140				prefixStr = keyCodeStr;
141			else
142				prefixStr = prefixStr + " " + keyCodeStr;
143
144			KeyEventTranslator.Key keyStroke = KeyEventTranslator.parseKey(keyCodeStr);
145			if(keyStroke == null)
146				return;
147
148			if(st.hasMoreTokens())
149			{
150				Object o = current.get(keyStroke);
151				if(o instanceof Hashtable)
152					current = (Hashtable)o;
153				else
154				{
155					Hashtable hash = new Hashtable();
156					hash.put(PREFIX_STR,prefixStr);
157					o = hash;
158					current.put(keyStroke,o);
159					current = (Hashtable)o;
160				}
161			}
162			else
163				current.put(keyStroke,action);
164		}
165	} //}}}
166
167	//{{{ removeKeyBinding() method
168	/**
169	 * Removes a key binding from this input handler. This is not yet
170	 * implemented.
171	 * @param keyBinding The key binding
172	 */
173	public void removeKeyBinding(String keyBinding)
174	{
175		Hashtable current = bindings;
176
177		StringTokenizer st = new StringTokenizer(keyBinding);
178		while(st.hasMoreTokens())
179		{
180			String keyCodeStr = st.nextToken();
181			KeyEventTranslator.Key keyStroke = KeyEventTranslator.parseKey(keyCodeStr);
182			if(keyStroke == null)
183				return;
184
185			if(st.hasMoreTokens())
186			{
187				Object o = current.get(keyStroke);
188				if(o instanceof Hashtable)
189					current = ((Hashtable)o);
190				else if(o != null)
191				{
192					// we have binding foo
193					// but user asks to remove foo bar?
194					current.remove(keyStroke);
195					return;
196				}
197				else
198				{
199					// user asks to remove non-existent
200					return;
201				}
202			}
203			else
204				current.remove(keyStroke);
205		}
206	} //}}}
207
208	//{{{ removeAllKeyBindings() method
209	/**
210	 * Removes all key bindings from this input handler.
211	 */
212	public void removeAllKeyBindings()
213	{
214		bindings.clear();
215	} //}}}
216
217	//{{{ getKeyBinding() method
218	/**
219	 * Returns either an edit action, or a hashtable if the specified key
220	 * is a prefix.
221	 * @param keyBinding The key binding
222	 * @since jEdit 3.2pre5
223	 */
224	public Object getKeyBinding(String keyBinding)
225	{
226		Hashtable current = bindings;
227		StringTokenizer st = new StringTokenizer(keyBinding);
228
229		while(st.hasMoreTokens())
230		{
231			KeyEventTranslator.Key keyStroke = KeyEventTranslator.parseKey(
232				st.nextToken());
233			if(keyStroke == null)
234				return null;
235
236			if(st.hasMoreTokens())
237			{
238				Object o = current.get(keyStroke);
239				if(o instanceof Hashtable)
240				{
241					if(!st.hasMoreTokens())
242						return o;
243					else
244						current = (Hashtable)o;
245				}
246				else
247					return o;
248			}
249			else
250			{
251				return current.get(keyStroke);
252			}
253		}
254
255		return null;
256	} //}}}
257
258	//{{{ isPrefixActive() method
259	/**
260	 * Returns if a prefix key has been pressed.
261	 */
262	public boolean isPrefixActive()
263	{
264		return bindings != currentBindings
265			|| super.isPrefixActive();
266	} //}}}
267
268	//{{{ setBindings() method
269	/**
270	 * Replace the set of key bindings.
271	 * @since jEdit 4.3pre1
272	 */
273	public void setBindings(Hashtable bindings)
274	{
275		this.bindings = this.currentBindings = bindings;
276	} //}}}
277
278	//{{{ setCurrentBindings() method
279	public void setCurrentBindings(Hashtable bindings)
280	{
281		view.getStatus().setMessage((String)bindings.get(PREFIX_STR));
282		currentBindings = bindings;
283	} //}}}
284
285	//{{{ handleKey() method
286	/**
287	 * Handles the given keystroke.
288	 * @param keyStroke The key stroke
289	 * @since jEdit 4.2pre5
290	 */
291	public boolean handleKey(KeyEventTranslator.Key keyStroke)
292	{
293		char input = '\0';
294		if(keyStroke.modifiers == null
295			|| keyStroke.modifiers.equals("S"))
296		{
297			switch(keyStroke.key)
298			{
299			case '\n':
300			case '\t':
301				input = (char)keyStroke.key;
302				break;
303			default:
304				input = keyStroke.input;
305				break;
306			}
307		}
308
309		if(readNextChar != null)
310		{
311			if(input != '\0')
312			{
313				setCurrentBindings(bindings);
314				invokeReadNextChar(input);
315				repeatCount = 1;
316				return true;
317			}
318			else
319			{
320				readNextChar = null;
321				view.getStatus().setMessage(null);
322			}
323		}
324
325		Object o = currentBindings.get(keyStroke);
326		if(o == null)
327		{
328			// Don't beep if the user presses some
329			// key we don't know about unless a
330			// prefix is active. Otherwise it will
331			// beep when caps lock is pressed, etc.
332			if(currentBindings != bindings)
333			{
334				Toolkit.getDefaultToolkit().beep();
335				// F10 should be passed on, but C+e F10
336				// shouldn't
337				repeatCount = 1;
338				setCurrentBindings(bindings);
339			}
340			else if(input != '\0')
341				userInput(input);
342			else
343			{
344				// this is retarded. excuse me while I drool
345				// and make stupid noises
346				if(KeyEventWorkaround.isNumericKeypad(keyStroke.key))
347					KeyEventWorkaround.numericKeypadKey();
348			}
349			sendShortcutPrefixOff();
350		}
351		else if(o instanceof Hashtable)
352		{
353			setCurrentBindings((Hashtable)o);
354			ShortcutPrefixActiveEvent.firePrefixStateChange(currentBindings, true);
355			shortcutOn = true;
356			return true;
357		}
358		else if(o instanceof String)
359		{
360			setCurrentBindings(bindings);
361			sendShortcutPrefixOff();
362			invokeAction((String)o);
363			return true;
364		}
365		else if(o instanceof EditAction)
366		{
367			setCurrentBindings(bindings);
368			sendShortcutPrefixOff();
369			invokeAction((EditAction)o);
370			return true;
371		}
372		sendShortcutPrefixOff();
373		return false;
374	} //}}}
375
376	//{{{ handleKey() methodprotected void sendShortcutPrefixOff()
377	/**
378	 *  If 
379	 */
380	protected void sendShortcutPrefixOff()
381	{
382		if( shortcutOn == true )
383		{
384			ShortcutPrefixActiveEvent.firePrefixStateChange(null, false);
385			shortcutOn = false;
386		}
387	} //}}}
388	
389	protected boolean shortcutOn=false;
390	
391	//{{{ getSymbolicModifierName() method
392	/**
393	 * Returns a the symbolic modifier name for the specified Java modifier
394	 * flag.
395	 *
396	 * @param mod A modifier constant from <code>InputEvent</code>
397	 *
398	 * @since jEdit 4.1pre3
399	 */
400	public static char getSymbolicModifierName(int mod)
401	{
402		return KeyEventTranslator.getSymbolicModifierName(mod);
403	} //}}}
404
405	//{{{ getModifierString() method
406	/**
407	 * Returns a string containing symbolic modifier names set in the
408	 * specified event.
409	 *
410	 * @param evt The event
411	 *
412	 * @since jEdit 4.1pre3
413	 */
414	public static String getModifierString(InputEvent evt)
415	{
416		return KeyEventTranslator.getModifierString(evt);
417	} //}}}
418
419	//{{{ Private members
420
421	// Stores prefix name in bindings hashtable
422	public static Object PREFIX_STR = "PREFIX_STR";
423
424	private Hashtable bindings;
425	private Hashtable currentBindings;
426	//}}}
427
428}