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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (c) 2006-2014 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.dialogs;
  23. import com.dmdirc.addons.ui_swing.components.text.TextLabel;
  24. import com.dmdirc.addons.ui_swing.components.validating.ValidatingJTextField;
  25. import com.dmdirc.ui.IconManager;
  26. import com.dmdirc.util.validators.ValidationResponse;
  27. import com.dmdirc.util.validators.Validator;
  28. import java.awt.Window;
  29. import java.awt.event.WindowAdapter;
  30. import java.awt.event.WindowEvent;
  31. import java.util.concurrent.atomic.AtomicBoolean;
  32. import javax.swing.JButton;
  33. import javax.swing.WindowConstants;
  34. import javax.swing.event.DocumentEvent;
  35. import javax.swing.event.DocumentListener;
  36. import javax.swing.text.AbstractDocument;
  37. import javax.swing.text.DocumentFilter;
  38. import net.miginfocom.swing.MigLayout;
  39. /**
  40. * Standard input dialog.
  41. */
  42. public abstract class StandardInputDialog extends StandardDialog {
  43. /** Serial version UID. */
  44. private static final long serialVersionUID = 1;
  45. /** Validator. */
  46. private final Validator<String> validator;
  47. /** Text field. */
  48. private ValidatingJTextField textField;
  49. /** Blurb label. */
  50. private TextLabel blurb;
  51. /** Message. */
  52. private final String message;
  53. /** The icon manager to use for validating text fields. */
  54. private final IconManager iconManager;
  55. /** Are we saving? */
  56. protected final AtomicBoolean saving = new AtomicBoolean(false);
  57. /**
  58. * Instantiates a new standard input dialog.
  59. *
  60. * @param owner Dialog owner
  61. * @param modal modality type
  62. * @param iconManager The icon manager to use for validating text fields.
  63. * @param title Dialog title
  64. * @param message Dialog message
  65. */
  66. public StandardInputDialog(
  67. final Window owner, final ModalityType modal, final IconManager iconManager,
  68. final String title, final String message) {
  69. this(owner, modal, iconManager, title, message, o -> new ValidationResponse());
  70. }
  71. /**
  72. * Instantiates a new standard input dialog.
  73. *
  74. * @param owner Dialog owner
  75. * @param modal modality type
  76. * @param iconManager The icon manager to use for validating text fields.
  77. * @param validator Textfield validator
  78. * @param title Dialog title
  79. * @param message Dialog message
  80. */
  81. public StandardInputDialog(
  82. final Window owner, final ModalityType modal, final IconManager iconManager,
  83. final String title, final String message,
  84. final Validator<String> validator) {
  85. super(owner, modal);
  86. this.validator = validator;
  87. this.message = message;
  88. this.iconManager = iconManager;
  89. setTitle(title);
  90. setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  91. initComponents();
  92. addListeners();
  93. layoutComponents();
  94. }
  95. /**
  96. * Called when the dialog's OK button is clicked.
  97. *
  98. * @return whether the dialog can close
  99. */
  100. public abstract boolean save();
  101. /**
  102. * Called when the dialog's cancel button is clicked, or otherwise closed.
  103. */
  104. public abstract void cancelled();
  105. /**
  106. * Initialises the components.
  107. */
  108. private void initComponents() {
  109. orderButtons(new JButton(), new JButton());
  110. textField = new ValidatingJTextField(iconManager, validator);
  111. blurb = new TextLabel(message);
  112. validateText();
  113. }
  114. /**
  115. * Adds the listeners.
  116. */
  117. private void addListeners() {
  118. getOkButton().addActionListener(e -> {
  119. if (save()) {
  120. dispose();
  121. }
  122. });
  123. getCancelButton().addActionListener(e -> {
  124. cancelled();
  125. dispose();
  126. });
  127. addWindowListener(new WindowAdapter() {
  128. @Override
  129. public void windowOpened(final WindowEvent e) {
  130. textField.requestFocusInWindow();
  131. }
  132. @Override
  133. public void windowClosed(final WindowEvent e) {
  134. cancelled();
  135. //dispose();
  136. }
  137. });
  138. textField.getDocument().addDocumentListener(new DocumentListener() {
  139. @Override
  140. public void insertUpdate(final DocumentEvent e) {
  141. validateText();
  142. }
  143. @Override
  144. public void removeUpdate(final DocumentEvent e) {
  145. validateText();
  146. }
  147. @Override
  148. public void changedUpdate(final DocumentEvent e) {
  149. //Ignore
  150. }
  151. });
  152. }
  153. @Override
  154. public boolean enterPressed() {
  155. executeAction(getOkButton());
  156. return true;
  157. }
  158. /**
  159. * Validates the change.
  160. */
  161. private void validateText() {
  162. getOkButton().setEnabled(!validator.validate(getText()).isFailure());
  163. }
  164. /**
  165. * Lays out the components.
  166. */
  167. private void layoutComponents() {
  168. setLayout(new MigLayout("fill, wrap 1"));
  169. add(blurb, "growx");
  170. add(textField, "growx");
  171. add(getLeftButton(), "split 2, right");
  172. add(getRightButton(), "right");
  173. }
  174. /**
  175. * Returns the text in the input field.
  176. *
  177. * @return Input text
  178. */
  179. public final String getText() {
  180. return textField.getText();
  181. }
  182. /**
  183. * Sets the dialogs text to the specified text.
  184. *
  185. * @param text New test
  186. */
  187. public final void setText(final String text) {
  188. textField.setText(text);
  189. }
  190. /**
  191. * Sets a document filter for this dialog's textfield.
  192. *
  193. * @param filter Document filter to add
  194. */
  195. public void setDocumentFilter(final DocumentFilter filter) {
  196. ((AbstractDocument) textField.getDocument()).setDocumentFilter(filter);
  197. }
  198. }