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

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