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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. JTextComponent localTarget = null;
  118. if (target instanceof JTextComponent) {
  119. localTarget = (JTextComponent) target;
  120. } else if (target instanceof SwingInputField) {
  121. localTarget = ((SwingInputField) target).getTextField();
  122. }
  123. localTarget.getActionMap().put("tabPressed", new AbstractAction() {
  124. /**
  125. * A version number for this class. It should be changed whenever the class
  126. * structure is changed (or anything else that would prevent serialized
  127. * objects being unserialized with the new class).
  128. */
  129. private static final long serialVersionUID = 1;
  130. /** {@inheritDoc} */
  131. @Override
  132. public void actionPerformed(final ActionEvent e) {
  133. new LoggingSwingWorker() {
  134. /** {@inheritDoc} */
  135. @Override
  136. protected Object doInBackground() throws Exception {
  137. ((JTextField) e.getSource()).setEditable(false);
  138. doTabCompletion();
  139. return null;
  140. }
  141. /** {@inheritDoc} */
  142. @Override
  143. protected void done() {
  144. ((JTextField) e.getSource()).setEditable(true);
  145. }
  146. }.execute();
  147. }
  148. });
  149. localTarget.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).
  150. put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), "tabPressed");
  151. }
  152. /** {@inheritDoc} */
  153. @Override
  154. protected void addEnterHandler() {
  155. JTextComponent localTarget = null;
  156. if (target instanceof JTextComponent) {
  157. localTarget = (JTextComponent) target;
  158. } else if (target instanceof SwingInputField) {
  159. localTarget = ((SwingInputField) target).getTextField();
  160. }
  161. localTarget.getActionMap().put("enterButton", new AbstractAction() {
  162. /**
  163. * A version number for this class. It should be changed whenever the class
  164. * structure is changed (or anything else that would prevent serialized
  165. * objects being unserialized with the new class).
  166. */
  167. private static final long serialVersionUID = 1;
  168. /** {@inheritDoc} */
  169. @Override
  170. public void actionPerformed(final ActionEvent e) {
  171. final String line = target.getText();
  172. target.setText("");
  173. new LoggingSwingWorker() {
  174. /** {@inheritDoc} */
  175. @Override
  176. protected Object doInBackground() throws Exception {
  177. if (((JTextField) e.getSource()).isEditable()) {
  178. enterPressed(line);
  179. }
  180. return null;
  181. }
  182. }.execute();
  183. }
  184. });
  185. localTarget.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).
  186. put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enterButton");
  187. }
  188. /** {@inheritDoc} */
  189. @Override
  190. protected void addKeyHandler() {
  191. target.addKeyListener(this);
  192. }
  193. /**
  194. * {@inheritDoc}
  195. *
  196. * @param e Key event
  197. */
  198. @Override
  199. public void keyTyped(final KeyEvent e) {
  200. //Ignore
  201. }
  202. /**
  203. * {@inheritDoc}
  204. *
  205. * @param e Key event
  206. */
  207. @Override
  208. public void keyPressed(final KeyEvent e) {
  209. if (e.getKeyCode() != KeyEvent.VK_TAB && e.getKeyCode() !=
  210. KeyEvent.VK_UP && e.getKeyCode() != KeyEvent.VK_DOWN) {
  211. final String line = target.getText();
  212. if (UIUtilities.isCtrlDown(e) && e.getKeyCode() == KeyEvent.VK_ENTER
  213. && (flags & HANDLE_RETURN) == HANDLE_RETURN) {
  214. target.setText("");
  215. }
  216. SwingUtilities.invokeLater(new Runnable() {
  217. /** {@inheritDoc} */
  218. @Override
  219. public void run() {
  220. handleKeyPressed(line, e.getKeyCode(), e.isShiftDown(),
  221. UIUtilities.isCtrlDown(e));
  222. }
  223. });
  224. }
  225. }
  226. /**
  227. * {@inheritDoc}
  228. *
  229. * @param e Key event
  230. */
  231. @Override
  232. public void keyReleased(final KeyEvent e) {
  233. //Ignore
  234. }
  235. }