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   * Project: Alice X10 Home Automation
20   *
21   *  $Log: MacroTriggerPanel.java,v $
22   *  Revision 1.20  2004/07/22 03:06:49  wbogaardt
23   *  removed deprecated method calls.
24   *
25   *  Revision 1.19  2004/06/09 18:51:50  wbogaardt
26   *  changed enum to enumr avoid keyword issue
27   *
28   *  Revision 1.18  2004/02/27 01:29:47  wbogaardt
29   *  modified classes so they conform to Checkstyle format in readability
30   *
31   *  Revision 1.17  2004/02/06 23:27:13  wbogaardt
32   *  added clock label to internationalization, refactored menu bar from home center panel, fixed display of JTimeDialog so that it looks a bit better.
33   *
34   *  Revision 1.16  2004/02/06 20:06:15  wbogaardt
35   *  replaced ampm drop boxes with time buttons which launch a time panel move menu items around on main panel
36   *
37   *  Revision 1.15  2004/02/02 05:26:17  wbogaardt
38   *   removed form panel layout
39   *
40   *  Revision 1.14  2004/01/18 00:48:31  wbogaardt
41   *  refactored out unnecessary code and now have a functional initial design of monitoring panel
42   *
43   *  Revision 1.13  2004/01/16 23:52:46  wbogaardt
44   *  fixed trigger panel to disable time events options when app first starts up also made the events table modle display dim and brighten instead of + and - to the user.
45   *
46   *  Revision 1.12  2004/01/16 19:50:14  wbogaardt
47   *  refactored, fixed long standing bug with updating macro panels, add error notification to user for improper device codes
48   *
49   *  Revision 1.11  2004/01/16 00:53:34  wbogaardt
50   *  Fixed a very obscure bug with the Macro Panel that it didn't added new
51   *  x10 devices to the drop down of available x10 device for the macro. Modified Macro triggers to change the events to integer verses strings cleaner this way.
52   *
53   *  Revision 1.10  2004/01/15 21:05:17  wbogaardt
54   *  major revamp of Modules and interfaces changes overall structure of how information is stored
55   *
56   *  Revision 1.9  2003/12/30 18:47:40  wbogaardt
57   *  made labels so they are internationlized and fixed layout of trigger panel
58   *
59   *  Revision 1.8  2003/12/30 00:56:45  wbogaardt
60   *  added more internationalization to table column names.
61   *
62   *  Revision 1.7  2003/12/20 06:16:00  wbogaardt
63   *  moved most buttons text to i18n internationalization.
64   *
65   *  Revision 1.6  2003/12/12 23:17:33  wbogaardt
66   *  javadoc comments refactored methods so they are more descriptive
67   *
68   *  Revision 1.5  2003/10/10 22:50:43  wbogaardt
69   *  removed error messages and cleaned up format
70   *
71   *  Revision 1.4  2003/10/10 22:35:33  wbogaardt
72   *  fixed parsing of date information for am and pm values.
73   *
74   *  Revision 1.3  2003/10/10 21:39:01  wbogaardt
75   *  modified macro triggers to use calendar in stead of strings
76   *
77   *  Revision 1.2  2003/10/06 23:42:02  wbogaardt
78   *  Refactored code so that class is extracted from panel
79   *
80   */
81  import javax.swing.*;
82  import javax.swing.event.ListSelectionListener;
83  import javax.swing.event.ListSelectionEvent;
84  import javax.swing.table.TableColumn;
85  
86  /* standard java stuff */
87  import java.awt.*;
88  import java.awt.event.ActionListener;
89  import java.awt.event.ActionEvent;
90  import java.util.*;
91  import java.text.SimpleDateFormat;
92  import java.text.ParseException;
93  
94  /* special classes needed */
95  import org.wcb.autohome.util.TriggerRenderer;
96  import org.wcb.autohome.util.TriggerItem;
97  import org.wcb.autohome.util.DeviceIDRenderer;
98  import org.wcb.autohome.util.ui.TriggerRender;
99  import org.wcb.autohome.implementations.MacroTrigger;
100 import org.wcb.autohome.interfaces.*;
101 import org.wcb.util.TableSorter;
102 import org.wcb.util.SortButtonRenderer;
103 import org.wcb.util.TooltippedTable;
104 import org.wcb.autohome.model.MacroTriggerModel;
105 import org.wcb.common.JTimeDialog;
106 
107 /***
108  * Filename:    $Id: MacroTriggerPanel.java,v 1.20 2004/07/22 03:06:49 wbogaardt Exp $
109  *
110  * Abstract:  This class renders the trigger panel where you can add or modify a particular
111  *            macro trigger.  That is the user specifies the triggers whether they are event based
112  *            such as a light being turned on to trigger a particular macro to execute or if the trigger is time
113  *            based instaniating a macro to excecute at a particular time and date. See the Macro Sequence Panel
114  *          in reference to having an assigned macro fire off a number of actions such as turning on all lights
115  *          and appliances in area A or B.
116  *
117  * @author wbogaardt
118  */
119 public class MacroTriggerPanel extends JPanel implements RefreshInterface, SwingConstants, X10DeviceConstants {
120 
121     private JComboBox moduleIDCb, eventCb, triggerIDCb, macroEventCombo, macroStopEventCombo;
122     private JButton jbTimeButtonStart, jbTimeButtonEnd;
123     private JButton okStartButton, okEndButton;
124     private JTimeDialog jTimeDialog;
125     private String[] eventsString = {"Off", "On"};
126     private JToggleButton sunTB, monTB, tueTB, wedTB, thuTB, friTB, satTB;
127     private JButton addMacroButton, delMacroButton, updateMacroButton;
128     private JTextField startTimeTf, stopTimeTf, macroDescriptionTf;
129     private ImageIcon nDate, yDate;
130     private MacroTriggerModel macroModel = null;
131     private Vector MACROS;
132     private TooltippedTable macroTable;
133     private TableSorter macroSorter;
134     private SortButtonRenderer macroSortButtonRenderer;
135     private ListSelectionListener macroListListener;
136     private JPanel tablePanel;
137     private JScrollPane macroScroll;
138     private static final SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");
139 
140     /***
141      * This creates the macro trigger panel component for the
142      * swing application.
143      */
144     public MacroTriggerPanel()
145     {
146         setupComponents();
147         setupListeners();
148         loadTriggers();
149     }
150 
151     /***
152      *Sets up all the componets onto the JPanel object
153      */
154     private void setupComponents()
155     {
156         /* default data for table*/
157         Vector defaultData = new Vector();
158         defaultData.addElement(new MacroTrigger());
159 
160         setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
161         setBorder(BorderFactory.createTitledBorder(BorderFactory
162                 .createLineBorder(Color.black), AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.MACRO_INIT_TITLE_LABEL)));
163         yDate = AutoHomeAdminSession.DAYON;
164         nDate = AutoHomeAdminSession.DAYOFF;
165 
166         moduleIDCb = new JComboBox();
167         this.updateDeviceModules();
168         triggerIDCb = new JComboBox();
169         addTriggerID(triggerIDCb);
170         macroEventCombo = new JComboBox();
171         addSavedMacros(macroEventCombo);
172         macroStopEventCombo = new JComboBox();
173         addSavedMacros(macroStopEventCombo);
174         macroDescriptionTf = new JTextField(15);
175         eventCb = new JComboBox(eventsString);
176         jbTimeButtonStart = new JButton(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.CLOCK_LABEL));
177         jbTimeButtonEnd = new JButton(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.CLOCK_LABEL));
178         okStartButton = new JButton("OK");
179         okEndButton = new JButton("OK");
180         addMacroButton = new JButton(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.ADD_BUTTON));
181         delMacroButton = new JButton(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.DELETE_BUTTON));
182         updateMacroButton = new JButton(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.UPDATE_BUTTON));
183         startTimeTf = new JTextField(6);
184         startTimeTf.setEditable(false);
185         stopTimeTf = new JTextField(6);
186         stopTimeTf.setEditable(false);
187         /* build special buttons */
188         sunTB = new JToggleButton(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.SUN_BUTTON), nDate);
189         setupButton(sunTB);
190         monTB = new JToggleButton(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.MON_BUTTON), nDate);
191         setupButton(monTB);
192         tueTB = new JToggleButton(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.TUE_BUTTON), nDate);
193         setupButton(tueTB);
194         wedTB = new JToggleButton(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.WED_BUTTON), nDate);
195         setupButton(wedTB);
196         thuTB = new JToggleButton(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.THU_BUTTON), nDate);
197         setupButton(thuTB);
198         friTB = new JToggleButton(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.FRI_BUTTON), nDate);
199         setupButton(friTB);
200         satTB = new JToggleButton(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.SAT_BUTTON), nDate);
201         setupButton(satTB);
202 
203         tablePanel = new JPanel();
204 
205         macroModel = new MacroTriggerModel(defaultData);
206         macroSortButtonRenderer = new SortButtonRenderer();
207         if (macroModel != null)
208         {
209             /* sorting stuff for the table */
210             macroSorter = new TableSorter(macroModel);
211             macroTable = new TooltippedTable(macroSorter);
212             macroTable.setColHeaderRender(macroSortButtonRenderer);
213             macroSorter.addMouseListenerToHeaderInTable(macroTable, macroSortButtonRenderer);
214             macroSorter.sortByColumn(2, false);
215             TableColumn lampColumn = macroTable.getColumn(
216                     AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.TRIGGER_COLUMN));
217             lampColumn.setCellRenderer(new TriggerRender());
218         }
219         macroScroll = new JScrollPane(macroTable);
220         macroScroll.setPreferredSize(new Dimension(610, 115));
221         tablePanel.add(macroScroll);
222         add(tablePanel);
223         add(this.createDetailPanel());
224         add(this.createWeekdays());
225         add(this.createButtonsPanel());
226         this.setTimedActionControls(false);
227     }
228 
229     /***
230      * This creates the times and selected modules and macros detail information
231      * for the triggers.
232      * @return The panel after the table panel.
233      */
234     private JPanel createDetailPanel() {
235         GridBagConstraints gridBagConstraints;
236         JPanel detailPanel = new javax.swing.JPanel();
237         detailPanel.setLayout(new GridBagLayout());
238 
239         gridBagConstraints = new GridBagConstraints();
240         gridBagConstraints.anchor = GridBagConstraints.WEST;
241         detailPanel.add(new JLabel(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.TRIGGER_LABEL)), gridBagConstraints);
242 
243         gridBagConstraints = new GridBagConstraints();
244         gridBagConstraints.gridwidth = 2;
245         gridBagConstraints.anchor = GridBagConstraints.WEST;
246         detailPanel.add(triggerIDCb, gridBagConstraints);
247 
248         gridBagConstraints = new GridBagConstraints();
249         gridBagConstraints.gridx = 3;
250         gridBagConstraints.gridy = 0;
251         gridBagConstraints.anchor = GridBagConstraints.WEST;
252         detailPanel.add(new JLabel(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.DESCRIPTION_LABEL)), gridBagConstraints);
253 
254         gridBagConstraints = new GridBagConstraints();
255         gridBagConstraints.gridx = 4;
256         gridBagConstraints.gridy = 0;
257         gridBagConstraints.gridwidth = 2;
258         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
259         gridBagConstraints.anchor = GridBagConstraints.WEST;
260         detailPanel.add(macroDescriptionTf, gridBagConstraints);
261 
262         gridBagConstraints = new GridBagConstraints();
263         gridBagConstraints.gridx = 0;
264         gridBagConstraints.gridy = 1;
265         gridBagConstraints.anchor = GridBagConstraints.WEST;
266         detailPanel.add(new JLabel(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.INSTALLED_MODULES_LABEL)), gridBagConstraints);
267 
268         gridBagConstraints = new GridBagConstraints();
269         gridBagConstraints.gridx = 1;
270         gridBagConstraints.gridy = 1;
271         gridBagConstraints.gridwidth = 2;
272         gridBagConstraints.anchor = GridBagConstraints.WEST;
273         detailPanel.add(moduleIDCb, gridBagConstraints);
274 
275         gridBagConstraints = new GridBagConstraints();
276         gridBagConstraints.gridx = 3;
277         gridBagConstraints.gridy = 1;
278         gridBagConstraints.anchor = GridBagConstraints.WEST;
279         detailPanel.add(eventCb, gridBagConstraints);
280 
281         gridBagConstraints = new GridBagConstraints();
282         gridBagConstraints.gridx = 6;
283         gridBagConstraints.gridy = 1;
284         detailPanel.add(new JLabel(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.START_MACRO_LABEL)), gridBagConstraints);
285 
286         gridBagConstraints = new GridBagConstraints();
287         gridBagConstraints.gridx = 7;
288         gridBagConstraints.gridy = 1;
289         detailPanel.add(macroEventCombo, gridBagConstraints);
290 
291         gridBagConstraints = new GridBagConstraints();
292         gridBagConstraints.gridx = 0;
293         gridBagConstraints.gridy = 2;
294         gridBagConstraints.anchor = GridBagConstraints.WEST;
295         detailPanel.add(new JLabel(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.START_LABEL)), gridBagConstraints);
296 
297         gridBagConstraints = new GridBagConstraints();
298         gridBagConstraints.gridx = 1;
299         gridBagConstraints.gridy = 2;
300         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
301         gridBagConstraints.anchor = GridBagConstraints.WEST;
302         detailPanel.add(startTimeTf, gridBagConstraints);
303 
304         gridBagConstraints = new GridBagConstraints();
305         gridBagConstraints.gridx = 2;
306         gridBagConstraints.gridy = 2;
307         detailPanel.add(jbTimeButtonStart, gridBagConstraints);
308 
309         gridBagConstraints = new GridBagConstraints();
310         gridBagConstraints.gridx = 3;
311         gridBagConstraints.gridy = 2;
312         gridBagConstraints.anchor = GridBagConstraints.WEST;
313         detailPanel.add(new JLabel(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.STOP_LABEL)), gridBagConstraints);
314 
315         gridBagConstraints = new GridBagConstraints();
316         gridBagConstraints.gridx = 4;
317         gridBagConstraints.gridy = 2;
318         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
319         gridBagConstraints.anchor = GridBagConstraints.WEST;
320         detailPanel.add(stopTimeTf, gridBagConstraints);
321 
322         gridBagConstraints = new GridBagConstraints();
323         gridBagConstraints.gridx = 5;
324         gridBagConstraints.gridy = 2;
325         gridBagConstraints.anchor = GridBagConstraints.WEST;
326         detailPanel.add(jbTimeButtonEnd, gridBagConstraints);
327 
328         gridBagConstraints = new GridBagConstraints();
329         gridBagConstraints.gridx = 6;
330         gridBagConstraints.gridy = 2;
331         detailPanel.add(new JLabel(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.STOP_MACRO_LABEL)), gridBagConstraints);
332 
333         gridBagConstraints = new GridBagConstraints();
334         gridBagConstraints.gridx = 7;
335         gridBagConstraints.gridy = 2;
336         detailPanel.add(macroStopEventCombo, gridBagConstraints);
337         return detailPanel;
338     }
339 
340     /***
341      * Create the panel with toggle buttons which set the various
342      * weekdays to enable the macro on.
343      * @return The weekday toggle button panel.
344      */
345     private JPanel createWeekdays() {
346         JPanel jpWeekday = new JPanel();
347         jpWeekday.add(new JLabel(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.WEEKDAY_LABEL)));
348         jpWeekday.add(sunTB);
349         jpWeekday.add(monTB);
350         jpWeekday.add(tueTB);
351         jpWeekday.add(wedTB);
352         jpWeekday.add(thuTB);
353         jpWeekday.add(friTB);
354         jpWeekday.add(satTB);
355         return jpWeekday;
356     }
357 
358     /***
359      * Create the add delete and update panel
360      * @return the panel that allows adding of triggers
361      */
362     private JPanel createButtonsPanel() {
363         JPanel jpButtons = new JPanel();
364         jpButtons.add(addMacroButton);
365         jpButtons.add(updateMacroButton);
366         jpButtons.add(delMacroButton);
367         return jpButtons;
368     }
369     /***
370      *This sets up all the necessary action listeners for the tables
371      *or other button based commands.
372      *
373      */
374     private void setupListeners()
375     {
376         macroListListener = new ListSelectionListener() {
377             public void valueChanged(ListSelectionEvent e) {
378                 if (!e.getValueIsAdjusting())
379                 {
380                     clearAllViews();
381                     setDetails();
382                 }
383             }
384         };
385         if (macroModel != null)
386         {
387             macroTable.getSelectionModel().addListSelectionListener(macroListListener);
388         }
389         ActionListener al = new ActionListener() {
390             public void actionPerformed(ActionEvent evt) {
391                 Object src = evt.getSource();
392                 if (src == triggerIDCb)
393                 {
394                     if (triggerIDCb.getSelectedIndex() == 0)
395                     {
396                         setTimedActionControls(false);
397                     }
398                     if (triggerIDCb.getSelectedIndex() == 1)
399                     {
400                         setTimedActionControls(true);
401                     }
402                 }
403                 if (src == addMacroButton)
404                 {
405                     addMacro();
406                 }
407                 if (src == updateMacroButton)
408                 {
409                     updateMacroRow();
410                 }
411                 if (src == delMacroButton)
412                 {
413                     deleteMacroRow();
414                 }
415                  if (src == jbTimeButtonStart)
416                 {
417                     GregorianCalendar time = new GregorianCalendar();
418                     if (startTimeTf.getText() != null)
419                     {
420                         try
421                         {
422                             time.setTime(sdf.parse(startTimeTf.getText()));
423                         }
424                         catch (ParseException pe)
425                         {
426                             //dialog will default to current time
427                         }
428                     }
429                     jTimeDialog = new JTimeDialog(null, time, okStartButton);
430                     jTimeDialog.setLocationRelativeTo(MacroTriggerPanel.this);
431                     jTimeDialog.setVisible(true);
432                 }
433                 if (src == jbTimeButtonEnd)
434                 {
435                     GregorianCalendar time = new GregorianCalendar();
436                     if (stopTimeTf.getText() != null)
437                     {
438                         try
439                         {
440                             time.setTime(sdf.parse(stopTimeTf.getText()));
441                         }
442                         catch (ParseException pe)
443                         {
444                             //dialog will default to current time
445                         }
446                     }
447                     jTimeDialog = new JTimeDialog(null, time, okEndButton);
448                     jTimeDialog.setLocationRelativeTo(MacroTriggerPanel.this);
449                     jTimeDialog.setVisible(true);
450                 }
451                 if (src == okStartButton)
452                 {
453                     startTimeTf.setText(jTimeDialog.getTime());
454                     jTimeDialog.dispose();
455                 }
456                  if (src == okEndButton)
457                 {
458                     stopTimeTf.setText(jTimeDialog.getTime());
459                     jTimeDialog.dispose();
460                 }
461             }
462         };
463         triggerIDCb.addActionListener(al);
464         addMacroButton.addActionListener(al);
465         updateMacroButton.addActionListener(al);
466         delMacroButton.addActionListener(al);
467         jbTimeButtonStart.addActionListener(al);
468         jbTimeButtonEnd.addActionListener(al);
469         okStartButton.addActionListener(al);
470         okEndButton.addActionListener(al);
471     }
472 
473     /***
474      * This returns an instance of the MessageInterface
475      * that is used in this class to control the various
476      * messages.
477      * @return The interface refres to this class.
478      */
479     public RefreshInterface getInterface()
480     {
481         return this;
482     }
483 
484     /***
485      *Allows the items to refresh across multiple
486      *objects and in this case the GUI.
487      */
488     public void refresh()
489     {
490         this.updateDeviceModules();
491         this.addSavedMacros(macroEventCombo);
492         this.loadTriggers();
493         this.addSavedMacros(macroStopEventCombo);
494     }
495 
496     /***
497      * This determines whether to enable or disable
498      * the Timer based control options. Because
499      * timer based macros have more options then just
500      * the basic event detected macros.
501      * @param val true enables the time based buttons
502      */
503     private void setTimedActionControls(boolean val)
504     {
505         jbTimeButtonStart.setEnabled(val);
506         jbTimeButtonEnd.setEnabled(val);
507         //ampmCb.setEnabled(val);
508         //ampm2Cb.setEnabled(val);
509         startTimeTf.setEnabled(val);
510         stopTimeTf.setEnabled(val);
511         moduleIDCb.setEnabled(!val);
512         eventCb.setEnabled(!val);
513         sunTB.setEnabled(val);
514         monTB.setEnabled(val);
515         tueTB.setEnabled(val);
516         wedTB.setEnabled(val);
517         thuTB.setEnabled(val);
518         friTB.setEnabled(val);
519         satTB.setEnabled(val);
520         macroStopEventCombo.setEnabled(val);
521     }
522 
523     /***
524      * unselects all of the days of the week buttons
525      */
526     private void clearAllViews()
527     {
528         sunTB.setSelected(false);
529         monTB.setSelected(false);
530         tueTB.setSelected(false);
531         wedTB.setSelected(false);
532         thuTB.setSelected(false);
533         friTB.setSelected(false);
534         satTB.setSelected(false);
535     }
536 
537     /***
538      * takes a table vector and puts it into
539      * the table model format then refreshes the UI
540      * to display the new table information.
541      * @param tableData data to load to the table view.
542      */
543     public void setModel(Vector tableData)
544     {
545         if (tableData != null)
546         {
547             macroModel.setList(tableData);
548         }
549         updateUI();
550     }
551 
552     /***
553      * allows creates icons of the individual devices that
554      * were created from the Module panel
555      */
556     private void updateDeviceModules()
557     {
558         moduleIDCb = AutoHomeAdminSession.getInstance().setRenderedModules(moduleIDCb);
559         moduleIDCb.setRenderer(new DeviceIDRenderer());
560     }
561 
562     private void addSavedMacros(JComboBox comboBox)
563     {
564         comboBox = AutoHomeAdminSession.getInstance().getAvailableMacroEvents(comboBox);
565     }
566 
567     private void setupButton(JToggleButton jtb)
568     {
569         jtb.setSelectedIcon(yDate);
570         jtb.setVerticalTextPosition(TOP);
571         jtb.setHorizontalTextPosition(CENTER);
572     }
573 
574     /***
575      * Builds a trigger ICON for the trigger
576      * combo box.
577      */
578     public void addTriggerID(JComboBox comboBox)
579     {
580         comboBox.addItem(new TriggerItem(new Object[] {new Integer(TRIGGER_EVENT), AutoHomeAdminSession.CAUTIONICON}));
581         comboBox.addItem(new TriggerItem(new Object[] {new Integer(TIMER_EVENT), AutoHomeAdminSession.CLOCKICON}));
582         comboBox.setRenderer(new TriggerRenderer());
583     }
584 
585     /***
586      * loads the Macro Events from the AutoHomeAdminSession object
587      * That call in turn calls DeviceFactory which handles calling the
588      *file system and loading the macros into it.
589      */
590     public void loadTriggers()
591     {
592         setModel(AutoHomeAdminSession.getInstance().loadCM11AMacroTriggers());
593         MACROS = AutoHomeAdminSession.getInstance().loadAllCM11AMacros();
594     }
595 
596     /***
597      * builds a vector which is used to display
598      * in the Macro table view as a row. The information is retreived
599      * from the bottom pane which has various combo boxes and
600      * textfields where the data is derived, and compiled
601      * into a vector for insertion into the table
602      * @return The macro trigger of the selected detail item
603      */
604     private IMacroTrigger getDetailItem()
605     {
606         MacroTrigger macroTrigger = new MacroTrigger();
607         if (triggerIDCb.getSelectedIndex() == 0)
608         {
609             macroTrigger.setTriggerType(TRIGGER_EVENT);
610             macroTrigger.setInitiator((moduleIDCb.getSelectedItem()).toString());
611             macroTrigger.setDescription(macroDescriptionTf.getText());
612             macroTrigger.setAction((String) eventCb.getSelectedItem());
613             IMacro[] macro = new IMacro[1];
614             macro[0] = getMacros((String) macroEventCombo.getSelectedItem());
615             macroTrigger.setMacros(macro);
616         }
617 
618         /* for timer trigger events */
619         else if (triggerIDCb.getSelectedIndex() == 1)
620         {
621             macroTrigger.setTriggerType(TIMER_EVENT);
622             macroTrigger.setDescription(macroDescriptionTf.getText());
623             macroTrigger.setSunday(sunTB.isSelected());
624             macroTrigger.setMonday(monTB.isSelected());
625             macroTrigger.setTuesday(tueTB.isSelected());
626             macroTrigger.setWednesday(wedTB.isSelected());
627             macroTrigger.setThursday(thuTB.isSelected());
628             macroTrigger.setFriday(friTB.isSelected());
629             macroTrigger.setSaturday(satTB.isSelected());
630             macroTrigger.setStartTime(parseTime(startTimeTf.getText()));
631             macroTrigger.setStopTime(parseTime(stopTimeTf.getText()));
632             IMacro[] macros = new IMacro[2];
633             macros[0] = getMacros(macroEventCombo.getSelectedItem().toString());
634             macros[1] = getMacros(macroStopEventCombo.getSelectedItem().toString());
635             macroTrigger.setMacros(macros);
636         }
637         return macroTrigger;
638     }
639 
640     private Calendar parseTime(String field)
641     {
642         Calendar stimestamp = new GregorianCalendar();
643         try
644         {
645             stimestamp.setTime(sdf.parse(field));
646         }
647         catch (ParseException pe)
648         {
649             return stimestamp;
650         }
651         return stimestamp;
652     }
653 
654     private IMacro getMacros(String mname)
655     {
656         Enumeration enumr = MACROS.elements();
657         IMacro imacro;
658         while (enumr.hasMoreElements())
659         {
660             imacro = (IMacro) enumr.nextElement();
661             if (imacro.getMacroName().equalsIgnoreCase(mname))
662             {
663                 return imacro;
664             }
665         }
666         return null;
667     }
668 
669     /***
670      * gets the selected row and adds the row items
671      * into a vector which is passed to the
672      * bottom pane detail view to change the combo boxes
673      * and text fields to show the table data in a detail
674      * view for user manipulation.
675      */
676     private void setDetails()
677     {
678         int rownum = macroTable.getSelectedRow();
679         try
680         {
681             rownum = macroSorter.getMappingToRow(rownum);
682         }
683         catch (Exception e)
684         {
685             //sorting error
686         }
687         if (rownum > -1)
688         {
689             showInDetailView((IMacroTrigger) macroModel.getItemAt(rownum));
690         }
691     }
692 
693     /***
694      * gets the selected row from the table and
695      * shows it in the bottom pane for manipulations by the user
696      * @param trigger the trigger to show in the detail view of this panel.
697      */
698     private void showInDetailView(IMacroTrigger trigger)
699     {
700         IMacro[] macros = trigger.getMacros();
701         if (trigger.getTriggerType() == TRIGGER_EVENT)
702         {
703             triggerIDCb.setSelectedIndex(0); // set to module event
704             eventCb.setSelectedItem(trigger.getAction()); // set on/off
705             macroEventCombo.setSelectedItem(macros[0].getMacroName()); // selected macro events
706             IX10Module iX10device = trigger.getX10Module();
707             /* display the item Module ID */
708             for (int i = 0; i < moduleIDCb.getItemCount(); i++)
709             {
710                 try
711                 {
712                     if ((moduleIDCb.getItemAt(i)).toString().startsWith(iX10device.getFullDeviceCode()))
713                     {
714                         moduleIDCb.setSelectedIndex(i);
715                     }
716                 }
717                 catch (Exception err)
718                 {
719                     moduleIDCb.setSelectedIndex(0);
720                 }
721             }
722         }
723         /* take the information in the time field and convert it to 12 hour format*/
724         if (trigger.getTriggerType() == TIMER_EVENT)
725         {
726             triggerIDCb.setSelectedIndex(1); //set to timer event
727             SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");
728             startTimeTf.setText(sdf.format(trigger.getStartTime().getTime()));
729             stopTimeTf.setText(sdf.format(trigger.getStopTime().getTime()));
730             setupDaysofWeek(trigger); //set days of week boxes
731             macroEventCombo.setSelectedItem(macros[0].getMacroName());
732             macroStopEventCombo.setSelectedItem(macros[1].getMacroName());
733         }
734         macroDescriptionTf.setText(trigger.getDescription()); // description field
735     }
736 
737 
738     /***
739      * add a new macro to the macro table
740      */
741     public void addMacro()
742     {
743         macroModel.addRow(getDetailItem());
744         saveTriggers();
745     }
746 
747     /***
748      * This deletes the table row from the table model. It should
749      * also delete the item from the memory properties hash so that
750      * it doesn't get saved into the file when the user hits save file.
751      * This is done at the DeviceFactory object.
752      */
753     public void deleteMacroRow()
754     {
755         int rownum = macroTable.getSelectedRow();
756         try
757         {
758             rownum = macroSorter.getMappingToRow(rownum);
759         }
760         catch (Exception e)
761         {
762 
763         }
764         if (rownum > -1)
765         {
766             macroModel.removeRow(rownum);
767             saveTriggers();
768         }
769     }
770 
771     /***
772      * Updates the currently selected row
773      * from the details panel information.
774      */
775     public void updateMacroRow()
776     {
777         int rownum = macroTable.getSelectedRow();
778         IMacroTrigger rowData = getDetailItem();
779         try
780         {
781             rownum = macroSorter.getMappingToRow(rownum);
782         }
783         catch (Exception e)
784         {
785 
786         }
787         if (rownum > -1)
788         {
789             macroModel.setValueAt(rowData, rownum);
790         }
791         saveTriggers();
792     }
793 
794     /***
795      * Save the entire table model into a properties
796      * The format for the key is Macro.A21=TM,12:15/20:30,description,SMTWTFS,Macro1
797      * for timer trigger events and for Trigger events it is
798      *    Macro.A21=EV,A1,description,Off,Macro2
799      */
800     public void saveTriggers()
801     {
802         int rowCount = macroTable.getRowCount();
803         Vector rows = new Vector();
804         for (int i = 0; i < rowCount; i++)
805         {
806             rows.addElement(macroModel.getItemAt(i));
807         }
808         AutoHomeAdminSession.getInstance().saveCM11AMacroTriggers(rows);
809     }
810 
811     private void setupDaysofWeek(IMacroTrigger imt)
812     {
813         sunTB.setSelected(imt.getSunday());
814         monTB.setSelected(imt.getMonday());
815         tueTB.setSelected(imt.getTuesday());
816         wedTB.setSelected(imt.getWednesday());
817         thuTB.setSelected(imt.getThursday());
818         friTB.setSelected(imt.getFriday());
819         satTB.setSelected(imt.getSaturday());
820     }
821 }
822 
823 
824 
825