Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

NewServerDialog.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. /**
  81. * Creates a new instance of the dialog.
  82. *
  83. * @param parentWindow Main frame
  84. */
  85. private NewServerDialog(final MainFrame mainFrame) {
  86. super(mainFrame, ModalityType.MODELESS);
  87. this.mainFrame = mainFrame;
  88. initComponents();
  89. layoutComponents();
  90. addListeners();
  91. setResizable(false);
  92. update();
  93. }
  94. /**
  95. * Creates the new server dialog if one doesn't exist, and displays it.
  96. *
  97. * @param mainFrame Main frame
  98. */
  99. public static void showNewServerDialog(final MainFrame mainFrame) {
  100. me = getNewServerDialog(mainFrame);
  101. me.display();
  102. me.requestFocusInWindow();
  103. }
  104. /**
  105. * Returns the current instance of the NewServerDialog.
  106. *
  107. * @param mainFrame Main frame
  108. *
  109. * @return The current NewServerDialog instance
  110. */
  111. public static NewServerDialog getNewServerDialog(final MainFrame mainFrame) {
  112. synchronized (NewServerDialog.class) {
  113. if (me == null) {
  114. me = new NewServerDialog(mainFrame);
  115. }
  116. }
  117. return me;
  118. }
  119. /**
  120. * Is the new server dialog showing?
  121. *
  122. * @return true iif the NSD is showing
  123. */
  124. public static synchronized boolean isNewServerDialogShowing() {
  125. return me != null;
  126. }
  127. /** Updates the values to defaults. */
  128. private void update() {
  129. serverField.setText(IdentityManager.getGlobalConfig().getOption("general",
  130. "server"));
  131. portField.setText(IdentityManager.getGlobalConfig().getOption("general",
  132. "port"));
  133. passwordField.setText(IdentityManager.getGlobalConfig().getOption("general",
  134. "password"));
  135. sslCheck.setSelected(false);
  136. newServerWindowCheck.setEnabled(false);
  137. serverField.requestFocusInWindow();
  138. if (ServerManager.getServerManager().numServers() == 0 ||
  139. mainFrame.getActiveFrame() == null) {
  140. newServerWindowCheck.setSelected(true);
  141. newServerWindowCheck.setEnabled(false);
  142. } else {
  143. newServerWindowCheck.setEnabled(true);
  144. }
  145. populateProfiles();
  146. }
  147. /**
  148. * Adds listeners for various objects in the dialog.
  149. */
  150. private void addListeners() {
  151. getCancelButton().addActionListener(this);
  152. getOkButton().addActionListener(this);
  153. editProfileButton.addActionListener(this);
  154. }
  155. /**
  156. * Initialises the components in this dialog.
  157. */
  158. private void initComponents() {
  159. serverField = new ValidatingJTextField(new RegexStringValidator("^[^\\s]+$+",
  160. "Cannot contain spaces."));
  161. portField = new ValidatingJTextField(new PortValidator());
  162. passwordField = new JPasswordField();
  163. newServerWindowCheck = new JCheckBox();
  164. newServerWindowCheck.setSelected(true);
  165. sslCheck = new JCheckBox();
  166. identityField = new JComboBox();
  167. editProfileButton = new JButton();
  168. setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
  169. orderButtons(new JButton(), new JButton());
  170. setTitle("Connect to a new server");
  171. populateProfiles();
  172. editProfileButton.setText("Edit");
  173. newServerWindowCheck.setText("Open in a new server window?");
  174. newServerWindowCheck.setBorder(
  175. BorderFactory.createEmptyBorder(0, 0, 0, 0));
  176. newServerWindowCheck.setMargin(new Insets(0, 0, 0, 0));
  177. sslCheck.setText("Use a secure (SSL) connection?");
  178. sslCheck.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
  179. sslCheck.setMargin(new Insets(0, 0, 0, 0));
  180. }
  181. /** Populates the profiles list. */
  182. public void populateProfiles() {
  183. final List<Identity> profiles = IdentityManager.getProfiles();
  184. ((DefaultComboBoxModel) identityField.getModel()).removeAllElements();
  185. for (Identity profile : profiles) {
  186. ((DefaultComboBoxModel) identityField.getModel()).addElement(profile);
  187. }
  188. }
  189. /**
  190. * Lays out the components in the dialog.
  191. */
  192. private void layoutComponents() {
  193. getContentPane().setLayout(new MigLayout("fill"));
  194. getContentPane().add(new JLabel("Enter the details of the server that " +
  195. "you wish to connect to."), "span 3, wrap 1.5*unrel");
  196. getContentPane().add(new JLabel("Server: "), "");
  197. getContentPane().add(serverField, "growx, pushx, wrap");
  198. getContentPane().add(new JLabel("Port: "), "");
  199. getContentPane().add(portField, "growx, pushx, wrap");
  200. getContentPane().add(new JLabel("Password: "), "");
  201. getContentPane().add(passwordField, "growx, pushx, wrap");
  202. getContentPane().add(new JLabel("Profile: "), "");
  203. getContentPane().add(identityField, "split 2, growx, pushx");
  204. getContentPane().add(editProfileButton, "sg button, wrap");
  205. getContentPane().add(sslCheck, "skip, wrap");
  206. getContentPane().add(newServerWindowCheck, "skip, wrap 1.5*unrel");
  207. getContentPane().add(getLeftButton(), "split, skip, right, sg button");
  208. getContentPane().add(getRightButton(), "right, sg button");
  209. pack();
  210. }
  211. /**
  212. * Saves the dialog changes.
  213. */
  214. private void save() {
  215. if (!serverField.validateText()) {
  216. serverField.requestFocusInWindow();
  217. return;
  218. }
  219. if (!portField.validateText()) {
  220. portField.requestFocusInWindow();
  221. return;
  222. }
  223. final String host = serverField.getText();
  224. final String pass = passwordField.getText();
  225. final int port = Integer.parseInt(portField.getText());
  226. dispose();
  227. final Identity profile =
  228. (Identity) identityField.getSelectedItem();
  229. try {
  230. final URI address = new URI("irc" + (sslCheck.isSelected() ? "s" : ""), pass, host, port, null, null, null);
  231. // Open in a new window?
  232. if (newServerWindowCheck.isSelected() || ServerManager.getServerManager()
  233. .numServers() == 0 || mainFrame.getActiveFrame() == null) {
  234. new LoggingSwingWorker() {
  235. @Override
  236. protected Object doInBackground() throws Exception {
  237. final Server server = new Server(address, profile);
  238. server.connect();
  239. return null;
  240. }
  241. }.execute();
  242. } else {
  243. final com.dmdirc.ui.interfaces.Window active = mainFrame.getActiveFrame();
  244. final Server server = ServerManager.getServerManager().getServerFromFrame(active);
  245. new LoggingSwingWorker() {
  246. @Override
  247. protected Object doInBackground() throws Exception {
  248. if (server == null) {
  249. final Server newServer = new Server(address, profile);
  250. newServer.connect();
  251. } else {
  252. server.connect(address, profile);
  253. }
  254. return null;
  255. }
  256. }.execute();
  257. }
  258. } catch (URISyntaxException ex) {
  259. Logger.userError(ErrorLevel.MEDIUM, "Unable to create URI", ex);
  260. }
  261. }
  262. /**
  263. * {@inheritDoc}
  264. *
  265. * @param e Action event
  266. */
  267. @Override
  268. public void actionPerformed(final ActionEvent e) {
  269. if (e.getSource() == getOkButton()) {
  270. save();
  271. } else if (e.getSource() == editProfileButton) {
  272. ProfileManagerDialog.showProfileManagerDialog(mainFrame );
  273. } else if (e.getSource() == getCancelButton()) {
  274. dispose();
  275. }
  276. }
  277. /** {@inheritDoc} */
  278. @Override
  279. public boolean enterPressed() {
  280. executeAction(getOkButton());
  281. return true;
  282. }
  283. /** {@inheritDoc} */
  284. @Override
  285. public void dispose() {
  286. if (me == null) {
  287. return;
  288. }
  289. synchronized (NewServerDialog.this) {
  290. super.dispose();
  291. me = null;
  292. }
  293. }
  294. }