PageRenderTime 209ms CodeModel.GetById 201ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/gui/DefaultInputHandler.java

#
Java | 245 lines | 143 code | 13 blank | 89 comment | 33 complexity | 0b89653e006439539f2c8147b03b2e36 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 java.awt.event.InputEvent;
 27import java.awt.Toolkit;
 28import java.util.Hashtable;
 29import org.gjt.sp.jedit.*;
 30//}}}
 31
 32/**
 33 * The default input handler. It maps sequences of keystrokes into actions
 34 * and inserts key typed events into the text area.
 35 * @author Slava Pestov
 36 * @version $Id: DefaultInputHandler.java 12504 2008-04-22 23:12:43Z ezust $
 37 */
 38public class DefaultInputHandler extends InputHandler
 39{
 40	//{{{ DefaultInputHandler constructor
 41	/**
 42	 * Creates a new input handler with no key bindings defined.
 43	 * @param view The view
 44	 * @param bindings An explicitly-specified set of key bindings,
 45	 * must not be null.
 46	 * @since jEdit 4.3pre1
 47	 */
 48	public DefaultInputHandler(View view, Hashtable bindings)
 49	{
 50		super(view);
 51
 52		if(bindings == null)
 53			throw new NullPointerException();
 54		this.bindings = this.currentBindings = bindings;
 55	} //}}}
 56
 57	//{{{ DefaultInputHandler constructor
 58	/**
 59	 * Creates a new input handler with no key bindings defined.
 60	 * @param view The view
 61	 */
 62	public DefaultInputHandler(View view)
 63	{
 64		this(view,new Hashtable());
 65	} //}}}
 66
 67	//{{{ DefaultInputHandler constructor
 68	/**
 69	 * Creates a new input handler with the same set of key bindings
 70	 * as the one specified. Note that both input handlers share
 71	 * a pointer to exactly the same key binding table; so adding
 72	 * a key binding in one will also add it to the other.
 73	 * @param copy The input handler to copy key bindings from
 74	 * @param view The view
 75	 */
 76	public DefaultInputHandler(View view, DefaultInputHandler copy)
 77	{
 78		this(view,copy.bindings);
 79	} //}}}
 80
 81	//{{{ isPrefixActive() method
 82	/**
 83	 * Returns if a prefix key has been pressed.
 84	 */
 85	@Override
 86	public boolean isPrefixActive()
 87	{
 88		return bindings != currentBindings
 89			|| super.isPrefixActive();
 90	} //}}}
 91
 92	//{{{ setCurrentBindings() method
 93	@Override
 94	public void setCurrentBindings(Hashtable bindings)
 95	{
 96		view.getStatus().setMessage((String)bindings.get(PREFIX_STR));
 97		currentBindings = bindings;
 98	} //}}}
 99
100	//{{{ handleKey() method
101	/**
102	 * Handles the given keystroke.
103	 * @param keyStroke The key stroke
104	 * @param dryRun only calculate the return value, do not have any other effect
105	 * @since jEdit 4.2pre5
106	 */
107	public boolean handleKey(KeyEventTranslator.Key keyStroke,boolean dryRun)
108	{
109		char input = '\0';
110		if(keyStroke.modifiers == null
111			|| keyStroke.modifiers.equals("S"))
112		{
113			switch(keyStroke.key)
114			{
115			case '\n':
116			case '\t':
117				input = (char)keyStroke.key;
118				break;
119			default:
120				input = keyStroke.input;
121				break;
122			}
123		}
124
125		if(readNextChar != null)
126		{
127			if(input != '\0')
128			{
129				if (!dryRun)
130				{
131					setCurrentBindings(bindings);
132					invokeReadNextChar(input);
133					repeatCount = 1;
134				}
135				return true;
136			}
137			else
138			{
139				if (!dryRun) 
140				{
141					readNextChar = null;
142					view.getStatus().setMessage(null);
143				}
144			}
145		}
146
147		Object o = currentBindings.get(keyStroke);
148		if(o == null)
149		{
150			if (!dryRun) 
151			{
152				// Don't beep if the user presses some
153				// key we don't know about unless a
154				// prefix is active. Otherwise it will
155				// beep when caps lock is pressed, etc.
156				if(currentBindings != bindings)
157				{
158					Toolkit.getDefaultToolkit().beep();
159					// F10 should be passed on, but C+e F10
160					// shouldn't
161					repeatCount = 1;
162					setCurrentBindings(bindings);
163				}
164				else if(input != '\0') 
165				{
166					if (!keyStroke.isFromGlobalContext()) 
167					{ // let user input be only local
168						userInput(input);
169					}
170				} 
171				else
172				{
173					// this is retarded. excuse me while I drool
174					// and make stupid noises
175					if(KeyEventWorkaround.isNumericKeypad(keyStroke.key))
176						KeyEventWorkaround.numericKeypadKey();
177				}
178				sendShortcutPrefixOff();
179			}
180		}
181		else if(o instanceof Hashtable)
182		{
183			if (!dryRun) 
184			{
185				setCurrentBindings((Hashtable)o);
186				ShortcutPrefixActiveEvent.firePrefixStateChange(currentBindings, true);
187				shortcutOn = true;
188			}
189			return true;
190		}
191		else if(o instanceof String)
192		{
193			if (!dryRun) 
194			{
195				setCurrentBindings(bindings);
196				sendShortcutPrefixOff();
197				invokeAction((String)o);
198			}
199			return true;
200		}
201		else if(o instanceof EditAction)
202		{
203			if (!dryRun)
204			{
205				setCurrentBindings(bindings);
206				sendShortcutPrefixOff();
207				invokeAction((EditAction)o);
208			}
209			return true;
210		}
211		if (!dryRun)
212		{
213			sendShortcutPrefixOff();
214		}
215		return false;
216	} //}}}
217	
218	//{{{ getSymbolicModifierName() method
219	/**
220	 * Returns a the symbolic modifier name for the specified Java modifier
221	 * flag.
222	 *
223	 * @param mod A modifier constant from <code>InputEvent</code>
224	 *
225	 * @since jEdit 4.1pre3
226	 */
227	public static char getSymbolicModifierName(int mod)
228	{
229		return KeyEventTranslator.getSymbolicModifierName(mod);
230	} //}}}
231
232	//{{{ getModifierString() method
233	/**
234	 * Returns a string containing symbolic modifier names set in the
235	 * specified event.
236	 *
237	 * @param evt The event
238	 *
239	 * @since jEdit 4.1pre3
240	 */
241	public static String getModifierString(InputEvent evt)
242	{
243		return KeyEventTranslator.getModifierString(evt);
244	} //}}}
245}