Преглед на файлове

Add NewServerDialog and associated classes.

Change-Id: Id9a2531ee37ca4eee133e3bfa3183d21f8508ea5
Reviewed-on: http://gerrit.dmdirc.com/3596
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
changes/96/3596/4
Greg Holmes преди 10 години
родител
ревизия
7f7f6fd56c

+ 1
- 1
src/com/dmdirc/addons/ui_swing/components/menubar/ServerMenu.java Целия файл

@@ -26,7 +26,7 @@ import com.dmdirc.FrameContainer;
26 26
 import com.dmdirc.ServerState;
27 27
 import com.dmdirc.addons.ui_swing.Apple;
28 28
 import com.dmdirc.addons.ui_swing.components.frames.TextFrame;
29
-import com.dmdirc.addons.ui_swing.dialogs.NewServerDialog;
29
+import com.dmdirc.addons.ui_swing.dialogs.newserver.NewServerDialog;
30 30
 import com.dmdirc.addons.ui_swing.dialogs.serversetting.ServerSettingsDialog;
31 31
 import com.dmdirc.addons.ui_swing.injection.DialogProvider;
32 32
 import com.dmdirc.addons.ui_swing.injection.KeyedDialogProvider;

+ 77
- 0
src/com/dmdirc/addons/ui_swing/components/validating/IntegerJTextComponentComponentValidator.java Целия файл

@@ -0,0 +1,77 @@
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
+
23
+package com.dmdirc.addons.ui_swing.components.validating;
24
+
25
+import com.dmdirc.util.validators.Validator;
26
+
27
+import javax.swing.event.DocumentEvent;
28
+import javax.swing.event.DocumentListener;
29
+import javax.swing.text.JTextComponent;
30
+
31
+/**
32
+ * JTextComponent for integer input.
33
+ */
34
+public class IntegerJTextComponentComponentValidator
35
+        extends ComponentValidator<Integer, JTextComponent> implements DocumentListener {
36
+
37
+    /**
38
+     * Creates a new validator.
39
+     *
40
+     * @param textArea  Text component to validate
41
+     * @param validator Validator to validate against
42
+     */
43
+    public IntegerJTextComponentComponentValidator(final JTextComponent textArea,
44
+            final Validator<Integer> validator) {
45
+        super(textArea, validator);
46
+    }
47
+
48
+    @Override
49
+    public Integer getValidatable() {
50
+        try {
51
+            return Integer.parseInt(getComponent().getText());
52
+        } catch (NumberFormatException ex) {
53
+            return -1;
54
+        }
55
+    }
56
+
57
+    @Override
58
+    public void addHooks() {
59
+        getComponent().getDocument().addDocumentListener(this);
60
+    }
61
+
62
+    @Override
63
+    public void insertUpdate(final DocumentEvent e) {
64
+        validate();
65
+    }
66
+
67
+    @Override
68
+    public void removeUpdate(final DocumentEvent e) {
69
+        validate();
70
+    }
71
+
72
+    @Override
73
+    public void changedUpdate(final DocumentEvent e) {
74
+        validate();
75
+    }
76
+
77
+}

+ 75
- 0
src/com/dmdirc/addons/ui_swing/components/validating/JComboBoxComponentValidator.java Целия файл

@@ -0,0 +1,75 @@
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
+
23
+package com.dmdirc.addons.ui_swing.components.validating;
24
+
25
+import com.dmdirc.util.validators.Validator;
26
+
27
+import java.util.ArrayList;
28
+import java.util.List;
29
+
30
+import javax.swing.JComboBox;
31
+import javax.swing.ListModel;
32
+import javax.swing.event.ListDataEvent;
33
+import javax.swing.event.ListDataListener;
34
+
35
+public class JComboBoxComponentValidator<T> extends ComponentValidator<List<T>, JComboBox<T>> {
36
+
37
+    public JComboBoxComponentValidator(final JComboBox<T> component,
38
+            final Validator<List<T>> validator) {
39
+        super(component, validator);
40
+    }
41
+
42
+    @Override
43
+    @SuppressWarnings("unchecked")
44
+    public List<T> getValidatable() {
45
+        final int size = getComponent().getModel().getSize();
46
+        final List<T> items = new ArrayList<T>(size);
47
+        final ListModel<T> model = getComponent().getModel();
48
+        for (int i = 0; i < size; i++) {
49
+            items.add(model.getElementAt(i));
50
+        }
51
+        return items;
52
+    }
53
+
54
+    @Override
55
+    public void addHooks() {
56
+        getComponent().getModel().addListDataListener(new ListDataListener() {
57
+
58
+            @Override
59
+            public void intervalAdded(final ListDataEvent e) {
60
+                validate();
61
+            }
62
+
63
+            @Override
64
+            public void intervalRemoved(final ListDataEvent e) {
65
+                validate();
66
+            }
67
+
68
+            @Override
69
+            public void contentsChanged(final ListDataEvent e) {
70
+                validate();
71
+            }
72
+        });
73
+    }
74
+
75
+}

+ 74
- 0
src/com/dmdirc/addons/ui_swing/components/validating/ValidationFactory.java Целия файл

@@ -28,6 +28,7 @@ import com.dmdirc.util.validators.Validator;
28 28
 import java.awt.Component;
29 29
 import java.util.List;
30 30
 
31
+import javax.swing.JComboBox;
31 32
 import javax.swing.JComponent;
32 33
 import javax.swing.JLayer;
33 34
 import javax.swing.JList;
@@ -77,6 +78,41 @@ public class ValidationFactory {
77 78
         return getPanel(display, componentValidator, iconManager);
78 79
     }
79 80
 
81
+    /**
82
+     * Retrieves a validating component panel for the given component.
83
+     *
84
+     * @param validation  Component to validate
85
+     * @param validator   Validator to validate against
86
+     * @param iconManager Icon manager to get icons from
87
+     *
88
+     * @return Validating component panel for the component
89
+     */
90
+    public static JComponent getIntValidatorPanel(final JTextComponent validation,
91
+            final Validator<Integer> validator, final IconManager iconManager) {
92
+        return getIntValidatorPanel(validation, validation, validator, iconManager);
93
+    }
94
+
95
+    /**
96
+     * Retrieves a validating component panel for the given component.
97
+     *
98
+     *
99
+     * @param display     Component to display instead in the panel
100
+     * @param validation  Component to validate
101
+     * @param validator   Validator to validate against
102
+     * @param iconManager Icon manager to get icons from
103
+     *
104
+     * @param <T>         Type of component to wrap in the layer UI
105
+     *
106
+     * @return Validating component panel for the component
107
+     */
108
+    public static <T extends Component> JComponent getIntValidatorPanel(final T display,
109
+            final JTextComponent validation, final Validator<Integer> validator,
110
+            final IconManager iconManager) {
111
+        final ComponentValidator<Integer, JTextComponent> componentValidator
112
+                = new IntegerJTextComponentComponentValidator(validation, validator);
113
+        return getPanel(display, componentValidator, iconManager);
114
+    }
115
+
80 116
     /**
81 117
      * Retrieves a validating component panel for the given component.
82 118
      *
@@ -115,6 +151,44 @@ public class ValidationFactory {
115 151
         return getPanel(display, componentValidator, iconManager);
116 152
     }
117 153
 
154
+    /**
155
+     * Retrieves a validating component panel for the given component.
156
+     *
157
+     * @param validation  Component to validate
158
+     * @param validator   Validator to validate against
159
+     * @param iconManager Icon manager to get icons from
160
+     *
161
+     * @param <T>         Type of component to wrap in the layer UI
162
+     *
163
+     * @return Validating component panel for the component
164
+     */
165
+    public static <T> JComponent getValidatorPanel(
166
+            final JComboBox<T> validation, final Validator<List<T>> validator,
167
+            final IconManager iconManager) {
168
+        return getValidatorPanel(validation, validation, validator, iconManager);
169
+    }
170
+
171
+    /**
172
+     * Retrieves a validating component panel for the given component.
173
+     *
174
+     * @param display     Component to display instead in the panel
175
+     * @param validation  Component to validate
176
+     * @param validator   Validator to validate against
177
+     * @param iconManager Icon manager to get icons from
178
+     *
179
+     * @param <T>         Type of component to wrap in the layer UI
180
+     * @param <V>         The type of item in the model that will be validated.
181
+     *
182
+     * @return Validating component panel for the component
183
+     */
184
+    public static <T extends Component, V> JComponent getValidatorPanel(final T display,
185
+            final JComboBox<V> validation, final Validator<List<V>> validator,
186
+            final IconManager iconManager) {
187
+        final ComponentValidator<List<V>, JComboBox<V>> componentValidator
188
+                = new JComboBoxComponentValidator<>(validation, validator);
189
+        return getPanel(display, componentValidator, iconManager);
190
+    }
191
+
118 192
     private static <T extends Component> JComponent getPanel(
119 193
             final T display,
120 194
             final ComponentValidator<?, ? extends JComponent> componentValidator,

+ 0
- 371
src/com/dmdirc/addons/ui_swing/dialogs/NewServerDialog.java Целия файл

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

+ 1
- 1
src/com/dmdirc/addons/ui_swing/dialogs/aliases/AliasManagerLinker.java Целия файл

@@ -27,9 +27,9 @@ import com.dmdirc.addons.ui_swing.components.renderers.PropertyListCellRenderer;
27 27
 import com.dmdirc.addons.ui_swing.components.vetoable.VetoableListSelectionModel;
28 28
 import com.dmdirc.addons.ui_swing.dialogs.StandardInputDialog;
29 29
 import com.dmdirc.commandparser.aliases.Alias;
30
-import com.dmdirc.ui.core.aliases.AliasDialogModelAdapter;
31 30
 import com.dmdirc.interfaces.ui.AliasDialogModel;
32 31
 import com.dmdirc.ui.IconManager;
32
+import com.dmdirc.ui.core.aliases.AliasDialogModelAdapter;
33 33
 
34 34
 import com.google.common.base.Optional;
35 35
 

+ 112
- 0
src/com/dmdirc/addons/ui_swing/dialogs/newserver/NewServerDialog.java Целия файл

@@ -0,0 +1,112 @@
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
+
23
+package com.dmdirc.addons.ui_swing.dialogs.newserver;
24
+
25
+import com.dmdirc.ClientModule.GlobalConfig;
26
+import com.dmdirc.addons.ui_swing.components.NoBorderJCheckBox;
27
+import com.dmdirc.addons.ui_swing.components.text.TextLabel;
28
+import com.dmdirc.addons.ui_swing.components.validating.ValidationFactory;
29
+import com.dmdirc.addons.ui_swing.dialogs.StandardDialog;
30
+import com.dmdirc.addons.ui_swing.dialogs.profiles.ProfileManagerDialog;
31
+import com.dmdirc.addons.ui_swing.injection.DialogProvider;
32
+import com.dmdirc.addons.ui_swing.injection.MainWindow;
33
+import com.dmdirc.interfaces.CommandController;
34
+import com.dmdirc.interfaces.config.ConfigProvider;
35
+import com.dmdirc.interfaces.ui.NewServerDialogModel;
36
+import com.dmdirc.ui.IconManager;
37
+
38
+import java.awt.Window;
39
+
40
+import javax.inject.Inject;
41
+import javax.swing.JButton;
42
+import javax.swing.JCheckBox;
43
+import javax.swing.JComboBox;
44
+import javax.swing.JLabel;
45
+import javax.swing.JTextField;
46
+
47
+import net.miginfocom.swing.MigLayout;
48
+
49
+/**
50
+ * Dialog to connect to a new server.
51
+ */
52
+public class NewServerDialog extends StandardDialog {
53
+
54
+    private static final long serialVersionUID = 1;
55
+
56
+    @Inject
57
+    public NewServerDialog(@MainWindow final Window mainFrame,
58
+            final NewServerDialogModel model,
59
+            @GlobalConfig final IconManager iconManager,
60
+            final CommandController commandController,
61
+            final DialogProvider<ProfileManagerDialog> profileManagerDialog) {
62
+        super(mainFrame, ModalityType.DOCUMENT_MODAL);
63
+        final NewServerLinker linker = new NewServerLinker(model, this);
64
+
65
+        final TextLabel info = new TextLabel("Enter the details of the server you wish to "
66
+                + "connect to.");
67
+        final JTextField hostname = new JTextField();
68
+        final JTextField port = new JTextField();
69
+        final JTextField password = new JTextField();
70
+        final JComboBox<ConfigProvider> profiles = new JComboBox<>();
71
+        final JButton edit = new JButton("Edit");
72
+        final JCheckBox ssl = new NoBorderJCheckBox("Use a secure (SSL) connection?");
73
+        final JCheckBox saveAsDefault
74
+                = new NoBorderJCheckBox("Save these settings as the defaults?");
75
+
76
+        setResizable(false);
77
+
78
+        setTitle("New Server");
79
+        setLayout(new MigLayout("fill"));
80
+
81
+        add(info, "span, wrap 2*unrel");
82
+        add(new JLabel("Hostname: "), "align label");
83
+        add(ValidationFactory.getValidatorPanel(hostname, model.getHostnameValidator(),
84
+                iconManager), "growx, wrap");
85
+        add(new JLabel("Port: "), "align label");
86
+        add(ValidationFactory.getIntValidatorPanel(port, model.getPortValidator(), iconManager),
87
+                "growx, wrap");
88
+        add(new JLabel("Password: "), "align label");
89
+        add(ValidationFactory.getValidatorPanel(password, model.getPasswordValidator(),
90
+                iconManager), "growx, wrap");
91
+        add(new JLabel("Profiles: "), "align label");
92
+        add(ValidationFactory.getValidatorPanel(profiles, model.getProfileListValidator(),
93
+                iconManager), "span, split2, growx");
94
+        add(edit, "wrap, sg button");
95
+        add(ssl, "skip, grow, span, wrap");
96
+        add(saveAsDefault, "skip, grow, span, wrap 2*unrel");
97
+        add(getLeftButton(), "span, split 2, right, sg button");
98
+        add(getRightButton(), "right, sg button");
99
+
100
+        model.loadModel();
101
+        linker.bindHostname(hostname);
102
+        linker.bindPort(port);
103
+        linker.bindPassword(password);
104
+        linker.bindProfiles(profiles);
105
+        linker.bindEditProfiles(edit, profileManagerDialog);
106
+        linker.bindSSL(ssl);
107
+        linker.bindSaveAsDefault(saveAsDefault);
108
+        linker.bindOKButton(getOkButton());
109
+        linker.bindCancelButton(getCancelButton());
110
+    }
111
+
112
+}

+ 259
- 0
src/com/dmdirc/addons/ui_swing/dialogs/newserver/NewServerLinker.java Целия файл

@@ -0,0 +1,259 @@
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
+
23
+package com.dmdirc.addons.ui_swing.dialogs.newserver;
24
+
25
+import com.dmdirc.addons.ui_swing.components.renderers.PropertyListCellRenderer;
26
+import com.dmdirc.addons.ui_swing.components.vetoable.VetoableComboBoxModel;
27
+import com.dmdirc.addons.ui_swing.dialogs.profiles.ProfileManagerDialog;
28
+import com.dmdirc.addons.ui_swing.injection.DialogProvider;
29
+import com.dmdirc.interfaces.config.ConfigProvider;
30
+import com.dmdirc.interfaces.ui.NewServerDialogModel;
31
+import com.dmdirc.ui.core.newserver.NewServerDialogModelAdapter;
32
+
33
+import com.google.common.base.Optional;
34
+
35
+import java.awt.event.ActionEvent;
36
+import java.awt.event.ActionListener;
37
+import java.awt.event.ItemEvent;
38
+import java.awt.event.ItemListener;
39
+import java.beans.PropertyChangeEvent;
40
+import java.beans.PropertyVetoException;
41
+import java.beans.VetoableChangeListener;
42
+
43
+import javax.swing.JButton;
44
+import javax.swing.JCheckBox;
45
+import javax.swing.JComboBox;
46
+import javax.swing.JTextField;
47
+import javax.swing.event.DocumentEvent;
48
+import javax.swing.event.DocumentListener;
49
+
50
+/**
51
+ * Links the New Server Dialog with its model.
52
+ */
53
+public class NewServerLinker {
54
+
55
+    private final NewServerDialogModel model;
56
+    private final NewServerDialog dialog;
57
+
58
+    public NewServerLinker(final NewServerDialogModel model, final NewServerDialog dialog) {
59
+        this.model = model;
60
+        this.dialog = dialog;
61
+    }
62
+
63
+    public void bindHostname(final JTextField hostnameField) {
64
+        hostnameField.setText(model.getHostname().isPresent() ? model.getHostname().get() : "");
65
+        hostnameField.getDocument().addDocumentListener(new DocumentListener() {
66
+
67
+            private void update() {
68
+                model.setHostname(Optional.fromNullable(hostnameField.getText()));
69
+            }
70
+
71
+            @Override
72
+            public void insertUpdate(final DocumentEvent e) {
73
+                update();
74
+            }
75
+
76
+            @Override
77
+            public void removeUpdate(final DocumentEvent e) {
78
+                update();
79
+            }
80
+
81
+            @Override
82
+            public void changedUpdate(final DocumentEvent e) {
83
+                update();
84
+            }
85
+        });
86
+        model.addListener(new NewServerDialogModelAdapter() {
87
+
88
+            @Override
89
+            public void serverDetailsChanged(final Optional<String> hostname,
90
+                    final Optional<Integer> port, final Optional<String> password,
91
+                    final boolean ssl, final boolean saveAsDefault) {
92
+                if (!hostname.equals(Optional.fromNullable(hostnameField.getText()))) {
93
+                    hostnameField.setText(hostname.isPresent() ? hostname.get() : "");
94
+                }
95
+            }
96
+        });
97
+    }
98
+
99
+    public void bindPort(final JTextField portField) {
100
+        portField.setText(model.getPort().isPresent() ? Integer.toString(model.getPort().get())
101
+                : "");
102
+        portField.getDocument().addDocumentListener(new DocumentListener() {
103
+
104
+            private void update() {
105
+                try {
106
+                    model.setPort(Optional.fromNullable(Integer.valueOf(portField.getText())));
107
+                } catch (NumberFormatException ex) {
108
+                    //Do nothing, it'll have to be corrected and its handled by the validator.
109
+                }
110
+            }
111
+
112
+            @Override
113
+            public void insertUpdate(final DocumentEvent e) {
114
+                update();
115
+            }
116
+
117
+            @Override
118
+            public void removeUpdate(final DocumentEvent e) {
119
+                update();
120
+            }
121
+
122
+            @Override
123
+            public void changedUpdate(final DocumentEvent e) {
124
+                update();
125
+            }
126
+        });
127
+    }
128
+
129
+    public void bindPassword(final JTextField passwordField) {
130
+        passwordField.setText(model.getPassword().isPresent() ? model.getPassword().get() : "");
131
+        passwordField.getDocument().addDocumentListener(new DocumentListener() {
132
+
133
+            private void update() {
134
+                model.setPassword(Optional.fromNullable(passwordField.getText()));
135
+            }
136
+
137
+            @Override
138
+            public void insertUpdate(final DocumentEvent e) {
139
+                update();
140
+            }
141
+
142
+            @Override
143
+            public void removeUpdate(final DocumentEvent e) {
144
+                update();
145
+            }
146
+
147
+            @Override
148
+            public void changedUpdate(final DocumentEvent e) {
149
+                update();
150
+            }
151
+        });
152
+    }
153
+
154
+    public void bindProfiles(final JComboBox<ConfigProvider> profilesCombobox) {
155
+        final VetoableComboBoxModel<ConfigProvider> comboBoxModel = new VetoableComboBoxModel<>();
156
+        profilesCombobox.setModel(comboBoxModel);
157
+        profilesCombobox.setRenderer(new PropertyListCellRenderer<>(profilesCombobox.getRenderer(),
158
+                ConfigProvider.class, "name"));
159
+        for (ConfigProvider profile : model.getProfileList()) {
160
+            comboBoxModel.addElement(profile);
161
+        }
162
+        if (model.getSelectedProfile().isPresent()) {
163
+            comboBoxModel.setSelectedItem(model.getSelectedProfile().get());
164
+        } else if (comboBoxModel.getSize() > 0) {
165
+            comboBoxModel.setSelectedItem(comboBoxModel.getElementAt(0));
166
+        }
167
+        comboBoxModel.addVetoableSelectionListener(new VetoableChangeListener() {
168
+
169
+            @Override
170
+            public void vetoableChange(final PropertyChangeEvent evt) throws PropertyVetoException {
171
+                if (evt.getNewValue() == null) {
172
+                    throw new PropertyVetoException("Selection cannot be null", evt);
173
+                }
174
+            }
175
+        });
176
+        profilesCombobox.addItemListener(new ItemListener() {
177
+
178
+            @Override
179
+            public void itemStateChanged(final ItemEvent e) {
180
+                if (e.getStateChange() == ItemEvent.SELECTED) {
181
+                    model.setSelectedProfile(Optional.fromNullable((ConfigProvider) e.getItem()));
182
+                }
183
+            }
184
+        });
185
+    }
186
+
187
+    public void bindEditProfiles(final JButton edit,
188
+            final DialogProvider<ProfileManagerDialog> profileManagerDialog) {
189
+        edit.addActionListener(new ActionListener() {
190
+
191
+            @Override
192
+            public void actionPerformed(final ActionEvent e) {
193
+                profileManagerDialog.displayOrRequestFocus();
194
+            }
195
+        });
196
+        model.addListener(new NewServerDialogModelAdapter() {
197
+
198
+            @Override
199
+            public void selectedProfileChanged(Optional<ConfigProvider> oldProfile,
200
+                    Optional<ConfigProvider> newProfile) {
201
+                edit.setEnabled(model.isProfileListValid()
202
+                        && model.getSelectedProfile().isPresent());
203
+            }
204
+        });
205
+    }
206
+
207
+    public void bindSSL(final JCheckBox sslCheckbox) {
208
+        sslCheckbox.setSelected(model.getSSL());
209
+        sslCheckbox.addActionListener(new ActionListener() {
210
+
211
+            @Override
212
+            public void actionPerformed(final ActionEvent e) {
213
+                model.setSSL(sslCheckbox.isSelected());
214
+            }
215
+        });
216
+    }
217
+
218
+    public void bindSaveAsDefault(final JCheckBox saveAsDefaultCheckbox) {
219
+        saveAsDefaultCheckbox.addActionListener(new ActionListener() {
220
+
221
+            @Override
222
+            public void actionPerformed(final ActionEvent e) {
223
+                model.setSaveAsDefault(saveAsDefaultCheckbox.isSelected());
224
+            }
225
+        });
226
+    }
227
+
228
+    public void bindOKButton(final JButton okButton) {
229
+        okButton.setEnabled(model.isSaveAllowed());
230
+        okButton.addActionListener(new ActionListener() {
231
+
232
+            @Override
233
+            public void actionPerformed(final ActionEvent e) {
234
+                model.save();
235
+                dialog.dispose();
236
+            }
237
+        });
238
+        model.addListener(new NewServerDialogModelAdapter() {
239
+
240
+            @Override
241
+            public void serverDetailsChanged(final Optional<String> hostname,
242
+                    final Optional<Integer> port, final Optional<String> password,
243
+                    final boolean ssl, final boolean saveAsDefault) {
244
+                okButton.setEnabled(model.isSaveAllowed());
245
+            }
246
+        });
247
+    }
248
+
249
+    public void bindCancelButton(final JButton cancelButton) {
250
+        cancelButton.addActionListener(new ActionListener() {
251
+
252
+            @Override
253
+            public void actionPerformed(final ActionEvent e) {
254
+                dialog.dispose();
255
+            }
256
+        });
257
+    }
258
+
259
+}

+ 1
- 1
src/com/dmdirc/addons/ui_swing/dialogs/profiles/ProfileManagerDialog.java Целия файл

@@ -95,7 +95,7 @@ public class ProfileManagerDialog extends StandardDialog {
95 95
             final IdentityFactory identityFactory,
96 96
             final IdentityController identityController,
97 97
             @GlobalConfig final IconManager iconManager) {
98
-        super(mainFrame, ModalityType.MODELESS);
98
+        super(mainFrame, ModalityType.DOCUMENT_MODAL);
99 99
         setTitle("Profile Manager");
100 100
         this.model = new ProfileManagerModel(identityController, identityFactory);
101 101
         this.controller = new ProfileManagerController(this, model, identityFactory);

+ 10
- 3
src/com/dmdirc/addons/ui_swing/injection/DialogModule.java Целия файл

@@ -29,19 +29,18 @@ import com.dmdirc.addons.ui_swing.MainFrame;
29 29
 import com.dmdirc.addons.ui_swing.PrefsComponentFactory;
30 30
 import com.dmdirc.addons.ui_swing.SwingWindowFactory;
31 31
 import com.dmdirc.addons.ui_swing.dialogs.FeedbackDialog;
32
-import com.dmdirc.addons.ui_swing.dialogs.NewServerDialog;
33 32
 import com.dmdirc.addons.ui_swing.dialogs.about.AboutDialog;
34 33
 import com.dmdirc.addons.ui_swing.dialogs.actionsmanager.ActionsManagerDialog;
34
+import com.dmdirc.addons.ui_swing.dialogs.aliases.AliasManagerDialog;
35 35
 import com.dmdirc.addons.ui_swing.dialogs.channellist.ChannelListDialog;
36 36
 import com.dmdirc.addons.ui_swing.dialogs.channelsetting.ChannelSettingsDialog;
37 37
 import com.dmdirc.addons.ui_swing.dialogs.error.ErrorListDialog;
38
-import com.dmdirc.addons.ui_swing.dialogs.aliases.AliasManagerDialog;
38
+import com.dmdirc.addons.ui_swing.dialogs.newserver.NewServerDialog;
39 39
 import com.dmdirc.addons.ui_swing.dialogs.prefs.SwingPreferencesDialog;
40 40
 import com.dmdirc.addons.ui_swing.dialogs.profiles.ProfileManagerDialog;
41 41
 import com.dmdirc.addons.ui_swing.dialogs.serversetting.ServerSettingsDialog;
42 42
 import com.dmdirc.addons.ui_swing.dialogs.updater.SwingRestartDialog;
43 43
 import com.dmdirc.addons.ui_swing.dialogs.updater.SwingUpdaterDialog;
44
-import com.dmdirc.ui.core.aliases.CoreAliasDialogModel;
45 44
 import com.dmdirc.config.prefs.PreferencesManager;
46 45
 import com.dmdirc.interfaces.CommandController;
47 46
 import com.dmdirc.interfaces.Connection;
@@ -49,7 +48,10 @@ import com.dmdirc.interfaces.LifecycleController;
49 48
 import com.dmdirc.interfaces.config.ConfigProvider;
50 49
 import com.dmdirc.interfaces.config.IdentityFactory;
51 50
 import com.dmdirc.interfaces.ui.AliasDialogModel;
51
+import com.dmdirc.interfaces.ui.NewServerDialogModel;
52 52
 import com.dmdirc.plugins.ServiceManager;
53
+import com.dmdirc.ui.core.aliases.CoreAliasDialogModel;
54
+import com.dmdirc.ui.core.newserver.CoreNewServerDialogModel;
53 55
 
54 56
 import java.awt.Window;
55 57
 import java.awt.datatransfer.Clipboard;
@@ -86,6 +88,11 @@ public class DialogModule {
86 88
         return model;
87 89
     }
88 90
 
91
+    @Provides
92
+    public NewServerDialogModel getNewServerDialogModel(final CoreNewServerDialogModel model) {
93
+        return model;
94
+    }
95
+
89 96
     @Provides
90 97
     @Singleton
91 98
     public DialogProvider<NewServerDialog> getNewServerDialogProvider(

Loading…
Отказ
Запис