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 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.ui_swing.components.inputfields;
  18. import com.dmdirc.addons.ui_swing.Apple;
  19. import com.dmdirc.addons.ui_swing.UIUtilities;
  20. import com.dmdirc.commandparser.parsers.CommandParser;
  21. import com.dmdirc.interfaces.CommandController;
  22. import com.dmdirc.events.eventbus.EventBus;
  23. import com.dmdirc.interfaces.WindowModel;
  24. import com.dmdirc.interfaces.ui.InputField;
  25. import com.dmdirc.plugins.ServiceManager;
  26. import com.dmdirc.ui.input.InputHandler;
  27. import com.dmdirc.ui.input.TabCompleterUtils;
  28. import java.awt.event.ActionEvent;
  29. import java.awt.event.InputEvent;
  30. import java.awt.event.KeyEvent;
  31. import java.awt.event.KeyListener;
  32. import javax.swing.AbstractAction;
  33. import javax.swing.JComponent;
  34. import javax.swing.JTextField;
  35. import javax.swing.KeyStroke;
  36. import javax.swing.text.JTextComponent;
  37. /**
  38. * Swing input handler.
  39. */
  40. public class SwingInputHandler extends InputHandler implements KeyListener {
  41. /**
  42. * Creates a new instance of InputHandler. Adds listeners to the target that we need to operate.
  43. *
  44. * @param serviceManager Manager to use to look up tab completion services.
  45. * @param target The text field this input handler is dealing with.
  46. * @param commandController The controller to use to retrieve command information.
  47. * @param commandParser The command parser to use for this text field.
  48. * @param parentWindow The window that owns this input handler
  49. * @param eventBus The event bus to use to dispatch input events.
  50. */
  51. public SwingInputHandler(
  52. final ServiceManager serviceManager,
  53. final InputField target,
  54. final CommandController commandController,
  55. final CommandParser commandParser,
  56. final WindowModel parentWindow,
  57. final TabCompleterUtils tabCompleterUtils,
  58. final EventBus eventBus) {
  59. super(serviceManager, target, commandController, commandParser, parentWindow,
  60. tabCompleterUtils, eventBus);
  61. }
  62. @Override
  63. protected void addUpHandler() {
  64. final JTextComponent localTarget;
  65. if (target instanceof JTextComponent) {
  66. localTarget = (JTextComponent) target;
  67. } else if (target instanceof SwingInputField) {
  68. localTarget = ((SwingInputField) target).getTextField();
  69. } else {
  70. throw new IllegalArgumentException("Unknown target");
  71. }
  72. localTarget.getActionMap().put("upArrow", new AbstractAction() {
  73. /** Serial version UID. */
  74. private static final long serialVersionUID = 1;
  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. @Override
  90. protected void addDownHandler() {
  91. final JTextComponent localTarget;
  92. if (target instanceof JTextComponent) {
  93. localTarget = (JTextComponent) target;
  94. } else if (target instanceof SwingInputField) {
  95. localTarget = ((SwingInputField) target).getTextField();
  96. } else {
  97. throw new IllegalArgumentException("Unknown target");
  98. }
  99. localTarget.getActionMap().put("downArrow", new AbstractAction() {
  100. /** Serial version UID. */
  101. private static final long serialVersionUID = 1;
  102. @Override
  103. public void actionPerformed(final ActionEvent e) {
  104. doBufferDown();
  105. }
  106. });
  107. if (Apple.isAppleUI()) {
  108. localTarget.getInputMap(JComponent.WHEN_FOCUSED).
  109. put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0),
  110. "downArrow");
  111. } else {
  112. localTarget.getInputMap(
  113. JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).
  114. put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0),
  115. "downArrow");
  116. }
  117. }
  118. @Override
  119. protected void addTabHandler() {
  120. final JTextComponent localTarget;
  121. if (target instanceof JTextComponent) {
  122. localTarget = (JTextComponent) target;
  123. } else if (target instanceof SwingInputField) {
  124. localTarget = ((SwingInputField) target).getTextField();
  125. } else {
  126. throw new IllegalArgumentException("Unknown target");
  127. }
  128. localTarget.getActionMap().put("insert-tab", new AbstractAction() {
  129. /** Serial version UID. */
  130. private static final long serialVersionUID = 1;
  131. @Override
  132. public void actionPerformed(final ActionEvent e) {
  133. localTarget.setEditable(false);
  134. UIUtilities.invokeOffEDT(() -> doTabCompletion(false),
  135. value -> localTarget.setEditable(true));
  136. }
  137. });
  138. localTarget.getActionMap().put("insert-shift-tab",
  139. new AbstractAction() {
  140. /** Serial version UID. */
  141. private static final long serialVersionUID = 1;
  142. @Override
  143. public void actionPerformed(final ActionEvent e) {
  144. localTarget.setEditable(false);
  145. UIUtilities.invokeOffEDT(() -> doTabCompletion(true),
  146. value -> localTarget.setEditable(true));
  147. }
  148. });
  149. localTarget.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).
  150. put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), "insert-tab");
  151. localTarget.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).
  152. put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_MASK),
  153. "insert-shift-tab");
  154. }
  155. @Override
  156. protected void addEnterHandler() {
  157. final JTextComponent localTarget;
  158. if (target instanceof JTextComponent) {
  159. localTarget = (JTextComponent) target;
  160. } else if (target instanceof SwingInputField) {
  161. localTarget = ((SwingInputField) target).getTextField();
  162. } else {
  163. throw new IllegalArgumentException("Unknown target");
  164. }
  165. localTarget.getActionMap().put("enterButton", new AbstractAction() {
  166. /** Serial version UID. */
  167. private static final long serialVersionUID = 1;
  168. @Override
  169. public void actionPerformed(final ActionEvent e) {
  170. final String line = target.getText();
  171. target.setText("");
  172. UIUtilities.invokeLater(() -> {
  173. final JTextField source;
  174. if (e.getSource() instanceof SwingInputField) {
  175. source = ((SwingInputField) e.getSource())
  176. .getTextField();
  177. } else if (e.getSource() instanceof JTextField) {
  178. source = (JTextField) e.getSource();
  179. } else {
  180. throw new IllegalArgumentException(
  181. "Event is not from known source.");
  182. }
  183. if (source.isEditable()) {
  184. UIUtilities.invokeOffEDT(() -> enterPressed(line));
  185. }
  186. });
  187. }
  188. });
  189. localTarget.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).
  190. put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
  191. "enterButton");
  192. }
  193. @Override
  194. protected void addKeyHandler() {
  195. target.addKeyListener(this);
  196. }
  197. @Override
  198. public void keyTyped(final KeyEvent e) {
  199. //Ignore
  200. }
  201. @Override
  202. public void keyPressed(final KeyEvent e) {
  203. if (e.getKeyCode() != KeyEvent.VK_TAB && e.getKeyCode()
  204. != KeyEvent.VK_UP && e.getKeyCode() != KeyEvent.VK_DOWN) {
  205. final String line = target.getText();
  206. if (UIUtilities.isCtrlDown(e) && e.getKeyCode() == KeyEvent.VK_ENTER
  207. && (flags & HANDLE_RETURN) == HANDLE_RETURN) {
  208. target.setText("");
  209. }
  210. handleKeyPressed(line, target.getCaretPosition(), e.getKeyCode(),
  211. e.isShiftDown(), UIUtilities.isCtrlDown(e));
  212. }
  213. }
  214. @Override
  215. public void keyReleased(final KeyEvent e) {
  216. validateText();
  217. }
  218. }