View Javadoc
1   /******************************************************************************
2    * ConnectToServerDialog.java - Manage the Connect to Server dialog
3    * $Id$
4    * 
5    * BuckoFIBS - Backgammon by BuckoSoft
6    * Copyright© 2009,2010 - Dick Balaska - BuckoSoft, Corp.
7    * 
8    * $Log$
9    * Revision 1.3  2010/03/03 13:12:21  inim
10   * Replaced (c) sign in comment mangled by CVS default encoding back to UTF-8
11   *
12   * Revision 1.2  2010/03/03 12:19:49  inim
13   * Moved source to UTF8 encoding from CP1252 encoding. To this end all source files' (c) message was updated to "Copyright© 2009,2010 - Dick Balaska - BuckoSoft, Corp.". This replaces the (c) sign to UTF8, and adds the new year 2010.
14   *
15   * Revision 1.1  2010/02/04 05:57:53  inim
16   * Mavenized project folder layout
17   *
18   * Revision 1.8  2009/01/28 18:15:10  dick
19   * Prettier cvs link in the javadoc.
20   *
21   * Revision 1.7  2009/01/18 05:00:40  dick
22   * The owner is a JFrame, not a JDialog.
23   *
24   * Revision 1.6  2009/01/09 07:17:38  dick
25   * Initialize the data after constructing the widget.
26   *
27   * Revision 1.5  2009/01/07 20:03:22  dick
28   * Cleanups and Javadoc.
29   *
30   * Revision 1.4  2009/01/07 08:33:31  dick
31   * Change the settings when the profile changes.
32   *
33   * Revision 1.3  2009/01/06 08:10:37  dick
34   * Don't use deprecated password stuff.
35   *
36   * Revision 1.2  2009/01/05 07:16:09  dick
37   * @see moved to .../account
38   *
39   * Revision 1.1  2009/01/05 07:09:53  dick
40   * ConnectToServerDialog and CreateAccountDialog moved to account.
41   *
42   * Revision 1.5  2009/01/04 19:57:49  dick
43   * Add the New Account handling.
44   *
45   * Revision 1.4  2008/12/11 08:47:55  dick
46   * Center the dialog over the MainDialog. (Looks like poo, but better than being at 0,0).
47   *
48   * Revision 1.3  2008/10/03 06:57:08  dick
49   * Set modal to true.
50   *
51   * Revision 1.2  2008/03/31 07:10:11  dick
52   * Add the username and password.
53   * Use a GridBagLayout.
54   *
55   * Revision 1.1  2008/03/30 05:42:26  dick
56   * Make sure we are connecting correctly...
57   *
58   */
59  package com.buckosoft.fibs.BuckoFIBS.gui.account;
60  
61  import java.awt.GridBagConstraints;
62  import java.awt.GridBagLayout;
63  import java.awt.Insets;
64  
65  import javax.swing.JButton;
66  import javax.swing.JDialog;
67  import javax.swing.JFrame;
68  import javax.swing.JLabel;
69  import javax.swing.JPanel;
70  import javax.swing.JPasswordField;
71  import javax.swing.JTextField;
72  
73  import com.buckosoft.fibs.BuckoFIBS.BFProperties;
74  import com.buckosoft.fibs.BuckoFIBS.CommandDispatcher;
75  
76  /** Manage the Connect to Server dialog.
77   * @author Dick Balaska
78   * @since 2008/03/30
79   * @version $Revision$ <br> $Date$
80   * @see <a href="http://cvs.buckosoft.com/Projects/BuckoFIBS/BuckoFIBS/src/com/buckosoft/fibs/BuckoFIBS/gui/account/ConnectToServerDialog.java">cvs ConnectToServerDialog.java</a>
81   */
82  public class ConnectToServerDialog extends JDialog implements ProfileSelectorNotifier {
83  
84  	private static final long serialVersionUID = 1L;
85  	private JPanel jContentPane = null;
86  	private JLabel serverNameLabel = null;
87  	private JButton jButtonConnect = null;
88  	private JButton jButtonCancel = null;
89  	private	CommandDispatcher	commandDispatcher;  //  @jve:decl-index=0:
90  	private	BFProperties		properties;
91  	private JLabel userNameLabel = null;
92  	private JTextField userNameField = null;
93  	private JLabel passwordLabel = null;
94  	private JPasswordField passwordTextField = null;
95  	private JButton jCreateNewAccountButton = null;
96  	private JPanel jButtonPanel = null;
97  	private ServerPortSelectorPanel jPanelServerPort = null;
98  	private JLabel jLabelProfiles = null;
99  	private ProfileSelectorPanel jPanelProfileSelector = null;
100 	
101 	/** Create the Connect to Server dialog
102 	 * @param owner The parent dialog
103 	 * @param commandDispatcher The commandDispatcher
104 	 */
105 	public ConnectToServerDialog(JFrame owner, CommandDispatcher commandDispatcher) {
106 		super(owner, true);
107 		this.commandDispatcher = commandDispatcher;
108 		this.properties = commandDispatcher.getProperties();
109 		initialize();
110 		this.setLocationRelativeTo(owner);
111 		activateProfileObjects();
112 		this.profileChanged();
113 	}
114 
115 	private void activateProfileObjects() {
116 		boolean b = this.properties.isAllowMultiplePersonalities();
117 		this.jLabelProfiles.setVisible(b);
118 		this.jPanelProfileSelector.setVisible(b);
119 	}
120 
121 	/**
122 	 * This method initializes this
123 	 * 
124 	 * @return void
125 	 */
126 	private void initialize() {
127 		this.setSize(309, 211);
128 		this.setModal(true);
129 		this.setTitle("Connect To Server");
130 		this.setContentPane(getJContentPane());
131 		userNameField.setText(this.properties.getUserName(jPanelProfileSelector.getSelectedProfile()));
132 		passwordTextField.setText(this.properties.getPassword(jPanelProfileSelector.getSelectedProfile()));
133 	}
134 
135 	private void connectButtonActionPerformed(java.awt.event.ActionEvent evt) {
136 		setVisible(false);
137 		int profileId = jPanelProfileSelector.getSelectedProfile();
138 		this.properties.setUserName(profileId, this.userNameField.getText());
139 		this.properties.setPassword(profileId, new String(this.passwordTextField.getPassword()));
140 		this.properties.setServerName(profileId, (String)this.jPanelServerPort.getServerName());
141 		this.properties.setServerPort(profileId, this.jPanelServerPort.getPort());
142 		dispose();
143 		commandDispatcher.dispatch(CommandDispatcher.Command.CONNECT_TO_SERVER);
144 	}
145 
146 	private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
147 		setVisible(false);
148 		dispose();
149 	}
150 
151 	private void newAccountButtonActionPerformed(java.awt.event.ActionEvent evt) {
152 		setVisible(false);
153 		dispose();
154 		commandDispatcher.dispatch(CommandDispatcher.Command.SHOW_NEW_ACCOUNT_DIALOG);
155 	}
156 
157 	
158 	/* (non-Javadoc)
159 	 * @see com.buckosoft.fibs.BuckoFIBS.gui.account.ProfileSelectorNotifier#profileChanged()
160 	 */
161 	@Override
162 	public void profileChanged() {
163 		int profileId = this.properties.getSelectedProfile();
164 		this.userNameField.setText(this.properties.getUserName(profileId));
165 		this.passwordTextField.setText(this.properties.getPassword(profileId));
166 		this.jPanelServerPort.setServerName(this.properties.getServerName(profileId));
167 	}
168 
169 	/**
170 	 * This method initializes jContentPane
171 	 * 
172 	 * @return javax.swing.JPanel
173 	 */
174 	private JPanel getJContentPane() {
175 		if (jContentPane == null) {
176 			GridBagConstraints gridBagConstraints71 = new GridBagConstraints();
177 			gridBagConstraints71.gridx = 1;
178 			gridBagConstraints71.fill = GridBagConstraints.HORIZONTAL;
179 			gridBagConstraints71.gridy = 0;
180 			GridBagConstraints gridBagConstraints61 = new GridBagConstraints();
181 			gridBagConstraints61.gridx = 0;
182 			gridBagConstraints61.anchor = GridBagConstraints.EAST;
183 			gridBagConstraints61.gridy = 0;
184 			jLabelProfiles = new JLabel();
185 			jLabelProfiles.setText("Profile:");
186 			GridBagConstraints gridBagConstraints51 = new GridBagConstraints();
187 			gridBagConstraints51.gridx = 1;
188 			gridBagConstraints51.fill = GridBagConstraints.HORIZONTAL;
189 			gridBagConstraints51.weightx = 1.0;
190 			gridBagConstraints51.anchor = GridBagConstraints.CENTER;
191 			gridBagConstraints51.gridy = 1;
192 			GridBagConstraints gridBagConstraints31 = new GridBagConstraints();
193 			gridBagConstraints31.gridx = 0;
194 			gridBagConstraints31.gridwidth = 2;
195 			gridBagConstraints31.insets = new Insets(0, 0, 5, 0);
196 			gridBagConstraints31.anchor = GridBagConstraints.SOUTH;
197 			gridBagConstraints31.weighty = 1.0;
198 			gridBagConstraints31.gridy = 10;
199 			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
200 			gridBagConstraints9.fill = GridBagConstraints.HORIZONTAL;
201 			gridBagConstraints9.gridy = 4;
202 			gridBagConstraints9.weightx = 1.0;
203 			gridBagConstraints9.insets = new Insets(0, 0, 30, 0);
204 			gridBagConstraints9.gridwidth = 1;
205 			gridBagConstraints9.gridx = 1;
206 			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
207 			gridBagConstraints8.gridx = 0;
208 			gridBagConstraints8.anchor = GridBagConstraints.WEST;
209 			gridBagConstraints8.ipady = 0;
210 			gridBagConstraints8.insets = new Insets(0, 0, 30, 0);
211 			gridBagConstraints8.gridy = 4;
212 			passwordLabel = new JLabel();
213 			passwordLabel.setText("Password:");
214 			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
215 			gridBagConstraints7.fill = GridBagConstraints.HORIZONTAL;
216 			gridBagConstraints7.gridy = 3;
217 			gridBagConstraints7.weightx = 1.0;
218 			gridBagConstraints7.gridwidth = 1;
219 			gridBagConstraints7.gridx = 1;
220 			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
221 			gridBagConstraints6.gridx = 0;
222 			gridBagConstraints6.anchor = GridBagConstraints.WEST;
223 			gridBagConstraints6.gridy = 3;
224 			userNameLabel = new JLabel();
225 			userNameLabel.setText("Username:");
226 			GridBagConstraints gridBagConstraints = new GridBagConstraints();
227 			gridBagConstraints.gridx = 0;
228 			gridBagConstraints.ipadx = 0;
229 			gridBagConstraints.ipady = 4;
230 			gridBagConstraints.anchor = GridBagConstraints.EAST;
231 			gridBagConstraints.gridy = 1;
232 			serverNameLabel = new JLabel();
233 			serverNameLabel.setText("Server:");
234 			jContentPane = new JPanel();
235 			jContentPane.setLayout(new GridBagLayout());
236 			jContentPane.add(serverNameLabel, gridBagConstraints);
237 			jContentPane.add(userNameLabel, gridBagConstraints6);
238 			jContentPane.add(getUserNameField(), gridBagConstraints7);
239 			jContentPane.add(passwordLabel, gridBagConstraints8);
240 			jContentPane.add(getPasswordTextField(), gridBagConstraints9);
241 			jContentPane.add(getJButtonPanel(), gridBagConstraints31);
242 			jContentPane.add(getJPanelServerPort(), gridBagConstraints51);
243 			jContentPane.add(jLabelProfiles, gridBagConstraints61);
244 			jContentPane.add(getJPanelProfileSelector(), gridBagConstraints71);
245 		}
246 		return jContentPane;
247 	}
248 
249 	/** This method initializes userNameField	
250 	 * @return javax.swing.JTextField	
251 	 */
252 	private JTextField getUserNameField() {
253 		if (userNameField == null) {
254 			userNameField = new JTextField();
255 		}
256 		return userNameField;
257 	}
258 
259 	/**
260 	 * This method initializes passwordTextField	
261 	 * 	
262 	 * @return javax.swing.JTextField	
263 	 */
264 	private JPasswordField getPasswordTextField() {
265 		if (passwordTextField == null) {
266 			passwordTextField = new JPasswordField();
267 		}
268 		return passwordTextField;
269 	}
270 
271 	/**
272 	 * This method initializes jButtonConnect	
273 	 * 	
274 	 * @return javax.swing.JButton	
275 	 */
276 	private JButton getJButtonConnect() {
277 		if (jButtonConnect == null) {
278 			jButtonConnect = new JButton();
279 			jButtonConnect.setText("Connect");
280 			jButtonConnect.addActionListener(new java.awt.event.ActionListener() {
281 				public void actionPerformed(java.awt.event.ActionEvent e) {
282 					connectButtonActionPerformed(e);
283 				}
284 			});
285 		}
286 		return jButtonConnect;
287 	}
288 
289 	/**
290 	 * This method initializes jButtonCancel	
291 	 * 	
292 	 * @return javax.swing.JButton	
293 	 */
294 	private JButton getJButtonCancel() {
295 		if (jButtonCancel == null) {
296 			jButtonCancel = new JButton();
297 			jButtonCancel.setText("Cancel");
298 			jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
299 				public void actionPerformed(java.awt.event.ActionEvent e) {
300 					cancelButtonActionPerformed(e);
301 				}
302 			});
303 		}
304 		return jButtonCancel;
305 	}
306 
307 	/**
308 	 * This method initializes jCreateNewAccountButton	
309 	 * 	
310 	 * @return javax.swing.JButton	
311 	 */
312 	private JButton getJCreateNewAccountButton() {
313 		if (jCreateNewAccountButton == null) {
314 			jCreateNewAccountButton = new JButton();
315 			jCreateNewAccountButton.setText("New Account");
316 			jCreateNewAccountButton.addActionListener(new java.awt.event.ActionListener() {
317 				public void actionPerformed(java.awt.event.ActionEvent e) {
318 					newAccountButtonActionPerformed(e);
319 				}
320 			});
321 		}
322 		return jCreateNewAccountButton;
323 	}
324 
325 	/**
326 	 * This method initializes jButtonPanel	
327 	 * 	
328 	 * @return javax.swing.JPanel	
329 	 */
330 	private JPanel getJButtonPanel() {
331 		if (jButtonPanel == null) {
332 			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
333 			gridBagConstraints5.insets = new Insets(0, 4, 1, 4);
334 			gridBagConstraints5.gridy = -1;
335 			gridBagConstraints5.weightx = 0.3;
336 			gridBagConstraints5.gridx = -1;
337 			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
338 			gridBagConstraints4.insets = new Insets(0, 4, 1, 0);
339 			gridBagConstraints4.gridy = -1;
340 			gridBagConstraints4.ipadx = 0;
341 			gridBagConstraints4.ipady = 0;
342 			gridBagConstraints4.weightx = 0.3;
343 			gridBagConstraints4.gridx = -1;
344 			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
345 			gridBagConstraints1.insets = new Insets(0, 0, 1, 4);
346 			gridBagConstraints1.gridy = -1;
347 			gridBagConstraints1.ipadx = 0;
348 			gridBagConstraints1.ipady = 0;
349 			gridBagConstraints1.weightx = 0.3;
350 			gridBagConstraints1.gridx = -1;
351 			jButtonPanel = new JPanel();
352 			jButtonPanel.setLayout(new GridBagLayout());
353 			jButtonPanel.add(getJButtonConnect(), gridBagConstraints1);
354 			jButtonPanel.add(getJCreateNewAccountButton(), gridBagConstraints5);
355 			jButtonPanel.add(getJButtonCancel(), gridBagConstraints4);
356 		}
357 		return jButtonPanel;
358 	}
359 
360 	/**
361 	 * This method initializes jPanelServerPort	
362 	 * 	
363 	 * @return javax.swing.JPanel	
364 	 */
365 	private ServerPortSelectorPanel getJPanelServerPort() {
366 		if (jPanelServerPort == null) {
367 			jPanelServerPort = new ServerPortSelectorPanel(properties);
368 //			jPanelServerPort.setProperties(properties);
369 			jPanelServerPort.setLayout(new GridBagLayout());
370 		}
371 		return jPanelServerPort;
372 	}
373 
374 	/**
375 	 * This method initializes jPanelProfileSelector	
376 	 * 	
377 	 * @return javax.swing.JPanel	
378 	 */
379 	private ProfileSelectorPanel getJPanelProfileSelector() {
380 		if (jPanelProfileSelector == null) {
381 			jPanelProfileSelector = new ProfileSelectorPanel(this, this, properties);
382 			jPanelProfileSelector.setLayout(new GridBagLayout());
383 		}
384 		return jPanelProfileSelector;
385 	}
386 
387 }  //  @jve:decl-index=0:visual-constraint="10,10"