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

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