Statistics
| Revision:

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

History | View | Annotate | Download (35.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.newpattern;
29

    
30
import java.awt.Color;
31
import java.awt.Dimension;
32
import java.awt.GridBagConstraints;
33
import java.io.File;
34
import java.util.ArrayList;
35
import java.util.List;
36
import java.util.Vector;
37

    
38
import javax.swing.DefaultCellEditor;
39
import javax.swing.DefaultComboBoxModel;
40
import javax.swing.ImageIcon;
41
import javax.swing.JButton;
42
import javax.swing.JComboBox;
43
import javax.swing.JLabel;
44
import javax.swing.JOptionPane;
45
import javax.swing.JPanel;
46
import javax.swing.JScrollPane;
47
import javax.swing.JTable;
48
import javax.swing.JTextArea;
49
import javax.swing.table.DefaultTableCellRenderer;
50
import javax.swing.table.DefaultTableModel;
51
import javax.swing.table.TableColumn;
52

    
53
import org.gvsig.fmap.dal.exception.ReadException;
54
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
55
import org.gvsig.fmap.dal.feature.FeatureStore;
56
import org.gvsig.fmap.dal.feature.FeatureType;
57
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
58
import org.gvsig.geocoding.Literal;
59
import org.gvsig.geocoding.RelationsComponent;
60
import org.gvsig.geocoding.extension.GeocoController;
61
import org.gvsig.geocoding.gui.settings.SettingsPanel;
62
import org.gvsig.geocoding.gui.styles.ComposedStyle;
63
import org.gvsig.geocoding.gui.styles.DoubleRangeStyle;
64
import org.gvsig.geocoding.gui.styles.SimpleCentroidStyle;
65
import org.gvsig.geocoding.gui.styles.SimpleRangeStyle;
66
import org.gvsig.geocoding.pattern.Patterngeocoding;
67
import org.gvsig.geocoding.pattern.Settings;
68
import org.gvsig.geocoding.styles.AbstractGeocodingStyle;
69
import org.gvsig.geocoding.styles.Composed;
70
import org.gvsig.geocoding.styles.DoubleRange;
71
import org.gvsig.geocoding.styles.SimpleCentroid;
72
import org.gvsig.geocoding.styles.SimpleRange;
73
import org.gvsig.geocoding.utils.ComboShowName;
74
import org.gvsig.geocoding.utils.GeocoUtils;
75
import org.slf4j.Logger;
76
import org.slf4j.LoggerFactory;
77

    
78
import com.iver.andami.PluginServices;
79
import com.iver.andami.ui.mdiManager.IWindow;
80
import com.iver.andami.ui.mdiManager.WindowInfo;
81

    
82
/**
83
 * New pattern panel
84
 * 
85
 * @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
86
 * @author <a href="mailto:vsanjaime@prodevelop.es"> Vicente Sanjaime Calvet</a>
87
 */
88
public class NewGeocoPatternPanel extends JPanel implements IWindow {
89

    
90
        private static final Logger log = LoggerFactory
91
                        .getLogger(NewGeocoPatternPanel.class);
92
        private static final long serialVersionUID = 1L;
93

    
94
        private Patterngeocoding pattern = null;
95
        private GeocoController control = null;
96

    
97
        private SimpleRangePanel srange = null;
98
        private DoubleRangePanel drange = null;
99

    
100
        private boolean editing = false;
101

    
102
        private JButton jButAdd;
103
        private JButton jButCancel;
104
        private JButton jButDel;
105
        private JButton jButDown;
106
        private JButton jButSave;
107
        private JButton jButUp;
108
        private JComboBox jComboLayers;
109
        private JComboBox jComboType;
110
        private JLabel jLabLayers;
111
        private JLabel jLabType;
112
        private JTextArea jTextTypeInfo;
113
        private JPanel jPanButsEle;
114
        private JPanel jPanButtons;
115
        private JPanel jPanDataSource;
116
        private JPanel jPanElements;
117
        private JPanel jPanRange;
118
        private JPanel jPanSettings;
119
        private JPanel jPanType;
120
        private JScrollPane jScrollElements;
121
        private JScrollPane jScrollTypeInfo;
122
        private JTable jTableComponents;
123

    
124
        private SettingsPanel settingsPanel;
125

    
126
        /**
127
         * Constructor
128
         */
129
        public NewGeocoPatternPanel(GeocoController _control, Patterngeocoding pat) {
130
                this.control = _control;
131

    
132
                initComponents();
133
                setMessages();
134
                setImages();
135
                // Fill the combo of geocoding types
136
                fillTypesCombo();
137
                // fill the combo of layers
138
                fillLayersCombo();
139

    
140
                // dynamic panels
141
                srange = new SimpleRangePanel(control, null);
142
                drange = new DoubleRangePanel(control, null);
143

    
144
                // fill the table elemnts
145
                List<String> elements = control.getListAddressComponents();
146
                List<FeatureAttributeDescriptor> descs = getLayerDescriptors();
147
                fillComponentsCombos(elements, descs);
148

    
149
                // if modify a pattern
150
                if (pat != null) {
151
                        editing = true;
152
                        updatePanel(pat);
153
                } else {
154
                        pattern = new Patterngeocoding();
155
                        control.getGmodel().setPattern(pattern);
156

    
157
                        // put settings parameters
158
                        settingsPanel.setMaxResults(pattern.getSettings()
159
                                        .getResultsNumber());
160
                        settingsPanel.setScore(pattern.getSettings().getScore());
161
                }
162

    
163
        }
164

    
165
        /**
166
         * 
167
         * @param pat
168
         */
169
        public void updatePanel(Patterngeocoding pat) {
170

    
171
                // update settings
172
                settingsPanel.setMaxResults(pat.getSettings().getResultsNumber());
173
                double sco = pat.getSettings().getScore();
174
                settingsPanel.setScore(sco);
175

    
176
                // update style and update components
177
                AbstractGeocodingStyle astyle = pat.getSource().getStyle();
178
                if (astyle instanceof SimpleCentroid) {
179
                        SimpleCentroid simSty = (SimpleCentroid) astyle;
180
                        jComboType.setSelectedIndex(0);
181
                        setLiteralTableComponents(simSty.getRelationsLiteral());
182
                } else if (astyle instanceof SimpleRange) {
183
                        jComboType.setSelectedIndex(1);
184
                        SimpleRange ransimSty = (SimpleRange) astyle;
185
                        srange.setDescriptorFronMun(((RelationsComponent) ransimSty
186
                                        .getFirstNumber()).getValue());
187
                        srange.setDescriptorToMun(((RelationsComponent) ransimSty
188
                                        .getLastNumber()).getValue());
189
                        setLiteralTableComponents(ransimSty.getRelationsLiteral());
190
                } else if (astyle instanceof DoubleRange) {
191
                        jComboType.setSelectedIndex(2);
192
                        DoubleRange randouSty = (DoubleRange) astyle;
193
                        drange.setDescriptorRightFronMun(((RelationsComponent) randouSty
194
                                        .getFirstRightNumber()).getValue());
195
                        drange.setDescriptorRightToMun(((RelationsComponent) randouSty
196
                                        .getLastRightNumber()).getValue());
197
                        drange.setDescriptorLeftFronMun(((RelationsComponent) randouSty
198
                                        .getFirstLeftNumber()).getValue());
199
                        drange.setDescriptorLeftToMun(((RelationsComponent) randouSty
200
                                        .getLastLeftNumber()).getValue());
201
                        setLiteralTableComponents(randouSty.getRelationsLiteral());
202
                } else if (astyle instanceof Composed) {
203
                        Composed comSty = (Composed) astyle;
204
                        jComboType.setSelectedIndex(3);
205
                        setLiteralTableComponents(comSty.getRelationsLiteral());
206
                }
207
                // update layer
208
                FeatureStore store = pat.getSource().getLayerSource();
209
                String name = store.getName();
210
                // TODO falta recargar la layer desde el datasotre para seleccionar la
211
                // capa correcta en el combo
212
                // jComboLayers.seS
213

    
214
        }
215

    
216
        /**
217
         * Get window info
218
         * 
219
         * @return
220
         */
221
        public WindowInfo getWindowInfo() {
222
                WindowInfo info = new WindowInfo(WindowInfo.MODALDIALOG
223
                                | WindowInfo.RESIZABLE);
224

    
225
                info.setMinimumSize(this.getMinimumSize());
226
                info.setWidth(480);
227
                info.setHeight(470);
228
                info.setTitle(PluginServices.getText(this, "new_geocoding_pattern"));
229
                return info;
230
        }
231

    
232
        /**
233
         * get window profile
234
         * 
235
         * @return
236
         */
237
        public Object getWindowProfile() {
238
                return null;
239
        }
240

    
241
        /**
242
         * Initialize panel components
243
         */
244
        @SuppressWarnings("serial")
245
        private void initComponents() {
246
                java.awt.GridBagConstraints gridBagConstraints;
247

    
248
                jPanType = new JPanel();
249
                jLabType = new JLabel();
250
                jComboType = new JComboBox();
251
                jTextTypeInfo = new JTextArea();
252
                jScrollTypeInfo = new JScrollPane();
253
                jPanDataSource = new JPanel();
254
                jLabLayers = new JLabel();
255
                jComboLayers = new JComboBox();
256
                jPanElements = new JPanel();
257
                jScrollElements = new JScrollPane();
258
                jTableComponents = new JTable();
259
                jPanRange = new JPanel();
260
                jPanButsEle = new JPanel();
261
                jButUp = new JButton();
262
                jButAdd = new JButton();
263
                jButDel = new JButton();
264
                jButDown = new JButton();
265
                jPanSettings = new JPanel();
266
                jPanButtons = new JPanel();
267
                jButSave = new JButton();
268
                jButCancel = new JButton();
269
                settingsPanel = new SettingsPanel(control);
270

    
271
                setLayout(new java.awt.GridBagLayout());
272

    
273
                jPanType.setBorder(javax.swing.BorderFactory
274
                                .createTitledBorder("Geocoding type"));
275
                jPanType.setLayout(new java.awt.GridBagLayout());
276

    
277
                jLabType.setIcon(new ImageIcon(PluginServices.getPluginServices(this)
278
                                .getClassLoader().getBaseDir()
279
                                + File.separator
280
                                + "images"
281
                                + File.separator
282
                                + "new"
283
                                + File.separator + "centroid.png"));
284
                gridBagConstraints = new java.awt.GridBagConstraints();
285
                gridBagConstraints.gridx = 0;
286
                gridBagConstraints.gridy = 0;
287
                gridBagConstraints.gridheight = 2;
288
                gridBagConstraints.insets = new java.awt.Insets(2, 10, 2, 5);
289
                jPanType.add(jLabType, gridBagConstraints);
290

    
291
                jComboType.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
292
                                "1", "2", "3" }));
293
                jComboType.setMinimumSize(new java.awt.Dimension(26, 20));
294
                jComboType.setPreferredSize(new java.awt.Dimension(30, 20));
295
                jComboType.addActionListener(new java.awt.event.ActionListener() {
296
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
297
                                evChangeType(evt);
298
                        }
299
                });
300
                gridBagConstraints = new java.awt.GridBagConstraints();
301
                gridBagConstraints.gridx = 1;
302
                gridBagConstraints.gridy = 0;
303
                gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
304
                gridBagConstraints.weightx = 1.0;
305
                gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 10);
306
                jPanType.add(jComboType, gridBagConstraints);
307

    
308
                jTextTypeInfo.setColumns(20);
309
                jTextTypeInfo.setFont(new java.awt.Font("Arial", 0, 10));
310
                jTextTypeInfo.setLineWrap(true);
311
                jTextTypeInfo.setRows(2);
312
                jTextTypeInfo.setEditable(false);
313
                jTextTypeInfo.setMinimumSize(new Dimension(160, 30));
314
                jTextTypeInfo.setPreferredSize(new Dimension(160, 40));
315
                jTextTypeInfo.setText(PluginServices.getText(null, "simcenstyle"));
316
                jScrollTypeInfo.setViewportView(jTextTypeInfo);
317

    
318
                gridBagConstraints = new java.awt.GridBagConstraints();
319
                gridBagConstraints.gridx = 1;
320
                gridBagConstraints.gridy = 1;
321
                gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
322
                gridBagConstraints.weightx = 1.0;
323
                gridBagConstraints.weighty = 1.0;
324
                gridBagConstraints.insets = new java.awt.Insets(5, 10, 3, 10);
325
                jPanType.add(jScrollTypeInfo, gridBagConstraints);
326

    
327
                gridBagConstraints = new java.awt.GridBagConstraints();
328
                gridBagConstraints.gridx = 0;
329
                gridBagConstraints.gridy = 0;
330
                gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
331
                gridBagConstraints.weightx = 1.0;
332
                gridBagConstraints.weighty = 0.3;
333
                gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
334
                add(jPanType, gridBagConstraints);
335

    
336
                jPanDataSource.setBorder(javax.swing.BorderFactory
337
                                .createTitledBorder("Data source"));
338
                jPanDataSource.setLayout(new java.awt.GridBagLayout());
339

    
340
                jLabLayers.setText("Layers");
341
                gridBagConstraints = new java.awt.GridBagConstraints();
342
                gridBagConstraints.gridx = 0;
343
                gridBagConstraints.gridy = 0;
344
                gridBagConstraints.insets = new java.awt.Insets(2, 10, 2, 5);
345
                jPanDataSource.add(jLabLayers, gridBagConstraints);
346

    
347
                jComboLayers.setModel(new javax.swing.DefaultComboBoxModel(
348
                                new String[] { "1", "2", "3" }));
349
                jComboLayers.setMinimumSize(new java.awt.Dimension(26, 20));
350
                jComboLayers.setPreferredSize(new java.awt.Dimension(30, 20));
351
                jComboLayers.addActionListener(new java.awt.event.ActionListener() {
352
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
353
                                evChangeLayer(evt);
354
                        }
355
                });
356
                gridBagConstraints = new java.awt.GridBagConstraints();
357
                gridBagConstraints.gridx = 1;
358
                gridBagConstraints.gridy = 0;
359
                gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
360
                gridBagConstraints.weightx = 1.0;
361
                gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 10);
362
                jPanDataSource.add(jComboLayers, gridBagConstraints);
363

    
364
                gridBagConstraints = new java.awt.GridBagConstraints();
365
                gridBagConstraints.gridx = 0;
366
                gridBagConstraints.gridy = 1;
367
                gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
368
                gridBagConstraints.weightx = 1.0;
369
                gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
370
                add(jPanDataSource, gridBagConstraints);
371

    
372
                jPanElements.setBorder(javax.swing.BorderFactory
373
                                .createTitledBorder("Elements"));
374
                jPanElements.setLayout(new java.awt.GridBagLayout());
375

    
376
                jTableComponents.setModel(new javax.swing.table.DefaultTableModel(
377
                                new Object[][] {}, new String[] { "Elements", "Field" }) {
378
                        boolean[] canEdit = new boolean[] { true, true };
379

    
380
                        public boolean isCellEditable(int rowIndex, int columnIndex) {
381
                                return canEdit[columnIndex];
382
                        }
383
                });
384

    
385
                jScrollElements.setViewportView(jTableComponents);
386

    
387
                gridBagConstraints = new java.awt.GridBagConstraints();
388
                gridBagConstraints.gridx = 0;
389
                gridBagConstraints.gridy = 0;
390
                gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
391
                gridBagConstraints.weightx = 1.0;
392
                gridBagConstraints.weighty = 0.9;
393
                jPanElements.add(jScrollElements, gridBagConstraints);
394

    
395
                jPanRange.setLayout(new java.awt.GridBagLayout());
396
                gridBagConstraints = new java.awt.GridBagConstraints();
397
                gridBagConstraints.gridx = 0;
398
                gridBagConstraints.gridy = 1;
399
                gridBagConstraints.gridwidth = 2;
400
                gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 2);
401
                jPanElements.add(jPanRange, gridBagConstraints);
402

    
403
                jPanButsEle.setLayout(new java.awt.GridBagLayout());
404

    
405
                jButUp.addActionListener(new java.awt.event.ActionListener() {
406
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
407
                                evUpComponent(evt);
408
                        }
409
                });
410
                gridBagConstraints = new java.awt.GridBagConstraints();
411
                gridBagConstraints.gridx = 0;
412
                gridBagConstraints.gridy = 0;
413
                gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
414
                jPanButsEle.add(jButUp, gridBagConstraints);
415

    
416
                jButAdd.addActionListener(new java.awt.event.ActionListener() {
417
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
418
                                evAddComponent(evt);
419
                        }
420
                });
421
                gridBagConstraints = new java.awt.GridBagConstraints();
422
                gridBagConstraints.gridx = 0;
423
                gridBagConstraints.gridy = 1;
424
                jPanButsEle.add(jButAdd, gridBagConstraints);
425

    
426
                jButDel.addActionListener(new java.awt.event.ActionListener() {
427
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
428
                                evDelComponent(evt);
429
                        }
430
                });
431
                gridBagConstraints = new java.awt.GridBagConstraints();
432
                gridBagConstraints.gridx = 0;
433
                gridBagConstraints.gridy = 2;
434
                jPanButsEle.add(jButDel, gridBagConstraints);
435

    
436
                jButDown.addActionListener(new java.awt.event.ActionListener() {
437
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
438
                                evDownComponent(evt);
439
                        }
440
                });
441
                gridBagConstraints = new java.awt.GridBagConstraints();
442
                gridBagConstraints.gridx = 0;
443
                gridBagConstraints.gridy = 3;
444
                jPanButsEle.add(jButDown, gridBagConstraints);
445

    
446
                gridBagConstraints = new java.awt.GridBagConstraints();
447
                gridBagConstraints.gridx = 1;
448
                gridBagConstraints.gridy = 0;
449
                gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
450
                gridBagConstraints.weighty = 1.0;
451
                gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 0);
452
                jPanElements.add(jPanButsEle, gridBagConstraints);
453

    
454
                gridBagConstraints = new java.awt.GridBagConstraints();
455
                gridBagConstraints.gridx = 0;
456
                gridBagConstraints.gridy = 2;
457
                gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
458
                gridBagConstraints.weightx = 1.0;
459
                gridBagConstraints.weighty = 1.0;
460
                gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
461
                add(jPanElements, gridBagConstraints);
462

    
463
                jPanSettings.setBorder(javax.swing.BorderFactory
464
                                .createTitledBorder("Settings"));
465
                jPanSettings.setLayout(new java.awt.GridBagLayout());
466

    
467
                gridBagConstraints = new java.awt.GridBagConstraints();
468
                gridBagConstraints.gridx = 0;
469
                gridBagConstraints.gridy = 0;
470
                gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
471
                gridBagConstraints.weightx = 1.0;
472
                gridBagConstraints.weighty = 1.0;
473
                gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
474
                jPanSettings.add(settingsPanel, gridBagConstraints);
475

    
476
                gridBagConstraints = new java.awt.GridBagConstraints();
477
                gridBagConstraints.gridx = 0;
478
                gridBagConstraints.gridy = 3;
479
                gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
480
                gridBagConstraints.weightx = 1.0;
481
                gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
482
                add(jPanSettings, gridBagConstraints);
483

    
484
                jPanButtons.setLayout(new java.awt.GridBagLayout());
485

    
486
                jButSave.setText("Save");
487
                jButSave.addActionListener(new java.awt.event.ActionListener() {
488
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
489
                                evSave(evt);
490
                        }
491
                });
492
                gridBagConstraints = new java.awt.GridBagConstraints();
493
                gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 10);
494
                jPanButtons.add(jButSave, gridBagConstraints);
495

    
496
                jButCancel.setText("Cancel");
497
                jButCancel.addActionListener(new java.awt.event.ActionListener() {
498
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
499
                                evCancel(evt);
500
                        }
501
                });
502
                gridBagConstraints = new java.awt.GridBagConstraints();
503
                gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
504
                jPanButtons.add(jButCancel, gridBagConstraints);
505

    
506
                gridBagConstraints = new java.awt.GridBagConstraints();
507
                gridBagConstraints.gridx = 0;
508
                gridBagConstraints.gridy = 4;
509
                gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
510
                gridBagConstraints.weightx = 1.0;
511
                gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
512
                add(jPanButtons, gridBagConstraints);
513
        }
514

    
515
        /**
516
         * Change layer selected in the combo
517
         * 
518
         * @param evt
519
         */
520
        private void evChangeLayer(java.awt.event.ActionEvent evt) {
521
                ComboLayer cLyr = (ComboLayer) jComboLayers.getSelectedItem();
522
                if (cLyr != null) {
523

    
524
                        // reload the descriptors of the new layer in the combos
525
                        srange.setComboValues(getLayerDescriptors());
526
                        drange.setComboValues(getLayerDescriptors());
527

    
528
                        // clean the table model
529
                        GeocoUtils.clearTableModel((DefaultTableModel) jTableComponents
530
                                        .getModel());
531
                        // jTableComponents.setModel(model);
532
                        jTableComponents.validate();
533

    
534
                        // fill the combos of the table with descriptors of the new layer
535
                        fillComponentsCombos(control.getListAddressComponents(),
536
                                        getLayerDescriptors());
537
                }
538

    
539
        }
540

    
541
        /**
542
         * save the pattern
543
         * 
544
         * @param evt
545
         */
546
        private void evSave(java.awt.event.ActionEvent evt) {
547
                /* PreSave */
548
                boolean ok = preSave();
549

    
550
                if (ok) {
551
                        /* save the new pattern in the model */
552
                        control.getGmodel().setPattern(pattern);
553

    
554
                        /* save pattern */
555
                        boolean apto = control.savePattern();
556

    
557
                        /* close the panel */
558
                        if (apto) {
559
                                IWindow[] iws = PluginServices.getMDIManager().getAllWindows();
560
                                for (int i = 0; i < iws.length; i++) {
561
                                        if (iws[i] instanceof NewGeocoPatternPanel) {
562
                                                PluginServices.getMDIManager().closeWindow(iws[i]);
563
                                        }
564
                                }
565
                                control.updateGeocoGUI();
566

    
567
                                // enable components
568
                                if (control.getPattern() != null) {
569
                                        control.getGpanel().enableComponents();
570
                                        // insert address panel
571
                                        control.getGpanel().insertAddressPanel();
572

    
573
                                }
574
                        }
575
                }
576
        }
577

    
578
        /**
579
         * Cancel the process
580
         * 
581
         * @param evt
582
         */
583
        private void evCancel(java.awt.event.ActionEvent evt) {
584
                /* Close the window */
585
                IWindow[] iws = PluginServices.getMDIManager().getAllWindows();
586
                for (int i = 0; i < iws.length; i++) {
587
                        if (iws[i] instanceof NewGeocoPatternPanel) {
588
                                PluginServices.getMDIManager().closeWindow(iws[i]);
589
                        }
590
                }
591
        }
592

    
593
        /**
594
         * Change geocoding type
595
         * 
596
         * @param evt
597
         */
598
        private void evChangeType(java.awt.event.ActionEvent evt) {
599
                ComboShowName selected = (ComboShowName) jComboType.getSelectedItem();
600
                if (selected instanceof SimpleCentroidStyle) {
601
                        jTextTypeInfo.setText(PluginServices.getText(null, "simcenstyle"));
602
                        insertDynamicPanel(null);
603
                        jLabType.setIcon(new ImageIcon(PluginServices.getPluginServices(
604
                                        this).getClassLoader().getBaseDir()
605
                                        + File.separator
606
                                        + "images"
607
                                        + File.separator
608
                                        + "new"
609
                                        + File.separator + "centroid.png"));
610
                        jLabType.validate();
611
                }
612
                if (selected instanceof SimpleRangeStyle) {
613
                        jTextTypeInfo
614
                                        .setText(PluginServices.getText(null, "rangesimstyle"));
615
                        insertDynamicPanel(srange);
616
                        List<FeatureAttributeDescriptor> desc = getLayerDescriptors();
617
                        srange.setComboValues(desc);
618
                        jLabType.setIcon(new ImageIcon(PluginServices.getPluginServices(
619
                                        this).getClassLoader().getBaseDir()
620
                                        + File.separator
621
                                        + "images"
622
                                        + File.separator
623
                                        + "new"
624
                                        + File.separator + "simplerange.png"));
625
                        jLabType.validate();
626
                }
627
                if (selected instanceof DoubleRangeStyle) {
628
                        jTextTypeInfo
629
                                        .setText(PluginServices.getText(null, "rangedoustyle"));
630
                        insertDynamicPanel(drange);
631
                        List<FeatureAttributeDescriptor> desc = getLayerDescriptors();
632
                        drange.setComboValues(desc);
633
                        jLabType.setIcon(new ImageIcon(PluginServices.getPluginServices(
634
                                        this).getClassLoader().getBaseDir()
635
                                        + File.separator
636
                                        + "images"
637
                                        + File.separator
638
                                        + "new"
639
                                        + File.separator + "doublerange.png"));
640
                        jLabType.validate();
641
                }
642
                if (selected instanceof ComposedStyle) {
643
                        jTextTypeInfo.setText(PluginServices.getText(null, "compostyle"));
644
                        insertDynamicPanel(null);
645
                        jLabType.setIcon(new ImageIcon(PluginServices.getPluginServices(
646
                                        this).getClassLoader().getBaseDir()
647
                                        + File.separator
648
                                        + "images"
649
                                        + File.separator
650
                                        + "new"
651
                                        + File.separator + "composed.png"));
652
                        jLabType.validate();
653
                }
654

    
655
        }
656

    
657
        /**
658
         * Up one position selected component in the table
659
         * 
660
         * @param evt
661
         */
662
        private void evUpComponent(java.awt.event.ActionEvent evt) {
663
                DefaultTableModel model = (DefaultTableModel) jTableComponents
664
                                .getModel();
665
                int sel = jTableComponents.getSelectedRow();
666
                if (model.getRowCount() > 0 && sel > 0) {
667

    
668
                        model.moveRow(sel, sel, sel - 1);
669
                        jTableComponents.setRowSelectionInterval(sel - 1, sel - 1);
670
                        jTableComponents.validate();
671
                }
672
        }
673

    
674
        /**
675
         * Add a new component to table
676
         * 
677
         * @param evt
678
         */
679
        private void evAddComponent(java.awt.event.ActionEvent evt) {
680
                DefaultTableModel model = (DefaultTableModel) jTableComponents
681
                                .getModel();
682
                String[] rowData = { "...", "..." };
683
                model.addRow(rowData);
684
                int n = model.getRowCount();
685
                jTableComponents.setRowSelectionInterval(n - 1, n - 1);
686
                jTableComponents.validate();
687
        }
688

    
689
        /**
690
         * Remove selected component of the table
691
         * 
692
         * @param evt
693
         */
694
        private void evDelComponent(java.awt.event.ActionEvent evt) {
695

    
696
                int sel = jTableComponents.getSelectedRow();
697
                if (sel >= 0) {
698
                        DefaultTableModel model = (DefaultTableModel) jTableComponents
699
                                        .getModel();
700
                        model.removeRow(sel);
701
                        if (sel != 0) {
702
                                jTableComponents.setRowSelectionInterval(sel - 1, sel - 1);
703
                        }
704
                        jTableComponents.validate();
705
                }
706

    
707
        }
708

    
709
        /**
710
         * 
711
         * @param evt
712
         */
713
        private void evDownComponent(java.awt.event.ActionEvent evt) {
714
                DefaultTableModel model = (DefaultTableModel) jTableComponents
715
                                .getModel();
716
                int sel = jTableComponents.getSelectedRow();
717
                if (model.getRowCount() > 0 && sel < model.getRowCount() - 1) {
718
                        model.moveRow(sel, sel, sel + 1);
719
                        jTableComponents.setRowSelectionInterval(sel + 1, sel + 1);
720
                        jTableComponents.validate();
721
                }
722
        }
723

    
724
        /**
725
         * Fills the combo of geocoding types
726
         */
727
        private void fillTypesCombo() {
728

    
729
                SimpleCentroidStyle simple = new SimpleCentroidStyle();
730
                SimpleRangeStyle simplerange = new SimpleRangeStyle();
731
                DoubleRangeStyle doublerange = new DoubleRangeStyle();
732
                ComposedStyle composed = new ComposedStyle();
733
                Object[] item = { simple, simplerange, doublerange, composed };
734
                DefaultComboBoxModel model = new DefaultComboBoxModel(item);
735
                jComboType.setModel(model);
736
        }
737

    
738
        /**
739
         * Fills the combo of geocoding types
740
         */
741
        private void fillLayersCombo() {
742

    
743
                List<FLyrVect> lyrs = GeocoUtils.getListgvSIGVectLayers();
744
                DefaultComboBoxModel model = new DefaultComboBoxModel();
745
                if (lyrs.size() > 0) {
746
                        for (FLyrVect lyr : lyrs) {
747
                                ComboLayer cLyr = new ComboLayer(lyr);
748
                                model.addElement(cLyr);
749
                        }
750
                        jComboLayers.setModel(model);
751
                } else {
752
                        jComboLayers.setModel(model);
753
                }
754
        }
755

    
756
        /**
757
         * This method updates all labels of the panel from properties file
758
         */
759
        private void setMessages() {
760

    
761
                PluginServices ps = PluginServices.getPluginServices(this);
762

    
763
                jPanDataSource.setBorder(GeocoUtils.getTitledBorder(ps
764
                                .getText("datasource")));
765
                jPanType.setBorder(GeocoUtils.getTitledBorder(ps.getText("gtype")));
766
                jLabLayers.setText(ps.getText("selectedlayer"));
767
                jPanElements.setBorder(GeocoUtils.getTitledBorder(ps
768
                                .getText("components")));
769
                jPanSettings.setBorder(GeocoUtils.getTitledBorder(ps
770
                                .getText("settings")));
771
                jButCancel.setText(ps.getText("cancel"));
772
                jButSave.setText(ps.getText("save"));
773

    
774
                String[] volumnNames = { ps.getText("xelement"), ps.getText("xfield") };
775
                ((DefaultTableModel) jTableComponents.getModel())
776
                                .setColumnIdentifiers(volumnNames);
777

    
778
        }
779

    
780
        /**
781
         * This method updates all images of the panel
782
         */
783
        private void setImages() {
784

    
785
                String baseDir = PluginServices.getPluginServices(this)
786
                                .getClassLoader().getBaseDir();
787

    
788
                jButSave.setIcon(new ImageIcon(baseDir + File.separator + "images"
789
                                + File.separator + "icons16" + File.separator + "save.png"));
790
                jButCancel.setIcon(new ImageIcon(baseDir + File.separator + "images"
791
                                + File.separator + "icons16" + File.separator + "stop.png"));
792

    
793
                jButUp.setIcon(new ImageIcon(baseDir + File.separator + "images"
794
                                + File.separator + "icons16" + File.separator + "up.png"));
795
                jButAdd.setIcon(new ImageIcon(baseDir + File.separator + "images"
796
                                + File.separator + "icons16" + File.separator + "add.png"));
797
                jButDel.setIcon(new ImageIcon(baseDir + File.separator + "images"
798
                                + File.separator + "icons16" + File.separator + "remove.png"));
799
                jButDown.setIcon(new ImageIcon(baseDir + File.separator + "images"
800
                                + File.separator + "icons16" + File.separator + "down.png"));
801
        }
802

    
803
        /**
804
         * This method saves same pattern's attributes
805
         */
806
        private boolean preSave() {
807

    
808
                boolean ok = true;
809

    
810
                // save the feature store in the pattern
811
                ComboLayer cLyr = (ComboLayer) jComboLayers.getSelectedItem();
812
                if (cLyr != null) {
813
                        FLyrVect lyr = cLyr.getLayer();
814
                        FeatureStore store = null;
815
                        try {
816
                                store = lyr.getFeatureStore();
817
                                this.pattern.getSource().setLayerSource(store);
818

    
819
                        } catch (ReadException e) {
820
                                log.error(
821
                                                "Error getting the FeatureStore of the selected layer",
822
                                                e);
823
                        }
824
                } else {
825
                        ok = false;
826
                        String message = PluginServices.getText(null, "nopatternlayer");
827
                        String title = PluginServices.getText(null, "geocoding");
828
                        JOptionPane.showMessageDialog(null, message, title,
829
                                        JOptionPane.ERROR_MESSAGE);
830
                }
831
                if (cLyr != null) {
832
                        // save style of geocoding pattern
833
                        ComboShowName cType = (ComboShowName) jComboType.getSelectedItem();
834
                        AbstractGeocodingStyle style = null;
835
                        if (cType instanceof SimpleCentroidStyle) {
836
                                style = new SimpleCentroid();
837
                                Literal literal = getLiteralTableComponents();
838
                                if (literal.size() > 0) {
839
                                        style.setRelationsLiteral(literal);
840
                                        pattern.getSource().setStyle(style);
841
                                } else {
842
                                        ok = false;
843
                                        showMessageLiteralEmpty();
844
                                }
845

    
846
                        }
847
                        if (cType instanceof SimpleRangeStyle) {
848
                                style = new SimpleRange();
849
                                Literal literal = getLiteralTableComponents();
850
                                if (literal.size() > 0) {
851
                                        style.setRelationsLiteral(literal);
852
                                        RelationsComponent[] comps = getSimpleRangeComponents();
853
                                        ((SimpleRange) style).setFirstNumber(comps[0]);
854
                                        ((SimpleRange) style).setLastNumber(comps[1]);
855
                                        pattern.getSource().setStyle(style);
856
                                } else {
857
                                        ok = false;
858
                                        showMessageLiteralEmpty();
859
                                }
860
                        }
861
                        if (cType instanceof DoubleRangeStyle) {
862
                                style = new DoubleRange();
863
                                Literal literal = getLiteralTableComponents();
864
                                if (literal.size() > 0) {
865
                                        style.setRelationsLiteral(literal);
866
                                        RelationsComponent[] comps = getDoubleRangeComponents();
867
                                        ((DoubleRange) style).setFirstRightNumber(comps[0]);
868
                                        ((DoubleRange) style).setLastRightNumber(comps[1]);
869
                                        ((DoubleRange) style).setFirstLeftNumber(comps[2]);
870
                                        ((DoubleRange) style).setLastLeftNumber(comps[3]);
871
                                        pattern.getSource().setStyle(style);
872
                                } else {
873
                                        ok = false;
874
                                        showMessageLiteralEmpty();
875
                                }
876
                        }
877
                        if (cType instanceof ComposedStyle) {
878
                                style = new Composed();
879
                                Literal literal = getLiteralTableComponents();
880
                                if (literal.size() > 0) {
881
                                        style.setRelationsLiteral(literal);
882
                                        pattern.getSource().setStyle(style);
883
                                } else {
884
                                        ok = false;
885
                                        showMessageLiteralEmpty();
886
                                }
887
                        }
888
                }
889

    
890
                // Get parameters
891
                Settings sett = pattern.getSettings();
892
                sett.setResultsNumber(settingsPanel.getMaxResults());
893
                sett.setScore(settingsPanel.getScore());
894
                pattern.setSettings(sett);
895

    
896
                return ok;
897
        }
898

    
899
        /**
900
         * insert ranges panel
901
         * 
902
         * @param pan
903
         */
904
        private void insertDynamicPanel(JPanel pan) {
905

    
906
                if (pan != null) {
907
                        jPanRange.removeAll();
908

    
909
                        GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
910
                        gridBagConstraints.gridx = 0;
911
                        gridBagConstraints.gridy = 0;
912
                        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
913
                        gridBagConstraints.weightx = 1.0;
914
                        gridBagConstraints.weighty = 1.0;
915

    
916
                        jPanRange.add(pan, gridBagConstraints);
917

    
918
                        this.setSize(this.getWidth() + 1, this.getHeight());
919
                        jPanRange.validate();
920
                        pan.validate();
921
                        this.validate();
922
                } else {
923
                        jPanRange.removeAll();
924
                        this.validate();
925
                        jPanRange.validate();
926
                }
927
        }
928

    
929
        /**
930
         * Get layer descriptors
931
         * 
932
         * @return
933
         */
934
        private List<FeatureAttributeDescriptor> getLayerDescriptors() {
935
                try {
936
                        ComboLayer cLyr = (ComboLayer) jComboLayers.getSelectedItem();
937
                        if (cLyr != null) {
938
                                List<FeatureAttributeDescriptor> descs = new ArrayList<FeatureAttributeDescriptor>();
939
                                FeatureType fType = cLyr.getLayer().getFeatureStore()
940
                                                .getDefaultFeatureType();
941
                                for (int i = 0; i < fType.size(); i++) {
942
                                        descs.add(fType.getAttributeDescriptor(i));
943
                                }
944
                                return descs;
945
                        }
946
                } catch (Exception e) {
947
                        return null;
948
                }
949
                return null;
950

    
951
        }
952

    
953
        /**
954
         * 
955
         * @param elements
956
         * @param descs
957
         */
958
        private void fillComponentsCombos(List<String> elements,
959
                        List<FeatureAttributeDescriptor> descs) {
960

    
961
                if (elements != null) {
962

    
963
                        // first column (elements)
964
                        TableColumn column0 = jTableComponents.getColumnModel()
965
                                        .getColumn(0);
966
                        DefaultTableCellRenderer render = new DefaultTableCellRenderer();
967
                        render.setBackground(new Color(255, 253, 224));
968
                        column0.setCellRenderer(render);
969
                        JComboBox combo0 = new JComboBox();
970
                        DefaultComboBoxModel model0 = new DefaultComboBoxModel();
971
                        for (String ele : elements) {
972
                                model0.addElement(ele);
973
                        }
974
                        combo0.setModel(model0);
975
                        DefaultCellEditor dce = new DefaultCellEditor(combo0);
976
                        dce.setClickCountToStart(2);
977
                        column0.setCellEditor(dce);
978
                }
979

    
980
                if (descs != null) {
981
                        // second column (fields)
982
                        TableColumn column1 = jTableComponents.getColumnModel()
983
                                        .getColumn(1);
984
                        JComboBox combo1 = new JComboBox();
985
                        DefaultComboBoxModel model1 = new DefaultComboBoxModel();
986
                        for (FeatureAttributeDescriptor desc : descs) {
987
                                ComboDesc cdesc = new ComboDesc(desc);
988
                                model1.addElement(cdesc);
989
                        }
990
                        combo1.setModel(model1);
991
                        DefaultCellEditor dce = new DefaultCellEditor(combo1);
992
                        dce.setClickCountToStart(2);
993
                        column1.setCellEditor(dce);
994
                }
995
        }
996

    
997
        /**
998
         * get literal from table of components
999
         * 
1000
         * @return
1001
         */
1002
        @SuppressWarnings("unchecked")
1003
        private Literal getLiteralTableComponents() {
1004
                Literal literal = new Literal();
1005
                DefaultTableModel model = (DefaultTableModel) jTableComponents
1006
                                .getModel();
1007
                Vector data = model.getDataVector();
1008
                for (int i = 0; i < data.size(); i++) {
1009
                        FeatureAttributeDescriptor desc = null;
1010
                        String element = "";
1011
                        try {
1012
                                Vector vec = (Vector) data.elementAt(i);
1013
                                element = (String) vec.elementAt(0);
1014

    
1015
                                ComboDesc cdesc = (ComboDesc) vec.elementAt(1);
1016
                                desc = cdesc.getDescriptor();
1017
                        } catch (Exception e) {
1018
                                desc = null;
1019
                                element = "...";
1020
                        }
1021

    
1022
                        if (element.compareTo("...") != 0 && desc != null) {
1023
                                RelationsComponent rComp = new RelationsComponent(element, desc);
1024
                                literal.add(rComp);
1025
                        }
1026
                }
1027
                return literal;
1028
        }
1029

    
1030
        /**
1031
         * set literal to table of components
1032
         * 
1033
         * @param lit
1034
         */
1035

    
1036
        private void setLiteralTableComponents(Literal lit) {
1037
                // clean the table model
1038
                GeocoUtils.clearTableModel((DefaultTableModel) jTableComponents
1039
                                .getModel());
1040
                DefaultTableModel model = (DefaultTableModel) jTableComponents
1041
                                .getModel();
1042
                for (Object obj : lit) {
1043
                        RelationsComponent comp = (RelationsComponent) obj;
1044
                        Object[] obje = { comp.getKeyElement(),
1045
                                        new ComboDesc(comp.getValue()) };
1046
                        model.addRow(obje);
1047
                }
1048
                jTableComponents.setModel(model);
1049

    
1050
        }
1051

    
1052
        /**
1053
         * Get components of the simple rang
1054
         * 
1055
         * @return
1056
         */
1057
        private RelationsComponent[] getSimpleRangeComponents() {
1058
                RelationsComponent[] comps = new RelationsComponent[2];
1059
                // FromNum
1060
                FeatureAttributeDescriptor fdesc = srange.getDescriptorFronMun();
1061
                if (fdesc != null) {
1062
                        RelationsComponent comp = new RelationsComponent("fromNum", fdesc);
1063
                        comps[0] = comp;
1064
                }
1065
                // ToNum
1066
                FeatureAttributeDescriptor tdesc = srange.getDescriptorToMun();
1067
                if (tdesc != null) {
1068
                        RelationsComponent comp = new RelationsComponent("toNum", tdesc);
1069
                        comps[1] = comp;
1070
                }
1071

    
1072
                return comps;
1073
        }
1074

    
1075
        /**
1076
         * Get components of the double range
1077
         * 
1078
         * @return
1079
         */
1080
        private RelationsComponent[] getDoubleRangeComponents() {
1081
                RelationsComponent[] comps = new RelationsComponent[4];
1082

    
1083
                // RightFromNum
1084
                FeatureAttributeDescriptor rfdesc = drange.getDescriptorRightFronMun();
1085
                if (rfdesc != null) {
1086
                        RelationsComponent comp = new RelationsComponent("rightFromNum",
1087
                                        rfdesc);
1088
                        comps[0] = comp;
1089
                }
1090
                // RightToNum
1091
                FeatureAttributeDescriptor rtdesc = drange.getDescriptorRightToMun();
1092
                if (rtdesc != null) {
1093
                        RelationsComponent comp = new RelationsComponent("rightToNum",
1094
                                        rtdesc);
1095
                        comps[1] = comp;
1096
                }
1097

    
1098
                // LeftFromNum
1099
                FeatureAttributeDescriptor lfdesc = drange.getDescriptorLeftFronMun();
1100
                if (lfdesc != null) {
1101
                        RelationsComponent comp = new RelationsComponent("leftFromNum",
1102
                                        lfdesc);
1103
                        comps[2] = comp;
1104
                }
1105
                // LeftToNum
1106
                FeatureAttributeDescriptor ltdesc = drange.getDescriptorLeftToMun();
1107
                if (ltdesc != null) {
1108
                        RelationsComponent comp = new RelationsComponent("leftToNum",
1109
                                        ltdesc);
1110
                        comps[3] = comp;
1111
                }
1112

    
1113
                return comps;
1114
        }
1115

    
1116
        /**
1117
         * Show a error message when the literal has not components
1118
         */
1119
        private void showMessageLiteralEmpty() {
1120
                String title = PluginServices.getText(null, "geocoding");
1121
                String message = PluginServices.getText(null, "literalempty");
1122
                JOptionPane.showMessageDialog(null, message, title,
1123
                                JOptionPane.ERROR_MESSAGE);
1124

    
1125
        }
1126

    
1127
}