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.

AddEntryInputDialog.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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.serverlistdialog;
  23. import com.dmdirc.addons.serverlists.ServerGroup;
  24. import com.dmdirc.addons.ui_swing.components.text.TextLabel;
  25. import com.dmdirc.addons.ui_swing.components.validating.ValidatingJTextField;
  26. import com.dmdirc.addons.ui_swing.dialogs.StandardDialog;
  27. import com.dmdirc.addons.ui_swing.dialogs.StandardInputDialog;
  28. import com.dmdirc.ui.IconManager;
  29. import com.dmdirc.util.validators.NotEmptyValidator;
  30. import com.dmdirc.util.validators.URIValidator;
  31. import com.dmdirc.util.validators.Validator;
  32. import java.awt.Window;
  33. import java.awt.event.ActionEvent;
  34. import java.awt.event.ActionListener;
  35. import java.awt.event.WindowAdapter;
  36. import java.awt.event.WindowEvent;
  37. import java.net.URI;
  38. import javax.swing.JButton;
  39. import javax.swing.JLabel;
  40. import javax.swing.JTree;
  41. import javax.swing.event.DocumentEvent;
  42. import javax.swing.event.DocumentListener;
  43. import javax.swing.tree.DefaultMutableTreeNode;
  44. import net.miginfocom.swing.MigLayout;
  45. /**
  46. * Standard input dialog.
  47. */
  48. public class AddEntryInputDialog extends StandardDialog {
  49. /** Serial version UID. */
  50. private static final long serialVersionUID = 1;
  51. /** Entry name Validator. */
  52. private final Validator<String> entryValidator;
  53. /** URI Validator. */
  54. private final Validator<String> uriValidator;
  55. /** Group name. */
  56. private ValidatingJTextField entryName;
  57. /** Network name. */
  58. private ValidatingJTextField uri;
  59. /** Blurb label. */
  60. private TextLabel blurb;
  61. /** Message. */
  62. private final String message;
  63. /** Parent tree. */
  64. private final JTree items;
  65. /** Parent model. */
  66. private final ServerListModel model;
  67. /**
  68. * Instantiates a new standard input dialog.
  69. *
  70. * @param iconManager Icon Manager
  71. * @param items Parent tree
  72. * @param owner Dialog owner
  73. * @param model Server list model
  74. */
  75. public AddEntryInputDialog(final IconManager iconManager,
  76. final Window owner, final JTree items,
  77. final ServerListModel model) {
  78. super(owner, ModalityType.MODELESS);
  79. this.items = items;
  80. this.model = model;
  81. this.entryValidator = new NotEmptyValidator();
  82. this.uriValidator = new URIValidator();
  83. this.message = "Please fill in the entry name and its address";
  84. setTitle("Add new server entry");
  85. setDefaultCloseOperation(StandardInputDialog.DISPOSE_ON_CLOSE);
  86. initComponents(iconManager);
  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 boolean save() {
  96. DefaultMutableTreeNode groupNode = (DefaultMutableTreeNode) items.
  97. getSelectionPath().getLastPathComponent();
  98. while (!((groupNode.getUserObject()) instanceof ServerGroup)) {
  99. groupNode = (DefaultMutableTreeNode) groupNode.getParent();
  100. }
  101. model.addEntry((ServerGroup) groupNode.getUserObject(), getEntryName(),
  102. URI.create(getURI()));
  103. return true;
  104. }
  105. /**
  106. * Initialises the components.
  107. */
  108. private void initComponents(final IconManager iconManager) {
  109. orderButtons(new JButton(), new JButton());
  110. entryName = new ValidatingJTextField(iconManager, entryValidator);
  111. uri = new URIJTextField(iconManager, "", uriValidator);
  112. blurb = new TextLabel(message);
  113. validateText();
  114. }
  115. /**
  116. * Adds the listeners.
  117. */
  118. private void addListeners() {
  119. getOkButton().addActionListener(new ActionListener() {
  120. /** {@inheritDoc} */
  121. @Override
  122. public void actionPerformed(final ActionEvent e) {
  123. if (save()) {
  124. dispose();
  125. }
  126. }
  127. });
  128. getCancelButton().addActionListener(new ActionListener() {
  129. /** {@inheritDoc} */
  130. @Override
  131. public void actionPerformed(final ActionEvent e) {
  132. dispose();
  133. }
  134. });
  135. addWindowListener(new WindowAdapter() {
  136. /** {@inheritDoc} */
  137. @Override
  138. public void windowOpened(final WindowEvent e) {
  139. entryName.requestFocusInWindow();
  140. }
  141. /** {@inheritDoc} */
  142. @Override
  143. public void windowClosed(final WindowEvent e) {
  144. //Ignore
  145. }
  146. });
  147. final DocumentListener dl = new DocumentListener() {
  148. /** {@inheritDoc} */
  149. @Override
  150. public void insertUpdate(final DocumentEvent e) {
  151. validateText();
  152. }
  153. /** {@inheritDoc} */
  154. @Override
  155. public void removeUpdate(final DocumentEvent e) {
  156. validateText();
  157. }
  158. /** {@inheritDoc} */
  159. @Override
  160. public void changedUpdate(final DocumentEvent e) {
  161. //Ignore
  162. }
  163. };
  164. entryName.getDocument().addDocumentListener(dl);
  165. uri.getDocument().addDocumentListener(dl);
  166. }
  167. /** {@inheritDoc} */
  168. @Override
  169. public boolean enterPressed() {
  170. executeAction(getOkButton());
  171. return true;
  172. }
  173. /**
  174. * Validates the change.
  175. */
  176. private void validateText() {
  177. getOkButton().setEnabled(!entryValidator.validate(getEntryName())
  178. .isFailure() && !uriValidator.validate(getURI()).isFailure());
  179. }
  180. /**
  181. * Lays out the components.
  182. */
  183. private void layoutComponents() {
  184. setLayout(new MigLayout("fill, wrap 2"));
  185. add(blurb, "growx, spanx 2");
  186. add(new JLabel("Item name: "));
  187. add(entryName, "growx");
  188. add(new JLabel("Server URI: "));
  189. add(uri, "growx");
  190. add(getLeftButton(), "split 3, skip, right");
  191. add(getRightButton(), "right");
  192. }
  193. /**
  194. * Returns the text in the entry name.
  195. *
  196. * @return Entry name
  197. */
  198. public String getEntryName() {
  199. return entryName.getText();
  200. }
  201. /**
  202. * Returns the URI in the URI field.
  203. *
  204. * @return server URI
  205. */
  206. public String getURI() {
  207. return uri.getText();
  208. }
  209. }