Statistics
| Revision:

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

History | View | Annotate | Download (16.1 KB)

1 4720 jaume
/* 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$
45
 * $Log$
46 4763 jaume
 * Revision 1.9  2006-04-07 11:10:26  jaume
47 4720 jaume
 * *** empty log message ***
48
 *
49 4763 jaume
 * Revision 1.8  2006/04/07 08:27:48  jaume
50
 * *** empty log message ***
51
 *
52 4753 jaume
 * Revision 1.7  2006/04/06 10:35:48  jaume
53
 * *** empty log message ***
54
 *
55 4751 jaume
 * Revision 1.5  2006/04/05 17:08:18  jaume
56
 * *** empty log message ***
57
 *
58 4749 jaume
 * Revision 1.4  2006/04/05 16:02:09  jaume
59
 * *** empty log message ***
60
 *
61 4747 jaume
 * Revision 1.3  2006/04/05 14:50:37  jaume
62
 * *** empty log message ***
63
 *
64 4746 jaume
 * Revision 1.2  2006/04/03 17:10:03  jaume
65
 * *** empty log message ***
66
 *
67 4721 jaume
 * Revision 1.1  2006/04/03 16:10:27  jaume
68
 * *** empty log message ***
69 4720 jaume
 *
70 4721 jaume
 *
71 4720 jaume
 */
72
package org.gvsig.gps.panel;
73
74 4749 jaume
import gnu.io.CommPortIdentifier;
75
import gnu.io.PortInUseException;
76 4752 jaume
import gnu.io.SerialPort;
77 4720 jaume
78 4749 jaume
import java.awt.geom.Point2D;
79 4753 jaume
import java.util.ArrayList;
80 4749 jaume
import java.util.Enumeration;
81 4753 jaume
import java.util.Hashtable;
82 4749 jaume
83 4753 jaume
import javax.swing.DefaultCellEditor;
84 4747 jaume
import javax.swing.JButton;
85 4753 jaume
import javax.swing.JComponent;
86 4749 jaume
import javax.swing.JLabel;
87 4752 jaume
import javax.swing.JOptionPane;
88 4720 jaume
import javax.swing.JPanel;
89
import javax.swing.JScrollPane;
90
import javax.swing.JTable;
91 4753 jaume
import javax.swing.ListSelectionModel;
92
import javax.swing.event.TableColumnModelListener;
93
import javax.swing.event.TableModelEvent;
94 4721 jaume
import javax.swing.table.AbstractTableModel;
95 4753 jaume
import javax.swing.table.DefaultTableColumnModel;
96
import javax.swing.table.JTableHeader;
97
import javax.swing.table.TableCellRenderer;
98 4721 jaume
import javax.swing.table.TableColumn;
99 4753 jaume
import javax.swing.table.TableColumnModel;
100 4720 jaume
101 4749 jaume
import org.gvsig.gps.GPSDriver;
102
import org.gvsig.gps.GPSExtension;
103
import org.gvsig.gps.listeners.GPSEventListener;
104
105 4720 jaume
import com.iver.andami.PluginServices;
106 4753 jaume
import com.iver.andami.plugins.Extension;
107
import com.iver.andami.ui.mdiManager.SingletonView;
108 4720 jaume
import com.iver.andami.ui.mdiManager.ViewInfo;
109 4752 jaume
import com.iver.utiles.XMLEntity;
110 4753 jaume
/**
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 4749 jaume
        private boolean alreadyStarted;
118
        private GPSDriver gps;
119 4721 jaume
    private JPanel centerPanel = null;
120
        private JPanel northPanel = null;
121
        private JScrollPane jScrollPane = null;
122
        private JTable tblStatus = null;
123 4752 jaume
        private StatusTableModel model;
124 4749 jaume
        private JButton btnStart = null;
125
        private JButton btnPause = null;
126
        private JButton btnStop = null;
127
        private JLabel lblPos;
128 4752 jaume
        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 4749 jaume
135 4753 jaume
        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 4749 jaume
152 4753 jaume
        private GPSControlPanel() {
153 4720 jaume
                super();
154 4749 jaume
                gps = GPSDriver.getInstance();
155 4753 jaume
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 4752 jaume
                XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
170
                // Try to restore last port used
171
                String portName = null;
172 4753 jaume
                if (xml.contains("gps-serialPortName")) {
173
                        portName = xml.getStringProperty("gps-serialPortName");
174 4752 jaume
                }
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 4753 jaume
                        xml.putProperty("gps-serialPortStopBits", "1");
188 4752 jaume
                if (!xml.contains("gps-serialPortParities"))
189
                        xml.putProperty("gps-serialPortParities", "none");
190
191 4753 jaume
                setPortSpeed(xml.getIntProperty("gps-serialPortSpeed"));
192
                setPortDataBits(xml.getIntProperty("gps-serialPortDataBits"));
193 4752 jaume
                setPortStopBits(xml.getStringProperty("gps-serialPortStopBits"));
194
                setPortParity(xml.getStringProperty("gps-serialPortParities"));
195 4720 jaume
        }
196
197 4753 jaume
        public static GPSControlPanel getInstance() {
198
                if (instance == null)
199
                        instance = new GPSControlPanel();
200
                return instance;
201
        }
202
203 4720 jaume
        /**
204
         * This method initializes this
205
         *
206
         * @return void
207
         */
208
        private void initialize() {
209 4749 jaume
                this.setLayout(null);
210 4753 jaume
                this.setSize(572, 433);
211 4746 jaume
                this.add(getCenterPanel(), java.awt.BorderLayout.CENTER);
212 4721 jaume
                this.add(getNorthPanel(), java.awt.BorderLayout.NORTH);
213 4749 jaume
                this.add(getBtnStart(), null);
214
                this.add(getBtnPause(), null);
215
                this.add(getBtnStop(), null);
216
217 4720 jaume
        }
218
219
        public ViewInfo getViewInfo() {
220 4753 jaume
                ViewInfo m_viewInfo = new ViewInfo(ViewInfo.ICONIFIABLE);
221 4752 jaume
                m_viewInfo.setTitle(PluginServices.getText(this, "gps_control_panel"));
222 4749 jaume
                m_viewInfo.setWidth(this.getWidth()+8);
223
                m_viewInfo.setHeight(this.getHeight()+8);
224 4720 jaume
225
                return m_viewInfo;
226
        }
227
228 4721 jaume
        /**
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 4749 jaume
                        centerPanel.setLayout(null);
237 4721 jaume
                        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 4753 jaume
                        centerPanel.setSize(572, 291);
242
                        centerPanel.setLocation(0, 111);
243 4721 jaume
                        centerPanel.add(getJScrollPane(), java.awt.BorderLayout.NORTH);
244 4752 jaume
                        centerPanel.add(getBtnConfig(), null);
245 4721 jaume
                }
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 4749 jaume
                        northPanel.setLayout(null);
258 4753 jaume
                        northPanel.setSize(572, 112);
259
                        northPanel.setLocation(0, 0);
260 4721 jaume
                }
261
                return northPanel;
262
        }
263 4749 jaume
264
        /**
265 4752 jaume
         * This method initializes txtSampleRate
266 4749 jaume
         *
267
         * @return javax.swing.JTextField
268
         */
269 4752 jaume
270 4721 jaume
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 4753 jaume
                        jScrollPane.setSize(560, 231);
280 4749 jaume
                        jScrollPane.setLocation(5, 25);
281 4721 jaume
                        jScrollPane.setViewportView(getTblStatus());
282
                }
283
                return jScrollPane;
284
        }
285
286
        /**
287 4752 jaume
         * This method initializes tblStatus
288 4721 jaume
         *
289
         * @return javax.swing.JTable
290
         */
291
        private JTable getTblStatus() {
292
                if (tblStatus == null) {
293
                        tblStatus = new JTable();
294 4753 jaume
                        tblStatus.setShowHorizontalLines(false);
295
296 4721 jaume
                        model = new StatusTableModel();
297
                        tblStatus.setModel(model);
298
                }
299
                return tblStatus;
300
        }
301
302
        /**
303 4752 jaume
         * This method initializes btnStart
304 4721 jaume
         *
305
         * @return javax.swing.JButton
306
         */
307 4749 jaume
        private JButton getBtnStart() {
308
                if (btnStart == null) {
309
                        btnStart = new JButton();
310 4752 jaume
                        btnStart.setText("start");
311 4753 jaume
                        btnStart.setBounds(214, 407, 70, 20);
312 4749 jaume
                        btnStart.addActionListener(new java.awt.event.ActionListener() {
313
                                public void actionPerformed(java.awt.event.ActionEvent e) {
314
                                        start();
315
                                }
316
                        });
317 4721 jaume
                }
318 4749 jaume
                return btnStart;
319 4721 jaume
        }
320
321 4749 jaume
        /**
322 4752 jaume
         * This method initializes btnPause
323 4749 jaume
         *
324
         * @return javax.swing.JButton
325
         */
326
        private JButton getBtnPause() {
327
                if (btnPause == null) {
328
                        btnPause = new JButton();
329 4753 jaume
                        btnPause.setBounds(289, 407, 70, 20);
330
                        btnPause.setText("pause");
331 4749 jaume
                        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 4752 jaume
         * This method initializes btnStop
342 4749 jaume
         *
343
         * @return javax.swing.JButton
344
         */
345
        private JButton getBtnStop() {
346
                if (btnStop == null) {
347
                        btnStop = new JButton();
348 4753 jaume
                        btnStop.setBounds(409, 407, 70, 21);
349 4752 jaume
                        btnStop.setText("stop");
350 4749 jaume
                        btnStop.addActionListener(new java.awt.event.ActionListener() {
351
                                public void actionPerformed(java.awt.event.ActionEvent e) {
352
                                        gps.closePort();
353
                                }
354
                        });
355 4753 jaume
                        alreadyStarted = false;
356 4749 jaume
                }
357
                return btnStop;
358
        }
359 4753 jaume
360
        protected void refresh() {
361
                this.repaint();
362
        }
363 4752 jaume
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 4753 jaume
                                        private TableModelEvent e = new TableModelEvent(model);
372 4752 jaume
                                        public void unhandledMessage(String message) {
373 4753 jaume
                                                model.putValue(kMsg, message);
374
                                                tblStatus.tableChanged(e);
375 4752 jaume
                                        }
376
377
                                        public void connectionLost() {
378 4753 jaume
                                                model.putValue(kStatus, PluginServices.getText(this, "off"));
379
                                                tblStatus.tableChanged(e);
380 4752 jaume
                                        }
381
382
                                        public void connectionEstablished() {
383 4753 jaume
                                                model.putValue(kStatus, PluginServices.getText(this, "on"));
384
                                                tblStatus.tableChanged(e);
385 4752 jaume
                                        }
386
387
                                        public void newLonLatPositionReceived(double lon, double lat) {
388 4753 jaume
                                                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 4763 jaume
                                                ((GPSExtension) thisExtension).drawSymbol(null, null, p, false);
393 4752 jaume
                                        }
394
395
                                        public void signalLevelChanged(float level) {
396 4753 jaume
                                                model.putValue(kSignalLevel , level+"");
397
                                                tblStatus.tableChanged(e);
398 4752 jaume
                                        }
399
400
                                        public void speedChanged(float speed, short course) {
401 4753 jaume
                                                model.putValue(kSpeed, speed+"");
402
                                                model.putValue(kCourse, course+"?");
403
                                                tblStatus.tableChanged(e);
404 4752 jaume
                                        }
405
406 4753 jaume
                                        public void estimatedPosErrorChanged(double err) {
407
                                                model.putValue(kEstErr, err+"");
408
                                                tblStatus.tableChanged(e);
409 4752 jaume
                                        }
410
411
                                        public void heightChanged(float height) {
412 4753 jaume
                                                model.putValue(kHeight, height+"");
413
                                                tblStatus.tableChanged(e);
414 4752 jaume
                                        }
415
416
                                        public void precisionChanged(float pDop, float hDop, float vDop) {
417 4753 jaume
                                                model.putValue(kPosPrecision, pDop+"");
418
                                                model.putValue(kHorPrecision, hDop+"");
419
                                                model.putValue(kVerPrecision, vDop+"");
420
                                                tblStatus.tableChanged(e);
421 4752 jaume
                                        }
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 4749 jaume
432
        /**
433 4752 jaume
         * This method initializes jButton
434 4749 jaume
         *
435 4752 jaume
         * @return javax.swing.JButton
436 4749 jaume
         */
437 4752 jaume
        private JButton getBtnConfig() {
438
                if (btnConfig == null) {
439
                        btnConfig = new JButton();
440
                        btnConfig.setBounds(265, 263, 70, 20);
441 4753 jaume
                        btnConfig.addActionListener(new java.awt.event.ActionListener() {
442
                                public void actionPerformed(java.awt.event.ActionEvent e) {
443
                                        config();
444
                                }
445
                        });
446 4752 jaume
                        btnConfig.setText(PluginServices.getText(this, "config"));
447 4749 jaume
                }
448 4752 jaume
                return btnConfig;
449 4749 jaume
        }
450 4752 jaume
451
        /**
452 4753 jaume
         * Opens the config dialog.
453
         */
454
        protected void config() {
455
                PluginServices.getMDIManager().addView(new GPSConfigPanel(this));
456
        }
457
458
        /**
459 4752 jaume
         * Sets the port to be monitorized.
460
         * @param port
461
         */
462
        public void setPort(CommPortIdentifier port) {
463 4753 jaume
                XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
464
                xml.putProperty("gps-serialPortName", port.getName());
465 4752 jaume
                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 4749 jaume
498 4752 jaume
        /**
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 4749 jaume
                }
510
        }
511
512 4752 jaume
        /**
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 4753 jaume
        public Object getViewModel() {
531
                return "GPS Control Panel";
532
        }
533
}  //  @jve:decl-index=0:visual-constraint="10,10"
534
535
536 4721 jaume
class StatusTableModel extends AbstractTableModel {
537 4753 jaume
        private ArrayList keys = new ArrayList();
538
        private ArrayList values = new ArrayList();
539
        private Hashtable index = new Hashtable();
540 4749 jaume
541 4753 jaume
        public void putRow(int rowIndex, String[] cellValues) {
542
                keys.set(rowIndex, cellValues[0]);
543
                values.set(rowIndex, cellValues[1]);
544 4721 jaume
        }
545
546 4753 jaume
        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 4721 jaume
        public int getColumnCount() {
570
                return 2;
571
        }
572
573
        public int getRowCount() {
574
                if (keys==null) return 0;
575 4753 jaume
                return keys.size();
576 4721 jaume
        }
577
578
        public Object getValueAt(int rowIndex, int columnIndex) {
579
                if (columnIndex == 0)
580 4753 jaume
                        return keys.get(rowIndex);
581 4721 jaume
                else if (columnIndex == 1)
582 4753 jaume
                        return values.get(rowIndex);
583 4721 jaume
                return null;
584
        }
585 4749 jaume
586
587
588 4721 jaume
}