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