View Javadoc

1   package org.wcb.autohome;
2   /***
3    * Copyright (C) 1999  Walter Bogaardt
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
18   *
19   * Project: Alice X10 Home Automation
20   */
21  import javax.swing.JPanel;
22  import javax.swing.JScrollPane;
23  import javax.swing.JLabel;
24  import javax.swing.BoxLayout;
25  import javax.swing.BorderFactory;
26  import javax.swing.JSplitPane;
27  import javax.swing.event.ListSelectionListener;
28  import javax.swing.event.ListSelectionEvent;
29  import javax.swing.table.TableColumn;
30  import java.awt.Color;
31  import java.awt.Dimension;
32  import java.awt.Component;
33  import java.awt.BorderLayout;
34  import java.awt.event.MouseAdapter;
35  import java.awt.event.MouseEvent;
36  import java.util.Vector;
37  import org.wcb.autohome.model.EventsTableModel;
38  import org.wcb.autohome.util.TablePopup;
39  import org.wcb.autohome.util.ui.LightRender;
40  import org.wcb.util.TooltippedTable;
41  import org.wcb.util.SortButtonRenderer;
42  import org.wcb.util.TableSorter;
43  import org.wcb.autohome.interfaces.RefreshInterface;
44  import org.wcb.autohome.interfaces.IX10Events;
45  import org.wcb.autohome.interfaces.I18nConstants;
46  import org.wcb.autohome.implementations.X10Events;
47  
48  /***
49   * * Project: Alice X10 Home Automation<BR>
50   * Filename:  $Id: EventsPanel.java,v 1.18 2004/07/22 03:06:49 wbogaardt Exp $<BR>
51   * Abstract: Used to display detail information on the events panel and run the
52   *           displays the events table.
53   *
54   * $Log: EventsPanel.java,v $
55   * Revision 1.18  2004/07/22 03:06:49  wbogaardt
56   * removed deprecated method calls.
57   *
58   * Revision 1.17  2004/02/25 21:38:32  wbogaardt
59   * added javadocs and fixed formating for checkstyle report
60   *
61   * Revision 1.16  2004/02/01 20:42:48  wbogaardt
62   * removed form panel reference and changed sizing of the split bars
63   *
64   * Revision 1.15  2004/01/21 05:29:40  wbogaardt
65   * fixed bug saving file format and added disable monitoring
66   *
67   * Revision 1.14  2004/01/18 00:48:31  wbogaardt
68   * refactored out unnecessary code and now have a functional initial design of monitoring panel
69   *
70   * Revision 1.13  2004/01/17 07:21:15  wbogaardt
71   * added serialization to run events and allow monitoring of these events to the file system to reload later
72   *
73   * Revision 1.12  2004/01/16 22:57:38  wbogaardt
74   * Improved display layout of module panel and added basic monitoring panel
75   * CV: ----------------------------------------------------------------------
76   *
77   * Revision 1.11  2004/01/16 19:50:14  wbogaardt
78   * refactored, fixed long standing bug with updating macro panels, add error notification to user for improper device
79   * codes
80   *
81   * Revision 1.10  2004/01/16 00:53:34  wbogaardt
82   * Fixed a very obscure bug with the Macro Panel that it didn't added new
83   * x10 devices to the drop down of available x10 device for the macro. Modified Macro triggers to change the events to
84   * integer verses strings cleaner this way.
85   *
86   * Revision 1.9  2004/01/15 21:05:17  wbogaardt
87   * major revamp of Modules and interfaces changes overall structure of how information is stored
88   *
89   * Revision 1.8  2003/12/30 18:47:40  wbogaardt
90   * made labels so they are internationlized and fixed layout of trigger panel
91   *
92   * Revision 1.7  2003/12/30 00:56:45  wbogaardt
93   * added more internationalization to table column names.
94   *
95   * Revision 1.6  2003/12/22 20:51:29  wbogaardt
96   * refactored name assignments and formatted code for readability.
97   *
98   * Revision 1.5  2003/12/12 23:17:33  wbogaardt
99   * javadoc comments refactored methods so they are more descriptive
100  *
101  * Revision 1.4  2003/10/10 22:50:43  wbogaardt
102  * removed error messages and cleaned up format
103  *
104  * Revision 1.3  2003/10/10 18:39:09  wbogaardt
105  * changed date time information from a string to a calendar object
106  *
107  *
108  *@author wbogaardt
109  *@version v 1.15
110  */
111 public class EventsPanel extends JPanel implements RefreshInterface {
112 
113     private TooltippedTable ttTippedTable;
114     private EventsTableModel deviceModel = null;
115     private EventsDetailPanel eventsDetailPanel;
116     private JScrollPane jsScrollingPane;
117     private TablePopup tpPopup;
118     private JPanel jpTablePanel;
119     private TableSorter tsSorter;
120     private JLabel jlDeviceCount;
121     private MouseAdapter maMouse;
122     private ListSelectionListener lslListListener;
123     private SortButtonRenderer sortButtonRenderer;
124 
125     private static Vector vEVENTS_VECTOR;
126 
127     /***
128      * Creates the events panel with a top component of a table
129      * and a bottom component of a detail panel.
130      */
131     public EventsPanel()
132     {
133         setupComponents();
134         setupListeners();
135     }
136 
137     /***
138      * Sets up the panel's swing components.
139      */
140     private void setupComponents()
141     {
142         /* setup default data */
143         Vector defaultData = new Vector();
144         defaultData.addElement(new X10Events());
145 
146         deviceModel = new EventsTableModel(defaultData);
147         setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
148 
149         /*set up the table panel */
150         jpTablePanel = new JPanel();
151         jpTablePanel.setBorder(BorderFactory
152                 .createTitledBorder
153                 (BorderFactory.createLineBorder
154                 (Color.black), AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.EVENTS_LABEL)));
155         jpTablePanel.setPreferredSize(new Dimension(650, 300)); //650,115
156         tpPopup = new TablePopup(this);
157         tpPopup.setVisible(false);
158         sortButtonRenderer = new SortButtonRenderer();
159         jlDeviceCount = new JLabel(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.EVENTS_TOTAL_LABEL));
160         jlDeviceCount.setAlignmentX(Component.RIGHT_ALIGNMENT);
161 
162         if (deviceModel != null)
163         {
164             tsSorter = new TableSorter(deviceModel);
165             ttTippedTable = new TooltippedTable(tsSorter);
166             tsSorter.addMouseListenerToHeaderInTable(ttTippedTable, sortButtonRenderer);
167             tsSorter.sortByColumn(2, false);
168             jsScrollingPane = new JScrollPane(ttTippedTable);
169             jsScrollingPane.setPreferredSize(new Dimension(620, 200)); //620,105
170             jpTablePanel.add(jlDeviceCount, BorderLayout.NORTH);
171             jpTablePanel.add(jsScrollingPane, BorderLayout.CENTER);
172             this.initializeDeviceColumn();
173         }
174         /* setup the detail panel */
175         eventsDetailPanel = new EventsDetailPanel(this);
176 
177         JSplitPane mySplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT, jpTablePanel, eventsDetailPanel);
178         mySplit.setDividerLocation(215);
179         mySplit.setOneTouchExpandable(true);
180         add(mySplit);
181     }
182 
183     /***
184      *Set up listeners for the maMouse events
185      *and other actions
186      */
187     private void setupListeners()
188     {
189         lslListListener = new ListSelectionListener() {
190             public void valueChanged(ListSelectionEvent e)
191             {
192                 if (!e.getValueIsAdjusting())
193                 {
194                     setDetails();
195                 }
196             }
197         };
198         /* This maMouse adapter it used for selecting various rows in the table */
199         maMouse = new MouseAdapter() {
200             public void mousePressed(MouseEvent mice)
201             {
202                 if ((mice.getModifiers() == MouseEvent.META_MASK))
203                 {
204                     tpPopup.setVisible(true);
205                     tpPopup.show(mice.getComponent(), mice.getX(), mice.getY());
206                 }
207                 else
208                 {
209                     tpPopup.setVisible(false);
210                 }
211             }
212         };
213         if (deviceModel != null)
214         {
215             ttTippedTable.addMouseListener(maMouse);
216             ttTippedTable.getSelectionModel().addListSelectionListener(lslListListener);
217         }
218     }
219 
220     /***
221      * takes a table vector and puts it into
222      * the table model format then refreshes the UI
223      * to display the new table information.
224      * @param vTableData vector of events to build new table model.
225      */
226     public void setModel(Vector vTableData)
227     {
228         if (vTableData != null)
229         {
230             deviceModel.setList(vTableData);
231         }
232         updateUI();
233     }
234 
235     /***
236      *Sets the table column in the first position to show
237      *a graphic representation rather than a string name
238      */
239     private void initializeDeviceColumn()
240     {
241         TableColumn lampColumn = ttTippedTable.getColumn(
242                 AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.DEVICE_COLUMN));
243         lampColumn.setCellRenderer(new LightRender());
244     }
245 
246     /***
247      * Sets the details panel information to the selected table row
248       */
249     private void setDetails()
250     {
251         int rownum = ttTippedTable.getSelectedRow();
252         try
253         {
254             rownum = tsSorter.getMappingToRow(rownum);
255         }
256         catch (Exception e)
257         {  //sort error
258         }
259         if (rownum > -1)
260         {
261             eventsDetailPanel.updateView((IX10Events) deviceModel.getItemAt(rownum));
262         }
263     }
264 
265     /***
266      * Shows an updated count when new events are added to the table.
267      */
268     private void updateCount()
269     {
270         jlDeviceCount.setText(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.EVENTS_TOTAL_LABEL)
271                 + ttTippedTable.getRowCount());
272     }
273 
274     /***
275      * This returns an instance of the MessageInterface
276      * that is used in this class to control the various
277      * messages.
278      * @return this panel's RefreshInterface.
279      */
280     public RefreshInterface getInterface()
281     {
282         return this;
283     }
284 
285     /***
286      *Allows calling of the refresh interface so that it
287      *updates the drop down box with the new list of available
288      *X10 device modules as entered in the ModulePanel.
289      */
290     public void refresh()
291     {
292         eventsDetailPanel.updateDeviceID();
293     }
294 
295     /***
296      * loads the modules and adds them to the table model for
297      * the user.  This event is started by
298      * the File open confirm actions
299      * @return list of events from file system
300      */
301     public Vector loadData()
302     {
303         vEVENTS_VECTOR = AutoHomeAdminSession.getInstance().loadAllAliceEvents();
304         setModel(vEVENTS_VECTOR);
305         return vEVENTS_VECTOR;
306     }
307 
308     /***
309      * This deletes the table row from the table model
310      * and then updates the Hash Table to set the
311      * Key value(ModuleID) to null;
312      */
313     public void deleteRow()
314     {
315         int rownum = ttTippedTable.getSelectedRow();
316         try
317         {
318             rownum = tsSorter.getMappingToRow(rownum);
319         }
320         catch (Exception e)
321         {
322 
323         }
324         if (rownum > -1)
325         {
326             deviceModel.removeRow(rownum);
327             saveAllData();
328         }
329     }
330 
331     /***
332      * Adds a new row to the table model
333      * By taking an IX10Events object
334      * @param rowData added X10Event to table model
335      */
336     public void addNewRow(IX10Events rowData)
337     {
338         deviceModel.addRow(rowData);
339         saveAllData();
340     }
341     /***
342      * Updates a currently selected row from the
343      * details panel information
344      * @param rowData X10Event to update to the table model.
345      */
346     public void updateRow(IX10Events rowData)
347     {
348         int rownum = ttTippedTable.getSelectedRow();
349         try
350         {
351             rownum = tsSorter.getMappingToRow(rownum);
352         }
353         catch (Exception e)
354         {
355         }
356         if (rownum > -1)
357         {
358             deviceModel.setValueAt(rowData, rownum);
359             saveAllData();
360         }
361         updateUI();
362     }
363 
364     /***
365      * Save the entire table model into a properties
366      * The format for the key is Event.EV+rowNumber=
367      * Module type, Description, MODULEID, Action, -------, ACTION TIME
368      * ex: Event.EV1=Lamp, Living Room Light, A1, On,-MT-TFS, 15:50
369      */
370     public void saveAllData()
371     {
372         int rowCount = ttTippedTable.getRowCount();
373         Vector listItems = new Vector();
374         for (int i = 0; i < rowCount; i++)
375         {
376             listItems.addElement(deviceModel.getItemAt(i));
377         }
378         updateCount();
379         AutoHomeAdminSession.getInstance().saveAllAliceEvents(listItems);
380     }
381 }
382 
383 
384 
385 
386