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.

NewServerDialog.java 13KB

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