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.

SwingInputField.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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.addons.ui_swing.components.colours.ColourPickerDialog;
  24. import com.dmdirc.ui.IconManager;
  25. import com.dmdirc.config.IdentityManager;
  26. import com.dmdirc.ui.interfaces.InputField;
  27. import com.dmdirc.ui.interfaces.InputValidationListener;
  28. import com.dmdirc.addons.ui_swing.UIUtilities;
  29. import com.dmdirc.util.ListenerList;
  30. import com.dmdirc.util.ReturnableThread;
  31. import java.awt.Color;
  32. import java.awt.Window;
  33. import java.awt.event.ActionEvent;
  34. import java.awt.event.ActionListener;
  35. import java.awt.event.KeyEvent;
  36. import java.awt.event.KeyListener;
  37. import javax.swing.JComponent;
  38. import javax.swing.JLabel;
  39. import javax.swing.JTextField;
  40. import javax.swing.text.BadLocationException;
  41. import net.miginfocom.swing.MigLayout;
  42. /** Swing input field. */
  43. public class SwingInputField extends JComponent implements InputField,
  44. KeyListener, InputValidationListener {
  45. /**
  46. * A version number for this class. It should be changed whenever the class
  47. * structure is changed (or anything else that would prevent serialized
  48. * objects being unserialized with the new class).
  49. */
  50. private static final long serialVersionUID = 1;
  51. /** Colour picker. */
  52. private ColourPickerDialog colourPicker;
  53. /** Input field text field. */
  54. private final JTextField textField;
  55. /** Line wrap indicator. */
  56. private final JLabel wrapIndicator;
  57. /** Error indicator. */
  58. private final JLabel errorIndicator;
  59. /** Listener list. */
  60. private final ListenerList listeners;
  61. /** Parent Window. */
  62. private final Window parentWindow;
  63. /**
  64. * Instantiates a new swing input field.
  65. *
  66. * @param parentWindow Parent window
  67. */
  68. public SwingInputField(final Window parentWindow) {
  69. super();
  70. this.parentWindow = parentWindow;
  71. listeners = new ListenerList();
  72. textField = new JTextField();
  73. textField.setFocusTraversalKeysEnabled(false);
  74. textField.addKeyListener(this);
  75. textField.setOpaque(true);
  76. wrapIndicator =
  77. new JLabel(IconManager.getIconManager().getIcon("linewrap"));
  78. wrapIndicator.setVisible(false);
  79. errorIndicator =
  80. new JLabel(IconManager.getIconManager().getIcon("input-error"));
  81. errorIndicator.setVisible(false);
  82. setLayout(new MigLayout("ins 0, hidemode 3"));
  83. add(textField, "growx, pushx");
  84. add(wrapIndicator, "");
  85. add(errorIndicator, "");
  86. setActionMap(textField.getActionMap());
  87. setInputMap(SwingInputField.WHEN_FOCUSED,
  88. textField.getInputMap(SwingInputField.WHEN_FOCUSED));
  89. setInputMap(SwingInputField.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
  90. textField.getInputMap(SwingInputField.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT));
  91. setInputMap(SwingInputField.WHEN_IN_FOCUSED_WINDOW,
  92. textField.getInputMap(SwingInputField.WHEN_IN_FOCUSED_WINDOW));
  93. }
  94. /** {@inheritDoc} */
  95. @Override
  96. public void requestFocus() {
  97. UIUtilities.invokeLater(new Runnable() {
  98. /** {@inheritDoc} */
  99. @Override
  100. public void run() {
  101. textField.requestFocus();
  102. }
  103. });
  104. }
  105. /** {@inheritDoc} */
  106. @Override
  107. public boolean requestFocusInWindow() {
  108. return UIUtilities.invokeAndWait(new ReturnableThread<Boolean>() {
  109. /** {@inheritDoc} */
  110. @Override
  111. public void run() {
  112. setObject(textField.requestFocusInWindow());
  113. }
  114. });
  115. }
  116. /** {@inheritDoc} */
  117. @Override
  118. public void showColourPicker(final boolean irc, final boolean hex) {
  119. UIUtilities.invokeLater(new Runnable() {
  120. /** {@inheritDoc} */
  121. @Override
  122. public void run() {
  123. if (IdentityManager.getGlobalConfig().getOptionBool("general",
  124. "showcolourdialog")) {
  125. colourPicker = new ColourPickerDialog(irc, hex, parentWindow);
  126. colourPicker.addActionListener(new ActionListener() {
  127. @Override
  128. public void actionPerformed(final ActionEvent actionEvent) {
  129. try {
  130. textField.getDocument().
  131. insertString(textField.getCaretPosition(),
  132. actionEvent.getActionCommand(), null);
  133. } catch (BadLocationException ex) {
  134. //Ignore, wont happen
  135. }
  136. colourPicker.dispose();
  137. colourPicker = null;
  138. }
  139. });
  140. colourPicker.display();
  141. colourPicker.setLocation((int) textField.getLocationOnScreen().
  142. getX(),
  143. (int) textField.getLocationOnScreen().getY() -
  144. colourPicker.getHeight());
  145. }
  146. }
  147. });
  148. }
  149. /** {@inheritDoc} */
  150. @Override
  151. public void hideColourPicker() {
  152. UIUtilities.invokeLater(new Runnable() {
  153. /** {@inheritDoc} */
  154. @Override
  155. public void run() {
  156. if (colourPicker != null) {
  157. colourPicker.dispose();
  158. colourPicker = null;
  159. }
  160. }
  161. });
  162. }
  163. /**
  164. * Returns the textfield for this inputfield.
  165. *
  166. * @return JTextField
  167. */
  168. public JTextField getTextField() {
  169. return UIUtilities.invokeAndWait(new ReturnableThread<JTextField>() {
  170. /** {@inheritDoc} */
  171. @Override
  172. public void run() {
  173. setObject(textField);
  174. }
  175. });
  176. }
  177. /** {@inheritDoc} */
  178. @Override
  179. public void addActionListener(final ActionListener listener) {
  180. UIUtilities.invokeLater(new Runnable() {
  181. /** {@inheritDoc} */
  182. @Override
  183. public void run() {
  184. textField.addActionListener(listener);
  185. }
  186. });
  187. }
  188. /** {@inheritDoc} */
  189. @Override
  190. public void addKeyListener(final KeyListener listener) {
  191. UIUtilities.invokeLater(new Runnable() {
  192. /** {@inheritDoc} */
  193. @Override
  194. public void run() {
  195. listeners.add(KeyListener.class, listener);
  196. }
  197. });
  198. }
  199. /** {@inheritDoc} */
  200. @Override
  201. public void removeActionListener(final ActionListener listener) {
  202. UIUtilities.invokeLater(new Runnable() {
  203. /** {@inheritDoc} */
  204. @Override
  205. public void run() {
  206. textField.removeActionListener(listener);
  207. }
  208. });
  209. }
  210. /** {@inheritDoc} */
  211. @Override
  212. public void removeKeyListener(final KeyListener listener) {
  213. UIUtilities.invokeLater(new Runnable() {
  214. /** {@inheritDoc} */
  215. @Override
  216. public void run() {
  217. listeners.remove(KeyListener.class, listener);
  218. }
  219. });
  220. }
  221. /** {@inheritDoc} */
  222. @Override
  223. public String getSelectedText() {
  224. return UIUtilities.invokeAndWait(new ReturnableThread<String>() {
  225. /** {@inheritDoc} */
  226. @Override
  227. public void run() {
  228. setObject(textField.getSelectedText());
  229. }
  230. });
  231. }
  232. /** {@inheritDoc} */
  233. @Override
  234. public int getSelectionEnd() {
  235. return UIUtilities.invokeAndWait(new ReturnableThread<Integer>() {
  236. /** {@inheritDoc} */
  237. @Override
  238. public void run() {
  239. setObject(textField.getSelectionEnd());
  240. }
  241. });
  242. }
  243. /** {@inheritDoc} */
  244. @Override
  245. public int getSelectionStart() {
  246. return UIUtilities.invokeAndWait(new ReturnableThread<Integer>() {
  247. /** {@inheritDoc} */
  248. @Override
  249. public void run() {
  250. setObject(textField.getSelectionStart());
  251. }
  252. });
  253. }
  254. /** {@inheritDoc} */
  255. @Override
  256. public String getText() {
  257. return UIUtilities.invokeAndWait(new ReturnableThread<String>() {
  258. /** {@inheritDoc} */
  259. @Override
  260. public void run() {
  261. setObject(textField.getText());
  262. }
  263. });
  264. }
  265. /** {@inheritDoc} */
  266. @Override
  267. public void setText(final String text) {
  268. UIUtilities.invokeLater(new Runnable() {
  269. /** {@inheritDoc} */
  270. @Override
  271. public void run() {
  272. textField.setText(text);
  273. }
  274. });
  275. }
  276. /** {@inheritDoc} */
  277. @Override
  278. public int getCaretPosition() {
  279. return UIUtilities.invokeAndWait(new ReturnableThread<Integer>() {
  280. /** {@inheritDoc} */
  281. @Override
  282. public void run() {
  283. setObject(textField.getCaretPosition());
  284. }
  285. });
  286. }
  287. /** {@inheritDoc} */
  288. @Override
  289. public void setCaretPosition(final int position) {
  290. UIUtilities.invokeLater(new Runnable() {
  291. /** {@inheritDoc} */
  292. @Override
  293. public void run() {
  294. textField.setCaretPosition(position);
  295. }
  296. });
  297. }
  298. /**
  299. * Replaces the selection with the specified text.
  300. *
  301. * @param clipboard Text to replace selection with
  302. */
  303. public void replaceSelection(final String clipboard) {
  304. UIUtilities.invokeLater(new Runnable() {
  305. /** {@inheritDoc} */
  306. @Override
  307. public void run() {
  308. textField.replaceSelection(clipboard);
  309. }
  310. });
  311. }
  312. /**
  313. * Sets the caret colour to the specified coloour.
  314. *
  315. * @param optionColour Colour for the caret
  316. */
  317. public void setCaretColor(final Color optionColour) {
  318. UIUtilities.invokeLater(new Runnable() {
  319. /** {@inheritDoc} */
  320. @Override
  321. public void run() {
  322. textField.setCaretColor(optionColour);
  323. }
  324. });
  325. }
  326. /**
  327. * Sets the foreground colour to the specified coloour.
  328. *
  329. * @param optionColour Colour for the caret
  330. */
  331. @Override
  332. public void setForeground(final Color optionColour) {
  333. UIUtilities.invokeLater(new Runnable() {
  334. /** {@inheritDoc} */
  335. @Override
  336. public void run() {
  337. textField.setForeground(optionColour);
  338. }
  339. });
  340. }
  341. /**
  342. * Sets the background colour to the specified coloour.
  343. *
  344. * @param optionColour Colour for the caret
  345. */
  346. @Override
  347. public void setBackground(final Color optionColour) {
  348. UIUtilities.invokeLater(new Runnable() {
  349. /** {@inheritDoc} */
  350. @Override
  351. public void run() {
  352. textField.setBackground(optionColour);
  353. }
  354. });
  355. }
  356. /** {@inheritDoc} */
  357. @Override
  358. public boolean hasFocus() {
  359. return UIUtilities.invokeAndWait(new ReturnableThread<Boolean>() {
  360. /** {@inheritDoc} */
  361. @Override
  362. public void run() {
  363. setObject(textField.hasFocus());
  364. }
  365. });
  366. }
  367. /** {@inheritDoc} */
  368. @Override
  369. public boolean isFocusOwner() {
  370. return UIUtilities.invokeAndWait(new ReturnableThread<Boolean>() {
  371. /** {@inheritDoc} */
  372. @Override
  373. public void run() {
  374. setObject(textField.isFocusOwner());
  375. }
  376. });
  377. }
  378. /**
  379. * {@inheritDoc}
  380. *
  381. * @param e Key event
  382. */
  383. @Override
  384. public void keyTyped(final KeyEvent e) {
  385. for (KeyListener listener : listeners.get(KeyListener.class)) {
  386. listener.keyTyped(e);
  387. }
  388. }
  389. /**
  390. * {@inheritDoc}
  391. *
  392. * @param e Key event
  393. */
  394. @Override
  395. public void keyPressed(final KeyEvent e) {
  396. for (KeyListener listener : listeners.get(KeyListener.class)) {
  397. listener.keyPressed(e);
  398. }
  399. }
  400. /**
  401. * {@inheritDoc}
  402. *
  403. * @param e Key event
  404. */
  405. @Override
  406. public void keyReleased(final KeyEvent e) {
  407. for (KeyListener listener : listeners.get(KeyListener.class)) {
  408. listener.keyReleased(e);
  409. }
  410. }
  411. /** {@inheritDoc} */
  412. @Override
  413. public void illegalCommand(final String reason) {
  414. UIUtilities.invokeLater(new Runnable() {
  415. /** {@inheritDoc} */
  416. @Override
  417. public void run() {
  418. errorIndicator.setVisible(true);
  419. errorIndicator.setToolTipText(reason);
  420. wrapIndicator.setVisible(false);
  421. }
  422. });
  423. }
  424. /** {@inheritDoc} */
  425. @Override
  426. public void legalCommand() {
  427. UIUtilities.invokeLater(new Runnable() {
  428. /** {@inheritDoc} */
  429. @Override
  430. public void run() {
  431. errorIndicator.setVisible(false);
  432. errorIndicator.setToolTipText(null);
  433. }
  434. });
  435. }
  436. /** {@inheritDoc} */
  437. @Override
  438. public void wrappedText(final int count) {
  439. UIUtilities.invokeLater(new Runnable() {
  440. /** {@inheritDoc} */
  441. @Override
  442. public void run() {
  443. wrapIndicator.setVisible(count > 1);
  444. wrapIndicator.setToolTipText(count + " lines");
  445. errorIndicator.setVisible(false);
  446. }
  447. });
  448. }
  449. }