View Javadoc

1   package org.wcb.common.component;
2   
3   import org.wcb.autohome.interfaces.I18nConstants;
4   import org.wcb.autohome.AutoHomeAdminSession;
5   import org.wcb.autohome.HomeCenterPanel;
6   import org.wcb.autohome.exceptions.HomeException;
7   import org.wcb.common.GuiLib;
8   import org.wcb.common.UIEditorDialog;
9   import org.wcb.common.MiniBrowser;
10  import org.wcb.common.event.PrintListener;
11  
12  import javax.swing.*;
13  import java.awt.event.ActionListener;
14  import java.awt.event.ActionEvent;
15  import java.awt.event.KeyEvent;
16  import java.awt.*;
17  import java.util.ResourceBundle;
18  import java.io.IOException;
19  import java.net.URL;
20  import java.net.MalformedURLException;
21  
22  /***
23   * Copyright (C) 1999  Walter Bogaardt
24   *
25   * This library is free software; you can redistribute it and/or
26   * modify it under the terms of the GNU Lesser General Public
27   * License as published by the Free Software Foundation; either
28   * version 2 of the License, or (at your option) any later version.
29   *
30   * This library is distributed in the hope that it will be useful,
31   * but WITHOUT ANY WARRANTY; without even the implied warranty of
32   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
33   * Lesser General Public License for more details.
34   *
35   * You should have received a copy of the GNU Lesser General Public
36   * License along with this library; if not, write to the Free Software
37   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
38   *
39   * Project: Alice X10 Home Automation
40   *
41   *  $Log: JAliceMenuBar.java,v $
42   *  Revision 1.3  2004/07/22 03:06:50  wbogaardt
43   *  removed deprecated method calls.
44   *
45   *  Revision 1.2  2004/02/23 20:08:30  wbogaardt
46   *  added virtual key ability to menu bar and added print menu option to menu bar
47   *
48   *  Revision 1.1  2004/02/06 23:25:32  wbogaardt
49   *  refactored menu bar out of HomeCenterPanel class
50   *
51   *
52   */
53  public class JAliceMenuBar extends JMenuBar implements ActionListener {
54  
55      private JMenu jmFile, jmOption, jmHelp, jmSearch;
56      private JMenuItem jmiFileOpen, jmiSave, jmiSaveAs, jmiFileNew, jmiClose;
57      private JMenuItem jmiPrint;
58      private JMenuItem jmiUIEditor;
59      private JMenuItem jmiAbout, jmiHelp;
60      private JMenuItem jmiAllOff, jmiAllLightsOn, jmiAllLightsOff;
61      private JMenuItem jmiHomeDirector, jmiX10, jmiSmartHome;
62      private JMenuItem jmiClearAllMacros;
63      private JMenuItem jmiClearTimedMacros;
64      private JMenuItem jmiClearEventsMacro;
65      private JMenuItem jmiMonitorCM11;
66      private JMenuItem jmiNoMonitorCM11;
67      private JMenuItem jmiACode = new JMenuItem("A");
68      private JMenuItem jmiBCode = new JMenuItem("B");
69      private JMenuItem jmiCCode = new JMenuItem("C");
70      private JMenuItem jmiDCode = new JMenuItem("D");
71      private JMenuItem jmiECode = new JMenuItem("E");
72      private JMenuItem jmiFCode = new JMenuItem("F");
73      private JMenuItem jmiGCode = new JMenuItem("G");
74      private JMenuItem jmiHCode = new JMenuItem("H");
75      private JMenuItem jmiICode = new JMenuItem("I");
76      private JMenuItem jmiJCode = new JMenuItem("J");
77      private JMenuItem jmiKCode = new JMenuItem("K");
78      private JMenuItem jmiLCode = new JMenuItem("L");
79      private JMenuItem jmiMCode = new JMenuItem("M");
80      private JMenuItem jmiNCode = new JMenuItem("N");
81      private JMenuItem jmiOCode = new JMenuItem("O");
82      private JMenuItem jmiPCode = new JMenuItem("P");
83      private static char HOUSE = 'A';
84      private MiniBrowser miniBrowser = null;
85      private static String X10_HOME = "http://www.x10.com";
86      private static String IBM_HOME = "http://jhome.sourceforge.net";
87      private static String SMART_HOME = "http://www.smarthome.com";
88      private static String APP_TITLE = "A.L.I.C.E.";
89      private HomeCenterPanel hcpReference;
90  
91      /***
92       * This class takes the reference to the HomeCenter panel so
93       * that actions such as file saving can take place.
94       * @param homePanel
95       */
96      public JAliceMenuBar(HomeCenterPanel homePanel){
97          this.hcpReference = homePanel;
98          this.constructMenuBar();
99      }
100 
101     private void constructMenuBar() {
102         ResourceBundle rbResource = AutoHomeAdminSession.getInstance().getI18n();
103         jmFile = new JMenu(rbResource.getString(I18nConstants.FILE_MENU));
104         jmiFileNew = new JMenuItem(rbResource.getString(I18nConstants.NEW_MENU));
105         jmiFileOpen = new JMenuItem(rbResource.getString(I18nConstants.OPEN_MENU));
106         jmiSave = new JMenuItem(rbResource.getString(I18nConstants.SAVE_MENU));
107         jmiSaveAs = new JMenuItem(rbResource.getString(I18nConstants.SAVEAS_MENU));
108         jmiPrint = new JMenuItem(rbResource.getString(I18nConstants.PRINT_BUTTON));
109         jmiClose = new JMenuItem(rbResource.getString(I18nConstants.EXIT_MENU));
110         jmFile.add(jmiFileNew);
111         jmFile.add(jmiFileOpen);
112         jmFile.addSeparator();
113         jmFile.add(jmiSave);
114         jmFile.add(jmiSaveAs);
115         jmFile.add(jmiPrint);
116         jmFile.addSeparator();
117         jmFile.add(jmiClose);
118 
119         jmOption = new JMenu(rbResource.getString(I18nConstants.OPTION_MENU));
120         JMenu jmCM11A = new JMenu(rbResource.getString(I18nConstants.CM11A_MENU));
121         jmiUIEditor = new JMenuItem(rbResource.getString(I18nConstants.GLOBAL_MENU));
122         jmiAllLightsOff = new JMenuItem(rbResource.getString(I18nConstants.ALL_LIGHTS_OFF_MENU));
123         jmiAllOff = new JMenuItem(rbResource.getString(I18nConstants.ALL_OFF_MENU));
124         jmiAllLightsOn = new JMenuItem(rbResource.getString(I18nConstants.ALL_LIGHTS_ON_MENU));
125         jmiClearTimedMacros = new JMenuItem(rbResource.getString(I18nConstants.CLEAR_TIMER_MENU));
126         jmiClearEventsMacro = new JMenuItem(rbResource.getString(I18nConstants.CLEAR_EVENT_MENU));
127         jmiClearAllMacros=new JMenuItem(rbResource.getString(I18nConstants.CLEAR_ALL_MENU));
128         jmCM11A.add(jmiAllLightsOn);
129         jmCM11A.add(jmiAllLightsOff);
130         jmCM11A.add(jmiAllOff);
131         jmCM11A.addSeparator();
132         jmCM11A.add(jmiClearTimedMacros);
133         jmCM11A.add(jmiClearEventsMacro);
134         jmCM11A.add(jmiClearAllMacros);
135 
136         JMenu jmMonitor = new JMenu("Monitor");
137         jmiMonitorCM11 = new JMenuItem(rbResource.getString(I18nConstants.MONITOR_MENU));
138         jmiNoMonitorCM11 = new JMenuItem(rbResource.getString(I18nConstants.NO_MONITOR_MENU));
139         jmMonitor.add(jmiMonitorCM11);
140         jmMonitor.add(jmiNoMonitorCM11);
141 
142         JMenu jmHouseCode = new JMenu("House Codes");
143         jmHouseCode.add(jmiACode);
144         jmHouseCode.add(jmiBCode);
145         jmHouseCode.add(jmiCCode);
146         jmHouseCode.add(jmiDCode);
147         jmHouseCode.add(jmiECode);
148         jmHouseCode.add(jmiFCode);
149         jmHouseCode.add(jmiGCode);
150         jmHouseCode.add(jmiHCode);
151         jmHouseCode.add(jmiICode);
152         jmHouseCode.add(jmiJCode);
153         jmHouseCode.add(jmiKCode);
154         jmHouseCode.add(jmiLCode);
155         jmHouseCode.add(jmiMCode);
156         jmHouseCode.add(jmiNCode);
157         jmHouseCode.add(jmiOCode);
158         jmHouseCode.add(jmiPCode);
159 
160         jmOption.add(jmiUIEditor);
161         jmOption.add(jmCM11A);
162         jmOption.add(jmHouseCode);
163         jmOption.add(jmMonitor);
164 
165         jmSearch = new JMenu(rbResource.getString(I18nConstants.SEARCH_MENU));
166         jmiX10 = new JMenuItem("X10 corp");
167         jmiSmartHome = new JMenuItem("Smart Home");
168         jmiHomeDirector = new JMenuItem("JHome");
169         jmSearch.add(jmiX10);
170         jmSearch.add(jmiSmartHome);
171         jmSearch.add(jmiHomeDirector);
172 
173         jmHelp = new JMenu(rbResource.getString(I18nConstants.HELP_MENU));
174         jmiAbout = new JMenuItem(rbResource.getString(I18nConstants.ABOUT_MENU));
175         jmiHelp = new JMenuItem(rbResource.getString(I18nConstants.CONTENT_MENU));
176         jmHelp.add(jmiAbout);
177         jmHelp.add(jmiHelp);
178 
179         add(jmFile);
180         add(jmOption);
181         add(jmSearch);
182         add(jmHelp);
183         this.setupMenuListeners();
184     }
185 
186     /***
187      * The action events being listed to for the menu items.
188      * @param evt event detected
189      */
190     public void actionPerformed(ActionEvent evt)
191     {
192         Object src = evt.getSource();
193         if (src == jmiFileOpen)
194         {
195             hcpReference.openFile();
196         }
197         if(src == jmiFileNew)
198         {
199             hcpReference.openNewFile();
200         }
201         if (src == jmiSave)
202         {
203             hcpReference.saveFile();
204         }
205         if (src == jmiSaveAs)
206         {
207             hcpReference.saveFileAs();
208         }
209         if (src == jmiClose)
210         {
211             hcpReference.shutdownSystem();
212         }
213         if(src == jmiAllLightsOn)
214         {
215             sendCommandToHouse(1);
216         }
217         if(src == jmiAllLightsOff)
218         {
219             sendCommandToHouse(0);
220         }
221         if (src == jmiAllOff)
222         {
223             sendCommandToHouse(2);
224         }
225         if(src == jmiClearTimedMacros)
226         {
227             try
228             {
229                 AutoHomeAdminSession.getInstance().clearTimerMacrosFromCM11A();
230             }
231             catch(HomeException err)
232             {
233                 showErrorMessage(err.toString());
234             }
235         }
236         if(src == jmiClearEventsMacro)
237         {
238             try
239             {
240                 AutoHomeAdminSession.getInstance().clearMacroEventsFromCM11A();
241             }
242             catch(HomeException err)
243             {
244                 showErrorMessage(err.toString());
245             }
246         }
247         if(src == jmiClearAllMacros)
248         {
249             try
250             {
251                 AutoHomeAdminSession.getInstance().clearMacroEventsFromCM11A();
252                 AutoHomeAdminSession.getInstance().clearTimerMacrosFromCM11A();
253             }
254             catch(HomeException err)
255             {
256                 showErrorMessage(err.toString());
257             }
258         }
259         if(src == jmiMonitorCM11)
260         {
261             if(AutoHomeAdminSession.getInstance().monitorCM11A())
262             {
263                 jmiMonitorCM11.setEnabled(false);
264                 jmiNoMonitorCM11.setEnabled(true);
265             } else{
266                 showErrorMessage("Monitoring device not connected to Serial port.");
267             }
268 
269         }
270         if(src == jmiNoMonitorCM11)
271         {
272             if(AutoHomeAdminSession.getInstance().disableMonitorCM11A())
273             {
274                 jmiMonitorCM11.setEnabled(true);
275                 jmiNoMonitorCM11.setEnabled(false);
276             } else{
277                 showErrorMessage("Monitoring device not connected to Serial port.");
278             }
279         }
280         if (src == jmiX10)
281         {
282             this.showWebPage(X10_HOME);
283         }
284         if (src == jmiSmartHome)
285         {
286             this.showWebPage(SMART_HOME);
287         }
288         if (src == jmiHomeDirector)
289         {
290             this.showWebPage(IBM_HOME);
291         }
292         if (src == jmiUIEditor)
293         {
294             this.showEditor();
295         }
296         if (src == jmiAbout)
297         {
298             this.showMiniBrowser("help/about.html");
299         }
300         if (src == jmiHelp)
301         {
302             this.showMiniBrowser("help/help.html");
303         }
304         if(src == jmiACode){
305             HOUSE = jmiACode.getText().charAt(0);
306             enableAllHouseCodes();
307             jmiACode.setEnabled(false);
308         }
309         if(src == jmiBCode){
310             HOUSE = jmiBCode.getText().charAt(0);
311             enableAllHouseCodes();
312             jmiBCode.setEnabled(false);
313         }
314         if(src == jmiCCode){
315             HOUSE = jmiCCode.getText().charAt(0);
316             enableAllHouseCodes();
317             jmiCCode.setEnabled(false);
318         }
319         if(src == jmiDCode){
320             HOUSE = jmiDCode.getText().charAt(0);
321             enableAllHouseCodes();
322             jmiDCode.setEnabled(false);
323         }
324         if(src == jmiECode){
325             HOUSE = jmiECode.getText().charAt(0);
326             enableAllHouseCodes();
327             jmiECode.setEnabled(false);
328         }
329         if(src == jmiFCode){
330             HOUSE = jmiFCode.getText().charAt(0);
331             enableAllHouseCodes();
332             jmiFCode.setEnabled(false);
333         }
334         if(src == jmiGCode){
335             HOUSE = jmiGCode.getText().charAt(0);
336             enableAllHouseCodes();
337             jmiGCode.setEnabled(false);
338         }
339         if(src == jmiHCode){
340             HOUSE = jmiHCode.getText().charAt(0);
341             enableAllHouseCodes();
342             jmiHCode.setEnabled(false);
343         }
344         if(src == jmiICode){
345             HOUSE = jmiICode.getText().charAt(0);
346             enableAllHouseCodes();
347             jmiICode.setEnabled(false);
348         }
349         if(src == jmiJCode){
350             HOUSE = jmiJCode.getText().charAt(0);
351             enableAllHouseCodes();
352             jmiJCode.setEnabled(false);
353         }
354         if(src == jmiKCode){
355             HOUSE = jmiKCode.getText().charAt(0);
356             enableAllHouseCodes();
357             jmiKCode.setEnabled(false);
358         }
359         if(src == jmiLCode){
360             HOUSE = jmiLCode.getText().charAt(0);
361             enableAllHouseCodes();
362             jmiLCode.setEnabled(false);
363         }
364         if(src == jmiMCode){
365             HOUSE = jmiMCode.getText().charAt(0);
366             enableAllHouseCodes();
367             jmiMCode.setEnabled(false);
368         }
369         if(src == jmiNCode){
370             HOUSE = jmiNCode.getText().charAt(0);
371             enableAllHouseCodes();
372             jmiNCode.setEnabled(false);
373         }
374         if(src == jmiOCode){
375             HOUSE = jmiOCode.getText().charAt(0);
376             enableAllHouseCodes();
377             jmiOCode.setEnabled(false);
378         }
379         if(src == jmiPCode){
380             HOUSE = jmiPCode.getText().charAt(0);
381             enableAllHouseCodes();
382             jmiPCode.setEnabled(false);
383         }
384     }
385     /***
386      * Setup for the various menu items.
387      */
388     public void setupMenuListeners() {
389         jmiFileOpen.addActionListener(this);
390         jmiFileOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, Event.CTRL_MASK));
391         jmiFileNew.addActionListener(this);
392         jmiFileNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, Event.CTRL_MASK));
393         jmiSave.addActionListener(this);
394         jmiSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, Event.CTRL_MASK));
395         jmiSaveAs.addActionListener(this);
396         jmiPrint.addActionListener(new PrintListener());
397         jmiPrint.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, Event.CTRL_MASK));
398         jmiClose.addActionListener(this);
399         jmiClose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, Event.CTRL_MASK));
400         jmiUIEditor.addActionListener(this);
401         jmiAllLightsOn.addActionListener(this);
402         jmiAllLightsOff.addActionListener(this);
403         jmiAllOff.addActionListener(this);
404         jmiClearTimedMacros.addActionListener(this);
405         jmiClearEventsMacro.addActionListener(this);
406         jmiClearAllMacros.addActionListener(this);
407         jmiX10.addActionListener(this);
408         jmiSmartHome.addActionListener(this);
409         jmiHomeDirector.addActionListener(this);
410         jmiAbout.addActionListener(this);
411         jmiHelp.addActionListener(this);
412         jmiMonitorCM11.addActionListener(this);
413         jmiNoMonitorCM11.addActionListener(this);
414         jmiACode.addActionListener(this);
415         jmiBCode.addActionListener(this);
416         jmiCCode.addActionListener(this);
417         jmiDCode.addActionListener(this);
418         jmiECode.addActionListener(this);
419         jmiFCode.addActionListener(this);
420         jmiGCode.addActionListener(this);
421         jmiHCode.addActionListener(this);
422         jmiICode.addActionListener(this);
423         jmiJCode.addActionListener(this);
424         jmiKCode.addActionListener(this);
425         jmiLCode.addActionListener(this);
426         jmiMCode.addActionListener(this);
427         jmiNCode.addActionListener(this);
428         jmiOCode.addActionListener(this);
429         jmiPCode.addActionListener(this);
430     }
431 
432     /***
433      * This switches between showing a web page if it failes because the user
434      * never set up their browser it will default to the mini swing browser.
435      * @param page
436      */
437      private void showWebPage(String page){
438         String cmd[] = new String[2];
439         cmd[0] = (AutoHomeAdminSession.getInstance().getAppProperties()).getProperty("browser");
440         if (page != null)
441         {
442             cmd[1] = page;
443         }
444         else
445         {
446             cmd[1] = "http://jhome.sourceforge.net";
447         }
448         boolean writeFile = false;
449         /* use the user defined web browser to display the web site */
450         if (cmd[0] != null)
451         {
452             try
453             {
454                 Runtime r = Runtime.getRuntime();
455                 r.exec(cmd);
456                 writeFile = true;
457             }
458             catch (IOException err)
459             {
460                 Toolkit.getDefaultToolkit().beep();
461                 AutoHomeAdminSession.getInstance().printMessage("Problem running Browser -" + err.toString());
462                 showErrorMessage("Invalid browser or path information selected!");
463                 writeFile = false;
464             }
465         }
466         /* no user defined web browser use a quick and dirty java based browser */
467         if (cmd[0] == null || !writeFile)
468         {
469             try
470             {
471                 URL links = new URL(cmd[1]);
472                 if(miniBrowser == null)
473                 {
474                     miniBrowser = new MiniBrowser(null, APP_TITLE);
475                 }
476                 miniBrowser.displayURL(links);
477                 miniBrowser.setVisible(true);
478                 miniBrowser.setVisible(true);
479             }
480             catch (MalformedURLException malUrl)
481             {
482                 Toolkit.getDefaultToolkit().beep();
483                 Frame fr = GuiLib.getFrameForComponent(hcpReference.getCenterPanel());
484                 if ((fr != null) && (fr instanceof JFrame))
485                 {
486                     JComponent rootC = ((JFrame)fr).getRootPane();
487                     UIEditorDialog editor = new UIEditorDialog(rootC);
488                     editor.setLocationRelativeTo(JAliceMenuBar.this);
489                     editor.setVisible(true);
490                 }
491             }
492         }
493     }
494 
495     /***
496      * Show the Global configuration editor dialog box.
497      */
498     private void showEditor(){
499         UIEditorDialog editor;
500         Frame fr = GuiLib.getFrameForComponent(hcpReference.getCenterPanel());
501         if ((fr != null) && (fr instanceof JFrame))
502         {
503             JComponent rootC = ((JFrame)fr).getRootPane();
504             editor = new UIEditorDialog(rootC);
505             editor.setTitle("Alice Global Settings");
506         }
507         else
508         {
509             editor = new UIEditorDialog();
510         }
511         editor.setLocationRelativeTo(JAliceMenuBar.this);
512         editor.setVisible(true);
513     }
514 
515     /***
516      * This sends the command to the house based on all actions.
517      * 0 is for all lights off, 1 is for all lights on, 2 is for all devices off.
518      * @param iCommand Command to send to CM11A.
519      */
520     private void sendCommandToHouse(int iCommand){
521         try
522         {
523             AutoHomeAdminSession.getInstance().sendCommandToSection(HOUSE, iCommand);
524         }
525         catch(HomeException err)
526         {
527             showErrorMessage("Sending House command failed:\n" + err.getMessage());
528         }
529 
530     }
531 
532     /***
533      * Show the mini alice browser with the alice specific url information.
534      * @param urlInfo
535      */
536     private void showMiniBrowser(String urlInfo){
537          if (miniBrowser == null)
538             {
539                 miniBrowser = new MiniBrowser(null,APP_TITLE);
540             }
541             miniBrowser.displayURL(miniBrowser.setHomeURL(urlInfo));
542             miniBrowser.setVisible(true);
543     }
544 
545     /***
546      * Enables all the menu items for the house codes
547      * because one of them will be disabled.
548      */
549     private void enableAllHouseCodes(){
550         jmiACode.setEnabled(true);
551         jmiBCode.setEnabled(true);
552         jmiCCode.setEnabled(true);
553         jmiDCode.setEnabled(true);
554         jmiECode.setEnabled(true);
555         jmiFCode.setEnabled(true);
556         jmiGCode.setEnabled(true);
557         jmiHCode.setEnabled(true);
558         jmiICode.setEnabled(true);
559         jmiJCode.setEnabled(true);
560         jmiKCode.setEnabled(true);
561         jmiLCode.setEnabled(true);
562         jmiMCode.setEnabled(true);
563         jmiNCode.setEnabled(true);
564         jmiOCode.setEnabled(true);
565         jmiPCode.setEnabled(true);
566     }
567 
568     private void showErrorMessage(String errorString) {
569         JOptionPane.showMessageDialog(null, errorString, "Warning",
570                 JOptionPane.ERROR_MESSAGE);
571     }
572 }