Statistics
| Revision:

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

History | View | Annotate | Download (15.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: GPSConfigPanel.java 4829 2006-04-12 10:03:32Z jaume $
45
 * $Log$
46
 * Revision 1.9  2006-04-12 10:03:32  jaume
47
 * *** empty log message ***
48
 *
49
 * Revision 1.8  2006/04/12 06:49:47  jaume
50
 * *** empty log message ***
51
 *
52
 * Revision 1.7  2006/04/11 13:19:51  jaume
53
 * *** empty log message ***
54
 *
55
 * Revision 1.6  2006/04/10 17:13:28  jaume
56
 * *** empty log message ***
57
 *
58
 * Revision 1.5  2006/04/10 14:09:46  jaume
59
 * *** empty log message ***
60
 *
61
 * Revision 1.4  2006/04/10 11:21:52  jaume
62
 * *** empty log message ***
63
 *
64
 * Revision 1.3  2006/04/07 12:45:55  jaume
65
 * *** empty log message ***
66
 *
67
 * Revision 1.2  2006/04/07 08:27:48  jaume
68
 * *** empty log message ***
69
 *
70
 * Revision 1.1  2006/04/06 10:35:14  jaume
71
 * *** empty log message ***
72
 *
73
 *
74
 */
75
package org.gvsig.gps.panel;
76

    
77
import gnu.io.CommPortIdentifier;
78

    
79
import java.awt.Component;
80
import java.util.Enumeration;
81

    
82
import javax.swing.JButton;
83
import javax.swing.JComboBox;
84
import javax.swing.JLabel;
85
import javax.swing.JOptionPane;
86
import javax.swing.JPanel;
87
import javax.swing.JTextField;
88
import javax.swing.SwingConstants;
89

    
90
import org.gvsig.gps.tools.PointCalibrate;
91

    
92
import com.iver.andami.PluginServices;
93
import com.iver.andami.ui.mdiManager.SingletonView;
94
import com.iver.andami.ui.mdiManager.View;
95
import com.iver.andami.ui.mdiManager.ViewInfo;
96
import com.iver.cit.gvsig.fmap.MapControl;
97
import com.iver.cit.gvsig.fmap.tools.Behavior.PointBehavior;
98
import com.iver.utiles.XMLEntity;
99

    
100
public class GPSConfigPanel extends JPanel implements SingletonView{
101
        public static String viewModel = "GPSConfigPanel";
102
        private JComboBox cmbPorts = null;
103
        private JPanel pnlPortConfig = null;
104
        private JTextField txtSampleRate;
105
        private JLabel lblSampleRate;
106
        private JLabel lblPort;
107
        private JLabel lblPortSpeed = null;
108
        private JComboBox cmbPortSpeed = null;
109
        private JButton btnOk = null;
110
        private JButton btnCancel = null;
111
        private GPSControlPanel control;
112
        private JComboBox cmbDataBits = null;
113
        private JLabel lblDataBits = null;
114
        private JLabel lblStopBits = null;
115
        private JComboBox cmbStopBits = null;
116
        private JComboBox cmbParity = null;
117
        private JLabel lblParity = null;
118
        private String[] parities = new String[] { "even", "odd", "mark", "space", "none" };
119
        private JLabel lblCalibrate = null;
120
        private JButton btnCalibrate = null;
121
        private JComboBox cmbViews = null;
122
        private JLabel lblMillis = null;
123
        
124

    
125
        /**
126
         * This is the default constructor
127
         */
128
        public GPSConfigPanel(GPSControlPanel control) {
129
                super();
130
                this.control = control;
131
                initialize();
132
        }
133

    
134
        /**
135
         * This method initializes this
136
         * 
137
         * @return void
138
         */
139
        private void initialize() {
140
                lblCalibrate = new JLabel();
141
                lblCalibrate.setBounds(8, 8, 313, 112);
142
                lblCalibrate.setText(PluginServices.getText(this, "html_recalibrate_receiver"));
143
                this.setLayout(null);
144
                this.setSize(329, 384);
145
                this.add(getPnlPortConfig(), null);
146
                this.add(getBtnOk(), null);
147
                this.add(getBtnCancel(), null);
148
                this.add(lblCalibrate, null);
149
                this.add(getBtnCalibrate(), null);
150
                this.add(getCmbViews(), null);
151
        }
152

    
153
        public ViewInfo getViewInfo() {
154
                ViewInfo m_viewInfo = new ViewInfo(ViewInfo.ICONIFIABLE);
155
                m_viewInfo.setTitle(PluginServices.getText(this, "gps_settings"));
156
                m_viewInfo.setWidth(this.getWidth()+8);
157
                m_viewInfo.setHeight(this.getHeight()+8);
158
                return m_viewInfo;
159
        }
160

    
161
        /**
162
         * This method initializes jPanel        
163
         *         
164
         * @return javax.swing.JPanel        
165
         */    
166
        private JPanel getPnlPortConfig() {
167
                if (pnlPortConfig == null) {
168
                        lblMillis = new JLabel();
169
                        lblMillis.setBounds(230, 31, 89, 20);
170
                        lblMillis.setText(PluginServices.getText(this, "milliseconds"));
171
                        lblParity = new JLabel();
172
                        lblParity.setBounds(10, 146, 150, 20);
173
                        lblParity.setHorizontalAlignment(SwingConstants.RIGHT);
174
                        lblParity.setText(PluginServices.getText(this, "parity")+":");
175
                        lblStopBits = new JLabel();
176
                        lblStopBits.setBounds(10, 123, 150, 20);
177
                        lblStopBits.setHorizontalAlignment(SwingConstants.RIGHT);
178
                        lblStopBits.setText(PluginServices.getText(this, "stop_bits")+":");
179
                        lblDataBits = new JLabel();
180
                        lblDataBits.setBounds(10, 100, 150, 20);
181
                        lblDataBits.setHorizontalAlignment(SwingConstants.RIGHT);
182
                        lblDataBits.setText(PluginServices.getText(this, "data_bits")+":");
183
                        lblPortSpeed = new JLabel();
184
                        lblPortSpeed.setBounds(10, 77, 150, 20);
185
                        lblPortSpeed.setHorizontalAlignment(SwingConstants.RIGHT);
186
                        lblPortSpeed.setText(PluginServices.getText(this, "port_speed")+":");
187
                        pnlPortConfig = new JPanel();
188
                        pnlPortConfig.setLayout(null);
189
                        pnlPortConfig.setBounds(0, 170, 329, 179);
190
                        pnlPortConfig.setBorder(javax.swing.BorderFactory.createTitledBorder(
191
                                          null, PluginServices.getText(this, "port_config"),
192
                                          javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, 
193
                                          javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
194
                        pnlPortConfig.add(getTxtSampleRate(), null);
195
                        pnlPortConfig.add(getCmbPorts(), null);
196
                        lblSampleRate = new JLabel();
197
                        lblSampleRate.setBounds(10, 31, 150, 20);
198
                        lblSampleRate.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
199
                        lblSampleRate.setText(PluginServices.getText(this, "sample_rate")+":");
200
                        lblPort = new JLabel();
201
                        lblPort.setBounds(10, 54, 150, 20);
202
                        lblPort.setText(PluginServices.getText(this, "port")+":");
203
                        lblPort.setHorizontalAlignment(SwingConstants.RIGHT);
204
                        pnlPortConfig.add(lblSampleRate, null);
205
                        pnlPortConfig.add(lblPort, null);
206
                        pnlPortConfig.add(lblPortSpeed, null);
207
                        pnlPortConfig.add(getCmbPortSpeed(), null);
208
                        pnlPortConfig.add(getCmbDataBits(), null);
209
                        pnlPortConfig.add(getCmbStopBits(), null);
210
                        pnlPortConfig.add(getCmbParity(), null);
211
                        pnlPortConfig.add(lblDataBits, null);
212
                        pnlPortConfig.add(lblStopBits, null);
213
                        pnlPortConfig.add(lblParity, null);
214
                        pnlPortConfig.add(lblMillis, null);
215
                }
216
                return pnlPortConfig;
217
        }
218

    
219
        private JTextField getTxtSampleRate() {
220
                if (txtSampleRate == null) {
221
                        txtSampleRate = new JTextField();
222
                        txtSampleRate.setBounds(165, 31, 60, 20);
223
                        XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
224
                        int rate = control.getSampleRate();
225
                        if (!xml.contains("gps-sampleRate"))
226
                                xml.putProperty("gps-sampleRate", rate);
227
                        txtSampleRate.setText(xml.getStringProperty("gps-sampleRate"));
228
                }
229
                return txtSampleRate;
230
        }
231
        
232
        /**
233
         * This method initializes jComboBox        
234
         *         
235
         * @return javax.swing.JComboBox        
236
         */    
237
        private JComboBox getCmbPorts() {
238
                if (cmbPorts == null) {
239
                        cmbPorts = new JComboBox();
240
                        cmbPorts.setBounds(165, 54, 154, 20);
241
                        XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
242
                        String portName = null;
243
                        if (xml.contains("gps-serialPortSpeed"))
244
                                portName = xml.getStringProperty("gps-serialPortSpeed");
245
                        Enumeration portList = CommPortIdentifier.getPortIdentifiers();
246
                        while (portList.hasMoreElements()) {
247
                                CommPortIdentifier port = (CommPortIdentifier) portList.nextElement();
248
                                if (port.getPortType() == CommPortIdentifier.PORT_SERIAL) {
249
                                        cmbPorts.addItem(port.getName());
250
                                        if (portName != null && portName.equals(port.getName())) {
251
                                                cmbPorts.setSelectedIndex(cmbPorts.getItemCount()-1);
252
                                        }
253
                                }
254
                        }
255
                }
256
                return cmbPorts;
257
        }
258

    
259
        /**
260
         * This method initializes jComboBox        
261
         *         
262
         * @return javax.swing.JComboBox        
263
         */    
264
        private JComboBox getCmbPortSpeed() {
265
                if (cmbPortSpeed == null) {
266
                        cmbPortSpeed = new JComboBox();
267
                        cmbPortSpeed.setBounds(165, 77, 154, 20);
268
                        cmbPortSpeed.setEditable(false);
269
                        XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
270
                        String[] speeds = new String[] {
271
                                         "75"   ,  "110" ,  "134" ,  "150" ,  "300",
272
                                        "600"   , "1200" , "1800" , "2400" , "4800",
273
                                        "9600"  , "14400", "19200", "38400","57600",
274
                                        "115200","230400"        
275
                                };
276
                        int speed = xml.getIntProperty("gps-serialPortSpeed");
277
                        for (int i = 0; i < speeds.length; i++) {
278
                                cmbPortSpeed.addItem(speeds[i]);
279
                                if (Integer.parseInt(speeds[i]) == speed)
280
                                        cmbPortSpeed.setSelectedIndex(i);
281
                        }
282
                        
283
                }
284
                return cmbPortSpeed;
285
        }
286

    
287
        /**
288
         * This method initializes btnOk        
289
         *         
290
         * @return javax.swing.JButton        
291
         */    
292
        private JButton getBtnOk() {
293
                if (btnOk == null) {
294
                        btnOk = new JButton();
295
                        btnOk.setBounds(78, 353, 85, 25);
296
                        btnOk.setText(PluginServices.getText(this, "ok"));
297
                        btnOk.addActionListener(new java.awt.event.ActionListener() { 
298
                                public void actionPerformed(java.awt.event.ActionEvent e) {    
299
                                        ok();
300
                                }
301
                        });
302
                }
303
                return btnOk;
304
        }
305

    
306
        /**
307
         * This method initializes btnCancel        
308
         *         
309
         * @return javax.swing.JButton        
310
         */    
311
        private JButton getBtnCancel() {
312
                if (btnCancel == null) {
313
                        btnCancel = new JButton();
314
                        btnCancel.setBounds(163, 353, 85, 25);
315
                        btnCancel.addActionListener(new java.awt.event.ActionListener() { 
316
                                public void actionPerformed(java.awt.event.ActionEvent e) {    
317
                                        cancel();
318
                                }
319
                        });
320
                        btnCancel.setText(PluginServices.getText(this, "cancel"));
321
                }
322
                return btnCancel;
323
        }
324

    
325
        private void cancel() {
326
                PluginServices.getMDIManager().closeView(this);                
327
        }
328
        
329
        private void ok() {
330
                try {
331
                        control.setSampleRate(Integer.parseInt(txtSampleRate.getText()));
332
                        control.setPortSpeed(Integer.parseInt((String) cmbPortSpeed.getSelectedItem()));
333
                        control.setPortDataBits(Integer.parseInt((String) cmbDataBits.getSelectedItem()));
334
                        control.setPortStopBits((String) cmbStopBits.getSelectedItem());
335
                        control.setPortParity(parities[cmbParity.getSelectedIndex()]);
336
                        String portName = (String) cmbPorts.getSelectedItem();
337
                        Enumeration portList = CommPortIdentifier.getPortIdentifiers();
338
                        boolean portFound = false;
339
                        while (portList.hasMoreElements()) {
340
                                CommPortIdentifier port = (CommPortIdentifier) portList.nextElement();
341
                                if (port.getName().equals(portName)) {
342
                                        control.setPort(port);
343
                                        portFound = true;
344
                                        break;
345
                                }
346
                        }
347
                        if (!portFound) throw new Exception();
348
                        PluginServices.getMDIManager().closeView(this);
349
                } catch (Exception e) {
350
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), PluginServices.getText(this, "config_error"));
351
                }
352
        }
353

    
354
        /**
355
         * This method initializes jComboBox        
356
         *         
357
         * @return javax.swing.JComboBox        
358
         */    
359
        private JComboBox getCmbDataBits() {
360
                if (cmbDataBits == null) {
361
                        cmbDataBits = new JComboBox();
362
                        cmbDataBits.setBounds(165, 100, 154, 20);
363
                        String[] dataBitsValues;
364
                        XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
365
                        dataBitsValues = new String[] { "5", "6", "7", "8" };
366
                        int dataBits = xml.getIntProperty("gps-serialPortDataBits");
367
                        for (int i = 0; i < dataBitsValues.length; i++) {
368
                                cmbDataBits.addItem(dataBitsValues[i]);
369
                                if (Integer.parseInt(dataBitsValues[i]) == dataBits)
370
                                        cmbDataBits.setSelectedIndex(i);
371
                        }
372
                }
373
                return cmbDataBits;
374
        }
375

    
376
        /**
377
         * This method initializes jComboBox1        
378
         *         
379
         * @return javax.swing.JComboBox        
380
         */    
381
        private JComboBox getCmbStopBits() {
382
                if (cmbStopBits == null) {
383
                        cmbStopBits = new JComboBox();
384
                        cmbStopBits.setBounds(165, 123, 154, 20);
385
                        cmbStopBits.setEditable(false);
386
                        
387
                        XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
388
                        String stopBits = xml.getStringProperty("gps-serialPortStopBits");
389
                        
390
                        String[] stopBitsValues = new String[] { "1", "1.5", "2" };
391
                        for (int i = 0; i < stopBitsValues.length; i++) {
392
                                cmbStopBits.addItem(stopBitsValues[i]);
393
                                if (stopBitsValues[i].equals(stopBits))
394
                                        cmbStopBits.setSelectedIndex(i);
395
                        }
396
                }
397
                return cmbStopBits;
398
        }
399

    
400
        /**
401
         * This method initializes jComboBox2        
402
         *         
403
         * @return javax.swing.JComboBox        
404
         */    
405
        private JComboBox getCmbParity() {
406
                if (cmbParity == null) {
407
                        cmbParity = new JComboBox();
408
                        cmbParity.setBounds(165, 146, 154, 20);
409
                        cmbParity.setEditable(false);
410
                        XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
411
                        
412
                        String parity = xml.getStringProperty("gps-serialPortParities");
413
                        
414
                        for (int i = 0; i < parities.length; i++) {
415
                                cmbParity.addItem(PluginServices.getText(this, parities[i]));
416
                                if (parities[i].equals(parity))
417
                                        cmbParity.setSelectedIndex(i);
418
                        }
419
                }
420
                return cmbParity;
421
        }
422

    
423
        /**
424
         * This method initializes btnCalibrate        
425
         *         
426
         * @return javax.swing.JButton        
427
         */    
428
        private JButton getBtnCalibrate() {
429
                if (btnCalibrate == null) {
430
                        btnCalibrate = new JButton();
431
                        btnCalibrate.setBounds(225, 147, 96, 20);
432
                        btnCalibrate.addActionListener(new java.awt.event.ActionListener() { 
433
                                public void actionPerformed(java.awt.event.ActionEvent e) {    
434
                                        calibrate((String)cmbViews.getSelectedItem());
435
                                }
436
                        });
437
                        btnCalibrate.setText(PluginServices.getText(this, "calibrate"));
438
                        
439
                }
440
                return btnCalibrate;
441
        }
442

    
443
        /**
444
         * Sets the tool that catches a point in the view specified by the viewName
445
         * parameter.
446
         * @param viewName
447
         */
448
        protected void calibrate(String viewName) {
449
                if (viewName!=null) {
450
                        View[] views = PluginServices.getMDIManager().getAllViews();
451
                        for (int i = 0; i < views.length; i++) {
452
                                if ((views[i] instanceof com.iver.cit.gvsig.gui.View)
453
                                                && (views[i].getViewInfo().getTitle().equals(viewName))){
454
                                        com.iver.cit.gvsig.gui.View v = (com.iver.cit.gvsig.gui.View) views[i];
455
                                        
456
                                        String toolName = "gpsCalibrator";
457
                                        MapControl mc = v.getMapControl();
458
                                        String currentTool = v.getMapControl().getTool();
459
                                        PointCalibrate calibrator = new PointCalibrate(v, currentTool);
460
                                        mc.addMapTool(toolName, new PointBehavior(calibrator));
461
                                        mc.setTool(toolName);
462
                                        PluginServices.getMDIManager().addView(v);
463
                                        return;
464
                                }
465
                        }
466
                }
467
        }
468

    
469
        /**
470
         * This method initializes cmbViews        
471
         *         
472
         * @return javax.swing.JComboBox        
473
         */    
474
        private JComboBox getCmbViews() {
475
                if (cmbViews == null) {
476
                        cmbViews = new JComboBox();
477
                        cmbViews.setBounds(8, 127, 313, 17);
478
                        cmbViews.setToolTipText(PluginServices.getText(this, "select_the_view_to_do_calibration"));
479
                }
480
                cmbViews.setEnabled(false);
481
                cmbViews.removeAll();
482
                View[] aViews = PluginServices.getMDIManager().getAllViews();
483
                for (int i = 0; i < aViews.length; i++) {
484
                        if (aViews[i] instanceof com.iver.cit.gvsig.gui.View) {
485
                                cmbViews.addItem(aViews[i].getViewInfo().getTitle());
486
                                cmbViews.setEnabled(true);
487
                        }
488
                }
489
                btnCalibrate.setEnabled(cmbViews.isEnabled());
490
                return cmbViews;
491
        }
492

    
493
        public Object getViewModel() {
494
                return viewModel;
495
        }
496
}