View Javadoc

1   package org.wcb.autohome;
2   
3   import org.wcb.autohome.interfaces.X10DeviceConstants;
4   import org.wcb.autohome.interfaces.I18nConstants;
5   import org.wcb.autohome.interfaces.IX10MonitorEvent;
6   import org.wcb.autohome.interfaces.IRunEvent;
7   import org.wcb.autohome.util.DeviceIDRenderer;
8   import org.wcb.autohome.util.Item;
9   import org.wcb.autohome.implementations.X10MonitorEvent;
10  import org.wcb.autohome.implementations.X10Module;
11  import org.wcb.autohome.implementations.RunEvent;
12  
13  import javax.swing.*;
14  import java.awt.*;
15  import java.awt.event.ActionListener;
16  import java.awt.event.ActionEvent;
17  import java.io.File;
18  import java.util.StringTokenizer;
19  
20  /***
21   * Copyright (C) 1999  Walter Bogaardt
22   *
23   * This library is free software; you can redistribute it and/or
24   * modify it under the terms of the GNU Lesser General Public
25   * License as published by the Free Software Foundation; either
26   * version 2 of the License, or (at your option) any later version.
27   *
28   * This library is distributed in the hope that it will be useful,
29   * but WITHOUT ANY WARRANTY; without even the implied warranty of
30   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
31   * Lesser General Public License for more details.
32   *
33   * You should have received a copy of the GNU Lesser General Public
34   * License along with this library; if not, write to the Free Software
35   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
36   *
37   * Project: Alice X10 Home Automation
38   * Filename:  $Id: MonitorDetailPanel.java,v 1.7 2004/02/04 05:20:28 wbogaardt Exp $<BR>
39   * Abstract: Used to display monitoring information on the monitor panel and run the
40   *           displays the events table.
41   *
42   * $Log: MonitorDetailPanel.java,v $
43   * Revision 1.7  2004/02/04 05:20:28  wbogaardt
44   * removed pop up message from clicking cancel button on monitor panel and removed unused methods infactory
45   *
46   * Revision 1.6  2004/01/31 07:40:23  wbogaardt
47   * 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.
48   *
49   */
50  public class MonitorDetailPanel extends JPanel implements ActionListener, SwingConstants, X10DeviceConstants{
51  
52      private MonitorPanel mPanel;
53      private JButton jbAddButton;
54      private JButton jbUpdateButton;
55      private JButton jbFindButton;
56      private JComboBox jcbInstalledModules;
57      private JFileChooser jfcChooser;
58  
59      private JTextField jtfDescription = new JTextField(15);
60      private JTextField jtfLocation = new JTextField(15);
61      private JTextField jtfApplicationName = new JTextField(25);
62      private JTextField jtfApplicationParameter = new JTextField(25);
63      private JTextField jtfSpeakText = new JTextField(50);
64      private JComboBox jcbAction;
65      private JComboBox jcbRunModule = new JComboBox();
66      private JComboBox jcbActionEvent;
67  
68      private String[] sActionArray = {"Send Email", "Run Application", "Speak Text"};
69      private String[] sEventsArray = {"Off","ON"};
70  
71      public MonitorDetailPanel(MonitorPanel mPanel)
72      {
73          this.mPanel = mPanel;
74          this.setupComponents();
75          this.setupListeners();
76      }
77  
78      private void setupComponents(){
79          this.jbAddButton = new JButton(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.ADD_BUTTON));
80          this.jbUpdateButton = new JButton(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.UPDATE_BUTTON));
81          this.jbFindButton = new JButton(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.FIND_BUTTON));
82          this.jcbInstalledModules = new JComboBox();
83          this.updateDeviceID(this.jcbInstalledModules);
84          this.updateDeviceID(this.jcbRunModule);
85          this.jcbAction = new JComboBox(sActionArray);
86          this.jcbActionEvent = new JComboBox(sEventsArray);
87  
88          setBorder(BorderFactory.createTitledBorder(BorderFactory
89                  .createLineBorder(Color.black),AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.DETAIL_LABEL)));
90  
91          GridBagConstraints gridBagConstraints;
92          JLabel monitor = new JLabel("Monitor");
93          JLabel description = new JLabel("Description");
94          JLabel jLabel1 = new JLabel("Location");
95          JLabel execute = new JLabel("Execute");
96          JLabel module = new JLabel("Module");
97          JLabel jLabel2 = new JLabel("Action");
98          JLabel jLabel3 = new JLabel("Run Application");
99          JLabel jLabel4 = new JLabel("Parameters");
100         JLabel jLabel5 = new JLabel("Text to Speak");
101 
102         setLayout(new GridBagLayout());
103 
104         gridBagConstraints = new GridBagConstraints();
105         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
106         gridBagConstraints.anchor = GridBagConstraints.EAST;
107         add(monitor, gridBagConstraints);
108 
109         gridBagConstraints = new GridBagConstraints();
110         gridBagConstraints.anchor = GridBagConstraints.WEST;
111         add(jcbInstalledModules, gridBagConstraints);
112 
113         gridBagConstraints = new GridBagConstraints();
114         gridBagConstraints.anchor = GridBagConstraints.EAST;
115         add(description, gridBagConstraints);
116 
117 
118         gridBagConstraints = new GridBagConstraints();
119         gridBagConstraints.gridwidth = 2;
120         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
121         add(jtfDescription, gridBagConstraints);
122 
123 
124         gridBagConstraints = new GridBagConstraints();
125         gridBagConstraints.gridx = 5;
126         gridBagConstraints.gridy = 0;
127         add(jbAddButton, gridBagConstraints);
128 
129 
130         gridBagConstraints = new GridBagConstraints();
131         gridBagConstraints.gridx = 6;
132         gridBagConstraints.gridy = 0;
133         add(jbUpdateButton, gridBagConstraints);
134 
135         gridBagConstraints = new GridBagConstraints();
136         gridBagConstraints.gridx = 0;
137         gridBagConstraints.gridy = 1;
138         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
139         gridBagConstraints.anchor = GridBagConstraints.EAST;
140         add(jLabel1, gridBagConstraints);
141 
142 
143         gridBagConstraints = new GridBagConstraints();
144         gridBagConstraints.gridx = 1;
145         gridBagConstraints.gridy = 1;
146         gridBagConstraints.gridwidth = 2;
147         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
148         add(jtfLocation, gridBagConstraints);
149 
150         gridBagConstraints = new GridBagConstraints();
151         gridBagConstraints.gridx = 3;
152         gridBagConstraints.gridy = 1;
153         gridBagConstraints.anchor = GridBagConstraints.WEST;
154         add(execute, gridBagConstraints);
155 
156         gridBagConstraints = new GridBagConstraints();
157         gridBagConstraints.gridx = 4;
158         gridBagConstraints.gridy = 1;
159         gridBagConstraints.anchor = GridBagConstraints.WEST;
160         add(jcbAction, gridBagConstraints);
161 
162         gridBagConstraints = new GridBagConstraints();
163         gridBagConstraints.gridx = 0;
164         gridBagConstraints.gridy = 2;
165         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
166         add(module, gridBagConstraints);
167 
168         gridBagConstraints = new GridBagConstraints();
169         gridBagConstraints.gridx = 1;
170         gridBagConstraints.gridy = 2;
171         gridBagConstraints.anchor = GridBagConstraints.WEST;
172         add(jcbRunModule, gridBagConstraints);
173 
174         gridBagConstraints = new GridBagConstraints();
175         gridBagConstraints.gridx = 2;
176         gridBagConstraints.gridy = 2;
177         gridBagConstraints.anchor = GridBagConstraints.EAST;
178         add(jLabel2, gridBagConstraints);
179 
180         gridBagConstraints = new GridBagConstraints();
181         gridBagConstraints.gridx = 3;
182         gridBagConstraints.gridy = 2;
183         gridBagConstraints.anchor = GridBagConstraints.WEST;
184         add(jcbActionEvent, gridBagConstraints);
185 
186         gridBagConstraints = new GridBagConstraints();
187         gridBagConstraints.gridx = 0;
188         gridBagConstraints.gridy = 3;
189         add(jLabel3, gridBagConstraints);
190 
191 
192         gridBagConstraints = new GridBagConstraints();
193         gridBagConstraints.gridx = 1;
194         gridBagConstraints.gridy = 3;
195         gridBagConstraints.gridwidth = 3;
196         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
197         add(jtfApplicationName, gridBagConstraints);
198 
199 
200         gridBagConstraints = new GridBagConstraints();
201         gridBagConstraints.gridx = 4;
202         gridBagConstraints.gridy = 3;
203         add(jbFindButton, gridBagConstraints);
204 
205         gridBagConstraints = new GridBagConstraints();
206         gridBagConstraints.gridx = 0;
207         gridBagConstraints.gridy = 4;
208         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
209         add(jLabel4, gridBagConstraints);
210 
211 
212         gridBagConstraints = new GridBagConstraints();
213         gridBagConstraints.gridx = 1;
214         gridBagConstraints.gridy = 4;
215         gridBagConstraints.gridwidth = 3;
216         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
217         add(jtfApplicationParameter, gridBagConstraints);
218 
219         gridBagConstraints = new GridBagConstraints();
220         gridBagConstraints.gridx = 0;
221         gridBagConstraints.gridy = 5;
222         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
223         add(jLabel5, gridBagConstraints);
224 
225 
226         gridBagConstraints = new GridBagConstraints();
227         gridBagConstraints.gridx = 1;
228         gridBagConstraints.gridy = 5;
229         gridBagConstraints.gridwidth = 3;
230         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
231         add(jtfSpeakText, gridBagConstraints);
232 
233         this.enableSection(RunEvent.SEND_EMAIL);
234     }
235 
236     private void setupListeners(){
237         this.jbAddButton.addActionListener(this);
238         this.jbUpdateButton.addActionListener(this);
239         this.jbFindButton.addActionListener(this);
240         this.jcbAction.addActionListener(this);
241     }
242 
243     /***
244      *Updates the drop down combo box with the list of available X10 modules
245      *added in the ModulePanel.  We wanted this information to updated to subsequent
246      *panels if we made changes to X10 devices so that they either become available
247      *or unavailable for either this panel or others.
248      */
249     public void updateDeviceID(){
250         this.updateDeviceID(this.jcbInstalledModules);
251         this.updateDeviceID(this.jcbRunModule);
252     }
253 
254     private void updateDeviceID(JComboBox jcb)
255     {
256         jcb = AutoHomeAdminSession.getInstance().setRenderedModules(jcb);
257         jcb.setRenderer(new DeviceIDRenderer());
258     }
259 
260     /***
261      * Must be implemented for this class
262      * but the panel to have an add and update button
263      * to update the monitoring of an event.
264      * @param evt ActionEvent on add update and find buttons
265      */
266     public void actionPerformed(ActionEvent evt)
267     {
268         Object src = evt.getSource();
269         if(src == jbAddButton)
270         {
271             mPanel.addNewRow(saveDetailData());
272         }
273         if(src == jbUpdateButton)
274         {
275             mPanel.updateRow(saveDetailData());
276         }
277         if(src == jbFindButton)
278         {
279             this.findApplication();
280         }
281         if(src == jcbAction){
282             this.enableSection(jcbAction.getSelectedIndex());
283         }
284     }
285 
286     /***
287      * Update the detail view of the row selected.
288      * @param me X10Monitor event to update to the different panels.
289      */
290     public void updateDetailView(IX10MonitorEvent me){
291 
292         /* display the item Module ID */
293         for (int i = 0; i < jcbInstalledModules.getItemCount(); i++)
294         {
295             if(((jcbInstalledModules.getItemAt(i)).toString()).startsWith(me.getMonitoringModule().getFullDeviceCode()))
296             {
297                 jcbInstalledModules.setSelectedIndex(i);
298             }
299         }
300         jtfDescription.setText(me.getDescription());
301         jtfLocation.setText(me.getLocation());
302         IRunEvent[] re =me.getRunEvent();
303         this.enableSection(re[0].getRunType());
304         // if the event is a run application event then set information in the text boxes
305         if(re[0].getRunType() == RunEvent.RUN_APPLICATION)
306         {
307             jtfApplicationName.setText(re[0].getCommand());
308             jtfApplicationParameter.setText(this.getArguments(re[0].getArguments()));
309         }
310         if(re[0].getRunType() == RunEvent.SPEAK_COMMAND)
311         {
312             jtfSpeakText.setText(re[0].getSentence());
313         }
314         //setup the drop down to the proper run module
315         for (int i = 0; i < this.jcbRunModule.getItemCount(); i++)
316         {
317             if(((this.jcbRunModule.getItemAt(i)).toString()).startsWith(re[0].getX10Module().getFullDeviceCode()))
318             {
319                 this.jcbRunModule.setSelectedIndex(i);
320             }
321         }
322         jcbActionEvent.setSelectedIndex(re[0].getModuleCommand());
323     }
324 
325     /***
326      * This enables the buttons on each of the sections so that
327      * if the command drop down is selected then the command options are enabled to the user and so on.
328      * @param i Section to enable.
329      */
330     private void enableSection(int i){
331         jcbAction.setSelectedIndex(i);
332         switch(i)
333         {
334             case RunEvent.SEND_EMAIL:
335                 jcbRunModule.setEnabled(false);
336                 jcbActionEvent.setEnabled(false);
337                 jtfApplicationName.setEnabled(false);
338                 jtfApplicationParameter.setEnabled(false);
339                 jbFindButton.setEnabled(false);
340                 jtfSpeakText.setEnabled(false);
341                 break;
342             case RunEvent.RUN_MODULE:
343                 jcbRunModule.setEnabled(true);
344                 jcbActionEvent.setEnabled(true);
345                 jtfApplicationName.setEnabled(false);
346                 jtfApplicationParameter.setEnabled(false);
347                 jbFindButton.setEnabled(false);
348                 jtfSpeakText.setEnabled(false);
349                 break;
350             case RunEvent.RUN_APPLICATION:
351                 jcbRunModule.setEnabled(false);
352                 jcbActionEvent.setEnabled(false);
353                 jtfApplicationName.setEnabled(true);
354                 jtfApplicationParameter.setEnabled(true);
355                 jbFindButton.setEnabled(true);
356                 jtfSpeakText.setEnabled(false);
357                 break;
358             case RunEvent.SPEAK_COMMAND:
359                 jcbRunModule.setEnabled(false);
360                 jcbActionEvent.setEnabled(false);
361                 jtfApplicationName.setEnabled(false);
362                 jtfApplicationParameter.setEnabled(false);
363                 jbFindButton.setEnabled(false);
364                 jtfSpeakText.setEnabled(true);
365                 break;
366         }
367     }
368 
369     /***
370      * This gets all of the details from the detail panel
371      * and then gets all the appropriate information formats it into a X10MonitoriEvent object and allows it
372      * to be saved to the table view and application files.
373      * @return X10MonitorEvent that can be saved.
374      */
375     private X10MonitorEvent saveDetailData(){
376         X10MonitorEvent xEvent = new X10MonitorEvent();
377         xEvent.setMonitoringModule(this.getModule((Item)jcbInstalledModules.getSelectedItem()));
378         xEvent.setDescription(jtfDescription.getText());
379         xEvent.setLocation(jtfLocation.getText());
380         RunEvent runEvent = new RunEvent();
381         runEvent.setRunType(jcbAction.getSelectedIndex()); // email, run module, run application
382         switch(jcbAction.getSelectedIndex()){
383             case RunEvent.SEND_EMAIL:
384                 break;
385             case RunEvent.RUN_MODULE:
386                 runEvent.setX10Module(this.getModule((Item)jcbRunModule.getSelectedItem())); //module
387                 runEvent.setModuleCommand(jcbActionEvent.getSelectedIndex()); //module's command
388             case RunEvent.RUN_APPLICATION:
389                 runEvent.setCommand(this.jtfApplicationName.getText());
390                 runEvent.setArguments(this.getArguments(this.jtfApplicationParameter.getText()));
391                 break;
392             case RunEvent.SPEAK_COMMAND:
393                 runEvent.setSentence(this.jtfSpeakText.getText());
394                 break;
395         }
396         RunEvent[] aRunEvent = {runEvent};
397         xEvent.setRunEvent(aRunEvent); //add runable events to overall thing.
398         return xEvent;
399     }
400 
401     /***
402      * Gets the Module from the drop down list and by selecting an index.
403      * @param iType Item object in the drop down list
404      * @return An X10Module.
405      */
406     private X10Module getModule(Item iType){
407         try
408         {
409             int i = Integer.parseInt((iType.toString()).substring(1));
410             return new X10Module(iType.toString().charAt(0), i, "", "Event", iType.getType());
411         }
412         catch(NumberFormatException err)
413         {
414             return new X10Module(iType.toString().charAt(0), 1, "", "Event", iType.getType());
415         }
416     }
417 
418     /***
419      * Parses the string and returns it as an array of strings.
420      * @param sParse Single string to parse into an array.
421      * @return Array of strings
422      */
423     private String[] getArguments(String sParse){
424         StringTokenizer stTokenizer = new StringTokenizer(sParse);
425         String[] sArrayReturn = new String[10];
426         int iCounter = 0;
427         while(stTokenizer.hasMoreTokens())
428         {
429             sArrayReturn[iCounter] = stTokenizer.nextToken();
430             iCounter++;
431         }
432         return sArrayReturn;
433     }
434 
435     /***
436      * Creates a displayable string from an array of strings.
437      * @param sParse The Array of strings
438      * @return One string with each array separated by a space.
439      */
440     private String getArguments(String[] sParse){
441         StringBuffer sb = new StringBuffer("");
442         if(sParse != null)
443         {
444             for(int i = 0; i < sParse.length; i++)
445             {
446                 if(sParse[i] != null)
447                 {
448                     sb.append(sParse[i] + " ");
449                 }
450             }
451         }
452         return sb.toString();
453     }
454     /***
455      * Opens a file open dialog box to allow the user to find
456      * the browser and then save that setting into the properties file
457      */
458     private void findApplication() {
459         if (jfcChooser == null) {
460             jfcChooser = new JFileChooser();
461         }
462         int fileState = jfcChooser.showOpenDialog(null);
463         File file = jfcChooser.getSelectedFile();
464         if (file !=null && fileState == JFileChooser.APPROVE_OPTION) {
465             jtfApplicationName.setText(file.getAbsolutePath());
466         }
467     }
468 }