PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/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
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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 5310 2005-12-19 02:29:26Z jchoyt $
  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. * @since jEdit 4.2pre5
  265. */
  266. public boolean handleKey(KeyEventTranslator.Key keyStroke)
  267. {
  268. char input = '\0';
  269. if(keyStroke.modifiers == null
  270. || keyStroke.modifiers.equals("S"))
  271. {
  272. switch(keyStroke.key)
  273. {
  274. case '\n':
  275. case '\t':
  276. input = (char)keyStroke.key;
  277. break;
  278. default:
  279. input = keyStroke.input;
  280. break;
  281. }
  282. }
  283. if(readNextChar != null)
  284. {
  285. if(input != '\0')
  286. {
  287. setCurrentBindings(bindings);
  288. invokeReadNextChar(input);
  289. repeatCount = 1;
  290. return true;
  291. }
  292. else
  293. {
  294. readNextChar = null;
  295. view.getStatus().setMessage(null);
  296. }
  297. }
  298. Object o = currentBindings.get(keyStroke);
  299. if(o == null)
  300. {
  301. // Don't beep if the user presses some
  302. // key we don't know about unless a
  303. // prefix is active. Otherwise it will
  304. // beep when caps lock is pressed, etc.
  305. if(currentBindings != bindings)
  306. {
  307. Toolkit.getDefaultToolkit().beep();
  308. // F10 should be passed on, but C+e F10
  309. // shouldn't
  310. repeatCount = 1;
  311. setCurrentBindings(bindings);
  312. }
  313. else if(input != '\0')
  314. userInput(input);
  315. else
  316. {
  317. // this is retarded. excuse me while I drool
  318. // and make stupid noises
  319. if(KeyEventWorkaround.isNumericKeypad(keyStroke.key))
  320. KeyEventWorkaround.numericKeypadKey();
  321. }
  322. sendShortcutPrefixOff();
  323. }
  324. else if(o instanceof Hashtable)
  325. {
  326. setCurrentBindings((Hashtable)o);
  327. ShortcutPrefixActiveEvent.firePrefixStateChange(currentBindings, true);
  328. shortcutOn = true;
  329. return true;
  330. }
  331. else if(o instanceof String)
  332. {
  333. setCurrentBindings(bindings);
  334. sendShortcutPrefixOff();
  335. invokeAction((String)o);
  336. return true;
  337. }
  338. else if(o instanceof EditAction)
  339. {
  340. setCurrentBindings(bindings);
  341. sendShortcutPrefixOff();
  342. invokeAction((EditAction)o);
  343. return true;
  344. }
  345. sendShortcutPrefixOff();
  346. return false;
  347. } //}}}
  348. //{{{ handleKey() methodprotected void sendShortcutPrefixOff()
  349. /**
  350. * If
  351. */
  352. protected void sendShortcutPrefixOff()
  353. {
  354. if( shortcutOn == true )
  355. {
  356. ShortcutPrefixActiveEvent.firePrefixStateChange(null, false);
  357. shortcutOn = false;
  358. }
  359. } //}}}
  360. protected boolean shortcutOn=false;
  361. //{{{ getSymbolicModifierName() method
  362. /**
  363. * Returns a the symbolic modifier name for the specified Java modifier
  364. * flag.
  365. *
  366. * @param mod A modifier constant from <code>InputEvent</code>
  367. *
  368. * @since jEdit 4.1pre3
  369. */
  370. public static char getSymbolicModifierName(int mod)
  371. {
  372. return KeyEventTranslator.getSymbolicModifierName(mod);
  373. } //}}}
  374. //{{{ getModifierString() method
  375. /**
  376. * Returns a string containing symbolic modifier names set in the
  377. * specified event.
  378. *
  379. * @param evt The event
  380. *
  381. * @since jEdit 4.1pre3
  382. */
  383. public static String getModifierString(InputEvent evt)
  384. {
  385. return KeyEventTranslator.getModifierString(evt);
  386. } //}}}
  387. //{{{ Private members
  388. // Stores prefix name in bindings hashtable
  389. public static Object PREFIX_STR = "PREFIX_STR";
  390. private Hashtable bindings;
  391. private Hashtable currentBindings;
  392. //}}}
  393. }