|
@@ -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
|
|
-}
|