PageRenderTime 78ms CodeModel.GetById 61ms app.highlight 14ms RepoModel.GetById 0ms app.codeStats 1ms

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

#
Java | 511 lines | 355 code | 39 blank | 117 comment | 85 complexity | 1ee0ca211812e7758851f9d35f24dcf4 MD5 | raw file
  1/*
  2 * KeyEventTranslator.java - Hides some warts of AWT event API
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2003, 2005 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.*;
 27import java.util.HashMap;
 28import java.util.Map;
 29import org.gjt.sp.jedit.Debug;
 30import org.gjt.sp.jedit.OperatingSystem;
 31import org.gjt.sp.util.Log;
 32import org.gjt.sp.util.StandardUtilities;
 33//}}}
 34
 35/**
 36 * In conjunction with the <code>KeyEventWorkaround</code>, hides some
 37 * warts in the AWT key event API.
 38 *
 39 * @author Slava Pestov
 40 * @version $Id: KeyEventTranslator.java 20108 2011-10-18 12:16:38Z evanpw $
 41 */
 42public class KeyEventTranslator
 43{
 44	//{{{ addTranslation() method
 45	/**
 46	 * Adds a keyboard translation.
 47	 * @param key1 Translate this key
 48	 * @param key2 Into this key
 49	 * @since jEdit 4.2pre3
 50	 */
 51	public static void addTranslation(Key key1, Key key2)
 52	{
 53		transMap.put(key1,key2);
 54	} //}}}
 55
 56	//{{{ translateKeyEvent() method
 57
 58	protected static KeyEvent lastKeyPressEvent;
 59
 60	protected static boolean lastKeyPressAccepted;
 61
 62	/**
 63	 * Pass this an event from {@link
 64	 * KeyEventWorkaround#processKeyEvent(java.awt.event.KeyEvent)}.
 65	 * @param evt the KeyEvent to translate
 66	 * @since jEdit 4.2pre3
 67	 */
 68	public static Key translateKeyEvent(KeyEvent evt)
 69	{
 70		int modifiers = evt.getModifiers();
 71		Key returnValue;
 72
 73		switch(evt.getID())
 74		{
 75		case KeyEvent.KEY_PRESSED:
 76			int keyCode = evt.getKeyCode();
 77			if((keyCode >= KeyEvent.VK_0
 78				&& keyCode <= KeyEvent.VK_9)
 79				|| (keyCode >= KeyEvent.VK_A
 80				&& keyCode <= KeyEvent.VK_Z))
 81			{
 82				if(Debug.ALTERNATIVE_DISPATCHER)
 83					return null;
 84				else
 85				{
 86					returnValue = new Key(
 87						modifiersToString(modifiers),
 88						'\0',Character.toLowerCase(
 89						(char)keyCode));
 90				}
 91			}
 92			else
 93			{
 94				if(keyCode == KeyEvent.VK_TAB)
 95				{
 96					evt.consume();
 97					returnValue = new Key(
 98						modifiersToString(modifiers),
 99						keyCode,'\0');
100				}
101				else if(keyCode == KeyEvent.VK_SPACE)
102				{
103					// for SPACE or S+SPACE we pass the
104					// key typed since international
105					// keyboards sometimes produce a
106					// KEY_PRESSED SPACE but not a
107					// KEY_TYPED SPACE, eg if you have to
108					// do a "<space> to insert ".
109					if((modifiers & ~InputEvent.SHIFT_MASK) == 0)
110						returnValue = null;
111                    else if (Debug.ALTERNATIVE_DISPATCHER && (modifiers & ~InputEvent.META_MASK) == 0)
112                        returnValue = null;
113					else
114					{
115						returnValue = new Key(
116							modifiersToString(modifiers),
117							0,' ');
118					}
119				}
120				else
121				{
122					returnValue = new Key(
123						modifiersToString(modifiers),
124						keyCode,'\0');
125				}
126			}
127			break;
128		case KeyEvent.KEY_TYPED:
129			char ch = evt.getKeyChar();
130
131			if(KeyEventWorkaround.isMacControl(evt))
132				ch |= 0x60;
133
134			switch(ch)
135			{
136			case '\n':
137			case '\t':
138			case '\b':
139				return null;
140			case ' ':
141                if (Debug.ALTERNATIVE_DISPATCHER && (modifiers & ~InputEvent.META_MASK) == 0)
142                    returnValue = new Key(
143                        modifiersToString(modifiers),
144                        0,' ');
145				else if((modifiers & ~InputEvent.SHIFT_MASK) != 0)
146					return null;
147			}
148
149			int ignoreMods;
150			if(Debug.ALT_KEY_PRESSED_DISABLED)
151			{
152				/* on MacOS, A+ can be user input */
153				ignoreMods = InputEvent.SHIFT_MASK
154					| InputEvent.ALT_GRAPH_MASK
155					| InputEvent.ALT_MASK;
156			}
157			else
158			{
159				/* on MacOS, A+ can be user input */
160				ignoreMods = InputEvent.SHIFT_MASK
161					| InputEvent.ALT_GRAPH_MASK;
162			}
163
164			if((modifiers & InputEvent.ALT_GRAPH_MASK) == 0
165				&& (modifiers & ~ignoreMods) != 0)
166			{
167				if(Debug.ALTERNATIVE_DISPATCHER)
168				{
169					returnValue = new Key(
170						modifiersToString(modifiers),
171						0,ch);
172				}
173				else
174					return null;
175			}
176			else
177			{
178				if(ch == ' ')
179				{
180					returnValue = new Key(
181						modifiersToString(modifiers),
182						0,ch);
183				}
184				else
185					returnValue = new Key(null,0,ch);
186			}
187			break;
188		default:
189			return null;
190		}
191
192		/* I guess translated events do not have the 'evt' field set
193		so consuming won't work. I don't think this is a problem as
194		nothing uses translation anyway */
195		Key trans = transMap.get(returnValue);
196		if(trans == null)
197			return returnValue;
198		else
199			return trans;
200	} //}}}
201
202	//{{{ parseKey() method
203	/**
204	 * Converts a string to a keystroke. The string should be of the
205	 * form <i>modifiers</i>+<i>shortcut</i> where <i>modifiers</i>
206	 * is any combination of A for Alt, C for Control, S for Shift
207	 * or M for Meta, and <i>shortcut</i> is either a single character,
208	 * or a keycode name from the <code>KeyEvent</code> class, without
209	 * the <code>VK_</code> prefix.
210	 * @param keyStroke A string description of the key stroke
211	 * @since jEdit 4.2pre3
212	 */
213	public static Key parseKey(String keyStroke)
214	{
215		if(keyStroke == null)
216			return null;
217		int modifiers = 0;
218		String key;
219		int endOfModifiers = keyStroke.indexOf('+');
220		if(endOfModifiers <= 0)	// not found or found at first
221		{
222			key = keyStroke;
223		}
224		else
225		{
226			for(int i = 0; i < endOfModifiers; i++)
227			{
228				switch(Character.toUpperCase(keyStroke
229					.charAt(i)))
230				{
231				case 'A':
232					modifiers |= a;
233					break;
234				case 'C':
235					modifiers |= c;
236					break;
237				case 'M':
238					modifiers |= m;
239					break;
240				case 'S':
241					modifiers |= s;
242					break;
243				}
244			}
245			key = keyStroke.substring(endOfModifiers + 1);
246		}
247		if(key.length() == 1)
248		{
249			return new Key(modifiersToString(modifiers),0,key.charAt(0));
250		}
251		else if(key.length() == 0)
252		{
253			Log.log(Log.ERROR,KeyEventTranslator.class,
254				"Invalid key stroke: " + keyStroke);
255			return null;
256		}
257		else if(key.equals("SPACE"))
258		{
259			return new Key(modifiersToString(modifiers),0,' ');
260		}
261		else
262		{
263			int ch;
264
265			try
266			{
267				ch = KeyEvent.class.getField("VK_".concat(key))
268					.getInt(null);
269			}
270			catch(Exception e)
271			{
272				Log.log(Log.ERROR,KeyEventTranslator.class,
273					"Invalid key stroke: "
274					+ keyStroke);
275				return null;
276			}
277
278			return new Key(modifiersToString(modifiers),ch,'\0');
279		}
280	} //}}}
281
282	//{{{ setModifierMapping() method
283	/**
284	 * Changes the mapping between symbolic modifier key names
285	 * (<code>C</code>, <code>A</code>, <code>M</code>, <code>S</code>) and
286	 * Java modifier flags.
287	 *
288	 * You can map more than one Java modifier to a symobolic modifier, for
289	 * example :
290	 * <p><code><pre>
291	 *	setModifierMapping(
292	 *		InputEvent.CTRL_MASK,
293	 *		InputEvent.ALT_MASK | InputEvent.META_MASK,
294	 *		0,
295	 *		InputEvent.SHIFT_MASK);
296	 *<pre></code></p>
297	 *
298	 * You cannot map a Java modifer to more than one symbolic modifier.
299	 *
300	 * @param c The modifier(s) to map the <code>C</code> modifier to
301	 * @param a The modifier(s) to map the <code>A</code> modifier to
302	 * @param m The modifier(s) to map the <code>M</code> modifier to
303	 * @param s The modifier(s) to map the <code>S</code> modifier to
304	 *
305	 * @since jEdit 4.2pre3
306	 */
307	public static void setModifierMapping(int c, int a, int m, int s)
308	{
309
310		int duplicateMapping =
311			(c & a) | (c & m) | (c & s) | (a & m) | (a & s) | (m & s);
312
313		if((duplicateMapping & InputEvent.CTRL_MASK) != 0)
314		{
315			throw new IllegalArgumentException(
316				"CTRL is mapped to more than one modifier");
317		}
318		if((duplicateMapping & InputEvent.ALT_MASK) != 0)
319		{
320			throw new IllegalArgumentException(
321				"ALT is mapped to more than one modifier");
322		}
323		if((duplicateMapping & InputEvent.META_MASK) != 0)
324		{
325			throw new IllegalArgumentException(
326				"META is mapped to more than one modifier");
327		}
328		if((duplicateMapping & InputEvent.SHIFT_MASK) != 0)
329		{
330			throw new IllegalArgumentException(
331				"SHIFT is mapped to more than one modifier");
332		}
333
334		KeyEventTranslator.c = c;
335		KeyEventTranslator.a = a;
336		KeyEventTranslator.m = m;
337		KeyEventTranslator.s = s;
338	} //}}}
339
340	//{{{ getSymbolicModifierName() method
341	/**
342	 * Returns a the symbolic modifier name for the specified Java modifier
343	 * flag.
344	 *
345	 * @param mod A modifier constant from <code>InputEvent</code>
346	 *
347	 * @since jEdit 4.2pre3
348	 */
349	public static char getSymbolicModifierName(int mod)
350	{
351		if((mod & c) != 0)
352			return 'C';
353		else if((mod & a) != 0)
354			return 'A';
355		else if((mod & m) != 0)
356			return 'M';
357		else if((mod & s) != 0)
358			return 'S';
359		else
360			return '\0';
361	} //}}}
362
363	//{{{ modifiersToString() method
364	private static final int[] MODS = {
365		InputEvent.CTRL_MASK,
366		InputEvent.ALT_MASK,
367		InputEvent.META_MASK,
368		InputEvent.SHIFT_MASK
369	};
370
371	public static String modifiersToString(int mods)
372	{
373		StringBuilder buf = null;
374
375		for(int i = 0; i < MODS.length; i++)
376		{
377			if((mods & MODS[i]) != 0)
378				buf = lazyAppend(buf,getSymbolicModifierName(MODS[i]));
379		}
380
381		if(buf == null)
382			return null;
383		else
384			return buf.toString();
385	} //}}}
386
387	//{{{ getModifierString() method
388	/**
389	 * Returns a string containing symbolic modifier names set in the
390	 * specified event.
391	 *
392	 * @param evt The event
393	 *
394	 * @since jEdit 4.2pre3
395	 */
396	public static String getModifierString(InputEvent evt)
397	{
398		StringBuilder buf = new StringBuilder();
399		if(evt.isControlDown())
400			buf.append(getSymbolicModifierName(InputEvent.CTRL_MASK));
401		if(evt.isAltDown())
402			buf.append(getSymbolicModifierName(InputEvent.ALT_MASK));
403		if(evt.isMetaDown())
404			buf.append(getSymbolicModifierName(InputEvent.META_MASK));
405		if(evt.isShiftDown())
406			buf.append(getSymbolicModifierName(InputEvent.SHIFT_MASK));
407		return buf.length() == 0 ? null : buf.toString();
408	} //}}}
409
410	static int c, a, m, s;
411
412	//{{{ Private members
413	/** This map is a pool of Key. */
414	private static final Map<Key, Key> transMap = new HashMap<Key, Key>();
415
416	private static StringBuilder lazyAppend(StringBuilder buf, char ch)
417	{
418		if(buf == null)
419			buf = new StringBuilder();
420		if(buf.indexOf(String.valueOf(ch)) == -1)
421			buf.append(ch);
422		return buf;
423	}
424
425	static
426	{
427		if(OperatingSystem.isMacOS())
428		{
429			setModifierMapping(
430				InputEvent.META_MASK,  /* == C+ */
431				InputEvent.CTRL_MASK,  /* == A+ */
432				/* M+ discarded by key event workaround! */
433				InputEvent.ALT_MASK,   /* == M+ */
434				InputEvent.SHIFT_MASK  /* == S+ */);
435		}
436		else
437		{
438			setModifierMapping(
439				InputEvent.CTRL_MASK,
440				InputEvent.ALT_MASK,
441				InputEvent.META_MASK,
442				InputEvent.SHIFT_MASK);
443		}
444	} //}}}
445
446	//{{{ Key class
447	public static class Key
448	{
449		public final String modifiers;
450		public final int key;
451		public final char input;
452
453		private final int hashCode;
454		/**
455			Wether this Key event applies to all jEdit windows (and not only a specific jEdit GUI component).
456		*/
457		protected boolean isFromGlobalContext;
458
459		public Key(String modifiers, int key, char input)
460		{
461			this.modifiers = modifiers;
462			this.key = key;
463			this.input = input;
464			hashCode = key + input;
465		}
466
467		@Override
468		public int hashCode()
469		{
470			return hashCode;
471		}
472
473		@Override
474		public boolean equals(Object o)
475		{
476			if(o instanceof Key)
477			{
478				Key k = (Key)o;
479				if(StandardUtilities.objectsEqual(modifiers,
480					k.modifiers) && key == k.key
481					&& input == k.input)
482				{
483					return true;
484				}
485			}
486
487			return false;
488		}
489
490		@Override
491		public String toString()
492		{
493			return (modifiers == null ? "" : modifiers)
494				+ '<'
495				+ Integer.toString(key,16)
496				+ ','
497				+ Integer.toString(input,16)
498				+ '>';
499		}
500
501		public void setIsFromGlobalContext(boolean to)
502		{
503			isFromGlobalContext = to;
504		}
505
506		public boolean isFromGlobalContext()
507		{
508			return isFromGlobalContext;
509		}
510	} //}}}
511}