View Javadoc

1   package org.wcb.autohome;
2   /***
3    * Copyright (C) 1999  Walter Bogaardt
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
18   *
19   */
20  
21  /* special classes needed */
22  import org.wcb.autohome.interfaces.MessageInterface;
23  import javax.swing.JTextField;
24  import javax.swing.JPanel;
25  import javax.swing.JComboBox;
26  import javax.swing.JCheckBox;
27  import javax.swing.JButton;
28  import javax.swing.JProgressBar;
29  import javax.swing.JOptionPane;
30  import javax.swing.JLabel;
31  import javax.swing.BorderFactory;
32  import java.awt.*;
33  import java.awt.event.ActionListener;
34  import java.awt.event.ActionEvent;
35  import java.util.Calendar;
36  import java.util.GregorianCalendar;
37  import org.wcb.autohome.util.SunriseCalculator;
38  import org.wcb.autohome.util.NumericalField;
39  import org.wcb.autohome.exceptions.HomeException;
40  import org.wcb.common.UIDefaultsLoader;
41  
42  public class TimePanel extends JPanel {
43  
44      private JTextField sunriseTf, sunsetTf;
45      private JTextField hourTf, minTf, secTf;
46      private JTextField latitudeDegTf, longitudeDegTf, gmtTf;
47      private JComboBox monthCb, dayCb, dateOfWeekCb, ampmCb;
48      private JCheckBox autoRecoverCb;
49      private MessageInterface msgInterface;
50      private JProgressBar batteryBar;
51      private JButton setTimerButton, resetButton, batteryButton;
52      private String[] months = {"January", "February", "March",
53                                 "April", "May", "June", "July",
54                                 "August", "September", "October",
55                                 "November", "December"};
56      private String[] dayOfWeek = {"Sunday", "Monday", "Tuesday",
57                                    "Wednesday", "Thursday", "Friday",
58                                    "Saturday"};
59      private UIDefaultsLoader prop = null;
60  
61      /***
62       * This class builds a panel that has components
63       * that allow the user to set and modify the time
64       * information stored in the CM11A device only.  Since
65       * the CM17A does not have time storage capabilities
66       * the setEnabled(boolean) method is called to disable
67       * the fields in this panel.
68       */
69      public TimePanel()
70      {
71          setupComponents();
72          setupListeners();
73      }
74  
75      private void setupComponents()
76      {
77          setBorder(BorderFactory.
78                  createTitledBorder(BorderFactory.
79                  createLineBorder(Color.black),
80                          "Two-way Interface Time"));
81          msgInterface = AutoHomeAdminSession.getInstance().getMessageInterface();
82          sunriseTf = new JTextField(4);
83          sunsetTf = new JTextField(4);
84          hourTf = new JTextField(2);
85          hourTf.setDocument(new NumericalField());
86          minTf = new JTextField(2);
87          minTf.setDocument(new NumericalField());
88          secTf = new JTextField(2);
89          secTf.setDocument(new NumericalField());
90          monthCb = new JComboBox(months);
91          dateOfWeekCb = new JComboBox(dayOfWeek);
92          dayCb = new JComboBox();
93          for(int i = 1; i < 32; i++)
94          {
95              dayCb.addItem(new Integer(i));
96          }
97          ampmCb = new JComboBox();
98          ampmCb.addItem("AM");
99          ampmCb.addItem("PM");
100         resetButton = new JButton("Reset");
101         resetButton.setToolTipText("Resets fields to original day and time");
102         setTimerButton = new JButton("Send Time");
103         setTimerButton.setToolTipText("Sets time for CM11A");
104         batteryButton = new JButton("Get Status");
105         longitudeDegTf = new JTextField(5);
106         latitudeDegTf = new JTextField(5);
107         gmtTf = new JTextField(4);
108         batteryBar = new JProgressBar();
109         batteryBar.setStringPainted(true);
110         batteryBar.setString("Battery Level");
111         batteryBar.setMaximum(500);
112         batteryBar.setValue(AutoHomeAdminSession.getInstance().getCM11ABatteryUsage());
113         autoRecoverCb = new JCheckBox("Enable auto recover power failure");
114         this.createMainPanel();
115         setCurrentTime();
116         loadSavedProperties();
117     }
118 
119     /***
120      * Create the main time panel and call to some of the
121      * sub components. This all gets placed on this panel.
122      */
123     private void createMainPanel(){
124         GridBagConstraints gridBagConstraints;
125 
126         setLayout(new java.awt.GridBagLayout());
127 
128         gridBagConstraints = new java.awt.GridBagConstraints();
129         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
130         add(new JLabel("Latitude"), gridBagConstraints);
131 
132         gridBagConstraints = new java.awt.GridBagConstraints();
133         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
134         add(latitudeDegTf, gridBagConstraints);
135 
136         gridBagConstraints = new java.awt.GridBagConstraints();
137         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
138         add(new JLabel("Longitude"), gridBagConstraints);
139 
140         gridBagConstraints = new java.awt.GridBagConstraints();
141         gridBagConstraints.gridx = 3;
142         gridBagConstraints.gridy = 0;
143         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
144         add(longitudeDegTf, gridBagConstraints);
145 
146         gridBagConstraints = new java.awt.GridBagConstraints();
147         gridBagConstraints.gridx = 0;
148         gridBagConstraints.gridy = 1;
149         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
150         add(new JLabel("GMT"), gridBagConstraints);
151 
152         gmtTf.setText("jTextField3");
153         gridBagConstraints = new java.awt.GridBagConstraints();
154         gridBagConstraints.gridx = 1;
155         gridBagConstraints.gridy = 1;
156         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
157         add(gmtTf, gridBagConstraints);
158 
159         gridBagConstraints = new java.awt.GridBagConstraints();
160         gridBagConstraints.gridx = 0;
161         gridBagConstraints.gridy = 2;
162         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
163         add(new JLabel("Sunrise"), gridBagConstraints);
164 
165         gridBagConstraints = new java.awt.GridBagConstraints();
166         gridBagConstraints.gridx = 1;
167         gridBagConstraints.gridy = 2;
168         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
169         add(sunriseTf, gridBagConstraints);
170 
171 
172         gridBagConstraints = new java.awt.GridBagConstraints();
173         gridBagConstraints.gridx = 2;
174         gridBagConstraints.gridy = 2;
175         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
176         add(new JLabel("Sunset"), gridBagConstraints);
177 
178 
179         gridBagConstraints = new java.awt.GridBagConstraints();
180         gridBagConstraints.gridx = 3;
181         gridBagConstraints.gridy = 2;
182         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
183         add(sunsetTf, gridBagConstraints);
184 
185         gridBagConstraints = new java.awt.GridBagConstraints();
186         gridBagConstraints.gridx = 0;
187         gridBagConstraints.gridy = 4;
188         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
189         add(monthCb, gridBagConstraints);
190 
191         gridBagConstraints = new java.awt.GridBagConstraints();
192         gridBagConstraints.gridx = 1;
193         gridBagConstraints.gridy = 4;
194         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
195         add(dayCb, gridBagConstraints);
196 
197         gridBagConstraints = new java.awt.GridBagConstraints();
198         gridBagConstraints.gridx = 2;
199         gridBagConstraints.gridy = 4;
200         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
201         add(dateOfWeekCb, gridBagConstraints);
202 
203         gridBagConstraints = new java.awt.GridBagConstraints();
204         gridBagConstraints.gridx = 0;
205         gridBagConstraints.gridy = 3;
206         gridBagConstraints.gridwidth = 4;
207         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
208         add(this.createClockPanel(), gridBagConstraints);
209 
210         gridBagConstraints = new java.awt.GridBagConstraints();
211         gridBagConstraints.gridx = 0;
212         gridBagConstraints.gridy = 6;
213         gridBagConstraints.gridwidth = 2;
214         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
215         add(batteryBar, gridBagConstraints);
216 
217         gridBagConstraints = new java.awt.GridBagConstraints();
218         gridBagConstraints.gridx = 2;
219         gridBagConstraints.gridy = 6;
220         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
221         add(resetButton, gridBagConstraints);
222 
223         gridBagConstraints = new java.awt.GridBagConstraints();
224         gridBagConstraints.gridx = 3;
225         gridBagConstraints.gridy = 6;
226         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
227         add(setTimerButton, gridBagConstraints);
228 
229         gridBagConstraints = new java.awt.GridBagConstraints();
230         gridBagConstraints.gridx = 0;
231         gridBagConstraints.gridy = 7;
232         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
233         add(batteryButton, gridBagConstraints);
234 
235         gridBagConstraints = new java.awt.GridBagConstraints();
236         gridBagConstraints.gridx = 0;
237         gridBagConstraints.gridy = 5;
238         gridBagConstraints.gridwidth = 3;
239         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
240         add(autoRecoverCb, gridBagConstraints);
241     }
242 
243     /***
244      * This creates the clock panel with drop downs
245      * @return Reference to the clock panel.
246      */
247     private JPanel createClockPanel(){
248         JPanel jpClock = new JPanel();
249         jpClock.add(new JLabel("Set Time:"));
250         JLabel colon = new JLabel(":");
251         jpClock.add(hourTf);
252 
253         jpClock.add(colon);
254         jpClock.add(minTf);
255         jpClock.add(colon);
256         jpClock.add(secTf);
257         jpClock.add(ampmCb);
258         return jpClock;
259     }
260 
261     /***
262      * Calls the status bar message interface
263      * to refresh the sunrise and sunset times
264      * to the status bar indicators.
265      */
266     public void refresh()
267     {
268         msgInterface.setSunriseTime(sunriseTf.getText());
269         msgInterface.setSunsetTime(sunsetTf.getText());
270     }
271 
272     /***
273      * Takes the boolean value and enables or disables fields on
274      * the time setting panel based on X10 interface
275      * being used.  CM11A currently allows setting of its clock
276      * as the CM17A does not have storage capabilities to a
277      * clock
278      */
279     public void setEnabled(boolean val)
280     {
281         hourTf.setEnabled(val);
282         minTf.setEnabled(val);
283         secTf.setEnabled(val);
284         monthCb.setEnabled(val);
285         dayCb.setEnabled(val);
286         dateOfWeekCb.setEnabled(val);
287         ampmCb.setEnabled(val);
288         batteryBar.setEnabled(val);
289         setTimerButton.setEnabled(val);
290         resetButton.setEnabled(val);
291         msgInterface.isGatewayCM11A(val);
292     }
293 
294     private void setCurrentTime()
295     {
296         GregorianCalendar day = new GregorianCalendar();
297         if (day.get(Calendar.HOUR) == 0)
298             hourTf.setText("12");
299         else
300             hourTf.setText("" + day.get(Calendar.HOUR));
301         minTf.setText("" + day.get(Calendar.MINUTE));
302         secTf.setText("" + day.get(Calendar.SECOND));
303         monthCb.setSelectedIndex(day.get(Calendar.MONTH));
304         dayCb.setSelectedIndex(day.get(Calendar.DAY_OF_MONTH) - 1);
305         dateOfWeekCb.setSelectedIndex(day.get(Calendar.DAY_OF_WEEK) - 1);
306         if (day.get(Calendar.HOUR_OF_DAY) >= 12)
307         {
308             ampmCb.setSelectedIndex(1);
309         }
310         else
311         {
312             ampmCb.setSelectedIndex(0);
313         }
314     }
315 
316     private void setupListeners()
317     {
318         ActionListener al = new ActionListener(){
319             public void actionPerformed(ActionEvent evt)
320             {
321                 Object src = evt.getSource();
322                 if (src == setTimerButton){
323                     Toolkit.getDefaultToolkit().beep();
324                     setBatteryTimer();
325                     setAutoRecover();
326                 }
327                 if (src == resetButton)
328                 {
329                     getSunriseTime();
330                 }
331                 if(src == batteryButton)
332                 {
333                     //is auto recover true?
334                     AutoHomeAdminSession.getInstance().updateCM11AStatus();
335                     //update date time fields
336                     loadSavedProperties();
337                     //get battery usage value.
338                     int iValue = AutoHomeAdminSession.getInstance().getCM11ABatteryUsage();
339                      batteryBar.setValue(500 - iValue);
340                 }
341                 if (src == gmtTf)
342                 {
343                     getSunriseTime();
344                 }
345             }
346         };
347         setTimerButton.addActionListener(al);
348         resetButton.addActionListener(al);
349         gmtTf.addActionListener(al);
350         batteryButton.addActionListener(al);
351     }
352 
353     /***
354      * Set the battery timer and the battery bar to the
355      * reset full value. This is So if the battery has
356      * been replaced it can be indcated on the battery bar.
357      */
358     private void setBatteryTimer(){
359         int answer = JOptionPane.showConfirmDialog(null,
360                 "Do you want to reset the CM11A battery timer as well?");
361         if (answer == JOptionPane.YES_OPTION)
362         {
363             sendClockTimeToX10(true);
364             batteryBar.setValue(500);
365         }
366         if (answer == JOptionPane.NO_OPTION)
367         {
368             sendClockTimeToX10(false);
369         }
370     }
371 
372     /***
373      * This allows the CM11A to auto recover itself from
374      * a power failure.  Check if the user did not set auto
375      * recover on the gui to true that they
376      * really want to do this.
377      */
378     private void setAutoRecover(){
379         if(!autoRecoverCb.isSelected())
380         {
381             AutoHomeAdminSession.getInstance().recoverCM11A(false);
382         }
383         else if(autoRecoverCb.isSelected())
384         {
385             AutoHomeAdminSession.getInstance().recoverCM11A(true);
386         }
387     }
388 
389     /***
390      * Save properties of latitude, longitude, GMT, sunrise
391      * and sunset times to a properties file.
392      */
393     public void saveProperties()
394     {
395         prop = AutoHomeAdminSession.getInstance().getAppProperties();
396         prop.setProperty("latitude", latitudeDegTf.getText());
397         prop.setProperty("longitude", longitudeDegTf.getText());
398         prop.setProperty("GMT", gmtTf.getText());
399         prop.setProperty("sunrise", sunriseTf.getText());
400         prop.setProperty("sunset", sunsetTf.getText());
401         prop.saveProperties();
402     }
403 
404     /***
405      * Load previously saved properties from a properties file
406      * this includes sunrise, sunset, longitude, latitude,
407      * and GMT information.
408      */
409     private void loadSavedProperties()
410     {
411         prop = AutoHomeAdminSession.getInstance().getAppProperties();
412         sunriseTf.setText(prop.getProperty("sunrise"));
413         sunsetTf.setText(prop.getProperty("sunset"));
414         latitudeDegTf.setText(prop.getProperty("latitude"));
415         longitudeDegTf.setText(prop.getProperty("longitude"));
416         gmtTf.setText(prop.getProperty("GMT"));
417         autoRecoverCb.setSelected(AutoHomeAdminSession.getInstance().isAutoRecoverCM11A());
418         Calendar cal = AutoHomeAdminSession.getInstance().getCM11ADate();
419         hourTf.setText(cal.get(Calendar.HOUR)+"");
420         minTf.setText(cal.get(Calendar.MINUTE)+"");
421         secTf.setText(cal.get(Calendar.SECOND)+"");
422         monthCb.setSelectedIndex(cal.get(Calendar.MONTH));
423         dateOfWeekCb.setSelectedIndex(cal.get(Calendar.DAY_OF_WEEK) - 1);
424         dayCb.setSelectedIndex(cal.get(Calendar.DAY_OF_MONTH));
425     }
426 
427     private void getSunriseTime()
428     {
429         try
430         {
431             Double latitude = Double.valueOf(latitudeDegTf.getText());
432             Double longitude = Double.valueOf(longitudeDegTf.getText());
433             Double gmt = Double.valueOf(gmtTf.getText());
434             SunriseCalculator calc = new SunriseCalculator(latitude.doubleValue(),
435                     longitude.doubleValue(), gmt.doubleValue());
436             sunriseTf.setText(calc.getSunrise());
437             sunsetTf.setText(calc.getSunset());
438         }
439         catch(NumberFormatException err)
440         {
441 
442         }
443         refresh();
444     }
445 
446     private void sendClockTimeToX10(boolean resetClockBattery){
447         int hour = 0;
448         if (ampmCb.getSelectedIndex() == 1)
449         {
450             try
451             {
452                 hour = Integer.parseInt(hourTf.getText());
453                 if (hour != 12)
454                     hour = hour + 12;
455             }
456             catch(NumberFormatException exc)
457             {
458                 hourTf.setText("");
459             }
460         }
461         try {
462             GregorianCalendar calendar = new GregorianCalendar();
463 
464             int minute = Integer.parseInt(minTf.getText());
465             int seconds = Integer.parseInt(secTf.getText());
466             int month = monthCb.getSelectedIndex();
467             int day = dayCb.getSelectedIndex() + 1;
468             int dayofweek = dateOfWeekCb.getSelectedIndex();
469 
470             calendar.set(Calendar.HOUR_OF_DAY, hour);
471             calendar.set(Calendar.MINUTE, minute);
472             calendar.set(Calendar.SECOND, seconds);
473             calendar.set(Calendar.MONTH, month);
474             calendar.set(Calendar.DAY_OF_MONTH, day);
475             calendar.set(Calendar.DAY_OF_WEEK, dayofweek);
476             AutoHomeAdminSession.getInstance().setCM11AClock(calendar, 'A', resetClockBattery,true,false);
477         }
478         catch (HomeException err)
479         {
480             showErrorMessage(err.toString());
481         }
482     }
483 
484     private void showErrorMessage(String errorString)
485     {
486         Toolkit.getDefaultToolkit().beep();
487         JOptionPane.showMessageDialog(null, errorString, "Failed Data",
488                 JOptionPane.ERROR_MESSAGE);
489     }
490 }
491 
492 
493 
494 
495 
496 
497