您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

StandardInputDialog.java 6.6KB

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