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.BorderLayout;
19  import java.awt.FlowLayout;
20  import java.awt.GridLayout;
21  import java.awt.event.ActionEvent;
22  
23  import javax.swing.AbstractAction;
24  import javax.swing.Action;
25  import javax.swing.JButton;
26  import javax.swing.JFrame;
27  import javax.swing.JPanel;
28  import javax.swing.SwingUtilities;
29  
30  import motej.Mote;
31  import motej.MoteFinder;
32  import motej.MoteFinderListener;
33  import motej.event.CoreButtonEvent;
34  import motej.event.CoreButtonListener;
35  import motej.request.ReportModeRequest;
36  import motejx.extensions.balanceboard.BalanceBoard;
37  
38  /**
39   * 
40   * <p>
41   * @author <a href="mailto:vfritzsch@users.sourceforge.net">Volker Fritzsch</a>
42   */
43  public class BalanceBoardGUI {
44  
45  	private JFrame frame;
46  	
47  	private Mote mote;
48  	
49  	private MoteFinderListener moteFinderListener = new MoteFinderListener() {
50  	
51  		public void moteFound(final Mote m) {
52  			System.out.println("Found mote!");
53  			mote = m;
54  			mote.setPlayerLeds(new boolean[] { true, false, false, false });
55  			mote.addCoreButtonListener(new CoreButtonListener() {
56  			
57  				public void buttonPressed(CoreButtonEvent evt) {
58  					System.out.println("Button pressed: " + evt.getButton());
59  				}
60  			});
61  		}
62  	};
63  	
64  	private Action findBalanceBoardAction = new AbstractAction("Find Balance Board") {
65  	
66  		public void actionPerformed(ActionEvent e) {
67  			MoteFinder finder = MoteFinder.getMoteFinder();
68  			finder.addMoteFinderListener(moteFinderListener);
69  			finder.startDiscovery();
70  		}
71  	};
72  	
73  	private Action statusInformationAction = new AbstractAction("Status Information") {
74  		
75  		public void actionPerformed(ActionEvent e) {
76  			final StatusInformationReportPanel statusInformationReportPanel = new StatusInformationReportPanel(mote);
77  			SwingUtilities.invokeLater(new Runnable() {
78  			
79  				public void run() {
80  					frame.getContentPane().add(statusInformationReportPanel.getPanel(), BorderLayout.WEST);	
81  					frame.pack();
82  				}
83  			});
84  		}
85  	};
86  	
87  	private Action moteCalibrationAction = new AbstractAction("Mote Calibration") {
88  		
89  		public void actionPerformed(ActionEvent e) {
90  			final CalibrationDataReportPanel p = new CalibrationDataReportPanel(mote.getCalibrationDataReport());
91  			SwingUtilities.invokeLater(new Runnable() {
92  			
93  				public void run() {
94  					frame.getContentPane().add(p.getPanel(), BorderLayout.EAST);
95  					frame.pack();
96  				}
97  			});
98  		}
99  	};
100 	
101 	private Action bbCalibrationAction = new AbstractAction("Balance Board Calibration") {
102 		
103 		public void actionPerformed(ActionEvent e) {
104 			final BalanceBoardCalibrationDataPanel p = new BalanceBoardCalibrationDataPanel(mote.<BalanceBoard>getExtension().getCalibrationData());
105 			SwingUtilities.invokeLater(new Runnable() {
106 			
107 				public void run() {
108 					frame.getContentPane().add(p.getPanel(), BorderLayout.SOUTH);
109 					frame.pack();
110 				}
111 			});
112 		}
113 	};
114 	
115 	private Action bbListenerAction = new AbstractAction("Balance Board Listener") {
116 		
117 		public void actionPerformed(ActionEvent e) {
118 			final BalanceBoardListenerPanel p = new BalanceBoardListenerPanel(mote.<BalanceBoard>getExtension());
119 			SwingUtilities.invokeLater(new Runnable() {
120 			
121 				public void run() {
122 					frame.getContentPane().add(p.getPanel(), BorderLayout.CENTER);
123 					frame.pack();
124 				}
125 			});
126 		}
127 	};
128 	
129 	protected Action report0x32Action = new AbstractAction("Report: 0x32") {
130 		
131 		public void actionPerformed(ActionEvent arg0) {
132 			mote.setReportMode(ReportModeRequest.DATA_REPORT_0x32);
133 		};
134 		
135 	};
136 	
137 	protected Action graphicalPanelAction = new AbstractAction("Graphical Panel") {
138 		
139 		public void actionPerformed(ActionEvent e) {
140 			final BalanceBoardGraphicalPanel p1 = new BalanceBoardGraphicalPanel(mote.<BalanceBoard>getExtension());
141 			final BalanceBoardListenerPanel p2 = new BalanceBoardListenerPanel(mote.<BalanceBoard>getExtension());
142 			SwingUtilities.invokeLater(new Runnable() {
143 			
144 				public void run() {
145 					JPanel tmp = new JPanel(new GridLayout(2,1));
146 					tmp.add(p1.getPanel());
147 					tmp.add(p2.getPanel());
148 					frame.getContentPane().add(tmp, BorderLayout.CENTER);
149 					frame.pack();
150 				}
151 			});
152 		}
153 	};
154 	
155 	public BalanceBoardGUI() {
156 		initGUI();
157 	}
158 	
159 	private JPanel buttonPanel = new JPanel(new FlowLayout());
160 	
161 	protected void initGUI() {
162 		buttonPanel.add(new JButton(findBalanceBoardAction));
163 		buttonPanel.add(new JButton(statusInformationAction));
164 		buttonPanel.add(new JButton(moteCalibrationAction));
165 		buttonPanel.add(new JButton(bbCalibrationAction));
166 		buttonPanel.add(new JButton(bbListenerAction));
167 		buttonPanel.add(new JButton(graphicalPanelAction));
168 		buttonPanel.add(new JButton(report0x32Action));
169 		frame = new JFrame("motej BalanceBoard GUI");
170 		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
171 		frame.getContentPane().setLayout(new BorderLayout());
172 		frame.getContentPane().add(buttonPanel, BorderLayout.NORTH);
173 		frame.pack();
174 		frame.setVisible(true);
175 	}
176 	
177 	public static void main(String[] args) {
178 		SwingUtilities.invokeLater(new Runnable() {
179 		
180 			public void run() {
181 				new BalanceBoardGUI();
182 			}
183 		});
184 	}
185 }