Revision 4753 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.7  2006-04-06 10:35:48  jaume
46
 * Revision 1.8  2006-04-07 08:27:48  jaume
47 47
 * *** empty log message ***
48 48
 *
49
 * Revision 1.7  2006/04/06 10:35:48  jaume
50
 * *** empty log message ***
51
 *
49 52
 * Revision 1.5  2006/04/05 17:08:18  jaume
50 53
 * *** empty log message ***
51 54
 *
......
70 73
import gnu.io.SerialPort;
71 74

  
72 75
import java.awt.geom.Point2D;
76
import java.util.ArrayList;
73 77
import java.util.Enumeration;
78
import java.util.Hashtable;
74 79

  
80
import javax.swing.DefaultCellEditor;
75 81
import javax.swing.JButton;
82
import javax.swing.JComponent;
76 83
import javax.swing.JLabel;
77 84
import javax.swing.JOptionPane;
78 85
import javax.swing.JPanel;
79 86
import javax.swing.JScrollPane;
80 87
import javax.swing.JTable;
81
import javax.swing.SwingConstants;
88
import javax.swing.ListSelectionModel;
89
import javax.swing.event.TableColumnModelListener;
90
import javax.swing.event.TableModelEvent;
82 91
import javax.swing.table.AbstractTableModel;
92
import javax.swing.table.DefaultTableColumnModel;
93
import javax.swing.table.JTableHeader;
94
import javax.swing.table.TableCellRenderer;
83 95
import javax.swing.table.TableColumn;
96
import javax.swing.table.TableColumnModel;
84 97

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

  
94 102
import com.iver.andami.PluginServices;
95
import com.iver.andami.ui.mdiManager.View;
103
import com.iver.andami.plugins.Extension;
104
import com.iver.andami.ui.mdiManager.SingletonView;
96 105
import com.iver.andami.ui.mdiManager.ViewInfo;
97 106
import com.iver.utiles.XMLEntity;
98

  
99
public class GPSControlPanel extends JPanel implements View {
107
/**
108
 * Singleton view that allows the user to monitorize and configure the
109
 * status of the GPS.
110
 * 
111
 * @author jaume dominguez faus - jaume.dominguez@iver.es
112
 */
113
public class GPSControlPanel extends JPanel implements SingletonView {
100 114
	private boolean alreadyStarted;
101 115
	private GPSDriver gps;
102 116
    private JPanel centerPanel = null;
......
107 121
	private JButton btnStart = null;
108 122
	private JButton btnPause = null;
109 123
	private JButton btnStop = null;
110
	private JLabel lblPort;
111
	private JLabel lblPosValue;
112 124
	private JLabel lblPos;
113 125
	private JButton btnConfig = null;
114 126
	private CommPortIdentifier port;
......
117 129
	private int stopBits;
118 130
	private int parity;
119 131
	
132
	private final static Extension thisExtension = PluginServices.getExtension(GPSExtension.class);
133
	private final static String kPos = PluginServices.getText(thisExtension, "UTM_position");
134
	private final static String kMsg = PluginServices.getText(thisExtension, "unknown_message");
135
	private final static String kSignalLevel = PluginServices.getText(thisExtension, "signal_stregth");
136
	private final static String kStatus = PluginServices.getText(thisExtension, "status");
137
	private final static String kSpeed = PluginServices.getText(thisExtension, "speed");
138
	private final static String kCourse = PluginServices.getText(thisExtension, "course");
139
	private final static String kEstErr = PluginServices.getText(thisExtension, "estimated_error");
140
	private final static String kHeight = PluginServices.getText(thisExtension, "height");
141
	private final static String kPosPrecision = PluginServices.getText(thisExtension, "position_precision");
142
	private final static String kHorPrecision = PluginServices.getText(thisExtension, "horizontal_precision");
143
	private final static String kVerPrecision = PluginServices.getText(thisExtension, "vertical_precision");
144
	
145
	
146
	private static GPSControlPanel instance;
147
	
120 148

  
121
	public GPSControlPanel() {
149
	private GPSControlPanel() {
122 150
		super();
123 151
		gps = GPSDriver.getInstance();
152
		
153
		setUp();
154
		initialize();
155
		refreshTable();
156
	}
157
	
158
	private void refreshTable() {
159
		model.putValue(kPos , PluginServices.getText(this, "unknown"));
160
		model.putValue(PluginServices.getText(this, "port"), (port!=null) ? port.getName() : PluginServices.getText(this, "not_yet_set"));
161
		model.putValue(PluginServices.getText(this, "speed")+" (bps)", portSpeed+"");
162
		
163
	}
164

  
165
	private void setUp() {
124 166
		XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
125
		
126 167
		// Try to restore last port used
127 168
		String portName = null;
128
		if (!xml.contains("gps-lastSerialPortName")) {
129
			portName = xml.getStringProperty("gps-lastSerialPortSpeed");
169
		if (xml.contains("gps-serialPortName")) {
170
			portName = xml.getStringProperty("gps-serialPortName");
130 171
		}
131 172
		Enumeration portList = CommPortIdentifier.getPortIdentifiers();
132 173
		while (portName!=null && portList.hasMoreElements()) {
......
140 181
		if (!xml.contains("gps-serialPortDataBits"))
141 182
			xml.putProperty("gps-serialPortDataBits" , "8");
142 183
		if (!xml.contains("gps-serialPortStopBits")) 
143
			xml.putProperty("gps-serialPortStopBits", stopBits);
184
			xml.putProperty("gps-serialPortStopBits", "1");
144 185
		if (!xml.contains("gps-serialPortParities"))
145 186
			xml.putProperty("gps-serialPortParities", "none");
146 187
		
147
		portSpeed = xml.getIntProperty("gps-serialPortSpeed");
148
		dataBits = xml.getIntProperty("gps-serialPortDataBits");
188
		setPortSpeed(xml.getIntProperty("gps-serialPortSpeed"));
189
		setPortDataBits(xml.getIntProperty("gps-serialPortDataBits"));
149 190
		setPortStopBits(xml.getStringProperty("gps-serialPortStopBits"));
150 191
		setPortParity(xml.getStringProperty("gps-serialPortParities"));
151
		initialize();
152 192
	}
153 193

  
194
	public static GPSControlPanel getInstance() {
195
		if (instance == null)
196
			instance = new GPSControlPanel();
197
		return instance;
198
	}
199

  
154 200
	/**
155 201
	 * This method initializes this
156 202
	 * 
157 203
	 * @return void
158 204
	 */
159 205
	private void initialize() {
160
		lblPosValue = new JLabel();
161
		lblPosValue.setBounds(156, 25, 223, 20);
162
		lblPosValue.setFont(new java.awt.Font("MS Sans Serif", java.awt.Font.BOLD, 11));
163
		lblPos = new JLabel();
164
		lblPos.setText("Pos:");
165
		lblPos.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
166
		lblPos.setBounds(10, 25, 143, 20);
167 206
		this.setLayout(null);
168
		this.setSize(391, 433);
207
		this.setSize(572, 433);
169 208
		this.add(getCenterPanel(), java.awt.BorderLayout.CENTER);
170 209
		this.add(getNorthPanel(), java.awt.BorderLayout.NORTH);
171 210
		this.add(getBtnStart(), null);
......
175 214
	}
176 215

  
177 216
	public ViewInfo getViewInfo() {
178
		ViewInfo m_viewInfo = new ViewInfo(ViewInfo.ICONIFIABLE |
179
				ViewInfo.MAXIMIZABLE | ViewInfo.RESIZABLE);
217
		ViewInfo m_viewInfo = new ViewInfo(ViewInfo.ICONIFIABLE);
180 218
		m_viewInfo.setTitle(PluginServices.getText(this, "gps_control_panel"));
181 219
		m_viewInfo.setWidth(this.getWidth()+8);
182 220
		m_viewInfo.setHeight(this.getHeight()+8);
......
197 235
								  null, PluginServices.getText(this, "gps_status"),
198 236
								  javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, 
199 237
								  javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
200
			centerPanel.setSize(383, 291);
201
			centerPanel.setLocation(5, 111);
238
			centerPanel.setSize(572, 291);
239
			centerPanel.setLocation(0, 111);
202 240
			centerPanel.add(getJScrollPane(), java.awt.BorderLayout.NORTH);
203 241
			centerPanel.add(getBtnConfig(), null);
204 242
		}
......
214 252
		if (northPanel == null) {
215 253
			northPanel = new JPanel();
216 254
			northPanel.setLayout(null);
217
			northPanel.setSize(385, 105);
218
			northPanel.setLocation(0, 2);
219
			northPanel.add(lblPos, null);
220
			northPanel.add(lblPosValue, null);
221
			northPanel.add(lblPort, null);
255
			northPanel.setSize(572, 112);
256
			northPanel.setLocation(0, 0);
222 257
		}
223 258
		return northPanel;
224 259
	}
......
238 273
	private JScrollPane getJScrollPane() {
239 274
		if (jScrollPane == null) {
240 275
			jScrollPane = new JScrollPane();
241
			jScrollPane.setSize(370, 231);
276
			jScrollPane.setSize(560, 231);
242 277
			jScrollPane.setLocation(5, 25);
243 278
			jScrollPane.setViewportView(getTblStatus());
244 279
		}
......
253 288
	private JTable getTblStatus() {
254 289
		if (tblStatus == null) {
255 290
			tblStatus = new JTable();
256
			TableColumn column = null;
257
			for (int i = 0; i < tblStatus.getColumnCount(); i++) {
258
			    column = tblStatus.getColumnModel().getColumn(i);
259
			    int width = (int) (tblStatus.getWidth()*0.3);
260
			    if (i==0) {
261
			    	column.setWidth(width); 			    	
262
			    } else {
263
			    	column.setWidth(tblStatus.getWidth()-width);
264
			    }
265
			}
266
			tblStatus.setTableHeader(null);
291
			tblStatus.setShowHorizontalLines(false);
292
			
267 293
			model = new StatusTableModel();
268 294
			tblStatus.setModel(model);
269 295
		}
......
279 305
		if (btnStart == null) {
280 306
			btnStart = new JButton();
281 307
			btnStart.setText("start");
282
			btnStart.setBounds(75, 405, 70, 20);
308
			btnStart.setBounds(214, 407, 70, 20);
283 309
			btnStart.addActionListener(new java.awt.event.ActionListener() { 
284 310
				public void actionPerformed(java.awt.event.ActionEvent e) {    
285 311
					start();
......
297 323
	private JButton getBtnPause() {
298 324
		if (btnPause == null) {
299 325
			btnPause = new JButton();
300
			btnPause.setBounds(150, 405, 70, 20);
301
			btnPause.setText("sause");
326
			btnPause.setBounds(289, 407, 70, 20);
327
			btnPause.setText("pause");
302 328
			btnPause.addActionListener(new java.awt.event.ActionListener() { 
303 329
				public void actionPerformed(java.awt.event.ActionEvent e) {    
304 330
					gps.silence();
......
316 342
	private JButton getBtnStop() {
317 343
		if (btnStop == null) {
318 344
			btnStop = new JButton();
319
			btnStop.setBounds(270, 405, 70, 21);
345
			btnStop.setBounds(409, 407, 70, 21);
320 346
			btnStop.setText("stop");
321 347
			btnStop.addActionListener(new java.awt.event.ActionListener() { 
322 348
				public void actionPerformed(java.awt.event.ActionEvent e) {    
323 349
					gps.closePort();
324 350
				}
325 351
			});
352
			alreadyStarted = false;
326 353
		}
327 354
		return btnStop;
328 355
	}
356

  
357
	protected void refresh() {
358
		this.repaint();
359
	}
329 360
	
330 361
	public void start() {
331 362
		if (!alreadyStarted && port!=null) {
......
334 365
				gps.setPort(port, portSpeed, dataBits, stopBits, parity); 
335 366
				
336 367
				gps.addEventListener(new GPSEventListener() {
337
					
368
					private TableModelEvent e = new TableModelEvent(model);
338 369
					public void unhandledMessage(String message) {
339
						System.out.println("unhandledMessage"+": "+message+"\n");
340
						lblPosValue.setText(message);
370
						model.putValue(kMsg, message);
371
						tblStatus.tableChanged(e);
341 372
					}
342 373
					
343 374
					public void connectionLost() {
344
						System.out.println("connectionLost");
375
						model.putValue(kStatus, PluginServices.getText(this, "off"));
376
						tblStatus.tableChanged(e);
345 377
					}
346 378
					
347 379
					public void connectionEstablished() {
348
						System.out.println("connectionEstablished");
380
						model.putValue(kStatus, PluginServices.getText(this, "on"));
381
						tblStatus.tableChanged(e);
349 382
					}
350 383
					
351 384
					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);
385
						Point2D p = new Point2D.Double(lon, lat);
386
						model.putValue(kPos, "("+lon+", "+lat+")");
387
						tblStatus.tableChanged(e);	
388
						// esto igual ho hauria de canviar.
389
						((GPSExtension) thisExtension).drawSymbol(null, null, p);
362 390
					}
363 391
					
364 392
					public void signalLevelChanged(float level) {
393
						model.putValue(kSignalLevel , level+"");
394
						tblStatus.tableChanged(e);
365 395
					}
366 396
					
367 397
					public void speedChanged(float speed, short course) {
398
						model.putValue(kSpeed, speed+"");
399
						model.putValue(kCourse, course+"?");
400
						tblStatus.tableChanged(e);
368 401
					}
369 402
					
370
					public void estimatedPosErrorChanged(double f) {
403
					public void estimatedPosErrorChanged(double err) {
404
						model.putValue(kEstErr, err+"");
405
						tblStatus.tableChanged(e);
371 406
					}
372 407
					
373 408
					public void heightChanged(float height) {
409
						model.putValue(kHeight, height+"");
410
						tblStatus.tableChanged(e);
374 411
					}
375 412
					
376 413
					public void precisionChanged(float pDop, float hDop, float vDop) {
414
						model.putValue(kPosPrecision, pDop+"");
415
						model.putValue(kHorPrecision, hDop+"");
416
						model.putValue(kVerPrecision, vDop+"");
417
						tblStatus.tableChanged(e);
377 418
					}
378 419
					
379 420
				});
......
394 435
		if (btnConfig == null) {
395 436
			btnConfig = new JButton();
396 437
			btnConfig.setBounds(265, 263, 70, 20);
438
			btnConfig.addActionListener(new java.awt.event.ActionListener() { 
439
				public void actionPerformed(java.awt.event.ActionEvent e) {    
440
					config();
441
				}
442
			});
397 443
			btnConfig.setText(PluginServices.getText(this, "config"));
398 444
		}
399 445
		return btnConfig;
400 446
	}
401 447

  
402 448
	/**
449
	 * Opens the config dialog.
450
	 */
451
	protected void config() {
452
		PluginServices.getMDIManager().addView(new GPSConfigPanel(this));		
453
	}
454

  
455
	/**
403 456
	 * Sets the port to be monitorized.
404 457
	 * @param port
405 458
	 */
406 459
	public void setPort(CommPortIdentifier port) {
460
		XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
461
		xml.putProperty("gps-serialPortName", port.getName());
407 462
		this.port = port;
408 463
	}
409 464

  
......
468 523
			this.parity = SerialPort.PARITY_SPACE;
469 524
		}
470 525
	}
471
} 
472 526

  
527
	public Object getViewModel() {
528
		return "GPS Control Panel";
529
	}
530
}  //  @jve:decl-index=0:visual-constraint="10,10" 
531

  
532

  
473 533
class StatusTableModel extends AbstractTableModel {
474
	private String[] keys, values;
534
	private ArrayList keys = new ArrayList();
535
	private ArrayList values = new ArrayList();
536
	private Hashtable index = new Hashtable();
475 537
	
476
	public void setTableData(String[] keys, String[] values) {
477
		this.keys = keys;
478
		this.values = values;
538
	public void putRow(int rowIndex, String[] cellValues) {
539
		keys.set(rowIndex, cellValues[0]);
540
		values.set(rowIndex, cellValues[1]);
479 541
	}
480 542
	
543
	public int putValue(String key, String value) {
544
		if (index.containsKey(key)) {
545
			int i = ((Integer)index.get(key)).intValue();
546
			values.set(i, value);
547
			return i;
548
		}
549
		return addRow(new String[] {key, value});
550
	}
551
	
552
	
553
	public void clear() {
554
		index.clear();
555
		keys.clear();
556
		values.clear();
557
	}
558
	
559
	public int addRow(String[] cellValues) {
560
		index.put(cellValues[0], new Integer(keys.size()));
561
		keys.add(cellValues[0]);
562
		values.add(cellValues[1]);
563
		return keys.size()-1;
564
	}
565
	
481 566
	public int getColumnCount() {
482 567
		return 2;
483 568
	}
484 569

  
485 570
	public int getRowCount() {
486 571
		if (keys==null) return 0;
487
		return keys.length;
572
		return keys.size();
488 573
	}
489 574

  
490 575
	public Object getValueAt(int rowIndex, int columnIndex) {
491 576
		if (columnIndex == 0) 
492
			return values[rowIndex];
577
			return keys.get(rowIndex);
493 578
		else if (columnIndex == 1)
494
			return keys[rowIndex];
579
			return values.get(rowIndex);
495 580
		return null;
496 581
	}
497 582
	

Also available in: Unified diff