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.GridLayout;
24  import java.awt.event.ActionEvent;
25  import java.awt.event.ActionListener;
26  import java.util.Date;
27  
28  import javax.swing.ImageIcon;
29  import javax.swing.JButton;
30  import javax.swing.JLabel;
31  import javax.swing.JPanel;
32  import javax.swing.JScrollPane;
33  import javax.swing.JTextArea;
34  import javax.swing.JTextField;
35  import javax.swing.border.TitledBorder;
36  
37  import org.apache.log4j.Logger;
38  
39  import ui.Messages;
40  import ui.command.CommandExecutor;
41  import ui.command.information.InfoOnClientWorkstationCommand;
42  import ui.command.information.InfoOnServiceCommand;
43  import ui.command.information.InfoOnUserCommand;
44  import base.service.Service;
45  import base.session.Session;
46  
47  @SuppressWarnings("serial") //$NON-NLS-1$
48  public class SessionPanel extends JPanel {
49  
50  	private static final transient Logger logger = Logger
51  			.getLogger(SessionPanel.class.getName());
52  
53  	private JPanel topPanel;
54  
55  	private JPanel bottomPanel;
56  
57  	private JPanel descriptionPanel;
58  
59  	private JPanel startTimePanel;
60  
61  	private JPanel endTimePanel;
62  
63  	private JPanel servicePanel;
64  
65  	private JButton infoOnServiceButton;
66  
67  	private JPanel workstationPanel;
68  
69  	private JButton infoOnWorkstationButton;
70  
71  	private JPanel userPanel;
72  
73  	private JButton infoOnUserButton;
74  
75  	private final Session session;
76  
77  	private JTextField descriptionTextField;
78  
79  	public SessionPanel(Session session) {
80  		this.session = session;
81  		initialize();
82  	}
83  
84  	public String getSessionDescription() {
85  		return this.getDescriptionTextField().getText();
86  	}
87  
88  	/***
89  	 * @return Returns the topPanel.
90  	 */
91  	protected JPanel getTopPanel() {
92  		if (topPanel == null) {
93  			topPanel = new JPanel();
94  			topPanel.setLayout(new GridLayout(1, 2));
95  			JPanel leftPanel = new JPanel();
96  			leftPanel.setLayout(new GridLayout(2, 1));
97  			leftPanel.add(getUserPanel(), null);
98  			leftPanel.add(getWorkstationPanel(), null);
99  			topPanel.add(leftPanel, null);
100 			topPanel.add(getServicePanel(), null);
101 		}
102 		return topPanel;
103 	}
104 
105 	protected void initialize() {
106 		this.setLayout(new GridLayout(2, 1));
107 		this.add(getTopPanel(), null);
108 		this.add(getBottomPanel(), null);
109 	}
110 
111 	/***
112 	 * @return Returns the descriptionPanel.
113 	 */
114 	protected JPanel getDescriptionPanel() {
115 		if (descriptionPanel == null) {
116 			descriptionPanel = new JPanel();
117 			TitledBorder titledBorder = new TitledBorder(Messages.getString("common.description")); //$NON-NLS-1$
118 			descriptionPanel.setBorder(titledBorder);
119 			descriptionPanel.setLayout(new BorderLayout());
120 			descriptionPanel
121 					.add(getDescriptionTextField(), BorderLayout.CENTER);
122 		}
123 		return descriptionPanel;
124 	}
125 
126 	/***
127 	 * @return Returns the descriptionTextField.
128 	 */
129 	protected JTextField getDescriptionTextField() {
130 		if (descriptionTextField == null) {
131 			descriptionTextField = new JTextField(session == null ? "" //$NON-NLS-1$
132 					: session.getDescription());
133 		}
134 		return descriptionTextField;
135 	}
136 
137 	/***
138 	 * @return Returns the endTimePanel.
139 	 */
140 	protected JPanel getEndTimePanel() {
141 		if (endTimePanel == null) {
142 			endTimePanel = new JPanel();
143 			endTimePanel.setLayout(new BorderLayout());
144 			endTimePanel.setBorder(new TitledBorder(Messages.getString("common.endtime"))); //$NON-NLS-1$
145 			JTextField textField = new JTextField();
146 			textField
147 					.setText(session == null || session.getEndTime() == null ? Messages.getString("common.notyetavailable") //$NON-NLS-1$
148 							: session.getEndTime().toString());
149 			textField.setEditable(false);
150 			endTimePanel.add(textField, BorderLayout.CENTER);
151 		}
152 		return endTimePanel;
153 	}
154 
155 	/***
156 	 * @return Returns the session.
157 	 */
158 	protected Session getSession() {
159 		return session;
160 	}
161 
162 	/***
163 	 * @return Returns the startTimePanel.
164 	 */
165 	protected JPanel getStartTimePanel() {
166 		if (startTimePanel == null) {
167 			startTimePanel = new JPanel();
168 			startTimePanel.setLayout(new BorderLayout());
169 			startTimePanel.setBorder(new TitledBorder(Messages.getString("common.starttime"))); //$NON-NLS-1$
170 			JTextField textField = new JTextField();
171 			textField.setText(session == null ? new Date().toString() : session
172 					.getStartTime().toString());
173 			textField.setEditable(false);
174 			startTimePanel.add(textField, BorderLayout.CENTER);
175 		}
176 		return startTimePanel;
177 	}
178 
179 	/***
180 	 * @return Returns the userPanel.
181 	 */
182 	protected JPanel getUserPanel() {
183 		if (userPanel == null) {
184 			userPanel = new JPanel();
185 			TitledBorder titledBorder = new TitledBorder(Messages.getString("panel.sessionpanel.userinformations")); //$NON-NLS-1$
186 			userPanel.setBorder(titledBorder);
187 			JScrollPane scrollPane = new JScrollPane();
188 			JTextArea textArea = new JTextArea();
189 			scrollPane.setViewportView(textArea);
190 			textArea.setEditable(false);
191 			textArea.setText(session != null ? Messages.getString("common.id")+": " //$NON-NLS-1$ //$NON-NLS-2$
192 					+ session.getUser().getId() + "\n"+Messages.getString("common.who")+": " //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
193 					+ session.getUser().getName() + " " //$NON-NLS-1$
194 					+ session.getUser().getSurname() + "\n"+Messages.getString("common.nickname")+": " //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
195 					+ session.getUser().getNickname() + "\n"+Messages.getString("common.credential")+": " //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
196 					+ session.getUser().getCredential() : ""); //$NON-NLS-1$
197 			textArea.setCaretPosition(0);
198 			userPanel.setLayout(new BorderLayout());
199 			userPanel.add(scrollPane, BorderLayout.CENTER);
200 			JPanel buttonPanel = new JPanel();
201 			buttonPanel.setLayout(new GridLayout(1, 3));
202 			buttonPanel.add(new JLabel("")); //$NON-NLS-1$
203 			buttonPanel.add(getInfoOnUserButton());
204 			buttonPanel.add(new JLabel("")); //$NON-NLS-1$
205 			userPanel.add(buttonPanel, BorderLayout.SOUTH);
206 		}
207 		return userPanel;
208 	}
209 
210 	/***
211 	 * @return Returns the workstationPanel.
212 	 */
213 	protected JPanel getWorkstationPanel() {
214 		if (workstationPanel == null) {
215 			workstationPanel = new JPanel();
216 			TitledBorder titledBorder = new TitledBorder(
217 					Messages.getString("panel.sessionpanel.workstationinformations")); //$NON-NLS-1$
218 			workstationPanel.setBorder(titledBorder);
219 			JScrollPane scrollPane = new JScrollPane();
220 			JTextArea textArea = new JTextArea();
221 			scrollPane.setViewportView(textArea);
222 			textArea.setEditable(false);
223 			textArea.setText(session != null ? Messages.getString("common.id")+": " //$NON-NLS-1$ //$NON-NLS-2$
224 					+ session.getWorkstation().getId() + "\n"+Messages.getString("common.name")+": " //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
225 					+ session.getWorkstation().getName() + "\n"+Messages.getString("common.type")+": " //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
226 					+ session.getWorkstation().getType() + "\n"+Messages.getString("common.location")+": " //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
227 					+ session.getWorkstation().getLocation() : ""); //$NON-NLS-1$
228 			textArea.setCaretPosition(0);
229 			workstationPanel.setLayout(new BorderLayout());
230 			workstationPanel.add(scrollPane, BorderLayout.CENTER);
231 			JPanel buttonPanel = new JPanel();
232 			buttonPanel.setLayout(new GridLayout(1, 3));
233 			buttonPanel.add(new JLabel("")); //$NON-NLS-1$
234 			buttonPanel.add(getInfoOnWorkstationButton());
235 			buttonPanel.add(new JLabel("")); //$NON-NLS-1$
236 			workstationPanel.add(buttonPanel, BorderLayout.SOUTH);
237 		}
238 		return workstationPanel;
239 	}
240 
241 	/***
242 	 * @return Returns the servicePanel.
243 	 */
244 	protected JPanel getServicePanel() {
245 		if (servicePanel == null) {
246 			servicePanel = new JPanel();
247 			TitledBorder titledBorder = new TitledBorder(Messages.getString("panel.sessionpanel.serviceinformations")); //$NON-NLS-1$
248 			servicePanel.setBorder(titledBorder);
249 
250 			JScrollPane scrollPane = new JScrollPane();
251 			JTextArea textArea = new JTextArea();
252 			scrollPane.setViewportView(textArea);
253 			textArea.setEditable(false);
254 			StringBuffer sb = new StringBuffer();
255 			if (session != null) {
256 				sb.append(Messages.getString("common.id")+": " + session.getService().getId() + "\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
257 				sb.append(Messages.getString("common.name")+": " + session.getService().getName() + "\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
258 				sb.append(Messages.getString("common.ratetype")+": " + session.getService().getRateType() //$NON-NLS-1$ //$NON-NLS-2$
259 						+ "\n"); //$NON-NLS-1$
260 				sb.append(Messages.getString("common.baserate")+": " + session.getService().getBaseRate() //$NON-NLS-1$ //$NON-NLS-2$
261 						+ "\n"); //$NON-NLS-1$
262 				sb.append(Messages.getString("common.duration")+": " //$NON-NLS-1$ //$NON-NLS-2$
263 						+ Service.durationInMinute(session.getStartTime(),
264 								session.getEndTime()) + ""+Messages.getString("common.unit.minute") + "\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
265 				sb.append(Messages.getString("common.cost")+": " //$NON-NLS-1$ //$NON-NLS-2$
266 						+ Service.cost(session.getStartTime(), session
267 								.getEndTime(), session.getService()
268 								.getRateType(), session.getService()
269 								.getBaseRate()) + "\n"); //$NON-NLS-1$
270 			}
271 			textArea.setText(sb.toString());
272 			textArea.setCaretPosition(0);
273 			servicePanel.setLayout(new BorderLayout());
274 			servicePanel.add(scrollPane, BorderLayout.CENTER);
275 			JPanel buttonPanel = new JPanel();
276 			buttonPanel.setLayout(new GridLayout(1, 3));
277 			buttonPanel.add(new JLabel("")); //$NON-NLS-1$
278 			buttonPanel.add(getInfoOnServiceButton());
279 			buttonPanel.add(new JLabel("")); //$NON-NLS-1$
280 			servicePanel.add(buttonPanel, BorderLayout.SOUTH);
281 		}
282 		return servicePanel;
283 	}
284 
285 	/***
286 	 * @return Returns the infoOnUserButton.
287 	 */
288 	protected JButton getInfoOnUserButton() {
289 		if (infoOnUserButton == null) {
290 			infoOnUserButton = new JButton();
291 			infoOnUserButton
292 					.setToolTipText(Messages.getString("panel.sessionpanel.message1")); //$NON-NLS-1$
293 			infoOnUserButton.setIcon(new ImageIcon(this.getClass().getResource(
294 					"/icon/16x16/status/dialog-information.png"))); //$NON-NLS-1$
295 
296 			infoOnUserButton.addActionListener(new ActionListener() {
297 				public void actionPerformed(ActionEvent arg0) {
298 					logger.debug("actionPerformed infoOnUserButton"); //$NON-NLS-1$
299 					CommandExecutor.getInstance().executeCommand(
300 							new InfoOnUserCommand(session.getUser().getId()),
301 							false);
302 				}
303 			});
304 		}
305 		return infoOnUserButton;
306 	}
307 
308 	/***
309 	 * @return Returns the infoOnServiceButton.
310 	 */
311 	protected JButton getInfoOnServiceButton() {
312 		if (infoOnServiceButton == null) {
313 			infoOnServiceButton = new JButton();
314 			infoOnServiceButton
315 					.setToolTipText(Messages.getString("panel.sessionpanel.message2")); //$NON-NLS-1$
316 			infoOnServiceButton.setIcon(new ImageIcon(this.getClass()
317 					.getResource("/icon/16x16/status/dialog-information.png"))); //$NON-NLS-1$
318 
319 			infoOnServiceButton.addActionListener(new ActionListener() {
320 				public void actionPerformed(ActionEvent arg0) {
321 					logger.debug("actionPerformed infoOnServiceButton"); //$NON-NLS-1$
322 					CommandExecutor.getInstance().executeCommand(
323 							new InfoOnServiceCommand(session.getService()
324 									.getId()), false);
325 				}
326 			});
327 		}
328 		return infoOnServiceButton;
329 	}
330 
331 	/***
332 	 * @return Returns the infoOnWorkstationButton.
333 	 */
334 	protected JButton getInfoOnWorkstationButton() {
335 		if (infoOnWorkstationButton == null) {
336 			infoOnWorkstationButton = new JButton();
337 			infoOnWorkstationButton
338 					.setToolTipText(Messages.getString("panel.sessionpanel.message3")); //$NON-NLS-1$
339 			infoOnWorkstationButton.setIcon(new ImageIcon(this.getClass()
340 					.getResource("/icon/16x16/status/dialog-information.png"))); //$NON-NLS-1$
341 
342 			infoOnWorkstationButton.addActionListener(new ActionListener() {
343 				public void actionPerformed(ActionEvent arg0) {
344 					logger.debug("actionPerformed infoOnWorkstationButton"); //$NON-NLS-1$
345 					CommandExecutor.getInstance().executeCommand(
346 							new InfoOnClientWorkstationCommand(session
347 									.getWorkstation().getId()), false);
348 				}
349 			});
350 		}
351 		return infoOnWorkstationButton;
352 	}
353 
354 	/***
355 	 * @return Returns the bottomPanel.
356 	 */
357 	protected JPanel getBottomPanel() {
358 		if (bottomPanel == null) {
359 			bottomPanel = new JPanel();
360 			bottomPanel.setLayout(new GridLayout(3, 1));
361 			bottomPanel.add(getDescriptionPanel(), null);
362 			bottomPanel.add(getStartTimePanel(), null);
363 			bottomPanel.add(getEndTimePanel(), null);
364 		}
365 		return bottomPanel;
366 	}
367 }