Statistics
| Revision:

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

History | View | Annotate | Download (11.7 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 4751 2006-04-05 21:46:22Z jaume $
45
 * $Log$
46
 * Revision 1.6  2006-04-05 21:46:22  jaume
47
 * *** empty log message ***
48
 *
49
 * Revision 1.5  2006/04/05 17:08:18  jaume
50
 * *** empty log message ***
51
 *
52
 * Revision 1.4  2006/04/05 16:02:09  jaume
53
 * *** empty log message ***
54
 *
55
 * Revision 1.3  2006/04/05 14:50:37  jaume
56
 * *** empty log message ***
57
 *
58
 * Revision 1.2  2006/04/03 17:10:03  jaume
59
 * *** empty log message ***
60
 *
61
 * Revision 1.1  2006/04/03 16:10:27  jaume
62
 * *** empty log message ***
63
 *
64
 *
65
 */
66
package org.gvsig.gps.panel;
67

    
68
import gnu.io.CommPortIdentifier;
69
import gnu.io.PortInUseException;
70

    
71
import java.awt.Component;
72
import java.awt.geom.Point2D;
73
import java.util.Enumeration;
74

    
75
import javax.swing.AbstractButton;
76
import javax.swing.JButton;
77
import javax.swing.JComboBox;
78
import javax.swing.JLabel;
79
import javax.swing.JPanel;
80
import javax.swing.JScrollPane;
81
import javax.swing.JTable;
82
import javax.swing.JTextField;
83
import javax.swing.SwingConstants;
84
import javax.swing.table.AbstractTableModel;
85
import javax.swing.table.TableColumn;
86

    
87
import org.cresques.cts.ICoordTrans;
88
import org.cresques.cts.IProjection;
89
import org.cresques.cts.ProjectionPool;
90
import org.cresques.cts.gt2.CoordSys;
91
import org.cresques.cts.gt2.CoordTrans;
92
import org.gvsig.gps.GPSDriver;
93
import org.gvsig.gps.GPSExtension;
94
import org.gvsig.gps.listeners.GPSEventListener;
95

    
96
import com.iver.andami.PluginServices;
97
import com.iver.andami.ui.mdiManager.View;
98
import com.iver.andami.ui.mdiManager.ViewInfo;
99

    
100
public class GPSControlPanel extends JPanel implements View {
101
        private boolean alreadyStarted;
102

    
103
        private GPSDriver gps;
104
        
105
    private JPanel centerPanel = null;
106
        private JPanel northPanel = null;
107
        private JScrollPane jScrollPane = null;
108
        private JTable tblStatus = null;
109
        private StatusTableModel model;  //  @jve:decl-index=0:visual-constraint="431,39"
110
        private JButton btnStart = null;
111
        private JButton btnPause = null;
112
        private JButton btnStop = null;
113

    
114
        private JComboBox cmbPorts = null;
115

    
116
        private JLabel lblPort;
117

    
118
        private JLabel lblSampleRate;
119

    
120
        private JLabel lblPosValue;
121

    
122
        private JLabel lblPos;
123

    
124
        private JTextField txtSampleRate;
125
        
126

    
127
        public GPSControlPanel() {
128
                super();
129
                gps = GPSDriver.getInstance();
130
                initialize();
131
        }
132

    
133
        /**
134
         * This method initializes this
135
         * 
136
         * @return void
137
         */
138
        private void initialize() {
139
                lblPort = new JLabel();
140
                lblPort.setBounds(10, 80, 143, 20);
141
                lblPort.setText("Port:");
142
                lblPort.setHorizontalAlignment(SwingConstants.RIGHT);
143
                lblSampleRate = new JLabel();
144
                lblSampleRate.setBounds(10, 52, 143, 20);
145
                lblSampleRate.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
146
                lblSampleRate.setText("Frecuencia de muestreo:");
147
                lblPosValue = new JLabel();
148
                lblPosValue.setBounds(156, 25, 223, 20);
149
                lblPosValue.setFont(new java.awt.Font("MS Sans Serif", java.awt.Font.BOLD, 11));
150
                lblPos = new JLabel();
151
                lblPos.setText("Pos:");
152
                lblPos.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
153
                lblPos.setBounds(10, 25, 143, 20);
154
                this.setLayout(null);
155
                this.setSize(391, 433);
156
                this.add(getCenterPanel(), java.awt.BorderLayout.CENTER);
157
                this.add(getNorthPanel(), java.awt.BorderLayout.NORTH);
158
                this.add(getBtnStart(), null);
159
                this.add(getBtnPause(), null);
160
                this.add(getBtnStop(), null);
161
                
162
        }
163

    
164
        public ViewInfo getViewInfo() {
165
                ViewInfo m_viewInfo = new ViewInfo(ViewInfo.ICONIFIABLE |
166
                                ViewInfo.MAXIMIZABLE | ViewInfo.RESIZABLE);
167
                m_viewInfo.setTitle(PluginServices.getText(this, "GPS Control panel"));
168
                m_viewInfo.setWidth(this.getWidth()+8);
169
                m_viewInfo.setHeight(this.getHeight()+8);
170
                
171
                return m_viewInfo;
172
        }
173

    
174
        /**
175
         * This method initializes southPanel        
176
         *         
177
         * @return javax.swing.JPanel        
178
         */    
179
        private JPanel getCenterPanel() {
180
                if (centerPanel == null) {
181
                        centerPanel = new JPanel();
182
                        centerPanel.setLayout(null);
183
                        centerPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
184
                                                                  null, PluginServices.getText(this, "gps_status"),
185
                                                                  javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, 
186
                                                                  javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
187
                        centerPanel.setSize(383, 291);
188
                        centerPanel.setLocation(5, 111);
189
                        centerPanel.add(getJScrollPane(), java.awt.BorderLayout.NORTH);
190
                }
191
                return centerPanel;
192
        }
193

    
194
        /**
195
         * This method initializes northPanel        
196
         *         
197
         * @return javax.swing.JPanel        
198
         */    
199
        private JPanel getNorthPanel() {
200
                if (northPanel == null) {
201
                        northPanel = new JPanel();
202
                        northPanel.setLayout(null);
203
                        northPanel.setSize(385, 105);
204
                        northPanel.setLocation(0, 2);
205
                        northPanel.add(getCmbPorts(), null);
206
                        northPanel.add(lblPos, null);
207
                        northPanel.add(lblPosValue, null);
208
                        northPanel.add(getTxtSampleRate(), null);
209
                        northPanel.add(lblPort, null);
210
                        northPanel.add(lblSampleRate, null);
211
                }
212
                return northPanel;
213
        }
214
        
215
        /**
216
         * This method initializes jTextField        
217
         *         
218
         * @return javax.swing.JTextField        
219
         */    
220
        private JTextField getTxtSampleRate() {
221
                if (txtSampleRate == null) {
222
                        txtSampleRate = new JTextField();
223
                        txtSampleRate.setBounds(156, 52, 223, 20);
224
                        txtSampleRate.addKeyListener(new java.awt.event.KeyAdapter() { 
225
                                public void keyTyped(java.awt.event.KeyEvent e) {    
226
                                        try {
227
                                                float time = Float.parseFloat(txtSampleRate.getText());
228
                                                gps.setSampleRate(Math.abs((int) (time * 1000)));
229
                                                
230
                                        } catch (Exception ex) {
231
                                                txtSampleRate.setText("");
232
                                        };
233
                                }
234
                        });
235
                }
236
                return txtSampleRate;
237
        }
238

    
239
        /**
240
         * This method initializes jScrollPane        
241
         *         
242
         * @return javax.swing.JScrollPane        
243
         */    
244
        private JScrollPane getJScrollPane() {
245
                if (jScrollPane == null) {
246
                        jScrollPane = new JScrollPane();
247
                        jScrollPane.setSize(370, 261);
248
                        jScrollPane.setLocation(5, 25);
249
                        jScrollPane.setViewportView(getTblStatus());
250
                }
251
                return jScrollPane;
252
        }
253

    
254
        /**
255
         * This method initializes tbl        
256
         *         
257
         * @return javax.swing.JTable        
258
         */    
259
        private JTable getTblStatus() {
260
                if (tblStatus == null) {
261
                        tblStatus = new JTable();
262
                        TableColumn column = null;
263
                        for (int i = 0; i < tblStatus.getColumnCount(); i++) {
264
                            column = tblStatus.getColumnModel().getColumn(i);
265
                            int width = (int) (tblStatus.getWidth()*0.3);
266
                            if (i==0) {
267
                                    column.setWidth(width);                                     
268
                            } else {
269
                                    column.setWidth(tblStatus.getWidth()-width);
270
                            }
271
                        }
272
                        tblStatus.setTableHeader(null);
273
                        model = new StatusTableModel();
274
                        tblStatus.setModel(model);
275
                }
276
                return tblStatus;
277
        }
278

    
279
        /**
280
         * This method initializes jButton        
281
         *         
282
         * @return javax.swing.JButton        
283
         */    
284
        private JButton getBtnStart() {
285
                if (btnStart == null) {
286
                        btnStart = new JButton();
287
                        btnStart.setText("Start");
288
                        btnStart.setBounds(75, 405, 70, 20);
289
                        btnStart.addActionListener(new java.awt.event.ActionListener() { 
290
                                public void actionPerformed(java.awt.event.ActionEvent e) {    
291
                                        start();
292
                                }
293
                        });
294
                }
295
                return btnStart;
296
        }
297

    
298
        /**
299
         * This method initializes jButton1        
300
         *         
301
         * @return javax.swing.JButton        
302
         */    
303
        private JButton getBtnPause() {
304
                if (btnPause == null) {
305
                        btnPause = new JButton();
306
                        btnPause.setBounds(150, 405, 70, 20);
307
                        btnPause.setText("Pause");
308
                        btnPause.addActionListener(new java.awt.event.ActionListener() { 
309
                                public void actionPerformed(java.awt.event.ActionEvent e) {    
310
                                        gps.silence();
311
                                }
312
                        });
313
                }
314
                return btnPause;
315
        }
316

    
317
        /**
318
         * This method initializes jButton2        
319
         *         
320
         * @return javax.swing.JButton        
321
         */    
322
        private JButton getBtnStop() {
323
                if (btnStop == null) {
324
                        btnStop = new JButton();
325
                        btnStop.setBounds(270, 405, 70, 21);
326
                        btnStop.setText("Stop");
327
                        btnStop.addActionListener(new java.awt.event.ActionListener() { 
328
                                public void actionPerformed(java.awt.event.ActionEvent e) {    
329
                                        gps.closePort();
330
                                }
331
                        });
332
                }
333
                return btnStop;
334
        }
335

    
336
        /**
337
         * This method initializes jComboBox        
338
         *         
339
         * @return javax.swing.JComboBox        
340
         */    
341
        private JComboBox getCmbPorts() {
342
                if (cmbPorts == null) {
343
                        cmbPorts = new JComboBox();
344
                        cmbPorts.setBounds(156, 80, 166, 20);
345
                        Enumeration portList = CommPortIdentifier.getPortIdentifiers();
346
                        while (portList.hasMoreElements()) {
347
                                cmbPorts.addItem(((CommPortIdentifier) portList.nextElement()).getName());
348
                        }
349
                        
350
                }
351
                return cmbPorts;
352
        }
353
        
354
        public void start() {
355
                if (!alreadyStarted) {
356
                        Enumeration portList = CommPortIdentifier.getPortIdentifiers();
357
                        while (portList.hasMoreElements()) {
358
                                CommPortIdentifier myPortId = (CommPortIdentifier) portList.nextElement();
359
                                if (myPortId.getName().equals(getCmbPorts().getSelectedItem().toString()))
360
                                        try {
361
                                                
362
                                                gps.setPort(myPortId, 4800);
363
                                                gps.addEventListener(new GPSEventListener() {
364
                                                        
365
                                                        public void unhandledMessage(String message) {
366
                                                                System.out.println("unhandledMessage"+": "+message+"\n");
367
                                                                lblPosValue.setText(message);
368
                                                        }
369
                                                        
370
                                                        public void connectionLost() {
371
                                                                System.out.println("connectionLost");
372
                                                        }
373
                                                        
374
                                                        public void connectionEstablished() {
375
                                                                System.out.println("connectionEstablished");
376
                                                        }
377
                                                        
378
                                                        public void newLonLatPositionReceived(double lon, double lat) {
379
                                                                IProjection reqProj = ProjectionPool.get("EPSG:23030");
380
                                                                IProjection latLonProj = ProjectionPool.get("EPSG:4326");                    
381
                                                                ICoordTrans ct = new CoordTrans((CoordSys) latLonProj, (CoordSys) reqProj);
382
                                                                Point2D pDst = new Point2D.Double();
383
                                                                Point2D pSrc = new Point2D.Double(lon, lat);
384
                                                                //Point2D pSrc = new Point2D.Double(-0.36297333240509033, 39.463722229003906);
385
                                                                pDst = ct.convert(pSrc, pDst);
386
                                                                lblPosValue.setText("("+pDst.getX()+", "+pDst.getY()+")");
387
                                                                
388
                                                                GPSExtension ext = (GPSExtension) PluginServices.getExtension(GPSExtension.class);
389
                                                                ext.drawPoint(null, null, pDst);
390
                                                        }
391
                                                        
392
                                                        public void signalLevelChanged(float level) {
393
                                                        }
394
                                                        
395
                                                        public void speedChanged(float speed, short course) {
396
                                                        }
397
                                                        
398
                                                        public void estimatedPosErrorChanged(double f) {
399
                                                        }
400
                                                        
401
                                                        public void heightChanged() {
402
                                                        }
403
                                                        
404
                                                });
405
                                                gps.start();
406
                                                alreadyStarted = true;
407
                                        } catch (PortInUseException e) {
408
                                                // TODO Auto-generated catch block
409
                                                e.printStackTrace();
410
                                        }
411
                        }
412
                }
413
        }
414
}  //  @jve:decl-index=0:visual-constraint="13,3"
415

    
416
class StatusTableModel extends AbstractTableModel {
417
        private String[] keys, values;
418
        
419
        public void setTableData(String[] keys, String[] values) {
420
                this.keys = keys;
421
                this.values = values;
422
        }
423
        
424
        public int getColumnCount() {
425
                return 2;
426
        }
427

    
428
        public int getRowCount() {
429
                if (keys==null) return 0;
430
                return keys.length;
431
        }
432

    
433
        public Object getValueAt(int rowIndex, int columnIndex) {
434
                if (columnIndex == 0) 
435
                        return values[rowIndex];
436
                else if (columnIndex == 1)
437
                        return keys[rowIndex];
438
                return null;
439
        }
440
        
441

    
442
        
443
}
444