Statistics
| Revision:

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

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

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

    
62
import com.iver.andami.PluginServices;
63

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

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

    
76
        private GeocoController control = null;
77

    
78
        private JLabel jLabType;
79
        private JPanel jPanAddress;
80
        private JScrollPane jScrollAddress;
81
        private JTable jTableAddress;
82

    
83
        /**
84
         * Constructor
85
         */
86
        public AddressSimpleCentroidPanel(GeocoController control, Literal literal) {
87
                initComponents();
88
                this.control = control;
89

    
90
                setImages();
91
                setMesages();
92

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

    
102
        }
103

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

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

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

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

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

    
134
                jLabType = new javax.swing.JLabel();
135
                jPanAddress = new javax.swing.JPanel();
136
                jScrollAddress = new javax.swing.JScrollPane();
137
                jTableAddress = new javax.swing.JTable();
138

    
139
                setLayout(new java.awt.GridBagLayout());
140

    
141
                gridBagConstraints = new java.awt.GridBagConstraints();
142
                gridBagConstraints.insets = new java.awt.Insets(5, 15, 5, 10);
143
                add(jLabType, gridBagConstraints);
144

    
145
                jPanAddress.setLayout(new java.awt.GridBagLayout());
146

    
147
                jTableAddress
148
                                .setModel(new javax.swing.table.DefaultTableModel(
149
                                                new Object[][] { { null, null }, { null, null },
150
                                                                { null, null } }, new String[] { "Element",
151
                                                                "value" }) {
152
                                        Class[] types = new Class[] { java.lang.String.class,
153
                                                        java.lang.String.class };
154
                                        boolean[] canEdit = new boolean[] { false, true };
155

    
156
                                        public Class getColumnClass(int columnIndex) {
157
                                                return types[columnIndex];
158
                                        }
159

    
160
                                        public boolean isCellEditable(int rowIndex, int columnIndex) {
161
                                                return canEdit[columnIndex];
162
                                        }
163
                                });                
164
                jScrollAddress.setViewportView(jTableAddress);
165

    
166
                gridBagConstraints = new java.awt.GridBagConstraints();
167
                gridBagConstraints.gridx = 0;
168
                gridBagConstraints.gridy = 0;
169
                gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
170
                gridBagConstraints.weightx = 1.0;
171
                gridBagConstraints.weighty = 1.0;
172
                jPanAddress.add(jScrollAddress, gridBagConstraints);
173

    
174
                gridBagConstraints = new java.awt.GridBagConstraints();
175
                gridBagConstraints.gridx = 1;
176
                gridBagConstraints.gridy = 0;
177
                gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
178
                gridBagConstraints.weightx = 1.0;
179
                gridBagConstraints.weighty = 1.0;
180
                gridBagConstraints.insets = new java.awt.Insets(10, 15, 10, 10);
181
                add(jPanAddress, gridBagConstraints);
182
        }
183

    
184
        /**
185
         * Create a simple table model from literal components
186
         * 
187
         * @param lit
188
         */
189
        private void createSimpleTableModel(Literal lit) {
190
                DefaultTableModel model = (DefaultTableModel) jTableAddress.getModel();
191
                // first column
192
                TableColumn column0 = jTableAddress.getColumnModel().getColumn(0);
193
                DefaultTableCellRenderer render = new DefaultTableCellRenderer();
194
                render.setBackground(new Color(255, 253, 224));
195
                column0.setCellRenderer(render);
196

    
197
                // fill elements
198
                GeocoUtils.clearTableModel(model);
199

    
200
                for (Object obj : lit) {
201
                        RelationsComponent comp = (RelationsComponent) obj;
202
                        String key = comp.getKeyElement();
203
                        Object[] row = { key, "" };
204
                        model.addRow(row);
205
                }
206
                jTableAddress.setModel(model);
207
        }
208

    
209
        /**
210
         * Create a simple table model from literal components
211
         * 
212
         * @param lit
213
         */
214
        private void createTableTableModel(Literal lit,
215
                        List<FeatureAttributeDescriptor> descs) {
216
                DefaultTableModel model = (DefaultTableModel) jTableAddress.getModel();
217
                // first column
218
                TableColumn column0 = jTableAddress.getColumnModel().getColumn(0);
219
                DefaultTableCellRenderer render = new DefaultTableCellRenderer();
220
                render.setBackground(new Color(255, 253, 224));
221
                column0.setCellRenderer(render);
222

    
223
                // fill elements
224
                GeocoUtils.clearTableModel(model);
225

    
226
                // Put combos in the column 1
227
                GeocoUtils.putCombosInTable(jTableAddress, descs, 1);
228

    
229
                // put the elements in the column 0
230
                for (Object obj : lit) {
231
                        RelationsComponent comp = (RelationsComponent) obj;
232
                        // Element
233
                        String key = comp.getKeyElement();
234
                        // insert
235
                        Object[] row = { key, "" };
236
                        model.addRow(row);
237
                }
238
                jTableAddress.setModel(model);
239
        }
240

    
241
        /**
242
         * get address from user interface
243
         * 
244
         * @param model
245
         * @return
246
         */
247
        public Address getSimpleAddress() {
248
                
249
                jTableAddress.validate();
250
                jTableAddress.setRowSelectionInterval(0, 0);                
251
                DefaultTableModel model = (DefaultTableModel) jTableAddress.getModel();
252

    
253
                Address address = new Address();
254
                Literal literal = new Literal();
255
                Vector vec = model.getDataVector();
256

    
257
                for (int i = 0; i < vec.size(); i++) {
258
                        Vector vecc = (Vector) vec.elementAt(i);
259
                        String key = (String) vecc.elementAt(0);
260
                        String value = (String) vecc.elementAt(1);
261
                        literal.add(new AddressComponent(key, value));
262
                }
263

    
264
                address.setMainLiteral(literal);
265
                return address;
266
        }
267

    
268
        /**
269
         * Get the addres from table
270
         * 
271
         * @param model
272
         * @param row
273
         * @return
274
         */
275
        public Address getTableAddress(int row) {
276

    
277
                jTableAddress.validate();
278
                jTableAddress.setRowSelectionInterval(0, 0);
279
                DefaultTableModel model = (DefaultTableModel) jTableAddress.getModel();
280
                // Get store from Gmodel and the feature
281
                FeatureStore store = control.getGmodel().getSelectedTableStore();
282
                FeatureSet features = null;
283
                Feature feature = null;
284
                try {
285
                        features = store.getFeatureSet();
286
                        Iterator<Feature> it = features.iterator(row);
287
                        feature = it.next();
288
                } catch (DataException e) {
289
                        log.error("Get the feature of FeatureStore", e);
290
                }
291

    
292
                // Create the address
293
                Address address = new Address();
294
                Literal literal = new Literal();
295
                Vector vec = model.getDataVector();
296

    
297
                for (int i = 0; i < vec.size(); i++) {
298
                        Vector vecc = (Vector) vec.elementAt(i);
299
                        String key = (String) vecc.elementAt(0);
300
                        ComboDesc cdesc = (ComboDesc) vecc.elementAt(1);
301
                        FeatureAttributeDescriptor desc = cdesc.getDescriptor();
302
                        String fieldName = desc.getName();
303
                        Object obj = feature.get(fieldName);
304
                        String value = obj.toString();
305
                        literal.add(new AddressComponent(key, value));
306
                }
307

    
308
                address.setMainLiteral(literal);
309

    
310
                return address;
311
        }
312

    
313

    
314
}