Statistics
| Revision:

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

History | View | Annotate | Download (11.6 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 4753 2006-04-07 08:27:48Z jaume $
45
 * $Log$
46
 * Revision 1.2  2006-04-07 08:27:48  jaume
47
 * *** empty log message ***
48
 *
49
 * Revision 1.1  2006/04/06 10:35:14  jaume
50
 * *** empty log message ***
51
 *
52
 *
53
 */
54
package org.gvsig.gps.panel;
55

    
56
import gnu.io.CommPortIdentifier;
57

    
58
import java.awt.Component;
59
import java.util.Enumeration;
60

    
61
import javax.swing.JButton;
62
import javax.swing.JComboBox;
63
import javax.swing.JLabel;
64
import javax.swing.JOptionPane;
65
import javax.swing.JPanel;
66
import javax.swing.JTextField;
67
import javax.swing.SwingConstants;
68

    
69
import com.iver.andami.PluginServices;
70
import com.iver.andami.ui.mdiManager.View;
71
import com.iver.andami.ui.mdiManager.ViewInfo;
72
import com.iver.utiles.XMLEntity;
73

    
74
public class GPSConfigPanel extends JPanel implements View{
75
        private JComboBox cmbPorts = null;
76
        private JPanel pnlPortConfig = null;
77
        private JTextField txtSampleRate;
78
        private JLabel lblSampleRate;
79
        private JLabel lblPort;
80
        private JLabel lblPortSpeed = null;
81
        private JComboBox cmbPortSpeed = null;
82
        private JButton btnOk = null;
83
        private JButton btnCancel = null;
84
        private GPSControlPanel control;
85
        private JComboBox cmbDataBits = null;
86
        private JLabel lblDataBits = null;
87
        private JLabel lblStopBits = null;
88
        private JComboBox cmbStopBits = null;
89
        private JComboBox cmbParity = null;
90
        private JLabel lblParity = null;
91
        private String[] parities = new String[] { "even", "odd", "mark", "space", "none" };
92
        
93

    
94
        /**
95
         * This is the default constructor
96
         */
97
        public GPSConfigPanel(GPSControlPanel control) {
98
                super();
99
                this.control = control;
100
                initialize();
101
        }
102

    
103
        /**
104
         * This method initializes this
105
         * 
106
         * @return void
107
         */
108
        private void initialize() {
109
                this.setLayout(null);
110
                this.setSize(247, 214);
111
                this.add(getPnlPortConfig(), null);
112
                this.add(getBtnOk(), null);
113
                this.add(getBtnCancel(), null);
114
        }
115

    
116
        public ViewInfo getViewInfo() {
117
                ViewInfo m_viewInfo = new ViewInfo(ViewInfo.ICONIFIABLE);
118
                m_viewInfo.setTitle(PluginServices.getText(this, "gps_settings"));
119
                m_viewInfo.setWidth(this.getWidth()+8);
120
                m_viewInfo.setHeight(this.getHeight()+8);
121
                return m_viewInfo;
122
        }
123

    
124
        /**
125
         * This method initializes jPanel        
126
         *         
127
         * @return javax.swing.JPanel        
128
         */    
129
        private JPanel getPnlPortConfig() {
130
                if (pnlPortConfig == null) {
131
                        lblParity = new JLabel();
132
                        lblParity.setBounds(10, 146, 84, 20);
133
                        lblParity.setHorizontalAlignment(SwingConstants.RIGHT);
134
                        lblParity.setText(PluginServices.getText(this, "parity")+":");
135
                        lblStopBits = new JLabel();
136
                        lblStopBits.setBounds(10, 123, 84, 20);
137
                        lblStopBits.setHorizontalAlignment(SwingConstants.RIGHT);
138
                        lblStopBits.setText(PluginServices.getText(this, "stop_bits")+":");
139
                        lblDataBits = new JLabel();
140
                        lblDataBits.setBounds(10, 100, 84, 20);
141
                        lblDataBits.setHorizontalAlignment(SwingConstants.RIGHT);
142
                        lblDataBits.setText(PluginServices.getText(this, "data_bits")+":");
143
                        lblPortSpeed = new JLabel();
144
                        lblPortSpeed.setBounds(10, 77, 84, 20);
145
                        lblPortSpeed.setHorizontalAlignment(SwingConstants.RIGHT);
146
                        lblPortSpeed.setText(PluginServices.getText(this, "port_speed")+":");
147
                        pnlPortConfig = new JPanel();
148
                        pnlPortConfig.setLayout(null);
149
                        pnlPortConfig.setBounds(0, 0, 247, 179);
150
                        pnlPortConfig.setBorder(javax.swing.BorderFactory.createTitledBorder(
151
                                          null, PluginServices.getText(this, "port_config"),
152
                                          javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, 
153
                                          javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
154
                        pnlPortConfig.add(getTxtSampleRate(), null);
155
                        pnlPortConfig.add(getCmbPorts(), null);
156
                        lblSampleRate = new JLabel();
157
                        lblSampleRate.setBounds(10, 31, 84, 20);
158
                        lblSampleRate.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
159
                        lblSampleRate.setText(PluginServices.getText(this, "sample_rate")+":");
160
                        lblPort = new JLabel();
161
                        lblPort.setBounds(10, 54, 84, 20);
162
                        lblPort.setText(PluginServices.getText(this, "port")+":");
163
                        lblPort.setHorizontalAlignment(SwingConstants.RIGHT);
164
                        pnlPortConfig.add(lblSampleRate, null);
165
                        pnlPortConfig.add(lblPort, null);
166
                        pnlPortConfig.add(lblPortSpeed, null);
167
                        pnlPortConfig.add(getCmbPortSpeed(), null);
168
                        pnlPortConfig.add(getCmbDataBits(), null);
169
                        pnlPortConfig.add(getCmbStopBits(), null);
170
                        pnlPortConfig.add(getCmbParity(), null);
171
                        pnlPortConfig.add(lblDataBits, null);
172
                        pnlPortConfig.add(lblStopBits, null);
173
                        pnlPortConfig.add(lblParity, null);
174
                }
175
                return pnlPortConfig;
176
        }
177

    
178
        private JTextField getTxtSampleRate() {
179
                if (txtSampleRate == null) {
180
                        txtSampleRate = new JTextField();
181
                        txtSampleRate.setBounds(101, 31, 136, 20);
182
                        txtSampleRate.addKeyListener(new java.awt.event.KeyAdapter() { 
183
                                public void keyTyped(java.awt.event.KeyEvent e) {    
184
//                                        try {
185
//                                                float time = Float.parseFloat(txtSampleRate.getText());
186
//                                                gps.setSampleRate(Math.abs((int) (time * 1000)));
187
//                                                
188
//                                        } catch (Exception ex) {
189
//                                                txtSampleRate.setText("");
190
//                                        };
191
                                }
192
                        });
193
                }
194
                return txtSampleRate;
195
        }
196
        
197
        /**
198
         * This method initializes jComboBox        
199
         *         
200
         * @return javax.swing.JComboBox        
201
         */    
202
        private JComboBox getCmbPorts() {
203
                if (cmbPorts == null) {
204
                        cmbPorts = new JComboBox();
205
                        cmbPorts.setBounds(101, 54, 136, 20);
206
                        XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
207
                        String portName = null;
208
                        if (xml.contains("gps-serialPortSpeed"))
209
                                portName = xml.getStringProperty("gps-serialPortSpeed");
210
                        Enumeration portList = CommPortIdentifier.getPortIdentifiers();
211
                        while (portList.hasMoreElements()) {
212
                                CommPortIdentifier port = (CommPortIdentifier) portList.nextElement();
213
                                cmbPorts.addItem(port.getName());
214
                                if (portName != null && portName.equals(port.getName())) {
215
                                        cmbPorts.setSelectedIndex(cmbPorts.getItemCount()-1);
216
                                }
217
                        }
218
                }
219
                return cmbPorts;
220
        }
221

    
222
        /**
223
         * This method initializes jComboBox        
224
         *         
225
         * @return javax.swing.JComboBox        
226
         */    
227
        private JComboBox getCmbPortSpeed() {
228
                if (cmbPortSpeed == null) {
229
                        cmbPortSpeed = new JComboBox();
230
                        cmbPortSpeed.setBounds(101, 77, 136, 20);
231
                        cmbPortSpeed.setEditable(false);
232
                        XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
233
                        String[] speeds = new String[] {
234
                                         "75"  ,  "110" ,  "134" ,  "150" ,  "300",
235
                                        "600"  , "1200" , "1800" , "2400" , "4800",
236
                                        "9600" , "14400", "19200", "38400","57600",
237
                                        "115200"        
238
                                };
239
                        int speed = xml.getIntProperty("gps-serialPortSpeed");
240
                        for (int i = 0; i < speeds.length; i++) {
241
                                cmbPortSpeed.addItem(speeds[i]);
242
                                if (Integer.parseInt(speeds[i]) == speed)
243
                                        cmbPortSpeed.setSelectedIndex(i);
244
                        }
245
                        
246
                }
247
                return cmbPortSpeed;
248
        }
249

    
250
        /**
251
         * This method initializes btnOk        
252
         *         
253
         * @return javax.swing.JButton        
254
         */    
255
        private JButton getBtnOk() {
256
                if (btnOk == null) {
257
                        btnOk = new JButton();
258
                        btnOk.setBounds(38, 183, 85, 25);
259
                        btnOk.setText(PluginServices.getText(this, "ok"));
260
                        btnOk.addActionListener(new java.awt.event.ActionListener() { 
261
                                public void actionPerformed(java.awt.event.ActionEvent e) {    
262
                                        ok();
263
                                }
264
                        });
265
                }
266
                return btnOk;
267
        }
268

    
269
        /**
270
         * This method initializes btnCancel        
271
         *         
272
         * @return javax.swing.JButton        
273
         */    
274
        private JButton getBtnCancel() {
275
                if (btnCancel == null) {
276
                        btnCancel = new JButton();
277
                        btnCancel.setBounds(123, 183, 85, 25);
278
                        btnCancel.addActionListener(new java.awt.event.ActionListener() { 
279
                                public void actionPerformed(java.awt.event.ActionEvent e) {    
280
                                        cancel();
281
                                }
282
                        });
283
                        btnCancel.setText(PluginServices.getText(this, "cancel"));
284
                }
285
                return btnCancel;
286
        }
287

    
288
        protected void cancel() {
289
                PluginServices.getMDIManager().closeView(this);                
290
        }
291
        
292
        protected void ok() {
293
                try {
294
                        control.setPortSpeed(Integer.parseInt((String) cmbPortSpeed.getSelectedItem()));
295
                        control.setPortDataBits(Integer.parseInt((String) cmbDataBits.getSelectedItem()));
296
                        control.setPortStopBits((String) cmbStopBits.getSelectedItem());
297
                        control.setPortParity(parities[cmbParity.getSelectedIndex()]);
298
                        String portName = (String) cmbPorts.getSelectedItem();
299
                        Enumeration portList = CommPortIdentifier.getPortIdentifiers();
300
                        boolean portFound = false;
301
                        while (portList.hasMoreElements()) {
302
                                CommPortIdentifier port = (CommPortIdentifier) portList.nextElement();
303
                                if (port.getName().equals(portName)) {
304
                                        control.setPort(port);
305
                                        portFound = true;
306
                                        break;
307
                                }
308
                        }
309
                        if (!portFound) throw new Exception();
310
                        PluginServices.getMDIManager().closeView(this);
311
                } catch (Exception e) {
312
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), PluginServices.getText(this, "config_error"));
313
                }
314
        }
315

    
316
        /**
317
         * This method initializes jComboBox        
318
         *         
319
         * @return javax.swing.JComboBox        
320
         */    
321
        private JComboBox getCmbDataBits() {
322
                if (cmbDataBits == null) {
323
                        cmbDataBits = new JComboBox();
324
                        cmbDataBits.setBounds(101, 100, 136, 20);
325
                        String[] dataBitsValues;
326
                        XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
327
                        dataBitsValues = new String[] { "5", "6", "7", "8" };
328
                        int dataBits = xml.getIntProperty("gps-serialPortDataBits");
329
                        for (int i = 0; i < dataBitsValues.length; i++) {
330
                                cmbDataBits.addItem(dataBitsValues[i]);
331
                                if (Integer.parseInt(dataBitsValues[i]) == dataBits)
332
                                        cmbDataBits.setSelectedIndex(i);
333
                        }
334
                }
335
                return cmbDataBits;
336
        }
337

    
338
        /**
339
         * This method initializes jComboBox1        
340
         *         
341
         * @return javax.swing.JComboBox        
342
         */    
343
        private JComboBox getCmbStopBits() {
344
                if (cmbStopBits == null) {
345
                        cmbStopBits = new JComboBox();
346
                        cmbStopBits.setBounds(101, 123, 136, 20);
347
                        cmbStopBits.setEditable(false);
348
                        
349
                        XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
350
                        String stopBits = xml.getStringProperty("gps-serialPortStopBits");
351
                        
352
                        String[] stopBitsValues = new String[] { "1", "1.5", "2" };
353
                        for (int i = 0; i < stopBitsValues.length; i++) {
354
                                cmbStopBits.addItem(stopBitsValues[i]);
355
                                if (stopBitsValues[i].equals(stopBits))
356
                                        cmbStopBits.setSelectedIndex(i);
357
                        }
358
                }
359
                return cmbStopBits;
360
        }
361

    
362
        /**
363
         * This method initializes jComboBox2        
364
         *         
365
         * @return javax.swing.JComboBox        
366
         */    
367
        private JComboBox getCmbParity() {
368
                if (cmbParity == null) {
369
                        cmbParity = new JComboBox();
370
                        cmbParity.setBounds(101, 146, 136, 20);
371
                        cmbParity.setEditable(false);
372
                        XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
373
                        
374
                        String parity = xml.getStringProperty("gps-serialPortParities");
375
                        
376
                        for (int i = 0; i < parities.length; i++) {
377
                                cmbParity.addItem(PluginServices.getText(this, parities[i]));
378
                                if (parities[i].equals(parity))
379
                                        cmbParity.setSelectedIndex(i);
380
                        }
381
                }
382
                return cmbParity;
383
        }
384
}  //  @jve:decl-index=0:visual-constraint="13,19"