Statistics
| Revision:

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

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

    
92
import gnu.io.CommPortIdentifier;
93
import gnu.io.PortInUseException;
94
import gnu.io.SerialPort;
95

    
96
import java.awt.geom.Point2D;
97
import java.util.ArrayList;
98
import java.util.Enumeration;
99
import java.util.Hashtable;
100

    
101
import javax.swing.JButton;
102
import javax.swing.JLabel;
103
import javax.swing.JOptionPane;
104
import javax.swing.JPanel;
105
import javax.swing.JScrollPane;
106
import javax.swing.JTable;
107
import javax.swing.event.TableModelEvent;
108
import javax.swing.table.AbstractTableModel;
109

    
110
import org.gvsig.gps.GPSDriver;
111
import org.gvsig.gps.GPSExtension;
112
import org.gvsig.gps.listeners.GPSEventListener;
113

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

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

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

    
207
        
208

    
209
        public static GPSControlPanel getInstance() {
210
                if (instance == null)
211
                        instance = new GPSControlPanel();
212
                return instance;
213
        }
214

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

    
231
        public ViewInfo getViewInfo() {
232
                ViewInfo m_viewInfo = new ViewInfo(ViewInfo.ICONIFIABLE);
233
                m_viewInfo.setTitle(PluginServices.getText(this, "gps_control_panel"));
234
                m_viewInfo.setWidth(this.getWidth()+8);
235
                m_viewInfo.setHeight(this.getHeight()+8);
236
                
237
                return m_viewInfo;
238
        }
239

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

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

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

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

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

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

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

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

    
440
        /**
441
         * This method initializes jButton        
442
         *         
443
         * @return javax.swing.JButton        
444
         */    
445
        private JButton getBtnConfig() {
446
                if (btnConfig == null) {
447
                        btnConfig = new JButton();
448
                        btnConfig.setBounds(479, 234, 86, 20);
449
                        btnConfig.addActionListener(new java.awt.event.ActionListener() { 
450
                                public void actionPerformed(java.awt.event.ActionEvent e) {    
451
                                        config();
452
                                }
453
                        });
454
                        btnConfig.setText(PluginServices.getText(this, "config"));
455
                }
456
                return btnConfig;
457
        }
458

    
459
        /**
460
         * Opens the config dialog.
461
         */
462
        protected void config() {
463
                PluginServices.getMDIManager().addView(new GPSConfigPanel(this));                
464
        }
465

    
466
        /**
467
         * Sets the port to be monitorized.
468
         * @param port
469
         */
470
        public void setPort(CommPortIdentifier port) {
471
                XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
472
                xml.putProperty("gps-serialPortName", port.getName());
473
                model.putValue(kPort, port.getName());
474
                this.port = port;
475
        }
476

    
477
        /**
478
         * Sets the speed in bps of the port that is being monitorized.
479
         * @param portSpeed
480
         */
481
        public void setPortSpeed(int portSpeed) {
482
                XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
483
                xml.putProperty("gps-serialPortSpeed", portSpeed);
484
                model.putValue(kPortSpeed, portSpeed+"");
485
                
486
                this.portSpeed = portSpeed;
487
        }
488

    
489
        
490
        /**
491
         * Sets the amount of data bits of the port that is being monitorized.
492
         * @param int, one of 5, 6, 7, or 8
493
         */
494
        public void setPortDataBits(int dataBits) {
495
                switch (dataBits) {
496
                case 5:
497
                        this.dataBits = SerialPort.DATABITS_5;
498
                        break;
499
                case 6:
500
                        this.dataBits = SerialPort.DATABITS_6;
501
                        break;
502
                case 7:
503
                        this.dataBits = SerialPort.DATABITS_7;
504
                        break;
505
                case 8:
506
                        this.dataBits = SerialPort.DATABITS_8;
507
                        break;
508
                }
509
                if (dataBits != 0){
510
                        XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
511
                        xml.putProperty("gps-serialPortDataBits", dataBits);
512
                }
513
        }
514
        
515
        /**
516
         * Sets the port's stop bits
517
         * @param stopBIts, one of "1", "1.5", or "2"
518
         */
519
        public void setPortStopBits(String stopBits) {
520
                if (stopBits.equals("1")) {
521
                        this.stopBits = SerialPort.STOPBITS_1;
522
                } else if (stopBits.equals("1.5")) {
523
                        this.stopBits = SerialPort.STOPBITS_1_5;
524
                } else if (stopBits.equals("2")) {
525
                        this.stopBits = SerialPort.STOPBITS_2;
526
                }
527
        }
528

    
529
        /**
530
         * Sets the frequency of the receiver sampling.
531
         * @param millis, the amount of milliseconds between samples
532
         */
533
        public void setSampleRate(int millis) {
534
                gps.setSampleRate(millis);
535
        }
536

    
537
        /**
538
         * Sets the parity of the port that is being monitorized
539
         * @param string: one of "even", "mark", "none", "odd", or "space"
540
         */
541
        public void setPortParity(String parity) {
542
                if (parity.equals("even")) {
543
                        this.parity = SerialPort.PARITY_EVEN;
544
                } else if (parity.equals("mark")) {
545
                        this.parity = SerialPort.PARITY_MARK;
546
                } else if (parity.equals("none")) {
547
                        this.parity = SerialPort.PARITY_NONE;
548
                } else if (parity.equals("odd")) {
549
                        this.parity = SerialPort.PARITY_ODD;
550
                } else if (parity.equals("space")) {
551
                        this.parity = SerialPort.PARITY_SPACE;
552
                }
553
        }
554

    
555
        public Object getViewModel() {
556
                return "GPS Control Panel";
557
        }
558

    
559
}  //  @jve:decl-index=0:visual-constraint="10,10" 
560

    
561

    
562
class StatusTableModel extends AbstractTableModel {
563
        private ArrayList keys = new ArrayList();
564
        private ArrayList values = new ArrayList();
565
        private Hashtable index = new Hashtable();
566
        
567
        public void putRow(int rowIndex, String[] cellValues) {
568
                keys.set(rowIndex, cellValues[0]);
569
                values.set(rowIndex, cellValues[1]);
570
        }
571
        
572
        public int putValue(String key, String value) {
573
                if (index.containsKey(key)) {
574
                        int i = ((Integer)index.get(key)).intValue();
575
                        values.set(i, value);
576
                        return i;
577
                }
578
                return addRow(new String[] {key, value});
579
        }
580
        
581
        
582
        public void clear() {
583
                index.clear();
584
                keys.clear();
585
                values.clear();
586
        }
587
        
588
        public int addRow(String[] cellValues) {
589
                index.put(cellValues[0], new Integer(keys.size()));
590
                keys.add(cellValues[0]);
591
                values.add(cellValues[1]);
592
                return keys.size()-1;
593
        }
594
        
595
        public int getColumnCount() {
596
                return 2;
597
        }
598

    
599
        public int getRowCount() {
600
                if (keys==null) return 0;
601
                return keys.size();
602
        }
603

    
604
        public Object getValueAt(int rowIndex, int columnIndex) {
605
                if (columnIndex == 0) 
606
                        return keys.get(rowIndex);
607
                else if (columnIndex == 1)
608
                        return values.get(rowIndex);
609
                return null;
610
        }
611
        
612

    
613
        
614
}
615