View Javadoc

1   /**
2    *  BlueCove - Java library for Bluetooth
3    *  Copyright (C) 2006-2007 Vlad Skarzhevskyy
4    * 
5    *  Licensed to the Apache Software Foundation (ASF) under one
6    *  or more contributor license agreements.  See the NOTICE file
7    *  distributed with this work for additional information
8    *  regarding copyright ownership.  The ASF licenses this file
9    *  to you under the Apache License, Version 2.0 (the
10   *  "License"); you may not use this file except in compliance
11   *  with the License.  You may obtain a copy of the License at
12   *
13   *    http://www.apache.org/licenses/LICENSE-2.0
14   *
15   *  Unless required by applicable law or agreed to in writing,
16   *  software distributed under the License is distributed on an
17   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   *  KIND, either express or implied.  See the License for the
19   *  specific language governing permissions and limitations
20   *  under the License.
21   *
22   *  @author vlads
23   *  @version $Id: ConfigurationDialog.java 2607 2008-12-17 23:51:33Z skarzhevskyy $
24   */
25  package net.sf.bluecove.awt;
26  
27  import java.awt.BorderLayout;
28  import java.awt.Button;
29  import java.awt.Checkbox;
30  import java.awt.Component;
31  import java.awt.Font;
32  import java.awt.Frame;
33  import java.awt.GridLayout;
34  import java.awt.Label;
35  import java.awt.Panel;
36  import java.awt.TextField;
37  import java.awt.event.ActionEvent;
38  import java.awt.event.ActionListener;
39  import java.awt.event.MouseAdapter;
40  import java.awt.event.MouseEvent;
41  import java.lang.reflect.Field;
42  import java.util.Enumeration;
43  import java.util.Vector;
44  
45  import javax.bluetooth.UUID;
46  
47  import org.bluecove.tester.log.Logger;
48  
49  import net.sf.bluecove.Configuration;
50  import net.sf.bluecove.Consts;
51  import net.sf.bluecove.util.BluetoothTypesInfo;
52  import net.sf.bluecove.util.BooleanVar;
53  import net.sf.bluecove.util.IntVar;
54  import net.sf.bluecove.util.StringVar;
55  
56  /**
57   *
58   * 
59   */
60  public class ConfigurationDialog extends OkCancelDialog {
61  
62  	private static final long serialVersionUID = 1L;
63  
64  	private static final String NULL_VALUE = "{null}";
65  
66  	private Panel panelItems;
67  
68  	private Vector configItems = new Vector();
69  
70  	private Button btnPagePrev, btnPageNext;
71  
72  	private int page = 0;
73  
74  	private class ConfigurationComponent {
75  
76  		String name;
77  
78  		String guiName;
79  
80  		Component guiComponent;
81  
82  		Field configField;
83  
84  	}
85  
86  	public ConfigurationDialog(Frame owner) {
87  		super(owner, "Configuration", true);
88  
89  		panelBtns.add(btnPagePrev = new Button("<<"));
90  		btnPagePrev.addActionListener(new ActionListener() {
91  			public void actionPerformed(ActionEvent e) {
92  				onPage(-1);
93  			}
94  		});
95  
96  		panelBtns.add(btnPageNext = new Button(">>"));
97  		btnPageNext.addActionListener(new ActionListener() {
98  			public void actionPerformed(ActionEvent e) {
99  				onPage(1);
100 			}
101 		});
102 
103 		if (Configuration.screenSizeSmall) {
104 			this.setFont(new Font("Default", Font.PLAIN, 9));
105 		}
106 		// if (Configuration.screenSizeSmall) {
107 		// Scrollbar slider =new Scrollbar(Scrollbar.VERTICAL, 0, 1, 0, 255);
108 		// add(slider, BorderLayout.EAST);
109 		//			 
110 		// //ScrollPane scrollPane = new ScrollPane();
111 		// //this.add(scrollPane, BorderLayout.NORTH);
112 		// //scrollPane.add(panelItems, BorderLayout.NORTH);
113 		// }
114 		//		 
115 		panelItems = new BorderPanel();
116 		this.add(panelItems, BorderLayout.NORTH);
117 
118 		addConfig("deviceClassFilter");
119 		addConfig("discoverDevicesComputers");
120 		addConfig("discoverDevicesPhones");
121 		addConfig("listedDevicesOnly");
122 		addConfig("discoveryUUID");
123 		addConfig("useShortUUID");
124 		addConfig("useServiceClassExtUUID");
125 		if (Configuration.screenSizeSmall) {
126 			addConfig(null);
127 		}
128 		addConfig("testRFCOMM");
129 		addConfig("TEST_CASE_FIRST");
130 		addConfig("TEST_CASE_LAST", "TEST_CASE_LAST (" + Consts.TEST_LAST_WORKING + "/"
131 				+ Consts.TEST_LAST_BLUECOVE_WORKING + ")");
132 		addConfig("STERSS_TEST_CASE");
133 		addConfig("testL2CAP");
134 		addConfig("TEST_CASE_L2CAP_FIRST");
135 		addConfig("TEST_CASE_L2CAP_LAST");
136 		addConfig("bluecovepsm");
137 
138 		addConfig(null);
139 
140 		addConfig("testServerOBEX_TCP");
141 		addConfig("testServerOBEXObjectPush");
142 		addConfig("authenticateOBEX");
143 		addConfig("authenticate");
144 		addConfig("encrypt");
145 		addConfig("authorize");
146 
147 		if (Configuration.screenSizeSmall) {
148 			addConfig(null);
149 		}
150 
151 		addConfig("clientSleepBetweenConnections");
152 		addConfig("clientTestTimeOutSec");
153 		addConfig("serverSleepB4ClosingConnection");
154 		addConfig("testServiceAttributes");
155 		addConfig("testIgnoreNotWorkingServiceAttributes", "ignoreNotWorkingServAttr");
156 		addConfig("testAllServiceAttributes");
157 
158 		addConfig("testServerForceDiscoverable");
159 
160 		addConfig(null);
161 
162 		addConfig("clientContinuous");
163 		addConfig("clientContinuousDiscovery");
164 		addConfig("clientContinuousDiscoveryDevices");
165 		addConfig("clientContinuousServicesSearch");
166 		addConfig("clientTestConnections");
167 		addConfig("clientTestConnectionsMultipleThreads", "concurrent connections");
168 
169 		addConfig("tgSleep");
170 		addConfig("tgSize");
171 		addConfig("tgDurationMin");
172 
173 		panelItems.setLayout(new GridLayout(configItems.size(), 2));
174 
175 		buildUIItems();
176 		updateGUI();
177 
178 		this.pack();
179 		OkCancelDialog.centerParent(this);
180 	}
181 
182 	protected void onClose(boolean isCancel) {
183 		if (!isCancel) {
184 			updateConfig();
185 		}
186 		setVisible(false);
187 	}
188 
189 	private void onPage(int i) {
190 		page += i;
191 		updateConfig();
192 		buildUIItems();
193 		updateGUI();
194 	}
195 
196 	private static String nvl(Object o) {
197 		if (o == null) {
198 			return NULL_VALUE;
199 		} else {
200 			return o.toString();
201 		}
202 	}
203 
204 	private void updateGUI() {
205 		for (Enumeration en = configItems.elements(); en.hasMoreElements();) {
206 			ConfigurationComponent cc = (ConfigurationComponent) en.nextElement();
207 			if (cc.guiComponent == null) {
208 				continue;
209 			}
210 			Class type = cc.configField.getType();
211 			try {
212 				if (type.equals(boolean.class)) {
213 					Checkbox c = (Checkbox) cc.guiComponent;
214 					c.setState(cc.configField.getBoolean(Configuration.class));
215 				} else if (type.equals(BooleanVar.class)) {
216 					Checkbox c = (Checkbox) cc.guiComponent;
217 					c.setState(((BooleanVar) cc.configField.get(Configuration.class)).booleanValue());
218 				} else if (type.equals(UUID.class)) {
219 					TextField tf = (TextField) cc.guiComponent;
220 					tf.setText(nvl(cc.configField.get(Configuration.class)));
221 				} else if ((type.equals(String.class)) || (type.equals(StringVar.class))) {
222 					TextField tf = (TextField) cc.guiComponent;
223 					tf.setText(nvl(cc.configField.get(Configuration.class)));
224 				} else if (type.equals(int.class)) {
225 					TextField tf = (TextField) cc.guiComponent;
226 					tf.setText(String.valueOf(cc.configField.getInt(Configuration.class)));
227 				} else if (type.equals(IntVar.class)) {
228 					TextField tf = (TextField) cc.guiComponent;
229 					tf.setText(cc.configField.get(Configuration.class).toString());
230 				}
231 			} catch (Throwable e) {
232 				Logger.error("internal error for " + cc.name, e);
233 			}
234 		}
235 		this.pack();
236 	}
237 
238 	private void buildUIItems() {
239 
240 		panelItems.removeAll();
241 
242 		int cPage = 0;
243 		int lineCount = 0;
244 		for (Enumeration en = configItems.elements(); en.hasMoreElements();) {
245 			final ConfigurationComponent cc = (ConfigurationComponent) en.nextElement();
246 
247 			if (cc.name == null) {
248 				cPage++;
249 				continue;
250 			}
251 			if (cPage != page) {
252 				cc.guiComponent = null;
253 				continue;
254 			}
255 
256 			Class type = cc.configField.getType();
257 
258 			if ((type.equals(boolean.class)) || (type.equals(BooleanVar.class))) {
259 				Checkbox c = new Checkbox();
260 				cc.guiComponent = c;
261 			} else if ((type.equals(String.class)) || (type.equals(StringVar.class)) || (type.equals(UUID.class))
262 					|| (type.equals(int.class)) || (type.equals(IntVar.class))) {
263 				TextField tf = new TextField();
264 				cc.guiComponent = tf;
265 			} else {
266 				Logger.error("internal error for " + cc.name + " unsupported class " + type.getName());
267 				return;
268 			}
269 
270 			Label l = new Label((cc.guiName == null) ? cc.name : cc.guiName);
271 			panelItems.add(l);
272 			panelItems.add(cc.guiComponent);
273 
274 			lineCount++;
275 
276 			l.addMouseListener(new MouseAdapter() {
277 				Component guiComponent = cc.guiComponent;
278 
279 				public void mouseClicked(MouseEvent e) {
280 					guiComponent.requestFocus();
281 				}
282 			});
283 		}
284 		panelItems.setLayout(new GridLayout(lineCount, 2));
285 		btnPagePrev.setEnabled((page > 0));
286 		btnPageNext.setEnabled((cPage > page));
287 	}
288 
289 	private void updateConfig() {
290 		for (Enumeration en = configItems.elements(); en.hasMoreElements();) {
291 			ConfigurationComponent cc = (ConfigurationComponent) en.nextElement();
292 			if (cc.guiComponent == null) {
293 				continue;
294 			}
295 			Class type = cc.configField.getType();
296 			try {
297 				if (type.equals(boolean.class)) {
298 					Checkbox c = (Checkbox) cc.guiComponent;
299 					cc.configField.setBoolean(Configuration.class, c.getState());
300 				} else if (type.equals(BooleanVar.class)) {
301 					Checkbox c = (Checkbox) cc.guiComponent;
302 					((BooleanVar) cc.configField.get(Configuration.class)).setValue(c.getState());
303 				} else if (type.equals(String.class)) {
304 					TextField tf = (TextField) cc.guiComponent;
305 					if (NULL_VALUE.equals(tf.getText().trim())) {
306 						cc.configField.set(Configuration.class, null);
307 					} else {
308 						cc.configField.set(Configuration.class, tf.getText().trim());
309 					}
310 				} else if (type.equals(StringVar.class)) {
311 					TextField tf = (TextField) cc.guiComponent;
312 					if (NULL_VALUE.equals(tf.getText().trim())) {
313 						((StringVar) cc.configField.get(Configuration.class)).setValue(null);
314 					} else {
315 						((StringVar) cc.configField.get(Configuration.class)).setValue(tf.getText().trim());
316 					}
317 				} else if (type.equals(int.class)) {
318 					TextField tf = (TextField) cc.guiComponent;
319 					cc.configField.setInt(Configuration.class, Integer.valueOf(tf.getText().trim()).intValue());
320 				} else if (type.equals(IntVar.class)) {
321 					TextField tf = (TextField) cc.guiComponent;
322 					((IntVar) cc.configField.get(Configuration.class)).setValue(tf.getText());
323 				} else if (type.equals(UUID.class)) {
324 					TextField tf = (TextField) cc.guiComponent;
325 					if (NULL_VALUE.equals(tf.getText().trim())) {
326 						cc.configField.set(Configuration.class, null);
327 					} else {
328 						cc.configField.set(Configuration.class, BluetoothTypesInfo.UUIDConsts.getUUID(tf.getText()
329 								.trim()));
330 					}
331 				}
332 			} catch (Throwable e) {
333 				Logger.error("internal error for " + cc.name, e);
334 			}
335 		}
336 	}
337 
338 	private void addConfig(String name) {
339 		addConfig(name, null);
340 	}
341 
342 	private void addConfig(String name, String guiName) {
343 		ConfigurationComponent cc = new ConfigurationComponent();
344 		cc.name = name;
345 		if (cc.name != null) {
346 			try {
347 				cc.configField = Configuration.class.getDeclaredField(name);
348 			} catch (Throwable e) {
349 				Logger.error("internal error for " + name, e);
350 				return;
351 			}
352 		}
353 		cc.guiName = guiName;
354 		cc.guiComponent = null;
355 		configItems.addElement(cc);
356 	}
357 
358 }