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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.ui_swing.components.inputfields;
  18. import com.dmdirc.addons.ui_swing.UIUtilities;
  19. import com.dmdirc.addons.ui_swing.components.IconManager;
  20. import com.dmdirc.addons.ui_swing.components.colours.ColourPickerDialog;
  21. import com.dmdirc.addons.ui_swing.injection.MainWindow;
  22. import com.dmdirc.config.GlobalConfig;
  23. import com.dmdirc.config.provider.AggregateConfigProvider;
  24. import com.dmdirc.interfaces.ui.InputField;
  25. import com.dmdirc.interfaces.ui.InputValidationListener;
  26. import com.dmdirc.ui.messages.ColourManager;
  27. import com.dmdirc.util.collections.ListenerList;
  28. import java.awt.Color;
  29. import java.awt.FontMetrics;
  30. import java.awt.KeyboardFocusManager;
  31. import java.awt.Window;
  32. import java.awt.event.ActionListener;
  33. import java.awt.event.KeyEvent;
  34. import java.awt.event.KeyListener;
  35. import java.beans.PropertyChangeEvent;
  36. import java.beans.PropertyChangeListener;
  37. import java.util.concurrent.Callable;
  38. import javax.inject.Inject;
  39. import javax.swing.JComponent;
  40. import javax.swing.JLabel;
  41. import javax.swing.JTextField;
  42. import javax.swing.text.BadLocationException;
  43. import net.miginfocom.layout.PlatformDefaults;
  44. import net.miginfocom.swing.MigLayout;
  45. /** Swing input field. */
  46. public class SwingInputField extends JComponent implements InputField,
  47. KeyListener, InputValidationListener, PropertyChangeListener {
  48. /** Serial version UID. */
  49. private static final long serialVersionUID = 1;
  50. /** Colour picker. */
  51. private ColourPickerDialog colourPicker;
  52. /** Input field text field. */
  53. private final JTextField textField;
  54. /** Line wrap indicator. */
  55. private final JLabel wrapIndicator;
  56. /** Error indicator. */
  57. private final JLabel errorIndicator;
  58. /** Listener list. */
  59. private final ListenerList listeners;
  60. /** Parent Window. */
  61. private final Window parentWindow;
  62. /** The config to read settings from. */
  63. private final AggregateConfigProvider globalConfig;
  64. /** The icon manager to use for icons. */
  65. private final IconManager iconManager;
  66. /** The colour manager to use. */
  67. private final ColourManager colourManager;
  68. /**
  69. * Instantiates a new swing input field.
  70. *
  71. * @param mainWindow Main window, to use as a parent for dialogs.
  72. * @param globalConfig The configuration to read settings from.
  73. * @param iconManager The icon manager to use for icons.
  74. * @param colourManager The colour manager to use.
  75. */
  76. @Inject
  77. public SwingInputField(
  78. @MainWindow final Window mainWindow,
  79. @GlobalConfig final AggregateConfigProvider globalConfig,
  80. final IconManager iconManager,
  81. @GlobalConfig final ColourManager colourManager) {
  82. // TODO: Use ColourManagerFactory and pass in the parent container
  83. this.parentWindow = mainWindow;
  84. this.globalConfig = globalConfig;
  85. this.iconManager = iconManager;
  86. this.colourManager = colourManager;
  87. listeners = new ListenerList();
  88. textField = new JTextField();
  89. textField.setFocusTraversalKeysEnabled(false);
  90. textField.addKeyListener(this);
  91. textField.setOpaque(true);
  92. wrapIndicator = new JLabel(iconManager.getIcon("linewrap"));
  93. wrapIndicator.setVisible(false);
  94. errorIndicator = new JLabel(iconManager.getIcon("input-error"));
  95. errorIndicator.setVisible(false);
  96. setLayout(new MigLayout("ins 0, hidemode 3"));
  97. final FontMetrics fm = textField.getFontMetrics(textField.getFont());
  98. add(textField, "grow, push, hmin " + (fm.getMaxAscent() + fm.
  99. getMaxDescent() + PlatformDefaults.getPanelInsets(0).getValue()));
  100. add(wrapIndicator, "");
  101. add(errorIndicator, "");
  102. setActionMap(textField.getActionMap());
  103. setInputMap(SwingInputField.WHEN_FOCUSED,
  104. textField.getInputMap(SwingInputField.WHEN_FOCUSED));
  105. setInputMap(SwingInputField.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
  106. textField.getInputMap(SwingInputField.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT));
  107. setInputMap(SwingInputField.WHEN_IN_FOCUSED_WINDOW,
  108. textField.getInputMap(SwingInputField.WHEN_IN_FOCUSED_WINDOW));
  109. KeyboardFocusManager.getCurrentKeyboardFocusManager()
  110. .addPropertyChangeListener(this);
  111. }
  112. @Override
  113. public void requestFocus() {
  114. UIUtilities.invokeLater(textField::requestFocus);
  115. }
  116. @Override
  117. public boolean requestFocusInWindow() {
  118. return UIUtilities.invokeAndWait((Callable<Boolean>) textField::requestFocusInWindow);
  119. }
  120. @Override
  121. public void showColourPicker(final boolean irc, final boolean hex) {
  122. UIUtilities.invokeLater(() -> {
  123. if (globalConfig.getOptionBool("general", "showcolourdialog")) {
  124. colourPicker = new ColourPickerDialog(SwingInputField.this,
  125. colourManager, iconManager, irc, hex, parentWindow);
  126. colourPicker.addActionListener(actionEvent -> {
  127. try {
  128. textField.getDocument().
  129. insertString(textField.getCaretPosition(),
  130. actionEvent.getActionCommand(), null);
  131. } catch (final BadLocationException ex) {
  132. //Ignore, wont happen
  133. }
  134. colourPicker.dispose();
  135. colourPicker = null;
  136. });
  137. colourPicker.setVisible(true);
  138. colourPicker.setLocation((int) textField.getLocationOnScreen().
  139. getX(),
  140. (int) textField.getLocationOnScreen().getY() - colourPicker.getHeight());
  141. }
  142. });
  143. }
  144. @Override
  145. public void hideColourPicker() {
  146. UIUtilities.invokeLater(() -> {
  147. if (colourPicker != null) {
  148. colourPicker.dispose();
  149. colourPicker = null;
  150. }
  151. });
  152. }
  153. /**
  154. * Returns the textfield for this inputfield.
  155. *
  156. * @return JTextField
  157. */
  158. public JTextField getTextField() {
  159. return UIUtilities.invokeAndWait(() -> textField);
  160. }
  161. @Override
  162. public void addActionListener(final ActionListener listener) {
  163. UIUtilities.invokeLater(() -> textField.addActionListener(listener));
  164. }
  165. @Override
  166. public void addKeyListener(final KeyListener listener) {
  167. UIUtilities.invokeLater(() -> listeners.add(KeyListener.class, listener));
  168. }
  169. @Override
  170. public void removeActionListener(final ActionListener listener) {
  171. UIUtilities.invokeLater(() -> textField.removeActionListener(listener));
  172. }
  173. @Override
  174. public void removeKeyListener(final KeyListener listener) {
  175. UIUtilities.invokeLater(() -> listeners.remove(KeyListener.class, listener));
  176. }
  177. @Override
  178. public String getSelectedText() {
  179. return UIUtilities.invokeAndWait(textField::getSelectedText);
  180. }
  181. @Override
  182. public int getSelectionEnd() {
  183. return UIUtilities.invokeAndWait(textField::getSelectionEnd);
  184. }
  185. @Override
  186. public int getSelectionStart() {
  187. return UIUtilities.invokeAndWait(textField::getSelectionStart);
  188. }
  189. @Override
  190. public String getText() {
  191. return UIUtilities.invokeAndWait((Callable<String>) textField::getText);
  192. }
  193. @Override
  194. public void setText(final String text) {
  195. UIUtilities.invokeLater(() -> textField.setText(text));
  196. }
  197. @Override
  198. public int getCaretPosition() {
  199. return UIUtilities.invokeAndWait(textField::getCaretPosition);
  200. }
  201. @Override
  202. public void setCaretPosition(final int position) {
  203. UIUtilities.invokeLater(() -> textField.setCaretPosition(position));
  204. }
  205. /**
  206. * Replaces the selection with the specified text.
  207. *
  208. * @param clipboard Text to replace selection with
  209. */
  210. public void replaceSelection(final String clipboard) {
  211. UIUtilities.invokeLater(() -> textField.replaceSelection(clipboard));
  212. }
  213. /**
  214. * Sets the caret colour to the specified coloour.
  215. *
  216. * @param optionColour Colour for the caret
  217. */
  218. public void setCaretColor(final Color optionColour) {
  219. UIUtilities.invokeLater(() -> textField.setCaretColor(optionColour));
  220. }
  221. /**
  222. * Sets the foreground colour to the specified coloour.
  223. *
  224. * @param optionColour Colour for the caret
  225. */
  226. @Override
  227. public void setForeground(final Color optionColour) {
  228. UIUtilities.invokeLater(() -> textField.setForeground(optionColour));
  229. }
  230. /**
  231. * Sets the background colour to the specified coloour.
  232. *
  233. * @param optionColour Colour for the caret
  234. */
  235. @Override
  236. public void setBackground(final Color optionColour) {
  237. UIUtilities.invokeLater(() -> textField.setBackground(optionColour));
  238. }
  239. @Override
  240. public boolean hasFocus() {
  241. return UIUtilities.invokeAndWait(textField::hasFocus);
  242. }
  243. @Override
  244. public boolean isFocusOwner() {
  245. return UIUtilities.invokeAndWait(textField::isFocusOwner);
  246. }
  247. /**
  248. * Sets the start index of the selection for this component.
  249. *
  250. * @param selectionStart Start index
  251. */
  252. public void setSelectionStart(final int selectionStart) {
  253. UIUtilities.invokeLater(() -> textField.setSelectionStart(selectionStart));
  254. }
  255. /**
  256. * Sets the end index of the selection for this component.
  257. *
  258. * @param selectionEnd End index
  259. */
  260. public void setSelectionEnd(final int selectionEnd) {
  261. UIUtilities.invokeLater(() -> textField.setSelectionEnd(selectionEnd));
  262. }
  263. @Override
  264. public void keyTyped(final KeyEvent e) {
  265. for (final KeyListener listener : listeners.get(KeyListener.class)) {
  266. listener.keyTyped(e);
  267. }
  268. }
  269. @Override
  270. public void keyPressed(final KeyEvent e) {
  271. for (final KeyListener listener : listeners.get(KeyListener.class)) {
  272. listener.keyPressed(e);
  273. }
  274. }
  275. @Override
  276. public void keyReleased(final KeyEvent e) {
  277. for (final KeyListener listener : listeners.get(KeyListener.class)) {
  278. listener.keyReleased(e);
  279. }
  280. }
  281. @Override
  282. public void illegalCommand(final String reason) {
  283. UIUtilities.invokeLater(() -> {
  284. errorIndicator.setVisible(true);
  285. errorIndicator.setToolTipText(reason);
  286. wrapIndicator.setVisible(false);
  287. });
  288. }
  289. @Override
  290. public void legalCommand() {
  291. UIUtilities.invokeLater(() -> {
  292. errorIndicator.setVisible(false);
  293. errorIndicator.setToolTipText(null);
  294. });
  295. }
  296. @Override
  297. public void wrappedText(final int count) {
  298. UIUtilities.invokeLater(() -> {
  299. wrapIndicator.setVisible(count > 1);
  300. wrapIndicator.setToolTipText(count + " lines");
  301. errorIndicator.setVisible(false);
  302. });
  303. }
  304. @Override
  305. public void propertyChange(final PropertyChangeEvent evt) {
  306. if (!isFocusOwner()) {
  307. hideColourPicker();
  308. }
  309. }
  310. }