Statistics
| Revision:

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

History | View | Annotate | Download (35.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.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.Map;
37
import java.util.Vector;
38

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

    
54
import org.gvsig.andami.PluginServices;
55
import org.gvsig.andami.ui.mdiManager.IWindow;
56
import org.gvsig.andami.ui.mdiManager.WindowInfo;
57
import org.gvsig.fmap.dal.DataStore;
58
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
59
import org.gvsig.fmap.dal.feature.FeatureType;
60
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
61
import org.gvsig.geocoding.GeocodingTags;
62
import org.gvsig.geocoding.address.Literal;
63
import org.gvsig.geocoding.address.impl.DefaultLiteral;
64
import org.gvsig.geocoding.extension.GeocodingController;
65
import org.gvsig.geocoding.gui.settings.SettingsPanel;
66
import org.gvsig.geocoding.gui.styles.ComposedStyle;
67
import org.gvsig.geocoding.gui.styles.DoubleRangeStyle;
68
import org.gvsig.geocoding.gui.styles.SimpleCentroidStyle;
69
import org.gvsig.geocoding.gui.styles.SimpleRangeStyle;
70
import org.gvsig.geocoding.pattern.GeocodingPattern;
71
import org.gvsig.geocoding.pattern.GeocodingSettings;
72
import org.gvsig.geocoding.pattern.impl.DefaultGeocodingPattern;
73
import org.gvsig.geocoding.styles.AbstractGeocodingStyle;
74
import org.gvsig.geocoding.styles.impl.Composed;
75
import org.gvsig.geocoding.styles.impl.DoubleRange;
76
import org.gvsig.geocoding.styles.impl.SimpleCentroid;
77
import org.gvsig.geocoding.styles.impl.SimpleRange;
78
import org.gvsig.geocoding.utils.ComboLayerName;
79
import org.gvsig.geocoding.utils.ComboShowName;
80
import org.gvsig.geocoding.utils.GeocodingUtils;
81
import org.gvsig.tools.dataTypes.DataTypes;
82
import org.slf4j.Logger;
83
import org.slf4j.LoggerFactory;
84

    
85
/**
86
 * New pattern panel
87
 * 
88
 * @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
89
 * @author <a href="mailto:vsanjaime@prodevelop.es"> Vicent Sanjaime Calvet</a>
90
 */
91
public class NewPatternPanel extends JPanel implements IWindow {
92

    
93
        private static final Logger log = LoggerFactory
94
                        .getLogger(NewPatternPanel.class);
95
        private static final long serialVersionUID = 1L;
96

    
97
        private GeocodingPattern editPattern = null;
98
        private GeocodingController control = null;
99

    
100
        private SimpleRangePanel srange = null;
101
        private DoubleRangePanel drange = null;
102

    
103
        private boolean editing = false;
104

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

    
127
        private SettingsPanel settingsPanel;
128

    
129
        /**
130
         * Constructor
131
         */
132
        public NewPatternPanel(GeocodingController control, GeocodingPattern pat) {
133
                this.control = control;
134

    
135
                this.initComponents();
136
                this.setMessages();
137
                this.setImages();
138
                // Fill the combo of geocoding types
139
                this.fillTypesCombo();
140
                // fill the combo of layers
141
                this.fillLayersCombo();
142

    
143
                // dynamic panels
144
                this.srange = new SimpleRangePanel(control, null);
145
                this.drange = new DoubleRangePanel(control, null);
146

    
147
                // fill the table elemnts
148
                List<String> elements = control.getListAddressComponents();
149
                List<String> descs = getLayerDescriptors();
150
                this.fillComponentsCombos(elements, descs);
151

    
152
                // if modify a pattern
153
                if (pat != null) {
154
                        this.editing = true;
155
                        this.editPattern = pat;
156
                        this.updatePanel(this.editPattern);
157
                } else {
158
                        this.editPattern = new DefaultGeocodingPattern();
159
                        this.control.getGmodel().setPattern(editPattern);
160

    
161
                        // put settings parameters
162
                        this.settingsPanel.setMaxResults(editPattern.getSettings()
163
                                        .getMaxResultsNumber());
164
                        this.settingsPanel
165
                                        .setScore(editPattern.getSettings().getMinScore());
166
                }
167

    
168
        }
169

    
170
        /**
171
         * 
172
         * @param pat
173
         */
174
        public void updatePanel(GeocodingPattern pat) {
175

    
176
                // update settings
177
                settingsPanel.setMaxResults(pat.getSettings().getMaxResultsNumber());
178
                double sco = pat.getSettings().getMinScore();
179
                settingsPanel.setScore(sco);
180

    
181
                // update style and update components
182
                AbstractGeocodingStyle astyle = pat.getDataSource().getStyle();
183
                if (astyle instanceof SimpleCentroid) {
184
                        SimpleCentroid simSty = (SimpleCentroid) astyle;
185
                        jComboType.setSelectedIndex(0);
186
                        setLiteralTableComponents(simSty.getRelationsLiteral());
187
                } else if (astyle instanceof SimpleRange) {
188
                        jComboType.setSelectedIndex(1);
189
                        SimpleRange ransimSty = (SimpleRange) astyle;
190
                        srange.setDescriptorFromNum(ransimSty.getFirstNumber());
191
                        srange.setDescriptorToNum(ransimSty.getLastNumber());
192
                        setLiteralTableComponents(ransimSty.getRelationsLiteral());
193
                } else if (astyle instanceof DoubleRange) {
194
                        jComboType.setSelectedIndex(2);
195
                        DoubleRange randouSty = (DoubleRange) astyle;
196
                        drange.setDescriptorRightFronMun(randouSty.getFirstRightNumber());
197
                        drange.setDescriptorRightToMun(randouSty.getLastRightNumber());
198
                        drange.setDescriptorLeftFronMun(randouSty.getFirstLeftNumber());
199
                        drange.setDescriptorLeftToMun(randouSty.getLastLeftNumber());
200
                        setLiteralTableComponents(randouSty.getRelationsLiteral());
201
                } else if (astyle instanceof Composed) {
202
                        Composed comSty = (Composed) astyle;
203
                        jComboType.setSelectedIndex(3);
204
                        setLiteralTableComponents(comSty.getRelationsLiteral());
205
                }
206
                // update layer
207
                // DataStore store = pattern.getSource().getStore();
208
                // String name = store.getName();
209
                // // TODO falta recargar la layer desde el datasotre para seleccionar
210
                // 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
                                + "multi_centroid.png"));
283
                gridBagConstraints = new java.awt.GridBagConstraints();
284
                gridBagConstraints.gridx = 0;
285
                gridBagConstraints.gridy = 0;
286
                gridBagConstraints.gridheight = 2;
287
                gridBagConstraints.insets = new java.awt.Insets(2, 10, 2, 5);
288
                jPanType.add(jLabType, gridBagConstraints);
289

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

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

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

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

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

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

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

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

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

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

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

    
384
                jScrollElements.setViewportView(jTableComponents);
385

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
538
        }
539

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

    
549
                if (ok) {
550

    
551
                        /* save pattern */
552
                        boolean apto = control.savePattern(this.editPattern, this.editing);
553

    
554
                        if (this.editing) {
555
                                this.editing = false;
556
                        }
557
                        /* pattern ok */
558
                        if (apto) {
559
                                // run in background the index process of the datastore
560
                                control.indexing();
561
                                // close panel
562
                                IWindow[] iws = PluginServices.getMDIManager().getAllWindows();
563
                                for (int i = 0; i < iws.length; i++) {
564
                                        if (iws[i] instanceof NewPatternPanel) {
565
                                                PluginServices.getMDIManager().closeWindow(iws[i]);
566
                                        }
567
                                }
568
                                // update panel
569
                                control.updateGeocoGUI();
570

    
571
                                // enable components
572
                                if (control.getPattern() != null) {
573
                                        control.getGPanel().enableComponents();
574
                                        // insert address panel
575
                                        control.getGPanel().insertAddressPanel();
576

    
577
                                }
578
                                // clean table results
579
                                // FIXME
580
                                // control.clearGeocodingPoint();
581
                                // clean points on view
582
                                control.getGPanel().cleanTableResults();
583
                        }
584
                        // close new window panel
585
                        else {
586
                                IWindow[] iws = PluginServices.getMDIManager().getAllWindows();
587
                                for (int i = 0; i < iws.length; i++) {
588
                                        if (iws[i] instanceof NewPatternPanel) {
589
                                                PluginServices.getMDIManager().closeWindow(iws[i]);
590
                                        }
591
                                }
592
                        }
593
                }
594
        }
595

    
596
        /**
597
         * Cancel the process
598
         * 
599
         * @param evt
600
         */
601
        private void evCancel(java.awt.event.ActionEvent evt) {
602
                /* Close the window */
603
                IWindow[] iws = PluginServices.getMDIManager().getAllWindows();
604
                for (int i = 0; i < iws.length; i++) {
605
                        if (iws[i] instanceof NewPatternPanel) {
606
                                PluginServices.getMDIManager().closeWindow(iws[i]);
607
                        }
608
                }
609

    
610
        }
611

    
612
        /**
613
         * Change geocoding type
614
         * 
615
         * @param evt
616
         */
617
        private void evChangeType(java.awt.event.ActionEvent evt) {
618
                ComboShowName selected = (ComboShowName) jComboType.getSelectedItem();
619

    
620
                String baseDir = PluginServices.getPluginServices(this)
621
                                .getClassLoader().getBaseDir();
622

    
623
                if (selected instanceof SimpleCentroidStyle) {
624
                        jTextTypeInfo.setText(PluginServices.getText(null, "simcenstyle"));
625
                        insertDynamicPanel(null);
626
                        String path = baseDir + File.separator + "images" + File.separator
627
                                        + "multi_centroid.png";
628
                        jLabType.setIcon(new ImageIcon(path));
629
                        jLabType.validate();
630
                        this.validate();
631
                }
632
                if (selected instanceof SimpleRangeStyle) {
633
                        jTextTypeInfo
634
                                        .setText(PluginServices.getText(null, "rangesimstyle"));
635
                        insertDynamicPanel(srange);
636
                        List<String> desc = getLayerDescriptors();
637
                        srange.setComboValues(desc);
638
                        String path = baseDir + File.separator + "images" + File.separator
639
                                        + "single_rank.png";
640
                        jLabType.setIcon(new ImageIcon(path));
641
                        jLabType.validate();
642
                        this.validate();
643
                }
644
                if (selected instanceof DoubleRangeStyle) {
645
                        jTextTypeInfo
646
                                        .setText(PluginServices.getText(null, "rangedoustyle"));
647
                        insertDynamicPanel(drange);
648
                        List<String> desc = getLayerDescriptors();
649
                        drange.setComboValues(desc);
650
                        String path = baseDir + File.separator + "images" + File.separator
651
                                        + "double_rank.png";
652
                        jLabType.setIcon(new ImageIcon(path));
653
                        jLabType.validate();
654
                        this.validate();
655
                }
656
                if (selected instanceof ComposedStyle) {
657
                        jTextTypeInfo.setText(PluginServices.getText(null, "compostyle"));
658
                        insertDynamicPanel(null);
659
                        String path = baseDir + File.separator + "images" + File.separator
660
                                        + "composed.png";
661
                        jLabType.setIcon(new ImageIcon(path));
662
                        jLabType.validate();
663
                        this.validate();
664
                }
665

    
666
        }
667

    
668
        /**
669
         * Up one position selected component in the table
670
         * 
671
         * @param evt
672
         */
673
        private void evUpComponent(java.awt.event.ActionEvent evt) {
674
                DefaultTableModel model = (DefaultTableModel) jTableComponents
675
                                .getModel();
676
                int sel = jTableComponents.getSelectedRow();
677
                if (model.getRowCount() > 0 && sel > 0) {
678

    
679
                        model.moveRow(sel, sel, sel - 1);
680
                        jTableComponents.setRowSelectionInterval(sel - 1, sel - 1);
681
                        jTableComponents.validate();
682
                }
683
        }
684

    
685
        /**
686
         * Add a new component to table
687
         * 
688
         * @param evt
689
         */
690
        private void evAddComponent(java.awt.event.ActionEvent evt) {
691
                DefaultTableModel model = (DefaultTableModel) jTableComponents
692
                                .getModel();
693
                String[] rowData = { "...", "..." };
694
                model.addRow(rowData);
695
                int n = model.getRowCount();
696
                jTableComponents.setRowSelectionInterval(n - 1, n - 1);
697
                jTableComponents.validate();
698
        }
699

    
700
        /**
701
         * Remove selected component of the table
702
         * 
703
         * @param evt
704
         */
705
        private void evDelComponent(java.awt.event.ActionEvent evt) {
706

    
707
                int sel = jTableComponents.getSelectedRow();
708
                if (sel >= 0) {
709
                        DefaultTableModel model = (DefaultTableModel) jTableComponents
710
                                        .getModel();
711
                        model.removeRow(sel);
712
                        if (sel != 0) {
713
                                jTableComponents.setRowSelectionInterval(sel - 1, sel - 1);
714
                        }
715
                        jTableComponents.validate();
716
                }
717

    
718
        }
719

    
720
        /**
721
         * 
722
         * @param evt
723
         */
724
        private void evDownComponent(java.awt.event.ActionEvent evt) {
725
                DefaultTableModel model = (DefaultTableModel) jTableComponents
726
                                .getModel();
727
                int sel = jTableComponents.getSelectedRow();
728
                if (model.getRowCount() > 0 && sel < model.getRowCount() - 1) {
729
                        model.moveRow(sel, sel, sel + 1);
730
                        jTableComponents.setRowSelectionInterval(sel + 1, sel + 1);
731
                        jTableComponents.validate();
732
                }
733
        }
734

    
735
        /**
736
         * Fills the combo of geocoding types
737
         */
738
        private void fillTypesCombo() {
739

    
740
                SimpleCentroidStyle simple = new SimpleCentroidStyle();
741
                SimpleRangeStyle simplerange = new SimpleRangeStyle();
742
                DoubleRangeStyle doublerange = new DoubleRangeStyle();
743
                ComposedStyle composed = new ComposedStyle();
744
                Object[] item = { simple, simplerange, doublerange, composed };
745
                DefaultComboBoxModel model = new DefaultComboBoxModel(item);
746
                jComboType.setModel(model);
747
        }
748

    
749
        /**
750
         * Fills the combo of geocoding types
751
         */
752
        private void fillLayersCombo() {
753

    
754
                List<FLyrVect> lyrs = control.getListgvSIGVectLayers();
755
                DefaultComboBoxModel model = new DefaultComboBoxModel();
756
                if (lyrs.size() > 0) {
757
                        for (FLyrVect lyr : lyrs) {
758
                                ComboLayerName cLyr = new ComboLayerName(lyr);
759
                                model.addElement(cLyr);
760
                        }
761
                        jComboLayers.setModel(model);
762
                } else {
763
                        jComboLayers.setModel(model);
764
                }
765
        }
766

    
767
        /**
768
         * This method updates all labels of the panel from properties file
769
         */
770
        private void setMessages() {
771

    
772
                PluginServices ps = PluginServices.getPluginServices(this);
773

    
774
                jPanDataSource.setBorder(GeocodingUtils.getTitledBorder(ps
775
                                .getText("datasource")));
776
                jPanType.setBorder(GeocodingUtils.getTitledBorder(ps.getText("gtype")));
777
                jLabLayers.setText(ps.getText("selectedlayer"));
778
                jPanElements.setBorder(GeocodingUtils.getTitledBorder(ps
779
                                .getText("components")));
780
                jPanSettings.setBorder(GeocodingUtils.getTitledBorder(ps
781
                                .getText("settings")));
782
                jButCancel.setText(ps.getText("cancel"));
783
                jButSave.setText(ps.getText("save"));
784

    
785
                String[] volumnNames = { ps.getText("xelement"), ps.getText("xfield") };
786
                ((DefaultTableModel) jTableComponents.getModel())
787
                                .setColumnIdentifiers(volumnNames);
788

    
789
        }
790

    
791
        /**
792
         * This method updates all images of the panel
793
         */
794
        private void setImages() {
795

    
796
                String baseDir = PluginServices.getPluginServices(this)
797
                                .getClassLoader().getBaseDir();
798

    
799
                jButSave.setIcon(new ImageIcon(baseDir + File.separator + "images"
800
                                + File.separator + "icons16" + File.separator + "save.png"));
801
                jButCancel.setIcon(new ImageIcon(baseDir + File.separator + "images"
802
                                + File.separator + "icons16" + File.separator + "stop.png"));
803

    
804
                jButUp.setIcon(new ImageIcon(baseDir + File.separator + "images"
805
                                + File.separator + "icons16" + File.separator + "up.png"));
806
                jButAdd.setIcon(new ImageIcon(baseDir + File.separator + "images"
807
                                + File.separator + "icons16" + File.separator + "add.png"));
808
                jButDel.setIcon(new ImageIcon(baseDir + File.separator + "images"
809
                                + File.separator + "icons16" + File.separator + "remove.png"));
810
                jButDown.setIcon(new ImageIcon(baseDir + File.separator + "images"
811
                                + File.separator + "icons16" + File.separator + "down.png"));
812
        }
813

    
814
        /**
815
         * This method gets attributes of the pattern
816
         */
817
        private boolean getPatternAttributes() {
818

    
819
                boolean ok = true;
820

    
821
                // save the feature store in the pattern
822
                ComboLayerName cLyr = (ComboLayerName) jComboLayers.getSelectedItem();
823
                if (cLyr != null) {
824
                        FLyrVect lyr = cLyr.getLayer();
825
                        String lyrName = lyr.getName();
826
                        DataStore store = lyr.getDataStore();
827
                        String lyrProvider = store.getParameters().getDescription();
828

    
829
                        this.editPattern.getDataSource().setLayerName(lyrName);
830
                        this.editPattern.getDataSource().setLayerProvider(lyrProvider);
831
                } else {
832
                        ok = false;
833
                        String message = PluginServices.getText(null, "nopatternlayer");
834
                        String title = PluginServices.getText(null, "geocoding");
835
                        JOptionPane.showMessageDialog(null, message, title,
836
                                        JOptionPane.ERROR_MESSAGE);
837
                }
838
                if (cLyr != null) {
839
                        // save style of geocoding pattern
840
                        ComboShowName cType = (ComboShowName) jComboType.getSelectedItem();
841
                        AbstractGeocodingStyle style = null;
842
                        if (cType instanceof SimpleCentroidStyle) {
843
                                style = new SimpleCentroid();
844
                                Literal literal = getLiteralTableComponents(style);
845
                                if (literal.size() > 0) {
846
                                        style.setRelationsLiteral(literal);
847
                                        this.editPattern.getDataSource().setStyle(style);
848
                                } else {
849
                                        ok = false;
850
                                        showMessageLiteralEmpty();
851
                                }
852

    
853
                        }
854
                        if (cType instanceof SimpleRangeStyle) {
855
                                style = new SimpleRange();
856
                                Literal literal = getLiteralTableComponents(style);
857
                                if (literal.size() > 0) {
858
                                        style.setRelationsLiteral(literal);
859
                                        this.editPattern.getDataSource().setStyle(style);
860
                                } else {
861
                                        ok = false;
862
                                        showMessageLiteralEmpty();
863
                                }
864
                        }
865
                        if (cType instanceof DoubleRangeStyle) {
866
                                style = new DoubleRange();
867
                                Literal literal = getLiteralTableComponents(style);
868
                                if (literal.size() > 0) {
869
                                        style.setRelationsLiteral(literal);
870
                                        this.editPattern.getDataSource().setStyle(style);
871
                                } else {
872
                                        ok = false;
873
                                        showMessageLiteralEmpty();
874
                                }
875
                        }
876
                        if (cType instanceof ComposedStyle) {
877
                                style = new Composed();
878
                                Literal literal = getLiteralTableComponents(style);
879
                                if (literal.size() > 0) {
880
                                        style.setRelationsLiteral(literal);
881
                                        this.editPattern.getDataSource().setStyle(style);
882
                                } else {
883
                                        ok = false;
884
                                        showMessageLiteralEmpty();
885
                                }
886
                        }
887
                }
888

    
889
                // Get parameters
890
                GeocodingSettings sett = this.editPattern.getSettings();
891
                sett.setMaxResultsNumber(settingsPanel.getMaxResults());
892
                sett.setMinScore(settingsPanel.getScore());
893
                this.editPattern.setSettings(sett);
894

    
895
                return ok;
896
        }
897

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

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

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

    
915
                        jPanRange.add(pan, gridBagConstraints);
916

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

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

    
954
        }
955

    
956
        /**
957
         * 
958
         * @param elements
959
         * @param descs
960
         */
961
        private void fillComponentsCombos(List<String> elements, List<String> descs) {
962

    
963
                if (elements != null) {
964

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

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

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

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

    
1022
                        if (element.compareTo("...") != 0 && desc != null) {
1023
                                literal.put(element, desc);
1024
                        }
1025
                }
1026

    
1027
                if (style instanceof SimpleRange) {
1028
                        String from = srange.getDescriptorFromNum();
1029
                        String to = srange.getDescriptorToNum();
1030
                        // literal.put(GeocodingTags.FIRSTNUMBER, from);
1031
                        // literal.put(GeocodingTags.LASTNUMBER, to);
1032
                        ((SimpleRange) style).setFirstNumber(from);
1033
                        ((SimpleRange) style).setLastNumber(to);
1034
                }
1035
                if (style instanceof DoubleRange) {
1036
                        String lfrom = drange.getDescriptorLeftFronMun();
1037
                        String lto = drange.getDescriptorLeftToMun();
1038
                        String rfrom = drange.getDescriptorRightFronMun();
1039
                        String rto = drange.getDescriptorRightToMun();
1040
                        // literal.put(GeocodingTags.FIRSTLEFTNUMBER, lfrom);
1041
                        // literal.put(GeocodingTags.FIRSTRIGHTNUMBER, lto);
1042
                        // literal.put(GeocodingTags.LASTLEFTNUMBER, rfrom);
1043
                        // literal.put(GeocodingTags.LASTRIGHTNUMBER, rto);
1044

    
1045
                        ((DoubleRange) style).setFirstLeftNumber(lfrom);
1046
                        ((DoubleRange) style).setFirstRightNumber(lto);
1047
                        ((DoubleRange) style).setLastLeftNumber(rfrom);
1048
                        ((DoubleRange) style).setLastRightNumber(rto);
1049
                }
1050

    
1051
                return literal;
1052
        }
1053

    
1054
        /**
1055
         * set literal to table of components
1056
         * 
1057
         * @param lit
1058
         */
1059

    
1060
        private void setLiteralTableComponents(Literal lit) {
1061
                // clean the table model
1062
                this.clearTableModel((DefaultTableModel) jTableComponents.getModel());
1063
                DefaultTableModel model = (DefaultTableModel) jTableComponents
1064
                                .getModel();
1065
                for (Map.Entry<String, String> ele : lit.entrySet()) {
1066

    
1067
                        String key = ele.getKey();
1068
                        if (key.compareTo(GeocodingTags.FIRSTNUMBER) == 0
1069
                                        || key.compareTo(GeocodingTags.LASTNUMBER) == 0
1070
                                        || key.compareTo(GeocodingTags.FIRSTRIGHTNUMBER) == 0
1071
                                        || key.compareTo(GeocodingTags.FIRSTLEFTNUMBER) == 0
1072
                                        || key.compareTo(GeocodingTags.LASTRIGHTNUMBER) == 0
1073
                                        || key.compareTo(GeocodingTags.LASTLEFTNUMBER) == 0) {
1074
                                // nothing to do
1075
                        } else {
1076
                                Object[] obje = { ele.getKey(), ele.getValue() };
1077
                                model.addRow(obje);
1078
                        }
1079
                }
1080
                jTableComponents.setModel(model);
1081

    
1082
        }
1083

    
1084
        /**
1085
         * Show a error message when the literal has not components
1086
         */
1087
        private void showMessageLiteralEmpty() {
1088
                String title = PluginServices.getText(null, "geocoding");
1089
                String message = PluginServices.getText(null, "literalempty");
1090
                JOptionPane.showMessageDialog(null, message, title,
1091
                                JOptionPane.ERROR_MESSAGE);
1092

    
1093
        }
1094

    
1095
        /**
1096
         * Clear all rows of the TableModel
1097
         * 
1098
         * @param model
1099
         */
1100
        private void clearTableModel(DefaultTableModel model) {
1101
                int n = model.getRowCount();
1102
                for (int i = n - 1; i > -1; i--) {
1103
                        model.removeRow(i);
1104
                }
1105
        }
1106

    
1107
}