Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.geocoding.extension / src / org / gvsig / geocoding / gui / address / AddressRangePanel.java @ 32526

History | View | Annotate | Download (14.3 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 * 
21
 */
22

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2008 Prodevelop S.L. main developer
26
 */
27

    
28
package org.gvsig.geocoding.gui.address;
29

    
30
import java.awt.Color;
31
import java.io.File;
32
import java.util.Iterator;
33
import java.util.List;
34
import java.util.Vector;
35

    
36
import javax.swing.ImageIcon;
37
import javax.swing.JComboBox;
38
import javax.swing.JLabel;
39
import javax.swing.JPanel;
40
import javax.swing.JScrollPane;
41
import javax.swing.JTable;
42
import javax.swing.JTextField;
43
import javax.swing.table.DefaultTableCellRenderer;
44
import javax.swing.table.DefaultTableModel;
45
import javax.swing.table.TableColumn;
46

    
47
import org.gvsig.andami.PluginServices;
48
import org.gvsig.fmap.dal.exception.DataException;
49
import org.gvsig.fmap.dal.feature.Feature;
50
import org.gvsig.fmap.dal.feature.FeatureSet;
51
import org.gvsig.fmap.dal.feature.FeatureStore;
52
import org.gvsig.geocoding.GeocodingTags;
53
import org.gvsig.geocoding.address.Address;
54
import org.gvsig.geocoding.address.Literal;
55
import org.gvsig.geocoding.address.NumberAddress;
56
import org.gvsig.geocoding.address.impl.DefaultLiteral;
57
import org.gvsig.geocoding.address.impl.DefaultNumberAddress;
58
import org.gvsig.geocoding.extension.GeocodingController;
59
import org.gvsig.geocoding.styles.AbstractGeocodingStyle;
60
import org.gvsig.geocoding.styles.impl.SimpleRange;
61
import org.gvsig.geocoding.utils.GeocodingExtTags;
62
import org.slf4j.Logger;
63
import org.slf4j.LoggerFactory;
64

    
65
/**
66
 * Address simple and double range panel
67
 * 
68
 * @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
69
 * @author <a href="mailto:vsanjaime@prodevelop.es"> Vicente Sanjaime Calvet</a>
70
 */
71
public class AddressRangePanel extends AbstractAddressPanel {
72

    
73
        private static final long serialVersionUID = 1L;
74
        private Logger log = LoggerFactory.getLogger(AddressRangePanel.class);
75

    
76
        private GeocodingController control = null;
77

    
78
        private JLabel jLabNumber;
79
        private JLabel jLabType;
80
        private JPanel jPanNumber;
81
        private JPanel jPanTable;
82
        private JScrollPane jScrollTable;
83
        private JTable jTableAddress;
84
        private JTextField jTextNumber;
85
        private JComboBox jComboNumber;
86
        private JPanel jPanId;
87
        private JLabel jLabId;
88
        private JComboBox jComboId;
89

    
90
        /**
91
         * Constructor
92
         */
93
        public AddressRangePanel(GeocodingController control, Literal literal) {
94
                this.control = control;
95
                initComponents();
96

    
97
                setImages();
98
                setMesages();
99

    
100
                // create table model
101
                if (control.getGmodel().isSimple()) {
102
                        createSimpleTableModel(literal);
103
                } else {
104
                        List<String> descs = control.getGmodel()
105
                                        .getListDescriptorSelectedTable();
106
                        createTableTableModel(literal, descs);
107
                        jComboId.setModel(createFieldsComboModel(descs));
108
                        jComboNumber.setModel(createFieldsComboModel(descs));
109
                }
110

    
111
        }
112

    
113
        /**
114
         * Set images in the panel
115
         */
116
        private void setImages() {
117
                String baseDir = PluginServices.getPluginServices(this)
118
                                .getClassLoader().getBaseDir();
119

    
120
                AbstractGeocodingStyle style = control.getPattern().getDataSource()
121
                                .getStyle();
122
                if (style instanceof SimpleRange) {
123
                        jLabType.setIcon(new ImageIcon(baseDir + File.separator + "images"
124
                                        + File.separator + "single_rank.png"));
125
                } else {
126
                        jLabType.setIcon(new ImageIcon(baseDir + File.separator + "images"
127
                                        + File.separator + "double_rank.png"));
128
                }
129
        }
130

    
131
        /**
132
         * Set strings in the panel
133
         */
134
        private void setMesages() {
135
                PluginServices ps = PluginServices.getPluginServices(this);
136

    
137
                jLabNumber.setText(ps.getText("xnumAddress"));
138
                jLabId.setText(ps.getText("xidfield"));
139

    
140
                DefaultTableModel model = (DefaultTableModel) jTableAddress.getModel();
141
                String element = ps.getText("xelement");
142
                String address1 = ps.getText("xvalue");
143
                Object[] headers = { element, address1 };
144
                model.setColumnIdentifiers(headers);
145
        }
146

    
147
        /**
148
         * Initialize components
149
         */
150
        private void initComponents() {
151
                java.awt.GridBagConstraints gridBagConstraints;
152

    
153
                jLabType = new JLabel();
154
                jPanTable = new JPanel();
155
                jScrollTable = new JScrollPane();
156
                jTableAddress = new JTable();
157
                jPanNumber = new JPanel();
158
                jLabNumber = new JLabel();
159
                jTextNumber = new JTextField();
160
                jComboNumber = new JComboBox();
161
                jLabId = new JLabel();
162
                jComboId = new JComboBox();
163
                jPanId = new JPanel();
164

    
165
                setLayout(new java.awt.GridBagLayout());
166

    
167
                gridBagConstraints = new java.awt.GridBagConstraints();
168
                gridBagConstraints.insets = new java.awt.Insets(3, 15, 5, 10);
169
                add(jLabType, gridBagConstraints);
170

    
171
                jPanTable.setLayout(new java.awt.GridBagLayout());
172

    
173
                jPanId.setLayout(new java.awt.GridBagLayout());
174

    
175
                if (!control.getGmodel().isSimple()) {
176
                        jLabId.setText("Field ID");
177
                        jLabId.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
178
                        gridBagConstraints = new java.awt.GridBagConstraints();
179
                        gridBagConstraints.gridx = 0;
180
                        gridBagConstraints.gridy = 0;
181
                        gridBagConstraints.insets = new java.awt.Insets(4, 10, 4, 5);
182
                        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
183
                        gridBagConstraints.weightx = 1.0;
184
                        jPanId.add(jLabId, gridBagConstraints);
185

    
186
                        jComboId.setModel(new javax.swing.DefaultComboBoxModel(
187
                                        new String[] { "" }));
188
                        jComboId.setMinimumSize(new java.awt.Dimension(200, 20));
189
                        jComboId.setName("jComboId");
190

    
191
                        gridBagConstraints = new java.awt.GridBagConstraints();
192
                        gridBagConstraints.gridx = 1;
193
                        gridBagConstraints.gridy = 0;
194
                        gridBagConstraints.insets = new java.awt.Insets(4, 0, 4, 4);
195
                        jPanId.add(jComboId, gridBagConstraints);
196

    
197
                        gridBagConstraints = new java.awt.GridBagConstraints();
198
                        gridBagConstraints.gridx = 0;
199
                        gridBagConstraints.gridy = 0;
200
                        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
201
                        gridBagConstraints.weightx = 1.0;
202
                        gridBagConstraints.insets = new java.awt.Insets(0, 0, 2, 0);
203
                        jPanTable.add(jPanId, gridBagConstraints);
204
                }
205

    
206
                jTableAddress
207
                                .setModel(new javax.swing.table.DefaultTableModel(
208
                                                new Object[][] { { null, null }, { null, null },
209
                                                                { null, null } }, new String[] { "Element",
210
                                                                "Value" }) {
211
                                        Class[] types = new Class[] { java.lang.String.class,
212
                                                        java.lang.String.class };
213
                                        boolean[] canEdit = new boolean[] { false, true };
214

    
215
                                        public Class getColumnClass(int columnIndex) {
216
                                                return types[columnIndex];
217
                                        }
218

    
219
                                        public boolean isCellEditable(int rowIndex, int columnIndex) {
220
                                                return canEdit[columnIndex];
221
                                        }
222
                                });
223

    
224
                jScrollTable.setViewportView(jTableAddress);
225

    
226
                gridBagConstraints = new java.awt.GridBagConstraints();
227
                gridBagConstraints.gridx = 0;
228
                gridBagConstraints.gridy = 1;
229
                gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
230
                gridBagConstraints.weightx = 1.0;
231
                gridBagConstraints.weighty = 1.0;
232
                jPanTable.add(jScrollTable, gridBagConstraints);
233

    
234
                jPanNumber.setLayout(new java.awt.GridBagLayout());
235

    
236
                jLabNumber.setText("Number");
237
                jLabNumber.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
238
                gridBagConstraints = new java.awt.GridBagConstraints();
239
                gridBagConstraints.gridx = 0;
240
                gridBagConstraints.gridy = 0;
241
                gridBagConstraints.insets = new java.awt.Insets(4, 10, 4, 5);
242
                gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
243
                gridBagConstraints.weightx = 1.0;
244
                jPanNumber.add(jLabNumber, gridBagConstraints);
245

    
246
                if (control.getGmodel().isSimple()) {
247
                        jTextNumber.setText("1");
248
                        jTextNumber.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
249
                        jTextNumber.setMinimumSize(new java.awt.Dimension(30, 19));
250
                        jTextNumber.setPreferredSize(new java.awt.Dimension(70, 19));
251
                        jTextNumber.addFocusListener(new java.awt.event.FocusAdapter() {
252
                                public void focusLost(java.awt.event.FocusEvent evt) {
253
                                        evNumberLostFocus(evt);
254
                                }
255
                        });
256
                        gridBagConstraints = new java.awt.GridBagConstraints();
257
                        gridBagConstraints.gridx = 1;
258
                        gridBagConstraints.gridy = 0;
259
                        gridBagConstraints.insets = new java.awt.Insets(4, 0, 4, 4);
260
                        jPanNumber.add(jTextNumber, gridBagConstraints);
261
                } else {
262

    
263
                        jComboNumber.setModel(new javax.swing.DefaultComboBoxModel(
264
                                        new String[] { "" }));
265
                        jComboNumber.setMinimumSize(new java.awt.Dimension(200, 20));
266
                        jComboNumber.setName("jComboNumber");
267

    
268
                        gridBagConstraints = new java.awt.GridBagConstraints();
269
                        gridBagConstraints.gridx = 1;
270
                        gridBagConstraints.gridy = 0;
271
                        gridBagConstraints.insets = new java.awt.Insets(4, 0, 4, 4);
272
                        jPanNumber.add(jComboNumber, gridBagConstraints);
273
                }
274

    
275
                gridBagConstraints = new java.awt.GridBagConstraints();
276
                gridBagConstraints.gridx = 0;
277
                gridBagConstraints.gridy = 2;
278
                gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
279
                gridBagConstraints.weightx = 1.0;
280
                gridBagConstraints.insets = new java.awt.Insets(2, 0, 0, 0);
281
                jPanTable.add(jPanNumber, gridBagConstraints);
282

    
283
                gridBagConstraints = new java.awt.GridBagConstraints();
284
                gridBagConstraints.gridx = 1;
285
                gridBagConstraints.gridy = 0;
286
                gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
287
                gridBagConstraints.weightx = 1.0;
288
                gridBagConstraints.weighty = 1.0;
289
                gridBagConstraints.insets = new java.awt.Insets(2, 15, 2, 10);
290
                add(jPanTable, gridBagConstraints);
291
        }
292

    
293
        /**
294
         * Number text field lost focus
295
         * 
296
         * @param evt
297
         */
298
        private void evNumberLostFocus(java.awt.event.FocusEvent evt) {
299
                String txt = jTextNumber.getText();
300
                try {
301
                        Integer.parseInt(txt);
302
                } catch (Exception e) {
303
                        jTextNumber.setText("1");
304
                        jTextNumber.requestFocus();
305
                }
306
        }
307

    
308
        /**
309
         * Create a simple table model from literal components
310
         * 
311
         * @param lit
312
         */
313
        private void createSimpleTableModel(Literal lit) {
314
                DefaultTableModel model = (DefaultTableModel) jTableAddress.getModel();
315

    
316
                // first column
317
                TableColumn column0 = jTableAddress.getColumnModel().getColumn(0);
318
                DefaultTableCellRenderer render = new DefaultTableCellRenderer();
319
                render.setBackground(new Color(255, 253, 224));
320
                column0.setCellRenderer(render);
321

    
322
                // fill elements
323
                super.clearTableModel(model);
324

    
325
                for (String key : lit.keySet()) {
326
                        if(key.compareTo(GeocodingTags.FIRSTNUMBER)==0 || key.compareTo(GeocodingTags.LASTNUMBER)==0 || key.compareTo(GeocodingTags.FIRSTRIGHTNUMBER)==0 || key.compareTo(GeocodingTags.FIRSTLEFTNUMBER)==0 || key.compareTo(GeocodingTags.LASTRIGHTNUMBER)==0 || key.compareTo(GeocodingTags.LASTLEFTNUMBER)==0 ){
327
                                //nothing to do
328
                        }else{
329
                                Object[] row = { key, "" };
330
                                model.addRow(row);
331
                        }                        
332
                }
333
                jTableAddress.setModel(model);
334
        }
335

    
336
        /**
337
         * Create a simple table model from literal components
338
         * 
339
         * @param lit
340
         */
341
        private void createTableTableModel(Literal lit, List<String> descs) {
342
                DefaultTableModel model = (DefaultTableModel) jTableAddress.getModel();
343

    
344
                // first column
345
                TableColumn column0 = jTableAddress.getColumnModel().getColumn(0);
346
                DefaultTableCellRenderer render = new DefaultTableCellRenderer();
347
                render.setBackground(new Color(255, 253, 224));
348
                column0.setCellRenderer(render);
349

    
350
                // fill elements
351
                super.clearTableModel(model);
352

    
353
                // Put combos in the column 1
354
                super.putCombosInTable(jTableAddress, descs, 1);
355

    
356
                // put the elements in the column 0
357
                for (String key : lit.keySet()) {
358
                        if(key.compareTo(GeocodingTags.FIRSTNUMBER)==0 || key.compareTo(GeocodingTags.LASTNUMBER)==0 || key.compareTo(GeocodingTags.FIRSTRIGHTNUMBER)==0 || key.compareTo(GeocodingTags.FIRSTLEFTNUMBER)==0 || key.compareTo(GeocodingTags.LASTRIGHTNUMBER)==0 || key.compareTo(GeocodingTags.LASTLEFTNUMBER)==0){
359
                                //nothing to do
360
                        }else{
361
                                Object[] row = { key, "" };
362
                                model.addRow(row);
363
                        }        
364
                }
365
                jTableAddress.setModel(model);
366
        }
367

    
368
        /**
369
         * get address from user interface
370
         * 
371
         * @param model
372
         * @return
373
         */
374
        public Address getSimpleAddress() {
375

    
376
                jTableAddress.validate();
377
                jTableAddress.setRowSelectionInterval(0, 0);
378
                DefaultTableModel model = (DefaultTableModel) jTableAddress.getModel();
379
                NumberAddress address = new DefaultNumberAddress();
380
                Literal mainLiteral = new DefaultLiteral();
381
                Vector vec = model.getDataVector();
382

    
383
                for (int i = 0; i < vec.size(); i++) {
384
                        Vector vecc = (Vector) vec.elementAt(i);
385
                        String key = (String) vecc.elementAt(0);
386
                        String value = (String) vecc.elementAt(1);
387
                        mainLiteral.put(key, value);
388
                }
389
                address.setMainLiteral(mainLiteral);
390
                String strNumber = jTextNumber.getText();
391
                address.setNumber(Integer.parseInt(strNumber));
392
                return address;
393
        }
394

    
395
        /**
396
         * Get the addres from table
397
         * 
398
         * @param model
399
         * @param row
400
         * @return
401
         */
402
        public Address getTableAddress(int row) {
403

    
404
                jTableAddress.validate();
405
                jTableAddress.setRowSelectionInterval(0, 0);
406
                DefaultTableModel model = (DefaultTableModel) jTableAddress.getModel();
407

    
408
                // save in the gmodel the descriptor of identifier field
409
                String descid = (String) jComboId.getModel().getSelectedItem();
410
                control.getGmodel().setIdMasiveTable(descid);
411

    
412
                // Get store from Gmodel and the feature
413
                FeatureStore store = control.getGmodel().getSelectedTableStore();
414
                FeatureSet features = null;
415
                Feature feature = null;
416
                try {
417
                        features = store.getFeatureSet();
418
                        Iterator<Feature> it = features.iterator(row);
419
                        feature = it.next();
420
                } catch (DataException e) {
421
                        log.error("Get the feature of FeatureStore", e);
422
                }
423

    
424
                // Create the address
425
                NumberAddress address = new DefaultNumberAddress();
426
                Literal literal = new DefaultLiteral();
427
                Vector vec = model.getDataVector();
428

    
429
                for (int i = 0; i < vec.size(); i++) {
430
                        Vector vecc = (Vector) vec.elementAt(i);
431
                        String key = (String) vecc.elementAt(0);
432
                        String fieldName = (String) vecc.elementAt(1);
433
                        Object obj = feature.get(fieldName);
434
                        String value = obj.toString();
435
                        literal.put(key, value);
436
                }
437

    
438
                address.setMainLiteral(literal);
439
                String nfieldName = (String) jComboNumber.getModel().getSelectedItem();
440
                Object nobj = feature.get(nfieldName);
441
                String strNumber = nobj.toString();
442
                int ii = 1;
443
                try {
444
                        ii = Integer.parseInt(strNumber);
445
                        address.setNumber(ii);
446
                } catch (Exception e) {
447
                        address.setNumber(ii);
448
                }
449

    
450
                return address;
451
        }
452

    
453
}