Statistics
| Revision:

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

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 4763 2006-04-07 11:10:26Z jaume $
45
 * $Log$
46
 * Revision 1.9  2006-04-07 11:10:26  jaume
47
 * *** empty log message ***
48
 *
49
 * Revision 1.8  2006/04/07 08:27:48  jaume
50
 * *** empty log message ***
51
 *
52
 * Revision 1.7  2006/04/06 10:35:48  jaume
53
 * *** empty log message ***
54
 *
55
 * Revision 1.5  2006/04/05 17:08:18  jaume
56
 * *** empty log message ***
57
 *
58
 * Revision 1.4  2006/04/05 16:02:09  jaume
59
 * *** empty log message ***
60
 *
61
 * Revision 1.3  2006/04/05 14:50:37  jaume
62
 * *** empty log message ***
63
 *
64
 * Revision 1.2  2006/04/03 17:10:03  jaume
65
 * *** empty log message ***
66
 *
67
 * Revision 1.1  2006/04/03 16:10:27  jaume
68
 * *** empty log message ***
69
 *
70
 *
71
 */
72
package org.gvsig.gps.panel;
73

    
74
import gnu.io.CommPortIdentifier;
75
import gnu.io.PortInUseException;
76
import gnu.io.SerialPort;
77

    
78
import java.awt.geom.Point2D;
79
import java.util.ArrayList;
80
import java.util.Enumeration;
81
import java.util.Hashtable;
82

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

    
101
import org.gvsig.gps.GPSDriver;
102
import org.gvsig.gps.GPSExtension;
103
import org.gvsig.gps.listeners.GPSEventListener;
104

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

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

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

    
197
        public static GPSControlPanel getInstance() {
198
                if (instance == null)
199
                        instance = new GPSControlPanel();
200
                return instance;
201
        }
202

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
535

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

    
573
        public int getRowCount() {
574
                if (keys==null) return 0;
575
                return keys.size();
576
        }
577

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

    
587
        
588
}
589