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.Dimension;
24  import java.awt.GridLayout;
25  import java.awt.event.ActionEvent;
26  import java.awt.event.ActionListener;
27  
28  import javax.swing.ImageIcon;
29  import javax.swing.JButton;
30  import javax.swing.JLabel;
31  import javax.swing.JOptionPane;
32  import javax.swing.JPanel;
33  import javax.swing.JScrollPane;
34  import javax.swing.JTable;
35  import javax.swing.border.EtchedBorder;
36  import javax.swing.border.TitledBorder;
37  
38  import org.apache.log4j.Logger;
39  
40  import ui.Messages;
41  import ui.command.CommandExecutor;
42  import ui.command.creation.ShowNewUserDialogCommand;
43  import ui.command.deletion.DeleteUserCommand;
44  import ui.command.information.InfoOnUserCommand;
45  import ui.table.TableSorter;
46  import ui.table.UserTable;
47  import ui.table.model.UserTableModel;
48  
49  @SuppressWarnings("serial") //$NON-NLS-1$
50  public class ICUserPanel extends JPanel {
51  
52  	private static final transient Logger logger = Logger
53  			.getLogger(ICUserPanel.class.getName());
54  
55  	private JPanel userPanel;
56  
57  	private JPanel buttonPanel;
58  
59  	private JButton newUserButton;
60  
61  	private JButton deleteUserButton;
62  
63  	private JButton infoOnUserButton;
64  
65  	private JScrollPane userTableScrollPane;
66  
67  	private JTable userTable;
68  
69  	private TableSorter userTableSorter;
70  
71  	private UserTableModel userTableModel;
72  
73  	public ICUserPanel() {
74  		initialize();
75  	}
76  
77  	protected void initialize() {
78  		this.setName(Messages.getString("common.users")); //$NON-NLS-1$
79  		this.setLayout(new BorderLayout());
80  		this.add(getUserPanel(), BorderLayout.CENTER);
81  		this.add(getButtonPanel(), BorderLayout.SOUTH);
82  	}
83  
84  	/***
85  	 * @return Returns the buttonPanel.
86  	 */
87  	protected JPanel getButtonPanel() {
88  		if (buttonPanel == null) {
89  			buttonPanel = new JPanel();
90  			buttonPanel.setLayout(new GridLayout(1, 5));
91  			buttonPanel.setBorder(new EtchedBorder());
92  			buttonPanel.add(new JLabel("")); //$NON-NLS-1$
93  			buttonPanel.add(getNewUserButton());
94  			buttonPanel.add(getInfoOnUserButton());
95  			buttonPanel.add(getDeleteUserButton());
96  			buttonPanel.add(new JLabel("")); //$NON-NLS-1$
97  
98  		}
99  		return buttonPanel;
100 	}
101 
102 	/***
103 	 * @return Returns the userPanel.
104 	 */
105 	protected JPanel getUserPanel() {
106 		if (userPanel == null) {
107 			userPanel = new JPanel();
108 			userPanel.setLayout(new BorderLayout());
109 			TitledBorder titledBorder = new TitledBorder(Messages.getString("common.registeredusers")); //$NON-NLS-1$
110 			userPanel.setBorder(titledBorder);
111 			userPanel.add(getUserTableScrollPane(), BorderLayout.CENTER);
112 		}
113 		return userPanel;
114 	}
115 
116 	/***
117 	 * @return Returns the userTable.
118 	 */
119 	protected JTable getUserTable() {
120 		if (userTable == null) {
121 			userTable = new UserTable(getUserTableSorter());
122 			userTable
123 					.setPreferredScrollableViewportSize(new Dimension(500, 70));
124 			// We need to place it here to avoid a ciclyc call...
125 			userTableSorter.setTableHeader(userTable.getTableHeader());
126 			// Set up tool tips for column headers.
127 			userTable
128 					.getTableHeader()
129 					.setToolTipText(
130 							Messages.getString("tablesorter.header.tooltip")); //$NON-NLS-1$
131 		}
132 		return userTable;
133 	}
134 
135 	/***
136 	 * @return Returns the userTableScrollPane.
137 	 */
138 	protected JScrollPane getUserTableScrollPane() {
139 		if (userTableScrollPane == null) {
140 			userTableScrollPane = new JScrollPane(getUserTable());
141 		}
142 		return userTableScrollPane;
143 	}
144 
145 	/***
146 	 * @return Returns the userTableSorter.
147 	 */
148 	protected TableSorter getUserTableSorter() {
149 		if (userTableSorter == null) {
150 			userTableSorter = new TableSorter(getUserTableModel());
151 		}
152 		return userTableSorter;
153 	}
154 
155 	/***
156 	 * @return Returns the userTableModel.
157 	 */
158 	protected UserTableModel getUserTableModel() {
159 		if (userTableModel == null) {
160 			userTableModel = new UserTableModel();
161 		}
162 		return userTableModel;
163 	}
164 
165 	/***
166 	 * @return Returns the deleteUserButton.
167 	 */
168 	protected JButton getDeleteUserButton() {
169 		if (deleteUserButton == null) {
170 			deleteUserButton = new JButton(Messages.getString("button.delete")); //$NON-NLS-1$
171 			deleteUserButton.setIcon(new ImageIcon(this.getClass().getResource(
172 					"/icon/16x16/actions/edit-delete.png"))); //$NON-NLS-1$
173 
174 			deleteUserButton.addActionListener(new ActionListener() {
175 				public void actionPerformed(ActionEvent arg0) {
176 					logger.debug("actionPerformed deleteButton"); //$NON-NLS-1$
177 					if (getUserTable().getSelectedRow() != -1) {
178 						int userId = Integer
179 								.parseInt(((TableSorter) getUserTable()
180 										.getModel()).getValueAt(
181 										getUserTable().getSelectedRow(),
182 										UserTableModel.USER_ID_COLUMN)
183 										.toString());
184 						CommandExecutor.getInstance().executeCommand(
185 								new DeleteUserCommand(userId), false);
186 					} else
187 						JOptionPane
188 								.showMessageDialog(
189 										null,
190 										Messages.getString("panel.icuserpanle.message1"), //$NON-NLS-1$
191 										Messages.getString("common.warning"), JOptionPane.WARNING_MESSAGE); //$NON-NLS-1$
192 				}
193 			});
194 		}
195 		return deleteUserButton;
196 	}
197 
198 	/***
199 	 * @return Returns the infoOnUserButton.
200 	 */
201 	protected JButton getInfoOnUserButton() {
202 		if (infoOnUserButton == null) {
203 			infoOnUserButton = new JButton(Messages.getString("button.info")); //$NON-NLS-1$
204 			infoOnUserButton.setIcon(new ImageIcon(this.getClass().getResource(
205 					"/icon/16x16/status/dialog-information.png"))); //$NON-NLS-1$
206 
207 			infoOnUserButton.addActionListener(new ActionListener() {
208 				public void actionPerformed(ActionEvent arg0) {
209 					logger.debug("actionPerformed infoButton"); //$NON-NLS-1$
210 					if (getUserTable().getSelectedRow() != -1) {
211 						int userId = Integer
212 								.parseInt(((TableSorter) getUserTable()
213 										.getModel()).getValueAt(
214 										getUserTable().getSelectedRow(),
215 										UserTableModel.USER_ID_COLUMN)
216 										.toString());
217 						CommandExecutor.getInstance().executeCommand(
218 								new InfoOnUserCommand(userId), false);
219 					} else
220 						JOptionPane
221 								.showMessageDialog(
222 										null,
223 										Messages.getString("panel.icuserpanel.message1"), //$NON-NLS-1$
224 										Messages.getString("common.warning"), JOptionPane.WARNING_MESSAGE); //$NON-NLS-1$
225 				}
226 			});
227 		}
228 		return infoOnUserButton;
229 	}
230 
231 	/***
232 	 * @return Returns the newUserButton.
233 	 */
234 	protected JButton getNewUserButton() {
235 		if (newUserButton == null) {
236 			newUserButton = new JButton(Messages.getString("button.new")); //$NON-NLS-1$
237 			newUserButton.setIcon(new ImageIcon(this.getClass().getResource(
238 					"/icon/16x16/actions/contact-new.png"))); //$NON-NLS-1$
239 
240 			newUserButton.addActionListener(new ActionListener() {
241 				public void actionPerformed(ActionEvent arg0) {
242 					logger.debug("actionPerformed newUserButton"); //$NON-NLS-1$
243 					CommandExecutor.getInstance().executeCommand(
244 							new ShowNewUserDialogCommand(), false);
245 				}
246 			});
247 		}
248 		return newUserButton;
249 	}
250 
251 }