Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extGeocoding / src / org / gvsig / geocoding / gui / address / AddressSimpleCentroidPanel.java @ 31249

History | View | Annotate | Download (10.2 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.table.DefaultTableCellRenderer;
43
import javax.swing.table.DefaultTableModel;
44
import javax.swing.table.TableColumn;
45

    
46
import org.gvsig.andami.PluginServices;
47
import org.gvsig.fmap.dal.exception.DataException;
48
import org.gvsig.fmap.dal.feature.Feature;
49
import org.gvsig.fmap.dal.feature.FeatureSet;
50
import org.gvsig.fmap.dal.feature.FeatureStore;
51
import org.gvsig.geocoding.address.Address;
52
import org.gvsig.geocoding.address.Literal;
53
import org.gvsig.geocoding.address.impl.DefaultAddress;
54
import org.gvsig.geocoding.address.impl.DefaultLiteral;
55
import org.gvsig.geocoding.extension.GeocodingController;
56
import org.slf4j.Logger;
57
import org.slf4j.LoggerFactory;
58

    
59
/**
60
 * Address simple centroid panel
61
 * 
62
 * @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
63
 * @author <a href="mailto:vsanjaime@prodevelop.es"> Vicente Sanjaime Calvet</a>
64
 */
65
public class AddressSimpleCentroidPanel extends AbstractAddressPanel {
66

    
67
        private static final long serialVersionUID = 1L;
68
        private Logger log = LoggerFactory
69
                        .getLogger(AddressSimpleCentroidPanel.class);
70

    
71
        private GeocodingController control = null;
72

    
73
        private JLabel jLabType;
74
        private JPanel jPanAddress;
75
        private JScrollPane jScrollAddress;
76
        private JTable jTableAddress;
77
        private JLabel jLabId;
78
        private JPanel jPanId;
79
        private JComboBox jComboId;
80

    
81
        /**
82
         * Constructor
83
         */
84
        public AddressSimpleCentroidPanel(GeocodingController control,
85
                        Literal literal) {
86

    
87
                this.control = control;
88
                initComponents();
89

    
90
                setImages();
91
                setMesages();
92

    
93
                // create table model
94
                if (this.control.getGmodel().isSimple()) {
95
                        createSimpleTableModel(literal);
96
                } else {
97
                        List<String> descs = this.control.getGmodel()
98
                                        .getListDescriptorSelectedTable();
99
                        createTableTableModel(literal, descs);
100
                        jComboId.setModel(createFieldsComboModel(descs));
101
                }
102

    
103
        }
104

    
105
        /**
106
         * Set images in the panel
107
         */
108
        private void setImages() {
109
                String baseDir = PluginServices.getPluginServices(this)
110
                                .getClassLoader().getBaseDir();
111

    
112
                jLabType.setIcon(new ImageIcon(baseDir + File.separator + "images"
113
                                + File.separator + "multi_centroid.png"));
114
        }
115

    
116
        /**
117
         * Set strings in the panel
118
         */
119
        private void setMesages() {
120
                PluginServices ps = PluginServices.getPluginServices(this);
121

    
122
                DefaultTableModel model = (DefaultTableModel) jTableAddress.getModel();
123
                String element = ps.getText("xelement");
124
                String value = ps.getText("xvalue");
125
                Object[] headers = { element, value };
126
                model.setColumnIdentifiers(headers);
127
                jLabId.setText(ps.getText("xidfield"));
128
        }
129

    
130
        /**
131
         * Initialize components
132
         */
133
        private void initComponents() {
134
                java.awt.GridBagConstraints gridBagConstraints;
135

    
136
                jLabType = new JLabel();
137
                jPanAddress = new JPanel();
138
                jScrollAddress = new JScrollPane();
139
                jTableAddress = new JTable();
140
                jLabId = new JLabel();
141
                jComboId = new JComboBox();
142
                jPanId = new JPanel();
143

    
144
                setLayout(new java.awt.GridBagLayout());
145

    
146
                gridBagConstraints = new java.awt.GridBagConstraints();
147
                gridBagConstraints.insets = new java.awt.Insets(3, 15, 5, 5);
148
                add(jLabType, gridBagConstraints);
149

    
150
                jPanAddress.setLayout(new java.awt.GridBagLayout());
151
                jPanId.setLayout(new java.awt.GridBagLayout());
152

    
153
                if (!control.getGmodel().isSimple()) {
154
                        jLabId.setText("Field ID");
155
                        jLabId.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
156
                        gridBagConstraints = new java.awt.GridBagConstraints();
157
                        gridBagConstraints.gridx = 0;
158
                        gridBagConstraints.gridy = 0;
159
                        gridBagConstraints.insets = new java.awt.Insets(4, 10, 4, 5);
160
                        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
161
                        gridBagConstraints.weightx = 1.0;
162
                        jPanId.add(jLabId, gridBagConstraints);
163

    
164
                        jComboId.setModel(new javax.swing.DefaultComboBoxModel(
165
                                        new String[] { "" }));
166
                        jComboId.setMinimumSize(new java.awt.Dimension(200, 20));
167
                        jComboId.setName("jComboId");
168

    
169
                        gridBagConstraints = new java.awt.GridBagConstraints();
170
                        gridBagConstraints.gridx = 1;
171
                        gridBagConstraints.gridy = 0;
172
                        gridBagConstraints.insets = new java.awt.Insets(4, 0, 4, 4);
173
                        jPanId.add(jComboId, gridBagConstraints);
174

    
175
                        gridBagConstraints = new java.awt.GridBagConstraints();
176
                        gridBagConstraints.gridx = 0;
177
                        gridBagConstraints.gridy = 0;
178
                        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
179
                        gridBagConstraints.weightx = 1.0;
180
                        gridBagConstraints.insets = new java.awt.Insets(0, 0, 2, 0);
181
                        jPanAddress.add(jPanId, gridBagConstraints);
182
                }
183

    
184
                jTableAddress
185
                                .setModel(new javax.swing.table.DefaultTableModel(
186
                                                new Object[][] { { null, null }, { null, null },
187
                                                                { null, null } }, new String[] { "Element",
188
                                                                "value" }) {
189
                                        Class[] types = new Class[] { java.lang.String.class,
190
                                                        java.lang.String.class };
191
                                        boolean[] canEdit = new boolean[] { false, true };
192

    
193
                                        public Class getColumnClass(int columnIndex) {
194
                                                return types[columnIndex];
195
                                        }
196

    
197
                                        public boolean isCellEditable(int rowIndex, int columnIndex) {
198
                                                return canEdit[columnIndex];
199
                                        }
200
                                });
201
                jScrollAddress.setViewportView(jTableAddress);
202

    
203
                gridBagConstraints = new java.awt.GridBagConstraints();
204
                gridBagConstraints.gridx = 0;
205
                gridBagConstraints.gridy = 1;
206
                gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
207
                gridBagConstraints.weightx = 1.0;
208
                gridBagConstraints.weighty = 1.0;
209
                jPanAddress.add(jScrollAddress, gridBagConstraints);
210

    
211
                gridBagConstraints = new java.awt.GridBagConstraints();
212
                gridBagConstraints.gridx = 1;
213
                gridBagConstraints.gridy = 0;
214
                gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
215
                gridBagConstraints.weightx = 1.0;
216
                gridBagConstraints.weighty = 1.0;
217
                gridBagConstraints.insets = new java.awt.Insets(3, 15, 5, 10);
218
                add(jPanAddress, gridBagConstraints);
219
        }
220

    
221
        /**
222
         * Create a simple table model from literal components
223
         * 
224
         * @param lit
225
         */
226
        private void createSimpleTableModel(Literal lit) {
227
                DefaultTableModel model = (DefaultTableModel) jTableAddress.getModel();
228
                // first column
229
                TableColumn column0 = jTableAddress.getColumnModel().getColumn(0);
230
                DefaultTableCellRenderer render = new DefaultTableCellRenderer();
231
                render.setBackground(new Color(255, 253, 224));
232
                column0.setCellRenderer(render);
233

    
234
                // fill elements
235
                super.clearTableModel(model);
236

    
237
                for (String key : lit.keySet()) {
238
                        Object[] row = { key, "" };
239
                        model.addRow(row);
240
                }
241
                jTableAddress.setModel(model);
242
        }
243

    
244
        /**
245
         * Create a simple table model from literal components
246
         * 
247
         * @param lit
248
         */
249
        private void createTableTableModel(Literal lit, List<String> descs) {
250
                DefaultTableModel model = (DefaultTableModel) jTableAddress.getModel();
251
                // first column
252
                TableColumn column0 = jTableAddress.getColumnModel().getColumn(0);
253
                DefaultTableCellRenderer render = new DefaultTableCellRenderer();
254
                render.setBackground(new Color(255, 253, 224));
255
                column0.setCellRenderer(render);
256

    
257
                // fill elements
258
                super.clearTableModel(model);
259

    
260
                // Put combos in the column 1
261
                super.putCombosInTable(jTableAddress, descs, 1);
262

    
263
                // put the elements in the column 0
264
                for (String key : lit.keySet()) {
265
                        // insert
266
                        Object[] row = { key, "" };
267
                        model.addRow(row);
268
                }
269
                jTableAddress.setModel(model);
270
        }
271

    
272
        /**
273
         * get address from user interface
274
         * 
275
         * @param model
276
         * @return
277
         */
278
        public Address getSimpleAddress() {
279

    
280
                jTableAddress.validate();
281
                jTableAddress.setRowSelectionInterval(0, 0);
282
                DefaultTableModel model = (DefaultTableModel) jTableAddress.getModel();
283

    
284
                Address address = new DefaultAddress();
285
                Literal literal = new DefaultLiteral();
286
                Vector vec = model.getDataVector();
287

    
288
                for (int i = 0; i < vec.size(); i++) {
289
                        Vector vecc = (Vector) vec.elementAt(i);
290
                        String key = (String) vecc.elementAt(0);
291
                        String value = (String) vecc.elementAt(1);
292
                        literal.put(key, value);
293
                }
294

    
295
                address.setMainLiteral(literal);
296
                return address;
297
        }
298

    
299
        /**
300
         * Get the addres from table
301
         * 
302
         * @param model
303
         * @param row
304
         * @return
305
         */
306
        public Address getTableAddress(int row) {
307

    
308
                jTableAddress.validate();
309
                jTableAddress.setRowSelectionInterval(0, 0);
310
                DefaultTableModel model = (DefaultTableModel) jTableAddress.getModel();
311

    
312
                // save in the gmodel the descriptor of identifier field
313
                String descid = (String) jComboId.getModel().getSelectedItem();
314
                control.getGmodel().setIdMasiveTable(descid);
315

    
316
                // Get store from Gmodel and the feature
317
                FeatureStore store = control.getGmodel().getSelectedTableStore();
318
                FeatureSet features = null;
319
                Feature feature = null;
320
                try {
321
                        features = store.getFeatureSet();
322
                        Iterator<Feature> it = features.iterator(row);
323
                        feature = it.next();
324
                } catch (DataException e) {
325
                        log.error("Get the feature of FeatureStore", e);
326
                }
327

    
328
                // Create the address
329
                Address address = new DefaultAddress();
330
                Literal literal = new DefaultLiteral();
331
                Vector vec = model.getDataVector();
332

    
333
                for (int i = 0; i < vec.size(); i++) {
334
                        Vector vecc = (Vector) vec.elementAt(i);
335
                        String key = (String) vecc.elementAt(0);
336
                        String fieldName = (String) vecc.elementAt(1);
337
                        Object obj = feature.get(fieldName);
338
                        String value = obj.toString();
339
                        literal.put(key, value);
340
                }
341

    
342
                address.setMainLiteral(literal);
343

    
344
                return address;
345
        }
346

    
347
}