Statistics
| Revision:

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

History | View | Annotate | Download (11.6 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.awt.event.KeyEvent;
32
import java.awt.event.KeyListener;
33
import java.io.File;
34
import java.util.Iterator;
35
import java.util.List;
36
import java.util.Vector;
37

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

    
48
import org.gvsig.fmap.dal.exception.DataException;
49
import org.gvsig.fmap.dal.feature.Feature;
50
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
51
import org.gvsig.fmap.dal.feature.FeatureSet;
52
import org.gvsig.fmap.dal.feature.FeatureStore;
53
import org.gvsig.geocoding.Address;
54
import org.gvsig.geocoding.AddressComponent;
55
import org.gvsig.geocoding.Literal;
56
import org.gvsig.geocoding.NumberAddress;
57
import org.gvsig.geocoding.RelationsComponent;
58
import org.gvsig.geocoding.extension.GeocoController;
59
import org.gvsig.geocoding.gui.newpattern.ComboDesc;
60
import org.gvsig.geocoding.styles.AbstractGeocodingStyle;
61
import org.gvsig.geocoding.styles.SimpleRange;
62
import org.gvsig.geocoding.utils.GeocoUtils;
63
import org.slf4j.Logger;
64
import org.slf4j.LoggerFactory;
65

    
66
import com.iver.andami.PluginServices;
67

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

    
76
        private static final long serialVersionUID = 1L;
77
        private Logger log = LoggerFactory.getLogger(AddressRangePanel.class);
78

    
79
        private GeocoController control = null;
80

    
81
        private JLabel jLabNumber;
82
        private JLabel jLabType;
83
        private JPanel jPanNumber;
84
        private JPanel jPanTable;
85
        private JScrollPane jScrollTable;
86
        private JTable jTableAddress;
87
        private JTextField jTextNumber;
88

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

    
96
                setImages();
97
                setMesages();
98

    
99
                // create table model
100
                if (control.getGmodel().isSimple()) {
101
                        createSimpleTableModel(literal);
102
                } else {
103
                        List<FeatureAttributeDescriptor> descs = control.getGmodel()
104
                                        .getListDescriptorSelectedTable();
105
                        createTableTableModel(literal, descs);
106
                }
107

    
108
        }
109

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

    
117
                AbstractGeocodingStyle style = control.getPattern().getSource()
118
                                .getStyle();
119
                if (style instanceof SimpleRange) {
120
                        jLabType.setIcon(new ImageIcon(baseDir + File.separator + "images"
121
                                        + File.separator + "new" + File.separator
122
                                        + "simplerange.png"));
123
                } else {
124
                        jLabType.setIcon(new ImageIcon(baseDir + File.separator + "images"
125
                                        + File.separator + "new" + File.separator
126
                                        + "doublerange.png"));
127
                }
128
        }
129

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

    
136
                jLabNumber.setText(ps.getText("xnumAddress"));
137

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

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

    
151
                jLabType = new javax.swing.JLabel();
152
                jPanTable = new javax.swing.JPanel();
153
                jScrollTable = new javax.swing.JScrollPane();
154
                jTableAddress = new javax.swing.JTable();
155
                jPanNumber = new javax.swing.JPanel();
156
                jLabNumber = new javax.swing.JLabel();
157
                jTextNumber = new javax.swing.JTextField();
158

    
159
                setLayout(new java.awt.GridBagLayout());
160

    
161
                gridBagConstraints = new java.awt.GridBagConstraints();
162
                gridBagConstraints.insets = new java.awt.Insets(5, 15, 5, 10);
163
                add(jLabType, gridBagConstraints);
164

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

    
167
                jTableAddress
168
                                .setModel(new javax.swing.table.DefaultTableModel(
169
                                                new Object[][] { { null, null }, { null, null },
170
                                                                { null, null } }, new String[] { "Element",
171
                                                                "Value" }) {
172
                                        Class[] types = new Class[] { java.lang.String.class,
173
                                                        java.lang.String.class };
174
                                        boolean[] canEdit = new boolean[] { false, true };
175

    
176
                                        public Class getColumnClass(int columnIndex) {
177
                                                return types[columnIndex];
178
                                        }
179

    
180
                                        public boolean isCellEditable(int rowIndex, int columnIndex) {
181
                                                return canEdit[columnIndex];
182
                                        }
183
                                });
184
                
185
                jScrollTable.setViewportView(jTableAddress);
186

    
187
                gridBagConstraints = new java.awt.GridBagConstraints();
188
                gridBagConstraints.gridx = 0;
189
                gridBagConstraints.gridy = 0;
190
                gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
191
                gridBagConstraints.weightx = 1.0;
192
                gridBagConstraints.weighty = 1.0;
193
                jPanTable.add(jScrollTable, gridBagConstraints);
194

    
195
                jPanNumber.setLayout(new java.awt.GridBagLayout());
196

    
197
                jLabNumber.setText("Number");
198
                jLabNumber.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
199
                gridBagConstraints = new java.awt.GridBagConstraints();
200
                gridBagConstraints.gridx = 0;
201
                gridBagConstraints.gridy = 0;
202
                gridBagConstraints.insets = new java.awt.Insets(4, 10, 4, 5);
203
                gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
204
                gridBagConstraints.weightx = 1.0;
205
                jPanNumber.add(jLabNumber, gridBagConstraints);
206

    
207
                jTextNumber.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
208
                jTextNumber.setMinimumSize(new java.awt.Dimension(30, 19));
209
                jTextNumber.setPreferredSize(new java.awt.Dimension(70, 19));
210
                jTextNumber.addFocusListener(new java.awt.event.FocusAdapter() {
211
                        public void focusLost(java.awt.event.FocusEvent evt) {
212
                                evNumberLostFocus(evt);
213
                        }
214
                });
215
                gridBagConstraints = new java.awt.GridBagConstraints();
216
                gridBagConstraints.gridx = 1;
217
                gridBagConstraints.gridy = 0;
218
                gridBagConstraints.insets = new java.awt.Insets(4, 0, 4, 4);
219
                jPanNumber.add(jTextNumber, gridBagConstraints);
220

    
221
                gridBagConstraints = new java.awt.GridBagConstraints();
222
                gridBagConstraints.gridx = 0;
223
                gridBagConstraints.gridy = 1;
224
                gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
225
                gridBagConstraints.weightx = 1.0;
226
                jPanTable.add(jPanNumber, gridBagConstraints);
227

    
228
                gridBagConstraints = new java.awt.GridBagConstraints();
229
                gridBagConstraints.gridx = 1;
230
                gridBagConstraints.gridy = 0;
231
                gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
232
                gridBagConstraints.weightx = 1.0;
233
                gridBagConstraints.weighty = 1.0;
234
                gridBagConstraints.insets = new java.awt.Insets(10, 15, 2, 10);
235
                add(jPanTable, gridBagConstraints);
236
        }
237

    
238
        /**
239
         * Number text field lost focus
240
         * 
241
         * @param evt
242
         */
243
        private void evNumberLostFocus(java.awt.event.FocusEvent evt) {
244
                String txt = jTextNumber.getText();
245
                try {
246
                        Integer.parseInt(txt);
247
                } catch (Exception e) {
248
                        jTextNumber.setText("");
249
                        jTextNumber.requestFocus();
250
                }
251
        }
252

    
253
        /**
254
         * Create a simple table model from literal components
255
         * 
256
         * @param lit
257
         */
258
        private void createSimpleTableModel(Literal lit) {
259
                DefaultTableModel model = (DefaultTableModel) jTableAddress.getModel();
260

    
261
                // first column
262
                TableColumn column0 = jTableAddress.getColumnModel().getColumn(0);
263
                DefaultTableCellRenderer render = new DefaultTableCellRenderer();
264
                render.setBackground(new Color(255, 253, 224));
265
                column0.setCellRenderer(render);
266

    
267
                // fill elements
268
                GeocoUtils.clearTableModel(model);
269

    
270
                for (Object obj : lit) {
271
                        RelationsComponent comp = (RelationsComponent) obj;
272
                        String key = comp.getKeyElement();
273
                        Object[] row = { key, "" };
274
                        model.addRow(row);
275
                }
276
                jTableAddress.setModel(model);
277
        }
278

    
279
        /**
280
         * Create a simple table model from literal components
281
         * 
282
         * @param lit
283
         */
284
        private void createTableTableModel(Literal lit,
285
                        List<FeatureAttributeDescriptor> descs) {
286
                DefaultTableModel model = (DefaultTableModel) jTableAddress.getModel();
287

    
288
                // first column
289
                TableColumn column0 = jTableAddress.getColumnModel().getColumn(0);
290
                DefaultTableCellRenderer render = new DefaultTableCellRenderer();
291
                render.setBackground(new Color(255, 253, 224));
292
                column0.setCellRenderer(render);
293

    
294
                // fill elements
295
                GeocoUtils.clearTableModel(model);
296

    
297
                // Put combos in the column 1
298
                GeocoUtils.putCombosInTable(jTableAddress, descs, 1);
299

    
300
                // put the elements in the column 0
301
                for (Object obj : lit) {
302
                        RelationsComponent comp = (RelationsComponent) obj;
303
                        // key
304
                        String key = comp.getKeyElement();
305
                        // insert
306
                        Object[] row = { key, "" };
307
                        model.addRow(row);
308
                }
309
                jTableAddress.setModel(model);
310
        }
311

    
312
        /**
313
         * get address from user interface
314
         * 
315
         * @param model
316
         * @return
317
         */
318
        public Address getSimpleAddress() {
319
                
320
                jTableAddress.validate();
321
                jTableAddress.setRowSelectionInterval(0, 0);
322
                DefaultTableModel model = (DefaultTableModel) jTableAddress.getModel();
323
                NumberAddress address = new NumberAddress();
324
                Literal mainLiteral = new Literal();
325
                Vector vec = model.getDataVector();
326

    
327
                for (int i = 0; i < vec.size(); i++) {
328
                        Vector vecc = (Vector) vec.elementAt(i);
329
                        String key = (String) vecc.elementAt(0);
330
                        String value = (String) vecc.elementAt(1);
331
                        mainLiteral.add(new AddressComponent(key, value));
332
                }
333
                address.setMainLiteral(mainLiteral);
334
                String strNumber = jTextNumber.getText();
335
                address.setNumber(Integer.parseInt(strNumber));
336
                return address;
337
        }
338

    
339
        /**
340
         * Get the addres from table
341
         * 
342
         * @param model
343
         * @param row
344
         * @return
345
         */
346
        public Address getTableAddress(int row) {
347

    
348
                jTableAddress.validate();
349
                jTableAddress.setRowSelectionInterval(0, 0);
350
                DefaultTableModel model = (DefaultTableModel) jTableAddress.getModel();
351
                // Get store from Gmodel and the feature
352
                FeatureStore store = control.getGmodel().getSelectedTableStore();
353
                FeatureSet features = null;
354
                Feature feature = null;
355
                try {
356
                        features = store.getFeatureSet();
357
                        Iterator<Feature> it = features.iterator(row);
358
                        feature = it.next();
359
                } catch (DataException e) {
360
                        log.error("Get the feature of FeatureStore", e);
361
                }
362

    
363
                // Create the address
364
                NumberAddress address = new NumberAddress();
365
                Literal literal = new Literal();
366
                Vector vec = model.getDataVector();
367

    
368
                for (int i = 0; i < vec.size(); i++) {
369
                        Vector vecc = (Vector) vec.elementAt(i);
370
                        String key = (String) vecc.elementAt(0);
371
                        ComboDesc cdesc = (ComboDesc) vecc.elementAt(1);
372
                        FeatureAttributeDescriptor desc = cdesc.getDescriptor();
373
                        String fieldName = desc.getName();
374
                        Object obj = feature.get(fieldName);
375
                        String value = obj.toString();
376
                        literal.add(new AddressComponent(key, value));
377
                }
378

    
379
                address.setMainLiteral(literal);
380
                String strNumber = jTextNumber.getText();
381
                address.setNumber(Integer.parseInt(strNumber));
382

    
383
                return address;
384
        }
385

    
386

    
387

    
388
}