You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SwingInputHandler.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Copyright (c) 2006-2013 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.ui_swing.components.inputfields;
  23. import com.dmdirc.WritableFrameContainer;
  24. import com.dmdirc.addons.ui_swing.Apple;
  25. import com.dmdirc.addons.ui_swing.UIUtilities;
  26. import com.dmdirc.addons.ui_swing.components.LoggingSwingWorker;
  27. import com.dmdirc.commandparser.parsers.CommandParser;
  28. import com.dmdirc.interfaces.ui.InputField;
  29. import com.dmdirc.interfaces.ui.UIController;
  30. import com.dmdirc.logger.ErrorLevel;
  31. import com.dmdirc.logger.Logger;
  32. import com.dmdirc.ui.input.InputHandler;
  33. import java.awt.event.ActionEvent;
  34. import java.awt.event.KeyEvent;
  35. import java.awt.event.KeyListener;
  36. import javax.swing.AbstractAction;
  37. import javax.swing.JComponent;
  38. import javax.swing.JTextField;
  39. import javax.swing.KeyStroke;
  40. import javax.swing.text.JTextComponent;
  41. /**
  42. * Swing input handler.
  43. */
  44. public class SwingInputHandler extends InputHandler implements KeyListener {
  45. /**
  46. * Creates a new instance of InputHandler. Adds listeners to the target
  47. * that we need to operate.
  48. *
  49. * @param controller The UIController that owns this input handler.
  50. * @param target The text field this input handler is dealing with.
  51. * @param commandParser The command parser to use for this text field.
  52. * @param parentWindow The window that owns this input handler
  53. */
  54. public SwingInputHandler(final UIController controller,
  55. final InputField target,
  56. final CommandParser commandParser,
  57. final WritableFrameContainer parentWindow) {
  58. super(controller, target, commandParser, parentWindow);
  59. }
  60. /** {@inheritDoc} */
  61. @Override
  62. protected void addUpHandler() {
  63. JTextComponent localTarget = null;
  64. if (target instanceof JTextComponent) {
  65. localTarget = (JTextComponent) target;
  66. } else if (target instanceof SwingInputField) {
  67. localTarget = ((SwingInputField) target).getTextField();
  68. } else {
  69. throw new IllegalArgumentException("Unknown target");
  70. }
  71. localTarget.getActionMap().put("upArrow", new AbstractAction() {
  72. /** Serial version UID. */
  73. private static final long serialVersionUID = 1;
  74. /** {@inheritDoc} */
  75. @Override
  76. public void actionPerformed(final ActionEvent e) {
  77. doBufferUp();
  78. }
  79. });
  80. if (Apple.isAppleUI()) {
  81. localTarget.getInputMap(JComponent.WHEN_FOCUSED).
  82. put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "upArrow");
  83. } else {
  84. localTarget.getInputMap(
  85. JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).
  86. put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "upArrow");
  87. }
  88. }
  89. /** {@inheritDoc} */
  90. @Override
  91. protected void addDownHandler() {
  92. JTextComponent localTarget = null;
  93. if (target instanceof JTextComponent) {
  94. localTarget = (JTextComponent) target;
  95. } else if (target instanceof SwingInputField) {
  96. localTarget = ((SwingInputField) target).getTextField();
  97. } else {
  98. throw new IllegalArgumentException("Unknown target");
  99. }
  100. localTarget.getActionMap().put("downArrow", new AbstractAction() {
  101. /** Serial version UID. */
  102. private static final long serialVersionUID = 1;
  103. /** {@inheritDoc} */
  104. @Override
  105. public void actionPerformed(final ActionEvent e) {
  106. doBufferDown();
  107. }
  108. });
  109. if (Apple.isAppleUI()) {
  110. localTarget.getInputMap(JComponent.WHEN_FOCUSED).
  111. put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0),
  112. "downArrow");
  113. } else {
  114. localTarget.getInputMap(
  115. JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).
  116. put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0),
  117. "downArrow");
  118. }
  119. }
  120. /** {@inheritDoc} */
  121. @Override
  122. protected void addTabHandler() {
  123. final JTextComponent localTarget;
  124. if (target instanceof JTextComponent) {
  125. localTarget = (JTextComponent) target;
  126. } else if (target instanceof SwingInputField) {
  127. localTarget = ((SwingInputField) target).getTextField();
  128. } else {
  129. throw new IllegalArgumentException("Unknown target");
  130. }
  131. localTarget.getActionMap().put("insert-tab", new AbstractAction() {
  132. /** Serial version UID. */
  133. private static final long serialVersionUID = 1;
  134. /** {@inheritDoc} */
  135. @Override
  136. public void actionPerformed(final ActionEvent e) {
  137. new LoggingSwingWorker<Object, Void>() {
  138. /** {@inheritDoc} */
  139. @Override
  140. protected Object doInBackground() {
  141. localTarget.setEditable(false);
  142. doTabCompletion(false);
  143. return null;
  144. }
  145. /** {@inheritDoc} */
  146. @Override
  147. protected void done() {
  148. localTarget.setEditable(true);
  149. }
  150. }.executeInExecutor();
  151. }
  152. });
  153. localTarget.getActionMap().put("insert-shift-tab",
  154. new AbstractAction() {
  155. /** Serial version UID. */
  156. private static final long serialVersionUID = 1;
  157. /** {@inheritDoc} */
  158. @Override
  159. public void actionPerformed(final ActionEvent e) {
  160. new LoggingSwingWorker<Object, Void>() {
  161. /** {@inheritDoc} */
  162. @Override
  163. protected Object doInBackground() {
  164. localTarget.setEditable(false);
  165. doTabCompletion(true);
  166. return null;
  167. }
  168. /** {@inheritDoc} */
  169. @Override
  170. protected void done() {
  171. localTarget.setEditable(true);
  172. }
  173. }.executeInExecutor();
  174. }
  175. });
  176. localTarget.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).
  177. put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), "insert-tab");
  178. localTarget.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).
  179. put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB,
  180. KeyEvent.SHIFT_MASK), "insert-shift-tab");
  181. }
  182. /** {@inheritDoc} */
  183. @Override
  184. protected void addEnterHandler() {
  185. JTextComponent localTarget = null;
  186. if (target instanceof JTextComponent) {
  187. localTarget = (JTextComponent) target;
  188. } else if (target instanceof SwingInputField) {
  189. localTarget = ((SwingInputField) target).getTextField();
  190. } else {
  191. throw new IllegalArgumentException("Unknown target");
  192. }
  193. localTarget.getActionMap().put("enterButton", new AbstractAction() {
  194. /** Serial version UID. */
  195. private static final long serialVersionUID = 1;
  196. /** {@inheritDoc} */
  197. @Override
  198. public void actionPerformed(final ActionEvent e) {
  199. final String line = target.getText();
  200. target.setText("");
  201. UIUtilities.invokeLater(new Runnable() {
  202. /** {@inheritDoc} */
  203. @Override
  204. public void run() {
  205. final JTextField source;
  206. if (e.getSource() instanceof SwingInputField) {
  207. source = ((SwingInputField) e.getSource())
  208. .getTextField();
  209. } else if (e.getSource() instanceof JTextField) {
  210. source = (JTextField) e.getSource();
  211. } else {
  212. Logger.appError(ErrorLevel.HIGH, "Unable to send "
  213. + "line", new IllegalArgumentException(
  214. "Event is not from known source."));
  215. return;
  216. }
  217. if (source.isEditable()) {
  218. new LoggingSwingWorker<Object, Void>() {
  219. /** {@inheritDoc} */
  220. @Override
  221. protected Object doInBackground() {
  222. enterPressed(line);
  223. return null;
  224. }
  225. }.executeInExecutor();
  226. }
  227. }
  228. });
  229. }
  230. });
  231. localTarget.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).
  232. put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
  233. "enterButton");
  234. }
  235. /** {@inheritDoc} */
  236. @Override
  237. protected void addKeyHandler() {
  238. target.addKeyListener(this);
  239. }
  240. /**
  241. * {@inheritDoc}
  242. *
  243. * @param e Key event
  244. */
  245. @Override
  246. public void keyTyped(final KeyEvent e) {
  247. //Ignore
  248. }
  249. /**
  250. * {@inheritDoc}
  251. *
  252. * @param e Key event
  253. */
  254. @Override
  255. public void keyPressed(final KeyEvent e) {
  256. if (e.getKeyCode() != KeyEvent.VK_TAB && e.getKeyCode()
  257. != KeyEvent.VK_UP && e.getKeyCode() != KeyEvent.VK_DOWN) {
  258. final String line = target.getText();
  259. if (UIUtilities.isCtrlDown(e) && e.getKeyCode() == KeyEvent.VK_ENTER
  260. && (flags & HANDLE_RETURN) == HANDLE_RETURN) {
  261. target.setText("");
  262. }
  263. handleKeyPressed(line, target.getCaretPosition(), e.getKeyCode(),
  264. e.isShiftDown(), UIUtilities.isCtrlDown(e));
  265. }
  266. }
  267. /**
  268. * {@inheritDoc}
  269. *
  270. * @param e Key event
  271. */
  272. @Override
  273. public void keyReleased(final KeyEvent e) {
  274. validateText();
  275. }
  276. }