1 package org.wcb.autohome.model;
2
3 import org.wcb.autohome.AutoHomeAdminSession;
4 import org.wcb.autohome.implementations.RunEvent;
5 import org.wcb.autohome.interfaces.I18nConstants;
6 import org.wcb.autohome.interfaces.IX10MonitorEvent;
7 import org.wcb.autohome.interfaces.IRunEvent;
8 import org.wcb.autohome.interfaces.IX10Module;
9
10 import javax.swing.table.AbstractTableModel;
11 import javax.swing.table.TableModel;
12 import java.util.*;
13 import java.text.SimpleDateFormat;
14
15 /***
16 * Copyright (C) 1999 Walter Bogaardt
17 *
18 * This library is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU Lesser General Public
20 * License as published by the Free Software Foundation; either
21 * version 2 of the License, or (at your option) any later version.
22 *
23 * This library is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * Lesser General Public License for more details.
27 *
28 * You should have received a copy of the GNU Lesser General Public
29 * License along with this library; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 *
32 * Project: Alice X10 Home Automation
33 *
34 * Date: January 14, 2004
35 * Time: 11:32:45 AM
36 * class that is used to display the resulting orders.
37 *
38 * $Log: MonitorTableModel.java,v $
39 * Revision 1.14 2004/06/09 18:50:32 wbogaardt
40 * changed enum to non-keyword for jvm1.5
41 *
42 * Revision 1.13 2004/01/21 00:11:29 wbogaardt
43 * updated versions and api information
44 *
45 * Revision 1.12 2004/01/20 19:35:57 wbogaardt
46 * fixed adding new objects to check for redundancy
47 *
48 * Revision 1.11 2004/01/20 00:20:36 wbogaardt
49 * allow email service to send status code back if email succeds or fails
50 *
51 * Revision 1.10 2004/01/19 22:35:38 wbogaardt
52 * added fixes to runing events and emails so they work and added a testing of a monitored event through the table popup on a right mouse click.
53 *
54 * Revision 1.9 2004/01/19 17:54:22 wbogaardt
55 * initial fixed monitor
56 *
57 * Revision 1.8 2004/01/18 00:48:33 wbogaardt
58 * refactored out unnecessary code and now have a functional initial design of monitoring panel
59 *
60 * Revision 1.7 2004/01/17 08:15:46 wbogaardt
61 * Have an initial working monitoring frame that actually shows the date and time an event takes place
62 *
63 * Revision 1.6 2004/01/17 07:21:17 wbogaardt
64 * added serialization to run events and allow monitoring of these events to the file system to reload later
65 *
66 * Revision 1.5 2004/01/17 06:38:05 wbogaardt
67 * modified what is displayed
68 *
69 * Revision 1.4 2004/01/16 22:57:41 wbogaardt
70 * Improved display layout of module panel and added basic monitoring panel
71 * CV: ----------------------------------------------------------------------
72 *
73 * Revision 1.3 2004/01/16 19:50:18 wbogaardt
74 * refactored, fixed long standing bug with updating macro panels, add error notification to user for improper device codes
75 *
76 * Revision 1.2 2004/01/15 21:05:20 wbogaardt
77 * major revamp of Modules and interfaces changes overall structure of how information is stored
78 *
79 * Revision 1.1 2004/01/15 00:11:34 wbogaardt
80 * Monitoring model for X10 events that have been detected
81 *
82 */
83 public class MonitorTableModel extends AbstractTableModel implements TableModel{
84
85 private Vector lists;
86 private String[] names = {AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.DEVICE_COLUMN),
87 AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.DESCRIPTION_COLUMN),
88 "Location",
89 "Last Change",
90 "Status",
91 AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.ACTION_COLUMN)};
92 private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd h:mm:ss a", new Locale("en","US"));
93
94 /***
95 * This is the constructor takes in the data set a for display.
96 * @param list Vector of IX10Monitor
97 */
98 public MonitorTableModel(Vector list) {
99 super();
100 setList(list);
101 }
102
103 /***
104 * Set the model to this new vector list. So that
105 * a complete new data set is put into the model.
106 *
107 * @param list New data set of IX10Monitor
108 */
109 public void setList(Vector list)
110 {
111 lists = list;
112 fireTableDataChanged();
113 }
114
115 /***
116 * Gets the Object for the selected row in the model
117 * @param row Row number in the model
118 * @return An object of IMacroTrigger
119 */
120 public Object getItemAt(int row) {
121 if(lists != null && lists.size() > 0)
122 {
123 return lists.elementAt(row);
124 }
125 return new String("");
126 }
127
128 /***
129 * Pass in the house code and device id if it matches one of the items
130 * in the listing then return that row Object and update the row with
131 * the new time.
132 * @param cHouse
133 * @param iDevice
134 * @return The object that an event was detected if the values don't match what is in the listing the return a null.
135 */
136 public IX10MonitorEvent getModuleOfEventDetected(char cHouse, int iDevice, String status){
137 Enumeration enumr = lists.elements();
138 int iCount = 0;
139 IX10MonitorEvent ev;
140 IX10Module iModule;
141 while(enumr.hasMoreElements())
142 {
143 ev = (IX10MonitorEvent) enumr.nextElement();
144 iModule = ev.getMonitoringModule();
145 if(iModule.getHouseCode() == cHouse && iModule.getDeviceCode() == iDevice)
146 {
147 ev.setTimeDetected(new GregorianCalendar());
148 ev.setStatus(status);
149 this.setValueAt(ev, iCount);
150 return ev;
151 }
152 iCount++;
153 }
154 return null;
155 }
156
157 /***
158 * Add a row of IX10Monitor object and
159 * fire data table change event.
160 * @param newRow Inserted IX10MonitorEvent object.
161 */
162 public void addRow(IX10MonitorEvent newRow)
163 {
164 Enumeration enumr = lists.elements();
165 int iCount = 0;
166 IX10MonitorEvent ev;
167 IX10Module iModule;
168 boolean alreadyExists = false;
169 while(enumr.hasMoreElements())
170 {
171 ev = (IX10MonitorEvent) enumr.nextElement();
172 iModule = ev.getMonitoringModule();
173 if(iModule.getHouseCode() == newRow.getMonitoringModule().getHouseCode()
174 && iModule.getDeviceCode() == newRow.getMonitoringModule().getDeviceCode())
175 {
176 this.setValueAt(ev, iCount);
177 alreadyExists = true;
178 break;
179 }
180 iCount++;
181 }
182 if(!alreadyExists){
183 lists.addElement(newRow);
184 fireTableDataChanged();
185 }
186 }
187
188 /***
189 * Remove the identified row from the
190 * model and fire a data table change event.
191 * @param row Row number to remove
192 */
193 public void removeRow(int row)
194 {
195 lists.removeElementAt(row);
196 fireTableDataChanged();
197 }
198
199 /***
200 * Set the value of the row to an IX10Module
201 * and fire a table data change event.
202 *
203 * @param aVal Object of IX10Module
204 * @param row the row number in the model
205 */
206 public void setValueAt(Object aVal, int row)
207 {
208 lists.setElementAt(aVal, row);
209 fireTableDataChanged();
210 }
211
212 /***
213 * Returns the value for the cell at columnIndex and rowIndex.
214 * @param row The row whose value is to be queried
215 * @param col The column whose value is to be queried
216 * @return The value Object at the specified cell
217 */
218 public Object getValueAt(int row, int col)
219 {
220 Object returnValue = "";
221 try
222 {
223 IX10MonitorEvent iEvent = (IX10MonitorEvent)lists.elementAt(row);
224 switch(col){
225 case 0:
226 return iEvent.getMonitoringModule();
227 case 1:
228 return iEvent.getDescription();
229 case 2:
230 return iEvent.getLocation();
231 case 3:
232 return this.getDateTime(iEvent.getTimeDetected());
233 case 4:
234 return iEvent.getStatus();
235 case 5:
236 IRunEvent[] runable_event = iEvent.getRunEvent();
237 return this.getRunableEventString(runable_event[0].getRunType());
238 default:
239 return "";
240 }
241
242 } catch (Exception e) {e.printStackTrace();}
243 return returnValue;
244 }
245
246
247 /***
248 * Returns the number of columns in the model. This can be used to
249 * determin how many columsn should be created and displayed by default in
250 * a JTable
251 * @return The number of columns in the model
252 */
253 public int getColumnCount()
254 {
255 return names.length;
256 }
257
258 /***
259 * Number of rows added to the table
260 * model and return the total number
261 * @return defaults to 0
262 */
263 public int getRowCount()
264 {
265 try
266 {
267 return lists.size();
268 }
269 catch (Exception e)
270 {
271 return 0;
272 }
273 }
274
275
276
277 /***
278 * Retruns the name of the column at columnIndex. This is used to
279 * initialize the table's column header name. Note: this name does not need to be unique;
280 * two columns in a table can have the same name.
281 * @param columnIndex The index of the column
282 * @return the name of the column
283 */
284 public String getColumnName(int columnIndex)
285 {
286 return names[columnIndex];
287 }
288
289 /***
290 * Returns the most specific superclass for all the cell values in the column. This is
291 * used by the JTable to set up a default render and
292 * editor for the column
293 * @param col the index of the column
294 * @return the common ancestor class of the object values in the model.
295 */
296 public Class getColumnClass(int col)
297 {
298 try
299 {
300 return getValueAt(0,col).getClass();
301 }
302 catch (Exception e)
303 {
304 return "".getClass();
305 }
306 }
307
308 /***
309 * The entire model does not allow any of the
310 * cells to be edited directly.
311 * @param row The selected row
312 * @param col Selected column
313 * @return always returns false
314 */
315 public boolean isCellEditable(int row, int col)
316 {
317 return false;
318 }
319
320 /***
321 * Takes the run type integer and returns a printable
322 * string to the user.
323 * @param iType values 0-3
324 * @return String to print to user
325 */
326 private String getRunableEventString(int iType){
327 switch(iType){
328 case RunEvent.SEND_EMAIL:
329 return "Send Email";
330 case RunEvent.RUN_APPLICATION:
331 return "Run Application";
332 case RunEvent.RUN_MODULE:
333 return "Execute Module";
334 case RunEvent.SPEAK_COMMAND:
335 return "Speak Text";
336 }
337 return "No runable event";
338 }
339
340 private String getDateTime(Calendar cal){
341 if(cal == null)
342 {
343 return "No events";
344 }
345 String output = sdf.format(cal.getTime());
346 return output;
347 }
348 }
349