Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

NewServerDialog.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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.Server;
  24. import com.dmdirc.ServerManager;
  25. import com.dmdirc.addons.ui_swing.MainFrame;
  26. import com.dmdirc.config.Identity;
  27. import com.dmdirc.config.IdentityManager;
  28. import com.dmdirc.config.prefs.validator.PortValidator;
  29. import com.dmdirc.config.prefs.validator.RegexStringValidator;
  30. import com.dmdirc.addons.ui_swing.components.LoggingSwingWorker;
  31. import com.dmdirc.addons.ui_swing.components.validating.ValidatingJTextField;
  32. import com.dmdirc.addons.ui_swing.dialogs.profiles.ProfileManagerDialog;
  33. import com.dmdirc.logger.ErrorLevel;
  34. import com.dmdirc.logger.Logger;
  35. import java.awt.Dialog.ModalityType;
  36. import java.awt.Insets;
  37. import java.awt.event.ActionEvent;
  38. import java.awt.event.ActionListener;
  39. import java.net.URI;
  40. import java.net.URISyntaxException;
  41. import java.util.List;
  42. import javax.swing.BorderFactory;
  43. import javax.swing.DefaultComboBoxModel;
  44. import javax.swing.JButton;
  45. import javax.swing.JCheckBox;
  46. import javax.swing.JComboBox;
  47. import javax.swing.JLabel;
  48. import javax.swing.JPasswordField;
  49. import javax.swing.JTextField;
  50. import javax.swing.WindowConstants;
  51. import net.miginfocom.swing.MigLayout;
  52. /**
  53. * Dialog that allows the user to enter details of a new server to connect to.
  54. */
  55. public final class NewServerDialog extends StandardDialog implements ActionListener {
  56. /**
  57. * A version number for this class. It should be changed whenever the class
  58. * structure is changed (or anything else that would prevent serialized
  59. * objects being unserialized with the new class).
  60. */
  61. private static final long serialVersionUID = 8;
  62. /** A previously created instance of NewServerDialog. */
  63. private static volatile NewServerDialog me;
  64. /** checkbox. */
  65. private JCheckBox newServerWindowCheck;
  66. /** checkbox. */
  67. private JCheckBox sslCheck;
  68. /** text field. */
  69. private ValidatingJTextField serverField;
  70. /** text field. */
  71. private ValidatingJTextField portField;
  72. /** text field. */
  73. private JTextField passwordField;
  74. /** combo box. */
  75. private JComboBox identityField;
  76. /** button. */
  77. private JButton editProfileButton;
  78. /** Main frame. */
  79. private MainFrame mainFrame;
  80. /** Opening new server? */
  81. private boolean openingServer = false;
  82. /**
  83. * Creates a new instance of the dialog.
  84. *
  85. * @param parentWindow Main frame
  86. */
  87. private NewServerDialog(final MainFrame mainFrame) {
  88. super(mainFrame, ModalityType.MODELESS);
  89. this.mainFrame = mainFrame;
  90. initComponents();
  91. layoutComponents();
  92. addListeners();
  93. setResizable(false);
  94. update();
  95. }
  96. /**
  97. * Creates the new server dialog if one doesn't exist, and displays it.
  98. *
  99. * @param mainFrame Main frame
  100. */
  101. public static void showNewServerDialog(final MainFrame mainFrame) {
  102. me = getNewServerDialog(mainFrame);
  103. me.display();
  104. me.requestFocusInWindow();
  105. }
  106. /**
  107. * Returns the current instance of the NewServerDialog.
  108. *
  109. * @param mainFrame Main frame
  110. *
  111. * @return The current NewServerDialog instance
  112. */
  113. public static NewServerDialog getNewServerDialog(final MainFrame mainFrame) {
  114. synchronized (NewServerDialog.class) {
  115. if (me == null) {
  116. me = new NewServerDialog(mainFrame);
  117. }
  118. }
  119. return me;
  120. }
  121. /**
  122. * Is the new server dialog showing?
  123. *
  124. * @return true iif the NSD is showing
  125. */
  126. public static synchronized boolean isNewServerDialogShowing() {
  127. return me != null;
  128. }
  129. /** Updates the values to defaults. */
  130. private void update() {
  131. serverField.setText(IdentityManager.getGlobalConfig().getOption("general",
  132. "server"));
  133. portField.setText(IdentityManager.getGlobalConfig().getOption("general",
  134. "port"));
  135. passwordField.setText(IdentityManager.getGlobalConfig().getOption("general",
  136. "password"));
  137. sslCheck.setSelected(false);
  138. newServerWindowCheck.setEnabled(false);
  139. serverField.requestFocusInWindow();
  140. if (ServerManager.getServerManager().numServers() == 0 ||
  141. mainFrame.getActiveFrame() == null) {
  142. newServerWindowCheck.setSelected(true);
  143. newServerWindowCheck.setEnabled(false);
  144. } else {
  145. newServerWindowCheck.setEnabled(true);
  146. }
  147. populateProfiles();
  148. }
  149. /**
  150. * Adds listeners for various objects in the dialog.
  151. */
  152. private void addListeners() {
  153. getCancelButton().addActionListener(this);
  154. getOkButton().addActionListener(this);
  155. editProfileButton.addActionListener(this);
  156. }
  157. /**
  158. * Initialises the components in this dialog.
  159. */
  160. private void initComponents() {
  161. serverField = new ValidatingJTextField(new RegexStringValidator("^[^\\s]+$+",
  162. "Cannot contain spaces."));
  163. portField = new ValidatingJTextField(new PortValidator());
  164. passwordField = new JPasswordField();
  165. newServerWindowCheck = new JCheckBox();
  166. newServerWindowCheck.setSelected(true);
  167. sslCheck = new JCheckBox();
  168. identityField = new JComboBox();
  169. editProfileButton = new JButton();
  170. setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
  171. orderButtons(new JButton(), new JButton());
  172. setTitle("Connect to a new server");
  173. populateProfiles();
  174. editProfileButton.setText("Edit");
  175. newServerWindowCheck.setText("Open in a new server window?");
  176. newServerWindowCheck.setBorder(
  177. BorderFactory.createEmptyBorder(0, 0, 0, 0));
  178. newServerWindowCheck.setMargin(new Insets(0, 0, 0, 0));
  179. sslCheck.setText("Use a secure (SSL) connection?");
  180. sslCheck.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
  181. sslCheck.setMargin(new Insets(0, 0, 0, 0));
  182. }
  183. /** Populates the profiles list. */
  184. public void populateProfiles() {
  185. final List<Identity> profiles = IdentityManager.getProfiles();
  186. ((DefaultComboBoxModel) identityField.getModel()).removeAllElements();
  187. for (Identity profile : profiles) {
  188. ((DefaultComboBoxModel) identityField.getModel()).addElement(profile);
  189. }
  190. }
  191. /**
  192. * Lays out the components in the dialog.
  193. */
  194. private void layoutComponents() {
  195. getContentPane().setLayout(new MigLayout("fill"));
  196. getContentPane().add(new JLabel("Enter the details of the server that " +
  197. "you wish to connect to."), "span 3, wrap 1.5*unrel");
  198. getContentPane().add(new JLabel("Server: "), "");
  199. getContentPane().add(serverField, "growx, pushx, wrap");
  200. getContentPane().add(new JLabel("Port: "), "");
  201. getContentPane().add(portField, "growx, pushx, wrap");
  202. getContentPane().add(new JLabel("Password: "), "");
  203. getContentPane().add(passwordField, "growx, pushx, wrap");
  204. getContentPane().add(new JLabel("Profile: "), "");
  205. getContentPane().add(identityField, "split 2, growx, pushx");
  206. getContentPane().add(editProfileButton, "sg button, wrap");
  207. getContentPane().add(sslCheck, "skip, wrap");
  208. getContentPane().add(newServerWindowCheck, "skip, wrap 1.5*unrel");
  209. getContentPane().add(getLeftButton(), "split, skip, right, sg button");
  210. getContentPane().add(getRightButton(), "right, sg button");
  211. pack();
  212. }
  213. /**
  214. * Saves the dialog changes.
  215. */
  216. private void save() {
  217. if (openingServer) {
  218. dispose();
  219. return;
  220. }
  221. if (!serverField.validateText()) {
  222. serverField.requestFocusInWindow();
  223. return;
  224. }
  225. if (!portField.validateText()) {
  226. portField.requestFocusInWindow();
  227. return;
  228. }
  229. final String host = serverField.getText();
  230. final String pass = passwordField.getText();
  231. final int port = Integer.parseInt(portField.getText());
  232. dispose();
  233. openingServer = true;
  234. final Identity profile =
  235. (Identity) identityField.getSelectedItem();
  236. try {
  237. final URI address = new URI("irc" + (sslCheck.isSelected() ? "s" : ""), pass, host, port, null, null, null);
  238. // Open in a new window?
  239. if (newServerWindowCheck.isSelected() || ServerManager.getServerManager()
  240. .numServers() == 0 || mainFrame.getActiveFrame() == null) {
  241. new LoggingSwingWorker() {
  242. @Override
  243. protected Object doInBackground() throws Exception {
  244. final Server server = new Server(address, profile);
  245. server.connect();
  246. return null;
  247. }
  248. }.execute();
  249. } else {
  250. final com.dmdirc.ui.interfaces.Window active = mainFrame.getActiveFrame();
  251. final Server server = ServerManager.getServerManager().getServerFromFrame(active);
  252. new LoggingSwingWorker() {
  253. @Override
  254. protected Object doInBackground() throws Exception {
  255. if (server == null) {
  256. final Server newServer = new Server(address, profile);
  257. newServer.connect();
  258. } else {
  259. server.connect(address, profile);
  260. }
  261. return null;
  262. }
  263. }.execute();
  264. }
  265. } catch (URISyntaxException ex) {
  266. Logger.userError(ErrorLevel.MEDIUM, "Unable to create URI", ex);
  267. }
  268. }
  269. /**
  270. * {@inheritDoc}
  271. *
  272. * @param e Action event
  273. */
  274. @Override
  275. public void actionPerformed(final ActionEvent e) {
  276. if (e.getSource() == getOkButton()) {
  277. save();
  278. } else if (e.getSource() == editProfileButton) {
  279. ProfileManagerDialog.showProfileManagerDialog(mainFrame );
  280. } else if (e.getSource() == getCancelButton()) {
  281. dispose();
  282. }
  283. }
  284. /** {@inheritDoc} */
  285. @Override
  286. public boolean enterPressed() {
  287. executeAction(getOkButton());
  288. return true;
  289. }
  290. /** {@inheritDoc} */
  291. @Override
  292. public void dispose() {
  293. if (me == null) {
  294. return;
  295. }
  296. synchronized (NewServerDialog.this) {
  297. super.dispose();
  298. me = null;
  299. }
  300. }
  301. }