View Javadoc

1   package org.wcb.autohome;
2   
3   import org.wcb.autohome.implementations.X10MonitorEvent;
4   import org.wcb.autohome.model.MonitorTableModel;
5   import org.wcb.autohome.interfaces.I18nConstants;
6   import org.wcb.autohome.interfaces.IX10MonitorEvent;
7   import org.wcb.autohome.interfaces.RefreshInterface;
8   import org.wcb.autohome.util.ui.LightRender;
9   import org.wcb.autohome.util.ui.MonitorTablePopup;
10  import org.wcb.autohome.util.AliceX10AddressListener;
11  import org.wcb.util.TableSorter;
12  import org.wcb.util.TooltippedTable;
13  import org.wcb.util.SortButtonRenderer;
14  
15  import javax.swing.*;
16  import javax.swing.event.ListSelectionListener;
17  import javax.swing.event.ListSelectionEvent;
18  import javax.swing.table.TableColumn;
19  import java.util.Vector;
20  import java.awt.*;
21  import java.awt.event.MouseAdapter;
22  import java.awt.event.MouseEvent;
23  
24  /***
25   * Copyright (C) 1999  Walter Bogaardt
26   *
27   * This library is free software; you can redistribute it and/or
28   * modify it under the terms of the GNU Lesser General Public
29   * License as published by the Free Software Foundation; either
30   * version 2 of the License, or (at your option) any later version.
31   *
32   * This library is distributed in the hope that it will be useful,
33   * but WITHOUT ANY WARRANTY; without even the implied warranty of
34   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
35   * Lesser General Public License for more details.
36   *
37   * You should have received a copy of the GNU Lesser General Public
38   * License along with this library; if not, write to the Free Software
39   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
40   *
41   * Project: Alice X10 Home Automation
42   * Filename:  $Id: MonitorPanel.java,v 1.12 2004/07/22 03:06:49 wbogaardt Exp $<BR>
43   * Abstract: Used to display monitoring information on the monitor panel and run the
44   *           displays the events table.
45   *
46   * $Log: MonitorPanel.java,v $
47   * Revision 1.12  2004/07/22 03:06:49  wbogaardt
48   * removed deprecated method calls.
49   *
50   * Revision 1.11  2004/02/01 20:42:48  wbogaardt
51   * removed form panel reference and changed sizing of the split bars
52   *
53   * Revision 1.10  2004/01/31 07:40:23  wbogaardt
54   * modified layout managers for the panel so that it uses the java standard layoutsrather than the FormLayout api. The purpose is to reduce dependency on non-standard layout api.
55   *
56   * Revision 1.9  2004/01/20 05:31:03  wbogaardt
57   * added speech event for runing
58   *
59   * Revision 1.8  2004/01/19 22:35:34  wbogaardt
60   * 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.
61   *
62   * Revision 1.7  2004/01/19 17:54:16  wbogaardt
63   * initial fixed monitor
64   *
65   * Revision 1.6  2004/01/18 17:39:09  wbogaardt
66   * added table pop up menu actions
67   *
68   * Revision 1.5  2004/01/18 05:27:57  wbogaardt
69   * fixed adding and deleting function of Monitor panel, and saving information
70   *
71   * Revision 1.4  2004/01/18 00:48:31  wbogaardt
72   * refactored out unnecessary code and now have a functional initial design of monitoring panel
73   *
74   * Revision 1.3  2004/01/17 08:15:40  wbogaardt
75   * Have an initial working monitoring frame that actually shows the date and time an event takes place
76   *
77   * Revision 1.2  2004/01/17 07:21:15  wbogaardt
78   * added serialization to run events and allow monitoring of these events to the file system to reload later
79   *
80   * Revision 1.1  2004/01/16 22:59:05  wbogaardt
81   * new classes for the monitor panel
82   *
83   */
84  public class MonitorPanel extends JPanel implements RefreshInterface, ListSelectionListener {
85  
86      private JPanel jpTablePanel;
87      private MonitorTableModel mtmMonitorModel;
88      private TooltippedTable ttTippedTable;
89      private JScrollPane jsScrollingPane;
90      private TableSorter tsSorter;
91      private SortButtonRenderer sortButtonRenderer;
92      private MouseAdapter maMouse;
93      private MonitorDetailPanel mDetailPanel;
94      private MonitorTablePopup tpMonitor;
95  
96  
97      public MonitorPanel(){
98          this.setupComponents();
99          this.setupListeners();
100     }
101 
102     private void setupComponents(){
103         /* setup default data */
104         Vector defaultData = new Vector();
105         defaultData.addElement(new X10MonitorEvent());
106 
107         this.mtmMonitorModel = new MonitorTableModel(defaultData);
108         this.tpMonitor = new MonitorTablePopup(this);
109         this.sortButtonRenderer = new SortButtonRenderer();
110 
111         setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
112         /*set up the table panel */
113         this.jpTablePanel = new JPanel();
114         this.jpTablePanel.setBorder(BorderFactory
115                 .createTitledBorder
116                 (BorderFactory.createLineBorder
117                 (Color.black),AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.EVENTS_LABEL)));
118         if (this.mtmMonitorModel != null)
119         {
120             this.tsSorter = new TableSorter(this.mtmMonitorModel);
121             this.ttTippedTable = new TooltippedTable(tsSorter);
122             this.tsSorter.addMouseListenerToHeaderInTable(ttTippedTable, sortButtonRenderer);
123             this.tsSorter.sortByColumn(2, false);
124             this.jsScrollingPane = new JScrollPane(ttTippedTable);
125             this.jsScrollingPane.setPreferredSize(new Dimension(620, 200));
126             this.jpTablePanel.add(jsScrollingPane, BorderLayout.CENTER);
127         }
128         this.initializeDeviceColumn();
129         mDetailPanel = new MonitorDetailPanel(this);
130         this.jpTablePanel.setPreferredSize(new Dimension(650, 200));
131         JSplitPane mySplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT, jpTablePanel, mDetailPanel);
132         mySplit.setDividerLocation(215);
133         mySplit.setOneTouchExpandable(true);
134         add(mySplit);
135 
136     }
137 
138     /***
139      * This gets reference to this panel so that the
140      * refresh interface can be used.
141      * @return This panel's refresh interface
142      */
143     public RefreshInterface getInterface(){
144         return this;
145     }
146     /***
147      *Allows the items to refresh across multiple
148      *objects and in this case the GUI.
149      */
150     public void refresh()
151     {
152         mDetailPanel.updateDeviceID();
153     }
154 
155     /***
156      * loads the modules and adds them to the table model for
157      * the user.  This event is started by
158      * the File open confirm actions
159      *
160      */
161     public void loadData()
162     {
163         Vector vData = AutoHomeAdminSession.getInstance().loadAllAliceMonitors();
164         if(vData != null )
165         {
166             this.setModel(vData);
167         }
168     }
169 
170     /***
171      *Set up listeners for the maMouse events
172      *and other actions
173      */
174     private void setupListeners(){
175         AutoHomeAdminSession.getInstance().setMonitorModel(mtmMonitorModel);
176         /* This maMouse adapter it used for selecting various rows in the table */
177         maMouse = new MouseAdapter(){
178             public void mousePressed(MouseEvent mice)
179             {
180                 if((mice.getModifiers() == MouseEvent.META_MASK))
181                 {
182                     tpMonitor.setVisible(true);
183                     tpMonitor.show(mice.getComponent(), mice.getX(), mice.getY());
184                 }
185                 else
186                 {
187                     tpMonitor.setVisible(false);
188                 }
189             }
190         };
191         if (this.mtmMonitorModel != null)
192         {
193             ttTippedTable.addMouseListener(maMouse);
194             ttTippedTable.getSelectionModel().addListSelectionListener(this);
195         }
196     }
197 
198     /***
199      * This is used for the list selection listener and must be implemented
200      * so that the selection of a row item in the table can display the detail
201      * information.
202      * @param e Event of selecting a row on the display table
203      */
204     public void valueChanged(ListSelectionEvent e) {
205         if(!e.getValueIsAdjusting()){
206             setDetails();
207         }
208     }
209 
210     /***
211      * When the user double clicks a row that row
212      * updates the detail panel view so users can
213      * modify it there.
214      */
215     private void setDetails() {
216         int rownum = ttTippedTable.getSelectedRow();
217         try
218         {
219             rownum = tsSorter.getMappingToRow(rownum);
220         }
221         catch (Exception e)
222         {
223         }
224         if (rownum > -1)
225         {
226             mDetailPanel.updateDetailView((IX10MonitorEvent)mtmMonitorModel.getItemAt(rownum));
227         }
228     }
229     /***
230      * takes a table vector and puts it into
231      * the table model format then refreshes the UI
232      * to display the new table information.
233      * @param vTableData vector of events to build new table model.
234      */
235     public void setModel(Vector vTableData)
236     {
237         if (vTableData != null)
238         {
239             this.mtmMonitorModel.setList(vTableData);
240         }
241         updateUI();
242     }
243 
244     /***
245      *Sets the table column in the first position to show
246      *a graphic representation rather than a string name
247      */
248     private void initializeDeviceColumn()
249     {
250         TableColumn lampColumn = ttTippedTable.getColumn(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.DEVICE_COLUMN));
251         lampColumn.setCellRenderer(new LightRender());
252     }
253 
254     /***
255      * This deletes the table row from the table model
256      * and then updates the Hash Table to set the
257      * Key value(ModuleID) to null;
258      */
259     public void deleteRow()
260     {
261         int rownum = ttTippedTable.getSelectedRow();
262         try
263         {
264             rownum = tsSorter.getMappingToRow(rownum);
265         }
266         catch(Exception e)
267         {
268 
269         }
270         if (rownum > -1)
271         {
272             mtmMonitorModel.removeRow(rownum);
273             saveAllData();
274         }
275     }
276 
277     /***
278      * This is used to test the running of an event so that the
279      * user can manuall through the UI run the event such as email, execute a module, or
280      * run an application.  Again this is for testing of an action that they would like
281      * to monitor.
282      */
283     public void runEvent() {
284         int rownum = ttTippedTable.getSelectedRow();
285         try
286         {
287             rownum = tsSorter.getMappingToRow(rownum);
288         }
289         catch (Exception e)
290         {
291         }
292         if (rownum > -1)
293         {
294             AliceX10AddressListener x10Listener = new AliceX10AddressListener(mtmMonitorModel);
295             x10Listener.runAction((IX10MonitorEvent)mtmMonitorModel.getItemAt(rownum));
296         }
297     }
298 
299     /***
300      * Adds a new row to the table model
301      * By taking an IX10MonitorEvent object
302      * @param rowData added IX10MonitorEvent to table model
303      */
304     public void addNewRow(IX10MonitorEvent rowData)
305     {
306         mtmMonitorModel.addRow(rowData);
307         saveAllData();
308     }
309 
310     /***
311      * Updates a currently selected row from the
312      * details panel information
313      * @param rowData IX10MonitorEvent to update to the table model.
314      */
315     public void updateRow(IX10MonitorEvent rowData)
316     {
317         int rownum = ttTippedTable.getSelectedRow();
318         try
319         {
320             rownum = tsSorter.getMappingToRow(rownum);
321         }
322         catch(Exception e)
323         {
324         }
325         if (rownum > -1)
326         {
327             mtmMonitorModel.setValueAt(rowData, rownum);
328             saveAllData();
329         }
330         updateUI();
331     }
332 
333     /***
334      * Save the entire table model into a serialized file
335      */
336     public void saveAllData()
337     {
338         int rowCount = ttTippedTable.getRowCount();
339         Vector listItems = new Vector();
340         for(int i = 0; i < rowCount; i++)
341         {
342             listItems.addElement(mtmMonitorModel.getItemAt(i));
343         }
344         AutoHomeAdminSession.getInstance().saveAllAliceMonitors(listItems);
345     }
346 }