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: ObexClientConnectionDialog.java 2471 2008-12-01 03:44:20Z 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.Choice;
31  import java.awt.Dialog;
32  import java.awt.Font;
33  import java.awt.Frame;
34  import java.awt.GridBagConstraints;
35  import java.awt.GridBagLayout;
36  import java.awt.Label;
37  import java.awt.Panel;
38  import java.awt.TextField;
39  import java.awt.event.ActionEvent;
40  import java.awt.event.ActionListener;
41  import java.awt.event.ItemEvent;
42  import java.awt.event.ItemListener;
43  import java.awt.event.WindowAdapter;
44  import java.awt.event.WindowEvent;
45  import java.util.Date;
46  import java.util.Timer;
47  import java.util.TimerTask;
48  
49  import net.sf.bluecove.Configuration;
50  
51  public class ObexClientConnectionDialog extends Dialog {
52  
53  	private static final long serialVersionUID = 1L;
54  
55  	private static final String configObexConnectionURL = "obexConnectionURL";
56  
57  	Button btnCancel, btnPut, btnGet, btnDisconnect;
58  
59  	TextField tfURL;
60  
61  	Choice choiceAllURLs;
62  
63  	TextField tfName;
64  
65  	TextField tfData;
66  
67  	Checkbox cbTimeout;
68  
69  	Label status;
70  
71  	ObexClientConnectionThread thread;
72  
73  	Timer monitorTimer;
74  
75  	private class ObexConnectionMonitor extends TimerTask {
76  
77  		public void run() {
78  			if (thread != null) {
79  				status.setText(thread.status);
80  				if (!thread.isRunning) {
81  					btnDisconnect.setEnabled(false);
82  					btnPut.setEnabled(true);
83  					btnGet.setEnabled(true);
84  				}
85  			} else {
86  				status.setText("Idle");
87  				btnDisconnect.setEnabled(false);
88  				btnPut.setEnabled(true);
89  				btnGet.setEnabled(true);
90  			}
91  		}
92  	}
93  
94  	public ObexClientConnectionDialog(Frame owner) {
95  		super(owner, "OBEX Client", false);
96  
97  		GridBagLayout gridbag = new GridBagLayout();
98  		GridBagConstraints c = new GridBagConstraints();
99  		c.fill = GridBagConstraints.BOTH;
100 
101 		Panel panelItems = new BorderPanel(gridbag);
102 		this.add(panelItems, BorderLayout.NORTH);
103 
104 		Label l = new Label("URL:");
105 		panelItems.add(l);
106 		panelItems.add(tfURL = new TextField("", 25));
107 		c.gridwidth = 1;
108 		gridbag.setConstraints(l, c);
109 		c.gridwidth = GridBagConstraints.REMAINDER;
110 		gridbag.setConstraints(tfURL, c);
111 
112 		if (Configuration.storage != null) {
113 			tfURL.setText(Configuration.storage.retriveData(configObexConnectionURL));
114 		}
115 
116 		Label l1 = new Label("Discovered:");
117 		panelItems.add(l1);
118 		choiceAllURLs = new Choice();
119 		c.gridwidth = 1;
120 		gridbag.setConstraints(l1, c);
121 		panelItems.add(choiceAllURLs);
122 		c.gridwidth = GridBagConstraints.REMAINDER;
123 		gridbag.setConstraints(choiceAllURLs, c);
124 
125 		Font logFont = new Font("Monospaced", Font.PLAIN, Configuration.screenSizeSmall ? 9 : 12);
126 		choiceAllURLs.setFont(logFont);
127 
128 		ServiceRecords.populateChoice(choiceAllURLs, true);
129 		choiceAllURLs.addItemListener(new ItemListener() {
130 			public void itemStateChanged(ItemEvent e) {
131 				selectURL();
132 			}
133 		});
134 
135 		Label l2 = new Label("Data:");
136 		panelItems.add(l2);
137 		panelItems.add(tfData = new TextField("Test Obex Message " + new Date().toString()));
138 		c.gridwidth = 1;
139 		gridbag.setConstraints(l2, c);
140 		c.gridwidth = GridBagConstraints.REMAINDER;
141 		gridbag.setConstraints(tfData, c);
142 
143 		Label l3 = new Label("Name:");
144 		panelItems.add(l3);
145 		panelItems.add(tfName = new TextField("test.txt"));
146 		c.gridwidth = 1;
147 		gridbag.setConstraints(l3, c);
148 		c.gridwidth = GridBagConstraints.REMAINDER;
149 		gridbag.setConstraints(tfName, c);
150 
151 		Label l4 = new Label("Timeouts:");
152 		panelItems.add(l4);
153 		panelItems.add(cbTimeout = new Checkbox());
154 		c.gridwidth = 1;
155 		gridbag.setConstraints(l4, c);
156 		c.gridwidth = GridBagConstraints.REMAINDER;
157 		gridbag.setConstraints(cbTimeout, c);
158 
159 		Label ls = new Label("Status:");
160 		panelItems.add(ls);
161 		c.gridwidth = 1;
162 		gridbag.setConstraints(ls, c);
163 
164 		status = new Label("Idle");
165 		panelItems.add(status);
166 		c.gridwidth = 2;
167 		gridbag.setConstraints(status, c);
168 
169 		Panel panelBtns = new Panel();
170 		this.add(panelBtns, BorderLayout.SOUTH);
171 
172 		panelBtns.add(btnPut = new Button("Put"));
173 		btnPut.addActionListener(new ActionListener() {
174 			public void actionPerformed(ActionEvent e) {
175 				send(true);
176 			}
177 		});
178 
179 		panelBtns.add(btnGet = new Button("Get"));
180 		btnGet.addActionListener(new ActionListener() {
181 			public void actionPerformed(ActionEvent e) {
182 				send(false);
183 			}
184 		});
185 
186 		panelBtns.add(btnDisconnect = new Button("Disconnect"));
187 		btnDisconnect.addActionListener(new ActionListener() {
188 			public void actionPerformed(ActionEvent e) {
189 				shutdown();
190 			}
191 		});
192 		btnDisconnect.setEnabled(false);
193 
194 		panelBtns.add(btnCancel = new Button("Cancel"));
195 		btnCancel.addActionListener(new ActionListener() {
196 			public void actionPerformed(ActionEvent e) {
197 				onClose();
198 			}
199 		});
200 
201 		this.addWindowListener(new WindowAdapter() {
202 			public void windowClosing(WindowEvent e) {
203 				onClose();
204 			}
205 		});
206 		this.pack();
207 		OkCancelDialog.centerParent(this);
208 
209 		try {
210 			monitorTimer = new Timer();
211 			monitorTimer.schedule(new ObexConnectionMonitor(), 1000, 700);
212 		} catch (Throwable java11) {
213 		}
214 	}
215 
216 	protected void selectURL() {
217 		String url = ServiceRecords.getChoiceURL(choiceAllURLs);
218 		if (url != null) {
219 			tfURL.setText(url);
220 		}
221 	}
222 
223 	protected void send(boolean isPut) {
224 		if (thread != null) {
225 			thread.shutdown();
226 			thread = null;
227 		}
228 		if (Configuration.storage != null) {
229 			Configuration.storage.storeData(configObexConnectionURL, tfURL.getText());
230 		}
231 		thread = new ObexClientConnectionThread(tfURL.getText(), tfName.getText(), tfData.getText(), isPut);
232 		thread.timeouts = cbTimeout.getState();
233 		thread.setDaemon(true);
234 		thread.start();
235 		btnDisconnect.setEnabled(true);
236 		btnPut.setEnabled(false);
237 		btnGet.setEnabled(false);
238 	}
239 
240 	public void shutdown() {
241 		if (thread != null) {
242 			thread.shutdown();
243 			thread = null;
244 		}
245 	}
246 
247 	protected void onClose() {
248 		shutdown();
249 		try {
250 			monitorTimer.cancel();
251 		} catch (Throwable java11) {
252 		}
253 		setVisible(false);
254 	}
255 
256 }