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.3KB

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