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   *
21   * $Log: LogPanel.java,v $
22   * Revision 1.4  2004/02/25 21:38:32  wbogaardt
23   * added javadocs and fixed formating for checkstyle report
24   *
25   * Revision 1.3  2003/12/22 20:51:29  wbogaardt
26   * refactored name assignments and formatted code for readability.
27   *
28   * Revision 1.2  2003/10/09 23:55:21  wbogaardt
29   * code clean up of formating and adding revision history to comments
30   *
31   */
32  import javax.swing.JPanel;
33  import javax.swing.JButton;
34  import javax.swing.JScrollPane;
35  import javax.swing.JTextArea;
36  import javax.swing.BoxLayout;
37  import javax.swing.Box;
38  import javax.swing.JFileChooser;
39  import javax.swing.BorderFactory;
40  
41  /* standard java stuff */
42  import java.awt.Dimension;
43  import java.awt.event.ActionListener;
44  import java.awt.event.ActionEvent;
45  import java.awt.BorderLayout;
46  import java.io.File;
47  
48  /* special classes needed */
49  import org.wcb.common.EventLogger;
50  import org.wcb.autohome.util.LogFilter;
51  import org.wcb.autohome.interfaces.MessageInterface;
52  
53  /***
54   * This is the display panel for the logging console at the bottom of
55   * the swing application.
56   */
57  public class LogPanel extends JPanel {
58  
59      private JButton jbOpenLog;
60      private JTextArea jtaLogDisplay;
61      private JFileChooser jfcFileChooser = null;
62      private MessageInterface msgInterface;
63  
64      /***
65       * This displays a panel at the bottom of the application
66       * to print messages to the user.
67       * @param msg Message Interface
68       */
69      public LogPanel(MessageInterface msg)
70      {
71          msgInterface = msg;
72          setupComponents();
73          setupListeners();
74      }
75  
76      /***
77       * Set up the components.
78       */
79      private void setupComponents()
80      {
81          setLayout(new BorderLayout());
82          JPanel centerPane = new JPanel();
83          centerPane.setLayout(new BoxLayout(centerPane, BoxLayout.Y_AXIS));
84          jbOpenLog = new JButton("OpenLog");
85          jtaLogDisplay = new JTextArea(5, 80);
86          JScrollPane textLog = new JScrollPane(jtaLogDisplay);
87          centerPane.add(textLog);
88          centerPane.add(Box.createRigidArea(new Dimension(0, 5)));
89          centerPane.add(jbOpenLog);
90          centerPane.setBorder(BorderFactory.createTitledBorder("X10 Log View"));
91          add(centerPane, BorderLayout.CENTER);
92      }
93  
94      /***
95       * Sets up the action listeners.
96       */
97      private void setupListeners()
98      {
99          ActionListener al = new ActionListener() {
100             public void actionPerformed(ActionEvent evt) {
101                 Object src = evt.getSource();
102                 if (src == jbOpenLog)
103                 {
104                     openLog();
105                 }
106             }
107         };
108         jbOpenLog.addActionListener(al);
109     }
110 
111     /***
112      * Opens the X10 logs file for user to view
113      * what has occured.
114      */
115     public void openLog()
116     {
117         if (jfcFileChooser == null)
118         {
119             jfcFileChooser = new JFileChooser();
120         }
121         jfcFileChooser.setFileFilter(new LogFilter());
122         int fileState = jfcFileChooser.showOpenDialog(null);
123         File file = jfcFileChooser.getSelectedFile();
124         if (file != null && fileState == JFileChooser.APPROVE_OPTION)
125         {
126             log("Loading x10 log file:" + file.getAbsolutePath());
127             EventLogger eventLog = new EventLogger();
128             jtaLogDisplay.setText(eventLog.readLog(file.getAbsolutePath()).toString());
129         }
130     }
131 
132     /***
133      * sends text information to the application statatus bar
134      *@param info the text message to print to the console.
135      */
136     private void log (String info)
137     {
138         msgInterface.printMessage(info);
139     }    
140 }