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.GridLayout;
25  import java.util.Date;
26  
27  import javax.swing.JComboBox;
28  import javax.swing.JPanel;
29  import javax.swing.JSplitPane;
30  import javax.swing.JTextField;
31  import javax.swing.border.TitledBorder;
32  
33  import ui.Messages;
34  import base.user.Document;
35  import base.user.DocumentReleaseAuthority;
36  import base.user.DocumentType;
37  
38  import com.toedter.calendar.JDateChooser;
39  
40  @SuppressWarnings("serial") //$NON-NLS-1$
41  public class DocumentPanel extends JPanel {
42  
43  	public DocumentPanel(Document document) {
44  		this.document = document;
45  		initialize();
46  	}
47  
48  	/*
49  	 * private DocumentType type; private String number; private String
50  	 * description; private Date release; private Date expiration; private Image
51  	 * image; private DocumentReleaseAuthority authority;
52  	 */
53  
54  	private JPanel topPanel;
55  
56  	private JPanel bottomPanel;
57  
58  	private JSplitPane contentPanel;
59  
60  	private JPanel typePanel;
61  
62  	private JComboBox typeComboBox;
63  
64  	private JPanel numberPanel;
65  
66  	private JTextField numberTextField;
67  
68  	private JPanel descriptionPanel;
69  
70  	private JTextField descriptionTextField;
71  
72  	private JPanel releasePanel;
73  
74  	private JDateChooser releaseDateChooser;
75  
76  	private JPanel expirationPanel;
77  
78  	private JDateChooser expirationDateChooser;
79  
80  	private ImagePanel imagePanel;
81  
82  	private JPanel releaseAuthorityPanel;
83  
84  	private JComboBox releaseAuthorityComboBox;
85  
86  	private Document document;
87  
88  	/***
89  	 * @return Returns the DocumentType stored in the authorityComboBox.
90  	 */
91  	public String getDocumentType() {
92  		return (String) getTypeComboBox().getSelectedItem();
93  	}
94  
95  	/***
96  	 * @return Returns the Document's number stored in the numberTextField.
97  	 */
98  	public String getDocumentNumber() {
99  		return getNumberTextField().getText();
100 	}
101 
102 	/***
103 	 * @return Returns the Document's description stored in the
104 	 *         descriptionTextField.
105 	 */
106 	public String getDocumentDescription() {
107 		return getDescriptionTextField().getText();
108 	}
109 
110 	/***
111 	 * @return Returns the Document's release date stored in the
112 	 *         releaseDateChooser.
113 	 */
114 	public Date getDocumentRelease() {
115 		return getReleaseDateChooser().getDate();
116 	}
117 
118 	/***
119 	 * @return Returns the Document's description stored in the
120 	 *         expirationDateChooser.
121 	 */
122 	public Date getDocumentExpiration() {
123 		return getExpirationDateChooser().getDate();
124 	}
125 
126 	/***
127 	 * @return Returns the DocumentReleaseAuthority stored in the
128 	 *         releaseAuthorityComboBox.
129 	 */
130 	public String getDocumentReleaseAuthority() {
131 		return (String) getReleaseAuthorityComboBox().getSelectedItem();
132 	}
133 
134 	/***
135 	 * @return Returns the Document's image path stored in the imagePanel.
136 	 */
137 	public String getDocumentImagePath() {
138 		return ((ImagePanel) getImagePanel()).getImagePath();
139 	}
140 
141 	protected void initialize() {
142 		this.setBorder(new TitledBorder(Messages.getString("common.document"))); //$NON-NLS-1$
143 		this.setLayout(new BorderLayout());
144 		this.add(getContentPanel(), BorderLayout.CENTER);
145 	}
146 
147 	/***
148 	 * @return Returns the authorityPanel.
149 	 */
150 	protected JPanel getReleaseAuthorityPanel() {
151 		if (releaseAuthorityPanel == null) {
152 			releaseAuthorityPanel = new JPanel();
153 			releaseAuthorityPanel.setLayout(new BorderLayout());
154 			TitledBorder titledBorder = new TitledBorder(Messages.getString("common.releaseauthority")); //$NON-NLS-1$
155 			releaseAuthorityPanel.setBorder(titledBorder);
156 			releaseAuthorityPanel.add(getReleaseAuthorityComboBox(),
157 					BorderLayout.CENTER);
158 		}
159 		return releaseAuthorityPanel;
160 	}
161 
162 	/***
163 	 * @return Returns the releaseAuthorityComboBox.
164 	 */
165 	protected JComboBox getReleaseAuthorityComboBox() {
166 		if (releaseAuthorityComboBox == null) {
167 			releaseAuthorityComboBox = new JComboBox();
168 			for (int i = 0; i < DocumentReleaseAuthority.DOCUMENT_RELEASE_AUTHORITY.length; i++)
169 				releaseAuthorityComboBox
170 						.addItem(DocumentReleaseAuthority.DOCUMENT_RELEASE_AUTHORITY[i]);
171 			releaseAuthorityComboBox
172 					.setSelectedItem(document == null ? DocumentReleaseAuthority.DOCUMENT_RELEASE_AUTHORITY[0]
173 							: document.getReleaseAuthority());
174 		}
175 		return releaseAuthorityComboBox;
176 	}
177 
178 	/***
179 	 * @return Returns the descriptionPanel.
180 	 */
181 	protected JPanel getDescriptionPanel() {
182 		if (descriptionPanel == null) {
183 			descriptionPanel = new JPanel();
184 			descriptionPanel.setLayout(new BorderLayout());
185 			TitledBorder titledBorder = new TitledBorder(Messages.getString("common.description")); //$NON-NLS-1$
186 			descriptionPanel.setBorder(titledBorder);
187 			descriptionPanel
188 					.add(getDescriptionTextField(), BorderLayout.CENTER);
189 		}
190 		return descriptionPanel;
191 	}
192 
193 	/***
194 	 * @return Returns the descriptionTextField.
195 	 */
196 	protected JTextField getDescriptionTextField() {
197 		if (descriptionTextField == null) {
198 			descriptionTextField = new JTextField();
199 			descriptionTextField.setText(document == null ? "" : document //$NON-NLS-1$
200 					.getDescription());
201 		}
202 		return descriptionTextField;
203 	}
204 
205 	/***
206 	 * @return Returns the expirationPanel.
207 	 */
208 	protected JPanel getExpirationPanel() {
209 		if (expirationPanel == null) {
210 			expirationPanel = new JPanel();
211 			expirationPanel.setLayout(new BorderLayout());
212 			TitledBorder titledBorder = new TitledBorder(Messages.getString("common.expiration")); //$NON-NLS-1$
213 			expirationPanel.setBorder(titledBorder);
214 			expirationPanel.add(getExpirationDateChooser());
215 		}
216 		return expirationPanel;
217 	}
218 
219 	/***
220 	 * @return Returns the expirationDateChooser.
221 	 */
222 	protected JDateChooser getExpirationDateChooser() {
223 		if (expirationDateChooser == null) {
224 			expirationDateChooser = new JDateChooser();
225 			expirationDateChooser.setDate(document == null ? new Date()
226 					: document.getExpiration());
227 		}
228 		return expirationDateChooser;
229 	}
230 
231 	/***
232 	 * @return Returns the imagePanel.
233 	 */
234 	protected ImagePanel getImagePanel() {
235 		if (imagePanel == null) {
236 			imagePanel = new ImagePanel(document == null ? "" : document //$NON-NLS-1$
237 					.getImagePath());
238 		}
239 		return imagePanel;
240 	}
241 
242 	/***
243 	 * @return Returns the numberPanel.
244 	 */
245 	protected JPanel getNumberPanel() {
246 		if (numberPanel == null) {
247 			numberPanel = new JPanel();
248 			numberPanel.setLayout(new BorderLayout());
249 			TitledBorder titledBorder = new TitledBorder(Messages.getString("common.number")); //$NON-NLS-1$
250 			numberPanel.setBorder(titledBorder);
251 			numberPanel.add(getNumberTextField(), BorderLayout.CENTER);
252 		}
253 		return numberPanel;
254 	}
255 
256 	/***
257 	 * @return Returns the numberTextField.
258 	 */
259 	protected JTextField getNumberTextField() {
260 		if (numberTextField == null) {
261 			numberTextField = new JTextField();
262 			numberTextField.setText(document == null ? "" : document //$NON-NLS-1$
263 					.getNumber());
264 		}
265 		return numberTextField;
266 	}
267 
268 	/***
269 	 * @return Returns the releasePanel.
270 	 */
271 	protected JPanel getReleasePanel() {
272 		if (releasePanel == null) {
273 			releasePanel = new JPanel();
274 			releasePanel.setLayout(new BorderLayout());
275 			TitledBorder titledBorder = new TitledBorder(Messages.getString("common.release")); //$NON-NLS-1$
276 			releasePanel.setBorder(titledBorder);
277 			releasePanel.add(getReleaseDateChooser());
278 
279 		}
280 		return releasePanel;
281 	}
282 
283 	/***
284 	 * @return Returns the releaseDateChooser.
285 	 */
286 	protected JDateChooser getReleaseDateChooser() {
287 		if (releaseDateChooser == null) {
288 			releaseDateChooser = new JDateChooser();
289 			releaseDateChooser.setDate(document == null ? new Date() : document
290 					.getRelease());
291 		}
292 		return releaseDateChooser;
293 	}
294 
295 	/***
296 	 * @return Returns the typePanel.
297 	 */
298 	protected JPanel getTypePanel() {
299 		if (typePanel == null) {
300 			typePanel = new JPanel();
301 			typePanel.setLayout(new BorderLayout());
302 			TitledBorder titledBorder = new TitledBorder(Messages.getString("common.type")); //$NON-NLS-1$
303 			typePanel.setBorder(titledBorder);
304 			typePanel.add(getTypeComboBox(), BorderLayout.CENTER);
305 		}
306 		return typePanel;
307 	}
308 
309 	/***
310 	 * @return Returns the typeComboBox.
311 	 */
312 	protected JComboBox getTypeComboBox() {
313 		if (typeComboBox == null) {
314 			typeComboBox = new JComboBox();
315 			for (int i = 0; i < DocumentType.DOCUMENT_TYPE.length; i++)
316 				typeComboBox.addItem(DocumentType.DOCUMENT_TYPE[i]);
317 			typeComboBox
318 					.setSelectedItem(document == null ? DocumentType.DOCUMENT_TYPE[0]
319 							: document.getType());
320 		}
321 		return typeComboBox;
322 	}
323 
324 	/***
325 	 * @return Returns the contentPanel.
326 	 */
327 	protected JSplitPane getContentPanel() {
328 		if (contentPanel == null) {
329 			contentPanel = new JSplitPane();
330 			contentPanel.setOrientation(JSplitPane.VERTICAL_SPLIT);
331 			contentPanel.setOneTouchExpandable(true);
332 			contentPanel.setTopComponent(getTopPanel());
333 			contentPanel.setBottomComponent(getBottomPanel());
334 		}
335 		return contentPanel;
336 	}
337 
338 	/***
339 	 * @return Returns the bottomPanel.
340 	 */
341 	protected JPanel getBottomPanel() {
342 		if (bottomPanel == null) {
343 			bottomPanel = new JPanel();
344 			bottomPanel.setLayout(new BorderLayout());
345 			TitledBorder titledBorder = new TitledBorder(Messages.getString("common.photo")); //$NON-NLS-1$
346 			bottomPanel.setBorder(titledBorder);
347 			bottomPanel.add(getImagePanel(), BorderLayout.CENTER);
348 		}
349 		return bottomPanel;
350 	}
351 
352 	/***
353 	 * @return Returns the topPanel.
354 	 */
355 	protected JPanel getTopPanel() {
356 		if (topPanel == null) {
357 			topPanel = new JPanel();
358 			TitledBorder titledBorder = new TitledBorder(Messages.getString("common.generalinformation")); //$NON-NLS-1$
359 			topPanel.setBorder(titledBorder);
360 
361 			topPanel.setLayout(new BorderLayout());
362 
363 			JPanel northPanel = new JPanel();
364 			northPanel.setLayout(new GridLayout(2, 2));
365 			northPanel.add(getTypePanel(), null);
366 			northPanel.add(getReleaseAuthorityPanel(), null);
367 			northPanel.add(getReleasePanel(), null);
368 			northPanel.add(getExpirationPanel(), null);
369 
370 			JPanel southPanel = new JPanel();
371 			southPanel.setLayout(new GridLayout(2, 1));
372 			southPanel.add(getNumberPanel(), null);
373 			southPanel.add(getDescriptionPanel(), null);
374 
375 			topPanel.add(northPanel, BorderLayout.CENTER);
376 			topPanel.add(southPanel, BorderLayout.SOUTH);
377 
378 		}
379 		return topPanel;
380 	}
381 
382 	public void clearDocumentData() {
383 		getNumberTextField().setText(""); //$NON-NLS-1$
384 		getDescriptionTextField().setText(""); //$NON-NLS-1$
385 		getReleaseDateChooser().setDate(new Date());
386 		getExpirationDateChooser().setDate(new Date());
387 
388 	}
389 
390 	public void setDocumentNumberError(boolean isError) {
391 		if (isError)
392 			((TitledBorder) this.getNumberPanel().getBorder())
393 					.setTitleColor(Color.RED);
394 		else
395 			((TitledBorder) this.getNumberPanel().getBorder())
396 					.setTitleColor(new TitledBorder("").getTitleColor()); //$NON-NLS-1$
397 		this.getNumberPanel().updateUI();
398 	}
399 
400 	public void setDocumentDateError(boolean isError) {
401 		if (isError) {
402 			((TitledBorder) this.getReleasePanel().getBorder())
403 					.setTitleColor(Color.RED);
404 			((TitledBorder) this.getExpirationPanel().getBorder())
405 					.setTitleColor(Color.RED);
406 		} else {
407 			((TitledBorder) this.getReleasePanel().getBorder())
408 					.setTitleColor(new TitledBorder("").getTitleColor()); //$NON-NLS-1$
409 			((TitledBorder) this.getExpirationPanel().getBorder())
410 					.setTitleColor(new TitledBorder("").getTitleColor()); //$NON-NLS-1$
411 		}
412 		this.getReleasePanel().updateUI();
413 		this.getExpirationPanel().updateUI();
414 	}
415 
416 }