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.dialog;
21  
22  import java.awt.BorderLayout;
23  import java.awt.Dimension;
24  import java.awt.GridLayout;
25  import java.awt.Toolkit;
26  import java.awt.event.ActionEvent;
27  import java.awt.event.ActionListener;
28  
29  import javax.swing.ImageIcon;
30  import javax.swing.JButton;
31  import javax.swing.JDialog;
32  import javax.swing.JLabel;
33  import javax.swing.JPanel;
34  import javax.swing.border.TitledBorder;
35  
36  import org.apache.log4j.Logger;
37  
38  import ui.Messages;
39  import ui.command.CommandExecutor;
40  import ui.command.IO.SaveUserCommand;
41  import ui.panel.UserPanel;
42  import base.user.User;
43  
44  @SuppressWarnings("serial") //$NON-NLS-1$
45  public class UserDialog extends JDialog {
46  
47  	private static final transient Logger logger = Logger
48  			.getLogger(UserDialog.class.getName());
49  
50  	private JPanel contentPanel;
51  
52  	private UserPanel userPanel;
53  
54  	private JPanel buttonPanel;
55  
56  	private JButton saveUserButton;
57  
58  	private JButton clearUserDataButton;
59  
60  	private User user;
61  
62  	private boolean createUser = false;
63  
64  	public UserDialog(User user) {
65  		this.user = user;
66  		if (user == null)
67  			createUser = true;
68  		initialize();
69  	}
70  
71  	public UserDialog() {
72  		createUser = true;
73  		initialize();
74  	}
75  
76  	protected void initialize() {
77  		this.setSize(800, 600);
78  		this.setResizable(false);
79  		// Center the dialog
80  		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
81  		Dimension frameSize = this.getSize();
82  		if (frameSize.height > screenSize.height) {
83  			frameSize.height = screenSize.height;
84  		}
85  		if (frameSize.width > screenSize.width) {
86  			frameSize.width = screenSize.width;
87  		}
88  		this.setLocation((screenSize.width - frameSize.width) / 2,
89  				(screenSize.height - frameSize.height) / 2);
90  
91  		this.setContentPane(getContentPanel());
92  		this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
93  	}
94  
95  	/***
96  	 * @return Returns the contentPanel.
97  	 */
98  	protected JPanel getContentPanel() {
99  		if (contentPanel == null) {
100 			contentPanel = new JPanel();
101 			contentPanel.setLayout(new BorderLayout());
102 			contentPanel.add(getUserPanel(), BorderLayout.CENTER);
103 			contentPanel.add(getButtonPanel(), BorderLayout.SOUTH);
104 		}
105 		return contentPanel;
106 	}
107 
108 	/***
109 	 * @return Returns the userPanel.
110 	 */
111 	protected UserPanel getUserPanel() {
112 		if (userPanel == null) {
113 			userPanel = new UserPanel(getUser());
114 		}
115 		return userPanel;
116 	}
117 
118 	/***
119 	 * @return Returns the user.
120 	 */
121 	public User getUser() {
122 		return user;
123 	}
124 
125 	/***
126 	 * @return Returns the buttonPanel.
127 	 */
128 	protected JPanel getButtonPanel() {
129 		if (buttonPanel == null) {
130 			buttonPanel = new JPanel();
131 			TitledBorder titledBorder = new TitledBorder(Messages.getString("dialog.availableactions")); //$NON-NLS-1$
132 			buttonPanel.setBorder(titledBorder);
133 			buttonPanel.setLayout(new GridLayout(1, 4));
134 			buttonPanel.add(new JLabel("")); //$NON-NLS-1$
135 			buttonPanel.add(getSaveUserButton());
136 			buttonPanel.add(getClearUserDataButton());
137 			buttonPanel.add(new JLabel("")); //$NON-NLS-1$
138 		}
139 		return buttonPanel;
140 	}
141 
142 	/***
143 	 * @return Returns the clearUserDataButton.
144 	 */
145 	protected JButton getClearUserDataButton() {
146 		if (clearUserDataButton == null) {
147 			clearUserDataButton = new JButton(Messages.getString("button.clear")); //$NON-NLS-1$
148 			clearUserDataButton.setIcon(new ImageIcon(this.getClass()
149 					.getResource("/icon/16x16/actions/view-refresh.png"))); //$NON-NLS-1$
150 
151 			clearUserDataButton.addActionListener(new ActionListener() {
152 				public void actionPerformed(ActionEvent arg0) {
153 					logger.debug("actionPerformed clearUserDataButton"); //$NON-NLS-1$
154 					getUserPanel().clearUserData();
155 				}
156 			});
157 		}
158 		return clearUserDataButton;
159 	}
160 
161 	/***
162 	 * @return Returns the saveUserButton.
163 	 */
164 	protected JButton getSaveUserButton() {
165 		if (saveUserButton == null) {
166 			saveUserButton = new JButton(Messages.getString("button.save")); //$NON-NLS-1$
167 			saveUserButton.setIcon(new ImageIcon(this.getClass().getResource(
168 					"/icon/16x16/actions/document-save.png"))); //$NON-NLS-1$
169 
170 			saveUserButton.addActionListener(new ActionListener() {
171 				public void actionPerformed(ActionEvent arg0) {
172 					logger.debug("actionPerformed saveUserButton"); //$NON-NLS-1$
173 					SaveUserCommand saveUserCommand = new SaveUserCommand(
174 							getUserPanel(), user, createUser);
175 					CommandExecutor.getInstance().executeCommand(
176 							saveUserCommand, true);
177 					if (saveUserCommand.getStatus() == SaveUserCommand.SUCCESS_STATUS)
178 						setVisible(false);
179 				}
180 			});
181 		}
182 		return saveUserButton;
183 	}
184 }