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  /*java necessary classes */
21  import java.awt.BorderLayout;
22  import java.awt.Toolkit;
23  import java.awt.Frame;
24  import java.awt.event.ActionEvent;
25  import java.awt.event.ActionListener;
26  import javax.swing.*;
27  import java.io.File;
28  import java.util.Date;
29  import java.util.ResourceBundle;
30  
31  /* my specific imports */
32  import org.wcb.common.*;
33  import org.wcb.common.event.PrintListener;
34  import org.wcb.common.component.JAliceMenuBar;
35  import org.wcb.autohome.util.OpenFileDialog;
36  import org.wcb.autohome.util.SaveFileDialog;
37  import org.wcb.autohome.exceptions.HomeException;
38  import org.wcb.autohome.interfaces.MessageInterface;
39  import org.wcb.autohome.interfaces.I18nConstants;
40  
41  /***
42   *  Project:   Home Automation Interface
43   *  Filename:  $Id: HomeCenterPanel.java,v 1.35 2004/07/22 03:06:49 wbogaardt Exp $
44   *  Abstract:  Central entry gui point for application.
45   *
46   * @author wbogaardt
47   * @version 1.2
48   */
49  public class HomeCenterPanel extends JComponent implements SwingConstants {
50  
51      private JButton jbOpen, jbSave, jbHelp, jbPrint, jbConfigure;
52      private ModulePanel modulePanel;
53      private EventsPanel eventsPanel;
54      private MonitorPanel monitorPanel;
55      private MacroPanel macroPanelWraper;
56      private SerialPanel serialPanel;
57      private JToolBar jtbToolbar = null;
58      private JButton jbTimeDisplay;
59      private MessageInterface messageInterface;
60      private static boolean lastFileLoaded = false;
61      private MiniBrowser miniBrowser = null;
62      private static final String SEP = System.getProperty("file.separator");
63      private UIDefaultsLoader props = null;
64      private ResourceBundle rbResource;
65      private Date currDate = null;
66      private static String APP_TITLE = "A.L.I.C.E.";
67  
68      private static String LAST_FILE = "last.file";
69  
70      /*** @link dependency */
71      /*#HomeCenterPanel lnkHomeCenterPanel;*/
72  
73      public HomeCenterPanel() {
74          super();
75          props = AutoHomeAdminSession.getInstance().getAppProperties();
76          setupComponents();
77      }
78  
79      public JMenuBar getJMenuBar()
80      {
81          return new JAliceMenuBar(this);
82      }
83  
84      public JToolBar getToolBar()
85      {
86          if (null == jtbToolbar)
87          {
88              constructToolBar();
89          }
90          return jtbToolbar;
91      }
92  
93      public HomeCenterPanel getCenterPanel() {
94          return this;
95      }
96  
97      public void setTitleBar() {
98          String fileName = AutoHomeAdminSession.CURRENT_FILENAME;
99          if (fileName != null)
100         {
101             GuiLib.getFrameForComponent(this.getCenterPanel()).setTitle(APP_TITLE + " " + fileName);
102             GuiLib.getFrameForComponent(this.getCenterPanel()).setIconImage(Toolkit.getDefaultToolkit().createImage(HomeCenterPanel.class.getResource("/images/Bulb.gif")));
103         }
104     }
105 
106     public void setupComponents() {
107         rbResource = AutoHomeAdminSession.getInstance().getI18n();
108         setLayout(new BorderLayout());
109 
110         JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
111         StatusBar statusBar = new StatusBar();
112         /* set the message interfaces so status bar gets them */
113         messageInterface = statusBar.getInterface();
114         AutoHomeAdminSession.getInstance().setMessageInterface(messageInterface);
115 
116         modulePanel = new ModulePanel();
117         eventsPanel = new EventsPanel();
118         monitorPanel = new MonitorPanel();
119         macroPanelWraper = new MacroPanel();
120         serialPanel = new SerialPanel();
121         // setup refresh interfaces to the module panel where new X10 devices are added.
122         modulePanel.setRefreshEventPanel(eventsPanel.getInterface());
123         modulePanel.setRefreshMacroPanel(macroPanelWraper.getInterface());
124         modulePanel.setRefreshMonitorPanel(monitorPanel.getInterface());
125 
126         tabbedPane.add(rbResource.getString(I18nConstants.DEVICE_PANEL), modulePanel);
127         tabbedPane.add(rbResource.getString(I18nConstants.EVENTS_PANEL), eventsPanel);
128         tabbedPane.add(rbResource.getString(I18nConstants.MONITOR_PANEL), monitorPanel);
129         tabbedPane.add(rbResource.getString(I18nConstants.MACRO_PANEL), macroPanelWraper);
130         tabbedPane.add(rbResource.getString(I18nConstants.SERIAL_PANEL), serialPanel);
131         add(BorderLayout.CENTER, tabbedPane);
132         add(BorderLayout.SOUTH, statusBar);
133     }
134 
135     private void constructToolBar() {
136         jtbToolbar = new JToolBar();
137         jbOpen = new JButton(rbResource.getString(I18nConstants.OPEN_BUTTON), new ImageIcon(Toolkit.getDefaultToolkit().createImage(HomeCenterPanel.class.getResource("/images/Open.gif"))));
138         jbSave = new JButton(rbResource.getString(I18nConstants.SAVE_BUTTON), new ImageIcon(Toolkit.getDefaultToolkit().createImage(HomeCenterPanel.class.getResource("/images/Save.gif"))));
139         jbPrint = new JButton(rbResource.getString(I18nConstants.PRINT_BUTTON), new ImageIcon(Toolkit.getDefaultToolkit().createImage(HomeCenterPanel.class.getResource("/images/Print.gif"))));
140         //jbPrint.setEnabled(false);
141         jbConfigure = new JButton(rbResource.getString(I18nConstants.CONFIGURE_BUTTON), new ImageIcon(Toolkit.getDefaultToolkit().createImage(HomeCenterPanel.class.getResource("/images/Hammer.gif"))));
142         jbHelp = new JButton(rbResource.getString(I18nConstants.HELP_BUTTON), new ImageIcon(Toolkit.getDefaultToolkit().createImage(HomeCenterPanel.class.getResource("/images/Help.gif"))));
143         jbTimeDisplay = new JButton(" ");
144         jtbToolbar.add(jbOpen);
145         jtbToolbar.add(jbSave);
146         jtbToolbar.add(jbPrint);
147         jtbToolbar.add(jbConfigure);
148         jtbToolbar.add(jbHelp);
149         jtbToolbar.addSeparator();
150         jtbToolbar.add(jbTimeDisplay);
151         jtbToolbar.putClientProperty("JToolBar.isRollover", Boolean.TRUE);
152         setupListeners();
153     }
154 
155     /***
156      * Various listeners for the tool bar menu items.
157      */
158     public void setupListeners() {
159         //jbClose = new JButton("Close");
160        // printDialog = new PrintPreviewDialog(null,jbClose);
161         ActionListener al = new ActionListener() {
162             public void actionPerformed(ActionEvent evt)
163             {
164                 Object src = evt.getSource();
165                 if (src == jbOpen)
166                 {
167                     openFile();
168                 }
169                 if (src == jbSave)
170                 {
171                     saveFile();
172                 }
173                 if (src == jbHelp)
174                 {
175                     if (miniBrowser == null)
176                     {
177                         miniBrowser = new MiniBrowser(null, APP_TITLE);
178                     }
179                     miniBrowser.displayURL(miniBrowser.setHomeURL("help/help.html"));
180                     miniBrowser.setVisible(true);
181                     miniBrowser.setVisible(true);
182                 }
183                 if (src == jbConfigure)
184                 {
185                     UIEditorDialog editor;
186                     Frame fr = GuiLib.getFrameForComponent(getCenterPanel());
187                     if ((fr != null) && (fr instanceof JFrame))
188                     {
189                         JComponent rootC = ((JFrame) fr).getRootPane();
190                         editor = new UIEditorDialog(rootC);
191                     }
192                     else
193                     {
194                         editor = new UIEditorDialog();
195                     }
196                     editor.setLocationRelativeTo(HomeCenterPanel.this);
197                     editor.setVisible(true);
198                 }
199 
200             }
201         };
202         /* displays new time and checks if port has been activated.*/
203         ActionListener timeListener = new ActionListener() {
204             public void actionPerformed(ActionEvent ae)
205             {
206                 currDate = new Date();
207                 jbTimeDisplay.setText(currDate.toString());
208                 messageInterface.interfaceConnected(AutoHomeAdminSession.getInstance().isX10GatwayConnected());
209             }
210         };
211         ActionListener time2Listener = new ActionListener() {
212             public void actionPerformed(ActionEvent ev)
213             {
214                 if (!lastFileLoaded)
215                 {
216                     messageInterface.printMessage("Opened last file ");
217                     lastFileLoaded = reloadData();
218                 }
219             }
220         };
221         /* Set up and fire off timeListener every second */
222         Timer oneSecondTimer = new Timer(1000, timeListener);
223         Timer fiveSecondTimer = new Timer(5000, time2Listener);
224         oneSecondTimer.start();
225         fiveSecondTimer.start();
226         jbOpen.addActionListener(al);
227         jbSave.addActionListener(al);
228         jbHelp.addActionListener(al);
229         jbConfigure.addActionListener(al);
230         jbPrint.addActionListener(new PrintListener());
231     }
232 
233     /***
234      * simple method to set this panel to visible
235      * status.
236      * @param answer enable displaying this panel
237      */
238     public void showMe(boolean answer)
239     {
240         setVisible(answer);
241     }
242 
243 
244     /***
245      * Opens a file dialog box and gets the user selected
246      * file and sets the name and loads the properties.
247      * It then sets the properties to the table models of each
248      * of the tabbed pane views.
249      */
250     public void openFile()
251     {
252         OpenFileDialog fd = new OpenFileDialog();
253         if (fd.isFileSelected())
254         {
255             File dataFile = new File(fd.getFilePath());
256             AutoHomeAdminSession.CURRENT_FILENAME = fd.getFilePath();
257             AutoHomeAdminSession.getInstance().loadX10File(fd.getFilePath());
258             props = AutoHomeAdminSession.getInstance().getAppProperties();
259             props.setProperty(LAST_FILE, fd.getFilePath());
260             messageInterface.printMessage("Opening " + dataFile.getName());
261             this.reloadData();
262             this.macroPanelWraper.refresh();
263             this.monitorPanel.refresh();
264             GuiLib.getFrameForComponent(getCenterPanel()).setTitle("A.L.I.C.E. - " + dataFile);
265         }
266     }
267 
268     /***
269      * Opens a new file and reloads everthing as a new application started.
270      *
271      */
272     public void openNewFile() {
273         AutoHomeAdminSession.getInstance().createNewFile(System.getProperty("user.home") + SEP + "default.x10");
274         this.reloadData();
275         GuiLib.getFrameForComponent(getCenterPanel()).setTitle("A.L.I.C.E. - " + System.getProperty("user.home") + SEP + "default.x10");
276     }
277 
278     /***
279      *Calls to reload data on a few panels if any
280      *of the data failes then we go and return false
281      *saying that the file failed to load correctly.
282      *
283      *@return boolean - True succes False failure
284      */
285     private boolean reloadData() {
286         try
287         {
288             Thread.sleep(1000);
289             modulePanel.loadData();
290         }
291         catch (Exception err)
292         {
293             return false;
294         }
295         try
296         {
297             Thread.sleep(1000);
298             eventsPanel.loadData();
299             monitorPanel.loadData();
300         }
301         catch (Exception err)
302         {
303             return false;
304         }
305         return true;
306     }
307 
308     /***
309      * Allows instantaneous saving of file from the last
310      * file opened.  If the file is not the same then it goes
311      * to the saveFileAs() method.
312      */
313     public void saveFile() {
314         String fileToSave = AutoHomeAdminSession.CURRENT_FILENAME;
315         if (fileToSave != null && fileToSave.length() > 3)
316         {
317             try
318             {
319                 AutoHomeAdminSession.getInstance().saveFile(fileToSave);
320                 serialPanel.saveProperties();
321                 AutoHomeAdminSession.getInstance().saveServerFile();
322                 props.saveProperties();
323                 messageInterface.printMessage("Saving file");
324             }
325             catch (HomeException err)
326             {
327                 messageInterface.printMessage("Error writing file:" + err);
328             }
329         }
330         else
331         {
332             saveFileAs();
333         }
334     }
335 
336     /***
337      * This allows the user to specify the file name they
338      * would like to save the file as via a File chooser dialog.
339      */
340     public void saveFileAs() {
341         SaveFileDialog fd = new SaveFileDialog();
342         if (fd.isFileSelected())
343         {
344             File dataFile = new File(fd.getFilePath());
345              try
346             {
347                 /* save files to serializer */
348                 AutoHomeAdminSession.getInstance().saveFile(dataFile.getAbsolutePath());
349                 messageInterface.printMessage("Saving file as " + dataFile.getName());
350                 /* Update the last file information */
351                 props.setProperty(LAST_FILE, dataFile.getAbsolutePath());
352                 props.saveProperties();
353             }
354             catch (HomeException he)
355             {
356                 messageInterface.printMessage("Error writing file:" + he);
357             }
358             AutoHomeAdminSession.CURRENT_FILENAME = dataFile.getAbsolutePath();
359             GuiLib.getFrameForComponent(getCenterPanel()).setTitle("A.L.I.C.E. - " + dataFile);
360         }
361     }
362 
363     /***
364      *Closes down the entire application by
365      *saving the last file information and closing out the
366      *serial port if it was initiated. If no file is available
367      *it will skip trying to save it.
368      */
369     public void shutdownSystem() {
370         AutoHomeAdminSession.getInstance().getAppProperties().saveProperties();
371         props.saveProperties();
372         AutoHomeAdminSession.getInstance().saveServerFile();
373         if (AutoHomeAdminSession.getInstance().isX10GatwayConnected())
374         {
375             if (AutoHomeAdminSession.getInstance().isX10GatwayConnected())
376             {
377                 try
378                 {
379                     AutoHomeAdminSession.getInstance().closeSerialPort();
380                 }
381                 catch (HomeException err)
382                 {
383                     AutoHomeAdminSession.getInstance().printMessage("Failed to close serial port.");
384                     System.exit(0);
385                 }
386             }
387         }
388         System.exit(0);
389     }
390 }