Statistics
| Revision:

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

History | View | Annotate | Download (14.9 KB)

1 4752 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 4827 jaume
 * Revision 1.8  2006-04-12 06:49:47  jaume
47 4752 jaume
 * *** empty log message ***
48
 *
49 4827 jaume
 * Revision 1.7  2006/04/11 13:19:51  jaume
50
 * *** empty log message ***
51
 *
52 4805 jaume
 * Revision 1.6  2006/04/10 17:13:28  jaume
53
 * *** empty log message ***
54
 *
55 4788 jaume
 * Revision 1.5  2006/04/10 14:09:46  jaume
56
 * *** empty log message ***
57
 *
58 4787 jaume
 * Revision 1.4  2006/04/10 11:21:52  jaume
59
 * *** empty log message ***
60
 *
61 4784 jaume
 * Revision 1.3  2006/04/07 12:45:55  jaume
62
 * *** empty log message ***
63
 *
64 4768 jaume
 * Revision 1.2  2006/04/07 08:27:48  jaume
65
 * *** empty log message ***
66
 *
67 4753 jaume
 * Revision 1.1  2006/04/06 10:35:14  jaume
68
 * *** empty log message ***
69 4752 jaume
 *
70 4753 jaume
 *
71 4752 jaume
 */
72
package org.gvsig.gps.panel;
73
74
import gnu.io.CommPortIdentifier;
75
76
import java.awt.Component;
77
import java.util.Enumeration;
78
79
import javax.swing.JButton;
80
import javax.swing.JComboBox;
81
import javax.swing.JLabel;
82
import javax.swing.JOptionPane;
83
import javax.swing.JPanel;
84
import javax.swing.JTextField;
85
import javax.swing.SwingConstants;
86
87 4768 jaume
import org.gvsig.gps.tools.PointCalibrate;
88
89 4752 jaume
import com.iver.andami.PluginServices;
90 4827 jaume
import com.iver.andami.ui.mdiManager.SingletonView;
91 4752 jaume
import com.iver.andami.ui.mdiManager.View;
92
import com.iver.andami.ui.mdiManager.ViewInfo;
93 4768 jaume
import com.iver.cit.gvsig.fmap.MapControl;
94
import com.iver.cit.gvsig.fmap.tools.Behavior.PointBehavior;
95 4752 jaume
import com.iver.utiles.XMLEntity;
96
97 4827 jaume
public class GPSConfigPanel extends JPanel implements SingletonView{
98
        public static String viewModel = "GPSConfigPanel";
99 4752 jaume
        private JComboBox cmbPorts = null;
100
        private JPanel pnlPortConfig = null;
101
        private JTextField txtSampleRate;
102
        private JLabel lblSampleRate;
103
        private JLabel lblPort;
104
        private JLabel lblPortSpeed = null;
105
        private JComboBox cmbPortSpeed = null;
106
        private JButton btnOk = null;
107
        private JButton btnCancel = null;
108
        private GPSControlPanel control;
109
        private JComboBox cmbDataBits = null;
110
        private JLabel lblDataBits = null;
111
        private JLabel lblStopBits = null;
112
        private JComboBox cmbStopBits = null;
113
        private JComboBox cmbParity = null;
114
        private JLabel lblParity = null;
115 4753 jaume
        private String[] parities = new String[] { "even", "odd", "mark", "space", "none" };
116 4768 jaume
        private JLabel lblCalibrate = null;
117
        private JButton btnCalibrate = null;
118
        private JComboBox cmbViews = null;
119 4784 jaume
        private JLabel lblMillis = null;
120 4753 jaume
121 4752 jaume
122
        /**
123
         * This is the default constructor
124
         */
125
        public GPSConfigPanel(GPSControlPanel control) {
126
                super();
127
                this.control = control;
128
                initialize();
129
        }
130
131
        /**
132
         * This method initializes this
133
         *
134
         * @return void
135
         */
136
        private void initialize() {
137 4768 jaume
                lblCalibrate = new JLabel();
138 4788 jaume
                lblCalibrate.setBounds(8, 8, 313, 112);
139 4768 jaume
                lblCalibrate.setText(PluginServices.getText(this, "html_recalibrate_receiver"));
140 4752 jaume
                this.setLayout(null);
141 4788 jaume
                this.setSize(329, 384);
142 4752 jaume
                this.add(getPnlPortConfig(), null);
143
                this.add(getBtnOk(), null);
144
                this.add(getBtnCancel(), null);
145 4768 jaume
                this.add(lblCalibrate, null);
146
                this.add(getBtnCalibrate(), null);
147
                this.add(getCmbViews(), null);
148 4752 jaume
        }
149
150
        public ViewInfo getViewInfo() {
151
                ViewInfo m_viewInfo = new ViewInfo(ViewInfo.ICONIFIABLE);
152
                m_viewInfo.setTitle(PluginServices.getText(this, "gps_settings"));
153
                m_viewInfo.setWidth(this.getWidth()+8);
154
                m_viewInfo.setHeight(this.getHeight()+8);
155
                return m_viewInfo;
156
        }
157
158
        /**
159
         * This method initializes jPanel
160
         *
161
         * @return javax.swing.JPanel
162
         */
163
        private JPanel getPnlPortConfig() {
164
                if (pnlPortConfig == null) {
165 4784 jaume
                        lblMillis = new JLabel();
166 4788 jaume
                        lblMillis.setBounds(230, 31, 89, 20);
167 4784 jaume
                        lblMillis.setText(PluginServices.getText(this, "milliseconds"));
168 4752 jaume
                        lblParity = new JLabel();
169 4788 jaume
                        lblParity.setBounds(10, 146, 150, 20);
170 4752 jaume
                        lblParity.setHorizontalAlignment(SwingConstants.RIGHT);
171
                        lblParity.setText(PluginServices.getText(this, "parity")+":");
172
                        lblStopBits = new JLabel();
173 4788 jaume
                        lblStopBits.setBounds(10, 123, 150, 20);
174 4752 jaume
                        lblStopBits.setHorizontalAlignment(SwingConstants.RIGHT);
175
                        lblStopBits.setText(PluginServices.getText(this, "stop_bits")+":");
176
                        lblDataBits = new JLabel();
177 4788 jaume
                        lblDataBits.setBounds(10, 100, 150, 20);
178 4752 jaume
                        lblDataBits.setHorizontalAlignment(SwingConstants.RIGHT);
179
                        lblDataBits.setText(PluginServices.getText(this, "data_bits")+":");
180
                        lblPortSpeed = new JLabel();
181 4788 jaume
                        lblPortSpeed.setBounds(10, 77, 150, 20);
182 4752 jaume
                        lblPortSpeed.setHorizontalAlignment(SwingConstants.RIGHT);
183
                        lblPortSpeed.setText(PluginServices.getText(this, "port_speed")+":");
184
                        pnlPortConfig = new JPanel();
185
                        pnlPortConfig.setLayout(null);
186 4788 jaume
                        pnlPortConfig.setBounds(0, 170, 329, 179);
187 4752 jaume
                        pnlPortConfig.setBorder(javax.swing.BorderFactory.createTitledBorder(
188
                                          null, PluginServices.getText(this, "port_config"),
189
                                          javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
190
                                          javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
191
                        pnlPortConfig.add(getTxtSampleRate(), null);
192
                        pnlPortConfig.add(getCmbPorts(), null);
193
                        lblSampleRate = new JLabel();
194 4788 jaume
                        lblSampleRate.setBounds(10, 31, 150, 20);
195 4752 jaume
                        lblSampleRate.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
196
                        lblSampleRate.setText(PluginServices.getText(this, "sample_rate")+":");
197
                        lblPort = new JLabel();
198 4788 jaume
                        lblPort.setBounds(10, 54, 150, 20);
199 4752 jaume
                        lblPort.setText(PluginServices.getText(this, "port")+":");
200
                        lblPort.setHorizontalAlignment(SwingConstants.RIGHT);
201
                        pnlPortConfig.add(lblSampleRate, null);
202
                        pnlPortConfig.add(lblPort, null);
203
                        pnlPortConfig.add(lblPortSpeed, null);
204
                        pnlPortConfig.add(getCmbPortSpeed(), null);
205
                        pnlPortConfig.add(getCmbDataBits(), null);
206
                        pnlPortConfig.add(getCmbStopBits(), null);
207
                        pnlPortConfig.add(getCmbParity(), null);
208
                        pnlPortConfig.add(lblDataBits, null);
209
                        pnlPortConfig.add(lblStopBits, null);
210
                        pnlPortConfig.add(lblParity, null);
211 4784 jaume
                        pnlPortConfig.add(lblMillis, null);
212 4752 jaume
                }
213
                return pnlPortConfig;
214
        }
215
216
        private JTextField getTxtSampleRate() {
217
                if (txtSampleRate == null) {
218
                        txtSampleRate = new JTextField();
219 4788 jaume
                        txtSampleRate.setBounds(165, 31, 60, 20);
220 4784 jaume
                        XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
221
                        if (!xml.contains("gps-sampleRate"))
222
                                xml.putProperty("gps-sampleRate", 2000);
223
                        txtSampleRate.setText(xml.getStringProperty("gps-sampleRate"));
224 4752 jaume
                }
225
                return txtSampleRate;
226
        }
227
228
        /**
229
         * This method initializes jComboBox
230
         *
231
         * @return javax.swing.JComboBox
232
         */
233
        private JComboBox getCmbPorts() {
234
                if (cmbPorts == null) {
235
                        cmbPorts = new JComboBox();
236 4788 jaume
                        cmbPorts.setBounds(165, 54, 154, 20);
237 4752 jaume
                        XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
238 4753 jaume
                        String portName = null;
239
                        if (xml.contains("gps-serialPortSpeed"))
240
                                portName = xml.getStringProperty("gps-serialPortSpeed");
241 4752 jaume
                        Enumeration portList = CommPortIdentifier.getPortIdentifiers();
242
                        while (portList.hasMoreElements()) {
243
                                CommPortIdentifier port = (CommPortIdentifier) portList.nextElement();
244
                                cmbPorts.addItem(port.getName());
245
                                if (portName != null && portName.equals(port.getName())) {
246
                                        cmbPorts.setSelectedIndex(cmbPorts.getItemCount()-1);
247
                                }
248
                        }
249
                }
250
                return cmbPorts;
251
        }
252
253
        /**
254
         * This method initializes jComboBox
255
         *
256
         * @return javax.swing.JComboBox
257
         */
258
        private JComboBox getCmbPortSpeed() {
259
                if (cmbPortSpeed == null) {
260
                        cmbPortSpeed = new JComboBox();
261 4788 jaume
                        cmbPortSpeed.setBounds(165, 77, 154, 20);
262 4752 jaume
                        cmbPortSpeed.setEditable(false);
263
                        XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
264
                        String[] speeds = new String[] {
265
                                         "75"  ,  "110" ,  "134" ,  "150" ,  "300",
266
                                        "600"  , "1200" , "1800" , "2400" , "4800",
267
                                        "9600" , "14400", "19200", "38400","57600",
268
                                        "115200"
269
                                };
270
                        int speed = xml.getIntProperty("gps-serialPortSpeed");
271
                        for (int i = 0; i < speeds.length; i++) {
272
                                cmbPortSpeed.addItem(speeds[i]);
273
                                if (Integer.parseInt(speeds[i]) == speed)
274
                                        cmbPortSpeed.setSelectedIndex(i);
275
                        }
276
277
                }
278
                return cmbPortSpeed;
279
        }
280
281
        /**
282
         * This method initializes btnOk
283
         *
284
         * @return javax.swing.JButton
285
         */
286
        private JButton getBtnOk() {
287
                if (btnOk == null) {
288
                        btnOk = new JButton();
289 4788 jaume
                        btnOk.setBounds(78, 353, 85, 25);
290 4752 jaume
                        btnOk.setText(PluginServices.getText(this, "ok"));
291
                        btnOk.addActionListener(new java.awt.event.ActionListener() {
292
                                public void actionPerformed(java.awt.event.ActionEvent e) {
293
                                        ok();
294
                                }
295
                        });
296
                }
297
                return btnOk;
298
        }
299
300
        /**
301
         * This method initializes btnCancel
302
         *
303
         * @return javax.swing.JButton
304
         */
305
        private JButton getBtnCancel() {
306
                if (btnCancel == null) {
307
                        btnCancel = new JButton();
308 4788 jaume
                        btnCancel.setBounds(163, 353, 85, 25);
309 4752 jaume
                        btnCancel.addActionListener(new java.awt.event.ActionListener() {
310
                                public void actionPerformed(java.awt.event.ActionEvent e) {
311
                                        cancel();
312
                                }
313
                        });
314
                        btnCancel.setText(PluginServices.getText(this, "cancel"));
315
                }
316
                return btnCancel;
317
        }
318
319 4827 jaume
        private void cancel() {
320 4752 jaume
                PluginServices.getMDIManager().closeView(this);
321
        }
322
323 4827 jaume
        private void ok() {
324 4752 jaume
                try {
325 4784 jaume
                        control.setSampleRate(Integer.parseInt(txtSampleRate.getText()));
326 4752 jaume
                        control.setPortSpeed(Integer.parseInt((String) cmbPortSpeed.getSelectedItem()));
327
                        control.setPortDataBits(Integer.parseInt((String) cmbDataBits.getSelectedItem()));
328
                        control.setPortStopBits((String) cmbStopBits.getSelectedItem());
329 4753 jaume
                        control.setPortParity(parities[cmbParity.getSelectedIndex()]);
330
                        String portName = (String) cmbPorts.getSelectedItem();
331
                        Enumeration portList = CommPortIdentifier.getPortIdentifiers();
332
                        boolean portFound = false;
333
                        while (portList.hasMoreElements()) {
334
                                CommPortIdentifier port = (CommPortIdentifier) portList.nextElement();
335
                                if (port.getName().equals(portName)) {
336
                                        control.setPort(port);
337
                                        portFound = true;
338
                                        break;
339
                                }
340
                        }
341
                        if (!portFound) throw new Exception();
342 4752 jaume
                        PluginServices.getMDIManager().closeView(this);
343
                } catch (Exception e) {
344
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), PluginServices.getText(this, "config_error"));
345
                }
346
        }
347
348
        /**
349
         * This method initializes jComboBox
350
         *
351
         * @return javax.swing.JComboBox
352
         */
353
        private JComboBox getCmbDataBits() {
354
                if (cmbDataBits == null) {
355
                        cmbDataBits = new JComboBox();
356 4788 jaume
                        cmbDataBits.setBounds(165, 100, 154, 20);
357 4752 jaume
                        String[] dataBitsValues;
358
                        XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
359
                        dataBitsValues = new String[] { "5", "6", "7", "8" };
360
                        int dataBits = xml.getIntProperty("gps-serialPortDataBits");
361
                        for (int i = 0; i < dataBitsValues.length; i++) {
362
                                cmbDataBits.addItem(dataBitsValues[i]);
363
                                if (Integer.parseInt(dataBitsValues[i]) == dataBits)
364
                                        cmbDataBits.setSelectedIndex(i);
365
                        }
366
                }
367
                return cmbDataBits;
368
        }
369
370
        /**
371
         * This method initializes jComboBox1
372
         *
373
         * @return javax.swing.JComboBox
374
         */
375
        private JComboBox getCmbStopBits() {
376
                if (cmbStopBits == null) {
377
                        cmbStopBits = new JComboBox();
378 4788 jaume
                        cmbStopBits.setBounds(165, 123, 154, 20);
379 4752 jaume
                        cmbStopBits.setEditable(false);
380
381
                        XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
382
                        String stopBits = xml.getStringProperty("gps-serialPortStopBits");
383
384
                        String[] stopBitsValues = new String[] { "1", "1.5", "2" };
385
                        for (int i = 0; i < stopBitsValues.length; i++) {
386
                                cmbStopBits.addItem(stopBitsValues[i]);
387
                                if (stopBitsValues[i].equals(stopBits))
388
                                        cmbStopBits.setSelectedIndex(i);
389
                        }
390
                }
391
                return cmbStopBits;
392
        }
393
394
        /**
395
         * This method initializes jComboBox2
396
         *
397
         * @return javax.swing.JComboBox
398
         */
399
        private JComboBox getCmbParity() {
400
                if (cmbParity == null) {
401
                        cmbParity = new JComboBox();
402 4788 jaume
                        cmbParity.setBounds(165, 146, 154, 20);
403 4752 jaume
                        cmbParity.setEditable(false);
404
                        XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
405
406
                        String parity = xml.getStringProperty("gps-serialPortParities");
407
408
                        for (int i = 0; i < parities.length; i++) {
409
                                cmbParity.addItem(PluginServices.getText(this, parities[i]));
410
                                if (parities[i].equals(parity))
411
                                        cmbParity.setSelectedIndex(i);
412
                        }
413
                }
414
                return cmbParity;
415
        }
416 4768 jaume
417
        /**
418
         * This method initializes btnCalibrate
419
         *
420
         * @return javax.swing.JButton
421
         */
422
        private JButton getBtnCalibrate() {
423
                if (btnCalibrate == null) {
424
                        btnCalibrate = new JButton();
425 4788 jaume
                        btnCalibrate.setBounds(225, 147, 96, 20);
426 4768 jaume
                        btnCalibrate.addActionListener(new java.awt.event.ActionListener() {
427
                                public void actionPerformed(java.awt.event.ActionEvent e) {
428 4784 jaume
                                        calibrate((String)cmbViews.getSelectedItem());
429 4768 jaume
                                }
430
                        });
431
                        btnCalibrate.setText(PluginServices.getText(this, "calibrate"));
432
433
                }
434
                return btnCalibrate;
435
        }
436
437 4784 jaume
        /**
438
         * Sets the tool that catches a point in the view specified by the viewName
439
         * parameter.
440
         * @param viewName
441
         */
442
        protected void calibrate(String viewName) {
443
                if (viewName!=null) {
444
                        View[] views = PluginServices.getMDIManager().getAllViews();
445
                        for (int i = 0; i < views.length; i++) {
446
                                if ((views[i] instanceof com.iver.cit.gvsig.gui.View)
447
                                                && (views[i].getViewInfo().getTitle().equals(viewName))){
448
                                        com.iver.cit.gvsig.gui.View v = (com.iver.cit.gvsig.gui.View) views[i];
449
450
                                        String toolName = "gpsCalibrator";
451
                                        MapControl mc = v.getMapControl();
452
                                        String currentTool = v.getMapControl().getTool();
453
                                        PointCalibrate calibrator = new PointCalibrate(v, currentTool);
454
                                        mc.addMapTool(toolName, new PointBehavior(calibrator));
455
                                        mc.setTool(toolName);
456 4827 jaume
                                        PluginServices.getMDIManager().addView(v);
457 4784 jaume
                                        return;
458
                                }
459
                        }
460 4768 jaume
                }
461
        }
462
463
        /**
464
         * This method initializes cmbViews
465
         *
466
         * @return javax.swing.JComboBox
467
         */
468
        private JComboBox getCmbViews() {
469
                if (cmbViews == null) {
470
                        cmbViews = new JComboBox();
471 4788 jaume
                        cmbViews.setBounds(8, 127, 313, 17);
472 4784 jaume
                        cmbViews.setToolTipText(PluginServices.getText(this, "select_the_view_to_do_calibration"));
473
                }
474 4788 jaume
                cmbViews.setEnabled(false);
475 4784 jaume
                cmbViews.removeAll();
476
                View[] aViews = PluginServices.getMDIManager().getAllViews();
477
                for (int i = 0; i < aViews.length; i++) {
478
                        if (aViews[i] instanceof com.iver.cit.gvsig.gui.View) {
479
                                cmbViews.addItem(aViews[i].getViewInfo().getTitle());
480 4805 jaume
                                cmbViews.setEnabled(true);
481 4768 jaume
                        }
482
                }
483 4788 jaume
                btnCalibrate.setEnabled(cmbViews.isEnabled());
484 4768 jaume
                return cmbViews;
485
        }
486 4827 jaume
487
        public Object getViewModel() {
488
                return viewModel;
489
        }
490 4805 jaume
}