Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extGeocoding / src / org / gvsig / geocoding / gui / newpattern / NewPatternPanel.java @ 31285

History | View | Annotate | Download (34.1 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.FeatureType;
59
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
60
import org.gvsig.geocoding.GeocodingTags;
61
import org.gvsig.geocoding.address.Literal;
62
import org.gvsig.geocoding.address.impl.DefaultLiteral;
63
import org.gvsig.geocoding.extension.GeocodingController;
64
import org.gvsig.geocoding.gui.settings.SettingsPanel;
65
import org.gvsig.geocoding.gui.styles.ComposedStyle;
66
import org.gvsig.geocoding.gui.styles.DoubleRangeStyle;
67
import org.gvsig.geocoding.gui.styles.SimpleCentroidStyle;
68
import org.gvsig.geocoding.gui.styles.SimpleRangeStyle;
69
import org.gvsig.geocoding.pattern.GeocodingSettings;
70
import org.gvsig.geocoding.pattern.GeocodingPattern;
71
import org.gvsig.geocoding.pattern.impl.DefaultGeocodingPattern;
72
import org.gvsig.geocoding.styles.AbstractGeocodingStyle;
73
import org.gvsig.geocoding.styles.impl.Composed;
74
import org.gvsig.geocoding.styles.impl.DoubleRange;
75
import org.gvsig.geocoding.styles.impl.SimpleCentroid;
76
import org.gvsig.geocoding.styles.impl.SimpleRange;
77
import org.gvsig.geocoding.utils.ComboLayerName;
78
import org.gvsig.geocoding.utils.ComboShowName;
79
import org.gvsig.geocoding.utils.GeocodingUtils;
80

    
81
import org.slf4j.Logger;
82
import org.slf4j.LoggerFactory;
83

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

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

    
96
        private GeocodingPattern pattern = null;
97
        private GeocodingController control = null;
98

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

    
102
        private boolean editing = false;
103

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

    
126
        private SettingsPanel settingsPanel;
127

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

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

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

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

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

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

    
165
        }
166

    
167
        /**
168
         * 
169
         * @param pat
170
         */
171
        public void updatePanel(GeocodingPattern pat) {
172

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

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

    
210
        }
211

    
212
        /**
213
         * Get window info
214
         * 
215
         * @return
216
         */
217
        public WindowInfo getWindowInfo() {
218
                WindowInfo info = new WindowInfo(WindowInfo.MODALDIALOG
219
                                | WindowInfo.RESIZABLE);
220

    
221
                info.setMinimumSize(this.getMinimumSize());
222
                info.setWidth(480);
223
                info.setHeight(470);
224
                info.setTitle(PluginServices.getText(this, "new_geocoding_pattern"));
225
                return info;
226
        }
227

    
228
        /**
229
         * get window profile
230
         * 
231
         * @return
232
         */
233
        public Object getWindowProfile() {
234
                return null;
235
        }
236

    
237
        /**
238
         * Initialize panel components
239
         */
240
        @SuppressWarnings("serial")
241
        private void initComponents() {
242
                java.awt.GridBagConstraints gridBagConstraints;
243

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

    
267
                setLayout(new java.awt.GridBagLayout());
268

    
269
                jPanType.setBorder(javax.swing.BorderFactory
270
                                .createTitledBorder("Geocoding type"));
271
                jPanType.setLayout(new java.awt.GridBagLayout());
272

    
273
                jLabType.setIcon(new ImageIcon(PluginServices.getPluginServices(this)
274
                                .getClassLoader().getBaseDir()
275
                                + File.separator
276
                                + "images"
277
                                + File.separator + "multi_centroid.png"));
278
                gridBagConstraints = new java.awt.GridBagConstraints();
279
                gridBagConstraints.gridx = 0;
280
                gridBagConstraints.gridy = 0;
281
                gridBagConstraints.gridheight = 2;
282
                gridBagConstraints.insets = new java.awt.Insets(2, 10, 2, 5);
283
                jPanType.add(jLabType, gridBagConstraints);
284

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

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

    
312
                gridBagConstraints = new java.awt.GridBagConstraints();
313
                gridBagConstraints.gridx = 1;
314
                gridBagConstraints.gridy = 1;
315
                gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
316
                gridBagConstraints.weightx = 1.0;
317
                gridBagConstraints.weighty = 1.0;
318
                gridBagConstraints.insets = new java.awt.Insets(5, 10, 3, 10);
319
                jPanType.add(jScrollTypeInfo, gridBagConstraints);
320

    
321
                gridBagConstraints = new java.awt.GridBagConstraints();
322
                gridBagConstraints.gridx = 0;
323
                gridBagConstraints.gridy = 0;
324
                gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
325
                gridBagConstraints.weightx = 1.0;
326
                gridBagConstraints.weighty = 0.3;
327
                gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
328
                add(jPanType, gridBagConstraints);
329

    
330
                jPanDataSource.setBorder(javax.swing.BorderFactory
331
                                .createTitledBorder("Data source"));
332
                jPanDataSource.setLayout(new java.awt.GridBagLayout());
333

    
334
                jLabLayers.setText("Layers");
335
                gridBagConstraints = new java.awt.GridBagConstraints();
336
                gridBagConstraints.gridx = 0;
337
                gridBagConstraints.gridy = 0;
338
                gridBagConstraints.insets = new java.awt.Insets(2, 10, 2, 5);
339
                jPanDataSource.add(jLabLayers, gridBagConstraints);
340

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

    
358
                gridBagConstraints = new java.awt.GridBagConstraints();
359
                gridBagConstraints.gridx = 0;
360
                gridBagConstraints.gridy = 1;
361
                gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
362
                gridBagConstraints.weightx = 1.0;
363
                gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
364
                add(jPanDataSource, gridBagConstraints);
365

    
366
                jPanElements.setBorder(javax.swing.BorderFactory
367
                                .createTitledBorder("Elements"));
368
                jPanElements.setLayout(new java.awt.GridBagLayout());
369

    
370
                jTableComponents.setModel(new javax.swing.table.DefaultTableModel(
371
                                new Object[][] {}, new String[] { "Elements", "Field" }) {
372
                        boolean[] canEdit = new boolean[] { true, true };
373

    
374
                        public boolean isCellEditable(int rowIndex, int columnIndex) {
375
                                return canEdit[columnIndex];
376
                        }
377
                });
378

    
379
                jScrollElements.setViewportView(jTableComponents);
380

    
381
                gridBagConstraints = new java.awt.GridBagConstraints();
382
                gridBagConstraints.gridx = 0;
383
                gridBagConstraints.gridy = 0;
384
                gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
385
                gridBagConstraints.weightx = 1.0;
386
                gridBagConstraints.weighty = 0.9;
387
                jPanElements.add(jScrollElements, gridBagConstraints);
388

    
389
                jPanRange.setLayout(new java.awt.GridBagLayout());
390
                gridBagConstraints = new java.awt.GridBagConstraints();
391
                gridBagConstraints.gridx = 0;
392
                gridBagConstraints.gridy = 1;
393
                gridBagConstraints.gridwidth = 2;
394
                gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 2);
395
                jPanElements.add(jPanRange, gridBagConstraints);
396

    
397
                jPanButsEle.setLayout(new java.awt.GridBagLayout());
398

    
399
                jButUp.addActionListener(new java.awt.event.ActionListener() {
400
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
401
                                evUpComponent(evt);
402
                        }
403
                });
404
                gridBagConstraints = new java.awt.GridBagConstraints();
405
                gridBagConstraints.gridx = 0;
406
                gridBagConstraints.gridy = 0;
407
                gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
408
                jPanButsEle.add(jButUp, gridBagConstraints);
409

    
410
                jButAdd.addActionListener(new java.awt.event.ActionListener() {
411
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
412
                                evAddComponent(evt);
413
                        }
414
                });
415
                gridBagConstraints = new java.awt.GridBagConstraints();
416
                gridBagConstraints.gridx = 0;
417
                gridBagConstraints.gridy = 1;
418
                jPanButsEle.add(jButAdd, gridBagConstraints);
419

    
420
                jButDel.addActionListener(new java.awt.event.ActionListener() {
421
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
422
                                evDelComponent(evt);
423
                        }
424
                });
425
                gridBagConstraints = new java.awt.GridBagConstraints();
426
                gridBagConstraints.gridx = 0;
427
                gridBagConstraints.gridy = 2;
428
                jPanButsEle.add(jButDel, gridBagConstraints);
429

    
430
                jButDown.addActionListener(new java.awt.event.ActionListener() {
431
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
432
                                evDownComponent(evt);
433
                        }
434
                });
435
                gridBagConstraints = new java.awt.GridBagConstraints();
436
                gridBagConstraints.gridx = 0;
437
                gridBagConstraints.gridy = 3;
438
                jPanButsEle.add(jButDown, gridBagConstraints);
439

    
440
                gridBagConstraints = new java.awt.GridBagConstraints();
441
                gridBagConstraints.gridx = 1;
442
                gridBagConstraints.gridy = 0;
443
                gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
444
                gridBagConstraints.weighty = 1.0;
445
                gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 0);
446
                jPanElements.add(jPanButsEle, gridBagConstraints);
447

    
448
                gridBagConstraints = new java.awt.GridBagConstraints();
449
                gridBagConstraints.gridx = 0;
450
                gridBagConstraints.gridy = 2;
451
                gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
452
                gridBagConstraints.weightx = 1.0;
453
                gridBagConstraints.weighty = 1.0;
454
                gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
455
                add(jPanElements, gridBagConstraints);
456

    
457
                jPanSettings.setBorder(javax.swing.BorderFactory
458
                                .createTitledBorder("Settings"));
459
                jPanSettings.setLayout(new java.awt.GridBagLayout());
460

    
461
                gridBagConstraints = new java.awt.GridBagConstraints();
462
                gridBagConstraints.gridx = 0;
463
                gridBagConstraints.gridy = 0;
464
                gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
465
                gridBagConstraints.weightx = 1.0;
466
                gridBagConstraints.weighty = 1.0;
467
                gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
468
                jPanSettings.add(settingsPanel, gridBagConstraints);
469

    
470
                gridBagConstraints = new java.awt.GridBagConstraints();
471
                gridBagConstraints.gridx = 0;
472
                gridBagConstraints.gridy = 3;
473
                gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
474
                gridBagConstraints.weightx = 1.0;
475
                gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
476
                add(jPanSettings, gridBagConstraints);
477

    
478
                jPanButtons.setLayout(new java.awt.GridBagLayout());
479

    
480
                jButSave.setText("Save");
481
                jButSave.addActionListener(new java.awt.event.ActionListener() {
482
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
483
                                evSave(evt);
484
                        }
485
                });
486
                gridBagConstraints = new java.awt.GridBagConstraints();
487
                gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 10);
488
                jPanButtons.add(jButSave, gridBagConstraints);
489

    
490
                jButCancel.setText("Cancel");
491
                jButCancel.addActionListener(new java.awt.event.ActionListener() {
492
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
493
                                evCancel(evt);
494
                        }
495
                });
496
                gridBagConstraints = new java.awt.GridBagConstraints();
497
                gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
498
                jPanButtons.add(jButCancel, gridBagConstraints);
499

    
500
                gridBagConstraints = new java.awt.GridBagConstraints();
501
                gridBagConstraints.gridx = 0;
502
                gridBagConstraints.gridy = 4;
503
                gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
504
                gridBagConstraints.weightx = 1.0;
505
                gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
506
                add(jPanButtons, gridBagConstraints);
507
        }
508

    
509
        /**
510
         * Change layer selected in the combo
511
         * 
512
         * @param evt
513
         */
514
        private void evChangeLayer(java.awt.event.ActionEvent evt) {
515
                ComboLayerName cLyr = (ComboLayerName) jComboLayers.getSelectedItem();
516
                if (cLyr != null) {
517

    
518
                        // reload the descriptors of the new layer in the combos
519
                        srange.setComboValues(getLayerDescriptors());
520
                        drange.setComboValues(getLayerDescriptors());
521

    
522
                        // clean the table model
523
                        this.clearTableModel((DefaultTableModel) jTableComponents
524
                                        .getModel());
525
                        // jTableComponents.setModel(model);
526
                        jTableComponents.validate();
527

    
528
                        // fill the combos of the table with descriptors of the new layer
529
                        fillComponentsCombos(control.getListAddressComponents(),
530
                                        getLayerDescriptors());
531
                }
532

    
533
        }
534

    
535
        /**
536
         * save the pattern
537
         * 
538
         * @param evt
539
         */
540
        private void evSave(java.awt.event.ActionEvent evt) {
541
                /* PreSave */
542
                boolean ok = preSave();
543

    
544
                if (ok) {
545
                        /* save the new pattern in the model */
546
                        control.getGmodel().setPattern(pattern);
547

    
548
                        /* save pattern */
549
                        boolean apto = control.savePattern();
550

    
551
                        /* close the panel */
552
                        if (apto) {
553
                                IWindow[] iws = PluginServices.getMDIManager().getAllWindows();
554
                                for (int i = 0; i < iws.length; i++) {
555
                                        if (iws[i] instanceof NewPatternPanel) {
556
                                                PluginServices.getMDIManager().closeWindow(iws[i]);
557
                                        }
558
                                }
559
                                // update panel
560
                                control.updateGeocoGUI();
561

    
562
                                // enable components
563
                                if (control.getPattern() != null) {
564
                                        control.getGPanel().enableComponents();
565
                                        // insert address panel
566
                                        control.getGPanel().insertAddressPanel();
567

    
568
                                }
569
                                // clean table results
570
                                control.clearGeocodingPoint();
571
                                // clean points on view
572
                                control.getGPanel().cleanTableResults();
573
                        }
574
                }
575
        }
576

    
577
        /**
578
         * Cancel the process
579
         * 
580
         * @param evt
581
         */
582
        private void evCancel(java.awt.event.ActionEvent evt) {
583
                /* Close the window */
584
                IWindow[] iws = PluginServices.getMDIManager().getAllWindows();
585
                for (int i = 0; i < iws.length; i++) {
586
                        if (iws[i] instanceof NewPatternPanel) {
587
                                PluginServices.getMDIManager().closeWindow(iws[i]);
588
                        }
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
                
601
                String baseDir = PluginServices.getPluginServices(this)
602
                .getClassLoader().getBaseDir();
603
                
604
                if (selected instanceof SimpleCentroidStyle) {
605
                        jTextTypeInfo.setText(PluginServices.getText(null, "simcenstyle"));
606
                        insertDynamicPanel(null);
607
                        String path = baseDir
608
                        + File.separator
609
                        + "images"
610
                        + File.separator + "multi_centroid.png";
611
                        jLabType.setIcon(new ImageIcon(path));
612
                        jLabType.validate();
613
                        this.validate();
614
                }
615
                if (selected instanceof SimpleRangeStyle) {
616
                        jTextTypeInfo
617
                                        .setText(PluginServices.getText(null, "rangesimstyle"));
618
                        insertDynamicPanel(srange);
619
                        List<String> desc = getLayerDescriptors();
620
                        srange.setComboValues(desc);
621
                        String path = baseDir
622
                        + File.separator
623
                        + "images"
624
                        + File.separator + "single_rank.png";
625
                        jLabType.setIcon(new ImageIcon(path));
626
                        jLabType.validate();
627
                        this.validate();
628
                }
629
                if (selected instanceof DoubleRangeStyle) {
630
                        jTextTypeInfo
631
                                        .setText(PluginServices.getText(null, "rangedoustyle"));
632
                        insertDynamicPanel(drange);
633
                        List<String> desc = getLayerDescriptors();
634
                        drange.setComboValues(desc);
635
                        String path = baseDir
636
                        + File.separator
637
                        + "images"
638
                        + File.separator + "double_rank.png";
639
                        jLabType.setIcon(new ImageIcon(path));
640
                        jLabType.validate();
641
                        this.validate();
642
                }
643
                if (selected instanceof ComposedStyle) {
644
                        jTextTypeInfo.setText(PluginServices.getText(null, "compostyle"));
645
                        insertDynamicPanel(null);
646
                        String path = baseDir
647
                        + File.separator
648
                        + "images"
649
                        + File.separator + "composed.png";
650
                        jLabType.setIcon(new ImageIcon(path));
651
                        jLabType.validate();
652
                        this.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 = control.getListgvSIGVectLayers();
744
                DefaultComboBoxModel model = new DefaultComboBoxModel();
745
                if (lyrs.size() > 0) {
746
                        for (FLyrVect lyr : lyrs) {
747
                                ComboLayerName cLyr = new ComboLayerName(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(GeocodingUtils.getTitledBorder(ps
764
                                .getText("datasource")));
765
                jPanType.setBorder(GeocodingUtils.getTitledBorder(ps.getText("gtype")));
766
                jLabLayers.setText(ps.getText("selectedlayer"));
767
                jPanElements.setBorder(GeocodingUtils.getTitledBorder(ps
768
                                .getText("components")));
769
                jPanSettings.setBorder(GeocodingUtils.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
                ComboLayerName cLyr = (ComboLayerName) jComboLayers.getSelectedItem();
812
                if (cLyr != null) {
813
                        FLyrVect lyr = cLyr.getLayer();
814
                        String lyrName = lyr.getName();
815
                        DataStore store = lyr.getDataStore();
816
                        String lyrProvider = store.getParameters().getDescription();
817

    
818
                        this.pattern.getSource().setLayerName(lyrName);
819
                        this.pattern.getSource().setLayerProvider(lyrProvider);
820

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

    
843
                        }
844
                        if (cType instanceof SimpleRangeStyle) {
845
                                style = new SimpleRange();
846
                                Literal literal = getLiteralTableComponents(style);
847
                                if (literal.size() > 0) {
848
                                        style.setRelationsLiteral(literal);
849
                                        pattern.getSource().setStyle(style);
850
                                } else {
851
                                        ok = false;
852
                                        showMessageLiteralEmpty();
853
                                }
854
                        }
855
                        if (cType instanceof DoubleRangeStyle) {
856
                                style = new DoubleRange();
857
                                Literal literal = getLiteralTableComponents(style);
858
                                if (literal.size() > 0) {
859
                                        style.setRelationsLiteral(literal);                                        
860
                                        pattern.getSource().setStyle(style);
861
                                } else {
862
                                        ok = false;
863
                                        showMessageLiteralEmpty();
864
                                }
865
                        }
866
                        if (cType instanceof ComposedStyle) {
867
                                style = new Composed();
868
                                Literal literal = getLiteralTableComponents(style);
869
                                if (literal.size() > 0) {
870
                                        style.setRelationsLiteral(literal);
871
                                        pattern.getSource().setStyle(style);
872
                                } else {
873
                                        ok = false;
874
                                        showMessageLiteralEmpty();
875
                                }
876
                        }
877
                }
878

    
879
                // Get parameters
880
                GeocodingSettings sett = pattern.getSettings();
881
                sett.setResultsNumber(settingsPanel.getMaxResults());
882
                sett.setScore(settingsPanel.getScore());
883
                pattern.setSettings(sett);
884

    
885
                return ok;
886
        }
887

    
888
        /**
889
         * insert ranges panel
890
         * 
891
         * @param pan
892
         */
893
        private void insertDynamicPanel(JPanel pan) {
894

    
895
                if (pan != null) {
896
                        jPanRange.removeAll();
897

    
898
                        GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
899
                        gridBagConstraints.gridx = 0;
900
                        gridBagConstraints.gridy = 0;
901
                        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
902
                        gridBagConstraints.weightx = 1.0;
903
                        gridBagConstraints.weighty = 1.0;
904

    
905
                        jPanRange.add(pan, gridBagConstraints);
906

    
907
                        this.setSize(this.getWidth() + 1, this.getHeight());
908
                        jPanRange.validate();
909
                        pan.validate();
910
                        this.validate();
911
                } else {
912
                        jPanRange.removeAll();
913
                        this.validate();
914
                        jPanRange.validate();
915
                }
916
        }
917

    
918
        /**
919
         * Get layer descriptors
920
         * 
921
         * @return
922
         */
923
        private List<String> getLayerDescriptors() {
924
                try {
925
                        ComboLayerName cLyr = (ComboLayerName) jComboLayers
926
                                        .getSelectedItem();
927
                        if (cLyr != null) {
928
                                List<String> descs = new ArrayList<String>();
929
                                FeatureType fType = cLyr.getLayer().getFeatureStore()
930
                                                .getDefaultFeatureType();
931
                                for (int i = 0; i < fType.size(); i++) {
932
                                        descs.add(fType.getAttributeDescriptor(i).getName());
933
                                }
934
                                return descs;
935
                        }
936
                } catch (Exception e) {
937
                        return null;
938
                }
939
                return null;
940

    
941
        }
942

    
943
        /**
944
         * 
945
         * @param elements
946
         * @param descs
947
         */
948
        private void fillComponentsCombos(List<String> elements, List<String> descs) {
949

    
950
                if (elements != null) {
951

    
952
                        // first column (elements)
953
                        TableColumn column0 = jTableComponents.getColumnModel()
954
                                        .getColumn(0);
955
                        DefaultTableCellRenderer render = new DefaultTableCellRenderer();
956
                        render.setBackground(new Color(255, 253, 224));
957
                        column0.setCellRenderer(render);
958
                        JComboBox combo0 = new JComboBox();
959
                        DefaultComboBoxModel model0 = new DefaultComboBoxModel();
960
                        for (String ele : elements) {
961
                                model0.addElement(ele);
962
                        }
963
                        combo0.setModel(model0);
964
                        DefaultCellEditor dce = new DefaultCellEditor(combo0);
965
                        dce.setClickCountToStart(2);
966
                        column0.setCellEditor(dce);
967
                }
968

    
969
                if (descs != null) {
970
                        // second column (fields)
971
                        TableColumn column1 = jTableComponents.getColumnModel()
972
                                        .getColumn(1);
973
                        JComboBox combo1 = new JComboBox();
974
                        DefaultComboBoxModel model1 = new DefaultComboBoxModel();
975
                        for (String desc : descs) {
976
                                model1.addElement(desc);
977
                        }
978
                        combo1.setModel(model1);
979
                        DefaultCellEditor dce = new DefaultCellEditor(combo1);
980
                        dce.setClickCountToStart(2);
981
                        column1.setCellEditor(dce);
982
                }
983
        }
984

    
985
        /**
986
         * get literal from table of components
987
         * 
988
         * @return
989
         */
990
        @SuppressWarnings("unchecked")
991
        private Literal getLiteralTableComponents(AbstractGeocodingStyle style) {
992
                Literal literal = new DefaultLiteral();
993
                DefaultTableModel model = (DefaultTableModel) jTableComponents
994
                                .getModel();
995
                Vector data = model.getDataVector();
996
                for (int i = 0; i < data.size(); i++) {
997
                        String desc = null;
998
                        String element = "";
999
                        try {
1000
                                Vector vec = (Vector) data.elementAt(i);
1001
                                element = (String) vec.elementAt(0);
1002

    
1003
                                desc = (String) vec.elementAt(1);
1004
                        } catch (Exception e) {
1005
                                desc = null;
1006
                                element = "...";
1007
                        }
1008

    
1009
                        if (element.compareTo("...") != 0 && desc != null) {
1010
                                literal.put(element, desc);
1011
                        }
1012
                }
1013

    
1014
                if (style instanceof SimpleRange) {
1015
                        String from = srange.getDescriptorFromNum();
1016
                        String to = srange.getDescriptorToNum();
1017
                        literal.put(GeocodingTags.FIRSTNUMBER, from);
1018
                        literal.put(GeocodingTags.LASTNUMBER, to);
1019
                        ((SimpleRange)style).setFirstNumber(GeocodingTags.FIRSTNUMBER);
1020
                        ((SimpleRange)style).setLastNumber(GeocodingTags.LASTNUMBER);
1021
                }
1022
                if (style instanceof DoubleRange) {
1023
                        String lfrom = drange.getDescriptorLeftFronMun();
1024
                        String lto = drange.getDescriptorLeftToMun();
1025
                        String rfrom = drange.getDescriptorRightFronMun();
1026
                        String rto = drange.getDescriptorRightToMun();
1027
                        literal.put(GeocodingTags.FIRSTLEFTNUMBER, lfrom);
1028
                        literal.put(GeocodingTags.FIRSTRIGHTNUMBER, lto);
1029
                        literal.put(GeocodingTags.LASTLEFTNUMBER, rfrom);
1030
                        literal.put(GeocodingTags.LASTRIGHTNUMBER, rto);
1031
                        
1032
                        ((DoubleRange)style).setFirstLeftNumber(GeocodingTags.FIRSTLEFTNUMBER);
1033
                        ((DoubleRange)style).setFirstRightNumber(GeocodingTags.FIRSTRIGHTNUMBER);
1034
                        ((DoubleRange)style).setLastLeftNumber(GeocodingTags.LASTLEFTNUMBER);
1035
                        ((DoubleRange)style).setLastRightNumber(GeocodingTags.LASTRIGHTNUMBER);
1036
                }
1037

    
1038
                return literal;
1039
        }
1040

    
1041
        /**
1042
         * set literal to table of components
1043
         * 
1044
         * @param lit
1045
         */
1046

    
1047
        private void setLiteralTableComponents(Literal lit) {
1048
                // clean the table model
1049
                this.clearTableModel((DefaultTableModel) jTableComponents.getModel());
1050
                DefaultTableModel model = (DefaultTableModel) jTableComponents
1051
                                .getModel();
1052
                for (Map.Entry<String, String> ele : lit.entrySet()) {
1053
                        Object[] obje = { ele.getKey(), ele.getValue() };
1054
                        model.addRow(obje);
1055
                }
1056
                jTableComponents.setModel(model);
1057

    
1058
        }
1059

    
1060

    
1061

    
1062
        /**
1063
         * Show a error message when the literal has not components
1064
         */
1065
        private void showMessageLiteralEmpty() {
1066
                String title = PluginServices.getText(null, "geocoding");
1067
                String message = PluginServices.getText(null, "literalempty");
1068
                JOptionPane.showMessageDialog(null, message, title,
1069
                                JOptionPane.ERROR_MESSAGE);
1070

    
1071
        }
1072

    
1073
        /**
1074
         * Clear all rows of the TableModel
1075
         * 
1076
         * @param model
1077
         */
1078
        private void clearTableModel(DefaultTableModel model) {
1079
                int n = model.getRowCount();
1080
                for (int i = n - 1; i > -1; i--) {
1081
                        model.removeRow(i);
1082
                }
1083
        }
1084

    
1085
}