1   /*
2    * Copyright 2008 motej
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License. 
15   */
16  package motej.demos.balanceboard;
17  
18  import java.awt.GridLayout;
19  
20  import javax.swing.JCheckBox;
21  import javax.swing.JLabel;
22  import javax.swing.JPanel;
23  import javax.swing.JProgressBar;
24  import javax.swing.SwingUtilities;
25  
26  import motej.Mote;
27  import motej.StatusInformationReport;
28  import motej.event.StatusInformationListener;
29  
30  /**
31   * 
32   * <p>
33   * @author <a href="mailto:vfritzsch@users.sourceforge.net">Volker Fritzsch</a>
34   */
35  public class StatusInformationReportPanel implements StatusInformationListener {
36  
37  	private JLabel batteryLevelLabel = new JLabel("Battery Level");
38  
39  	private JLabel led0Label = new JLabel("LED 0");
40  
41  	private JLabel led1Label = new JLabel("LED 1");
42  
43  	private JLabel led2Label = new JLabel("LED 2");
44  
45  	private JLabel led3Label = new JLabel("LED 3");
46  
47  	private JLabel continuousReportsLabel = new JLabel("Continuous Reports");
48  
49  	private JLabel extensionLabel = new JLabel("Extension Connected");
50  
51  	private JLabel speakerLabel = new JLabel("Speaker Enabled");
52  
53  	private JCheckBox speakerCheckBox = new JCheckBox();
54  
55  	private JCheckBox extensionCheckBox = new JCheckBox();
56  
57  	private JCheckBox continuousReportsCheckBox = new JCheckBox();
58  
59  	private JCheckBox led0CheckBox = new JCheckBox();
60  
61  	private JCheckBox led1CheckBox = new JCheckBox();
62  
63  	private JCheckBox led2CheckBox = new JCheckBox();
64  
65  	private JCheckBox led3CheckBox = new JCheckBox();
66  
67  	private JProgressBar batteryLevelProgressBar = new JProgressBar(0, 200);
68  
69  	private JPanel panel;
70  
71  	public StatusInformationReportPanel(Mote mote) {
72  		led0CheckBox.setEnabled(false);
73  		led1CheckBox.setEnabled(false);
74  		led2CheckBox.setEnabled(false);
75  		led3CheckBox.setEnabled(false);
76  		continuousReportsCheckBox.setEnabled(false);
77  		extensionCheckBox.setEnabled(false);
78  		speakerCheckBox.setEnabled(false);
79  		panel = new JPanel();
80  		panel.setLayout(new GridLayout(8, 2));
81  		panel.add(batteryLevelLabel);
82  		panel.add(batteryLevelProgressBar);
83  		panel.add(led0Label);
84  		panel.add(led0CheckBox);
85  		panel.add(led1Label);
86  		panel.add(led1CheckBox);
87  		panel.add(led2Label);
88  		panel.add(led2CheckBox);
89  		panel.add(led3Label);
90  		panel.add(led3CheckBox);
91  		panel.add(continuousReportsLabel);
92  		panel.add(continuousReportsCheckBox);
93  		panel.add(extensionLabel);
94  		panel.add(extensionCheckBox);
95  		panel.add(speakerLabel);
96  		panel.add(speakerCheckBox);
97  
98  		statusInformationReceived(mote.getStatusInformationReport());
99  		mote.addStatusInformationListener(this);
100 	}
101 
102 	public JPanel getPanel() {
103 		return panel;
104 	}
105 
106 	public void statusInformationReceived(final StatusInformationReport report) {
107 		if (report != null) {
108 			SwingUtilities.invokeLater(new Runnable() {
109 
110 				public void run() {
111 					led0CheckBox.setSelected(report.getLedEnabled()[0]);
112 					led1CheckBox.setSelected(report.getLedEnabled()[1]);
113 					led2CheckBox.setSelected(report.getLedEnabled()[2]);
114 					led3CheckBox.setSelected(report.getLedEnabled()[3]);
115 					continuousReportsCheckBox.setSelected(report
116 							.isContinuousReportingEnabled());
117 					extensionCheckBox.setSelected(report
118 							.isExtensionControllerConnected());
119 					speakerCheckBox.setSelected(report.isSpeakerEnabled());
120 					batteryLevelProgressBar.setValue(report.getBatteryLevel() & 0xff);
121 				}
122 			});
123 		}
124 	}
125 
126 }