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   */
20  
21  /*standard java classes needed*/
22  import java.util.Properties;
23  import java.util.Vector;
24  import java.util.Enumeration;
25  
26  /*my stuff needed */
27  import org.wcb.autohome.util.Item;
28  import org.wcb.autohome.interfaces.MessageInterface;
29  import org.wcb.autohome.implementations.Macro;
30  import org.wcb.autohome.interfaces.IX10Module;
31  import org.wcb.autohome.exceptions.HomeException;
32  import org.wcb.common.UIDefaultsLoader;
33  
34  import javax.swing.ImageIcon;
35  import javax.swing.JComboBox;
36  import java.awt.Toolkit;
37  
38  
39  /***
40   *  Project:   Home Automation Interface
41   *  Filename:  $Id: AutoHomeAdminSession.java,v 1.27 2004/06/09 18:52:34 wbogaardt Exp $
42   *  Abstract:  Centralized backend for various components to send events to
43   *             Serial port output. Also used to handle connection between manipulating files
44   *             information.
45   *
46   *
47   * @author wbogaardt
48   * @version 1.48
49   *
50   *  $Log: AutoHomeAdminSession.java,v $
51   *  Revision 1.27  2004/06/09 18:52:34  wbogaardt
52   *  changed enum to enumr to avoid java keyword issue
53   *
54   *  Revision 1.26  2004/02/08 03:22:40  wbogaardt
55   *   fixed a number of bugs with new startup of swing application
56   *
57   *  Revision 1.25  2004/02/03 21:02:24  wbogaardt
58   *  moved DeviceFactory away from rmi creation and simplified interface between gateway
59   *
60   *  Revision 1.24  2004/02/03 00:35:06  wbogaardt
61   *  reogranized files so that serial port settings are maintained in a bean to interface with config file
62   *
63   *  Revision 1.23  2004/01/18 00:48:31  wbogaardt
64   *  refactored out unnecessary code and now have a functional initial design of monitoring panel
65   *
66   *  Revision 1.22  2004/01/16 00:53:34  wbogaardt
67   *  Fixed a very obscure bug with the Macro Panel that it didn't added new
68   *  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.
69   *
70   *  Revision 1.21  2004/01/02 21:02:51  wbogaardt
71   *  removed excessive logging statements, and consolidated some variables
72   *
73   *  Revision 1.20  2003/12/31 01:09:37  wbogaardt
74   *  removed dead string reference to jhome.prop
75   *
76   *  Revision 1.19  2003/12/31 00:51:47  wbogaardt
77   *  refactored so that look and feel is read from HASConfig.ini
78   *
79   *  Revision 1.18  2003/12/31 00:08:08  wbogaardt
80   *  fixed bug with setting look and feel and having it persist for user next time open
81   *
82   *  Revision 1.17  2003/12/24 20:24:17  wbogaardt
83   *  Fixed display of console message on start up
84   *
85   *  Revision 1.16  2003/12/13 05:36:50  wbogaardt
86   *  fixed javadoc comments.
87   *
88   *
89   */
90  public class AutoHomeAdminSession extends AutoHomeSession {
91  
92      private static AutoHomeAdminSession singleton = null;
93      public static ImageIcon LIGHTICON, LIGHTICON_OFF, APPLIANCEICON, APPLIANCEICON_OFF,
94      DAYON, DAYOFF, INTERFACE_ON, INTERFACE_OFF,
95      SUNRISE, SUNSET, CLOCKICON, CAUTIONICON, DELETE_ROW_ICON;
96      private MessageInterface messageInterface=null;
97      private static UIDefaultsLoader PROPERTY_LOADER;
98  
99      /***
100      * This class provides the application a singular point of
101      * connection to the CM11A or CM17 devices.  This handles
102      * all of the interaction between the GUI and the X10 API.
103      */
104     private AutoHomeAdminSession(){
105         PROPERTY_LOADER = new UIDefaultsLoader();
106         super.loadStandAloneService(USER_HOME);
107     }
108 
109     private AutoHomeAdminSession(String dirname){
110         PROPERTY_LOADER = new UIDefaultsLoader(null,dirname);
111         super.loadStandAloneService(dirname);
112     }
113 
114     /***
115      * This provides a single static class for the
116      * entire application.
117      */
118     public static AutoHomeAdminSession getInstance() {
119         if (null==singleton){
120             singleton = new AutoHomeAdminSession();
121         }
122         return singleton;
123     }
124 
125     public static AutoHomeAdminSession getInstance(String dirname){
126         if(null==singleton){
127             singleton = new AutoHomeAdminSession(dirname);
128         }
129         return singleton;
130     }
131 
132     public void initiateGUI(){
133         singleton.loadImages();
134         singleton.loadLastSettings();
135     }
136 
137     public void setMessageInterface(MessageInterface msg){
138         messageInterface = msg;
139     }
140 
141     /***
142      * simple method to print messages to gui or console
143      */
144     public void printMessage(String messages) {
145         /* print the message to the GUI or to console */
146         if (messageInterface !=null)
147         {
148             messageInterface.printMessage(messages);
149         }
150         showMessage(messages);
151     }
152 
153     /***
154      * returns a reference to the MessageInterface provided
155      * to this class. (used by MacroSequencePanel)
156      */
157     public MessageInterface getMessageInterface() {
158         return messageInterface;
159     }
160 
161     /***
162      * accessor to get the current application setting hashtable
163      * via the Property loader object to handle additonal features
164      * such as file saving
165      */
166     public UIDefaultsLoader getAppProperties() {
167         return PROPERTY_LOADER;
168     }
169 
170     public void setAppProperties(Properties app){
171         PROPERTY_LOADER.setProperties(app);
172     }
173 
174     /***
175      * Load the images here as static so that
176      * they don't chew up the memory as quickly
177      * before as dynamic they ate up almost a meg of memory
178      * each creation on table views loading.
179      */
180     private void loadImages() {
181         LIGHTICON = new ImageIcon(Toolkit.getDefaultToolkit().createImage(AutoHomeAdminSession.class.getResource("/images/Bulb.gif")));
182         LIGHTICON_OFF = new ImageIcon(Toolkit.getDefaultToolkit().createImage(AutoHomeAdminSession.class.getResource("/images/OffBulb.gif")));
183         APPLIANCEICON = new ImageIcon(Toolkit.getDefaultToolkit().createImage(AutoHomeAdminSession.class.getResource("/images/Plug.gif")));
184         APPLIANCEICON_OFF = new ImageIcon(Toolkit.getDefaultToolkit().createImage(AutoHomeAdminSession.class.getResource("/images/UnPlug.gif")));
185         DAYON = new ImageIcon(Toolkit.getDefaultToolkit().createImage(AutoHomeAdminSession.class.getResource("/images/YDate.gif")));
186         DAYOFF = new ImageIcon(Toolkit.getDefaultToolkit().createImage(AutoHomeAdminSession.class.getResource("/images/Minus.gif")));
187         INTERFACE_ON = new ImageIcon(Toolkit.getDefaultToolkit().createImage(AutoHomeAdminSession.class.getResource("/images/Home_on.gif")));
188         INTERFACE_OFF = new ImageIcon(Toolkit.getDefaultToolkit().createImage(AutoHomeAdminSession.class.getResource("/images/Home_off.gif")));
189         SUNRISE = new ImageIcon(Toolkit.getDefaultToolkit().createImage(AutoHomeAdminSession.class.getResource("/images/sunrise.jpg")));
190         SUNSET = new ImageIcon(Toolkit.getDefaultToolkit().createImage(AutoHomeAdminSession.class.getResource("/images/sunset.jpg")));
191         CLOCKICON = new ImageIcon(Toolkit.getDefaultToolkit().createImage(AutoHomeAdminSession.class.getResource("/images/Clock.gif")));
192         CAUTIONICON = new ImageIcon(Toolkit.getDefaultToolkit().createImage(AutoHomeAdminSession.class.getResource("/images/Caution.gif")));
193         DELETE_ROW_ICON = new ImageIcon(Toolkit.getDefaultToolkit().createImage(AutoHomeAdminSession.class.getResource("/images/DeleteRow.gif")));
194     }
195 
196     /***
197      * Loads the last settings from the file properties
198      * file loader. These settings are the file name,
199      * serial port, baud, bit, and X10 device type information.
200      */
201     public void loadLastSettings() {
202         if (PROPERTY_LOADER.getProperty(LAST_FILE) != null && !PROPERTY_LOADER.getProperty(LAST_FILE).equals(""))
203         {
204             loadX10File(PROPERTY_LOADER.getProperty(LAST_FILE));
205         }
206         if (super.getSerialPort().getPort() != null || !super.getSerialPort().getPort().equalsIgnoreCase("NO PORTS FOUND"))
207         {
208             if(super.connectToSerialPortOnStartup()){
209                 try
210                 {
211                     int deviceType = getX10GatewayType();
212                     if(!isX10GatwayConnected())
213                     {
214                         connectPortToDevice(deviceType);
215                     }
216                     else
217                     {
218                         printMessage("Serial port is already connected to:" + getSerialPort().getPort());
219                     }
220                 }
221                 catch (HomeException err)
222                 {
223                     printMessage("Failed to connect to serial");
224                 }
225             }
226         }
227     }
228 
229     /***
230      *Takes in a JComboBox and adds items to its list as
231      *an Item object.  It then returns this same component to
232      *the calling object with the newly filled list of
233      *items.
234      *
235      *@param comp - Swing component that needs a list of items added to it.
236      *
237      *@return JComboBox - Swing component with the new list of items.
238      */
239     public JComboBox setRenderedModules(JComboBox comp){
240         if(comp.getItemCount() > 0)
241         {
242             comp.removeAllItems();
243         }
244         try
245         {
246             Vector vX10Devices = this.loadAllX10Devices();
247             Enumeration enumr = vX10Devices.elements();
248             IX10Module x10;
249             while(enumr.hasMoreElements())
250             {
251                 x10=(IX10Module)enumr.nextElement();
252                 comp.addItem(new Item(new Object[] {new Integer(x10.getType()), (x10.getType() == LAMP_MODULE_ON) ? LIGHTICON:APPLIANCEICON, x10.getFullDeviceCode()}));
253             }
254         }
255         catch(Exception ex)
256         {
257             comp.addItem(new Item(new Object[] {new Integer(LAMP_MODULE_ON), LIGHTICON, "A1"}));
258         }
259         if(comp.getItemCount() < 1)
260             comp.addItem(new Item(new Object[] {new Integer(LAMP_MODULE_ON), LIGHTICON, "A1"}));
261         return comp;
262     }
263 
264     /***
265      *This method takes a combo box object and inserts
266      *simply names of macros into its drop down list.
267      *This calls the loadAllCM11AMacros() method which currently
268      *does a search for keys of Macroname.delay=
269      */
270     public JComboBox getAvailableMacroEvents(JComboBox comboBox){
271         if (comboBox.getItemCount() > 0)
272         {
273             comboBox.removeAllItems();
274         }
275         Vector vec = loadAllCM11AMacros();
276         if(vec != null)
277         {
278             int size = vec.size();
279             for(int i = 0; i < size; i++)
280             {
281                 comboBox.addItem(((Macro)vec.elementAt(i)).getMacroName());
282             }
283         }
284         return comboBox;
285     }
286 }