View Javadoc

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