Revision 4752 trunk/extensions/extGPS/src/org/gvsig/gps/panel/GPSControlPanel.java
GPSControlPanel.java | ||
---|---|---|
43 | 43 |
* |
44 | 44 |
* $Id$ |
45 | 45 |
* $Log$ |
46 |
* Revision 1.6 2006-04-05 21:46:22 jaume
|
|
46 |
* Revision 1.7 2006-04-06 10:35:48 jaume
|
|
47 | 47 |
* *** empty log message *** |
48 | 48 |
* |
49 | 49 |
* Revision 1.5 2006/04/05 17:08:18 jaume |
... | ... | |
67 | 67 |
|
68 | 68 |
import gnu.io.CommPortIdentifier; |
69 | 69 |
import gnu.io.PortInUseException; |
70 |
import gnu.io.SerialPort; |
|
70 | 71 |
|
71 |
import java.awt.Component; |
|
72 | 72 |
import java.awt.geom.Point2D; |
73 | 73 |
import java.util.Enumeration; |
74 | 74 |
|
75 |
import javax.swing.AbstractButton; |
|
76 | 75 |
import javax.swing.JButton; |
77 |
import javax.swing.JComboBox; |
|
78 | 76 |
import javax.swing.JLabel; |
77 |
import javax.swing.JOptionPane; |
|
79 | 78 |
import javax.swing.JPanel; |
80 | 79 |
import javax.swing.JScrollPane; |
81 | 80 |
import javax.swing.JTable; |
82 |
import javax.swing.JTextField; |
|
83 | 81 |
import javax.swing.SwingConstants; |
84 | 82 |
import javax.swing.table.AbstractTableModel; |
85 | 83 |
import javax.swing.table.TableColumn; |
... | ... | |
96 | 94 |
import com.iver.andami.PluginServices; |
97 | 95 |
import com.iver.andami.ui.mdiManager.View; |
98 | 96 |
import com.iver.andami.ui.mdiManager.ViewInfo; |
97 |
import com.iver.utiles.XMLEntity; |
|
99 | 98 |
|
100 | 99 |
public class GPSControlPanel extends JPanel implements View { |
101 | 100 |
private boolean alreadyStarted; |
102 |
|
|
103 | 101 |
private GPSDriver gps; |
104 |
|
|
105 | 102 |
private JPanel centerPanel = null; |
106 | 103 |
private JPanel northPanel = null; |
107 | 104 |
private JScrollPane jScrollPane = null; |
108 | 105 |
private JTable tblStatus = null; |
109 |
private StatusTableModel model; // @jve:decl-index=0:visual-constraint="431,39"
|
|
106 |
private StatusTableModel model; |
|
110 | 107 |
private JButton btnStart = null; |
111 | 108 |
private JButton btnPause = null; |
112 | 109 |
private JButton btnStop = null; |
113 |
|
|
114 |
private JComboBox cmbPorts = null; |
|
115 |
|
|
116 | 110 |
private JLabel lblPort; |
117 |
|
|
118 |
private JLabel lblSampleRate; |
|
119 |
|
|
120 | 111 |
private JLabel lblPosValue; |
121 |
|
|
122 | 112 |
private JLabel lblPos; |
123 |
|
|
124 |
private JTextField txtSampleRate; |
|
113 |
private JButton btnConfig = null; |
|
114 |
private CommPortIdentifier port; |
|
115 |
private int portSpeed; |
|
116 |
private int dataBits; |
|
117 |
private int stopBits; |
|
118 |
private int parity; |
|
125 | 119 |
|
126 | 120 |
|
127 | 121 |
public GPSControlPanel() { |
128 | 122 |
super(); |
129 | 123 |
gps = GPSDriver.getInstance(); |
124 |
XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML(); |
|
125 |
|
|
126 |
// Try to restore last port used |
|
127 |
String portName = null; |
|
128 |
if (!xml.contains("gps-lastSerialPortName")) { |
|
129 |
portName = xml.getStringProperty("gps-lastSerialPortSpeed"); |
|
130 |
} |
|
131 |
Enumeration portList = CommPortIdentifier.getPortIdentifiers(); |
|
132 |
while (portName!=null && portList.hasMoreElements()) { |
|
133 |
CommPortIdentifier myPort = (CommPortIdentifier) portList.nextElement(); |
|
134 |
if (portName.equals(myPort.getName())) { |
|
135 |
setPort(myPort); |
|
136 |
} |
|
137 |
} |
|
138 |
if (!xml.contains("gps-serialPortSpeed")) |
|
139 |
xml.putProperty("gps-serialPortSpeed", 4800); |
|
140 |
if (!xml.contains("gps-serialPortDataBits")) |
|
141 |
xml.putProperty("gps-serialPortDataBits" , "8"); |
|
142 |
if (!xml.contains("gps-serialPortStopBits")) |
|
143 |
xml.putProperty("gps-serialPortStopBits", stopBits); |
|
144 |
if (!xml.contains("gps-serialPortParities")) |
|
145 |
xml.putProperty("gps-serialPortParities", "none"); |
|
146 |
|
|
147 |
portSpeed = xml.getIntProperty("gps-serialPortSpeed"); |
|
148 |
dataBits = xml.getIntProperty("gps-serialPortDataBits"); |
|
149 |
setPortStopBits(xml.getStringProperty("gps-serialPortStopBits")); |
|
150 |
setPortParity(xml.getStringProperty("gps-serialPortParities")); |
|
130 | 151 |
initialize(); |
131 | 152 |
} |
132 | 153 |
|
... | ... | |
136 | 157 |
* @return void |
137 | 158 |
*/ |
138 | 159 |
private void initialize() { |
139 |
lblPort = new JLabel(); |
|
140 |
lblPort.setBounds(10, 80, 143, 20); |
|
141 |
lblPort.setText("Port:"); |
|
142 |
lblPort.setHorizontalAlignment(SwingConstants.RIGHT); |
|
143 |
lblSampleRate = new JLabel(); |
|
144 |
lblSampleRate.setBounds(10, 52, 143, 20); |
|
145 |
lblSampleRate.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); |
|
146 |
lblSampleRate.setText("Frecuencia de muestreo:"); |
|
147 | 160 |
lblPosValue = new JLabel(); |
148 | 161 |
lblPosValue.setBounds(156, 25, 223, 20); |
149 | 162 |
lblPosValue.setFont(new java.awt.Font("MS Sans Serif", java.awt.Font.BOLD, 11)); |
... | ... | |
164 | 177 |
public ViewInfo getViewInfo() { |
165 | 178 |
ViewInfo m_viewInfo = new ViewInfo(ViewInfo.ICONIFIABLE | |
166 | 179 |
ViewInfo.MAXIMIZABLE | ViewInfo.RESIZABLE); |
167 |
m_viewInfo.setTitle(PluginServices.getText(this, "GPS Control panel"));
|
|
180 |
m_viewInfo.setTitle(PluginServices.getText(this, "gps_control_panel"));
|
|
168 | 181 |
m_viewInfo.setWidth(this.getWidth()+8); |
169 | 182 |
m_viewInfo.setHeight(this.getHeight()+8); |
170 | 183 |
|
... | ... | |
187 | 200 |
centerPanel.setSize(383, 291); |
188 | 201 |
centerPanel.setLocation(5, 111); |
189 | 202 |
centerPanel.add(getJScrollPane(), java.awt.BorderLayout.NORTH); |
203 |
centerPanel.add(getBtnConfig(), null); |
|
190 | 204 |
} |
191 | 205 |
return centerPanel; |
192 | 206 |
} |
... | ... | |
202 | 216 |
northPanel.setLayout(null); |
203 | 217 |
northPanel.setSize(385, 105); |
204 | 218 |
northPanel.setLocation(0, 2); |
205 |
northPanel.add(getCmbPorts(), null); |
|
206 | 219 |
northPanel.add(lblPos, null); |
207 | 220 |
northPanel.add(lblPosValue, null); |
208 |
northPanel.add(getTxtSampleRate(), null); |
|
209 | 221 |
northPanel.add(lblPort, null); |
210 |
northPanel.add(lblSampleRate, null); |
|
211 | 222 |
} |
212 | 223 |
return northPanel; |
213 | 224 |
} |
214 | 225 |
|
215 | 226 |
/** |
216 |
* This method initializes jTextField
|
|
227 |
* This method initializes txtSampleRate
|
|
217 | 228 |
* |
218 | 229 |
* @return javax.swing.JTextField |
219 | 230 |
*/ |
220 |
private JTextField getTxtSampleRate() { |
|
221 |
if (txtSampleRate == null) { |
|
222 |
txtSampleRate = new JTextField(); |
|
223 |
txtSampleRate.setBounds(156, 52, 223, 20); |
|
224 |
txtSampleRate.addKeyListener(new java.awt.event.KeyAdapter() { |
|
225 |
public void keyTyped(java.awt.event.KeyEvent e) { |
|
226 |
try { |
|
227 |
float time = Float.parseFloat(txtSampleRate.getText()); |
|
228 |
gps.setSampleRate(Math.abs((int) (time * 1000))); |
|
229 |
|
|
230 |
} catch (Exception ex) { |
|
231 |
txtSampleRate.setText(""); |
|
232 |
}; |
|
233 |
} |
|
234 |
}); |
|
235 |
} |
|
236 |
return txtSampleRate; |
|
237 |
} |
|
231 |
|
|
238 | 232 |
|
239 | 233 |
/** |
240 | 234 |
* This method initializes jScrollPane |
... | ... | |
244 | 238 |
private JScrollPane getJScrollPane() { |
245 | 239 |
if (jScrollPane == null) { |
246 | 240 |
jScrollPane = new JScrollPane(); |
247 |
jScrollPane.setSize(370, 261);
|
|
241 |
jScrollPane.setSize(370, 231);
|
|
248 | 242 |
jScrollPane.setLocation(5, 25); |
249 | 243 |
jScrollPane.setViewportView(getTblStatus()); |
250 | 244 |
} |
... | ... | |
252 | 246 |
} |
253 | 247 |
|
254 | 248 |
/** |
255 |
* This method initializes tbl |
|
249 |
* This method initializes tblStatus
|
|
256 | 250 |
* |
257 | 251 |
* @return javax.swing.JTable |
258 | 252 |
*/ |
... | ... | |
277 | 271 |
} |
278 | 272 |
|
279 | 273 |
/** |
280 |
* This method initializes jButton
|
|
274 |
* This method initializes btnStart
|
|
281 | 275 |
* |
282 | 276 |
* @return javax.swing.JButton |
283 | 277 |
*/ |
284 | 278 |
private JButton getBtnStart() { |
285 | 279 |
if (btnStart == null) { |
286 | 280 |
btnStart = new JButton(); |
287 |
btnStart.setText("Start");
|
|
281 |
btnStart.setText("start");
|
|
288 | 282 |
btnStart.setBounds(75, 405, 70, 20); |
289 | 283 |
btnStart.addActionListener(new java.awt.event.ActionListener() { |
290 | 284 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
... | ... | |
296 | 290 |
} |
297 | 291 |
|
298 | 292 |
/** |
299 |
* This method initializes jButton1
|
|
293 |
* This method initializes btnPause
|
|
300 | 294 |
* |
301 | 295 |
* @return javax.swing.JButton |
302 | 296 |
*/ |
... | ... | |
304 | 298 |
if (btnPause == null) { |
305 | 299 |
btnPause = new JButton(); |
306 | 300 |
btnPause.setBounds(150, 405, 70, 20); |
307 |
btnPause.setText("Pause");
|
|
301 |
btnPause.setText("sause");
|
|
308 | 302 |
btnPause.addActionListener(new java.awt.event.ActionListener() { |
309 | 303 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
310 | 304 |
gps.silence(); |
... | ... | |
315 | 309 |
} |
316 | 310 |
|
317 | 311 |
/** |
318 |
* This method initializes jButton2
|
|
312 |
* This method initializes btnStop
|
|
319 | 313 |
* |
320 | 314 |
* @return javax.swing.JButton |
321 | 315 |
*/ |
... | ... | |
323 | 317 |
if (btnStop == null) { |
324 | 318 |
btnStop = new JButton(); |
325 | 319 |
btnStop.setBounds(270, 405, 70, 21); |
326 |
btnStop.setText("Stop");
|
|
320 |
btnStop.setText("stop");
|
|
327 | 321 |
btnStop.addActionListener(new java.awt.event.ActionListener() { |
328 | 322 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
329 | 323 |
gps.closePort(); |
... | ... | |
332 | 326 |
} |
333 | 327 |
return btnStop; |
334 | 328 |
} |
329 |
|
|
330 |
public void start() { |
|
331 |
if (!alreadyStarted && port!=null) { |
|
332 |
try { |
|
333 |
|
|
334 |
gps.setPort(port, portSpeed, dataBits, stopBits, parity); |
|
335 |
|
|
336 |
gps.addEventListener(new GPSEventListener() { |
|
337 |
|
|
338 |
public void unhandledMessage(String message) { |
|
339 |
System.out.println("unhandledMessage"+": "+message+"\n"); |
|
340 |
lblPosValue.setText(message); |
|
341 |
} |
|
342 |
|
|
343 |
public void connectionLost() { |
|
344 |
System.out.println("connectionLost"); |
|
345 |
} |
|
346 |
|
|
347 |
public void connectionEstablished() { |
|
348 |
System.out.println("connectionEstablished"); |
|
349 |
} |
|
350 |
|
|
351 |
public void newLonLatPositionReceived(double lon, double lat) { |
|
352 |
IProjection reqProj = ProjectionPool.get("EPSG:23030"); |
|
353 |
IProjection latLonProj = ProjectionPool.get("EPSG:4326"); |
|
354 |
ICoordTrans ct = new CoordTrans((CoordSys) latLonProj, (CoordSys) reqProj); |
|
355 |
Point2D pDst = new Point2D.Double(); |
|
356 |
Point2D pSrc = new Point2D.Double(lon, lat); |
|
357 |
pDst = ct.convert(pSrc, pDst); |
|
358 |
lblPosValue.setText("("+pDst.getX()+", "+pDst.getY()+")"); |
|
359 |
|
|
360 |
GPSExtension ext = (GPSExtension) PluginServices.getExtension(GPSExtension.class); |
|
361 |
ext.drawSymbol(null, null, pDst); |
|
362 |
} |
|
363 |
|
|
364 |
public void signalLevelChanged(float level) { |
|
365 |
} |
|
366 |
|
|
367 |
public void speedChanged(float speed, short course) { |
|
368 |
} |
|
369 |
|
|
370 |
public void estimatedPosErrorChanged(double f) { |
|
371 |
} |
|
372 |
|
|
373 |
public void heightChanged(float height) { |
|
374 |
} |
|
375 |
|
|
376 |
public void precisionChanged(float pDop, float hDop, float vDop) { |
|
377 |
} |
|
378 |
|
|
379 |
}); |
|
380 |
gps.start(); |
|
381 |
alreadyStarted = true; |
|
382 |
} catch (PortInUseException e) { |
|
383 |
JOptionPane.showMessageDialog(this, PluginServices.getText(this, "port_in_use")); |
|
384 |
} |
|
385 |
} |
|
386 |
} |
|
335 | 387 |
|
336 | 388 |
/** |
337 |
* This method initializes jComboBox
|
|
389 |
* This method initializes jButton
|
|
338 | 390 |
* |
339 |
* @return javax.swing.JComboBox
|
|
391 |
* @return javax.swing.JButton
|
|
340 | 392 |
*/ |
341 |
private JComboBox getCmbPorts() { |
|
342 |
if (cmbPorts == null) { |
|
343 |
cmbPorts = new JComboBox(); |
|
344 |
cmbPorts.setBounds(156, 80, 166, 20); |
|
345 |
Enumeration portList = CommPortIdentifier.getPortIdentifiers(); |
|
346 |
while (portList.hasMoreElements()) { |
|
347 |
cmbPorts.addItem(((CommPortIdentifier) portList.nextElement()).getName()); |
|
348 |
} |
|
349 |
|
|
393 |
private JButton getBtnConfig() { |
|
394 |
if (btnConfig == null) { |
|
395 |
btnConfig = new JButton(); |
|
396 |
btnConfig.setBounds(265, 263, 70, 20); |
|
397 |
btnConfig.setText(PluginServices.getText(this, "config")); |
|
350 | 398 |
} |
351 |
return cmbPorts;
|
|
399 |
return btnConfig;
|
|
352 | 400 |
} |
401 |
|
|
402 |
/** |
|
403 |
* Sets the port to be monitorized. |
|
404 |
* @param port |
|
405 |
*/ |
|
406 |
public void setPort(CommPortIdentifier port) { |
|
407 |
this.port = port; |
|
408 |
} |
|
409 |
|
|
410 |
/** |
|
411 |
* Sets the speed in bps of the port that is being monitorized. |
|
412 |
* @param portSpeed |
|
413 |
*/ |
|
414 |
public void setPortSpeed(int portSpeed) { |
|
415 |
this.portSpeed = portSpeed; |
|
416 |
} |
|
417 |
|
|
418 |
/** |
|
419 |
* Sets the amount of data bits of the port that is being monitorized. |
|
420 |
* @param int, one of 5, 6, 7, or 8 |
|
421 |
*/ |
|
422 |
public void setPortDataBits(int dataBits) { |
|
423 |
switch (dataBits) { |
|
424 |
case 5: |
|
425 |
this.dataBits = SerialPort.DATABITS_5; |
|
426 |
break; |
|
427 |
case 6: |
|
428 |
this.dataBits = SerialPort.DATABITS_6; |
|
429 |
break; |
|
430 |
case 7: |
|
431 |
this.dataBits = SerialPort.DATABITS_7; |
|
432 |
break; |
|
433 |
case 8: |
|
434 |
this.dataBits = SerialPort.DATABITS_8; |
|
435 |
break; |
|
436 |
} |
|
437 |
|
|
438 |
} |
|
353 | 439 |
|
354 |
public void start() { |
|
355 |
if (!alreadyStarted) { |
|
356 |
Enumeration portList = CommPortIdentifier.getPortIdentifiers(); |
|
357 |
while (portList.hasMoreElements()) { |
|
358 |
CommPortIdentifier myPortId = (CommPortIdentifier) portList.nextElement(); |
|
359 |
if (myPortId.getName().equals(getCmbPorts().getSelectedItem().toString())) |
|
360 |
try { |
|
361 |
|
|
362 |
gps.setPort(myPortId, 4800); |
|
363 |
gps.addEventListener(new GPSEventListener() { |
|
364 |
|
|
365 |
public void unhandledMessage(String message) { |
|
366 |
System.out.println("unhandledMessage"+": "+message+"\n"); |
|
367 |
lblPosValue.setText(message); |
|
368 |
} |
|
369 |
|
|
370 |
public void connectionLost() { |
|
371 |
System.out.println("connectionLost"); |
|
372 |
} |
|
373 |
|
|
374 |
public void connectionEstablished() { |
|
375 |
System.out.println("connectionEstablished"); |
|
376 |
} |
|
377 |
|
|
378 |
public void newLonLatPositionReceived(double lon, double lat) { |
|
379 |
IProjection reqProj = ProjectionPool.get("EPSG:23030"); |
|
380 |
IProjection latLonProj = ProjectionPool.get("EPSG:4326"); |
|
381 |
ICoordTrans ct = new CoordTrans((CoordSys) latLonProj, (CoordSys) reqProj); |
|
382 |
Point2D pDst = new Point2D.Double(); |
|
383 |
Point2D pSrc = new Point2D.Double(lon, lat); |
|
384 |
//Point2D pSrc = new Point2D.Double(-0.36297333240509033, 39.463722229003906); |
|
385 |
pDst = ct.convert(pSrc, pDst); |
|
386 |
lblPosValue.setText("("+pDst.getX()+", "+pDst.getY()+")"); |
|
387 |
|
|
388 |
GPSExtension ext = (GPSExtension) PluginServices.getExtension(GPSExtension.class); |
|
389 |
ext.drawPoint(null, null, pDst); |
|
390 |
} |
|
391 |
|
|
392 |
public void signalLevelChanged(float level) { |
|
393 |
} |
|
394 |
|
|
395 |
public void speedChanged(float speed, short course) { |
|
396 |
} |
|
397 |
|
|
398 |
public void estimatedPosErrorChanged(double f) { |
|
399 |
} |
|
400 |
|
|
401 |
public void heightChanged() { |
|
402 |
} |
|
403 |
|
|
404 |
}); |
|
405 |
gps.start(); |
|
406 |
alreadyStarted = true; |
|
407 |
} catch (PortInUseException e) { |
|
408 |
// TODO Auto-generated catch block |
|
409 |
e.printStackTrace(); |
|
410 |
} |
|
411 |
} |
|
440 |
/** |
|
441 |
* Sets the port's stop bits |
|
442 |
* @param stopBIts, one of "1", "1.5", or "2" |
|
443 |
*/ |
|
444 |
public void setPortStopBits(String stopBits) { |
|
445 |
if (stopBits.equals("1")) { |
|
446 |
this.stopBits = SerialPort.STOPBITS_1; |
|
447 |
} else if (stopBits.equals("1.5")) { |
|
448 |
this.stopBits = SerialPort.STOPBITS_1_5; |
|
449 |
} else if (stopBits.equals("2")) { |
|
450 |
this.stopBits = SerialPort.STOPBITS_2; |
|
412 | 451 |
} |
413 | 452 |
} |
414 |
} // @jve:decl-index=0:visual-constraint="13,3" |
|
415 | 453 |
|
454 |
/** |
|
455 |
* Sets the parity of the port that is being monitorized |
|
456 |
* @param string: one of "even", "mark", "none", "odd", or "space" |
|
457 |
*/ |
|
458 |
public void setPortParity(String parity) { |
|
459 |
if (parity.equals("even")) { |
|
460 |
this.parity = SerialPort.PARITY_EVEN; |
|
461 |
} else if (parity.equals("mark")) { |
|
462 |
this.parity = SerialPort.PARITY_MARK; |
|
463 |
} else if (parity.equals("none")) { |
|
464 |
this.parity = SerialPort.PARITY_NONE; |
|
465 |
} else if (parity.equals("odd")) { |
|
466 |
this.parity = SerialPort.PARITY_ODD; |
|
467 |
} else if (parity.equals("space")) { |
|
468 |
this.parity = SerialPort.PARITY_SPACE; |
|
469 |
} |
|
470 |
} |
|
471 |
} |
|
472 |
|
|
416 | 473 |
class StatusTableModel extends AbstractTableModel { |
417 | 474 |
private String[] keys, values; |
418 | 475 |
|
Also available in: Unified diff