Statistics
| Revision:

root / trunk / extensions / extGPS / src / org / gvsig / gps / panel / GPSControlPanel.java @ 4768

History | View | Annotate | Download (16.1 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41

    
42
/* CVS MESSAGES:
43
 *
44
 * $Id: GPSControlPanel.java 4768 2006-04-07 12:45:55Z jaume $
45
 * $Log$
46
 * Revision 1.10  2006-04-07 12:45:55  jaume
47
 * *** empty log message ***
48
 *
49
 * Revision 1.9  2006/04/07 11:10:26  jaume
50
 * *** empty log message ***
51
 *
52
 * Revision 1.8  2006/04/07 08:27:48  jaume
53
 * *** empty log message ***
54
 *
55
 * Revision 1.7  2006/04/06 10:35:48  jaume
56
 * *** empty log message ***
57
 *
58
 * Revision 1.5  2006/04/05 17:08:18  jaume
59
 * *** empty log message ***
60
 *
61
 * Revision 1.4  2006/04/05 16:02:09  jaume
62
 * *** empty log message ***
63
 *
64
 * Revision 1.3  2006/04/05 14:50:37  jaume
65
 * *** empty log message ***
66
 *
67
 * Revision 1.2  2006/04/03 17:10:03  jaume
68
 * *** empty log message ***
69
 *
70
 * Revision 1.1  2006/04/03 16:10:27  jaume
71
 * *** empty log message ***
72
 *
73
 *
74
 */
75
package org.gvsig.gps.panel;
76

    
77
import gnu.io.CommPortIdentifier;
78
import gnu.io.PortInUseException;
79
import gnu.io.SerialPort;
80

    
81
import java.awt.geom.Point2D;
82
import java.util.ArrayList;
83
import java.util.Enumeration;
84
import java.util.Hashtable;
85

    
86
import javax.swing.DefaultCellEditor;
87
import javax.swing.JButton;
88
import javax.swing.JComponent;
89
import javax.swing.JLabel;
90
import javax.swing.JOptionPane;
91
import javax.swing.JPanel;
92
import javax.swing.JScrollPane;
93
import javax.swing.JTable;
94
import javax.swing.ListSelectionModel;
95
import javax.swing.event.TableColumnModelListener;
96
import javax.swing.event.TableModelEvent;
97
import javax.swing.table.AbstractTableModel;
98
import javax.swing.table.DefaultTableColumnModel;
99
import javax.swing.table.JTableHeader;
100
import javax.swing.table.TableCellRenderer;
101
import javax.swing.table.TableColumn;
102
import javax.swing.table.TableColumnModel;
103

    
104
import org.gvsig.gps.GPSDriver;
105
import org.gvsig.gps.GPSExtension;
106
import org.gvsig.gps.listeners.GPSEventListener;
107

    
108
import com.iver.andami.PluginServices;
109
import com.iver.andami.plugins.Extension;
110
import com.iver.andami.ui.mdiManager.SingletonView;
111
import com.iver.andami.ui.mdiManager.ViewInfo;
112
import com.iver.utiles.XMLEntity;
113
/**
114
 * Singleton view that allows the user to monitorize and configure the
115
 * status of the GPS.
116
 * 
117
 * @author jaume dominguez faus - jaume.dominguez@iver.es
118
 */
119
public class GPSControlPanel extends JPanel implements SingletonView {
120
        private boolean alreadyStarted;
121
        private GPSDriver gps;
122
    private JPanel centerPanel = null;
123
        private JPanel northPanel = null;
124
        private JScrollPane jScrollPane = null;
125
        private JTable tblStatus = null;
126
        private StatusTableModel model;
127
        private JButton btnStart = null;
128
        private JButton btnPause = null;
129
        private JButton btnStop = null;
130
        private JLabel lblPos;
131
        private JButton btnConfig = null;
132
        private CommPortIdentifier port;
133
        private int portSpeed;
134
        private int dataBits;
135
        private int stopBits;
136
        private int parity;
137
        
138
        private final static Extension thisExtension = PluginServices.getExtension(GPSExtension.class);
139
        private final static String kPos = PluginServices.getText(thisExtension, "UTM_position");
140
        private final static String kMsg = PluginServices.getText(thisExtension, "unknown_message");
141
        private final static String kSignalLevel = PluginServices.getText(thisExtension, "signal_stregth");
142
        private final static String kStatus = PluginServices.getText(thisExtension, "status");
143
        private final static String kSpeed = PluginServices.getText(thisExtension, "speed");
144
        private final static String kCourse = PluginServices.getText(thisExtension, "course");
145
        private final static String kEstErr = PluginServices.getText(thisExtension, "estimated_error");
146
        private final static String kHeight = PluginServices.getText(thisExtension, "height");
147
        private final static String kPosPrecision = PluginServices.getText(thisExtension, "position_precision");
148
        private final static String kHorPrecision = PluginServices.getText(thisExtension, "horizontal_precision");
149
        private final static String kVerPrecision = PluginServices.getText(thisExtension, "vertical_precision");
150
        
151
        
152
        private static GPSControlPanel instance;
153
        
154

    
155
        private GPSControlPanel() {
156
                super();
157
                gps = GPSDriver.getInstance();
158
                
159
                setUp();
160
                initialize();
161
                refreshTable();
162
        }
163
        
164
        private void refreshTable() {
165
                model.putValue(kPos , PluginServices.getText(this, "unknown"));
166
                model.putValue(PluginServices.getText(this, "port"), (port!=null) ? port.getName() : PluginServices.getText(this, "not_yet_set"));
167
                model.putValue(PluginServices.getText(this, "speed")+" (bps)", portSpeed+"");
168
                
169
        }
170

    
171
        private void setUp() {
172
                XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
173
                // Try to restore last port used
174
                String portName = null;
175
                if (xml.contains("gps-serialPortName")) {
176
                        portName = xml.getStringProperty("gps-serialPortName");
177
                }
178
                Enumeration portList = CommPortIdentifier.getPortIdentifiers();
179
                while (portName!=null && portList.hasMoreElements()) {
180
                        CommPortIdentifier myPort = (CommPortIdentifier) portList.nextElement();
181
                        if (portName.equals(myPort.getName())) {
182
                                setPort(myPort);
183
                        }
184
                }
185
                if (!xml.contains("gps-serialPortSpeed"))
186
                        xml.putProperty("gps-serialPortSpeed", 4800);
187
                if (!xml.contains("gps-serialPortDataBits"))
188
                        xml.putProperty("gps-serialPortDataBits" , "8");
189
                if (!xml.contains("gps-serialPortStopBits")) 
190
                        xml.putProperty("gps-serialPortStopBits", "1");
191
                if (!xml.contains("gps-serialPortParities"))
192
                        xml.putProperty("gps-serialPortParities", "none");
193
                
194
                setPortSpeed(xml.getIntProperty("gps-serialPortSpeed"));
195
                setPortDataBits(xml.getIntProperty("gps-serialPortDataBits"));
196
                setPortStopBits(xml.getStringProperty("gps-serialPortStopBits"));
197
                setPortParity(xml.getStringProperty("gps-serialPortParities"));
198
        }
199

    
200
        public static GPSControlPanel getInstance() {
201
                if (instance == null)
202
                        instance = new GPSControlPanel();
203
                return instance;
204
        }
205

    
206
        /**
207
         * This method initializes this
208
         * 
209
         * @return void
210
         */
211
        private void initialize() {
212
                this.setLayout(null);
213
                this.setSize(572, 433);
214
                this.add(getCenterPanel(), java.awt.BorderLayout.CENTER);
215
                this.add(getNorthPanel(), java.awt.BorderLayout.NORTH);
216
                this.add(getBtnStart(), null);
217
                this.add(getBtnPause(), null);
218
                this.add(getBtnStop(), null);
219
                
220
        }
221

    
222
        public ViewInfo getViewInfo() {
223
                ViewInfo m_viewInfo = new ViewInfo(ViewInfo.ICONIFIABLE);
224
                m_viewInfo.setTitle(PluginServices.getText(this, "gps_control_panel"));
225
                m_viewInfo.setWidth(this.getWidth()+8);
226
                m_viewInfo.setHeight(this.getHeight()+8);
227
                
228
                return m_viewInfo;
229
        }
230

    
231
        /**
232
         * This method initializes southPanel        
233
         *         
234
         * @return javax.swing.JPanel        
235
         */    
236
        private JPanel getCenterPanel() {
237
                if (centerPanel == null) {
238
                        centerPanel = new JPanel();
239
                        centerPanel.setLayout(null);
240
                        centerPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
241
                                                                  null, PluginServices.getText(this, "gps_status"),
242
                                                                  javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, 
243
                                                                  javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
244
                        centerPanel.setSize(572, 261);
245
                        centerPanel.setLocation(0, 141);
246
                        centerPanel.add(getJScrollPane(), java.awt.BorderLayout.NORTH);
247
                        centerPanel.add(getBtnConfig(), null);
248
                }
249
                return centerPanel;
250
        }
251

    
252
        /**
253
         * This method initializes northPanel        
254
         *         
255
         * @return javax.swing.JPanel        
256
         */    
257
        private JPanel getNorthPanel() {
258
                if (northPanel == null) {
259
                        northPanel = new JPanel();
260
                        northPanel.setLayout(null);
261
                        northPanel.setSize(572, 142);
262
                        northPanel.setLocation(0, 0);
263
                }
264
                return northPanel;
265
        }
266
        
267
        /**
268
         * This method initializes txtSampleRate        
269
         *         
270
         * @return javax.swing.JTextField        
271
         */    
272
        
273

    
274
        /**
275
         * This method initializes jScrollPane        
276
         *         
277
         * @return javax.swing.JScrollPane        
278
         */    
279
        private JScrollPane getJScrollPane() {
280
                if (jScrollPane == null) {
281
                        jScrollPane = new JScrollPane();
282
                        jScrollPane.setSize(560, 202);
283
                        jScrollPane.setLocation(5, 25);
284
                        jScrollPane.setViewportView(getTblStatus());
285
                }
286
                return jScrollPane;
287
        }
288

    
289
        /**
290
         * This method initializes tblStatus        
291
         *         
292
         * @return javax.swing.JTable        
293
         */    
294
        private JTable getTblStatus() {
295
                if (tblStatus == null) {
296
                        tblStatus = new JTable();
297
                        tblStatus.setShowHorizontalLines(false);
298
                        
299
                        model = new StatusTableModel();
300
                        tblStatus.setModel(model);
301
                }
302
                return tblStatus;
303
        }
304

    
305
        /**
306
         * This method initializes btnStart        
307
         *         
308
         * @return javax.swing.JButton        
309
         */    
310
        private JButton getBtnStart() {
311
                if (btnStart == null) {
312
                        btnStart = new JButton();
313
                        btnStart.setText("start");
314
                        btnStart.setBounds(214, 405, 70, 25);
315
                        btnStart.addActionListener(new java.awt.event.ActionListener() { 
316
                                public void actionPerformed(java.awt.event.ActionEvent e) {    
317
                                        start();
318
                                }
319
                        });
320
                }
321
                return btnStart;
322
        }
323

    
324
        /**
325
         * This method initializes btnPause        
326
         *         
327
         * @return javax.swing.JButton        
328
         */    
329
        private JButton getBtnPause() {
330
                if (btnPause == null) {
331
                        btnPause = new JButton();
332
                        btnPause.setBounds(289, 405, 70, 25);
333
                        btnPause.setText("pause");
334
                        btnPause.addActionListener(new java.awt.event.ActionListener() { 
335
                                public void actionPerformed(java.awt.event.ActionEvent e) {    
336
                                        gps.silence();
337
                                }
338
                        });
339
                }
340
                return btnPause;
341
        }
342

    
343
        /**
344
         * This method initializes btnStop        
345
         *         
346
         * @return javax.swing.JButton        
347
         */    
348
        private JButton getBtnStop() {
349
                if (btnStop == null) {
350
                        btnStop = new JButton();
351
                        btnStop.setBounds(409, 405, 70, 25);
352
                        btnStop.setText("stop");
353
                        btnStop.addActionListener(new java.awt.event.ActionListener() { 
354
                                public void actionPerformed(java.awt.event.ActionEvent e) {    
355
                                        gps.closePort();
356
                                }
357
                        });
358
                        alreadyStarted = false;
359
                }
360
                return btnStop;
361
        }
362

    
363
        protected void refresh() {
364
                this.repaint();
365
        }
366
        
367
        public void start() {
368
                if (!alreadyStarted && port!=null) {
369
                        try {
370
                                
371
                                gps.setPort(port, portSpeed, dataBits, stopBits, parity); 
372
                                
373
                                gps.addEventListener(new GPSEventListener() {
374
                                        private TableModelEvent e = new TableModelEvent(model);
375
                                        public void unhandledMessage(String message) {
376
                                                model.putValue(kMsg, message);
377
                                                tblStatus.tableChanged(e);
378
                                        }
379
                                        
380
                                        public void connectionLost() {
381
                                                model.putValue(kStatus, PluginServices.getText(this, "off"));
382
                                                tblStatus.tableChanged(e);
383
                                        }
384
                                        
385
                                        public void connectionEstablished() {
386
                                                model.putValue(kStatus, PluginServices.getText(this, "on"));
387
                                                tblStatus.tableChanged(e);
388
                                        }
389
                                        
390
                                        public void newLonLatPositionReceived(double lon, double lat) {
391
                                                Point2D p = new Point2D.Double(lon, lat);
392
                                                model.putValue(kPos, "("+lon+", "+lat+")");
393
                                                tblStatus.tableChanged(e);        
394
                                                // esto igual ho hauria de canviar.
395
                                                ((GPSExtension) thisExtension).drawSymbol(null, null, p, false);
396
                                        }
397
                                        
398
                                        public void signalLevelChanged(float level) {
399
                                                model.putValue(kSignalLevel , level+"");
400
                                                tblStatus.tableChanged(e);
401
                                        }
402
                                        
403
                                        public void speedChanged(float speed, short course) {
404
                                                model.putValue(kSpeed, speed+"");
405
                                                model.putValue(kCourse, course+"?");
406
                                                tblStatus.tableChanged(e);
407
                                        }
408
                                        
409
                                        public void estimatedPosErrorChanged(double err) {
410
                                                model.putValue(kEstErr, err+"");
411
                                                tblStatus.tableChanged(e);
412
                                        }
413
                                        
414
                                        public void heightChanged(float height) {
415
                                                model.putValue(kHeight, height+"");
416
                                                tblStatus.tableChanged(e);
417
                                        }
418
                                        
419
                                        public void precisionChanged(float pDop, float hDop, float vDop) {
420
                                                model.putValue(kPosPrecision, pDop+"");
421
                                                model.putValue(kHorPrecision, hDop+"");
422
                                                model.putValue(kVerPrecision, vDop+"");
423
                                                tblStatus.tableChanged(e);
424
                                        }
425
                                        
426
                                });
427
                                gps.start();
428
                                alreadyStarted = true;
429
                        } catch (PortInUseException e) {
430
                                JOptionPane.showMessageDialog(this, PluginServices.getText(this, "port_in_use"));
431
                        }
432
                }
433
        }
434

    
435
        /**
436
         * This method initializes jButton        
437
         *         
438
         * @return javax.swing.JButton        
439
         */    
440
        private JButton getBtnConfig() {
441
                if (btnConfig == null) {
442
                        btnConfig = new JButton();
443
                        btnConfig.setBounds(495, 232, 70, 20);
444
                        btnConfig.addActionListener(new java.awt.event.ActionListener() { 
445
                                public void actionPerformed(java.awt.event.ActionEvent e) {    
446
                                        config();
447
                                }
448
                        });
449
                        btnConfig.setText(PluginServices.getText(this, "config"));
450
                }
451
                return btnConfig;
452
        }
453

    
454
        /**
455
         * Opens the config dialog.
456
         */
457
        protected void config() {
458
                PluginServices.getMDIManager().addView(new GPSConfigPanel(this));                
459
        }
460

    
461
        /**
462
         * Sets the port to be monitorized.
463
         * @param port
464
         */
465
        public void setPort(CommPortIdentifier port) {
466
                XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
467
                xml.putProperty("gps-serialPortName", port.getName());
468
                this.port = port;
469
        }
470

    
471
        /**
472
         * Sets the speed in bps of the port that is being monitorized.
473
         * @param portSpeed
474
         */
475
        public void setPortSpeed(int portSpeed) {
476
                this.portSpeed = portSpeed;
477
        }
478

    
479
        /**
480
         * Sets the amount of data bits of the port that is being monitorized.
481
         * @param int, one of 5, 6, 7, or 8
482
         */
483
        public void setPortDataBits(int dataBits) {
484
                switch (dataBits) {
485
                case 5:
486
                        this.dataBits = SerialPort.DATABITS_5;
487
                        break;
488
                case 6:
489
                        this.dataBits = SerialPort.DATABITS_6;
490
                        break;
491
                case 7:
492
                        this.dataBits = SerialPort.DATABITS_7;
493
                        break;
494
                case 8:
495
                        this.dataBits = SerialPort.DATABITS_8;
496
                        break;
497
                }
498
                
499
        }
500
        
501
        /**
502
         * Sets the port's stop bits
503
         * @param stopBIts, one of "1", "1.5", or "2"
504
         */
505
        public void setPortStopBits(String stopBits) {
506
                if (stopBits.equals("1")) {
507
                        this.stopBits = SerialPort.STOPBITS_1;
508
                } else if (stopBits.equals("1.5")) {
509
                        this.stopBits = SerialPort.STOPBITS_1_5;
510
                } else if (stopBits.equals("2")) {
511
                        this.stopBits = SerialPort.STOPBITS_2;
512
                }
513
        }
514

    
515
        /**
516
         * Sets the parity of the port that is being monitorized
517
         * @param string: one of "even", "mark", "none", "odd", or "space"
518
         */
519
        public void setPortParity(String parity) {
520
                if (parity.equals("even")) {
521
                        this.parity = SerialPort.PARITY_EVEN;
522
                } else if (parity.equals("mark")) {
523
                        this.parity = SerialPort.PARITY_MARK;
524
                } else if (parity.equals("none")) {
525
                        this.parity = SerialPort.PARITY_NONE;
526
                } else if (parity.equals("odd")) {
527
                        this.parity = SerialPort.PARITY_ODD;
528
                } else if (parity.equals("space")) {
529
                        this.parity = SerialPort.PARITY_SPACE;
530
                }
531
        }
532

    
533
        public Object getViewModel() {
534
                return "GPS Control Panel";
535
        }
536
}  //  @jve:decl-index=0:visual-constraint="10,10" 
537

    
538

    
539
class StatusTableModel extends AbstractTableModel {
540
        private ArrayList keys = new ArrayList();
541
        private ArrayList values = new ArrayList();
542
        private Hashtable index = new Hashtable();
543
        
544
        public void putRow(int rowIndex, String[] cellValues) {
545
                keys.set(rowIndex, cellValues[0]);
546
                values.set(rowIndex, cellValues[1]);
547
        }
548
        
549
        public int putValue(String key, String value) {
550
                if (index.containsKey(key)) {
551
                        int i = ((Integer)index.get(key)).intValue();
552
                        values.set(i, value);
553
                        return i;
554
                }
555
                return addRow(new String[] {key, value});
556
        }
557
        
558
        
559
        public void clear() {
560
                index.clear();
561
                keys.clear();
562
                values.clear();
563
        }
564
        
565
        public int addRow(String[] cellValues) {
566
                index.put(cellValues[0], new Integer(keys.size()));
567
                keys.add(cellValues[0]);
568
                values.add(cellValues[1]);
569
                return keys.size()-1;
570
        }
571
        
572
        public int getColumnCount() {
573
                return 2;
574
        }
575

    
576
        public int getRowCount() {
577
                if (keys==null) return 0;
578
                return keys.size();
579
        }
580

    
581
        public Object getValueAt(int rowIndex, int columnIndex) {
582
                if (columnIndex == 0) 
583
                        return keys.get(rowIndex);
584
                else if (columnIndex == 1)
585
                        return values.get(rowIndex);
586
                return null;
587
        }
588
        
589

    
590
        
591
}
592