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   *  $Log: ModulePanel.java,v $
21   *  Revision 1.13  2004/07/22 03:17:54  wbogaardt
22   *  removed deprecated methods
23   *
24   *  Revision 1.12  2004/01/18 00:48:31  wbogaardt
25   *  refactored out unnecessary code and now have a functional initial design of monitoring panel
26   *
27   *  Revision 1.11  2004/01/16 22:57:38  wbogaardt
28   *  Improved display layout of module panel and added basic monitoring panel
29   *  CV: ----------------------------------------------------------------------
30   *
31   *  Revision 1.10  2004/01/16 19:50:14  wbogaardt
32   *  refactored, fixed long standing bug with updating macro panels, add error notification to user for improper device codes
33   *
34   *  Revision 1.9  2004/01/16 00:53:34  wbogaardt
35   *  Fixed a very obscure bug with the Macro Panel that it didn't added new
36   *  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.
37   *
38   *  Revision 1.8  2003/12/30 18:47:40  wbogaardt
39   *  made labels so they are internationlized and fixed layout of trigger panel
40   *
41   *  Revision 1.7  2003/12/30 00:56:45  wbogaardt
42   *  added more internationalization to table column names.
43   *
44   *  Revision 1.6  2003/12/22 20:51:29  wbogaardt
45   *  refactored name assignments and formatted code for readability.
46   *
47   *  Revision 1.5  2003/12/13 05:36:50  wbogaardt
48   *  fixed javadoc comments.
49   *
50   *  Revision 1.4  2003/12/12 23:17:33  wbogaardt
51   *  javadoc comments refactored methods so they are more descriptive
52   *
53   *  Revision 1.3  2003/10/11 02:59:10  wbogaardt
54   *  added delete option to popu menu selection
55   *
56   *  Revision 1.2  2003/10/06 23:42:02  wbogaardt
57   *  Refactored code so that class is extracted from panel
58   *
59   */
60  import javax.swing.JPanel;
61  import javax.swing.JOptionPane;
62  import javax.swing.JScrollPane;
63  import javax.swing.BorderFactory;
64  import javax.swing.event.ListSelectionListener;
65  import javax.swing.event.ListSelectionEvent;
66  import javax.swing.table.TableColumn;
67  import java.awt.Dimension;
68  import java.awt.Color;
69  import java.awt.BorderLayout;
70  import java.awt.event.MouseEvent;
71  import java.awt.event.MouseAdapter;
72  import java.util.Vector;
73  import org.wcb.autohome.util.ui.LightRender;
74  import org.wcb.autohome.util.ModuleTablePopup;
75  import org.wcb.util.TooltippedTable;
76  import org.wcb.util.TableSorter;
77  import org.wcb.util.SortButtonRenderer;
78  import org.wcb.autohome.interfaces.IX10Module;
79  import org.wcb.autohome.interfaces.RefreshInterface;
80  import org.wcb.autohome.interfaces.I18nConstants;
81  import org.wcb.autohome.implementations.X10Module;
82  import org.wcb.autohome.model.ModuleTableModel;
83  
84  /***
85   *  Project:   Home Automation Interface<BR>
86   *  Filename:  $Id: ModulePanel.java,v 1.13 2004/07/22 03:17:54 wbogaardt Exp $<BR>
87   *  Abstract:  This is a java swing panel that displays the table list of X10 modules
88   *  and allows the selected x10 module to either be edited or a new module to be added.
89   *
90   *
91   * @author wbogaardt
92   * @version 1.2
93   *
94   */
95  public class ModulePanel extends JPanel {
96  
97      private ModuleTableModel moduleModel=null;
98      private ModuleTablePopup popup = null;
99      private MouseAdapter maMouse = null;
100     private ModuleDetailPanel detailPanel;
101     private JScrollPane jspTableScroller;
102     private JPanel jpTableDisplayPanel;
103     private ListSelectionListener lslListListener;
104     private TooltippedTable ttTippedTable;
105     private TableSorter tsSorter;
106     private SortButtonRenderer sortButtonRenderer;
107     private RefreshInterface refreshInterface;
108     private RefreshInterface iMacroRefresh;
109     private RefreshInterface iMonitorRefresh;
110     private static Vector vINSTALLED_MODULES;
111 
112     public ModulePanel() {
113         setupComponents();
114         setupListeners();
115     }
116 
117     /***
118      * Allow the EventsPanel to refresh the listing
119      * of X10Modules added.
120      * @param refresh EventsPanel refresh interface
121      */
122     public void setRefreshEventPanel(RefreshInterface refresh){
123         this.refreshInterface = refresh;
124     }
125 
126     /***
127      * Allow the Macro trigger and Macro Sequence panels to refresh
128      * themselves with the new listing of X10Modules that have
129      * been added.
130      * @param refresh MacroPanel refresh interface
131      */
132     public void setRefreshMacroPanel(RefreshInterface refresh)
133     {
134         this.iMacroRefresh = refresh;
135     }
136 
137     /***
138      * Allows the Monitor panel to refresh itself with
139      * the new listing of X10Modules to monitor.
140      * @param refresh MonitorPanel refresh interface
141      */
142     public void setRefreshMonitorPanel(RefreshInterface refresh)
143     {
144         this.iMonitorRefresh = refresh;
145     }
146 
147     private void setupComponents(){
148         /* setup default data table */
149         Vector defaultData=new Vector();
150         defaultData.addElement(new X10Module());
151 
152         setLayout(new BorderLayout());
153         jpTableDisplayPanel = new JPanel();
154         jpTableDisplayPanel.setBorder(BorderFactory
155                 .createTitledBorder
156                 (BorderFactory.createLineBorder
157                 (Color.black),AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.INSTALLED_MODULES_LABEL)));
158         moduleModel = new ModuleTableModel(defaultData);
159         popup = new ModuleTablePopup(this);
160         popup.setVisible(false);
161         sortButtonRenderer = new SortButtonRenderer();
162         if (moduleModel != null) {
163             /* sorting stuff for the table */
164             tsSorter = new TableSorter(moduleModel);
165             ttTippedTable = new TooltippedTable(tsSorter);
166             ttTippedTable.setColHeaderRender(sortButtonRenderer);
167             tsSorter.addMouseListenerToHeaderInTable(ttTippedTable, sortButtonRenderer);
168             tsSorter.sortByColumn(2, false);
169             jspTableScroller = new JScrollPane(ttTippedTable);
170             jspTableScroller.setPreferredSize(new Dimension(360, 330));
171             jpTableDisplayPanel.add(jspTableScroller, BorderLayout.CENTER);
172             this.initializeLampColumn();
173         }
174         this.jpTableDisplayPanel.setPreferredSize(new Dimension(360, 330));
175         detailPanel = new ModuleDetailPanel(this);
176         add(jpTableDisplayPanel, BorderLayout.CENTER);
177         add(detailPanel, BorderLayout.EAST);
178     }
179 
180     private void setupListeners() {
181         lslListListener = new ListSelectionListener(){
182             public void valueChanged(ListSelectionEvent e) {
183                 if(!e.getValueIsAdjusting()){
184                     setDetails();
185                 }
186             }
187         };
188 
189         /* mouse Listener for the table view and popup menu */
190         maMouse = new MouseAdapter(){
191             public void mousePressed(MouseEvent mice)
192             {
193                 if((mice.getModifiers() == MouseEvent.META_MASK))
194                 {
195                     int rownum = ttTippedTable.getSelectedRow();
196                     try {
197                         rownum = tsSorter.getMappingToRow(rownum);
198                     } catch(Exception err){}
199                     if (rownum > -1)
200                     {
201                         popup.setModule((IX10Module)moduleModel.getItemAt(rownum));
202                         popup.setVisible(true);
203                         popup.show(mice.getComponent(), mice.getX(), mice.getY());
204                     }
205                 }
206                 else
207                 {
208                     popup.setVisible(false);
209                 }
210             }
211         };
212 
213         /* adds mouse listener only if table model is present */
214         if (moduleModel != null)
215         {
216             ttTippedTable.addMouseListener(maMouse);
217             ttTippedTable.getSelectionModel().addListSelectionListener(lslListListener);
218         }
219     }
220 
221     /***
222      * Sets the table model view with a Vector of
223      * IX10Module. This vecto becomes the new table
224      * model view and the old table is destroyed.
225      */
226     public void setModel(Vector tableData){
227         if (tableData != null)
228         {
229             this.moduleModel.setList(tableData);
230         }
231         updateUI();
232     }
233 
234     /***
235      *  This modifies the table model column and changes
236      *  the view to be represented by an icon in place
237      *  of lettering.
238      */
239     private void initializeLampColumn() {
240         TableColumn lampColumn = ttTippedTable.getColumn(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.DEVICE_COLUMN));
241         lampColumn.setCellRenderer(new LightRender());
242     }
243 
244     /***
245      * When the user double clicks a row that row
246      * updates the detail panel view so users can
247      * modify it there.
248      */
249     private void setDetails() {
250         int rownum = ttTippedTable.getSelectedRow();
251         try
252         {
253             rownum = tsSorter.getMappingToRow(rownum);
254         }
255         catch (Exception e)
256         {
257         }
258         if (rownum > -1)
259         {
260             detailPanel.updateView((IX10Module)moduleModel.getItemAt(rownum));
261         }
262     }
263 
264     /***
265      * loads the modules and adds them to the table model for
266      * the user.  This event is started by
267      * the File open confirm actions
268      */
269     public void loadData(){
270         vINSTALLED_MODULES = AutoHomeAdminSession.getInstance().loadAllX10Devices();
271         setModel(vINSTALLED_MODULES);
272     }
273 
274     /***
275      * This deletes the table row from the table model
276      * and then updates the Hash Table to set the
277      * Key value(ModuleID) to null;
278      */
279     public void deleteRow() {
280         int rownum = ttTippedTable.getSelectedRow();
281         try
282         {
283             rownum = tsSorter.getMappingToRow(rownum);
284         }
285         catch (Exception e)
286         {
287         }
288         if (rownum > -1)
289         {
290             moduleModel.removeRow(rownum);
291             saveAllData();
292         }
293     }
294 
295     /***
296      * Adds a new row to the table model
297      *
298      * @param rowData element to add to table model
299      */
300     public void addNewRow(IX10Module rowData) {
301         if (moduleModel != null )
302         {
303             /* if the module already exists then stop the User */
304             boolean matched = false;
305             for (int i = 0; i < moduleModel.getRowCount(); i++) {
306                 if ((rowData.getFullDeviceCode()).endsWith((String)moduleModel.getValueAt(i, 1)))
307                 {
308                     JOptionPane.showMessageDialog(null,
309                             "You already have a module defined with this ID",
310                             "Error", JOptionPane.ERROR_MESSAGE);
311                     matched = true;
312                 }
313             }
314             /* The module did not exist so add it to the list */
315             if (!matched)
316             {
317                 moduleModel.addRow(rowData);
318                 saveAllData();
319             }
320         }
321         else if (moduleModel == null)
322         {
323             Vector dataVector = new Vector();
324             dataVector.addElement(rowData);
325             setModel(dataVector);
326             saveAllData();
327         }
328     }
329 
330     /***
331      * Updates the currently selected row
332      * from the details panel information.
333      * @param rowData module of the selected row.
334      */
335     public void updateRow(IX10Module rowData) {
336         int rownum = ttTippedTable.getSelectedRow();
337         try
338         {
339             rownum = tsSorter.getMappingToRow(rownum);
340         }
341         catch (Exception e)
342         {
343         }
344         if (rownum > -1)
345         {
346             moduleModel.setValueAt(rowData, rownum);
347             saveAllData();
348         }
349         updateUI();
350     }
351 
352     /***
353      * Save the entire table model into a properties
354      * Hash table key for quick access and storage;
355      */
356     public void saveAllData(){
357         int rowCount = moduleModel.getRowCount();
358         vINSTALLED_MODULES = new Vector();
359         for(int i = 0; i < rowCount; i++)
360         {
361             vINSTALLED_MODULES.addElement(moduleModel.getItemAt(i));
362         }
363         AutoHomeAdminSession.getInstance().saveAllX10Devices(vINSTALLED_MODULES);
364         this.refreshInterface.refresh();
365         this.iMacroRefresh.refresh();
366         this.iMonitorRefresh.refresh();
367     }
368 }
369 
370