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.Color;
24  import java.awt.Dimension;
25  import java.awt.GridLayout;
26  import java.awt.Toolkit;
27  import java.awt.event.ActionEvent;
28  import java.awt.event.ActionListener;
29  import java.util.Date;
30  import java.util.GregorianCalendar;
31  
32  import javax.swing.ImageIcon;
33  import javax.swing.JButton;
34  import javax.swing.JComboBox;
35  import javax.swing.JDialog;
36  import javax.swing.JLabel;
37  import javax.swing.JPanel;
38  import javax.swing.JTextField;
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.SaveNewSessionCommand;
46  import ui.panel.SessionCreationPanel;
47  import base.InternetCafeManager;
48  import base.service.Service;
49  
50  import com.toedter.calendar.JTimeChooser;
51  
52  @SuppressWarnings("serial") //$NON-NLS-1$
53  public class NewSessionDialog extends JDialog {
54  
55  	private static final transient Logger logger = Logger
56  			.getLogger(NewSessionDialog.class.getName());
57  
58  	private JPanel contentPanel;
59  
60  	private SessionCreationPanel topPanel;
61  
62  	private JPanel buttonPanel;
63  
64  	private JButton saveSessionButton;
65  
66  	private JButton clearSessionDataButton;
67  
68  	private JPanel bottomPanel;
69  
70  	private JPanel descriptionPanel;
71  
72  	private JTextField descriptionTextField;
73  
74  	private JPanel startTimePanel;
75  
76  	private JTimeChooser startTimeChooser;
77  
78  	private JPanel endTimePanel;
79  
80  	private JTimeChooser endTimeChooser;
81  
82  	private JPanel servicePanel;
83  
84  	private JComboBox serviceComboBox;
85  
86  	public NewSessionDialog() {
87  		initialize();
88  	}
89  
90  	protected void initialize() {
91  		this.setResizable(false);
92  		this.setSize(550, 600);
93  		// Center the dialog
94  		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
95  		Dimension frameSize = this.getSize();
96  		if (frameSize.height > screenSize.height) {
97  			frameSize.height = screenSize.height;
98  		}
99  		if (frameSize.width > screenSize.width) {
100 			frameSize.width = screenSize.width;
101 		}
102 		this.setLocation((screenSize.width - frameSize.width) / 2,
103 				(screenSize.height - frameSize.height) / 2);
104 
105 		this.setContentPane(getContentPanel());
106 		this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
107 	}
108 
109 	/***
110 	 * @return Returns the contentPanel.
111 	 */
112 	protected JPanel getContentPanel() {
113 		if (contentPanel == null) {
114 			contentPanel = new JPanel();
115 			contentPanel.setLayout(new BorderLayout());
116 			JPanel panel = new JPanel();
117 			panel.setLayout(new BorderLayout());
118 
119 			panel.add(getTopPanel(), BorderLayout.CENTER);
120 			panel.add(getBottomPanel(), BorderLayout.SOUTH);
121 			contentPanel.add(panel, BorderLayout.CENTER);
122 			contentPanel.add(getButtonPanel(), BorderLayout.SOUTH);
123 		}
124 		return contentPanel;
125 	}
126 
127 	/***
128 	 * @return Returns the buttonPanel.
129 	 */
130 	protected JPanel getButtonPanel() {
131 		if (buttonPanel == null) {
132 			buttonPanel = new JPanel();
133 			TitledBorder titledBorder = new TitledBorder(
134 					Messages.getString("dialog.availableactions")); //$NON-NLS-1$
135 			buttonPanel.setBorder(titledBorder);
136 			buttonPanel.setLayout(new GridLayout(1, 4));
137 			buttonPanel.add(new JLabel("")); //$NON-NLS-1$
138 			buttonPanel.add(getSaveSessionButton());
139 			buttonPanel.add(getClearSessionDataButton());
140 			buttonPanel.add(new JLabel("")); //$NON-NLS-1$
141 		}
142 		return buttonPanel;
143 	}
144 
145 	/***
146 	 * @return Returns the topPanel.
147 	 */
148 	protected SessionCreationPanel getTopPanel() {
149 		if (topPanel == null) {
150 			topPanel = new SessionCreationPanel();
151 		}
152 		return topPanel;
153 	}
154 
155 	/***
156 	 * @return Returns the bottomPanel.
157 	 */
158 	protected JPanel getBottomPanel() {
159 		if (bottomPanel == null) {
160 			bottomPanel = new JPanel();
161 			bottomPanel.setLayout(new GridLayout(2, 1));
162 			JPanel topBottomPanel = new JPanel();
163 			topBottomPanel.setLayout(new BorderLayout());
164 			topBottomPanel.add(getServicePanel(), BorderLayout.NORTH);
165 			topBottomPanel.add(getDescriptionPanel(), BorderLayout.CENTER);
166 
167 			bottomPanel.add(topBottomPanel);
168 
169 			JPanel bottomBottomPanel = new JPanel();
170 			bottomBottomPanel.setLayout(new GridLayout(2, 1));
171 			bottomBottomPanel.add(getStartTimePanel());
172 			bottomBottomPanel.add(getEndTimePanel());
173 
174 			bottomPanel.add(bottomBottomPanel);
175 		}
176 		return bottomPanel;
177 	}
178 
179 	/***
180 	 * @return Returns the descriptionPanel.
181 	 */
182 	protected JPanel getDescriptionPanel() {
183 		if (descriptionPanel == null) {
184 			descriptionPanel = new JPanel();
185 			TitledBorder titledBorder = new TitledBorder(Messages.getString("common.description")); //$NON-NLS-1$
186 			descriptionPanel.setBorder(titledBorder);
187 			descriptionPanel.setLayout(new BorderLayout());
188 			descriptionPanel
189 					.add(getDescriptionTextField(), BorderLayout.CENTER);
190 		}
191 		return descriptionPanel;
192 	}
193 
194 	/***
195 	 * @return Returns the descriptionTextField.
196 	 */
197 	protected JTextField getDescriptionTextField() {
198 		if (descriptionTextField == null) {
199 			descriptionTextField = new JTextField();
200 		}
201 		return descriptionTextField;
202 	}
203 
204 	/***
205 	 * @return Returns the startTimePanel.
206 	 */
207 	protected JPanel getStartTimePanel() {
208 		if (startTimePanel == null) {
209 			startTimePanel = new JPanel();
210 			TitledBorder titledBorder = new TitledBorder(Messages.getString("common.starttime")); //$NON-NLS-1$
211 			startTimePanel.setBorder(titledBorder);
212 			startTimePanel.setLayout(new BorderLayout());
213 			startTimePanel.add(getStartTimeChooser(), BorderLayout.CENTER);
214 		}
215 		return startTimePanel;
216 	}
217 
218 	/***
219 	 * @return Returns the startTimeChooser.
220 	 */
221 	protected JTimeChooser getStartTimeChooser() {
222 		if (startTimeChooser == null) {
223 			startTimeChooser = new JTimeChooser();
224 			startTimeChooser
225 					.setValue(GregorianCalendar.getInstance().getTime());
226 		}
227 		return startTimeChooser;
228 	}
229 
230 	/***
231 	 * @return Returns the endTimePanel.
232 	 */
233 	protected JPanel getEndTimePanel() {
234 		if (endTimePanel == null) {
235 			endTimePanel = new JPanel();
236 			TitledBorder titledBorder = new TitledBorder(Messages.getString("common.endtime")); //$NON-NLS-1$
237 			endTimePanel.setBorder(titledBorder);
238 			endTimePanel.setLayout(new BorderLayout());
239 			endTimePanel.add(getEndTimeChooser(), BorderLayout.CENTER);
240 		}
241 		return endTimePanel;
242 	}
243 
244 	/***
245 	 * @return Returns the endTimeChooser.
246 	 */
247 	protected JTimeChooser getEndTimeChooser() {
248 		if (endTimeChooser == null) {
249 			endTimeChooser = new JTimeChooser();
250 			endTimeChooser.setValue(GregorianCalendar.getInstance().getTime());
251 
252 		}
253 		return endTimeChooser;
254 	}
255 
256 	/***
257 	 * @return Returns the serviceComboBox.
258 	 */
259 	protected JComboBox getServiceComboBox() {
260 		if (serviceComboBox == null) {
261 			serviceComboBox = new JComboBox();
262 			Service[] service = InternetCafeManager.getInstance().getService();
263 			for (int i = 0; i < service.length; i++)
264 				serviceComboBox.addItem(service[i]);
265 		}
266 		return serviceComboBox;
267 	}
268 
269 	/***
270 	 * @return Returns the servicePanel.
271 	 */
272 	protected JPanel getServicePanel() {
273 		if (servicePanel == null) {
274 			servicePanel = new JPanel();
275 			TitledBorder titledBorder = new TitledBorder(Messages.getString("common.service")); //$NON-NLS-1$
276 			servicePanel.setBorder(titledBorder);
277 			servicePanel.setLayout(new BorderLayout());
278 			servicePanel.add(getServiceComboBox(), BorderLayout.CENTER);
279 		}
280 		return servicePanel;
281 	}
282 
283 	/***
284 	 * @return Returns the clearSessionDataButton.
285 	 */
286 	protected JButton getClearSessionDataButton() {
287 		if (clearSessionDataButton == null) {
288 			clearSessionDataButton = new JButton(Messages.getString("button.clear")); //$NON-NLS-1$
289 			clearSessionDataButton.setIcon(new ImageIcon(this.getClass()
290 					.getResource("/icon/16x16/actions/view-refresh.png"))); //$NON-NLS-1$
291 
292 			clearSessionDataButton.addActionListener(new ActionListener() {
293 				public void actionPerformed(ActionEvent arg0) {
294 					logger.debug("actionPerformed clearSessionDataButton"); //$NON-NLS-1$
295 					getDescriptionTextField().setText(""); //$NON-NLS-1$
296 				}
297 			});
298 		}
299 		return clearSessionDataButton;
300 	}
301 
302 	/***
303 	 * @return Returns the saveSessionButton.
304 	 */
305 	protected JButton getSaveSessionButton() {
306 		if (saveSessionButton == null) {
307 			saveSessionButton = new JButton(Messages.getString("button.save")); //$NON-NLS-1$
308 			saveSessionButton.setIcon(new ImageIcon(this.getClass()
309 					.getResource("/icon/16x16/actions/document-save.png"))); //$NON-NLS-1$
310 
311 			final SaveNewSessionCommand saveNewSessionCommand = new SaveNewSessionCommand(
312 					this, topPanel);
313 			saveSessionButton.addActionListener(new ActionListener() {
314 				public void actionPerformed(ActionEvent arg0) {
315 					logger.debug("actionPerformed saveSessionButton"); //$NON-NLS-1$
316 					CommandExecutor.getInstance().executeCommand(
317 							saveNewSessionCommand, true);
318 					if (saveNewSessionCommand.getStatus() == SaveNewSessionCommand.SUCCESS_STATUS)
319 						setVisible(false);
320 				}
321 			});
322 		}
323 		return saveSessionButton;
324 	}
325 
326 	/***
327 	 * @return The session's description.
328 	 */
329 	public String getSessionDescription() {
330 		return getDescriptionTextField().getText();
331 	}
332 
333 	/***
334 	 * @return The session's startTime.
335 	 */
336 	public Date getSessionStartTime() {
337 		return (Date) getStartTimeChooser().getValue();
338 	}
339 
340 	/***
341 	 * @return The session's endTime.
342 	 */
343 	public Date getSessionEndTime() {
344 		return (Date) getEndTimeChooser().getValue();
345 	}
346 
347 	/***
348 	 * @return The session's service.
349 	 */
350 	public Service getSessionService() {
351 		return (Service) getServiceComboBox().getSelectedItem();
352 	}
353 
354 	public void setSessionDateError(boolean isError) {
355 		if (isError) {
356 			((TitledBorder) this.getStartTimePanel().getBorder())
357 					.setTitleColor(Color.RED);
358 			((TitledBorder) this.getEndTimePanel().getBorder())
359 					.setTitleColor(Color.RED);
360 		} else {
361 			((TitledBorder) this.getStartTimePanel().getBorder())
362 					.setTitleColor(new TitledBorder("").getTitleColor()); //$NON-NLS-1$
363 			((TitledBorder) this.getEndTimePanel().getBorder())
364 					.setTitleColor(new TitledBorder("").getTitleColor()); //$NON-NLS-1$
365 		}
366 		this.getStartTimePanel().updateUI();
367 		this.getEndTimePanel().updateUI();
368 	}
369 
370 }