Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGPS / src / org / gvsig / gps / panel / GPSControlPanel.java @ 4829

History | View | Annotate | Download (17.8 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 4829 2006-04-12 10:03:32Z jaume $
45
 * $Log$
46
 * Revision 1.17  2006-04-12 10:03:32  jaume
47
 * *** empty log message ***
48
 *
49
 * Revision 1.16  2006/04/12 06:51:56  jaume
50
 * *** empty log message ***
51
 *
52
 * Revision 1.14  2006/04/11 13:19:51  jaume
53
 * *** empty log message ***
54
 *
55
 * Revision 1.13  2006/04/10 17:13:28  jaume
56
 * *** empty log message ***
57
 *
58
 * Revision 1.12  2006/04/10 14:09:46  jaume
59
 * *** empty log message ***
60
 *
61
 * Revision 1.11  2006/04/10 11:21:52  jaume
62
 * *** empty log message ***
63
 *
64
 * Revision 1.10  2006/04/07 12:45:55  jaume
65
 * *** empty log message ***
66
 *
67
 * Revision 1.9  2006/04/07 11:10:26  jaume
68
 * *** empty log message ***
69
 *
70
 * Revision 1.8  2006/04/07 08:27:48  jaume
71
 * *** empty log message ***
72
 *
73
 * Revision 1.7  2006/04/06 10:35:48  jaume
74
 * *** empty log message ***
75
 *
76
 * Revision 1.5  2006/04/05 17:08:18  jaume
77
 * *** empty log message ***
78
 *
79
 * Revision 1.4  2006/04/05 16:02:09  jaume
80
 * *** empty log message ***
81
 *
82
 * Revision 1.3  2006/04/05 14:50:37  jaume
83
 * *** empty log message ***
84
 *
85
 * Revision 1.2  2006/04/03 17:10:03  jaume
86
 * *** empty log message ***
87
 *
88
 * Revision 1.1  2006/04/03 16:10:27  jaume
89
 * *** empty log message ***
90
 *
91
 *
92
 */
93
package org.gvsig.gps.panel;
94

    
95
import gnu.io.CommPortIdentifier;
96
import gnu.io.PortInUseException;
97
import gnu.io.SerialPort;
98

    
99
import java.awt.geom.Point2D;
100
import java.util.ArrayList;
101
import java.util.Enumeration;
102
import java.util.Hashtable;
103

    
104
import javax.swing.JButton;
105
import javax.swing.JLabel;
106
import javax.swing.JOptionPane;
107
import javax.swing.JPanel;
108
import javax.swing.JScrollPane;
109
import javax.swing.JTable;
110
import javax.swing.event.TableModelEvent;
111
import javax.swing.table.AbstractTableModel;
112

    
113
import org.gvsig.gps.GPSDriver;
114
import org.gvsig.gps.GPSExtension;
115
import org.gvsig.gps.listeners.GPSEventListener;
116

    
117
import com.iver.andami.PluginServices;
118
import com.iver.andami.plugins.Extension;
119
import com.iver.andami.ui.mdiManager.SingletonView;
120
import com.iver.andami.ui.mdiManager.ViewInfo;
121
import com.iver.utiles.XMLEntity;
122
import com.sun.media.jai.util.Rational;
123
/**
124
 * Singleton view that allows the user to monitorize and configure the
125
 * status of the GPS.
126
 * 
127
 * @author jaume dominguez faus - jaume.dominguez@iver.es
128
 */
129
public class GPSControlPanel extends JPanel implements SingletonView {
130
        private boolean alreadyStarted;
131
        private GPSDriver gps;
132
    private JPanel centerPanel = null;
133
        private JPanel northPanel = null;
134
        private JScrollPane jScrollPane = null;
135
        private JTable tblStatus = null;
136
        private StatusTableModel model;
137
        private JButton btnStart = null;
138
        private JButton btnPause = null;
139
        private JButton btnStop = null;
140
        private JButton btnConfig = null;
141
        private CommPortIdentifier port;
142
        private int portSpeed;
143
        private int dataBits;
144
        private int stopBits;
145
        private int parity;
146
        private boolean paused;
147
        
148
        private final static Extension thisExtension = PluginServices.getExtension(GPSExtension.class);
149
        private final static String kPos = PluginServices.getText(thisExtension, "GEO_position");
150
        private final static String kPort = PluginServices.getText(thisExtension, "port");
151
        private final static String kPortSpeed = PluginServices.getText(thisExtension, "port_speed");
152
        private final static String kMsg = PluginServices.getText(thisExtension, "unknown_message");
153
        private final static String kSignalLevel = PluginServices.getText(thisExtension, "signal_stregth");
154
        private final static String kStatus = PluginServices.getText(thisExtension, "status");
155
        private final static String kSpeed = PluginServices.getText(thisExtension, "speed");
156
        private final static String kCourse = PluginServices.getText(thisExtension, "course");
157
        private final static String kEstErr = PluginServices.getText(thisExtension, "estimated_error");
158
        private final static String kAltitude = PluginServices.getText(thisExtension, "altitude");
159
        private final static String kPosPrecision = PluginServices.getText(thisExtension, "position_precision");
160
        private final static String kHorPrecision = PluginServices.getText(thisExtension, "horizontal_precision");
161
        private final static String kVerPrecision = PluginServices.getText(thisExtension, "vertical_precision");
162
        private final static String kSatellitesInView = PluginServices.getText(thisExtension, "satellites_in_view");
163
        
164
        private static GPSControlPanel instance;
165
        
166

    
167
        private GPSControlPanel() {
168
                super();
169
                gps = GPSDriver.getInstance();
170
                
171
                initialize();
172
                refreshTable();
173
                setUp();
174
        }
175
        
176
        private void refreshTable() {
177
                model.putValue(kPos , PluginServices.getText(this, "unknown"));
178
                model.putValue(kPort, (port!=null) ? port.getName() : PluginServices.getText(this, "not_yet_set"));
179
                model.putValue(kPortSpeed, portSpeed+"");
180
                
181
        }
182

    
183
        private void setUp() {
184
                XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
185
                // Try to restore last port used
186
                String portName = null;
187
                if (xml.contains("gps-serialPortName")) {
188
                        portName = xml.getStringProperty("gps-serialPortName");
189
                }
190
                Enumeration portList = CommPortIdentifier.getPortIdentifiers();
191
                while (portName!=null && portList.hasMoreElements()) {
192
                        CommPortIdentifier myPort = (CommPortIdentifier) portList.nextElement();
193
                        if (portName.equals(myPort.getName())) {
194
                                setPort(myPort);
195
                        }
196
                }
197
                if (!xml.contains("gps-serialPortSpeed"))
198
                        xml.putProperty("gps-serialPortSpeed", 4800);
199
                if (!xml.contains("gps-serialPortDataBits"))
200
                        xml.putProperty("gps-serialPortDataBits" , "8");
201
                if (!xml.contains("gps-serialPortStopBits")) 
202
                        xml.putProperty("gps-serialPortStopBits", "1");
203
                if (!xml.contains("gps-serialPortParities"))
204
                        xml.putProperty("gps-serialPortParities", "none");
205
                
206
                setPortSpeed(xml.getIntProperty("gps-serialPortSpeed"));
207
                setPortDataBits(xml.getIntProperty("gps-serialPortDataBits"));
208
                setPortStopBits(xml.getStringProperty("gps-serialPortStopBits"));
209
                setPortParity(xml.getStringProperty("gps-serialPortParities"));
210
        }
211

    
212
        
213

    
214
        public static GPSControlPanel getInstance() {
215
                if (instance == null)
216
                        instance = new GPSControlPanel();
217
                return instance;
218
        }
219

    
220
        /**
221
         * This method initializes this
222
         * 
223
         * @return void
224
         */
225
        private void initialize() {
226
                this.setLayout(null);
227
                this.setSize(572, 433);
228
                this.add(getCenterPanel(), java.awt.BorderLayout.CENTER);
229
                this.add(getNorthPanel(), java.awt.BorderLayout.NORTH);
230
                this.add(getBtnStart(), null);
231
                this.add(getBtnPause(), null);
232
                this.add(getBtnStop(), null);
233
                
234
        }
235

    
236
        public ViewInfo getViewInfo() {
237
                ViewInfo m_viewInfo = new ViewInfo(ViewInfo.ICONIFIABLE);
238
                m_viewInfo.setTitle(PluginServices.getText(this, "gps_control_panel"));
239
                m_viewInfo.setWidth(this.getWidth()+8);
240
                m_viewInfo.setHeight(this.getHeight()+8);
241
                
242
                return m_viewInfo;
243
        }
244

    
245
        /**
246
         * This method initializes southPanel        
247
         *         
248
         * @return javax.swing.JPanel        
249
         */    
250
        private JPanel getCenterPanel() {
251
                if (centerPanel == null) {
252
                        centerPanel = new JPanel();
253
                        centerPanel.setLayout(null);
254
                        centerPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
255
                                                                  null, PluginServices.getText(this, "gps_status"),
256
                                                                  javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, 
257
                                                                  javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
258
                        centerPanel.setSize(572, 261);
259
                        centerPanel.setLocation(0, 141);
260
                        centerPanel.add(getJScrollPane(), java.awt.BorderLayout.NORTH);
261
                        centerPanel.add(getBtnConfig(), null);
262
                }
263
                return centerPanel;
264
        }
265

    
266
        /**
267
         * This method initializes northPanel        
268
         *         
269
         * @return javax.swing.JPanel        
270
         */    
271
        private JPanel getNorthPanel() {
272
                if (northPanel == null) {
273
                        northPanel = new JPanel();
274
                        northPanel.setLayout(null);
275
                        northPanel.setSize(572, 142);
276
                        northPanel.setLocation(0, 0);
277
                }
278
                return northPanel;
279
        }        
280

    
281
        /**
282
         * This method initializes jScrollPane        
283
         *         
284
         * @return javax.swing.JScrollPane        
285
         */    
286
        private JScrollPane getJScrollPane() {
287
                if (jScrollPane == null) {
288
                        jScrollPane = new JScrollPane();
289
                        jScrollPane.setSize(560, 212);
290
                        jScrollPane.setLocation(5, 19);
291
                        jScrollPane.setViewportView(getTblStatus());
292
                }
293
                return jScrollPane;
294
        }
295

    
296
        /**
297
         * This method initializes tblStatus        
298
         *         
299
         * @return javax.swing.JTable        
300
         */    
301
        private JTable getTblStatus() {
302
                if (tblStatus == null) {
303
                        tblStatus = new JTable();
304
                        tblStatus.setShowHorizontalLines(false);
305
                        
306
                        model = new StatusTableModel();
307
                        tblStatus.setModel(model);
308
                }
309
                return tblStatus;
310
        }
311

    
312
        /**
313
         * This method initializes btnStart        
314
         *         
315
         * @return javax.swing.JButton        
316
         */    
317
        private JButton getBtnStart() {
318
                if (btnStart == null) {
319
                        btnStart = new JButton();
320
                        btnStart.setText("start");
321
                        btnStart.setBounds(214, 405, 70, 25);
322
                        btnStart.addActionListener(new java.awt.event.ActionListener() { 
323
                                public void actionPerformed(java.awt.event.ActionEvent e) {    
324
                                        start();
325
                                }
326
                        });
327
                }
328
                return btnStart;
329
        }
330

    
331
        /**
332
         * This method initializes btnPause        
333
         *         
334
         * @return javax.swing.JButton        
335
         */    
336
        private JButton getBtnPause() {
337
                if (btnPause == null) {
338
                        btnPause = new JButton();
339
                        btnPause.setBounds(289, 405, 70, 25);
340
                        btnPause.setText("pause");
341
                        paused = false;
342
                        btnPause.addActionListener(new java.awt.event.ActionListener() { 
343
                                public void actionPerformed(java.awt.event.ActionEvent e) {    
344
                                        pause();
345
                                }
346
                        });
347
                }
348
                return btnPause;
349
        }
350

    
351
        private void pause() {
352
                getBtnPause().setText((paused) ? PluginServices.getText(this, "pause") : PluginServices.getText(this, "resume") );
353
                if (paused)
354
                        gps.connect();
355
                else 
356
                        gps.silence();
357
                paused = !paused;
358
        }
359

    
360
        /**
361
         * This method initializes btnStop        
362
         *         
363
         * @return javax.swing.JButton        
364
         */    
365
        private JButton getBtnStop() {
366
                if (btnStop == null) {
367
                        btnStop = new JButton();
368
                        btnStop.setBounds(409, 405, 70, 25);
369
                        btnStop.setText("stop");
370
                        btnStop.addActionListener(new java.awt.event.ActionListener() { 
371
                                public void actionPerformed(java.awt.event.ActionEvent e) {  
372
                                        stop();
373
                                        
374
                                }
375
                        });
376
                        alreadyStarted = false;
377
                }
378
                return btnStop;
379
        }
380

    
381
        private void stop() {
382
                alreadyStarted = false;
383
                getBtnPause().setEnabled(false);
384
                getBtnPause().setText(PluginServices.getText(this, "pause"));
385
                getBtnStart().setEnabled(true);
386
                gps.close();
387
        }
388

    
389
        protected void refresh() {
390
                this.repaint();
391
        }
392
        
393
        public void start() {
394
                if (!alreadyStarted && port!=null) {
395
                        try {
396
                                
397
                                gps.setPort(port, portSpeed, dataBits, stopBits, parity); 
398
                                model.clear();
399
                                refreshTable();
400
                                paused = false;
401
                                getBtnPause().setText(PluginServices.getText(this, "pause"));
402
                                getBtnPause().setEnabled(true);
403
                                getBtnStart().setEnabled(false);
404
                                gps.addEventListener(new GPSEventListener() {
405
                                        private TableModelEvent e = new TableModelEvent(model);
406
                                        public void unhandledMessage(String message) {
407
                                                model.putValue(kMsg, message);
408
                                                tblStatus.tableChanged(e);
409
                                        }
410
                                        
411
                                        public void connectionLost() {
412
                                                model.putValue(kStatus, PluginServices.getText(this, "off"));
413
                                                tblStatus.tableChanged(e);
414
                                        }
415
                                        
416
                                        public void connectionEstablished() {
417
                                                model.putValue(kStatus, PluginServices.getText(this, "on"));
418
                                                tblStatus.tableChanged(e);
419
                                        }
420
                                        
421
                                        public void newLonLatPositionReceived(double lon, double lat) {
422
                                                Point2D p = new Point2D.Double(lon, lat);
423
                                                char cLon = (lon>=0D) ? 'E' : 'W';
424
                                                char cLat = (lat>=0D) ? 'N' : 'S';
425
                                                model.putValue(kPos, Math.abs(lat)+""+cLat+", "+Math.abs(lon)+cLon);
426
                                                tblStatus.tableChanged(e);        
427
                                                // esto igual ho hauria de canviar.
428
                                                ((GPSExtension) thisExtension).drawSymbol(null, null, p, false);
429
                                        }
430
                                        
431
                                        public void signalQualityChanged(float level, int satellites, String qualityStatus) {
432
                                                model.putValue(kSignalLevel , level+"");
433
                                                model.putValue(kSatellitesInView, satellites+"");
434
                                                tblStatus.tableChanged(e);
435
                                        }
436
                                        
437
                                        public void speedChanged(float speed, short course) {
438
                                                model.putValue(kSpeed, speed+"");
439
                                                model.putValue(kCourse, course+"?");
440
                                                tblStatus.tableChanged(e);
441
                                        }
442
                                        
443
                                        public void estimatedPosErrorChanged(double err) {
444
                                                model.putValue(kEstErr, err+"");
445
                                                tblStatus.tableChanged(e);
446
                                        }
447
                                        
448
                                        public void altitudeChanged(float height) {
449
                                                model.putValue(kAltitude, height+"");
450
                                                tblStatus.tableChanged(e);
451
                                        }
452
                                        
453
                                        public void precisionChanged(float pDop, float hDop, float vDop) {
454
                                                model.putValue(kPosPrecision, pDop+"");
455
                                                model.putValue(kHorPrecision, hDop+"");
456
                                                model.putValue(kVerPrecision, vDop+"");
457
                                                tblStatus.tableChanged(e);
458
                                        }
459
                                        
460
                                });
461
                                gps.connect();
462
                                alreadyStarted = true;
463
                        } catch (PortInUseException e) {
464
                                JOptionPane.showMessageDialog(this, PluginServices.getText(this, "port_in_use"));
465
                        }
466
                }
467
        }
468

    
469
        /**
470
         * This method initializes jButton        
471
         *         
472
         * @return javax.swing.JButton        
473
         */    
474
        private JButton getBtnConfig() {
475
                if (btnConfig == null) {
476
                        btnConfig = new JButton();
477
                        btnConfig.setBounds(479, 234, 86, 20);
478
                        btnConfig.addActionListener(new java.awt.event.ActionListener() { 
479
                                public void actionPerformed(java.awt.event.ActionEvent e) {    
480
                                        config();
481
                                }
482
                        });
483
                        btnConfig.setText(PluginServices.getText(this, "config"));
484
                }
485
                return btnConfig;
486
        }
487

    
488
        /**
489
         * Opens the config dialog.
490
         */
491
        protected void config() {
492
                PluginServices.getMDIManager().addView(new GPSConfigPanel(this));                
493
        }
494

    
495
        /**
496
         * Sets the port to be monitorized.
497
         * @param port
498
         */
499
        public void setPort(CommPortIdentifier port) {
500
                XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
501
                xml.putProperty("gps-serialPortName", port.getName());
502
                model.putValue(kPort, port.getName());
503
                this.port = port;
504
        }
505

    
506
        /**
507
         * Sets the speed in bps of the port that is being monitorized.
508
         * @param portSpeed
509
         */
510
        public void setPortSpeed(int portSpeed) {
511
                XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
512
                xml.putProperty("gps-serialPortSpeed", portSpeed);
513
                model.putValue(kPortSpeed, portSpeed+"");
514
                
515
                this.portSpeed = portSpeed;
516
        }
517

    
518
        
519
        /**
520
         * Sets the amount of data bits of the port that is being monitorized.
521
         * @param int, one of 5, 6, 7, or 8
522
         */
523
        public void setPortDataBits(int dataBits) {
524
                switch (dataBits) {
525
                case 5:
526
                        this.dataBits = SerialPort.DATABITS_5;
527
                        break;
528
                case 6:
529
                        this.dataBits = SerialPort.DATABITS_6;
530
                        break;
531
                case 7:
532
                        this.dataBits = SerialPort.DATABITS_7;
533
                        break;
534
                case 8:
535
                        this.dataBits = SerialPort.DATABITS_8;
536
                        break;
537
                }
538
                if (dataBits != 0){
539
                        XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
540
                        xml.putProperty("gps-serialPortDataBits", dataBits);
541
                }
542
        }
543
        
544
        /**
545
         * Sets the port's stop bits
546
         * @param stopBIts, one of "1", "1.5", or "2"
547
         */
548
        public void setPortStopBits(String stopBits) {
549
                if (stopBits.equals("1")) {
550
                        this.stopBits = SerialPort.STOPBITS_1;
551
                } else if (stopBits.equals("1.5")) {
552
                        this.stopBits = SerialPort.STOPBITS_1_5;
553
                } else if (stopBits.equals("2")) {
554
                        this.stopBits = SerialPort.STOPBITS_2;
555
                }
556
        }
557

    
558
        /**
559
         * Sets the frequency of the receiver sampling.
560
         * @param millis, the amount of milliseconds between samples
561
         */
562
        public void setSampleRate(int millis) {
563
                gps.setSampleRate(millis);
564
        }
565

    
566
        /**
567
         * Sets the parity of the port that is being monitorized
568
         * @param string: one of "even", "mark", "none", "odd", or "space"
569
         */
570
        public void setPortParity(String parity) {
571
                if (parity.equals("even")) {
572
                        this.parity = SerialPort.PARITY_EVEN;
573
                } else if (parity.equals("mark")) {
574
                        this.parity = SerialPort.PARITY_MARK;
575
                } else if (parity.equals("none")) {
576
                        this.parity = SerialPort.PARITY_NONE;
577
                } else if (parity.equals("odd")) {
578
                        this.parity = SerialPort.PARITY_ODD;
579
                } else if (parity.equals("space")) {
580
                        this.parity = SerialPort.PARITY_SPACE;
581
                }
582
        }
583

    
584
        public Object getViewModel() {
585
                return "GPS Control Panel";
586
        }
587

    
588
        public int getSampleRate() {
589
                return gps.getSampleRate();
590
        }
591

    
592
}  //  @jve:decl-index=0:visual-constraint="10,10" 
593

    
594

    
595
class StatusTableModel extends AbstractTableModel {
596
        private ArrayList keys = new ArrayList();
597
        private ArrayList values = new ArrayList();
598
        private Hashtable index = new Hashtable();
599
        
600
        public void putRow(int rowIndex, String[] cellValues) {
601
                keys.set(rowIndex, cellValues[0]);
602
                values.set(rowIndex, cellValues[1]);
603
        }
604
        
605
        public int putValue(String key, String value) {
606
                if (index.containsKey(key)) {
607
                        int i = ((Integer)index.get(key)).intValue();
608
                        values.set(i, value);
609
                        return i;
610
                }
611
                return addRow(new String[] {key, value});
612
        }
613
        
614
        
615
        public void clear() {
616
                index.clear();
617
                keys.clear();
618
                values.clear();
619
        }
620
        
621
        public int addRow(String[] cellValues) {
622
                index.put(cellValues[0], new Integer(keys.size()));
623
                keys.add(cellValues[0]);
624
                values.add(cellValues[1]);
625
                return keys.size()-1;
626
        }
627
        
628
        public int getColumnCount() {
629
                return 2;
630
        }
631

    
632
        public int getRowCount() {
633
                if (keys==null) return 0;
634
                return keys.size();
635
        }
636

    
637
        public Object getValueAt(int rowIndex, int columnIndex) {
638
                if (columnIndex == 0) 
639
                        return keys.get(rowIndex);
640
                else if (columnIndex == 1)
641
                        return values.get(rowIndex);
642
                return null;
643
        }
644
        
645

    
646
        
647
}
648