Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

NewServerDialog.java 12KB

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