1 package org.convey;
2 
3 import java.awt.*;
4 
5 import java.awt.event.*;
6 
7 import javax.swing.*;
8 
9 import java.util.*;
10 
11 import java.io.*;
12 
13 /**
14  * @author Andy Ames
15  * @version 0.2
16  */
17 public class ConnectDialog extends JDialog implements ActionListener, Externalizable {
18 
19 	protected transient int result;
20 
21 	protected transient String server;
22 
23 	protected DefaultComboBoxModel serverComboBoxModel;
24 
25 	protected transient JComboBox serverComboBox;
26 
27 	protected transient String user;
28 
29 	protected Hashtable userComboBoxModels;
30 
31 	protected transient JComboBox userComboBox;
32 
33 	protected transient String password;
34 
35 	protected transient JPasswordField passTextField;
36 
37 	protected transient JPasswordField retypePassTextField;
38 
39 	protected transient JLabel retypePassLabel;
40 
41 	protected transient JCheckBox newUserCheckBox;
42 
43 	protected transient JCheckBox savePasswordCheckBox;
44 
45 	protected Hashtable passwords;
46 
ConnectDialog()47 	public ConnectDialog() {
48 		super(ConveyFrame.getConveyFrame(), "Connect", true);
49 	}
50 
ConnectDialog(Frame frame)51 	public ConnectDialog(Frame frame) {
52 		super(frame, "Connect", true);
53 
54 		Object[] serverObjects = new Object[]{"jabber.at", "jabber.com", "jabber.org"};
55 		this.serverComboBoxModel = new DefaultComboBoxModel(serverObjects);
56 		int i = this.serverComboBoxModel.getSize();
57 
58 		this.userComboBoxModels = new Hashtable();
59 		DefaultComboBoxModel model = new DefaultComboBoxModel();
60 		this.userComboBoxModels.put("jabber.at", model);
61 		model = new DefaultComboBoxModel();
62 		this.userComboBoxModels.put("jabber.com", model);
63 		model = new DefaultComboBoxModel();
64 		this.userComboBoxModels.put("jabber.org", model);
65 
66 		this.passwords = new Hashtable();
67 
68 		init();
69 	}
70 
init()71 	public void init() {
72 		this.result = JOptionPane.CANCEL_OPTION;
73 
74 		this.server = "";
75 		this.serverComboBox = new JComboBox(this.serverComboBoxModel);
76 		this.serverComboBox.setEditable(true);
77 		final ConnectDialog thisConnectDialog = this;
78 		this.serverComboBox.addActionListener(new ActionListener() {
79 			public void actionPerformed(ActionEvent e) {
80 				if(thisConnectDialog.serverComboBox.getSelectedIndex() != -1) {
81 					String item = (String)thisConnectDialog.serverComboBox.getSelectedItem();
82 					ComboBoxModel model = (ComboBoxModel)thisConnectDialog.userComboBoxModels.get(item);
83 					thisConnectDialog.userComboBox.setModel(model);
84 				} else {
85 					ComboBoxModel model = new DefaultComboBoxModel();
86 					thisConnectDialog.userComboBox.setModel(model);
87 					thisConnectDialog.userComboBoxModels.put(thisConnectDialog.serverComboBox.getSelectedItem(), model);
88 				}
89 				doPassword();
90 			}
91 		});
92 
93 		this.user = "";
94 		this.userComboBox = new JComboBox();
95 		this.userComboBox.setEditable(true);
96 		this.userComboBox.addActionListener(new ActionListener() {
97 			public void actionPerformed(ActionEvent e) {
98 				doPassword();
99 			}
100 		});
101 
102 		if(this.serverComboBoxModel.getSize() > 0) {
103 			this.serverComboBox.setSelectedIndex(0);
104 
105 			ComboBoxModel userModel = (ComboBoxModel)this.userComboBoxModels.get(this.serverComboBox.getSelectedItem());
106 			this.userComboBox.setModel(userModel);
107 			if(userModel.getSize() > 0)
108 				this.userComboBox.setSelectedIndex(0);
109 		}
110 
111 		this.passTextField = new JPasswordField();
112 		this.retypePassTextField = new JPasswordField();
113 		this.retypePassTextField.setEnabled(false);
114 
115 		JLabel serverLabel = new JLabel("Server");
116 		JLabel userLabel = new JLabel("User");
117 		JLabel passLabel = new JLabel("Password");
118 		this.retypePassLabel = new JLabel("Retype Password");
119 		this.retypePassLabel.setEnabled(false);
120 
121 		Action okAction = new AbstractAction("Ok") {
122 			public void actionPerformed(ActionEvent e) {
123 				thisConnectDialog.doAction();
124 			};
125 		};
126 
127 		Action cancelAction = new AbstractAction("Cancel") {
128 			public void actionPerformed(ActionEvent e) {
129 				thisConnectDialog.result = JOptionPane.CANCEL_OPTION;
130 				thisConnectDialog.hide();
131 			}
132 		};
133 
134 		JButton okButton = new JButton(okAction);
135 		okButton.setDefaultCapable(true);
136 
137 		JButton cancelButton = new JButton(cancelAction);
138 
139 		this.newUserCheckBox = new JCheckBox("New User");
140 		this.newUserCheckBox.addActionListener(this);
141 
142 		this.savePasswordCheckBox = new JCheckBox("Save Password");
143 
144 		super.getContentPane().setLayout(new GridBagLayout());
145 
146 		GridBagConstraints c = new GridBagConstraints();
147 
148 		c.gridwidth = 2;
149 		c.gridy = 0;
150 		c.anchor = GridBagConstraints.SOUTHWEST;
151 		c.weighty = 1.0;
152 		c.weightx = 1.0;
153 		c.insets = new Insets(3, 3, 3, 3);
154 		super.getContentPane().add(serverLabel, c);
155 
156 		c.gridy = 1;
157 		c.anchor = GridBagConstraints.WEST;
158 		c.weighty = 0.0;
159 		c.fill = GridBagConstraints.HORIZONTAL;
160 		super.getContentPane().add(this.serverComboBox, c);
161 
162 		c.gridy = 2;
163 		c.anchor = GridBagConstraints.SOUTHWEST;
164 		c.weighty = 1.0;
165 		c.fill = GridBagConstraints.NONE;
166 		super.getContentPane().add(userLabel, c);
167 
168 		c.gridy = 3;
169 		c.anchor = GridBagConstraints.WEST;
170 		c.weighty = 0.0;
171 		c.fill = GridBagConstraints.HORIZONTAL;
172 		super.getContentPane().add(this.userComboBox, c);
173 
174 		c.gridy = 4;
175 		c.anchor = GridBagConstraints.SOUTHWEST;
176 		c.weighty = 1.0;
177 		c.fill = GridBagConstraints.NONE;
178 		super.getContentPane().add(passLabel, c);
179 
180 		c.gridy = 5;
181 		c.anchor = GridBagConstraints.WEST;
182 		c.weighty = 0.0;
183 		c.fill = GridBagConstraints.HORIZONTAL;
184 		super.getContentPane().add(this.passTextField, c);
185 
186 		c.gridy = 6;
187 		c.anchor = GridBagConstraints.WEST;
188 		c.weighty = 0.0;
189 		c.fill = GridBagConstraints.HORIZONTAL;
190 		super.getContentPane().add(this.savePasswordCheckBox, c);
191 
192 		c.gridy = 7;
193 		c.anchor = GridBagConstraints.WEST;
194 		c.weighty = 0.0;
195 		c.fill = GridBagConstraints.HORIZONTAL;
196 		super.getContentPane().add(this.newUserCheckBox, c);
197 
198 		c.gridy = 8;
199 		c.anchor = GridBagConstraints.SOUTHWEST;
200 		c.weighty = 1.0;
201 		c.fill = GridBagConstraints.NONE;
202 		super.getContentPane().add(this.retypePassLabel, c);
203 
204 		c.gridy = 9;
205 		c.anchor = GridBagConstraints.WEST;
206 		c.weighty = 0.0;
207 		c.fill = GridBagConstraints.HORIZONTAL;
208 		super.getContentPane().add(this.retypePassTextField, c);
209 
210 		c.gridwidth = 1;
211 		c.gridy = 10;
212 		c.anchor = GridBagConstraints.EAST;
213 		c.weighty = 1.0;
214 		c.fill = GridBagConstraints.NONE;
215 		super.getContentPane().add(okButton, c);
216 
217 		c.gridx = 1;
218 		c.anchor = GridBagConstraints.WEST;
219 		super.getContentPane().add(cancelButton, c);
220 
221 		super.getRootPane().setDefaultButton(okButton);
222 
223 		super.enableEvents(AWTEvent.KEY_EVENT_MASK);
224 	}
225 
show()226 	public void show() {
227 		this.newUserCheckBox.setSelected(false);
228 		doPassword();
229 		this.retypePassLabel.setEnabled(false);
230 		this.retypePassTextField.setEnabled(false);
231 		this.retypePassTextField.setText("");
232 
233 		this.result = JOptionPane.CANCEL_OPTION;
234 		super.pack();
235 
236 		super.setLocationRelativeTo(super.getParent());
237 
238 		boolean badPass = true;
239 		while(badPass) {
240 			badPass = false;
241 
242 			super.show();
243 
244 			if(this.result == JOptionPane.OK_OPTION && this.newUserCheckBox.isSelected()) {
245 				String p1 = String.valueOf(this.passTextField.getPassword());
246 				String p2 = String.valueOf(this.retypePassTextField.getPassword());
247 
248 				if(!p1.equals(p2)) {
249 					JOptionPane.showMessageDialog(this, "Passwords are not consistant",
250 																				"Password", JOptionPane.ERROR_MESSAGE);
251 					badPass = true;
252 				}
253 			}
254 		}
255 	}
256 
getResult()257 	public int getResult() {
258 		return this.result;
259 	}
260 
getServer()261 	public String getServer() {
262 		return this.server;
263 	}
264 
getUser()265 	public String getUser() {
266 		return this.user;
267 	}
268 
getPassword()269 	public String getPassword() {
270 		return this.password;
271 	}
272 
isNewUser()273 	public boolean isNewUser() {
274 		return this.newUserCheckBox.isSelected();
275 	}
276 
actionPerformed(ActionEvent e)277 	public void actionPerformed(ActionEvent e) {
278 		if(this.newUserCheckBox.isSelected()) {
279 			this.retypePassLabel.setEnabled(true);
280 			this.retypePassTextField.setEnabled(true);
281 		} else {
282 			this.retypePassLabel.setEnabled(false);
283 			this.retypePassTextField.setEnabled(false);
284 			this.retypePassTextField.setText("");
285 		}
286 	}
287 
processKeyEvent(KeyEvent e)288 	protected void processKeyEvent(KeyEvent e) {
289 		if(e.getKeyCode() == KeyEvent.VK_ESCAPE)
290 			this.hide();
291 	}
292 
doPassword()293 	public void doPassword() {
294 		if(this.passTextField != null) {
295 			String serverItem = (String)this.serverComboBox.getSelectedItem();
296 			String userItem = (String)this.userComboBox.getSelectedItem();
297 			if(serverItem != null && userItem != null) {
298 				String key = userItem + "@" + serverItem;
299 				if(this.passwords.containsKey(key)) {
300 					this.savePasswordCheckBox.setSelected(true);
301 					this.passTextField.setText((String)this.passwords.get(key));
302 				} else {
303 					this.savePasswordCheckBox.setSelected(false);
304 					this.passTextField.setText("");
305 				}
306 			} else {
307 				this.savePasswordCheckBox.setSelected(false);
308 				this.passTextField.setText("");
309 			}
310 		}
311 	}
312 
doAction()313 	public void doAction() {
314 		this.server = (String)this.serverComboBox.getSelectedItem();
315 		this.user = (String)this.userComboBox.getSelectedItem();
316 		this.password = String.valueOf(this.passTextField.getPassword());
317 
318 		if(this.savePasswordCheckBox.isSelected())
319 			this.passwords.put(this.user + "@" + this.server, this.password);
320 		else
321 			this.passwords.remove(this.user + "@" + this.server);
322 
323 		DefaultComboBoxModel userModel = (DefaultComboBoxModel)this.userComboBox.getModel();
324 
325 		if(this.serverComboBox.getSelectedIndex() != -1)
326 			this.serverComboBoxModel.removeElement(this.server);
327 
328 		this.serverComboBoxModel.insertElementAt(this.server, 0);
329 		this.serverComboBox.setSelectedIndex(0);
330 
331 		if(this.userComboBox.getSelectedIndex() != -1)
332 			userModel.removeElement(this.user);
333 
334 		userModel.insertElementAt(this.user, 0);
335 		this.userComboBox.setSelectedIndex(0);
336 
337 		this.result = JOptionPane.OK_OPTION;
338 		this.hide();
339 	}
340 
writeExternal(ObjectOutput oo)341 	public void writeExternal(ObjectOutput oo) throws IOException {
342 		oo.writeInt(this.serverComboBoxModel.getSize());
343 		for(int i = 0; i < this.serverComboBoxModel.getSize(); i++) {
344 			String str = this.serverComboBoxModel.getElementAt(i).toString();
345 			oo.writeUTF(str);
346 
347 			DefaultComboBoxModel model = (DefaultComboBoxModel)this.userComboBoxModels.get(str);
348 			oo.writeInt(model.getSize());
349 			for(int j = 0; j < model.getSize(); j++)
350 				oo.writeUTF(model.getElementAt(j).toString());
351 		}
352 
353 		oo.writeInt(this.passwords.size());
354 		Iterator keyIter = this.passwords.keySet().iterator();
355 		Iterator valIter = this.passwords.values().iterator();
356 		while(keyIter.hasNext() && valIter.hasNext()) {
357 			oo.writeUTF(keyIter.next().toString());
358 			oo.writeUTF(valIter.next().toString());
359 		}
360 
361 		oo.flush();
362 	}
363 
readExternal(ObjectInput oi)364         public void readExternal(ObjectInput oi) throws IOException, ClassNotFoundException {
365           this.serverComboBoxModel = new DefaultComboBoxModel();
366           this.userComboBoxModels = new Hashtable();
367           this.passwords = new Hashtable();
368 
369           int serverCount = oi.readInt();
370 		for(int i = 0; i < serverCount; i++) {
371 			String str = oi.readUTF();
372 			this.serverComboBoxModel.addElement(str);
373 
374 			DefaultComboBoxModel userComboBoxModel = new DefaultComboBoxModel();
375 			this.userComboBoxModels.put(str, userComboBoxModel);
376 
377 			int userCount = oi.readInt();
378 			for(int j = 0; j < userCount; j++)
379 				userComboBoxModel.addElement(oi.readUTF());
380 		}
381 
382 		int passCount = oi.readInt();
383 		for(int k = 0; k < passCount; k++)
384 			this.passwords.put(oi.readUTF(), oi.readUTF());
385 
386 		init();
387 	}
388 
389 }