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.

AddGroupInputDialog.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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.Validator;
  31. import java.awt.Window;
  32. import java.awt.event.ActionEvent;
  33. import java.awt.event.ActionListener;
  34. import java.awt.event.WindowAdapter;
  35. import java.awt.event.WindowEvent;
  36. import javax.swing.JButton;
  37. import javax.swing.JLabel;
  38. import javax.swing.JTextField;
  39. import javax.swing.JTree;
  40. import javax.swing.event.DocumentEvent;
  41. import javax.swing.event.DocumentListener;
  42. import javax.swing.tree.DefaultMutableTreeNode;
  43. import net.miginfocom.swing.MigLayout;
  44. /**
  45. * Standard input dialog.
  46. */
  47. public class AddGroupInputDialog extends StandardDialog {
  48. /** Serial version UID. */
  49. private static final long serialVersionUID = 1;
  50. /** Validator. */
  51. private final Validator<String> validator;
  52. /** Group name. */
  53. private ValidatingJTextField groupName;
  54. /** Network name. */
  55. private JTextField networkName;
  56. /** Blurb label. */
  57. private TextLabel blurb;
  58. /** Message. */
  59. private final String message;
  60. /** Parent tree. */
  61. private final JTree tree;
  62. /** Parent model. */
  63. private final ServerListModel serverListModel;
  64. /**
  65. * Instantiates a new standard input dialog.
  66. *
  67. * @param iconManager Icon manager
  68. * @param items Parent tree
  69. * @param owner Dialog owner
  70. * @param model Server list model
  71. */
  72. public AddGroupInputDialog(final IconManager iconManager,
  73. final Window owner, final JTree items,
  74. final ServerListModel model) {
  75. super(owner, ModalityType.MODELESS);
  76. this.tree = items;
  77. this.serverListModel = model;
  78. this.validator = new NotEmptyValidator();
  79. this.message = "Please fill in the group name and its network name";
  80. setTitle("Add new server group");
  81. setDefaultCloseOperation(StandardInputDialog.DISPOSE_ON_CLOSE);
  82. initComponents(iconManager);
  83. addListeners();
  84. layoutComponents();
  85. }
  86. /**
  87. * Called when the dialog's OK button is clicked.
  88. *
  89. * @return whether the dialog can close
  90. */
  91. public boolean save() {
  92. DefaultMutableTreeNode groupNode = (DefaultMutableTreeNode) tree.
  93. getSelectionPath().getLastPathComponent();
  94. if (groupNode == null) {
  95. groupNode = (DefaultMutableTreeNode) tree.getModel().getRoot();
  96. } else {
  97. while (!((groupNode.getUserObject()) instanceof ServerGroup)) {
  98. if (groupNode.getParent() == null) {
  99. groupNode = (DefaultMutableTreeNode) tree.getModel().getRoot();
  100. break;
  101. } else {
  102. groupNode = (DefaultMutableTreeNode) groupNode.getParent();
  103. }
  104. }
  105. }
  106. if (groupNode == tree.getModel().getRoot()) {
  107. serverListModel.addGroup(null, getGroupName(), getNetworkName());
  108. } else {
  109. serverListModel.addGroup((ServerGroup) groupNode.getUserObject(),
  110. getGroupName(), getNetworkName());
  111. }
  112. return true;
  113. }
  114. /**
  115. * Initialises the components.
  116. */
  117. private void initComponents(final IconManager iconManager) {
  118. orderButtons(new JButton(), new JButton());
  119. groupName = new ValidatingJTextField(iconManager, validator);
  120. networkName = new JTextField();
  121. blurb = new TextLabel(message);
  122. validateText();
  123. }
  124. /**
  125. * Adds the listeners.
  126. */
  127. private void addListeners() {
  128. getOkButton().addActionListener(new ActionListener() {
  129. /** {@inheritDoc} */
  130. @Override
  131. public void actionPerformed(final ActionEvent e) {
  132. if (save()) {
  133. dispose();
  134. }
  135. }
  136. });
  137. getCancelButton().addActionListener(new ActionListener() {
  138. /** {@inheritDoc} */
  139. @Override
  140. public void actionPerformed(final ActionEvent e) {
  141. dispose();
  142. }
  143. });
  144. addWindowListener(new WindowAdapter() {
  145. /** {@inheritDoc} */
  146. @Override
  147. public void windowOpened(final WindowEvent e) {
  148. groupName.requestFocusInWindow();
  149. }
  150. /** {@inheritDoc} */
  151. @Override
  152. public void windowClosed(final WindowEvent e) {
  153. //Ignore
  154. }
  155. });
  156. groupName.getDocument().addDocumentListener(new DocumentListener() {
  157. /** {@inheritDoc} */
  158. @Override
  159. public void insertUpdate(final DocumentEvent e) {
  160. validateText();
  161. }
  162. /** {@inheritDoc} */
  163. @Override
  164. public void removeUpdate(final DocumentEvent e) {
  165. validateText();
  166. }
  167. /** {@inheritDoc} */
  168. @Override
  169. public void changedUpdate(final DocumentEvent e) {
  170. //Ignore
  171. }
  172. });
  173. }
  174. /** {@inheritDoc} */
  175. @Override
  176. public boolean enterPressed() {
  177. executeAction(getOkButton());
  178. return true;
  179. }
  180. /**
  181. * Validates the change.
  182. */
  183. private void validateText() {
  184. getOkButton().setEnabled(!validator.validate(getGroupName())
  185. .isFailure());
  186. }
  187. /**
  188. * Lays out the components.
  189. */
  190. private void layoutComponents() {
  191. setLayout(new MigLayout("fill, wrap 2"));
  192. add(blurb, "growx, spanx 2");
  193. add(new JLabel("Group name: "));
  194. add(groupName, "growx");
  195. add(new JLabel("Network name: "));
  196. add(networkName, "growx");
  197. add(getLeftButton(), "split 3, skip, right");
  198. add(getRightButton(), "right");
  199. }
  200. /**
  201. * Returns the text in the group name.
  202. *
  203. * @return Group name
  204. */
  205. public String getGroupName() {
  206. return groupName.getText();
  207. }
  208. /**
  209. * Returns the text in the network name.
  210. *
  211. * @return Network name
  212. */
  213. public String getNetworkName() {
  214. return networkName.getText();
  215. }
  216. }