Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

SwingInputField.java 12KB

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