1 package org.wcb.common.component;
2
3 import org.wcb.autohome.AutoHomeAdminSession;
4 import org.wcb.autohome.implementations.SerialPortBean;
5 import org.wcb.autohome.interfaces.I18nConstants;
6 import org.wcb.autohome.interfaces.X10DeviceConstants;
7 import org.wcb.common.UIDefaultsLoader;
8
9 import javax.swing.*;
10 import javax.swing.border.TitledBorder;
11 import java.awt.*;
12 import java.awt.event.ActionListener;
13 import java.awt.event.ActionEvent;
14 import java.util.Vector;
15 import java.io.File;
16
17 import com.jgoodies.plaf.LookUtils;
18 import com.jgoodies.plaf.Options;
19
20 /***
21 * Copyright (C) 1999 Walter Bogaardt
22 *
23 * This library is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU Lesser General Public
25 * License as published by the Free Software Foundation; either
26 * version 2 of the License, or (at your option) any later version.
27 *
28 * This library is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
31 * Lesser General Public License for more details.
32 *
33 * You should have received a copy of the GNU Lesser General Public
34 * License along with this library; if not, write to the Free Software
35 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
36 *
37 * Project: Alice X10 Home Automation
38 * Filename: $Id: GeneralConfigurationPanel.java,v 1.5 2004/02/27 01:29:53 wbogaardt Exp $<BR>
39 * Abstract: Used to display general configuration settings such as serial port settings and languages.
40 *
41 * $Log: GeneralConfigurationPanel.java,v $
42 * Revision 1.5 2004/02/27 01:29:53 wbogaardt
43 * modified classes so they conform to Checkstyle format in readability
44 *
45 * Revision 1.4 2004/02/03 00:36:41 wbogaardt
46 * added files
47 *
48 * Revision 1.3 2004/01/31 07:40:26 wbogaardt
49 * modified layout managers for the panel so that it uses the java standard layoutsrather than the FormLayout api.
50 * The purpose is to reduce dependency on non-standard layout api.
51 *
52 */
53 public class GeneralConfigurationPanel extends JPanel implements X10DeviceConstants {
54
55 private JCheckBox enableX10;
56 private JCheckBox enableSpeech;
57 private JTextField browserTf;
58 private JButton browserButton;
59 private JComboBox portChoice, baudChoice, dataChoice, stopChoice, parityChoice;
60 private JRadioButton java_radioButton, mac_radioButton, win_radioButton, motif_radioButton, jgoodie_radioButton;
61 private JRadioButton jrbEnglish, jrbDutch, jrbGerman, jrbEnglishEU;
62 private JFileChooser fc;
63 private static String JGOODIE_LAF = LookUtils.isWindowsXP() ? Options.getCrossPlatformLookAndFeelClassName() : Options.getSystemLookAndFeelClassName();
64
65 /***
66 * Creates the default constructor for the General configuration panel
67 * tab.
68 */
69 public GeneralConfigurationPanel() {
70 this.setup();
71 this.setPortValue();
72 }
73
74 /***
75 * Saves the general settings using the
76 * UIDefaults loader which reads in HASConfig.ini
77 * @param properties The properties from HASConfig.ini file
78 */
79 public void saveGeneral(UIDefaultsLoader properties) {
80 properties.setProperty(BROWSER, browserTf.getText());
81 AutoHomeAdminSession.getInstance().setLookAndFeel(this.getLFVal());
82
83 AutoHomeAdminSession.getInstance().connectSerialOnStartup(enableX10.isSelected());
84 AutoHomeAdminSession.getInstance().enableSpeechEngine(enableSpeech.isSelected());
85
86 SerialPortBean serialBean = new SerialPortBean(getStringValueOf(portChoice));
87 serialBean.setBaud(getIntValueOf(baudChoice));
88 serialBean.setDataBit(getIntValueOf(dataChoice));
89 serialBean.setStopBit(getIntValueOf(stopChoice));
90 serialBean.setParity(getStringValueOf(parityChoice));
91 AutoHomeAdminSession.getInstance().setSerialPort(serialBean);
92 }
93
94 private void setup() {
95 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
96 add(this.createBrowserConfigurationPanel());
97 add(this.createLookAndFeelPanel());
98 add(this.createSpeechPanel());
99 add(this.createLanguagePanel());
100 add(this.getConfigurationPanel());
101 this.setupListeners();
102 }
103
104 private void setupListeners() {
105 ActionListener al = new ActionListener() {
106 public void actionPerformed(ActionEvent evt) {
107 Object src = evt.getSource();
108 if (src == browserButton)
109 {
110 findBrowser();
111 }
112 if (src == java_radioButton)
113 {
114 setGuiToLookAndFeel(METAL);
115 }
116 if (src == jgoodie_radioButton)
117 {
118 setGuiToLookAndFeel(JGOODIE_LAF);
119 }
120 if (src == win_radioButton)
121 {
122 setGuiToLookAndFeel(WINDOWS);
123 }
124 if (src == motif_radioButton)
125 {
126 setGuiToLookAndFeel(MOTIF);
127 }
128 if (src == mac_radioButton)
129 {
130 setGuiToLookAndFeel(MAC);
131 }
132 if (src == jrbEnglish)
133 {
134 setToLanguage(0);
135 }
136 if (src == jrbDutch)
137 {
138 setToLanguage(1);
139 }
140 if (src == jrbGerman)
141 {
142 setToLanguage(2);
143 }
144 if (src == jrbEnglishEU)
145 {
146 setToLanguage(3);
147 }
148 }
149 };
150 browserButton.addActionListener(al);
151 java_radioButton.addActionListener(al);
152 jgoodie_radioButton.addActionListener(al);
153 motif_radioButton.addActionListener(al);
154 win_radioButton.addActionListener(al);
155 mac_radioButton.addActionListener(al);
156 jrbEnglish.addActionListener(al);
157 jrbDutch.addActionListener(al);
158 jrbGerman.addActionListener(al);
159 jrbEnglishEU.addActionListener(al);
160 }
161
162 /***
163 * This method sets the look and feel schema to
164 * the user selected option
165 * @param lf Look and feel class name.
166 */
167 private void setGuiToLookAndFeel(String lf) {
168 try
169 {
170 UIManager.setLookAndFeel(lf);
171 this.getLFVal();
172 }
173 catch (Exception ex)
174 {
175 JOptionPane.showMessageDialog(this, "Look & feel\n" + lf
176 + "\n not available for your system",
177 "Look and Feel", JOptionPane.ERROR_MESSAGE);
178 }
179 SwingUtilities.updateComponentTreeUI(this);
180 }
181
182 private JPanel getConfigurationPanel()
183 {
184 GridBagConstraints gridBagConstraints;
185
186 portChoice = new JComboBox();
187 listPortChoices();
188 baudChoice = new JComboBox(BAUD_RATE_ARRAY);
189 baudChoice.setSelectedIndex(2);
190 dataChoice = new JComboBox(DATA_BITS_ARRAY);
191 dataChoice.setSelectedIndex(3);
192 stopChoice = new JComboBox(STOP_BITS_ARRAY);
193 parityChoice = new JComboBox(PARITY_ARRAY);
194
195 JPanel configPanel = new JPanel();
196 configPanel.setLayout(new java.awt.GridBagLayout());
197 configPanel.setBorder(new TitledBorder("Serial Port Settings"));
198 configPanel.add(new JLabel("Serial Port:"), new java.awt.GridBagConstraints());
199
200
201 gridBagConstraints = new GridBagConstraints();
202 gridBagConstraints.gridwidth = 2;
203 gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
204 configPanel.add(portChoice, gridBagConstraints);
205
206 gridBagConstraints = new java.awt.GridBagConstraints();
207 gridBagConstraints.gridx = 0;
208 gridBagConstraints.gridy = 1;
209 gridBagConstraints.ipadx = 2;
210 gridBagConstraints.ipady = 2;
211 configPanel.add(new JLabel("Baud:"), gridBagConstraints);
212
213 gridBagConstraints = new java.awt.GridBagConstraints();
214 gridBagConstraints.gridx = 1;
215 gridBagConstraints.gridy = 1;
216 configPanel.add(baudChoice, gridBagConstraints);
217
218 gridBagConstraints = new java.awt.GridBagConstraints();
219 gridBagConstraints.gridx = 2;
220 gridBagConstraints.gridy = 1;
221 configPanel.add(new JLabel("Bits"), gridBagConstraints);
222
223 gridBagConstraints = new java.awt.GridBagConstraints();
224 gridBagConstraints.gridx = 3;
225 gridBagConstraints.gridy = 1;
226 configPanel.add(dataChoice, gridBagConstraints);
227
228 gridBagConstraints = new java.awt.GridBagConstraints();
229 gridBagConstraints.gridx = 0;
230 gridBagConstraints.gridy = 2;
231 configPanel.add(new JLabel("Stop Bits"), gridBagConstraints);
232
233 gridBagConstraints = new java.awt.GridBagConstraints();
234 gridBagConstraints.gridx = 1;
235 gridBagConstraints.gridy = 2;
236 configPanel.add(stopChoice, gridBagConstraints);
237
238 gridBagConstraints = new java.awt.GridBagConstraints();
239 gridBagConstraints.gridx = 2;
240 gridBagConstraints.gridy = 2;
241 configPanel.add(new JLabel("Parity"), gridBagConstraints);
242
243 gridBagConstraints = new java.awt.GridBagConstraints();
244 gridBagConstraints.gridx = 3;
245 gridBagConstraints.gridy = 2;
246 configPanel.add(parityChoice, gridBagConstraints);
247
248 return configPanel;
249 }
250
251 private JPanel createLookAndFeelPanel()
252 {
253 JPanel jpLookPanel = new JPanel();
254 jpLookPanel.setLayout(new BoxLayout(jpLookPanel, BoxLayout.X_AXIS));
255 jpLookPanel.setBorder(BorderFactory.createTitledBorder("Look & Feel"));
256 ButtonGroup group = new ButtonGroup();
257 java_radioButton = new JRadioButton("Java");
258 motif_radioButton = new JRadioButton("Motif");
259 win_radioButton = new JRadioButton("Windows");
260 mac_radioButton = new JRadioButton("Mac");
261 jgoodie_radioButton = new JRadioButton("JGoodies");
262 motif_radioButton.setEnabled(false);
263 win_radioButton.setEnabled(false);
264 mac_radioButton.setEnabled(false);
265 checkOS();
266 group.add(java_radioButton);
267 group.add(jgoodie_radioButton);
268 group.add(motif_radioButton);
269 group.add(win_radioButton);
270 group.add(mac_radioButton);
271 jpLookPanel.add(java_radioButton);
272 jpLookPanel.add(jgoodie_radioButton);
273 jpLookPanel.add(motif_radioButton);
274 jpLookPanel.add(win_radioButton);
275 jpLookPanel.add(mac_radioButton);
276 this.getLFVal();
277 return jpLookPanel;
278 }
279
280 private JPanel createSpeechPanel() {
281 JPanel jpSpeech = new JPanel();
282 enableX10 = new JCheckBox("Enable X10 connection on start-up");
283 enableSpeech = new JCheckBox("Enable speech");
284 jpSpeech.add(enableX10);
285 jpSpeech.add(enableSpeech);
286 return jpSpeech;
287 }
288
289 private JPanel createLanguagePanel()
290 {
291 JPanel jpLanguagePanel = new JPanel();
292 jpLanguagePanel.setLayout(new BoxLayout(jpLanguagePanel, BoxLayout.X_AXIS));
293 jpLanguagePanel.setBorder(BorderFactory.createTitledBorder("Language"));
294 ButtonGroup group = new ButtonGroup();
295 jrbEnglish = new JRadioButton("English(US)");
296 jrbDutch = new JRadioButton("Dutch");
297 jrbGerman = new JRadioButton("German");
298 jrbEnglishEU = new JRadioButton("English(GB)");
299 group.add(jrbEnglish);
300 group.add(jrbDutch);
301 group.add(jrbGerman);
302 group.add(jrbEnglishEU);
303 jpLanguagePanel.add(jrbEnglish);
304 jpLanguagePanel.add(jrbDutch);
305 jpLanguagePanel.add(jrbGerman);
306 jpLanguagePanel.add(jrbEnglishEU);
307 getLanguage();
308 return jpLanguagePanel;
309 }
310
311 private JPanel createBrowserConfigurationPanel()
312 {
313 JPanel jpBrowserPanel = new JPanel();
314 jpBrowserPanel.setLayout(new BoxLayout(jpBrowserPanel, BoxLayout.X_AXIS));
315 browserTf = new JTextField(16);
316 browserTf.setText(AutoHomeAdminSession.getInstance().getAppProperties().getProperty(BROWSER));
317 browserButton = new JButton(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.FIND_BUTTON));
318 jpBrowserPanel.add(new JLabel("Browser:"));
319 jpBrowserPanel.add(browserTf);
320 jpBrowserPanel.add(browserButton);
321 return jpBrowserPanel;
322 }
323
324 private void setToLanguage(int iLanguage)
325 {
326 AutoHomeAdminSession.getInstance().setCountry(iLanguage);
327 }
328
329 /***
330 * This will return an integer value of the current
331 * look and feel setting within the UIManager
332 * @return Look and feel class name
333 */
334 private String getLFVal() {
335 String lf = UIManager.getLookAndFeel().getClass().getName();
336 if (METAL.equals(lf))
337 {
338 java_radioButton.setSelected(true);
339 return METAL;
340 }
341 if (JGOODIE_LAF.equals(lf))
342 {
343 jgoodie_radioButton.setSelected(true);
344 return JGOODIE_LAF;
345 }
346 if (MAC.equals(lf))
347 {
348 mac_radioButton.setSelected(true);
349 return MAC;
350 }
351 if (MOTIF.equals(lf))
352 {
353 motif_radioButton.setSelected(true);
354 return MOTIF;
355 }
356 if (WINDOWS.equals(lf))
357 {
358 win_radioButton.setSelected(true);
359 return WINDOWS;
360 }
361 return METAL;
362 }
363
364 /***
365 * Gets the selected language as an integer value.
366 * @return Integer indicating language selected.
367 */
368 public int getLanguage()
369 {
370 String sCountry = AutoHomeAdminSession.getInstance().getCountry();
371 if (sCountry.equalsIgnoreCase("US"))
372 {
373 jrbEnglish.setSelected(true);
374 return 0;
375 }
376 if (sCountry.equalsIgnoreCase("NL"))
377 {
378 jrbDutch.setSelected(true);
379 return 1;
380 }
381 if (sCountry.equalsIgnoreCase("DE"))
382 {
383 jrbGerman.setSelected(true);
384 return 2;
385 }
386 if (sCountry.equalsIgnoreCase("GB"))
387 {
388 jrbEnglishEU.setSelected(true);
389 return 3;
390 }
391 return 1;
392 }
393
394
395 /***
396 * Check the os version of the system and enable
397 * the appropriate look and feels radio boxes
398 */
399 private void checkOS() {
400 String osName = System.getProperty("os.name");
401 if ((osName != null) && (osName.indexOf("Mac") != -1))
402 {
403 mac_radioButton.setEnabled(true);
404 }
405 if ((osName != null) && (osName.indexOf("Windows") != -1))
406 {
407 win_radioButton.setEnabled(true);
408 }
409 if ((osName != null) && (osName.indexOf("Solaris") != -1))
410 {
411 motif_radioButton.setEnabled(true);
412 }
413 if ((osName != null) && (osName.indexOf("Linux") != -1))
414 {
415 mac_radioButton.setEnabled(true);
416 win_radioButton.setEnabled(true);
417 motif_radioButton.setEnabled(true);
418 }
419 }
420
421 /***
422 * This get the integer value of the selected
423 * item in the combo box selection
424 * @param cb JCombo box list
425 * @return index number of the selected item in the combo box.
426 */
427 private int getIntValueOf(JComboBox cb) {
428 int returnValue = 0;
429 try
430 {
431 returnValue = Integer.parseInt((String) cb.getSelectedItem());
432 }
433 catch (NumberFormatException err)
434 {
435 }
436 return returnValue;
437 }
438
439 /***
440 * This returns the string value of the selected
441 * Object in the JComboBox
442 */
443 private String getStringValueOf(JComboBox cb) {
444 return (String) cb.getSelectedItem();
445 }
446
447 /***
448 * mutator to set the combo box for port settings
449 * to the value previously saved by the user
450 */
451 private void setPortValue() {
452 SerialPortBean serialBean = AutoHomeAdminSession.getInstance().getSerialPort();
453 try
454 {
455
456 for (int i = 0; i < portChoice.getItemCount(); i++)
457 {
458 if ((portChoice.getItemAt(i)).toString().equalsIgnoreCase(serialBean.getPort()))
459 {
460 portChoice.setSelectedIndex(i);
461 }
462 }
463 }
464 catch (Exception ex)
465 {
466 }
467 int baud = serialBean.getBaud();
468 int databit = serialBean.getDataBit();
469 int stopbit = serialBean.getStopBit();
470 String parity = serialBean.getParity();
471 for (int i = 0; i < BAUD_RATE_ARRAY.length; i++)
472 {
473 if (baud == Integer.parseInt(BAUD_RATE_ARRAY[i]))
474 {
475 baudChoice.setSelectedIndex(i);
476 }
477 }
478 for (int i = 0; i < DATA_BITS_ARRAY.length; i++)
479 {
480 if (databit == Integer.parseInt(DATA_BITS_ARRAY[i]))
481 {
482 dataChoice.setSelectedIndex(i);
483 }
484 }
485 for (int i = 0; i < STOP_BITS_ARRAY.length; i++)
486 {
487 if (stopbit == Integer.parseInt(STOP_BITS_ARRAY[i]))
488 {
489 stopChoice.setSelectedIndex(i);
490 }
491 }
492 for (int i = 0; i < PARITY_ARRAY.length; i++)
493 {
494 if (parity == PARITY_ARRAY[i])
495 {
496 parityChoice.setSelectedIndex(i);
497 }
498 }
499 this.enableX10.setSelected(AutoHomeAdminSession.getInstance().connectToSerialPortOnStartup());
500 this.enableSpeech.setSelected(AutoHomeAdminSession.getInstance().isSpeechEnabled());
501 }
502
503 /***
504 * This enumerates all the available ports in the Comm api
505 * A try catch clause has been wrapped around to catch
506 * a possible missing comm API in the user's class path.
507 * A Message dialog box will be shown to the user in this case.
508 */
509 private void listPortChoices() {
510 Vector vec = AutoHomeAdminSession.getInstance().getAvailablePorts();
511 if (vec.size() > 0)
512 {
513 for (int i = 0; i < vec.size(); i++)
514 {
515 portChoice.addItem(vec.elementAt(i));
516 }
517 }
518 else
519 {
520 portChoice.addItem("NO PORTS FOUND");
521 }
522 }
523
524 /***
525 * Opens a file open dialog box to allow the user to find
526 * the browser and then save that setting into the properties file
527 */
528 private void findBrowser() {
529 if (fc == null)
530 {
531 fc = new JFileChooser();
532 }
533 int fileState = fc.showOpenDialog(null);
534 File file = fc.getSelectedFile();
535 if (file != null && fileState == JFileChooser.APPROVE_OPTION)
536 {
537 browserTf.setText(file.getAbsolutePath());
538 }
539 else if (fileState == JFileChooser.CANCEL_OPTION)
540 {
541 JOptionPane.showMessageDialog(null, "No Browser Selected!");
542 }
543 }
544 }