File Source: NewServerDialog.java
/*
P/P * Method: com.dmdirc.addons.ui_swing.dialogs.NewServerDialog__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 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
23 package com.dmdirc.addons.ui_swing.dialogs;
24
25 import com.dmdirc.Server;
26 import com.dmdirc.ServerManager;
27 import com.dmdirc.addons.ui_swing.MainFrame;
28 import com.dmdirc.config.Identity;
29 import com.dmdirc.config.IdentityManager;
30 import com.dmdirc.config.prefs.validator.PortValidator;
31 import com.dmdirc.config.prefs.validator.RegexStringValidator;
32 import com.dmdirc.addons.ui_swing.components.LoggingSwingWorker;
33 import com.dmdirc.addons.ui_swing.components.StandardDialog;
34 import com.dmdirc.addons.ui_swing.components.validating.ValidatingJTextField;
35 import com.dmdirc.addons.ui_swing.dialogs.profiles.ProfileManagerDialog;
36
37 import java.awt.Dialog.ModalityType;
38 import java.awt.Insets;
39 import java.awt.event.ActionEvent;
40 import java.awt.event.ActionListener;
41
42 import java.awt.event.KeyEvent;
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.JComponent;
50 import javax.swing.JLabel;
51 import javax.swing.JPasswordField;
52 import javax.swing.JTextField;
53 import javax.swing.KeyStroke;
54 import javax.swing.WindowConstants;
55
56 import net.miginfocom.swing.MigLayout;
57
58 /**
59 * Dialog that allows the user to enter details of a new server to connect to.
60 */
/*
P/P * Method: JCheckBox access$000(NewServerDialog)
*
* Preconditions:
* x0 != null
* init'ed(x0.sslCheck)
*
* Postconditions:
* return_value == x0.sslCheck
* init'ed(return_value)
*/
61 public final class NewServerDialog extends StandardDialog implements ActionListener {
62
63 /**
64 * A version number for this class. It should be changed whenever the class
65 * structure is changed (or anything else that would prevent serialized
66 * objects being unserialized with the new class).
67 */
68 private static final long serialVersionUID = 8;
69 /** A previously created instance of NewServerDialog. */
70 private static volatile NewServerDialog me;
71 /** checkbox. */
72 private JCheckBox newServerWindowCheck;
73 /** checkbox. */
74 private JCheckBox sslCheck;
75 /** text field. */
76 private ValidatingJTextField serverField;
77 /** text field. */
78 private ValidatingJTextField portField;
79 /** text field. */
80 private JTextField passwordField;
81 /** combo box. */
82 private JComboBox identityField;
83 /** button. */
84 private JButton editProfileButton;
85 /** Main frame. */
86 private MainFrame mainFrame;
87
88 /**
89 * Creates a new instance of the dialog.
90 *
91 * @param parentWindow Main frame
92 */
93 private NewServerDialog(final MainFrame mainFrame) {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.dialogs.NewServerDialog(MainFrame)
*
* Preconditions:
* (soft) mainFrame != null
*
* Presumptions:
* init'ed(java.awt.Dialog$ModalityType.MODELESS)
*
* Postconditions:
* this.editProfileButton == &new JButton(initComponents#9)
* this.identityField == &new JComboBox(initComponents#8)
* this.mainFrame == mainFrame
* this.mainFrame != null
* this.newServerWindowCheck == &new JCheckBox(initComponents#6)
* this.passwordField == &new JPasswordField(initComponents#5)
* this.portField == &new ValidatingJTextField(initComponents#3)
* this.serverField == &new ValidatingJTextField(initComponents#1)
* this.sslCheck == &new JCheckBox(initComponents#7)
* new JButton(initComponents#9) num objects == 1
* ...
*/
94 super(mainFrame, ModalityType.MODELESS);
95
96 this.mainFrame = mainFrame;
97
98 initComponents();
99 layoutComponents();
100 addListeners();
101 setResizable(false);
102
103 update();
104 }
105
106 /**
107 * Creates the new server dialog if one doesn't exist, and displays it.
108 *
109 * @param mainFrame Main frame
110 */
111 public static void showNewServerDialog(final MainFrame mainFrame) {
/*
P/P * Method: void showNewServerDialog(MainFrame)
*
* Preconditions:
* init'ed(me)
* (soft) mainFrame != null
*
* Postconditions:
* me == One-of{old me, &new NewServerDialog(getNewServerDialog#1)}
* me != null
* new JButton(initComponents#9) num objects <= 1
* new JCheckBox(initComponents#6) num objects == new JButton(initComponents#9) num objects
* new JCheckBox(initComponents#7) num objects == new JButton(initComponents#9) num objects
* new JComboBox(initComponents#8) num objects == new JButton(initComponents#9) num objects
* new JLabel(ValidatingJTextField#1) num objects == new JButton(initComponents#9) num objects
* new JPasswordField(initComponents#5) num objects == new JButton(initComponents#9) num objects
* new JTextField(ValidatingJTextField#1) num objects == new JButton(initComponents#9) num objects
* new NewServerDialog(getNewServerDialog#1) num objects == new JButton(initComponents#9) num objects
* ...
*/
112 me = getNewServerDialog(mainFrame);
113
114 me.setLocationRelativeTo(mainFrame);
115 me.setVisible(true);
116 me.requestFocusInWindow();
117 }
118
119 /**
120 * Returns the current instance of the NewServerDialog.
121 *
122 * @param mainFrame Main frame
123 *
124 * @return The current NewServerDialog instance
125 */
126 public static NewServerDialog getNewServerDialog(final MainFrame mainFrame) {
/*
P/P * Method: NewServerDialog getNewServerDialog(MainFrame)
*
* Preconditions:
* init'ed(me)
* (soft) mainFrame != null
*
* Postconditions:
* me == One-of{old me, &new NewServerDialog(getNewServerDialog#1)}
* me != null
* return_value == One-of{old me, &new NewServerDialog(getNewServerDialog#1)}
* return_value != null
* new JButton(initComponents#9) num objects <= 1
* new JCheckBox(initComponents#6) num objects <= 1
* new JCheckBox(initComponents#7) num objects <= 1
* new JComboBox(initComponents#8) num objects <= 1
* new JLabel(ValidatingJTextField#1) num objects <= 1
* new JPasswordField(initComponents#5) num objects <= 1
* ...
*/
127 synchronized (NewServerDialog.class) {
128 if (me == null) {
129 me = new NewServerDialog(mainFrame);
130 }
131 }
132
133 return me;
134 }
135
136 /**
137 * Is the new server dialog showing?
138 *
139 * @return true iif the NSD is showing
140 */
141 public static synchronized boolean isNewServerDialogShowing() {
/*
P/P * Method: bool isNewServerDialogShowing()
*
* Preconditions:
* init'ed(me)
*
* Postconditions:
* init'ed(return_value)
*/
142 return me != null;
143 }
144
145 /** Updates the values to defaults. */
146 private void update() {
/*
P/P * Method: void update()
*
* Preconditions:
* this.identityField != null
* this.newServerWindowCheck != null
* this.passwordField != null
* this.portField != null
* this.portField.textField != null
* this.serverField != null
* this.serverField.textField != null
* this.sslCheck != null
* (soft) this.mainFrame != null
*
* Presumptions:
* com.dmdirc.ServerManager:getServerManager(...)@158 != null
* com.dmdirc.config.IdentityManager:getGlobalConfig(...)@147 != null
* com.dmdirc.config.IdentityManager:getGlobalConfig(...)@149 != null
* com.dmdirc.config.IdentityManager:getGlobalConfig(...)@151 != null
*
* Test Vectors:
* com.dmdirc.ServerManager:numServers(...)@158: {0}, {-231..-1, 1..232-1}
* com.dmdirc.addons.ui_swing.MainFrame:getActiveFrame(...)@158: Inverse{null}, Addr_Set{null}
*/
147 serverField.setText(IdentityManager.getGlobalConfig().getOption("general",
148 "server"));
149 portField.setText(IdentityManager.getGlobalConfig().getOption("general",
150 "port"));
151 passwordField.setText(IdentityManager.getGlobalConfig().getOption("general",
152 "password"));
153 sslCheck.setSelected(false);
154 newServerWindowCheck.setEnabled(false);
155
156 serverField.requestFocusInWindow();
157
158 if (ServerManager.getServerManager().numServers() == 0 ||
159 mainFrame.getActiveFrame() == null) {
160 newServerWindowCheck.setSelected(true);
161 newServerWindowCheck.setEnabled(false);
162 } else {
163 newServerWindowCheck.setEnabled(true);
164 }
165
166 populateProfiles();
167 }
168
169 /**
170 * Adds listeners for various objects in the dialog.
171 */
172 private void addListeners() {
/*
P/P * Method: void addListeners()
*
* Preconditions:
* this.editProfileButton != null
*
* Presumptions:
* com.dmdirc.addons.ui_swing.dialogs.NewServerDialog:getCancelButton(...)@173 != null
* com.dmdirc.addons.ui_swing.dialogs.NewServerDialog:getOkButton(...)@174 != null
* com.dmdirc.addons.ui_swing.dialogs.NewServerDialog:getRootPane(...)@185 != null
*/
173 getCancelButton().addActionListener(this);
174 getOkButton().addActionListener(this);
175 editProfileButton.addActionListener(this);
/*
P/P * Method: void com.dmdirc.addons.ui_swing.dialogs.NewServerDialog$1(NewServerDialog)
*/
176 final ActionListener enterListener = new ActionListener() {
177
178 /** {@inheritDoc} */
179 @Override
180 public void actionPerformed(final ActionEvent actionEvent) {
/*
P/P * Method: void actionPerformed(ActionEvent)
*
* Presumptions:
* com.dmdirc.addons.ui_swing.dialogs.NewServerDialog:getOkButton(...)@181 != null
*/
181 getOkButton().doClick();
182 }
183 };
184 final KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
185 getRootPane().registerKeyboardAction(enterListener, enter,
186 JComponent.WHEN_IN_FOCUSED_WINDOW);
187 }
188
189 /**
190 * Initialises the components in this dialog.
191 */
192 private void initComponents() {
/*
P/P * Method: void initComponents()
*
* Postconditions:
* this.editProfileButton == &new JButton(initComponents#9)
* this.identityField == &new JComboBox(initComponents#8)
* this.newServerWindowCheck == &new JCheckBox(initComponents#6)
* this.passwordField == &new JPasswordField(initComponents#5)
* this.portField == &new ValidatingJTextField(initComponents#3)
* this.serverField == &new ValidatingJTextField(initComponents#1)
* this.sslCheck == &new JCheckBox(initComponents#7)
* new JButton(initComponents#9) num objects == 1
* new JCheckBox(initComponents#6) num objects == 1
* new JCheckBox(initComponents#7) num objects == 1
* ...
*/
193 serverField = new ValidatingJTextField(new RegexStringValidator("^[^\\s]+$+",
194 "Cannot contain spaces."));
195 portField = new ValidatingJTextField(new PortValidator());
196 passwordField = new JPasswordField();
197 newServerWindowCheck = new JCheckBox();
198 newServerWindowCheck.setSelected(true);
199 sslCheck = new JCheckBox();
200 identityField = new JComboBox();
201 editProfileButton = new JButton();
202
203 setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
204 orderButtons(new JButton(), new JButton());
205 setTitle("Connect to a new server");
206
207 populateProfiles();
208
209 editProfileButton.setText("Edit");
210
211 newServerWindowCheck.setText("Open in a new server window?");
212 newServerWindowCheck.setBorder(
213 BorderFactory.createEmptyBorder(0, 0, 0, 0));
214 newServerWindowCheck.setMargin(new Insets(0, 0, 0, 0));
215
216 sslCheck.setText("Use a secure (SSL) connection?");
217 sslCheck.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
218 sslCheck.setMargin(new Insets(0, 0, 0, 0));
219 }
220
221 /** Populates the profiles list. */
222 public void populateProfiles() {
/*
P/P * Method: void populateProfiles()
*
* Preconditions:
* this.identityField != null
*
* Presumptions:
* com.dmdirc.config.IdentityManager:getProfiles(...)@223 != null
* javax.swing.JComboBox:getModel(...)@224 != null
* javax.swing.JComboBox:getModel(...)@226 != null
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@225: {0}, {1}
*/
223 final List<Identity> profiles = IdentityManager.getProfiles();
224 ((DefaultComboBoxModel) identityField.getModel()).removeAllElements();
225 for (Identity profile : profiles) {
226 ((DefaultComboBoxModel) identityField.getModel()).addElement(profile);
227 }
228 }
229
230 /**
231 * Lays out the components in the dialog.
232 */
233 private void layoutComponents() {
/*
P/P * Method: void layoutComponents()
*
* Preconditions:
* init'ed(this.editProfileButton)
* init'ed(this.identityField)
* init'ed(this.newServerWindowCheck)
* init'ed(this.passwordField)
* init'ed(this.portField)
* init'ed(this.serverField)
* init'ed(this.sslCheck)
*
* Presumptions:
* com.dmdirc.addons.ui_swing.dialogs.NewServerDialog:getContentPane(...)@234 != null
* com.dmdirc.addons.ui_swing.dialogs.NewServerDialog:getContentPane(...)@236 != null
* com.dmdirc.addons.ui_swing.dialogs.NewServerDialog:getContentPane(...)@238 != null
* com.dmdirc.addons.ui_swing.dialogs.NewServerDialog:getContentPane(...)@239 != null
* com.dmdirc.addons.ui_swing.dialogs.NewServerDialog:getContentPane(...)@240 != null
* ...
*/
234 getContentPane().setLayout(new MigLayout("fill"));
235
236 getContentPane().add(new JLabel("Enter the details of the server that " +
237 "you wish to connect to."), "span 3, wrap 1.5*unrel");
238 getContentPane().add(new JLabel("Server: "), "");
239 getContentPane().add(serverField, "growx, pushx, wrap");
240 getContentPane().add(new JLabel("Port: "), "");
241 getContentPane().add(portField, "growx, pushx, wrap");
242 getContentPane().add(new JLabel("Password: "), "");
243 getContentPane().add(passwordField, "growx, pushx, wrap");
244 getContentPane().add(new JLabel("Profile: "), "");
245 getContentPane().add(identityField, "split 2, growx, pushx");
246 getContentPane().add(editProfileButton, "sg button, wrap");
247 getContentPane().add(sslCheck, "skip, wrap");
248 getContentPane().add(newServerWindowCheck, "skip, wrap 1.5*unrel");
249 getContentPane().add(getLeftButton(), "split, skip, right, sg button");
250 getContentPane().add(getRightButton(), "right, sg button");
251
252 pack();
253 }
254
255 /**
256 * Saves the dialog changes.
257 */
258 private void save() {
/*
P/P * Method: void save()
*
* Preconditions:
* this.serverField != null
* this.serverField.textField != null
* (soft) init'ed(me)
* (soft) this.identityField != null
* (soft) this.mainFrame != null
* (soft) this.newServerWindowCheck != null
* (soft) this.passwordField != null
* (soft) this.portField != null
* (soft) this.portField.textField != null
* (soft) this.portField.validator != null
* ...
*
* Presumptions:
* com.dmdirc.ServerManager:getServerManager(...)@278 != null
* com.dmdirc.ServerManager:getServerManager(...)@291 != null
*
* Postconditions:
* me == One-of{old me, null}
* init'ed(me)
*
* Test Vectors:
* com.dmdirc.ServerManager:numServers(...)@278: {0}, {-231..-1, 1..232-1}
* com.dmdirc.addons.ui_swing.MainFrame:getActiveFrame(...)@278: Inverse{null}, Addr_Set{null}
* javax.swing.JCheckBox:isSelected(...)@278: {1}, {0}
*/
259 if (!serverField.validateText()) {
260 serverField.requestFocusInWindow();
261 return;
262 }
263 if (!portField.validateText()) {
264 portField.requestFocusInWindow();
265 return;
266 }
267
268 final String host = serverField.getText();
269 final String pass = passwordField.getText();
270 final int port = Integer.parseInt(portField.getText());
271
272 dispose();
273
274 final Identity profile =
275 (Identity) identityField.getSelectedItem();
276
277 // Open in a new window?
278 if (newServerWindowCheck.isSelected() || ServerManager.getServerManager().
279 numServers() == 0 || mainFrame.getActiveFrame() == null) {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.dialogs.NewServerDialog$2(NewServerDialog, String, int, String, Identity)
*
* Postconditions:
* this.val$host == Param_2
* init'ed(this.val$host)
* this.val$pass == Param_4
* init'ed(this.val$pass)
* this.val$port == Param_3
* init'ed(this.val$port)
* this.val$profile == Param_5
* init'ed(this.val$profile)
*/
280 new LoggingSwingWorker() {
281
282 @Override
283 protected Object doInBackground() throws Exception {
/*
P/P * Method: Object doInBackground()
*
* Preconditions:
* this.sslCheck != null
*
* Postconditions:
* return_value == null
*/
284 new Server(host, port, pass, sslCheck.isSelected(), profile);
285 return null;
286 }
287 }.execute();
288 } else {
289 final com.dmdirc.ui.interfaces.Window active =
290 mainFrame.getActiveFrame();
291 final Server server = ServerManager.getServerManager().
292 getServerFromFrame(active);
/*
P/P * Method: void com.dmdirc.addons.ui_swing.dialogs.NewServerDialog$3(NewServerDialog, Server, String, int, String, Identity)
*
* Postconditions:
* this.val$host == Param_3
* init'ed(this.val$host)
* this.val$pass == Param_5
* init'ed(this.val$pass)
* this.val$port == Param_4
* init'ed(this.val$port)
* this.val$profile == Param_6
* init'ed(this.val$profile)
* this.val$server == Param_2
* init'ed(this.val$server)
*/
293 new LoggingSwingWorker() {
294
295 @Override
296 protected Object doInBackground() throws Exception {
/*
P/P * Method: Object doInBackground()
*
* Preconditions:
* this.sslCheck != null
*
* Postconditions:
* return_value == null
*
* Test Vectors:
* this.val$server: Inverse{null}, Addr_Set{null}
*/
297 if (server == null) {
298 new Server(host, port, pass, sslCheck.isSelected(),
299 profile);
300 } else {
301 server.connect(host, port, pass, sslCheck.isSelected(),
302 profile);
303 }
304 return null;
305 }
306 }.execute();
307 }
308 }
309
310 /**
311 * {@inheritDoc}
312 *
313 * @param e Action event
314 */
315 @Override
316 public void actionPerformed(final ActionEvent e) {
/*
P/P * Method: void actionPerformed(ActionEvent)
*
* Preconditions:
* e != null
* (soft) init'ed(com/dmdirc/addons/ui_swing/dialogs/profiles/ProfileManagerDialog.me)
* (soft) init'ed(me)
* (soft) init'ed(this.editProfileButton)
* (soft) this.identityField != null
* (soft) this.mainFrame != null
* (soft) this.newServerWindowCheck != null
* (soft) this.passwordField != null
* (soft) this.portField != null
* (soft) this.portField.textField != null
* ...
*
* Postconditions:
* com/dmdirc/addons/ui_swing/dialogs/profiles/ProfileManagerDialog.me == One-of{old com/dmdirc/addons/ui_swing/dialogs/profiles/ProfileManagerDialog.me, &new ProfileManagerDialog(getProfileManagerDialog#1)}
* init'ed(com/dmdirc/addons/ui_swing/dialogs/profiles/ProfileManagerDialog.me)
* me == One-of{old me, null}
* init'ed(me)
* new ArrayList(ProfileManagerDialog#1) num objects <= 1
* new ProfileManagerDialog(getProfileManagerDialog#1) num objects <= 1
* init'ed(new ProfileManagerDialog(getProfileManagerDialog#1).addButton)
* init'ed(new ProfileManagerDialog(getProfileManagerDialog#1).deleteButton)
* init'ed(new ProfileManagerDialog(getProfileManagerDialog#1).deletedProfiles)
* init'ed(new ProfileManagerDialog(getProfileManagerDialog#1).details)
* ...
*/
317 if (e.getSource() == getOkButton()) {
318 save();
319 } else if (e.getSource() == editProfileButton) {
320 ProfileManagerDialog.showProfileManagerDialog(mainFrame );
321 } else if (e.getSource() == getCancelButton()) {
322 dispose();
323 }
324 }
325
326 /** {@inheritDoc} */
327 @Override
328 public void dispose() {
/*
P/P * Method: void dispose()
*
* Preconditions:
* init'ed(me)
*
* Postconditions:
* me == null
*
* Test Vectors:
* me: Inverse{null}, Addr_Set{null}
*/
329 if (me == null) {
330 return;
331 }
332 synchronized (NewServerDialog.this) {
333 super.dispose();
334 me = null;
335 }
336 }
337 }
SofCheck Inspector Build Version : 2.17854
| NewServerDialog.java |
2009-Jun-25 01:54:24 |
| NewServerDialog.class |
2009-Sep-02 17:04:15 |
| NewServerDialog$1.class |
2009-Sep-02 17:04:15 |
| NewServerDialog$2.class |
2009-Sep-02 17:04:15 |
| NewServerDialog$3.class |
2009-Sep-02 17:04:15 |