View Javadoc

1   /********************************************************************************
2    * InternetCafe is a software solution that helps the management of Cybercafes 
3    * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4    * Copyright (C) 2006  Guido Angelo Ingenito
5   
6    * This program is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU General Public License
8    * as published by the Free Software Foundation; either version 2
9    * of the License, or (at your option) any later version.
10  
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   * 
16   * You should have received a copy of the GNU General Public License
17   * along with this program; if not, write to the Free Software
18   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19   *******************************************************************************/
20  package ui.panel;
21  
22  import java.awt.BorderLayout;
23  import java.awt.Color;
24  import java.awt.Dimension;
25  import java.awt.GridLayout;
26  import java.awt.event.ActionEvent;
27  import java.awt.event.ActionListener;
28  import java.util.Date;
29  
30  import javax.swing.ImageIcon;
31  import javax.swing.JButton;
32  import javax.swing.JComboBox;
33  import javax.swing.JLabel;
34  import javax.swing.JPanel;
35  import javax.swing.JPasswordField;
36  import javax.swing.JSplitPane;
37  import javax.swing.JTextField;
38  import javax.swing.border.EtchedBorder;
39  import javax.swing.border.TitledBorder;
40  
41  import org.apache.log4j.Logger;
42  
43  import ui.Messages;
44  import ui.command.CommandExecutor;
45  import ui.command.IO.ExportUserAsVCardCommand;
46  import ui.command.creation.ShowUserAddressBookDialogCommand;
47  import ui.command.creation.ShowUserDocumentDialogCommand;
48  import ui.dialog.DocumentDialog;
49  import base.user.Document;
50  import base.user.User;
51  import base.user.UserCredential;
52  import base.user.UserFactory;
53  import base.user.UserGender;
54  
55  import com.toedter.calendar.JDateChooser;
56  
57  @SuppressWarnings("serial")//$NON-NLS-1$
58  public class UserPanel extends JPanel {
59  
60  	private static final transient Logger logger = Logger
61  			.getLogger(UserPanel.class.getName());
62  
63  	private JPanel topPanel;
64  
65  	private JPanel bottomPanel;
66  
67  	private JPanel contentPanel;
68  
69  	private JPanel buttonPanel;
70  
71  	private JButton documentButton;
72  
73  	private JButton exportVCardButton;
74  
75  	private JButton addressBookButton;
76  
77  	private JPanel namePanel;
78  
79  	private JTextField nameTextField;
80  
81  	private JPanel surnamePanel;
82  
83  	private JTextField surnameTextField;
84  
85  	private JPanel birthdayPanel;
86  
87  	private JDateChooser birthdayDateChooser;
88  
89  	private JPanel imagePanel;
90  
91  	private JPanel nicknamePanel;
92  
93  	private JTextField nicknameTextField;
94  
95  	private JPanel passwordPanel;
96  
97  	private JPasswordField passwordField;
98  
99  	private JPanel retypedPasswordPanel;
100 
101 	private JPasswordField retypedPasswordField;
102 
103 	private JPanel credentialPanel;
104 
105 	private JComboBox credentialComboBox;
106 
107 	private JPanel genderPanel;
108 
109 	private JComboBox genderComboBox;
110 
111 	private final DocumentDialog documentDialog;
112 
113 	private final User user;
114 
115 	public UserPanel(User user) {
116 		this.user = user;
117 		documentDialog = new DocumentDialog(user == null ? user = UserFactory
118 				.newUser() : user, user.getDocument());
119 		initialize();
120 	}
121 
122 	protected void initialize() {
123 		this.setBorder(new TitledBorder(Messages.getString("common.user"))); //$NON-NLS-1$
124 		this.setLayout(new BorderLayout());
125 		this.add(getContentPanel(), BorderLayout.CENTER);
126 
127 	}
128 
129 	/***
130 	 * @return Returns the credentialPanel.
131 	 */
132 	protected JPanel getCredentialPanel() {
133 		if (credentialPanel == null) {
134 			credentialPanel = new JPanel();
135 			credentialPanel.setLayout(new BorderLayout());
136 			TitledBorder titledBorder = new TitledBorder(Messages
137 					.getString("common.credential")); //$NON-NLS-1$
138 			credentialPanel.setBorder(titledBorder);
139 			credentialPanel.add(getCredentialComboBox(), BorderLayout.CENTER);
140 		}
141 		return credentialPanel;
142 	}
143 
144 	/***
145 	 * @return Returns the credentialComboBox.
146 	 */
147 	protected JComboBox getCredentialComboBox() {
148 		if (credentialComboBox == null) {
149 			credentialComboBox = new JComboBox();
150 			for (int i = 0; i < UserCredential.USER_CREDENTIAL.length; i++)
151 				credentialComboBox.addItem(UserCredential.USER_CREDENTIAL[i]);
152 
153 			credentialComboBox
154 					.setSelectedItem(user == null ? UserCredential.USER_CREDENTIAL[UserCredential.USER]
155 							: user.getCredential());
156 		}
157 		return credentialComboBox;
158 	}
159 
160 	/***
161 	 * @return Returns the genderPanel.
162 	 */
163 	protected JPanel getGenderPanel() {
164 		if (genderPanel == null) {
165 			genderPanel = new JPanel();
166 			genderPanel.setLayout(new BorderLayout());
167 			TitledBorder titledBorder = new TitledBorder(Messages
168 					.getString("common.gender")); //$NON-NLS-1$
169 			genderPanel.setBorder(titledBorder);
170 			genderPanel.add(getGenderComboBox(), BorderLayout.CENTER);
171 		}
172 		return genderPanel;
173 	}
174 
175 	/***
176 	 * @return Returns the genderComboBox.
177 	 */
178 	protected JComboBox getGenderComboBox() {
179 		if (genderComboBox == null) {
180 			genderComboBox = new JComboBox();
181 			for (int i = 0; i < UserGender.USER_GENDER.length; i++)
182 				genderComboBox.addItem(UserGender.USER_GENDER[i]);
183 			genderComboBox
184 					.setSelectedItem(user == null ? UserGender.USER_GENDER[UserGender.MALE]
185 							: user.getGender());
186 		}
187 		return genderComboBox;
188 	}
189 
190 	/***
191 	 * @return Returns the namePanel.
192 	 */
193 	protected JPanel getNamePanel() {
194 		if (namePanel == null) {
195 			namePanel = new JPanel();
196 			namePanel.setPreferredSize(new Dimension(100, 30));
197 			namePanel.setLayout(new BorderLayout());
198 			TitledBorder titledBorder = new TitledBorder(Messages
199 					.getString("common.name")); //$NON-NLS-1$
200 			namePanel.setBorder(titledBorder);
201 			namePanel.add(getNameTextField(), BorderLayout.CENTER);
202 		}
203 		return namePanel;
204 	}
205 
206 	/***
207 	 * @return Returns the nameTextField.
208 	 */
209 	protected JTextField getNameTextField() {
210 		if (nameTextField == null) {
211 			nameTextField = new JTextField();
212 			nameTextField.setText(user == null ? "" : user.getName()); //$NON-NLS-1$
213 			nameTextField.setDragEnabled(true);
214 		}
215 		return nameTextField;
216 	}
217 
218 	/***
219 	 * @return Returns the nicknamePanel.
220 	 */
221 	protected JPanel getNicknamePanel() {
222 		if (nicknamePanel == null) {
223 			nicknamePanel = new JPanel();
224 			nicknamePanel.setLayout(new BorderLayout());
225 			TitledBorder titledBorder = new TitledBorder(Messages
226 					.getString("common.nickname")); //$NON-NLS-1$
227 			nicknamePanel.setBorder(titledBorder);
228 			nicknamePanel.add(getNicknameTextField(), BorderLayout.CENTER);
229 		}
230 		return nicknamePanel;
231 	}
232 
233 	/***
234 	 * @return Returns the nicknameTextField.
235 	 */
236 	protected JTextField getNicknameTextField() {
237 		if (nicknameTextField == null) {
238 			nicknameTextField = new JTextField();
239 			nicknameTextField.setText(user == null ? "" : user.getNickname()); //$NON-NLS-1$
240 		}
241 		return nicknameTextField;
242 	}
243 
244 	/***
245 	 * @return Returns the passwordPanel.
246 	 */
247 	protected JPanel getPasswordPanel() {
248 		if (passwordPanel == null) {
249 			passwordPanel = new JPanel();
250 			passwordPanel.setLayout(new BorderLayout());
251 			TitledBorder titledBorder = new TitledBorder(Messages
252 					.getString("common.password")); //$NON-NLS-1$
253 			passwordPanel.setBorder(titledBorder);
254 			passwordPanel.add(getPasswordField(), BorderLayout.CENTER);
255 		}
256 		return passwordPanel;
257 	}
258 
259 	/***
260 	 * @return Returns the passwordField.
261 	 */
262 	protected JPasswordField getPasswordField() {
263 		if (passwordField == null) {
264 			passwordField = new JPasswordField();
265 			passwordField.setText(user == null ? "" : user.getPassword()); //$NON-NLS-1$
266 		}
267 		return passwordField;
268 	}
269 
270 	/***
271 	 * @return Returns the retypedPasswordPanel.
272 	 */
273 	protected JPanel getRetypedPasswordPanel() {
274 		if (retypedPasswordPanel == null) {
275 			retypedPasswordPanel = new JPanel();
276 			retypedPasswordPanel.setLayout(new BorderLayout());
277 			TitledBorder titledBorder = new TitledBorder(Messages
278 					.getString("common.password.retype")); //$NON-NLS-1$
279 			retypedPasswordPanel.setBorder(titledBorder);
280 			retypedPasswordPanel.add(getRetypedPasswordField(),
281 					BorderLayout.CENTER);
282 		}
283 		return retypedPasswordPanel;
284 	}
285 
286 	/***
287 	 * @return Returns the retypedPasswordField.
288 	 */
289 	protected JPasswordField getRetypedPasswordField() {
290 		if (retypedPasswordField == null) {
291 			retypedPasswordField = new JPasswordField();
292 			retypedPasswordField
293 					.setText(user == null ? "" : user.getPassword()); //$NON-NLS-1$
294 		}
295 		return retypedPasswordField;
296 	}
297 
298 	/***
299 	 * @return Returns the surnamePanel.
300 	 */
301 	protected JPanel getSurnamePanel() {
302 		if (surnamePanel == null) {
303 			surnamePanel = new JPanel();
304 			surnamePanel.setLayout(new BorderLayout());
305 			TitledBorder titledBorder = new TitledBorder(Messages
306 					.getString("common.surname")); //$NON-NLS-1$
307 			surnamePanel.setBorder(titledBorder);
308 			surnamePanel.add(getSurnameTextField(), BorderLayout.CENTER);
309 		}
310 		return surnamePanel;
311 	}
312 
313 	/***
314 	 * @return Returns the surnameTextField.
315 	 */
316 	protected JTextField getSurnameTextField() {
317 		if (surnameTextField == null) {
318 			surnameTextField = new JTextField();
319 			surnameTextField.setText(user == null ? "" : user.getSurname()); //$NON-NLS-1$
320 		}
321 		return surnameTextField;
322 	}
323 
324 	/***
325 	 * @return Returns the contentPanel.
326 	 */
327 	protected JPanel getContentPanel() {
328 		if (contentPanel == null) {
329 			contentPanel = new JPanel();
330 			contentPanel.setLayout(new GridLayout(2, 1));
331 			contentPanel.add(getTopPanel());
332 			contentPanel.add(getBottomPanel());
333 		}
334 		return contentPanel;
335 	}
336 
337 	/***
338 	 * @return Returns the birthdayPanel.
339 	 */
340 	protected JPanel getBirthdayPanel() {
341 		if (birthdayPanel == null) {
342 			birthdayPanel = new JPanel();
343 			TitledBorder titledBorder = new TitledBorder(Messages
344 					.getString("common.birthday")); //$NON-NLS-1$
345 			birthdayPanel.setBorder(titledBorder);
346 			birthdayPanel.setLayout(new BorderLayout());
347 			birthdayPanel.add(getBirthdayDateChooser());
348 		}
349 		return birthdayPanel;
350 	}
351 
352 	/***
353 	 * @return Returns the birthdayDateChooser.
354 	 */
355 	protected JDateChooser getBirthdayDateChooser() {
356 		if (birthdayDateChooser == null) {
357 			birthdayDateChooser = new JDateChooser();
358 			birthdayDateChooser.setDate(user == null ? new Date() : user
359 					.getBirthday());
360 		}
361 		return birthdayDateChooser;
362 	}
363 
364 	/***
365 	 * @return Returns the imagePanel.
366 	 */
367 	protected JPanel getImagePanel() {
368 		if (imagePanel == null) {
369 			imagePanel = new ImagePanel(user == null ? "" : user.getImagePath()); //$NON-NLS-1$
370 			TitledBorder titledBorder = new TitledBorder(Messages
371 					.getString("common.photo")); //$NON-NLS-1$
372 			imagePanel.setBorder(titledBorder);
373 		}
374 		return imagePanel;
375 	}
376 
377 	/***
378 	 * @return Returns the buttonPanel.
379 	 */
380 	protected JPanel getButtonPanel() {
381 		if (buttonPanel == null) {
382 			buttonPanel = new JPanel();
383 			buttonPanel.setBorder(new EtchedBorder());
384 			buttonPanel.setLayout(new GridLayout(5, 1));
385 			buttonPanel.add(new JLabel("")); //$NON-NLS-1$
386 			buttonPanel.add(getDocumentButton());
387 			buttonPanel.add(getAddressBookButton());
388 			buttonPanel.add(getExportVCardButton());
389 			buttonPanel.add(new JLabel("")); //$NON-NLS-1$
390 		}
391 		return buttonPanel;
392 	}
393 
394 	public void clearUserData() {
395 		getNameTextField().setText(""); //$NON-NLS-1$
396 		getSurnameTextField().setText(""); //$NON-NLS-1$
397 		getBirthdayDateChooser().setDate(new Date());
398 		getNicknameTextField().setText(""); //$NON-NLS-1$
399 		getPasswordField().setText(""); //$NON-NLS-1$
400 		getRetypedPasswordField().setText(""); //$NON-NLS-1$
401 	}
402 
403 	/***
404 	 * @return Returns the documentButton.
405 	 */
406 	protected JButton getDocumentButton() {
407 		if (documentButton == null) {
408 			documentButton = new JButton();
409 			documentButton.setToolTipText(Messages
410 					.getString("panel.userpanel.userdocument")); //$NON-NLS-1$
411 			documentButton.setIcon(new ImageIcon(this.getClass().getResource(
412 					"/icon/16x16/apps/accessories-text-editor.png"))); //$NON-NLS-1$
413 
414 			documentButton.addActionListener(new ActionListener() {
415 				public void actionPerformed(ActionEvent arg0) {
416 					logger.debug("actionPerformed documentButton"); //$NON-NLS-1$
417 					// If there was an error on the document we must reset it...
418 					setUserDocumentError(false);
419 					ShowUserDocumentDialogCommand showUserDocumentDialogCommand = new ShowUserDocumentDialogCommand(
420 							documentDialog, user);
421 					CommandExecutor.getInstance().executeCommand(
422 							showUserDocumentDialogCommand, false);
423 				}
424 			});
425 
426 		}
427 		return documentButton;
428 	}
429 
430 	public String getUserName() {
431 		return getNameTextField().getText();
432 	}
433 
434 	public String getUserSurname() {
435 		return getSurnameTextField().getText();
436 	}
437 
438 	public Date getUserBirthday() {
439 		return getBirthdayDateChooser().getDate();
440 	}
441 
442 	public String getUserNickname() {
443 		return getNicknameTextField().getText();
444 	}
445 
446 	public String getUserPassword() {
447 		return new String(getPasswordField().getPassword());
448 	}
449 
450 	public String getRetypedPassword() {
451 		return new String(getRetypedPasswordField().getPassword());
452 	}
453 
454 	public String getUserCredential() {
455 		return (String) getCredentialComboBox().getSelectedItem();
456 	}
457 
458 	public String getUserGender() {
459 		return (String) getGenderComboBox().getSelectedItem();
460 	}
461 
462 	public String getUserImagePath() {
463 		return ((ImagePanel) getImagePanel()).getImagePath();
464 	}
465 
466 	public Document getUserDocument() {
467 		return documentDialog == null ? null : documentDialog.getDocument();
468 	}
469 
470 	public void setUserNameError(boolean isError) {
471 		if (isError)
472 			((TitledBorder) this.getNamePanel().getBorder())
473 					.setTitleColor(Color.RED);
474 		else
475 			((TitledBorder) this.getNamePanel().getBorder())
476 					.setTitleColor(new TitledBorder("").getTitleColor()); //$NON-NLS-1$
477 		this.getNamePanel().updateUI();
478 	}
479 
480 	public void setUserSurnameError(boolean isError) {
481 		if (isError)
482 			((TitledBorder) this.getSurnamePanel().getBorder())
483 					.setTitleColor(Color.RED);
484 		else
485 			((TitledBorder) this.getSurnamePanel().getBorder())
486 					.setTitleColor(new TitledBorder("").getTitleColor()); //$NON-NLS-1$
487 		this.getSurnamePanel().updateUI();
488 	}
489 
490 	public void setUserBirthdayError(boolean isError) {
491 		if (isError)
492 			((TitledBorder) this.getBirthdayPanel().getBorder())
493 					.setTitleColor(Color.RED);
494 		else
495 			((TitledBorder) this.getBirthdayPanel().getBorder())
496 					.setTitleColor(new TitledBorder("").getTitleColor()); //$NON-NLS-1$
497 		this.getBirthdayPanel().updateUI();
498 	}
499 
500 	public void setUserNicknameError(boolean isError) {
501 		if (isError)
502 			((TitledBorder) this.getNicknamePanel().getBorder())
503 					.setTitleColor(Color.RED);
504 		else
505 			((TitledBorder) this.getNicknamePanel().getBorder())
506 					.setTitleColor(new TitledBorder("").getTitleColor()); //$NON-NLS-1$
507 		this.getNicknamePanel().updateUI();
508 	}
509 
510 	public void setUserPasswordError(boolean isError) {
511 		if (isError) {
512 			((TitledBorder) this.getPasswordPanel().getBorder())
513 					.setTitleColor(Color.RED);
514 			((TitledBorder) this.getRetypedPasswordPanel().getBorder())
515 					.setTitleColor(Color.RED);
516 		} else {
517 			((TitledBorder) this.getPasswordPanel().getBorder())
518 					.setTitleColor(new TitledBorder("").getTitleColor()); //$NON-NLS-1$
519 			((TitledBorder) this.getRetypedPasswordPanel().getBorder())
520 					.setTitleColor(new TitledBorder("").getTitleColor()); //$NON-NLS-1$
521 		}
522 		this.getPasswordPanel().updateUI();
523 		this.getRetypedPasswordPanel().updateUI();
524 	}
525 
526 	public void setUserDocumentError(boolean isError) {
527 		if (isError)
528 			this.getDocumentButton().setBackground(Color.RED);
529 		else
530 			this.getDocumentButton().setBackground(
531 					new JButton().getBackground());
532 
533 	}
534 
535 	/***
536 	 * @return Returns the addressBookButton.
537 	 */
538 	protected JButton getAddressBookButton() {
539 		if (addressBookButton == null) {
540 			addressBookButton = new JButton();
541 			addressBookButton.setToolTipText(Messages
542 					.getString("common.addressbook")); //$NON-NLS-1$
543 			addressBookButton.setIcon(new ImageIcon(this.getClass()
544 					.getResource("/icon/16x16/actions/address-book-new.png"))); //$NON-NLS-1$
545 
546 			addressBookButton.addActionListener(new ActionListener() {
547 				public void actionPerformed(ActionEvent arg0) {
548 					logger.debug("actionPerformed documentButton"); //$NON-NLS-1$
549 					CommandExecutor.getInstance().executeCommand(
550 							new ShowUserAddressBookDialogCommand(user), false);
551 				}
552 			});
553 		}
554 		return addressBookButton;
555 	}
556 
557 	/***
558 	 * @return Returns the exportVCardButton.
559 	 */
560 	protected JButton getExportVCardButton() {
561 		if (exportVCardButton == null) {
562 			exportVCardButton = new JButton();
563 			exportVCardButton.setToolTipText(Messages
564 					.getString("common.export.vcard")); //$NON-NLS-1$
565 			exportVCardButton.setIcon(new ImageIcon(this.getClass()
566 					.getResource("/icon/16x16/actions/contact-new.png"))); //$NON-NLS-1$
567 
568 			exportVCardButton.addActionListener(new ActionListener() {
569 				public void actionPerformed(ActionEvent arg0) {
570 					logger.debug("actionPerformed exportVCardButton"); //$NON-NLS-1$
571 					CommandExecutor.getInstance().executeCommand(
572 							new ExportUserAsVCardCommand(user), false);
573 				}
574 			});
575 		}
576 		return exportVCardButton;
577 	}
578 
579 	/***
580 	 * @return Returns the bottomPanel.
581 	 */
582 	protected JPanel getBottomPanel() {
583 		if (bottomPanel == null) {
584 			bottomPanel = new JPanel();
585 			bottomPanel.setLayout(new BorderLayout());
586 
587 			JPanel bottomLeftPanel = new JPanel();
588 			bottomLeftPanel.setLayout(new GridLayout(4, 1));
589 			bottomLeftPanel.add(getNicknamePanel());
590 			bottomLeftPanel.add(getPasswordPanel());
591 			bottomLeftPanel.add(getRetypedPasswordPanel());
592 			bottomLeftPanel.add(getCredentialPanel());
593 
594 			JPanel bottomRightPanel = new JPanel();
595 			bottomRightPanel.setLayout(new BorderLayout());
596 			bottomRightPanel.add(getButtonPanel(), BorderLayout.CENTER);
597 
598 			bottomPanel.add(bottomLeftPanel, BorderLayout.CENTER);
599 			bottomPanel.add(bottomRightPanel, BorderLayout.EAST);
600 		}
601 		return bottomPanel;
602 	}
603 
604 	/***
605 	 * @return Returns the topPanel.
606 	 */
607 	protected JPanel getTopPanel() {
608 		if (topPanel == null) {
609 			topPanel = new JPanel();
610 			topPanel.setLayout(new GridLayout(1, 2));
611 			JPanel topLeftPanel = new JPanel();
612 			topLeftPanel.setLayout(new GridLayout(4, 1));
613 			topLeftPanel.add(getNamePanel(), null);
614 			topLeftPanel.add(getSurnamePanel(), null);
615 			topLeftPanel.add(getGenderPanel(), null);
616 			topLeftPanel.add(getBirthdayPanel(), null);
617 
618 			JPanel topRightPanel = new JPanel();
619 			topRightPanel.setLayout(new BorderLayout());
620 			topRightPanel.add(getImagePanel(), BorderLayout.CENTER);
621 
622 			JSplitPane splitPane = new JSplitPane();
623 			splitPane.setLeftComponent(topLeftPanel);
624 			splitPane.setRightComponent(topRightPanel);
625 			splitPane.setOneTouchExpandable(true);
626 			topPanel.add(splitPane);
627 
628 		}
629 		return topPanel;
630 	}
631 }