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

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