View Javadoc

1   package org.wcb.autohome.model;
2   
3   import org.wcb.autohome.interfaces.IMacroEvent;
4   import org.wcb.autohome.interfaces.IX10Module;
5   import org.wcb.autohome.interfaces.X10DeviceConstants;
6   import org.wcb.autohome.interfaces.I18nConstants;
7   import org.wcb.autohome.AutoHomeAdminSession;
8   
9   import javax.swing.table.AbstractTableModel;
10  import javax.swing.table.TableModel;
11  import java.util.Vector;
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   * Date: Oct 6, 2003
32   * Time: 2:55:00 PM
33   * class that is used to display the resulting orders
34   *
35   * $Log: MacroEventTableModel.java,v $
36   * Revision 1.5  2004/01/16 19:50:18  wbogaardt
37   * refactored, fixed long standing bug with updating macro panels, add error notification to user for improper device codes
38   *
39   * Revision 1.4  2004/01/16 01:00:20  wbogaardt
40   * fixe for exceptioin stack
41   *
42   * Revision 1.3  2004/01/16 00:53:43  wbogaardt
43   * Fixed a very obscure bug with the Macro Panel that it didn't added new
44   * 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.
45   *
46   * Revision 1.2  2004/01/15 21:05:20  wbogaardt
47   * major revamp of Modules and interfaces changes overall structure of how information is stored
48   *
49   *
50   */
51  public class MacroEventTableModel extends AbstractTableModel implements TableModel {
52      private Vector lists = null;
53      private String[] names = {AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.DEVICE_COLUMN),
54                                AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.MODULE_ID_COLUMN),
55                                AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.ACTION_COLUMN)
56      };
57  
58      /***
59       * This is the constructor takes in the data set for display.
60       * @param list Vector of IMacroEvent
61       */
62      public MacroEventTableModel(Vector list)
63      {
64          super();
65          setList(list);
66      }
67  
68      /***
69       * Gets the Object for the selected row in the model
70       * @param row Row number in the model
71       * @return An object of IMacroEvent
72       */
73      public Object getItemAt(int row)
74      {
75          if(lists != null && lists.size() > 0)
76          {
77              return lists.elementAt(row);
78          }
79          return new String("");
80      }
81  
82      /***
83       * Set the model to this new vector list. So that
84       * a complete new data set is put into the model.
85       *
86       * @param list New data set of IMacroEvents
87       */
88      public void setList(Vector list)
89      {
90          lists = list;
91          fireTableDataChanged();
92      }
93  
94      /***
95       * Add a new event to the model and fire a table data change event
96       * @param newRow IMacroEvent object
97       */
98      public void addRow(IMacroEvent newRow)
99      {
100         lists.addElement(newRow);
101         fireTableDataChanged();
102     }
103 
104     /***
105      * Remove the selected row and fire a table data change event.
106      *
107      * @param row the row number to remove from the model
108      */
109     public void removeRow(int row)
110     {
111         lists.removeElementAt(row);
112         fireTableDataChanged();
113     }
114 
115     /***
116      * Set the value of the row to an IMacroEvent
117      * and fire a table data change event.
118      *
119      * @param aVal Object of IMacroEvent
120      * @param row the row number in the model
121      */
122     public void setValueAt(Object aVal, int row)
123     {
124         IMacroEvent item = (IMacroEvent)aVal;
125         lists.setElementAt(item, row);
126         fireTableDataChanged();
127     }
128 
129     /***
130      * Returns the value for the cell at columnIndex and rowIndex.
131      * @param row The row whose value is to be queried
132      * @param col The column whose value is to be queried
133      * @return The value Object at the specified cell
134      */
135     public Object getValueAt(int row, int col)
136     {
137         Object returnValue = "";
138         try
139         {
140             IMacroEvent element = (IMacroEvent)getItemAt(row);
141             IX10Module iX10Device = element.getX10Module();
142             if (element != null)
143             {
144                 switch(col)
145                 {
146                     case 0:
147                         returnValue = iX10Device;
148                         break;
149                     case 1:
150                         returnValue = iX10Device.getFullDeviceCode();
151                         break;
152                     case 2:
153                         returnValue = this.getPrintableNameForAction(element.getAction());
154                         break;
155                 }
156             }
157         }
158         catch (Exception e)
159         {
160             returnValue = "";
161         }
162         return returnValue;
163     }
164 
165     /***
166      * Gets a printable string for the console to print
167      * the action from an int value to a Word the end user
168      * can understand.
169      * @param iAction From the IMacroEvent.getAction();
170      * @return User understandable String of the action.
171      */
172     private String getPrintableNameForAction(int iAction)
173     {
174         switch(iAction)
175         {
176             case X10DeviceConstants.OFF_ACTION:
177                 return "Off";
178             case X10DeviceConstants.ON_ACTION:
179                 return "On";
180             case X10DeviceConstants.BRIGHT_ACTION:
181                 return "Brighten Light";
182             case X10DeviceConstants.DIM_ACTION:
183                 return "Dim Light";
184             default:
185                 return "On";
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     {
197         return names.length;
198     }
199 
200     /***
201      * Gets the row count of the model object
202      * @return Default of 0 if exception caught.
203      */
204     public int getRowCount()
205     {
206         try
207         {
208             return lists.size();
209         }
210         catch (Exception e)
211         {
212             return 0;
213         }
214     }
215 
216     // The default implementations of these methods in
217     // AbstractTableModel would work, but we can refine them.
218     /***
219      * Retruns the name of the column at columnIndex. This is used to
220      * initialize the table's column header name. Note: this name does not need to be unique;
221      * two columns in a table can have the same name.
222      * @param columnIndex The index of the column
223      * @return the name of the column
224      */
225     public String getColumnName(int columnIndex)
226     {
227         return names[columnIndex];
228     }
229 
230     /***
231      * Returns the most specific superclass for all the cell values in the column. This is
232      * used by the JTable to set up a default render and
233      * editor for the column
234      * @param col the index of the column
235      * @return the common ancestor class of the object values in the model.
236      */
237     public Class getColumnClass(int col)
238     {
239         try
240         {
241             return getValueAt(0,col).getClass();
242         }
243         catch (Exception e)
244         {
245             return "".getClass();
246         }
247     }
248 
249     /***
250      * The entire model does not allow any of the
251      * cells to be edited directly.
252      * @param row The selected row
253      * @param col Selected column
254      * @return always returns false
255      */
256     public boolean isCellEditable(int row, int col)
257     {
258         return false;
259     }
260 }