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.

StandardInputDialog.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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.dialogs;
  18. import com.dmdirc.addons.ui_swing.components.text.TextLabel;
  19. import com.dmdirc.addons.ui_swing.components.validating.ValidatingJTextField;
  20. import com.dmdirc.addons.ui_swing.components.IconManager;
  21. import com.dmdirc.util.validators.PermissiveValidator;
  22. import com.dmdirc.util.validators.Validator;
  23. import com.google.common.util.concurrent.Runnables;
  24. import java.awt.Window;
  25. import java.awt.event.WindowAdapter;
  26. import java.awt.event.WindowEvent;
  27. import java.util.concurrent.atomic.AtomicBoolean;
  28. import java.util.function.Consumer;
  29. import java.util.function.Function;
  30. import javax.swing.JButton;
  31. import javax.swing.WindowConstants;
  32. import javax.swing.event.DocumentEvent;
  33. import javax.swing.event.DocumentListener;
  34. import javax.swing.text.AbstractDocument;
  35. import javax.swing.text.DocumentFilter;
  36. import net.miginfocom.swing.MigLayout;
  37. /**
  38. * Standard input dialog.
  39. */
  40. public class StandardInputDialog extends StandardDialog {
  41. /** Serial version UID. */
  42. private static final long serialVersionUID = 1;
  43. /** Validator. */
  44. private final Validator<String> validator;
  45. /** Text field. */
  46. private ValidatingJTextField textField;
  47. /** Blurb label. */
  48. private TextLabel blurb;
  49. /** Message. */
  50. private final String message;
  51. /** The icon manager to use for validating text fields. */
  52. private final IconManager iconManager;
  53. /** Are we saving? */
  54. protected final AtomicBoolean saving = new AtomicBoolean(false);
  55. /** Function to call when the dialog is saved. */
  56. private final Function<String, Boolean> save;
  57. /** Function to call when the dialog is cancelled. */
  58. private final Runnable cancel;
  59. /**
  60. * Instantiates a new standard input dialog.
  61. *
  62. * @param owner Dialog owner
  63. * @param modal modality type
  64. * @param iconManager The icon manager to use for validating text fields.
  65. * @param title Dialog title
  66. * @param message Dialog message
  67. */
  68. public StandardInputDialog(
  69. final Window owner, final ModalityType modal, final IconManager iconManager,
  70. final String title, final String message,
  71. final Consumer<String> save) {
  72. this(owner, modal, iconManager, title, message,
  73. s -> { save.accept(s); return true; }, Runnables.doNothing());
  74. }
  75. /**
  76. * Instantiates a new standard input dialog.
  77. *
  78. * @param owner Dialog owner
  79. * @param modal modality type
  80. * @param iconManager The icon manager to use for validating text fields.
  81. * @param title Dialog title
  82. * @param message Dialog message
  83. */
  84. public StandardInputDialog(
  85. final Window owner, final ModalityType modal, final IconManager iconManager,
  86. final String title, final String message,
  87. final Function<String, Boolean> save) {
  88. this(owner, modal, iconManager, title, message, save, Runnables.doNothing());
  89. }
  90. /**
  91. * Instantiates a new standard input dialog.
  92. *
  93. * @param owner Dialog owner
  94. * @param modal modality type
  95. * @param iconManager The icon manager to use for validating text fields.
  96. * @param title Dialog title
  97. * @param message Dialog message
  98. */
  99. public StandardInputDialog(
  100. final Window owner, final ModalityType modal, final IconManager iconManager,
  101. final String title, final String message,
  102. final Function<String, Boolean> save,
  103. final Runnable cancel) {
  104. this(owner, modal, iconManager, title, message, new PermissiveValidator<>(), save, cancel);
  105. }
  106. /**
  107. * Instantiates a new standard input dialog.
  108. *
  109. * @param owner Dialog owner
  110. * @param modal modality type
  111. * @param iconManager The icon manager to use for validating text fields.
  112. * @param validator Textfield validator
  113. * @param title Dialog title
  114. * @param message Dialog message
  115. */
  116. public StandardInputDialog(
  117. final Window owner, final ModalityType modal, final IconManager iconManager,
  118. final String title, final String message,
  119. final Validator<String> validator,
  120. final Consumer<String> save) {
  121. this(owner, modal, iconManager, title, message, validator,
  122. (final String s) -> { save.accept(s); return true; }, Runnables.doNothing());
  123. }
  124. /**
  125. * Instantiates a new standard input dialog.
  126. *
  127. * @param owner Dialog owner
  128. * @param modal modality type
  129. * @param iconManager The icon manager to use for validating text fields.
  130. * @param validator Textfield validator
  131. * @param title Dialog title
  132. * @param message Dialog message
  133. */
  134. public StandardInputDialog(
  135. final Window owner, final ModalityType modal, final IconManager iconManager,
  136. final String title, final String message,
  137. final Validator<String> validator,
  138. final Function<String, Boolean> save) {
  139. this(owner, modal, iconManager, title, message, validator, save, Runnables.doNothing());
  140. }
  141. /**
  142. * Instantiates a new standard input dialog.
  143. *
  144. * @param owner Dialog owner
  145. * @param modal modality type
  146. * @param iconManager The icon manager to use for validating text fields.
  147. * @param validator Textfield validator
  148. * @param title Dialog title
  149. * @param message Dialog message
  150. */
  151. public StandardInputDialog(
  152. final Window owner, final ModalityType modal, final IconManager iconManager,
  153. final String title, final String message,
  154. final Validator<String> validator,
  155. final Consumer<String> save,
  156. final Runnable cancel) {
  157. this(owner, modal, iconManager, title, message, validator,
  158. (final String s) -> { save.accept(s); return true; }, cancel);
  159. }
  160. /**
  161. * Instantiates a new standard input dialog.
  162. *
  163. * @param owner Dialog owner
  164. * @param modal modality type
  165. * @param iconManager The icon manager to use for validating text fields.
  166. * @param validator Textfield validator
  167. * @param title Dialog title
  168. * @param message Dialog message
  169. */
  170. public StandardInputDialog(
  171. final Window owner, final ModalityType modal, final IconManager iconManager,
  172. final String title, final String message,
  173. final Validator<String> validator,
  174. final Function<String, Boolean> save,
  175. final Runnable cancel) {
  176. super(owner, modal);
  177. this.validator = validator;
  178. this.message = message;
  179. this.iconManager = iconManager;
  180. this.save = save;
  181. this.cancel = cancel;
  182. setTitle(title);
  183. setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  184. initComponents();
  185. addListeners();
  186. layoutComponents();
  187. }
  188. /**
  189. * Initialises the components.
  190. */
  191. private void initComponents() {
  192. orderButtons(new JButton(), new JButton());
  193. textField = new ValidatingJTextField(iconManager, validator);
  194. blurb = new TextLabel(message);
  195. validateText();
  196. }
  197. /**
  198. * Adds the listeners.
  199. */
  200. private void addListeners() {
  201. getOkButton().addActionListener(e -> {
  202. if (save.apply(getText())) {
  203. dispose();
  204. }
  205. });
  206. getCancelButton().addActionListener(e -> {
  207. cancel.run();
  208. dispose();
  209. });
  210. addWindowListener(new WindowAdapter() {
  211. @Override
  212. public void windowOpened(final WindowEvent e) {
  213. textField.requestFocusInWindow();
  214. }
  215. @Override
  216. public void windowClosed(final WindowEvent e) {
  217. cancel.run();
  218. //dispose();
  219. }
  220. });
  221. textField.getDocument().addDocumentListener(new DocumentListener() {
  222. @Override
  223. public void insertUpdate(final DocumentEvent e) {
  224. validateText();
  225. }
  226. @Override
  227. public void removeUpdate(final DocumentEvent e) {
  228. validateText();
  229. }
  230. @Override
  231. public void changedUpdate(final DocumentEvent e) {
  232. //Ignore
  233. }
  234. });
  235. }
  236. @Override
  237. public boolean enterPressed() {
  238. executeAction(getOkButton());
  239. return true;
  240. }
  241. /**
  242. * Validates the change.
  243. */
  244. private void validateText() {
  245. getOkButton().setEnabled(!validator.validate(getText()).isFailure());
  246. }
  247. /**
  248. * Lays out the components.
  249. */
  250. private void layoutComponents() {
  251. setLayout(new MigLayout("fill, wrap 1"));
  252. add(blurb, "growx");
  253. add(textField, "growx");
  254. add(getLeftButton(), "split 2, right");
  255. add(getRightButton(), "right");
  256. }
  257. /**
  258. * Returns the text in the input field.
  259. *
  260. * @return Input text
  261. */
  262. public final String getText() {
  263. return textField.getText();
  264. }
  265. /**
  266. * Sets the dialogs text to the specified text.
  267. *
  268. * @param text New test
  269. */
  270. public final void setText(final String text) {
  271. textField.setText(text);
  272. }
  273. /**
  274. * Sets a document filter for this dialog's textfield.
  275. *
  276. * @param filter Document filter to add
  277. */
  278. public void setDocumentFilter(final DocumentFilter filter) {
  279. ((AbstractDocument) textField.getDocument()).setDocumentFilter(filter);
  280. }
  281. }