123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- /*
- * Copyright (c) 2006-2014 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.addons.ui_swing.dialogs;
-
- import com.dmdirc.ClientModule.GlobalConfig;
- import com.dmdirc.ServerManager;
- import com.dmdirc.addons.ui_swing.MainFrame;
- import com.dmdirc.addons.ui_swing.UIUtilities;
- import com.dmdirc.addons.ui_swing.components.LoggingSwingWorker;
- import com.dmdirc.addons.ui_swing.components.validating.ValidatingJTextField;
- import com.dmdirc.addons.ui_swing.components.vetoable.VetoableChangeEvent;
- import com.dmdirc.addons.ui_swing.components.vetoable.VetoableComboBoxModel;
- import com.dmdirc.addons.ui_swing.components.vetoable.VetoableComboBoxSelectionListener;
- import com.dmdirc.addons.ui_swing.dialogs.profiles.ProfileManagerDialog;
- import com.dmdirc.addons.ui_swing.injection.DialogProvider;
- import com.dmdirc.interfaces.Connection;
- import com.dmdirc.interfaces.config.AggregateConfigProvider;
- import com.dmdirc.interfaces.config.ConfigProvider;
- import com.dmdirc.interfaces.config.ConfigProviderListener;
- import com.dmdirc.interfaces.config.IdentityController;
- import com.dmdirc.logger.ErrorLevel;
- import com.dmdirc.logger.Logger;
- import com.dmdirc.ui.IconManager;
- import com.dmdirc.util.validators.PortValidator;
- import com.dmdirc.util.validators.ServerNameValidator;
-
- import java.awt.Insets;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.net.URI;
- import java.net.URISyntaxException;
- import java.util.List;
-
- import javax.inject.Inject;
- import javax.swing.BorderFactory;
- import javax.swing.DefaultComboBoxModel;
- import javax.swing.JButton;
- import javax.swing.JCheckBox;
- import javax.swing.JComboBox;
- import javax.swing.JLabel;
- import javax.swing.JPasswordField;
- import javax.swing.JTextField;
- import javax.swing.MutableComboBoxModel;
- import javax.swing.WindowConstants;
-
- import net.miginfocom.swing.MigLayout;
-
- /**
- * Dialog that allows the user to enter details of a new server to connect to.
- */
- public class NewServerDialog extends StandardDialog implements
- ActionListener, VetoableComboBoxSelectionListener, ConfigProviderListener {
-
- /** Serial version UID. */
- private static final long serialVersionUID = 8;
- /** Identity Manager. */
- private final IdentityController identityController;
- /** Server manager. */
- private final ServerManager serverManager;
- /** Icon manager. */
- private final IconManager iconManager;
- /** Config. */
- private final AggregateConfigProvider config;
- /** Main frame. */
- private final MainFrame mainFrame;
- /** checkbox. */
- private JCheckBox newServerWindowCheck;
- /** checkbox. */
- private JCheckBox sslCheck;
- /** text field. */
- private ValidatingJTextField serverField;
- /** text field. */
- private ValidatingJTextField portField;
- /** text field. */
- private JTextField passwordField;
- /** combo box. */
- private JComboBox<ConfigProvider> identityField;
- /** button. */
- private JButton editProfileButton;
- /** Opening new server? */
- private boolean openingServer = false;
- /** Provider to use to retrieve PMDs. */
- private final DialogProvider<ProfileManagerDialog> profileDialogProvider;
-
- /**
- * Creates a new instance of the dialog.
- *
- * @param mainFrame Main frame
- * @param config Config
- * @param iconManager Icon manager
- * @param identityController Identity controller
- * @param serverManager Server manager
- * @param profileDialogProvider Provider to use to retrieve PMDs.
- */
- @Inject
- public NewServerDialog(
- final MainFrame mainFrame,
- @GlobalConfig final AggregateConfigProvider config,
- @GlobalConfig final IconManager iconManager,
- final IdentityController identityController,
- final ServerManager serverManager,
- final DialogProvider<ProfileManagerDialog> profileDialogProvider) {
- super(mainFrame, ModalityType.MODELESS);
- this.identityController = identityController;
- this.serverManager = serverManager;
- this.mainFrame = mainFrame;
- this.iconManager = iconManager;
- this.config = config;
- this.profileDialogProvider = profileDialogProvider;
-
- initComponents();
- layoutComponents();
- addListeners();
- setResizable(false);
-
- identityController.registerIdentityListener("profile", this);
- update();
- }
-
- /** {@inheritDoc} */
- @Override
- public void display() {
- super.display();
- requestFocusInWindow();
- serverField.selectAll();
- serverField.requestFocus();
- }
-
- /** Updates the values to defaults. */
- private void update() {
- serverField.setText(config.getOption("general", "server"));
- portField.setText(config.getOption("general", "port"));
- passwordField.setText(config.getOption("general", "password"));
- sslCheck.setSelected(false);
- newServerWindowCheck.setEnabled(false);
-
- serverField.requestFocusInWindow();
-
- if (serverManager.numServers() == 0 || mainFrame.getActiveFrame() == null) {
- newServerWindowCheck.setSelected(true);
- newServerWindowCheck.setEnabled(false);
- } else {
- newServerWindowCheck.setEnabled(true);
- }
-
- populateProfiles();
- }
-
- /**
- * Adds listeners for various objects in the dialog.
- */
- private void addListeners() {
- getCancelButton().addActionListener(this);
- getOkButton().addActionListener(this);
- editProfileButton.addActionListener(this);
- ((VetoableComboBoxModel) identityField.getModel()).addVetoableSelectionListener(this);
- }
-
- /**
- * Initialises the components in this dialog.
- */
- private void initComponents() {
- serverField = new ValidatingJTextField(iconManager, new ServerNameValidator());
- portField = new ValidatingJTextField(iconManager, new PortValidator());
- passwordField = new JPasswordField();
- newServerWindowCheck = new JCheckBox();
- newServerWindowCheck.setSelected(true);
- sslCheck = new JCheckBox();
- identityField = new JComboBox<>(new VetoableComboBoxModel<ConfigProvider>());
- editProfileButton = new JButton();
-
- setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
- orderButtons(new JButton(), new JButton());
- setTitle("Connect to a new server");
-
- populateProfiles();
-
- editProfileButton.setText("Edit");
-
- newServerWindowCheck.setText("Open in a new server window?");
- newServerWindowCheck.setBorder(
- BorderFactory.createEmptyBorder(0, 0, 0, 0));
- newServerWindowCheck.setMargin(new Insets(0, 0, 0, 0));
-
- sslCheck.setText("Use a secure (SSL) connection?");
- sslCheck.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
- sslCheck.setMargin(new Insets(0, 0, 0, 0));
- }
-
- /** Populates the profiles list. */
- public void populateProfiles() {
- final List<ConfigProvider> profiles = identityController.getProvidersByType("profile");
- ((DefaultComboBoxModel) identityField.getModel()).removeAllElements();
- for (ConfigProvider profile : profiles) {
- ((MutableComboBoxModel<ConfigProvider>) identityField.getModel()).addElement(profile);
- }
- }
-
- /**
- * Lays out the components in the dialog.
- */
- private void layoutComponents() {
- getContentPane().setLayout(new MigLayout("fill"));
-
- getContentPane().add(new JLabel("Enter the details of the server that "
- + "you wish to connect to."), "span 3, wrap 1.5*unrel");
- getContentPane().add(new JLabel("Server: "), "");
- getContentPane().add(serverField, "growx, pushx, wrap");
- getContentPane().add(new JLabel("Port: "), "");
- getContentPane().add(portField, "growx, pushx, wrap");
- getContentPane().add(new JLabel("Password: "), "");
- getContentPane().add(passwordField, "growx, pushx, wrap");
- getContentPane().add(new JLabel("Profile: "), "");
- getContentPane().add(identityField, "split 2, growx, pushx");
- getContentPane().add(editProfileButton, "sg button, wrap");
- getContentPane().add(sslCheck, "skip, wrap");
- getContentPane().add(newServerWindowCheck, "skip, wrap 1.5*unrel");
- getContentPane().add(getLeftButton(), "split, skip, right, sg button");
- getContentPane().add(getRightButton(), "right, sg button");
-
- pack();
- }
-
- /**
- * Saves the dialog changes.
- */
- private void save() {
- if (openingServer) {
- dispose();
- return;
- }
- if (!serverField.validateText()) {
- serverField.requestFocusInWindow();
- return;
- }
- if (!portField.validateText()) {
- portField.requestFocusInWindow();
- return;
- }
-
- final String host = serverField.getText();
- final String pass = passwordField.getText();
- final int port = Integer.parseInt(portField.getText());
-
- dispose();
- openingServer = true;
-
- final ConfigProvider profile = (ConfigProvider) identityField.getSelectedItem();
-
- try {
- final URI address =
- new URI("irc" + (sslCheck.isSelected() ? "s" : ""), pass, host, port, null, null,
- null);
-
- // Open in a new window?
- if (newServerWindowCheck.isSelected()
- || serverManager.numServers() == 0
- || mainFrame.getActiveFrame() == null) {
-
- new LoggingSwingWorker<Void, Void>() {
- @Override
- protected Void doInBackground() {
- serverManager.connectToAddress(address, profile);
- return null;
- }
- }.executeInExecutor();
- } else {
- final Connection connection = mainFrame.getActiveFrame().getContainer().
- getConnection();
-
- new LoggingSwingWorker<Void, Void>() {
- /** {@inheritDoc} */
- @Override
- protected Void doInBackground() {
- if (connection == null) {
- serverManager.connectToAddress(address, profile);
- } else {
- connection.connect(address, profile);
- }
- return null;
- }
- }.executeInExecutor();
- }
- } catch (URISyntaxException ex) {
- Logger.userError(ErrorLevel.MEDIUM, "Unable to create URI", ex);
- }
- }
-
- /**
- * {@inheritDoc}
- *
- * @param e Action event
- */
- @Override
- public void actionPerformed(final ActionEvent e) {
- if (e.getSource() == getOkButton()) {
- save();
- } else if (e.getSource() == editProfileButton) {
- profileDialogProvider.displayOrRequestFocus(this);
- } else if (e.getSource() == getCancelButton()) {
- dispose();
- }
- }
-
- /** {@inheritDoc} */
- @Override
- public boolean enterPressed() {
- executeAction(getOkButton());
- return true;
- }
-
- /** {@inheritDoc} */
- @Override
- public boolean selectionChanged(final VetoableChangeEvent e) {
- return e.getNewValue() != null;
- }
-
- /** {@inheritDoc} */
- @Override
- public void configProviderAdded(final ConfigProvider configProvider) {
- UIUtilities.invokeLater(new Runnable() {
- /** {@inheritDoc} */
- @Override
- public void run() {
- populateProfiles();
- }
- });
- }
-
- /** {@inheritDoc} */
- @Override
- public void dispose() {
- identityController.unregisterIdentityListener(this);
- super.dispose();
- }
-
- /** {@inheritDoc} */
- @Override
- public void configProviderRemoved(final ConfigProvider configProvider) {
- UIUtilities.invokeLater(new Runnable() {
- /** {@inheritDoc} */
- @Override
- public void run() {
- populateProfiles();
- }
- });
- }
-
- }
|