Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

SwingInputHandler.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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;
  23. import com.dmdirc.commandparser.parsers.CommandParser;
  24. import com.dmdirc.ui.input.InputHandler;
  25. import com.dmdirc.ui.interfaces.InputField;
  26. import com.dmdirc.ui.interfaces.InputWindow;
  27. import com.dmdirc.addons.ui_swing.Apple;
  28. import com.dmdirc.addons.ui_swing.UIUtilities;
  29. import java.awt.event.ActionEvent;
  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.SwingUtilities;
  37. import javax.swing.text.JTextComponent;
  38. /**
  39. * Swing input handler.
  40. */
  41. public class SwingInputHandler extends InputHandler implements KeyListener {
  42. /**
  43. * Creates a new instance of InputHandler. Adds listeners to the target
  44. * that we need to operate.
  45. *
  46. * @param target The text field this input handler is dealing with.
  47. * @param commandParser The command parser to use for this text field.
  48. * @param parentWindow The window that owns this input handler
  49. */
  50. public SwingInputHandler(final InputField target,
  51. final CommandParser commandParser, final InputWindow parentWindow) {
  52. super(target, commandParser, parentWindow);
  53. }
  54. /** {@inheritDoc} */
  55. @Override
  56. protected void addUpHandler() {
  57. JTextComponent localTarget = null;
  58. if (target instanceof JTextComponent) {
  59. localTarget = (JTextComponent) target;
  60. } else if (target instanceof SwingInputField) {
  61. localTarget = ((SwingInputField) target).getTextField();
  62. }
  63. localTarget.getActionMap().put("upArrow", new AbstractAction() {
  64. /**
  65. * A version number for this class. It should be changed whenever the class
  66. * structure is changed (or anything else that would prevent serialized
  67. * objects being unserialized with the new class).
  68. */
  69. private static final long serialVersionUID = 1;
  70. /** {@inheritDoc} */
  71. @Override
  72. public void actionPerformed(ActionEvent e) {
  73. doBufferUp();
  74. }
  75. });
  76. if (Apple.isAppleUI()) {
  77. localTarget.getInputMap(JComponent.WHEN_FOCUSED).
  78. put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "upArrow");
  79. } else {
  80. localTarget.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).
  81. put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "upArrow");
  82. }
  83. }
  84. /** {@inheritDoc} */
  85. @Override
  86. protected void addDownHandler() {
  87. JTextComponent localTarget = null;
  88. if (target instanceof JTextComponent) {
  89. localTarget = (JTextComponent) target;
  90. } else if (target instanceof SwingInputField) {
  91. localTarget = ((SwingInputField) target).getTextField();
  92. }
  93. localTarget.getActionMap().put("downArrow", new AbstractAction() {
  94. /**
  95. * A version number for this class. It should be changed whenever the class
  96. * structure is changed (or anything else that would prevent serialized
  97. * objects being unserialized with the new class).
  98. */
  99. private static final long serialVersionUID = 1;
  100. /** {@inheritDoc} */
  101. @Override
  102. public void actionPerformed(ActionEvent e) {
  103. doBufferDown();
  104. }
  105. });
  106. if (Apple.isAppleUI()) {
  107. localTarget.getInputMap(JComponent.WHEN_FOCUSED).
  108. put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "downArrow");
  109. } else {
  110. localTarget.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).
  111. put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "downArrow");
  112. }
  113. }
  114. /** {@inheritDoc} */
  115. @Override
  116. protected void addTabHandler() {
  117. final JTextComponent localTarget;
  118. if (target instanceof JTextComponent) {
  119. localTarget = (JTextComponent) target;
  120. } else if (target instanceof SwingInputField) {
  121. localTarget = ((SwingInputField) target).getTextField();
  122. } else {
  123. throw new IllegalArgumentException("Unknown target");
  124. }
  125. localTarget.getActionMap().put("insert-tab", new AbstractAction() {
  126. /**
  127. * A version number for this class. It should be changed whenever the class
  128. * structure is changed (or anything else that would prevent serialized
  129. * objects being unserialized with the new class).
  130. */
  131. private static final long serialVersionUID = 1;
  132. /** {@inheritDoc} */
  133. @Override
  134. public void actionPerformed(final ActionEvent e) {
  135. new LoggingSwingWorker() {
  136. /** {@inheritDoc} */
  137. @Override
  138. protected Object doInBackground() throws Exception {
  139. localTarget.setEditable(false);
  140. doTabCompletion();
  141. return null;
  142. }
  143. /** {@inheritDoc} */
  144. @Override
  145. protected void done() {
  146. localTarget.setEditable(true);
  147. }
  148. }.execute();
  149. }
  150. });
  151. localTarget.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).
  152. put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), "insert-tab");
  153. }
  154. /** {@inheritDoc} */
  155. @Override
  156. protected void addEnterHandler() {
  157. JTextComponent localTarget = null;
  158. if (target instanceof JTextComponent) {
  159. localTarget = (JTextComponent) target;
  160. } else if (target instanceof SwingInputField) {
  161. localTarget = ((SwingInputField) target).getTextField();
  162. }
  163. localTarget.getActionMap().put("enterButton", new AbstractAction() {
  164. /**
  165. * A version number for this class. It should be changed whenever the class
  166. * structure is changed (or anything else that would prevent serialized
  167. * objects being unserialized with the new class).
  168. */
  169. private static final long serialVersionUID = 1;
  170. /** {@inheritDoc} */
  171. @Override
  172. public void actionPerformed(final ActionEvent e) {
  173. final String line = target.getText();
  174. target.setText("");
  175. new LoggingSwingWorker() {
  176. /** {@inheritDoc} */
  177. @Override
  178. protected Object doInBackground() throws Exception {
  179. if (((JTextField) e.getSource()).isEditable()) {
  180. enterPressed(line);
  181. }
  182. return null;
  183. }
  184. }.execute();
  185. }
  186. });
  187. localTarget.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).
  188. put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enterButton");
  189. }
  190. /** {@inheritDoc} */
  191. @Override
  192. protected void addKeyHandler() {
  193. target.addKeyListener(this);
  194. }
  195. /**
  196. * {@inheritDoc}
  197. *
  198. * @param e Key event
  199. */
  200. @Override
  201. public void keyTyped(final KeyEvent e) {
  202. //Ignore
  203. }
  204. /**
  205. * {@inheritDoc}
  206. *
  207. * @param e Key event
  208. */
  209. @Override
  210. public void keyPressed(final KeyEvent e) {
  211. if (e.getKeyCode() != KeyEvent.VK_TAB && e.getKeyCode() !=
  212. KeyEvent.VK_UP && e.getKeyCode() != KeyEvent.VK_DOWN) {
  213. final String line = target.getText();
  214. if (UIUtilities.isCtrlDown(e) && e.getKeyCode() == KeyEvent.VK_ENTER
  215. && (flags & HANDLE_RETURN) == HANDLE_RETURN) {
  216. target.setText("");
  217. }
  218. SwingUtilities.invokeLater(new Runnable() {
  219. /** {@inheritDoc} */
  220. @Override
  221. public void run() {
  222. handleKeyPressed(line, e.getKeyCode(), e.isShiftDown(),
  223. UIUtilities.isCtrlDown(e));
  224. }
  225. });
  226. }
  227. }
  228. /**
  229. * {@inheritDoc}
  230. *
  231. * @param e Key event
  232. */
  233. @Override
  234. public void keyReleased(final KeyEvent e) {
  235. //Ignore
  236. }
  237. }