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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright (c) 2006-2015 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.DMDircMBassador;
  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.CommandController;
  29. import com.dmdirc.interfaces.WindowModel;
  30. import com.dmdirc.interfaces.ui.InputField;
  31. import com.dmdirc.plugins.ServiceManager;
  32. import com.dmdirc.ui.input.InputHandler;
  33. import com.dmdirc.ui.input.TabCompleterUtils;
  34. import java.awt.event.ActionEvent;
  35. import java.awt.event.KeyEvent;
  36. import java.awt.event.KeyListener;
  37. import javax.swing.AbstractAction;
  38. import javax.swing.JComponent;
  39. import javax.swing.JTextField;
  40. import javax.swing.KeyStroke;
  41. import javax.swing.text.JTextComponent;
  42. /**
  43. * Swing input handler.
  44. */
  45. public class SwingInputHandler extends InputHandler implements KeyListener {
  46. private final DMDircMBassador eventBus;
  47. /**
  48. * Creates a new instance of InputHandler. Adds listeners to the target that we need to operate.
  49. *
  50. * @param serviceManager Manager to use to look up tab completion services.
  51. * @param target The text field this input handler is dealing with.
  52. * @param commandController The controller to use to retrieve command information.
  53. * @param commandParser The command parser to use for this text field.
  54. * @param parentWindow The window that owns this input handler
  55. * @param eventBus The event bus to use to dispatch input events.
  56. */
  57. public SwingInputHandler(
  58. final ServiceManager serviceManager,
  59. final InputField target,
  60. final CommandController commandController,
  61. final CommandParser commandParser,
  62. final WindowModel parentWindow,
  63. final TabCompleterUtils tabCompleterUtils,
  64. final DMDircMBassador eventBus) {
  65. super(serviceManager, target, commandController, commandParser, parentWindow,
  66. tabCompleterUtils, eventBus);
  67. this.eventBus = eventBus;
  68. }
  69. @Override
  70. protected void addUpHandler() {
  71. final JTextComponent localTarget;
  72. if (target instanceof JTextComponent) {
  73. localTarget = (JTextComponent) target;
  74. } else if (target instanceof SwingInputField) {
  75. localTarget = ((SwingInputField) target).getTextField();
  76. } else {
  77. throw new IllegalArgumentException("Unknown target");
  78. }
  79. localTarget.getActionMap().put("upArrow", new AbstractAction() {
  80. /** Serial version UID. */
  81. private static final long serialVersionUID = 1;
  82. @Override
  83. public void actionPerformed(final ActionEvent e) {
  84. doBufferUp();
  85. }
  86. });
  87. if (Apple.isAppleUI()) {
  88. localTarget.getInputMap(JComponent.WHEN_FOCUSED).
  89. put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "upArrow");
  90. } else {
  91. localTarget.getInputMap(
  92. JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).
  93. put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "upArrow");
  94. }
  95. }
  96. @Override
  97. protected void addDownHandler() {
  98. final JTextComponent localTarget;
  99. if (target instanceof JTextComponent) {
  100. localTarget = (JTextComponent) target;
  101. } else if (target instanceof SwingInputField) {
  102. localTarget = ((SwingInputField) target).getTextField();
  103. } else {
  104. throw new IllegalArgumentException("Unknown target");
  105. }
  106. localTarget.getActionMap().put("downArrow", new AbstractAction() {
  107. /** Serial version UID. */
  108. private static final long serialVersionUID = 1;
  109. @Override
  110. public void actionPerformed(final ActionEvent e) {
  111. doBufferDown();
  112. }
  113. });
  114. if (Apple.isAppleUI()) {
  115. localTarget.getInputMap(JComponent.WHEN_FOCUSED).
  116. put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0),
  117. "downArrow");
  118. } else {
  119. localTarget.getInputMap(
  120. JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).
  121. put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0),
  122. "downArrow");
  123. }
  124. }
  125. @Override
  126. protected void addTabHandler() {
  127. final JTextComponent localTarget;
  128. if (target instanceof JTextComponent) {
  129. localTarget = (JTextComponent) target;
  130. } else if (target instanceof SwingInputField) {
  131. localTarget = ((SwingInputField) target).getTextField();
  132. } else {
  133. throw new IllegalArgumentException("Unknown target");
  134. }
  135. localTarget.getActionMap().put("insert-tab", new AbstractAction() {
  136. /** Serial version UID. */
  137. private static final long serialVersionUID = 1;
  138. @Override
  139. public void actionPerformed(final ActionEvent e) {
  140. new LoggingSwingWorker<Object, Void>(eventBus) {
  141. @Override
  142. protected Object doInBackground() {
  143. localTarget.setEditable(false);
  144. doTabCompletion(false);
  145. return null;
  146. }
  147. @Override
  148. protected void done() {
  149. localTarget.setEditable(true);
  150. super.done();
  151. }
  152. }.execute();
  153. }
  154. });
  155. localTarget.getActionMap().put("insert-shift-tab",
  156. new AbstractAction() {
  157. /** Serial version UID. */
  158. private static final long serialVersionUID = 1;
  159. @Override
  160. public void actionPerformed(final ActionEvent e) {
  161. new LoggingSwingWorker<Object, Void>(eventBus) {
  162. @Override
  163. protected Object doInBackground() {
  164. localTarget.setEditable(false);
  165. doTabCompletion(true);
  166. return null;
  167. }
  168. @Override
  169. protected void done() {
  170. localTarget.setEditable(true);
  171. super.done();
  172. }
  173. }.execute();
  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. @Override
  183. protected void addEnterHandler() {
  184. final JTextComponent localTarget;
  185. if (target instanceof JTextComponent) {
  186. localTarget = (JTextComponent) target;
  187. } else if (target instanceof SwingInputField) {
  188. localTarget = ((SwingInputField) target).getTextField();
  189. } else {
  190. throw new IllegalArgumentException("Unknown target");
  191. }
  192. localTarget.getActionMap().put("enterButton", new AbstractAction() {
  193. /** Serial version UID. */
  194. private static final long serialVersionUID = 1;
  195. @Override
  196. public void actionPerformed(final ActionEvent e) {
  197. final String line = target.getText();
  198. target.setText("");
  199. UIUtilities.invokeLater(() -> {
  200. final JTextField source;
  201. if (e.getSource() instanceof SwingInputField) {
  202. source = ((SwingInputField) e.getSource())
  203. .getTextField();
  204. } else if (e.getSource() instanceof JTextField) {
  205. source = (JTextField) e.getSource();
  206. } else {
  207. throw new IllegalArgumentException(
  208. "Event is not from known source.");
  209. }
  210. if (source.isEditable()) {
  211. new LoggingSwingWorker<Object, Void>(eventBus) {
  212. @Override
  213. protected Object doInBackground() {
  214. enterPressed(line);
  215. return null;
  216. }
  217. }.execute();
  218. }
  219. });
  220. }
  221. });
  222. localTarget.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).
  223. put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
  224. "enterButton");
  225. }
  226. @Override
  227. protected void addKeyHandler() {
  228. target.addKeyListener(this);
  229. }
  230. @Override
  231. public void keyTyped(final KeyEvent e) {
  232. //Ignore
  233. }
  234. @Override
  235. public void keyPressed(final KeyEvent e) {
  236. if (e.getKeyCode() != KeyEvent.VK_TAB && e.getKeyCode()
  237. != KeyEvent.VK_UP && e.getKeyCode() != KeyEvent.VK_DOWN) {
  238. final String line = target.getText();
  239. if (UIUtilities.isCtrlDown(e) && e.getKeyCode() == KeyEvent.VK_ENTER
  240. && (flags & HANDLE_RETURN) == HANDLE_RETURN) {
  241. target.setText("");
  242. }
  243. handleKeyPressed(line, target.getCaretPosition(), e.getKeyCode(),
  244. e.isShiftDown(), UIUtilities.isCtrlDown(e));
  245. }
  246. }
  247. @Override
  248. public void keyReleased(final KeyEvent e) {
  249. validateText();
  250. }
  251. }