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.Vector;
27  
28  import javax.swing.ImageIcon;
29  import javax.swing.JButton;
30  import javax.swing.JFileChooser;
31  import javax.swing.JList;
32  import javax.swing.JOptionPane;
33  import javax.swing.JPanel;
34  import javax.swing.JScrollPane;
35  import javax.swing.JTextField;
36  import javax.swing.SwingUtilities;
37  import javax.swing.UIManager;
38  import javax.swing.border.TitledBorder;
39  
40  import org.apache.log4j.Logger;
41  
42  import ui.Messages;
43  import ui.util.FileExtensionFilter;
44  import base.ConfigurationManager;
45  import base.InternetCafe;
46  
47  @SuppressWarnings("serial") //$NON-NLS-1$
48  public class InternetCafeConfigurationPanel extends JPanel {
49  
50  	private static final transient Logger logger = Logger
51  			.getLogger(InternetCafeConfigurationPanel.class.getName());
52  
53  	private JPanel internetCafeNamePanel;
54  
55  	private JTextField internetCafeNameTextField;
56  
57  	private JPanel internetCafeNameButtonPanel;
58  
59  	private JPanel internetCafeLogoImagePathPanel;
60  
61  	private JTextField internetCafeLogoImagePathTextField;
62  
63  	private JPanel internetCafeLogoImagePathButtonPanel;
64  
65  	private JPanel internetCafeLookAndFeelPanel;
66  
67  	private JScrollPane internetCafeLookAndFeelScrollPane;
68  
69  	private JList internetCafeLookAndFeelList;
70  
71  	private JPanel internetCafeLookAndFeelButtonPanel;
72  
73  	public InternetCafeConfigurationPanel() {
74  		initialize();
75  	}
76  
77  	protected void initialize() {
78  		TitledBorder titledBorder = new TitledBorder(
79  				Messages.getString("panel.internetcafeconfigurationpanel.internetcafeconfiguration")); //$NON-NLS-1$
80  		this.setBorder(titledBorder);
81  		this.setOpaque(true);
82  		this.setLayout(new GridLayout(3, 1));
83  		this.add(getInternetCafeNamePanel(), null);
84  		this.add(getInternetCafeLogoImagePathPanel(), null);
85  		this.add(getInternetCafeLookAndFeelPanel(), null);
86  	}
87  
88  	/***
89  	 * @return Returns the internetCafeNameTextField.
90  	 */
91  	protected JTextField getInternetCafeNameTextField() {
92  		if (internetCafeNameTextField == null) {
93  			internetCafeNameTextField = new JTextField(ConfigurationManager
94  					.getInstance().getInternetCafeName());
95  		}
96  		return internetCafeNameTextField;
97  	}
98  
99  	/***
100 	 * @return Returns the internetCafeNamePanel.
101 	 */
102 	protected JPanel getInternetCafeNamePanel() {
103 		if (internetCafeNamePanel == null) {
104 			internetCafeNamePanel = new JPanel();
105 			TitledBorder titledBorder = new TitledBorder(Messages.getString("panel.internetcafeconfigurationpanel.internetcafename")); //$NON-NLS-1$
106 			internetCafeNamePanel.setBorder(titledBorder);
107 			internetCafeNamePanel.setLayout(new BorderLayout());
108 			internetCafeNamePanel.add(getInternetCafeNameTextField(),
109 					BorderLayout.CENTER);
110 			internetCafeNamePanel.add(getInternetCafeNameButtonPanel(),
111 					BorderLayout.EAST);
112 			updateUI();
113 		}
114 		return internetCafeNamePanel;
115 	}
116 
117 	/***
118 	 * @return Returns the internetCafeLogoImagePathTextField.
119 	 */
120 	protected JTextField getInternetCafeLogoImagePathTextField() {
121 		if (internetCafeLogoImagePathTextField == null) {
122 			internetCafeLogoImagePathTextField = new JTextField(
123 					ConfigurationManager.getInstance()
124 							.getInternetCafeLogoPath());
125 		}
126 		return internetCafeLogoImagePathTextField;
127 	}
128 
129 	/***
130 	 * @return Returns the internetCafeLogoPanel.
131 	 */
132 	protected JPanel getInternetCafeLogoImagePathPanel() {
133 		if (internetCafeLogoImagePathPanel == null) {
134 			internetCafeLogoImagePathPanel = new JPanel();
135 			TitledBorder titledBorder = new TitledBorder(Messages.getString("panel.internetcafeconfigurationpanel.internetcafelogo")); //$NON-NLS-1$
136 			internetCafeLogoImagePathPanel.setBorder(titledBorder);
137 			internetCafeLogoImagePathPanel.setLayout(new BorderLayout());
138 			internetCafeLogoImagePathPanel.add(
139 					getInternetCafeLogoImagePathTextField(),
140 					BorderLayout.CENTER);
141 			internetCafeLogoImagePathPanel.add(
142 					getInternetCafeLogoImagePathButtonPanel(),
143 					BorderLayout.EAST);
144 		}
145 		return internetCafeLogoImagePathPanel;
146 	}
147 
148 	/***
149 	 * @return Returns the internetCafeNameButtonPanel.
150 	 */
151 	protected JPanel getInternetCafeNameButtonPanel() {
152 		if (internetCafeNameButtonPanel == null) {
153 			internetCafeNameButtonPanel = new JPanel();
154 			internetCafeNameButtonPanel.setLayout(new GridLayout(2, 1));
155 			JButton saveButton = new JButton(Messages.getString("button.save")); //$NON-NLS-1$
156 			saveButton.setIcon(new ImageIcon(this.getClass().getResource(
157 					"/icon/16x16/actions/document-save.png"))); //$NON-NLS-1$
158 
159 			JButton restoreButton = new JButton(Messages.getString("button.refresh")); //$NON-NLS-1$
160 			restoreButton.setIcon(new ImageIcon(this.getClass().getResource(
161 					"/icon/16x16/actions/view-refresh.png"))); //$NON-NLS-1$
162 
163 			internetCafeNameButtonPanel.add(saveButton, null);
164 			internetCafeNameButtonPanel.add(restoreButton, null);
165 
166 			saveButton.addActionListener(new ActionListener() {
167 				public void actionPerformed(ActionEvent arg0) {
168 					logger.debug("actionPerformed saveButton"); //$NON-NLS-1$
169 					ConfigurationManager.getInstance().setInternetCafeName(
170 							getInternetCafeNameTextField().getText());
171 				}
172 			});
173 
174 			restoreButton.addActionListener(new ActionListener() {
175 				public void actionPerformed(ActionEvent arg0) {
176 					logger.debug("actionPerformed restoreButton"); //$NON-NLS-1$
177 					getInternetCafeNameTextField().setText(
178 							ConfigurationManager.getInstance()
179 									.getInternetCafeName());
180 				}
181 			});
182 		}
183 		return internetCafeNameButtonPanel;
184 	}
185 
186 	/***
187 	 * @return Returns the internetCafeLogoImagePathButtonPanel.
188 	 */
189 	protected JPanel getInternetCafeLogoImagePathButtonPanel() {
190 		if (internetCafeLogoImagePathButtonPanel == null) {
191 			internetCafeLogoImagePathButtonPanel = new JPanel();
192 			internetCafeLogoImagePathButtonPanel
193 					.setLayout(new GridLayout(2, 1));
194 			JButton loadButton = new JButton(Messages.getString("button.load")); //$NON-NLS-1$
195 			loadButton.setIcon(new ImageIcon(this.getClass().getResource(
196 					"/icon/16x16/actions/document-open.png"))); //$NON-NLS-1$
197 
198 			JButton restoreButton = new JButton(Messages.getString("button.refresh")); //$NON-NLS-1$
199 			restoreButton.setIcon(new ImageIcon(this.getClass().getResource(
200 					"/icon/16x16/actions/view-refresh.png"))); //$NON-NLS-1$
201 
202 			internetCafeLogoImagePathButtonPanel.add(loadButton, null);
203 			internetCafeLogoImagePathButtonPanel.add(restoreButton, null);
204 
205 			loadButton.addActionListener(new ActionListener() {
206 				public void actionPerformed(ActionEvent arg0) {
207 					logger.debug("actionPerformed saveButton"); //$NON-NLS-1$
208 					final JFileChooser chooser = new JFileChooser();
209 					chooser.setDialogTitle(Messages.getString("common.load")); //$NON-NLS-1$
210 					chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
211 					chooser.setApproveButtonText(Messages.getString("button.load")); //$NON-NLS-1$
212 					chooser.setFileFilter(new FileExtensionFilter(Messages.getString("common.filetype.jpg"), //$NON-NLS-1$
213 							Messages.getString("common.filetype.jpg"))); //$NON-NLS-1$
214 					if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
215 						String path = chooser.getSelectedFile()
216 								.getAbsolutePath();
217 						ConfigurationManager.getInstance().setInternetCafeName(
218 								path);
219 						getInternetCafeLogoImagePathTextField().setText(path);
220 					}
221 				}
222 			});
223 
224 			restoreButton.addActionListener(new ActionListener() {
225 				public void actionPerformed(ActionEvent arg0) {
226 					logger.debug("actionPerformed restoreButton"); //$NON-NLS-1$
227 					getInternetCafeLogoImagePathTextField().setText(
228 							ConfigurationManager.getInstance()
229 									.getInternetCafeLogoPath());
230 				}
231 			});
232 		}
233 		return internetCafeLogoImagePathButtonPanel;
234 	}
235 
236 	/***
237 	 * @return Returns the internetCafeLookAndFeelButtonPanel.
238 	 */
239 	protected JPanel getInternetCafeLookAndFeelButtonPanel() {
240 		if (internetCafeLookAndFeelButtonPanel == null) {
241 			internetCafeLookAndFeelButtonPanel = new JPanel();
242 			internetCafeLookAndFeelButtonPanel.setLayout(new GridLayout(2, 1));
243 			JButton saveLookAndFeelButton = new JButton(Messages.getString("button.save")); //$NON-NLS-1$
244 			saveLookAndFeelButton.setIcon(new ImageIcon(this.getClass()
245 					.getResource("/icon/16x16/actions/document-save.png"))); //$NON-NLS-1$
246 
247 			JButton applyLookAndFeelButton = new JButton(Messages.getString("button.refresh")); //$NON-NLS-1$
248 			applyLookAndFeelButton.setIcon(new ImageIcon(this.getClass()
249 					.getResource("/icon/16x16/actions/view-refresh.png"))); //$NON-NLS-1$
250 
251 			internetCafeLookAndFeelButtonPanel.add(saveLookAndFeelButton);
252 			internetCafeLookAndFeelButtonPanel.add(applyLookAndFeelButton);
253 
254 			applyLookAndFeelButton.addActionListener(new ActionListener() {
255 				public void actionPerformed(ActionEvent arg0) {
256 					logger.debug("actionPerformed applyLookAndFeelButton"); //$NON-NLS-1$
257 					if (getInternetCafeLookAndFeelList().getSelectedValue() != null) {
258 						try {
259 							UIManager
260 									.setLookAndFeel(((LookAndFeelWrapper) getInternetCafeLookAndFeelList()
261 											.getSelectedValue()).lookAndFeelClass
262 											.getCanonicalName());
263 						} catch (Exception ex) {
264 							// TODO Auto-generated catch block
265 							logger.error(ex.getMessage());
266 							ex.printStackTrace();
267 						}
268 						SwingUtilities.updateComponentTreeUI(InternetCafe
269 								.getInstance().getMainFrame());
270 						InternetCafe.getInstance().getMainFrame().pack();
271 						updateUI();
272 					} else
273 						JOptionPane
274 								.showMessageDialog(
275 										null,
276 										Messages.getString("panel.internetcafeconfigurationpanel.message1"), //$NON-NLS-1$
277 										Messages.getString("common.warning"), JOptionPane.WARNING_MESSAGE); //$NON-NLS-1$
278 
279 				}
280 			});
281 
282 			saveLookAndFeelButton.addActionListener(new ActionListener() {
283 				public void actionPerformed(ActionEvent arg0) {
284 					logger.debug("actionPerformed saveLookAndFeelButton"); //$NON-NLS-1$
285 					if (getInternetCafeLookAndFeelList().getSelectedValue() != null) {
286 						ConfigurationManager
287 								.getInstance()
288 								.setInternetCafeLookAndFeel(
289 										((LookAndFeelWrapper) getInternetCafeLookAndFeelList()
290 												.getSelectedValue()).lookAndFeelClass);
291 					} else
292 						JOptionPane
293 								.showMessageDialog(
294 										null,
295 										Messages.getString("panel.internetcafeconfigurationpanel.message1"), //$NON-NLS-1$
296 										Messages.getString("common.warning"), JOptionPane.WARNING_MESSAGE); //$NON-NLS-1$
297 				}
298 			});
299 		}
300 		return internetCafeLookAndFeelButtonPanel;
301 	}
302 
303 	/***
304 	 * @return Returns the internetCafeLookAndFeelList.
305 	 */
306 	protected JList getInternetCafeLookAndFeelList() {
307 		if (internetCafeLookAndFeelList == null) {
308 			internetCafeLookAndFeelList = new JList();
309 			Vector<LookAndFeelWrapper> lookAndFeel = new Vector<LookAndFeelWrapper>();
310 			try {
311 				lookAndFeel.add(new LookAndFeelWrapper(Class
312 						.forName("apple.laf.AquaLookAndFeel"))); //$NON-NLS-1$
313 				lookAndFeel
314 						.add(new LookAndFeelWrapper(
315 								Class
316 										.forName("com.sun.java.swing.plaf.gtk.GTKLookAndFeel"))); //$NON-NLS-1$
317 				lookAndFeel
318 						.add(new LookAndFeelWrapper(
319 								Class
320 										.forName("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"))); //$NON-NLS-1$
321 				lookAndFeel
322 						.add(new LookAndFeelWrapper(
323 								Class
324 										.forName("com.sun.java.swing.plaf.motif.MotifLookAndFeel"))); //$NON-NLS-1$
325 			} catch (Exception ex) {
326 				logger.debug(ex.getMessage());
327 				ex.printStackTrace();
328 			}
329 			internetCafeLookAndFeelList.setListData(lookAndFeel);
330 		}
331 		return internetCafeLookAndFeelList;
332 	}
333 
334 	public class LookAndFeelWrapper {
335 
336 		public final Class lookAndFeelClass;
337 
338 		public LookAndFeelWrapper(Class lookAndFeelClass) {
339 			this.lookAndFeelClass = lookAndFeelClass;
340 		}
341 
342 		public String toString() {
343 			return lookAndFeelClass.getSimpleName();
344 		}
345 	}
346 
347 	/***
348 	 * @return Returns the internetCafeLookAndFeelPanel.
349 	 */
350 	protected JPanel getInternetCafeLookAndFeelPanel() {
351 		if (internetCafeLookAndFeelPanel == null) {
352 			internetCafeLookAndFeelPanel = new JPanel();
353 			TitledBorder titledBorder = new TitledBorder(Messages.getString("common.lookandfeel")); //$NON-NLS-1$
354 			internetCafeLookAndFeelPanel.setBorder(titledBorder);
355 			internetCafeLookAndFeelPanel.setLayout(new BorderLayout());
356 			internetCafeLookAndFeelPanel
357 					.add(getInternetCafeLookAndFeelScrollPane(),
358 							BorderLayout.CENTER);
359 			internetCafeLookAndFeelPanel.add(
360 					getInternetCafeLookAndFeelButtonPanel(), BorderLayout.EAST);
361 		}
362 		return internetCafeLookAndFeelPanel;
363 	}
364 
365 	/***
366 	 * @return Returns the internetCafeLookAndFeelScrollPane.
367 	 */
368 	protected JScrollPane getInternetCafeLookAndFeelScrollPane() {
369 		if (internetCafeLookAndFeelScrollPane == null) {
370 			internetCafeLookAndFeelScrollPane = new JScrollPane();
371 			internetCafeLookAndFeelScrollPane
372 					.setViewportView(getInternetCafeLookAndFeelList());
373 		}
374 		return internetCafeLookAndFeelScrollPane;
375 	}
376 }