View Javadoc

1   package org.wcb.autohome.model;
2   
3   import org.wcb.autohome.interfaces.IX10Module;
4   import org.wcb.autohome.interfaces.I18nConstants;
5   import org.wcb.autohome.AutoHomeAdminSession;
6   
7   import javax.swing.table.AbstractTableModel;
8   import javax.swing.table.TableModel;
9   import java.util.Vector;
10  
11  /***
12   * Copyright (C) 1999  Walter Bogaardt
13   *
14   * This library is free software; you can redistribute it and/or
15   * modify it under the terms of the GNU Lesser General Public
16   * License as published by the Free Software Foundation; either
17   * version 2 of the License, or (at your option) any later version.
18   *
19   * This library is distributed in the hope that it will be useful,
20   * but WITHOUT ANY WARRANTY; without even the implied warranty of
21   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22   * Lesser General Public License for more details.
23   *
24   * You should have received a copy of the GNU Lesser General Public
25   * License along with this library; if not, write to the Free Software
26   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
27   *
28   * Project: Alice X10 Home Automation
29   * Date: Oct 6, 2003
30   * Time: 3:49:34 PM
31   *
32   * class that is used to display the resulting orders..
33   *
34   *  $Log: ModuleTableModel.java,v $
35   *  Revision 1.3  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.2  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.1  2003/10/06 23:43:43  wbogaardt
42   *  refactored out of panel classes so these now work independent
43   *
44   */
45  public class ModuleTableModel extends AbstractTableModel implements TableModel {
46      private Vector lists = null;
47      private String[] names = {AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.DEVICE_COLUMN),
48          AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.MODULE_ID_COLUMN),
49          AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.DESCRIPTION_COLUMN)
50      };
51  
52      /***
53       * This is the constructor takes in the data set a for display.
54       * @param list Vector of IX10Modules
55       */
56      public ModuleTableModel(Vector list)
57      {
58          super();
59          setList(list);
60      }
61  
62      /***
63       * Gets the Object for the selected row in the model
64       * @param row Row number in the model
65       * @return An object of IX10Module
66       */
67      public Object getItemAt(int row)
68      {
69          if(lists != null && lists.size() > 0)
70          {
71              return lists.elementAt(row);
72          }
73          return new String("");
74      }
75  
76      /***
77       * Set the model to this new vector list. So that
78       * a complete new data set is put into the model.
79       *
80       * @param list New data set of IX10Module
81       */
82      public void setList(Vector list)
83      {
84          lists = list;
85          fireTableDataChanged();
86      }
87  
88      /***
89       * Add a row of IX10Module object and
90       * fire data table change event.
91       * @param newRow Inserted IX10Module object.
92       */
93      public void addRow(IX10Module newRow)
94      {
95          lists.addElement(newRow);
96          fireTableDataChanged();
97      }
98  
99      /***
100      * Remove the identified row from the
101      * model and fire a data table change event.
102      * @param row Row number to remove
103      */
104     public void removeRow(int row)
105     {
106         lists.removeElementAt(row);
107         fireTableDataChanged();
108     }
109 
110     /***
111      * Set the value of the row to an IX10Module
112      * and fire a table data change event.
113      *
114      * @param aVal Object of IX10Module
115      * @param row the row number in the model
116      */
117     public void setValueAt(Object aVal, int row)
118     {
119         IX10Module item = (IX10Module)aVal;
120         lists.setElementAt(item, row);
121         fireTableDataChanged();
122     }
123 
124     /***
125      * Returns the value for the cell at columnIndex and rowIndex.
126      * @param row The row whose value is to be queried
127      * @param col The column whose value is to be queried
128      * @return The value Object at the specified cell
129      */
130     public Object getValueAt(int row, int col)
131     {
132         Object returnValue = "";
133         try {
134             IX10Module element = (IX10Module)getItemAt(row);
135             if (element != null)
136             {
137                 switch(col)
138                 {
139                     case 0:
140                         returnValue = element;
141                         break;
142                     case 1:
143                         returnValue = element.getName();
144                         break;
145                     case 2:
146                         returnValue = element.getDescription();
147                         break;
148                 }
149             }
150         }
151         catch (Exception e)
152         {
153             returnValue = "";
154         }
155         return returnValue;
156     }
157 
158     // These methods always need to be implemented.
159     /***
160      * Returns the number of columns in the model. This can be used to
161      * determin how many columsn should be created and displayed by default in
162      * a JTable
163      * @return The number of columns in the model
164      */
165     public int getColumnCount()
166     {
167         return names.length;
168     }
169 
170     /***
171      * Number of X10Devices added to the table
172      * model and return the total number
173      * @return defaults to 0
174      */
175     public int getRowCount()
176     {
177         if(this.lists != null)
178         {
179             return this.lists.size();
180         }
181         return 0;
182     }
183 
184     /***
185      * Retruns the name of the column at columnIndex. This is used to
186      * initialize the table's column header name. Note: this name does not need to be unique;
187      * two columns in a table can have the same name.
188      * @param columnIndex The index of the column
189      * @return the name of the column
190      */
191     public String getColumnName(int columnIndex)
192     {
193         return names[columnIndex];
194     }
195 
196     /***
197      * Returns the most specific superclass for all the cell values in the column. This is
198      * used by the JTable to set up a default render and
199      * editor for the column
200      * @param col the index of the column
201      * @return the common ancestor class of the object values in the model.
202      */
203     public Class getColumnClass(int col)
204     {
205         try
206         {
207             return getValueAt(0,col).getClass();
208         }
209         catch (Exception e)
210         {
211             return "".getClass();
212         }
213     }
214 
215     /***
216      * The entire model does not allow any of the
217      * cells to be edited directly.
218      * @param row The selected row
219      * @param col Selected column
220      * @return always returns false
221      */
222     public boolean isCellEditable(int row, int col)
223     {
224         return false;
225     }
226 }