Revision 4749 trunk/extensions/extGPS/src/org/gvsig/gps/panel/GPSControlPanel.java

View differences:

GPSControlPanel.java
43 43
 *
44 44
 * $Id$
45 45
 * $Log$
46
 * Revision 1.4  2006-04-05 16:02:09  jaume
46
 * Revision 1.5  2006-04-05 17:08:18  jaume
47 47
 * *** empty log message ***
48 48
 *
49
 * Revision 1.4  2006/04/05 16:02:09  jaume
50
 * *** empty log message ***
51
 *
49 52
 * Revision 1.3  2006/04/05 14:50:37  jaume
50 53
 * *** empty log message ***
51 54
 *
......
59 62
 */
60 63
package org.gvsig.gps.panel;
61 64

  
62
import java.awt.BorderLayout;
63
import java.awt.Dimension;
65
import gnu.io.CommPortIdentifier;
66
import gnu.io.PortInUseException;
64 67

  
68
import java.awt.Component;
69
import java.awt.geom.Point2D;
70
import java.util.Enumeration;
71

  
72
import javax.swing.AbstractButton;
65 73
import javax.swing.JButton;
74
import javax.swing.JComboBox;
75
import javax.swing.JLabel;
66 76
import javax.swing.JPanel;
67 77
import javax.swing.JScrollPane;
68 78
import javax.swing.JTable;
79
import javax.swing.JTextField;
80
import javax.swing.SwingConstants;
69 81
import javax.swing.table.AbstractTableModel;
70 82
import javax.swing.table.TableColumn;
71 83

  
84
import org.cresques.cts.ICoordTrans;
85
import org.cresques.cts.IProjection;
86
import org.cresques.cts.ProjectionPool;
87
import org.cresques.cts.gt2.CoordSys;
88
import org.cresques.cts.gt2.CoordTrans;
89
import org.gvsig.gps.GPSDriver;
90
import org.gvsig.gps.GPSExtension;
91
import org.gvsig.gps.listeners.GPSEventListener;
92

  
72 93
import com.iver.andami.PluginServices;
73 94
import com.iver.andami.ui.mdiManager.View;
74 95
import com.iver.andami.ui.mdiManager.ViewInfo;
75 96

  
76
public class GPSControlPanel extends JPanel implements View{
97
public class GPSControlPanel extends JPanel implements View {
98
	private boolean alreadyStarted;
99

  
100
	private GPSDriver gps;
101
	
77 102
    private JPanel centerPanel = null;
78 103
	private JPanel northPanel = null;
79 104
	private JScrollPane jScrollPane = null;
80 105
	private JTable tblStatus = null;
81
	private JButton jButton = null;
82 106
	private StatusTableModel model;
107
	private JButton btnStart = null;
108
	private JButton btnPause = null;
109
	private JButton btnStop = null;
83 110

  
111
	private JComboBox cmbPorts = null;
112

  
113
	private JLabel lblPort;
114

  
115
	private JLabel lblSampleRate;
116

  
117
	private JLabel lblPosValue;
118

  
119
	private JLabel lblPos;
120

  
121
	private JTextField txtSampleRate;
122
	
123

  
84 124
	public GPSControlPanel() {
85 125
		super();
126
		gps = GPSDriver.getInstance();
86 127
		initialize();
87 128
	}
88 129

  
......
92 133
	 * @return void
93 134
	 */
94 135
	private void initialize() {
95
		this.setLayout(new BorderLayout());
96
		this.setSize(200, 200);
136
		lblPort = new JLabel();
137
		lblPort.setBounds(10, 80, 143, 20);
138
		lblPort.setText("Port:");
139
		lblPort.setHorizontalAlignment(SwingConstants.RIGHT);
140
		lblSampleRate = new JLabel();
141
		lblSampleRate.setBounds(10, 52, 143, 20);
142
		lblSampleRate.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
143
		lblSampleRate.setText("Frecuencia de muestreo:");
144
		lblPosValue = new JLabel();
145
		lblPosValue.setBounds(156, 25, 223, 20);
146
		lblPosValue.setFont(new java.awt.Font("MS Sans Serif", java.awt.Font.BOLD, 11));
147
		lblPos = new JLabel();
148
		lblPos.setText("Pos:");
149
		lblPos.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
150
		lblPos.setBounds(10, 25, 143, 20);
151
		this.setLayout(null);
152
		this.setSize(391, 433);
97 153
		this.add(getCenterPanel(), java.awt.BorderLayout.CENTER);
98 154
		this.add(getNorthPanel(), java.awt.BorderLayout.NORTH);
155
		this.add(getBtnStart(), null);
156
		this.add(getBtnPause(), null);
157
		this.add(getBtnStop(), null);
158
		
99 159
	}
100 160

  
101 161
	public ViewInfo getViewInfo() {
102 162
		ViewInfo m_viewInfo = new ViewInfo(ViewInfo.ICONIFIABLE |
103 163
				ViewInfo.MAXIMIZABLE | ViewInfo.RESIZABLE);
104 164
		m_viewInfo.setTitle(PluginServices.getText(this, "GPS Control panel"));
105
		m_viewInfo.setWidth(this.getWidth());
106
		m_viewInfo.setHeight(this.getHeight());
165
		m_viewInfo.setWidth(this.getWidth()+8);
166
		m_viewInfo.setHeight(this.getHeight()+8);
107 167
		
108 168
		return m_viewInfo;
109 169
	}
......
116 176
	private JPanel getCenterPanel() {
117 177
		if (centerPanel == null) {
118 178
			centerPanel = new JPanel();
119
			centerPanel.setLayout(new BorderLayout());
179
			centerPanel.setLayout(null);
120 180
			centerPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
121 181
								  null, PluginServices.getText(this, "gps_status"),
122 182
								  javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, 
123 183
								  javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
184
			centerPanel.setSize(383, 291);
185
			centerPanel.setLocation(5, 111);
124 186
			centerPanel.add(getJScrollPane(), java.awt.BorderLayout.NORTH);
125 187
		}
126 188
		return centerPanel;
......
134 196
	private JPanel getNorthPanel() {
135 197
		if (northPanel == null) {
136 198
			northPanel = new JPanel();
137
			northPanel.setPreferredSize(new Dimension(100, 100));
138
			northPanel.add(getJButton(), null);
199
			northPanel.setLayout(null);
200
			northPanel.setSize(385, 105);
201
			northPanel.setLocation(0, 2);
202
			northPanel.add(getCmbPorts(), null);
203
			northPanel.add(lblPos, null);
204
			northPanel.add(lblPosValue, null);
205
			northPanel.add(getTxtSampleRate(), null);
206
			northPanel.add(lblPort, null);
207
			northPanel.add(lblSampleRate, null);
139 208
		}
140 209
		return northPanel;
141 210
	}
211
	
212
	/**
213
	 * This method initializes jTextField	
214
	 * 	
215
	 * @return javax.swing.JTextField	
216
	 */    
217
	private JTextField getTxtSampleRate() {
218
		if (txtSampleRate == null) {
219
			txtSampleRate = new JTextField();
220
			txtSampleRate.setBounds(156, 52, 223, 20);
221
			txtSampleRate.addKeyListener(new java.awt.event.KeyAdapter() { 
222
				public void keyTyped(java.awt.event.KeyEvent e) {    
223
					try {
224
						float time = Float.parseFloat(txtSampleRate.getText());
225
						gps.setSampleRate(Math.abs((int) (time * 1000)));
226
						
227
					} catch (Exception ex) {
228
						txtSampleRate.setText("");
229
					};
230
				}
231
			});
232
		}
233
		return txtSampleRate;
234
	}
142 235

  
143 236
	/**
144 237
	 * This method initializes jScrollPane	
......
148 241
	private JScrollPane getJScrollPane() {
149 242
		if (jScrollPane == null) {
150 243
			jScrollPane = new JScrollPane();
244
			jScrollPane.setSize(370, 261);
245
			jScrollPane.setLocation(5, 25);
151 246
			jScrollPane.setViewportView(getTblStatus());
152 247
		}
153 248
		return jScrollPane;
......
171 266
			    	column.setWidth(tblStatus.getWidth()-width);
172 267
			    }
173 268
			}
174
			
175
			tblStatus.setPreferredSize(new Dimension(100, 100));
176 269
			tblStatus.setTableHeader(null);
177 270
			model = new StatusTableModel();
178 271
			tblStatus.setModel(model);
......
185 278
	 * 	
186 279
	 * @return javax.swing.JButton	
187 280
	 */    
188
	private JButton getJButton() {
189
		if (jButton == null) {
190
			jButton = new JButton();
281
	private JButton getBtnStart() {
282
		if (btnStart == null) {
283
			btnStart = new JButton();
284
			btnStart.setText("Start");
285
			btnStart.setBounds(75, 405, 70, 20);
286
			btnStart.addActionListener(new java.awt.event.ActionListener() { 
287
				public void actionPerformed(java.awt.event.ActionEvent e) {    
288
					start();
289
				}
290
			});
191 291
		}
192
		return jButton;
292
		return btnStart;
193 293
	}
194
}  //  @jve:decl-index=0:visual-constraint="6,7"
195 294

  
295
	/**
296
	 * This method initializes jButton1	
297
	 * 	
298
	 * @return javax.swing.JButton	
299
	 */    
300
	private JButton getBtnPause() {
301
		if (btnPause == null) {
302
			btnPause = new JButton();
303
			btnPause.setBounds(150, 405, 70, 20);
304
			btnPause.setText("Pause");
305
			btnPause.addActionListener(new java.awt.event.ActionListener() { 
306
				public void actionPerformed(java.awt.event.ActionEvent e) {    
307
					gps.silence();
308
				}
309
			});
310
		}
311
		return btnPause;
312
	}
313

  
314
	/**
315
	 * This method initializes jButton2	
316
	 * 	
317
	 * @return javax.swing.JButton	
318
	 */    
319
	private JButton getBtnStop() {
320
		if (btnStop == null) {
321
			btnStop = new JButton();
322
			btnStop.setBounds(270, 405, 70, 21);
323
			btnStop.setText("Stop");
324
			btnStop.addActionListener(new java.awt.event.ActionListener() { 
325
				public void actionPerformed(java.awt.event.ActionEvent e) {    
326
					gps.closePort();
327
				}
328
			});
329
		}
330
		return btnStop;
331
	}
332

  
333
	/**
334
	 * This method initializes jComboBox	
335
	 * 	
336
	 * @return javax.swing.JComboBox	
337
	 */    
338
	private JComboBox getCmbPorts() {
339
		if (cmbPorts == null) {
340
			cmbPorts = new JComboBox();
341
			cmbPorts.setBounds(156, 80, 166, 20);
342
			Enumeration portList = CommPortIdentifier.getPortIdentifiers();
343
			while (portList.hasMoreElements()) {
344
				cmbPorts.addItem(((CommPortIdentifier) portList.nextElement()).getName());
345
			}
346
			
347
		}
348
		return cmbPorts;
349
	}
350
	
351
	public void start() {
352
		if (!alreadyStarted) {
353
			Enumeration portList = CommPortIdentifier.getPortIdentifiers();
354
			while (portList.hasMoreElements()) {
355
				CommPortIdentifier myPortId = (CommPortIdentifier) portList.nextElement();
356
				if (myPortId.getName().equals(getCmbPorts().getSelectedItem().toString()))
357
					try {
358
						
359
						gps.setPort(myPortId, 4800);
360
						gps.addEventListener(new GPSEventListener() {
361
							
362
							public void unhandledMessage(String message) {
363
								System.out.println("unhandledMessage"+": "+message+"\n");
364
								lblPosValue.setText(message);
365
							}
366
							
367
							public void connectionLost() {
368
								System.out.println("connectionLost");
369
							}
370
							
371
							public void connectionEstablished() {
372
								System.out.println("connectionEstablished");
373
							}
374
							
375
							public void newLonLatPositionReceived(double lon, double lat) {
376
								IProjection reqProj = ProjectionPool.get("EPSG:23030");
377
								IProjection latLonProj = ProjectionPool.get("EPSG:4326");            	
378
								ICoordTrans ct = new CoordTrans((CoordSys) latLonProj, (CoordSys) reqProj);
379
								Point2D pDst = new Point2D.Double();
380
								//Point2D pSrc = new Point2D.Double(lon, lat);
381
								Point2D pSrc = new Point2D.Double(-0.36297333240509033, 39.463722229003906);
382
								pDst = ct.convert(pSrc, pDst);
383
								lblPosValue.setText("("+pDst.getX()+", "+pDst.getY()+")");
384
								
385
								GPSExtension ext = (GPSExtension) PluginServices.getExtension(GPSExtension.class);
386
								ext.drawPoint(null, null, pDst);
387
							}
388
							
389
							public void signalLevelChanged(float level) {
390
							}
391
							
392
							public void speedChanged(float speed, short course) {
393
							}
394
							
395
							public void estimatedPosErrorChanged(double f) {
396
							}
397
							
398
							public void heightChanged() {
399
							}
400
							
401
						});
402
						gps.start();
403
						alreadyStarted = true;
404
					} catch (PortInUseException e) {
405
						// TODO Auto-generated catch block
406
						e.printStackTrace();
407
					}
408
			}
409
		}
410
	}
411
}  //  @jve:decl-index=0:visual-constraint="13,3"
412

  
196 413
class StatusTableModel extends AbstractTableModel {
197 414
	private String[] keys, values;
198

  
415
	
199 416
	public void setTableData(String[] keys, String[] values) {
200 417
		this.keys = keys;
201 418
		this.values = values;
......
217 434
			return keys[rowIndex];
218 435
		return null;
219 436
	}
437
	
438

  
439
	
220 440
}
221 441

  

Also available in: Unified diff