View Javadoc

1   package org.wcb.autohome.model;
2   
3   import org.wcb.autohome.interfaces.*;
4   import org.wcb.autohome.AutoHomeAdminSession;
5   
6   import javax.swing.table.AbstractTableModel;
7   import javax.swing.table.TableModel;
8   import java.util.Vector;
9   import java.util.Calendar;
10  import java.text.SimpleDateFormat;
11  
12  /***
13   * Copyright (C) 1999  Walter Bogaardt
14   *
15   * This library is free software; you can redistribute it and/or
16   * modify it under the terms of the GNU Lesser General Public
17   * License as published by the Free Software Foundation; either
18   * version 2 of the License, or (at your option) any later version.
19   *
20   * This library is distributed in the hope that it will be useful,
21   * but WITHOUT ANY WARRANTY; without even the implied warranty of
22   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23   * Lesser General Public License for more details.
24   *
25   * You should have received a copy of the GNU Lesser General Public
26   * License along with this library; if not, write to the Free Software
27   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
28   *
29   * Project: Alice X10 Home Automation
30   *
31   * Date: Oct 6, 2003
32   * Time: 4:04:01 PM
33   * class that is used to display the resulting orders.
34   *   $Log: MacroTriggerModel.java,v $
35   *   Revision 1.4  2004/01/16 19:50:18  wbogaardt
36   *   refactored, fixed long standing bug with updating macro panels, add error notification to user for improper device codes
37   *
38   *   Revision 1.3  2004/01/15 21:05:20  wbogaardt
39   *   major revamp of Modules and interfaces changes overall structure of how information is stored
40   *
41   *   Revision 1.2  2003/10/10 21:39:07  wbogaardt
42   *   modified macro triggers to use calendar in stead of strings
43   *
44   *   Revision 1.1  2003/10/06 23:43:43  wbogaardt
45   *   refactored out of panel classes so these now work independent
46   *
47   */
48  public  class MacroTriggerModel extends AbstractTableModel implements TableModel
49      {
50      private Vector lists = null;
51      private String[] names = {AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.TRIGGER_COLUMN),
52                                AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.INITIATOR_COLUMN),
53                                AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.DESCRIPTION_COLUMN),
54                                AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.EVENT_COLUMN),
55                                AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.MACRO_COLUMN)
56      };
57      private static final SimpleDateFormat sdf = new SimpleDateFormat("h:mm");
58  
59      /***
60       * This is the constructor takes in the data set a for display.
61       * @param list Vector of IMacroTrigger
62       */
63      public MacroTriggerModel(Vector list) {
64          super();
65          setList(list);
66      }
67  
68      /***
69       * Set the model to this new vector list. So that
70       * a complete new data set is put into the model.
71       *
72       * @param list New data set of IMacroTrigger
73       */
74      public void setList(Vector list)
75      {
76          lists = list;
77          fireTableDataChanged();
78      }
79  
80      /***
81       * Gets the Object for the selected row in the model
82       * @param row Row number in the model
83       * @return An object of IMacroTrigger
84       */
85      public Object getItemAt(int row) {
86          if(lists != null && lists.size() > 0)
87          {
88              return lists.elementAt(row);
89          }
90          return new String("");
91      }
92  
93      /***
94       * Add a row of IMacroTrigger object and
95       * fire data table change event.
96       * @param newRow Inserted IMacroTrigger object.
97       */
98      public void addRow(IMacroTrigger newRow){
99          lists.addElement(newRow);
100         fireTableDataChanged();
101     }
102 
103     /***
104      * Remove the identified row from the
105      * model and fire a data table change event.
106      * @param row Row number to remove
107      */
108     public void removeRow(int row){
109         lists.removeElementAt(row);
110         fireTableDataChanged();
111     }
112 
113     /***
114      * Set the value of the row to an IMacroTrigger
115      * and fire a table data change event.
116      *
117      * @param aVal Object of IMacroTrigger
118      * @param row the row number in the model
119      */
120     public void setValueAt(Object aVal, int row){
121         IMacroTrigger item = (IMacroTrigger)aVal;
122         lists.setElementAt(item, row);
123         fireTableDataChanged();
124     }
125 
126     /***
127      * Returns the value for the cell at columnIndex and rowIndex.
128      * @param row The row whose value is to be queried
129      * @param col The column whose value is to be queried
130      * @return The value Object at the specified cell
131      */
132     public Object getValueAt(int row, int col)
133     {
134         Object returnValue = "";
135         try
136         {
137             IMacroTrigger element = (IMacroTrigger)getItemAt(row);
138             IX10Module iX10Device = element.getX10Module();
139             if (element != null)
140             {
141                 switch(col)
142                 {
143                     case 0:
144                         returnValue = element;
145                         break;
146                     case 1:
147                         if(element.getTriggerType()==X10DeviceConstants.TIMER_EVENT)
148                         {
149                             returnValue = getStartTime(element);
150                         }
151                         else
152                         {
153                             returnValue = iX10Device.getFullDeviceCode();
154                         }
155                         break;
156                     case 2:
157                         returnValue = element.getDescription();
158                         break;
159                     case 3:
160                         if(element.getTriggerType()==X10DeviceConstants.TRIGGER_EVENT){
161                             returnValue = element.getAction();
162                         } else {
163                             returnValue = getDays(element);
164                         }
165                         break;
166                     case 4:
167                         IMacro[] macs = element.getMacros();
168                         try{
169                             if(element.getTriggerType()==X10DeviceConstants.TIMER_EVENT){
170                                 returnValue = new String(macs[0].getMacroName()+"/"+macs[1].getMacroName());
171                             }else if(element.getTriggerType()==X10DeviceConstants.TRIGGER_EVENT){
172                                 returnValue = macs[0].getMacroName();
173                             }
174                         }catch(NullPointerException npe){
175                             returnValue = "Macro1";
176                         }
177                         break;
178                 }
179             }
180         }
181         catch (Exception e)
182         {
183             returnValue = "";
184         }
185         return returnValue;
186     }
187 
188     // These methods always need to be implemented.
189     /***
190      * Returns the number of columns in the model. This can be used to
191      * determin how many columsn should be created and displayed by default in
192      * a JTable
193      * @return The number of columns in the model
194      */
195     public int getColumnCount() {
196         return names.length;
197     }
198 
199     /***
200      * Number of X10Devices added to the table
201      * model and return the total number
202      * @return defaults to 0
203      */
204     public int getRowCount() {
205         try
206         {
207             return lists.size();
208         }
209         catch (Exception e)
210         {
211             return 0;
212         }
213     }
214 
215     // The default implementations of these methods in
216     // AbstractTableModel would work, but we can refine them.
217     /***
218      * Retruns the name of the column at columnIndex. This is used to
219      * initialize the table's column header name. Note: this name does not need to be unique;
220      * two columns in a table can have the same name.
221      * @param columnIndex The index of the column
222      * @return the name of the column
223      */
224     public String getColumnName(int columnIndex) {
225         return names[columnIndex];
226     }
227 
228     /***
229      * Returns the most specific superclass for all the cell values in the column. This is
230      * used by the JTable to set up a default render and
231      * editor for the column
232      * @param col the index of the column
233      * @return the common ancestor class of the object values in the model.
234      */
235     public Class getColumnClass(int col) {
236         try
237         {
238             return getValueAt(0,col).getClass();
239         }
240         catch (Exception e)
241         {
242             return "".getClass();
243         }
244     }
245 
246     /***
247      * The entire model does not allow any of the
248      * cells to be edited directly.
249      * @param row The selected row
250      * @param col Selected column
251      * @return always returns false
252      */
253     public boolean isCellEditable(int row, int col) {
254         return false;
255     }
256 
257     /***
258      * This takes the macro trigger and gets its start time
259      * information and returns that as a printable string
260      *
261      * @param imt The IMacroTrigger with a start time
262      * @return printable string Date in H:mm AM/PM format
263      */
264     private String getStartTime(IMacroTrigger imt)
265     {
266         String returnValue = sdf.format(imt.getStartTime().getTime());
267         if(imt.getStartTime().get(Calendar.AM_PM)==Calendar.AM)
268         {
269             returnValue = returnValue + " AM";
270         }
271         else
272         {
273             returnValue = returnValue + " PM";
274         }
275         return returnValue;
276     }
277 
278     /***
279      * Returns a string of days in the format of SMTWTFS
280      * based if the days are active or not for the event.
281      * @param imt The IMacroTrigger with its date information set
282      * @return string with appropriate days enabled
283      */
284     private String getDays(IMacroTrigger imt){
285         String returnString = ifXtrueReturnY(imt.getSunday(),"S")+
286                 ifXtrueReturnY(imt.getMonday(),"M") +
287                 ifXtrueReturnY(imt.getTuesday(),"T") +
288                 ifXtrueReturnY(imt.getWednesday(),"W") +
289                 ifXtrueReturnY(imt.getThursday(),"T") +
290                 ifXtrueReturnY(imt.getFriday(),"F") +
291                 ifXtrueReturnY(imt.getSaturday(),"S");
292         return returnString;
293     }
294 
295     /***
296      * Used by the getDays() method call to return
297      * either the string value if it boolean is is true
298      * and if it is false a - will be returned
299      * @param x boolean parameter
300      * @param y the string to be returned if bolean parameter is true
301      * @return either the string parameter or -
302      */
303     private String ifXtrueReturnY(boolean x, String y){
304         if(x)
305             return y;
306         else
307             return "-";
308     }
309 }