Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extDerivedGeometries / src / es / iver / derivedGeom / gui / AddGeometricInfoPanel.java @ 37422

History | View | Annotate | Download (55.9 KB)

1
package es.iver.derivedGeom.gui;
2

    
3
/* gvSIG. Geographic Information System of the Valencian Government
4
 *
5
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
6
 * of the Valencian Government (CIT)
7
 * 
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 * 
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *  
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
21
 * MA  02110-1301, USA.
22
 * 
23
 */
24

    
25
import java.awt.Color;
26
import java.awt.Component;
27
import java.awt.Dimension;
28
import java.awt.FlowLayout;
29
import java.awt.Font;
30
import java.awt.event.ActionEvent;
31
import java.awt.event.ActionListener;
32
import java.awt.event.ComponentAdapter;
33
import java.awt.event.ComponentEvent;
34
import java.awt.event.ItemEvent;
35
import java.awt.event.ItemListener;
36
import java.awt.event.MouseAdapter;
37
import java.awt.event.MouseEvent;
38
import java.util.ArrayList;
39
import java.util.HashSet;
40

    
41
import javax.swing.BorderFactory;
42
import javax.swing.DefaultListModel;
43
import javax.swing.Icon;
44
import javax.swing.ImageIcon;
45
import javax.swing.JButton;
46
import javax.swing.JCheckBox;
47
import javax.swing.JComponent;
48
import javax.swing.JLabel;
49
import javax.swing.JList;
50
import javax.swing.JOptionPane;
51
import javax.swing.JPanel;
52
import javax.swing.JScrollPane;
53
import javax.swing.JTree;
54
import javax.swing.ListCellRenderer;
55
import javax.swing.ListSelectionModel;
56
import javax.swing.Spring;
57
import javax.swing.SpringLayout;
58
import javax.swing.UIManager;
59
import javax.swing.event.ListDataEvent;
60
import javax.swing.event.ListDataListener;
61
import javax.swing.event.ListSelectionEvent;
62
import javax.swing.event.ListSelectionListener;
63
import javax.swing.tree.DefaultMutableTreeNode;
64
import javax.swing.tree.TreePath;
65

    
66
import org.apache.log4j.Logger;
67

    
68
import org.gvsig.fmap.swing.toc.TOC;
69
import org.gvsig.gui.beans.AcceptCancelPanel;
70
import org.gvsig.gui.beans.comboboxconfigurablelookup.DefaultComboBoxConfigurableLookUpModel;
71
import org.gvsig.gui.beans.comboboxconfigurablelookup.JComboBoxConfigurableLookUp;
72
import org.gvsig.gui.beans.incrementabletask.IncrementableTask;
73
import org.gvsig.gui.beans.progresspanel.ProgressPanel;
74
import org.gvsig.gui.beans.specificcaretposition.JTextFieldWithSCP;
75
import org.gvsig.gui.javax.swing.jLabelCellRenderer.JLabelCellRenderer;
76

    
77
import com.hardcode.gdbms.driver.exceptions.InitializeDriverException;
78
import com.hardcode.gdbms.driver.exceptions.InitializeWriterException;
79
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
80
import com.hardcode.gdbms.engine.data.driver.DriverException;
81
import com.iver.andami.PluginServices;
82
import com.iver.andami.messages.NotificationManager;
83
import com.iver.andami.ui.mdiManager.IWindow;
84
import com.iver.andami.ui.mdiManager.WindowInfo;
85
import com.iver.cit.gvsig.EditionUtilities;
86
import com.iver.cit.gvsig.exceptions.visitors.StopWriterVisitorException;
87
import com.iver.cit.gvsig.fmap.MapControl;
88
import com.iver.cit.gvsig.fmap.core.FShape;
89
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
90
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
91
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
92
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
93
import com.iver.cit.gvsig.fmap.edition.ISpatialWriter;
94
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
95
import com.iver.cit.gvsig.fmap.layers.FLayer;
96
import com.iver.cit.gvsig.fmap.layers.FLayers;
97
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
98
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
99
import com.iver.cit.gvsig.project.documents.table.ProjectTable;
100
import com.iver.cit.gvsig.project.documents.table.gui.Table;
101
import com.iver.cit.gvsig.project.documents.view.gui.View;
102
import com.iver.cit.gvsig.project.documents.view.toc.TocItemBranch;
103
import com.iver.utiles.swing.JComboBox;
104

    
105
import es.iver.derivedGeom.process.AddGeometricInfoProcess;
106
import es.iver.derivedGeom.utils.FShapeTypeNames;
107
import es.iver.derivedGeom.utils.GeomInfo;
108
import es.iver.derivedGeom.utils.GeomInfoFactory;
109
import es.iver.derivedGeom.utils.StringUtilitiesExtended;
110

    
111
/**
112
 * <p>Panel where user will select the geometric information of the layer, to be added.</p>
113
 * 
114
 * @version 23/05/08
115
 * 
116
 * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
117
 */
118
public class AddGeometricInfoPanel extends JPanel implements IWindow {
119
        private static final long serialVersionUID = -3858635696744526624L;
120

    
121
        private View view;
122
        private MapControl mapControl;
123
        private JLabel layersLabel = null;
124
        private JScrollPane southWestScrollPane = null;
125
    private JComboBox layersComboBox = null;
126
    private JPanel northPanel = null;
127
    private JPanel southPanel = null;
128
    private JPanel layersPanel = null;
129
    private JPanel southEastPanel = null;
130
    private JPanel southCenterPanel = null;
131
    private JPanel southEastCenterPanel = null;
132
    private JLabel isEditableLabel = null;
133
        private AdaptedAcceptCancelPanel acceptCancelPanel = null;
134
        private JScrollPane addedAttributesScrollPane = null;
135
    private JCheckBox newColumnCheckBox = null;
136
    private JTextFieldWithSCP nameOfColumnText = null;
137
    private JComboBoxConfigurableLookUp nameOfColumnCombo = null;
138
    private JList attributesList = null;
139
    private JList attributesAddedList = null;
140
        
141
        private ImageIcon bIcon;
142
        private ImageIcon rightIcon;
143
        private ImageIcon leftIcon;
144
        private ImageIcon doubleRightIcon;
145
        private ImageIcon doubleLeftIcon;
146
        private ImageIcon iconYes;
147
        private ImageIcon iconNo;
148
        private ImageIcon reloadIcon;
149
        private ImageIcon saveGPIcon;
150
        private JButton rightIconButton;
151
        private JButton leftIconButton;
152
        private JButton doubleRightIconButton;
153
        private JButton doubleLeftIconButton;
154
        private JButton reloadIconButton;
155
        private JButton saveGeomPropertyButton;
156
        private JComponent currentColumnNameComponent;
157

    
158
        private GeomInfo currentField;
159

    
160
    private WindowInfo viewInfo = null;
161
    private final short Window_Width = 510;
162
    private final short Window_Height = 300;
163
    private final int layersComboBox_Width = 310;
164
    private final int layersComboBox_Height = 22;
165
    private HashSet fieldNames = new HashSet();
166

    
167
    // Layer types:
168
    private final short UNDEFINED = -1;
169
    // End Layer types
170
    
171
    private int previous_Type = UNDEFINED;
172

    
173
    // Field name component type
174
//    private final Class TEXT_TYPE = JTextFieldWithSCP.class;
175
//    private final Class COMBO_TYPE = JComboBoxConfigurableLookUp.class;
176
    // End field name component type
177
    
178
    /**
179
     * <p>Creates a new form where user could select the geometric information to add.</p>
180
     */
181
    public AddGeometricInfoPanel(View view) {
182
            super();
183

    
184
            this.view = view;
185
            this.mapControl = view.getMapControl();
186
            
187
            initialize();
188
    }
189
    
190
    /**
191
     * <p>Initializes this component.</p>
192
     */                        
193
    private void initialize() {
194
                setLayout(new FlowLayout());
195
                
196
                currentField = null;
197
                
198
                bIcon = PluginServices.getIconTheme().get("/layerGroup");
199
                
200
            add(getNorthPanel());
201
            add(getSouthPanel());
202
            add(getAdaptedAcceptCancelPanel());
203

    
204
            getAdaptedAcceptCancelPanel().setOkButtonEnabled(false);
205

    
206
                setVisible(false);
207
            if (!refreshVisibleVectorLayers()) {
208
                    JOptionPane.showMessageDialog(this, PluginServices.getText(this, "No_vector_layer_can_be_save_changes"), PluginServices.getText(this, "Warning"), JOptionPane.WARNING_MESSAGE);
209
            } else {
210
                    setVisible(true);
211
                    PluginServices.getMDIManager().addWindow(this);
212
            }
213
    }
214

    
215
    /**
216
     * <p>This method initializes acceptCancelPanel.</p>        
217
     * 
218
     * @return an adapted {@link AcceptCancelPanel AcceptCancelPanel}
219
     */
220
    private AdaptedAcceptCancelPanel getAdaptedAcceptCancelPanel() {
221
            if (acceptCancelPanel == null) {
222
                    acceptCancelPanel = new AdaptedAcceptCancelPanel();
223
            }
224
            
225
            return acceptCancelPanel;
226
    }
227

    
228
    /**
229
         * <p>This method initializes northPanel.</p>        
230
         *         
231
         * @return javax.swing.JPanel
232
     */
233
    private JPanel getNorthPanel() {
234
            if (northPanel == null) {
235
                    northPanel = new JPanel();
236
                    northPanel.setBorder(BorderFactory.createTitledBorder(PluginServices.getText(this, "Select_layer")));
237
                    
238
                    northPanel.setPreferredSize(new Dimension(500, 65));
239
                    northPanel.add(getLayersPanel());
240
                    northPanel.add(getIsEditableLabel());
241
            }
242

    
243
            return northPanel;
244
     }
245

    
246
    /**
247
         * <p>This method initializes isEditableLabel.</p>        
248
         *         
249
         * @return javax.swing.JLabel
250
     */
251
    private JLabel getIsEditableLabel() {
252
            if (isEditableLabel == null) {
253
                    isEditableLabel = new JLabel(PluginServices.getText(this, "Writable"));
254
                    isEditableLabel.setToolTipText(PluginServices.getText(this, "is_editable_TOOLTIP_HTML_explanation"));
255
                    isEditableLabel.setPreferredSize(new Dimension(100, 28));
256
                    iconYes = PluginServices.getIconTheme().get("button-ok-icon");
257
                    iconNo = PluginServices.getIconTheme().get("button-cancel-icon");
258
            }
259

    
260
            return isEditableLabel;
261
    }
262

    
263
    /**
264
     * <p>Updates the lists with the geometric properties of <code>layer</code>.</p>
265
     * 
266
     * @param layer a vector layer
267
     */
268
    private void updateEditableLabel(FLyrVect layer) {
269
            if (layer.isWritable()) {
270
                    // Only can save the changes if layer is writable and its driver is of a PostGree database,
271
                    //  shape, or GML
272
                    VectorialDriver vd = layer.getSource().getDriver();
273
                    
274
                    if (vd == null)
275
                            return;
276
                    
277
                    if ( vd.getName().equalsIgnoreCase("gvSIG shp driver")
278
                                    || vd.getName().equalsIgnoreCase("PostGIS JDBC Driver")
279
                                    || vd.getName().equalsIgnoreCase("gvSIG GML Memory Driver")) {
280
                            isEditableLabel.setIcon(iconYes);
281
                                refreshFields(layer);
282
                    }
283
                    else {
284
                            isEditableLabel.setIcon(iconYes);
285
                            setEnabledFieldAttributeComponents(false);
286

    
287
                            DefaultListModel attrsListModel = (DefaultListModel) getAttributesList().getModel();
288
                            attrsListModel.removeAllElements();
289

    
290
                            DefaultListModel attrsAddedListModel = (DefaultListModel) getAttributesAddedList().getModel();
291
                            attrsAddedListModel.removeAllElements();
292

    
293
                            JOptionPane.showMessageDialog(this, PluginServices.getText(this, "gvSIG_cant_save_changes_in_this_kind_of_format"), PluginServices.getText(this, "Information"), JOptionPane.INFORMATION_MESSAGE);
294
                    }
295
            }
296
            else {
297
                    isEditableLabel.setIcon(iconNo);
298
                        setEnabledFieldAttributeComponents(false);
299

    
300
                        DefaultListModel attrsListModel = (DefaultListModel) getAttributesList().getModel();
301
                        attrsListModel.removeAllElements();
302

    
303
                        DefaultListModel attrsAddedListModel = (DefaultListModel) getAttributesAddedList().getModel();
304
                        attrsAddedListModel.removeAllElements();
305
            }
306
    }
307

    
308
    /**
309
         * <p>This method initializes southPanel.</p>        
310
         *         
311
         * @return javax.swing.JPanel
312
     */
313
    private JPanel getSouthPanel() {
314
            if (southPanel == null) {
315
                    southPanel = new JPanel();
316
                    southPanel.setBorder(BorderFactory.createTitledBorder(PluginServices.getText(this, "select_geometric_info")));
317

    
318
                    southPanel.setPreferredSize(new Dimension(500, 225));
319
                    southPanel.add(getSouthWestScrollPane());//getFieldNameLabel());
320
                    southPanel.add(getSouthCenterPanel());
321
                    southPanel.add(getSouthEastPanel());
322
            }
323

    
324
            return southPanel;
325
    }
326

    
327
    /**
328
         * <p>This method initializes southWestScrollPane.</p>        
329
         *         
330
         * @return javax.swing.JScrollPane
331
     */
332
    private JScrollPane getSouthWestScrollPane() {
333
            if (southWestScrollPane == null) {
334
                    southWestScrollPane = new JScrollPane(getAttributesList());
335
                     southWestScrollPane.setPreferredSize(new Dimension(214, 190));
336
            }
337
            
338
            return southWestScrollPane;
339
    }
340

    
341
    /**
342
         * <p>This method initializes attributesList.</p>        
343
         *         
344
         * @return javax.swing.JList
345
     */
346
    private JList getAttributesList() {
347
            if (attributesList == null) {
348
                    attributesList = new JList();
349
                    attributesList.setToolTipText(PluginServices.getText(this, "select_geometric_properties_to_add_TOOLTIP_HTML_explanation"));
350
                    attributesList.setModel(new DefaultListModel());
351
                    attributesList.setCellRenderer(new JLabelCellRenderer());
352
                    attributesList.setAlignmentX(JList.LEFT_ALIGNMENT);
353
                    attributesList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
354
                    attributesList.addMouseListener(new MouseAdapter() {
355
                            /*
356
                             * (non-Javadoc)
357
                             * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
358
                             */
359
                                public void mouseClicked(MouseEvent e) {
360
                                        if (e.getClickCount() == 2) {
361
                                                rightIconButton.doClick();
362

    
363
                                                return;
364
                                        }
365
                                }
366
                    });
367
            }
368
            
369
            return attributesList;
370
    }
371

    
372
    /**
373
         * <p>This method initializes southCenterPanel.</p>        
374
         *         
375
         * @return javax.swing.JPanel
376
     */
377
    private JPanel getSouthCenterPanel() {
378
            if (southCenterPanel == null) {
379
                    southCenterPanel = new JPanel();
380
                    southCenterPanel.setLayout(new SpringLayout());
381

    
382
                    rightIcon = PluginServices.getIconTheme().get("right-arrow-icon");
383
                     leftIcon = PluginServices.getIconTheme().get("left-arrow-icon");
384
                     
385
                     rightIconButton = new JButton(rightIcon);
386
                     rightIconButton.setToolTipText(PluginServices.getText(this, "add_selected_button_TOOLTIP_HTML_explanation"));
387
                     rightIconButton.setPreferredSize(new Dimension(40, 32));
388
                     rightIconButton.addActionListener(new ActionListener() {
389
                             /*
390
                              * (non-Javadoc)
391
                              * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
392
                              */
393
                                public void actionPerformed(ActionEvent e) {
394
                                        Object[] values = getAttributesList().getSelectedValues();
395

    
396
                                        DefaultListModel listModel = (DefaultListModel)getAttributesList().getModel();
397
                                        int firstIndex = -1;
398

    
399
                                        if (values.length > 0) {
400
                                                firstIndex = listModel.indexOf(values[0]);
401

    
402
                                                for (int i = 0; i < values.length; i++) {
403
                                                        ((DefaultListModel)getAttributesAddedList().getModel()).addElement(values[i]);
404
                                                        listModel.removeElement(values[i]);
405
                                                }
406
                                        }
407
                                        
408
                                        // Select another to improve the usability
409
                                        if (listModel.size() > 0) {
410
                                                if (firstIndex >= listModel.size())
411
                                                        getAttributesList().setSelectedIndex(listModel.size() -1);
412
                                                else {
413
                                                        getAttributesList().setSelectedIndex(firstIndex);
414
                                                }
415
                                        }
416
                                }
417
                     });
418
                     
419
                     leftIconButton = new JButton(leftIcon);
420
                     leftIconButton.setToolTipText(PluginServices.getText(this, "remove_selected_button_TOOLTIP_HTML_explanation"));
421
                     leftIconButton.setPreferredSize(new Dimension(40, 32));
422
                     leftIconButton.addActionListener(new ActionListener() {
423
                             /*
424
                              * (non-Javadoc)
425
                              * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
426
                              */
427
                                public void actionPerformed(ActionEvent e) {
428
                                        Object[] values = getAttributesAddedList().getSelectedValues();
429
                                        
430
                                        DefaultListModel listModel = (DefaultListModel)getAttributesAddedList().getModel();
431
                                        int firstIndex = -1;
432

    
433
                                        if (values.length > 0) {
434
                                                firstIndex = listModel.indexOf(values[0]);
435

    
436
                                                for (int i = 0; i < values.length; i++) {
437
                                                        ((DefaultListModel)getAttributesList().getModel()).addElement(values[i]);
438
                                                        listModel.removeElement(values[i]);
439
                                                }
440
                                        }
441
                                        
442
                                        // Select another to improve the usability
443
                                        if (listModel.size() > 0) {
444
                                                if (firstIndex >= listModel.size())
445
                                                        getAttributesAddedList().setSelectedIndex(listModel.size() -1);
446
                                                else {
447
                                                        getAttributesAddedList().setSelectedIndex(firstIndex);
448
                                                }
449
                                        }
450
                                        else {
451
                                                setEnabledFieldAttributeComponents(false);
452
                                        }
453
                                }
454
                     });
455

    
456
                     doubleRightIcon = PluginServices.getIconTheme().get("double-right-arrow-icon");
457
                     doubleLeftIcon = PluginServices.getIconTheme().get("double-left-arrow-icon");
458
                     doubleRightIconButton = new JButton(doubleRightIcon);
459
                     doubleRightIconButton.setToolTipText(PluginServices.getText(this, "add_all_button_TOOLTIP_HTML_explanation"));
460
                     doubleRightIconButton.setPreferredSize(new Dimension(40, 32));
461
                     doubleRightIconButton.addActionListener(new ActionListener() {
462
                             /*
463
                              * (non-Javadoc)
464
                              * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
465
                              */
466
                                public void actionPerformed(ActionEvent e) {
467
                                        Object[] values = ((DefaultListModel)getAttributesList().getModel()).toArray();
468

    
469
                                        if (values.length > 0) {
470
                                                for (int i = 0; i < values.length; i++) {
471
                                                        ((DefaultListModel)getAttributesAddedList().getModel()).addElement(values[i]);
472
                                                        ((DefaultListModel)getAttributesList().getModel()).removeElement(values[i]);
473
                                                }
474
                                        }
475
                                }
476
                     });
477

    
478
                     doubleLeftIconButton = new JButton(doubleLeftIcon);
479
                     doubleLeftIconButton.setToolTipText(PluginServices.getText(this, "remove_all_button_TOOLTIP_HTML_explanation"));
480
                     doubleLeftIconButton.setPreferredSize(new Dimension(40, 32));
481
                     doubleLeftIconButton.addActionListener(new ActionListener() {
482
                             /*
483
                              * (non-Javadoc)
484
                              * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
485
                              */
486
                                public void actionPerformed(ActionEvent e) {
487
                                        Object[] values = ((DefaultListModel)getAttributesAddedList().getModel()).toArray();
488

    
489
                                        if (values.length > 0) {
490
                                                for (int i = 0; i < values.length; i++) {
491
                                                        ((DefaultListModel)getAttributesList().getModel()).addElement(values[i]);
492
                                                        ((DefaultListModel)getAttributesAddedList().getModel()).removeElement(values[i]);
493
                                                }
494
                                        }
495
                                        
496
                                        setEnabledFieldAttributeComponents(false);
497
                                }
498
                     });
499
 
500
                     southCenterPanel.add(doubleRightIconButton);
501
                     southCenterPanel.add(rightIconButton);
502
                     southCenterPanel.add(leftIconButton);
503
                     southCenterPanel.add(doubleLeftIconButton);
504
                     southCenterPanel.add(getReloadIconButton());
505
                     doSpringLayoutOfSouthCenterPanel();
506
                    southCenterPanel.setPreferredSize(new Dimension(40, 190));
507
            }
508

    
509
            return southCenterPanel;
510
    }
511

    
512
    /**
513
         * <p>This method initializes reloadIconButton.</p>        
514
         *         
515
         * @return javax.swing.JButton
516
     */  
517
    private JButton getReloadIconButton() {
518
            if (reloadIconButton == null) {
519
                    reloadIcon = PluginServices.getIconTheme().get("reload-icon");
520
                     
521
                     reloadIconButton = new JButton(reloadIcon);
522
                     reloadIconButton.setToolTipText(PluginServices.getText(this, "reload_button_TOOLTIP_HTML_explanation"));
523
                     reloadIconButton.setPreferredSize(new Dimension(40, 52));
524
                     reloadIconButton.addActionListener(new ActionListener() {
525
                             /*
526
                              * (non-Javadoc)
527
                              * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
528
                              */
529
                                public void actionPerformed(ActionEvent e) {
530
                                        refreshVisibleVectorLayers();
531

    
532
                                        if (getLayersComboBox().getItemCount() > 0) {
533
                                                FLayerWrapper fW = (FLayerWrapper)getLayersComboBox().getItemAt(0);
534
                                                ((DefaultListModel)getAttributesList().getModel()).removeAllElements();
535
                                                ((DefaultListModel)getAttributesAddedList().getModel()).removeAllElements();
536

    
537
                                                if (fW != null) {
538
                                                        updateEditableLabel(fW.getLayer());
539
                                                }
540
                                        }
541

    
542
                                        getNewColumnCheckBox().setSelected(true);
543
                                        currentColumnNameComponent = getNameOfColumnText();
544
                                        getNameOfColumnText().setText("");
545
                                        getNameOfColumnText().setEnabled(false);
546
                                        getSouthEastPanel().add(currentColumnNameComponent);
547
                                        setEnabledFieldAttributeComponents(false);
548
                                        getSouthEastPanel().updateUI();
549
                                }
550
                     });
551
            }
552
            
553
            return reloadIconButton;
554
    }
555

    
556
    /**
557
     * <p>Sets the GUI components to <code>b</code>.</p>
558
     * 
559
     * @param b <code>true</code> to enable the GUI components, <code>false</code> to disable them
560
     */
561
    private void setEnabledFieldAttributeComponents(boolean b) {
562
            getNewColumnCheckBox().setEnabled(b);
563
            getSaveGeomPropertyButton().setEnabled(b);
564
            
565
            if (b == false) {
566
                    getSouthEastPanel().remove(currentColumnNameComponent);
567
                    getNameOfColumnText().setText("");
568
                    currentColumnNameComponent = getNameOfColumnText();
569
                    getSouthEastPanel().add(currentColumnNameComponent);
570
                    currentColumnNameComponent.setEnabled(b);
571
                    getSouthEastPanel().updateUI();
572
                        getAdaptedAcceptCancelPanel().setOkButtonEnabled(false);
573
            }
574
    }
575

    
576
        /**
577
         * <p>Creates the <code>Spring</code> layout of the south center panel.</p>
578
         */
579
        private void doSpringLayoutOfSouthCenterPanel() {
580
                Component[] components = getSouthCenterPanel().getComponents();
581
        SpringLayout layout = (SpringLayout)getSouthCenterPanel().getLayout();
582
        Spring yPad = Spring.constant(2);
583
        Spring xSpring = Spring.constant(0);
584
        Spring ySpring = yPad;
585

    
586
        // Make every component 5 pixels away from the component to its down.
587
        for (int i = 0; i < components.length; i++) {
588
            SpringLayout.Constraints cons = layout.getConstraints(components[i]);
589
            cons.setY(ySpring);
590
            ySpring = Spring.sum(yPad, cons.getConstraint("South"));
591

    
592
            cons.setX(xSpring);
593
        }
594
        }
595

    
596
        /**
597
         * <p>This method initializes southEastPanel.</p>        
598
         *         
599
         * @return javax.swing.JPanel
600
         */
601
    private JPanel getSouthEastPanel() {
602
            if (southEastPanel == null) {
603
                    southEastPanel = new JPanel();
604
                    southEastPanel.setPreferredSize(new Dimension(215, 190));
605
                    southEastPanel.add(getAddedAttributesScrollPane());
606
                    southEastPanel.add(getSouthEastCenterPanel());
607
                    southEastPanel.add(getNameOfColumnText());
608
                    currentColumnNameComponent = getNameOfColumnText();
609
            }
610

    
611
            return southEastPanel;
612
    }
613
    
614
        /**
615
         * <p>This method initializes southEastCenterPanel.</p>        
616
         *         
617
         * @return javax.swing.JPanel
618
         */
619
    private JPanel getSouthEastCenterPanel() {
620
            if (southEastCenterPanel == null) {
621
                    southEastCenterPanel = new JPanel();
622
                    southEastCenterPanel.setPreferredSize(new Dimension(215, 26));
623
                    
624
                    southEastCenterPanel.add(getNewColumnCheckBox());
625
                     southEastCenterPanel.add(getSaveGeomPropertyButton());
626
            }
627
            
628
            return southEastCenterPanel;
629
    }
630
    
631
        /**
632
         * <p>This method initializes saveGeomPropertyButton.</p>        
633
         *         
634
         * @return javax.swing.JButton
635
         */
636
    private JButton getSaveGeomPropertyButton() {
637
            if (saveGeomPropertyButton == null) {
638
                    saveGPIcon = PluginServices.getIconTheme().get("save-icon");
639

    
640
                    saveGeomPropertyButton = new JButton(saveGPIcon);
641
                    saveGeomPropertyButton.setPreferredSize(new Dimension(20, 20));
642
                    saveGeomPropertyButton.setToolTipText(PluginServices.getText(this, "save_changes_TOOLTIP_HTML_explanation"));
643
                    saveGeomPropertyButton.addActionListener(new ActionListener() {
644
                             /*
645
                              * (non-Javadoc)
646
                              * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
647
                              */
648
                                public void actionPerformed(ActionEvent e) {
649
                                        updateAttributesToCurrentField();
650
                                }
651
                     });
652
            }
653
            
654
            return saveGeomPropertyButton;
655
    }
656
    
657
        /**
658
         * <p>This method initializes addedAttributesScrollPane.</p>        
659
         *         
660
         * @return javax.swing.JScrollPane
661
         */
662
    private JScrollPane getAddedAttributesScrollPane() {
663
            if (addedAttributesScrollPane == null) {
664
                    addedAttributesScrollPane = new JScrollPane(getAttributesAddedList());
665
                    addedAttributesScrollPane.setPreferredSize(new Dimension(214, 126));
666
            }
667
            
668
            return addedAttributesScrollPane;
669
    }
670
    
671
        /**
672
         * <p>This method initializes attributesAddedList.</p>        
673
         *         
674
         * @return javax.swing.JList
675
         */
676
    private JList getAttributesAddedList() {
677
            if (attributesAddedList == null) {
678
                    attributesAddedList = new JList();
679
                    attributesAddedList.setToolTipText(PluginServices.getText(this, "select_how_add_each_geometric_property_TOOLTIP_HTML_explanation"));
680
                    attributesAddedList.setModel(new DefaultListModel());
681
                    attributesAddedList.setCellRenderer(new JLabelCellRenderer());
682
                    attributesAddedList.setAlignmentX(JList.LEFT_ALIGNMENT);
683
                    DefaultListModel listModel = ((DefaultListModel)attributesAddedList.getModel());
684
                    final DefaultListModel f_listModel = listModel;
685
                    
686
                    listModel.addListDataListener(new ListDataListener() {
687
                            /*
688
                             * (non-Javadoc)
689
                             * @see javax.swing.event.ListDataListener#contentsChanged(javax.swing.event.ListDataEvent)
690
                             */
691
                                public void contentsChanged(ListDataEvent e) {
692
                                }
693

    
694
                                /*
695
                                 * (non-Javadoc)
696
                                 * @see javax.swing.event.ListDataListener#intervalAdded(javax.swing.event.ListDataEvent)
697
                                 */
698
                                public void intervalAdded(ListDataEvent e) {
699
                                        if (f_listModel.size() > 0)
700
                                                getAdaptedAcceptCancelPanel().setOkButtonEnabled(true);
701
                                }
702

    
703
                                /*
704
                                 * (non-Javadoc)
705
                                 * @see javax.swing.event.ListDataListener#intervalRemoved(javax.swing.event.ListDataEvent)
706
                                 */
707
                                public void intervalRemoved(ListDataEvent e) {
708
                                        if (f_listModel.size() == 0)
709
                                                getAdaptedAcceptCancelPanel().setOkButtonEnabled(false);
710
                                }
711
                    });
712
                    
713
                    attributesAddedList.addListSelectionListener(new ListSelectionListener() {
714
                            /*
715
                             * (non-Javadoc)
716
                             * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
717
                             */
718
                                public void valueChanged(ListSelectionEvent e) {
719
                                        if (! e.getValueIsAdjusting()) {
720
                                                Object[] values = attributesAddedList.getSelectedValues();
721
                                                if (values.length == 1) {
722
                                                        currentField = ((GeomInfo)values[0]);
723
                                                    setEnabledFieldAttributeComponents(true);
724
                                                    updateAttributesFromCurrentField();
725
                                                }                
726
                                        }
727
                                }
728
                    });
729

    
730
                    attributesAddedList.addMouseListener(new MouseAdapter() {
731
                            /*
732
                             * (non-Javadoc)
733
                             * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
734
                             */
735
                                public void mouseClicked(MouseEvent e) {
736
                                        if (e.getClickCount() == 2) {
737
                                                leftIconButton.doClick();
738

    
739
                                                return;
740
                                        }
741
                                }
742
                    });
743
            }
744
            
745
            return attributesAddedList;
746
    }
747
    
748
        /**
749
         * <p>This method initializes newColumnCheckBox.</p>        
750
         *         
751
         * @return javax.swing.JCheckBox
752
         */
753
        private JCheckBox getNewColumnCheckBox() {
754
            if (newColumnCheckBox == null) {
755
                    newColumnCheckBox = new JCheckBox(PluginServices.getText(this, "New_field"));
756
                    newColumnCheckBox.setToolTipText(PluginServices.getText(this, "new_property_checkbox_TOOLTIP_HTML_explanation"));
757
                    newColumnCheckBox.setPreferredSize(new Dimension(175, 22));
758
                    newColumnCheckBox.setEnabled(false);
759
                    newColumnCheckBox.setSelected(true);
760
                    newColumnCheckBox.addItemListener(new ItemListener() {
761
                                public void itemStateChanged(ItemEvent e) {
762
                                        updateColumnNameFromField();
763
                                }
764
                    });
765
            }
766

    
767
            return newColumnCheckBox;                
768
        }
769
    
770
        /**
771
         * <p>Updates the component that has the name of the field selected.</p>        
772
         */
773
        private void updateColumnNameFromField() {
774
                getSouthEastPanel().remove(currentColumnNameComponent);
775
                
776
                // Adds all the field names of the layer
777
                if (getNewColumnCheckBox().isSelected()) {
778
                        currentColumnNameComponent = getNameOfColumnText();
779
                        currentColumnNameComponent.setEnabled(true);
780

    
781
                        if (currentField == null) {
782
                                getNameOfColumnText().setText("");
783
                        }
784
                        else {
785
                                getNameOfColumnText().setText(currentField.getName());
786
                        }
787
                }
788
                else {
789
                        currentColumnNameComponent = getNameOfColumnCombo();
790

    
791
                    try {
792
                                DefaultComboBoxConfigurableLookUpModel model = ((DefaultComboBoxConfigurableLookUpModel)((JComboBoxConfigurableLookUp)getNameOfColumnCombo()).getModel());
793
                                model.removeAllElements();
794

    
795
                            FLyrVect layer = ((FLyrVect)((FLayerWrapper)getLayersComboBox().getSelectedItem()).getLayer());
796

    
797
                            SelectableDataSource sds = layer.getRecordset();
798

    
799
                            for (int i = 0; i < sds.getFieldCount(); i ++) {
800
                                    model.addElement(sds.getFieldName(i));
801
                            }
802
                            
803
                            if (model.getSize() > 0) {
804
                                    if ((currentField.getName() == null) || (((DefaultComboBoxConfigurableLookUpModel)getNameOfColumnCombo().getModel()).getIndexOf(currentField.getName()) == -1))
805
                                            getNameOfColumnCombo().setSelectedItem(model.getElementAt(0));
806
                                    else {
807
                                            getNameOfColumnCombo().setSelectedItem(currentField.getName());
808
                                    }
809
                            }
810
                    }
811
                    catch (Exception ex) {
812
                            JOptionPane.showMessageDialog(null, PluginServices.getText(null, "Failed_loading_fields"), PluginServices.getText(null, "Error"), JOptionPane.ERROR_MESSAGE);
813
                    }
814
                }
815

    
816
                getSouthEastPanel().add(currentColumnNameComponent);
817
                getSouthEastPanel().updateUI();
818
        }
819
    
820
        /**
821
         * <p>Updates the component that has the name of the field selected.</p>        
822
         */
823
        private void updateAttributesToCurrentField() {
824
                if (currentField != null) {
825
                        currentField.setNewColumn(getNewColumnCheckBox().isSelected());
826

    
827
                        if (currentColumnNameComponent instanceof JTextFieldWithSCP) {
828
                                String userName = applyReplaces(getNameOfColumnText().getText());
829
                                String newName = getNewFieldName(fieldNames, userName);
830
                                if (!newName.equals(userName)) {
831
                                        JOptionPane.showMessageDialog(this,
832
                                                        PluginServices.getText(this, "Field_already_exists__")+userName+"\n"
833
                                                        + PluginServices.getText(this, "New_field_name_proposed__"+newName+"\n")
834
                                                        + PluginServices.getText(this, "Uncheck_New_Field_Option_if_you_want_to_update_an_existing_field"),
835
                                                        PluginServices.getText(this, "Warning_Field_exists"),
836
                                                        JOptionPane.WARNING_MESSAGE);
837
                                        getNameOfColumnText().setText(newName);
838
                                }
839
                                currentField.setName(newName);
840
                        }
841
                        else {
842
                                currentField.setName(applyReplaces(((String) getNameOfColumnCombo().getSelectedItem())));
843
                        }
844
                }
845
        }
846

    
847
        /**
848
         * <p>Applies the different kinds of replacement defined in
849
         *  {@link StringUtilitiesExtension StringUtilitiesExtension} to <code>s</code>.</p>
850
         * 
851
         * @param s text to be formatted
852
         * @return t text formatted
853
         */
854
        private String applyReplaces(String s) {
855
                return StringUtilitiesExtended.replaceAllAccents(
856
                                StringUtilitiesExtended.replaceAllCedilla(
857
                                        StringUtilitiesExtended.replaceAllNWithTilde(s)));
858
        }
859
        
860
        /**
861
         * <p>Updates the current field properties with the configuration in the GUI.</p>        
862
         */
863
        private void updateAttributesFromCurrentField() {
864
                if (currentField != null) {
865
                        getNewColumnCheckBox().setSelected(currentField.isNewColumn());
866
                        updateColumnNameFromField();
867
                }
868
        }
869
    
870
        /**
871
         * <p>This method initializes nameOfColumnText.</p>        
872
         *         
873
         * @return JTextFieldWithSCP
874
         */
875
        private JTextFieldWithSCP getNameOfColumnText() {
876
                if (nameOfColumnText == null) {
877
                        nameOfColumnText = new JTextFieldWithSCP();
878
                        nameOfColumnText.setPreferredSize(new Dimension(215, 22));
879
                        nameOfColumnText.setToolTipText(PluginServices.getText(this, "name_of_the_new_property_TOOLTIP_HTML_explanation"));
880
            }
881

    
882
            return nameOfColumnText;
883
        }
884
    
885
        /**
886
         * <p>This method initializes nameOfColumnCombo.</p>        
887
         *         
888
         * @return JComboBoxConfigurableLookUp
889
         */
890
        private JComboBoxConfigurableLookUp getNameOfColumnCombo() {
891
                if (nameOfColumnCombo == null) {
892
                        nameOfColumnCombo = new JComboBoxConfigurableLookUp();
893
                        DefaultComboBoxConfigurableLookUpModel model = ((DefaultComboBoxConfigurableLookUpModel)((JComboBoxConfigurableLookUp)nameOfColumnCombo).getModel());
894
                        model.setShowAllItemsInListBox(true);
895
                        nameOfColumnCombo.setPreferredSize(new Dimension(215, 22));
896
                        nameOfColumnCombo.setToolTipText(PluginServices.getText(this, "name_of_the_new_property_TOOLTIP_HTML_explanation"));
897
                }
898
                
899
                return nameOfColumnCombo;
900
        }
901

    
902
    /**
903
         * <p>This method initializes layersPanel.</p>        
904
         *         
905
         * @return javax.swing.JPanel
906
     */
907
    private JPanel getLayersPanel() {
908
            if (layersPanel == null) {
909
                    layersPanel = new JPanel();
910
                    layersPanel.setLayout(new FlowLayout());
911
                    layersPanel.setPreferredSize(new Dimension(380, 28));
912
                    layersPanel.add(getLayersLabel());
913
                    layersPanel.add(getLayersComboBox());
914
            }
915
            
916
            return layersPanel;
917
    }
918

    
919
    /**
920
         * <p>This method initializes layersLabel.</p>        
921
         *         
922
         * @return javax.swing.JLabel
923
     */
924
    private JLabel getLayersLabel() {
925
            if (layersLabel == null) {
926
                    layersLabel = new JLabel(PluginServices.getText(this, "Layer"));
927
                    layersLabel.setPreferredSize(new Dimension(40, 20));
928
                    layersLabel.setToolTipText(PluginServices.getText(this, "Layer"));
929
            }
930
            
931
            return layersLabel;
932
    }
933
    
934
    /**
935
         * <p>This method initializes layersComboBox.</p>        
936
         *         
937
         * @return javax.swing.JComboBox
938
     */
939
    private JComboBox getLayersComboBox() {
940
            if (layersComboBox == null) {
941
                    layersComboBox = new JComboBox();
942
                    layersComboBox.setPreferredSize(new Dimension(layersComboBox_Width, layersComboBox_Height));
943
                    layersComboBox.addItemListener(new ItemListener() {
944
                            /*
945
                             * (non-Javadoc)
946
                             * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
947
                             */
948
                                public void itemStateChanged(ItemEvent e) {
949
                                        if (e.getStateChange() == ItemEvent.SELECTED) {
950
                                                try {
951
                                                        FLayerWrapper fW = (FLayerWrapper)e.getItem();
952
                                                        
953
                                                        if (fW != null) {
954
                                                                FLayer layer = fW.getLayer();
955
                                                                
956
                                                                if (layer != null) {
957
                                                                        layersComboBox.setToolTipText("<html>" + PluginServices.getText(this, "Layer") + ": " + layer.getName() + "<br>" + 
958
                                                                                        PluginServices.getText(this, "Type") + ": " + FShapeTypeNames.getFShapeTypeName(((FLyrVect)layer).getShapeType()) + "</html>");
959
                                                                        
960
                                                                        updateEditableLabel((FLyrVect)layer);
961
                                                                }
962
                                                        }
963
                                                }
964
                                                catch(ReadDriverException dE) {
965
                                                        NotificationManager.showMessageError(PluginServices.getText(this, "problem_loading_layers"), dE);
966
                                                }
967
                                        }
968
                                }
969
                    });
970
            }
971
            
972
            return layersComboBox;
973
    }
974
        
975
        /**
976
         * <p>Updates "layersComboBox" with the name of visible layers of the associated <code>MapControl</code> instance.</p>
977
         * 
978
         * @see #refreshVisibleVectorLayers(FLayer, int)
979
         * 
980
         * @return <code>true</code> if there is any layer added that can be written, otherwise <code>false</code>
981
         */
982
        private boolean refreshVisibleVectorLayers() {
983
                FLayer rootNode = mapControl.getMapContext().getLayers();
984

    
985
                getLayersComboBox().removeAllItems();
986
                
987
                boolean b = refreshVisibleVectorLayers(rootNode, -1);
988

    
989
                // The selectedLayer will be, by default, the first being added.
990
                if (getLayersComboBox().getItemCount() > 0) {
991
                        getLayersComboBox().setRenderer(new LayersComboBoxCellRenderer());
992
                        getLayersComboBox().setSelectedIndex(0);
993
                }
994
                
995
                // If there is only one layer -> disable the possibility of selection by the user
996
                if (getLayersComboBox().getItemCount() <= 1) {
997
                        getLayersComboBox().setEnabled(false);
998
                }
999
                
1000
                return b;
1001
        }
1002
        
1003
        /**
1004
         * <p>Updates "layersComboBox" with the name of visible layers down <code>node</code>.</p>
1005
         * 
1006
         * @param node parent node
1007
         * @param level <code>node</code> level from the root
1008
         * 
1009
         * @return <code>true</code> if some layer added can be edited, otherwise <code>false</code>
1010
         */
1011
        private boolean refreshVisibleVectorLayers(FLayer node, int level) {
1012
                if (node instanceof FLayers) {
1013
                        FLayers root = (FLayers)node;
1014
                        boolean b = false;
1015

    
1016
                        for (int i = root.getLayersCount() - 1; i >= 0 ; i--) {
1017
                                if (root.getLayer(i).isVisible()) {
1018
                                        b |= refreshVisibleVectorLayers(root.getLayer(i), level + 1);
1019
                                }
1020
                        }
1021
                        
1022
                        return b;
1023
                }
1024
                else {
1025
                        String layerName = node.getName();
1026

    
1027
                        if ((node.isVisible()) && (node instanceof FLyrVect) && (layerName != null)) {
1028
                                getLayersComboBox().addItem(new FLayerWrapper((FLyrVect)node, level, bIcon, getLayerIcon(node)));
1029
                                
1030
                                return node.isWritable();
1031
                        }
1032
                }
1033
                
1034
                return false;
1035
        }
1036

    
1037
        /**
1038
         * <p>Returns the icon that represents the layer in the current active view's TOC.</p>
1039
         * 
1040
         * @param layer the layer
1041
         * @return the layer's icon in the current active view's TOC
1042
         */
1043
        private Icon getLayerIcon(FLayer layer) {
1044
            if (layer.getTocImageIcon() != null) {
1045
                    return layer.getTocImageIcon();
1046
            }
1047
            else {
1048
                    TocItemBranch branch = new TocItemBranch(layer);
1049

    
1050
                    return branch.getIcon();
1051
            }
1052
        }
1053

    
1054
        /**
1055
         * <p>Updates attrsListModel with the geometric attributes according the shape type of the current selected layer.</p>
1056
         *
1057
         * @throws DriverException if fails working with the data driver
1058
         */
1059
        private void refreshFields(FLyrVect seletedLayer) {
1060
                try {
1061
                        previous_Type = seletedLayer.getShapeType();
1062
                        
1063
                        SelectableDataSource ds =  seletedLayer.getSource().getRecordset();
1064
                        fieldNames = new HashSet();
1065
                        try {
1066
                                String[] fn = ds.getFieldNames();
1067
                                for (int i=0; i<fn.length; i++) {
1068
                                        fieldNames.add(fn[i]);
1069
                                }
1070
                        } catch (ReadDriverException e) {
1071
                                PluginServices.getLogger().error(e);
1072
                        }
1073
                        
1074
                        setEnabledFieldAttributeComponents(false);
1075

    
1076
                        DefaultListModel attrsListModel = (DefaultListModel) getAttributesList().getModel();
1077
                        attrsListModel.removeAllElements();
1078

    
1079
                        DefaultListModel attrsAddedListModel = (DefaultListModel) getAttributesAddedList().getModel();
1080
                        attrsAddedListModel.removeAllElements();
1081
                        
1082
                        GeomInfo geomInfo = null;
1083
                        
1084
                        switch (previous_Type) {
1085
                                case FShape.NULL:
1086
                                        break;
1087
                                case FShape.POINT:
1088
                                        geomInfo = GeomInfoFactory.createGeomInfo(PluginServices.getText(this, "X_coordinate"), getNewFieldName(fieldNames, PluginServices.getText(this, "X")), FShape.POINT);
1089
                                        geomInfo.setGeomSubType(GeomInfo.X);
1090
                                        attrsListModel.addElement(geomInfo);
1091
                                        
1092
                                        geomInfo = GeomInfoFactory.createGeomInfo(PluginServices.getText(this, "Y_coordinate"), getNewFieldName(fieldNames, PluginServices.getText(this, "Y")), FShape.POINT);
1093
                                        geomInfo.setGeomSubType(GeomInfo.Y);
1094
                                        attrsListModel.addElement(geomInfo);
1095
                                        
1096
                                        geomInfo = GeomInfoFactory.createGeomInfo(PluginServices.getText(this, "Z_coordinate"), getNewFieldName(fieldNames, PluginServices.getText(this, "Z")), FShape.POINT);
1097
                                        geomInfo.setGeomSubType(GeomInfo.Z);
1098
                                        attrsListModel.addElement(geomInfo);
1099
                                        break;
1100
                                case FShape.LINE:
1101
                                        attrsListModel.addElement(GeomInfoFactory.createGeomInfo(PluginServices.getText(this, "Length"), getNewFieldName(fieldNames, PluginServices.getText(this, "Length")), FShape.LINE));
1102
                                        break;
1103
                                case FShape.POLYGON:
1104
                                        geomInfo = GeomInfoFactory.createGeomInfo(PluginServices.getText(this, "Area"), getNewFieldName(fieldNames, PluginServices.getText(this, "Area")), FShape.POLYGON);
1105
                                        geomInfo.setGeomSubType(GeomInfo.AREA);
1106
                                        attrsListModel.addElement(geomInfo);
1107
                                        geomInfo = GeomInfoFactory.createGeomInfo(PluginServices.getText(this, "Perimeter"), getNewFieldName(fieldNames, PluginServices.getText(this, "Perimeter")), FShape.POLYGON);
1108
                                        geomInfo.setGeomSubType(GeomInfo.PERIMETER);
1109
                                        attrsListModel.addElement(geomInfo);
1110
                                        break;
1111
                                case FShape.TEXT:
1112
                                        break;
1113
                                case FShape.MULTI: // Can have points, multipoints, lines and polygons
1114
                                        /* POINT */
1115
                                        geomInfo = GeomInfoFactory.createGeomInfo(PluginServices.getText(this, "X_coordinate"), getNewFieldName(fieldNames, PluginServices.getText(this, "X")), FShape.POINT);
1116
                                        geomInfo.setGeomSubType(GeomInfo.X);
1117
                                        attrsListModel.addElement(geomInfo);
1118
                                        
1119
                                        geomInfo = GeomInfoFactory.createGeomInfo(PluginServices.getText(this, "Y_coordinate"), getNewFieldName(fieldNames, PluginServices.getText(this, "Y")), FShape.POINT);
1120
                                        geomInfo.setGeomSubType(GeomInfo.Y);
1121
                                        attrsListModel.addElement(geomInfo);
1122
                                        
1123
                                        geomInfo = GeomInfoFactory.createGeomInfo(PluginServices.getText(this, "Z_coordinate"), getNewFieldName(fieldNames, PluginServices.getText(this, "Z")), FShape.POINT);
1124
                                        geomInfo.setGeomSubType(GeomInfo.Z);
1125
                                        attrsListModel.addElement(geomInfo);
1126

    
1127
                                        /* LINE */
1128
                                        attrsListModel.addElement(GeomInfoFactory.createGeomInfo(PluginServices.getText(this, "Length"), getNewFieldName(fieldNames, PluginServices.getText(this, "Length")), FShape.LINE));
1129
                                        
1130
                                        /* POLYGON */
1131
                                        geomInfo = GeomInfoFactory.createGeomInfo(PluginServices.getText(this, "Area"), getNewFieldName(fieldNames, PluginServices.getText(this, "Area")), FShape.POLYGON);
1132
                                        geomInfo.setGeomSubType(GeomInfo.AREA);
1133
                                        attrsListModel.addElement(geomInfo);
1134

    
1135
                                        geomInfo = GeomInfoFactory.createGeomInfo(PluginServices.getText(this, "Perimeter"), getNewFieldName(fieldNames, PluginServices.getText(this, "Perimeter")), FShape.POLYGON);
1136
                                        geomInfo.setGeomSubType(GeomInfo.PERIMETER);
1137
                                        attrsListModel.addElement(geomInfo);
1138

    
1139
                                        /* MULTIPOINT */
1140
                                        attrsListModel.addElement(GeomInfoFactory.createGeomInfo(PluginServices.getText(this, "Number_of_points"), getNewFieldName(fieldNames, PluginServices.getText(this, "N_points")), FShape.MULTIPOINT));
1141
                                        break;
1142
                                case FShape.MULTIPOINT:
1143
                                        attrsListModel.addElement(GeomInfoFactory.createGeomInfo(PluginServices.getText(this, "Number_of_points"), getNewFieldName(fieldNames, PluginServices.getText(this, "N_points")), FShape.MULTIPOINT));
1144
                                        break;
1145
                                case FShape.CIRCLE:
1146
                                        break;
1147
                                case FShape.ARC:
1148
                                        break;
1149
                                case FShape.ELLIPSE:
1150
                                        break;
1151
                                case FShape.Z:
1152
                                        break;
1153
                                default : // UNDEFINED
1154
                        }
1155

    
1156
                        updateUI();
1157
                }
1158
                catch(ReadDriverException dE) {
1159
                        JOptionPane.showMessageDialog(this, PluginServices.getText(this, "Failed_loading_fields"), PluginServices.getText(this, "Error"), JOptionPane.ERROR_MESSAGE);
1160
                }
1161
        }
1162

    
1163
        private String getNewFieldName(HashSet currentFieldNames, String proposedName) {
1164
                String newName = proposedName;
1165
                int i=2;
1166
                while (currentFieldNames.contains(newName)) {
1167
                        newName = proposedName+i;
1168
                        
1169
                }
1170
                return newName;
1171
        }
1172

    
1173
        /*
1174
     * @see com.iver.andami.ui.mdiManager.View#getViewInfo()
1175
     */
1176
    public WindowInfo getWindowInfo() {
1177
        if (viewInfo == null) {
1178
            viewInfo = new WindowInfo(WindowInfo.MODALDIALOG | WindowInfo.RESIZABLE);
1179
            viewInfo.setTitle(PluginServices.getText(this, "add_geometric_info"));
1180
            viewInfo.setWidth(Window_Width);
1181
            viewInfo.setHeight(Window_Height);
1182
        }
1183
        return viewInfo;
1184
    }
1185

    
1186
    /**
1187
     * <p>Adapts {@link AcceptCancelPanel AcceptCancelPanel} to be used as a component of the <code>AddGeometricInfoPanel</code> panel.</p>
1188
     * 
1189
     * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
1190
     */
1191
    private class AdaptedAcceptCancelPanel extends AcceptCancelPanel {
1192
                private static final long serialVersionUID = -1630782817926954788L;
1193

    
1194
                /**
1195
             * <p>Creates a new <code>AdaptedAcceptCancelPanel</code></p>
1196
             */
1197
                public AdaptedAcceptCancelPanel () {
1198
                    super();
1199

    
1200
                    addOkButtonActionListener(getOKAction());
1201
                    addCancelButtonActionListener(getCancelAction());
1202
                    setPreferredSize(new Dimension(500, 28));
1203
            }
1204
            
1205
            /**
1206
              * <p>Create the action that will be executed when user pressed the <i>ok</i> button.</p>
1207
             * 
1208
             * @return action that will be executed when user pressed the <i>cancel</i> button
1209
             */
1210
            private ActionListener getOKAction() {
1211
                    // OK button action
1212
                    return new ActionListener() {
1213
                            /*
1214
                             * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
1215
                             */
1216
                                public void actionPerformed(ActionEvent e) {
1217
                                        closeThis();
1218
                                        
1219
                                        /* 1- Initial tasks */
1220
                                        /* 1.1- If layer is in edition, saves it and ends the edition */
1221
                                        FLyrVect layer = ((FLayerWrapper)getLayersComboBox().getSelectedItem()).getLayer();
1222
                                        final boolean wasBeingEdited = layer.isEditing();
1223
                                        
1224
                                        if (wasBeingEdited) {
1225
                                                try {
1226
                                                        saveLayer(layer);
1227
                                                }
1228
                                                catch (Exception ex) {
1229
                                                        NotificationManager.showMessageError(PluginServices.getText(this, "Failed_saving_the_layer"), ex);
1230
                                                        PluginServices.getMainFrame().enableControls();
1231
                                                        return;
1232
                                                }
1233
                                        }
1234

    
1235
                                        /* 1.2- Stores the active layers */
1236
                                        final FLayer[] activeLayers = getActiveLayers(view.getTOC());
1237

    
1238
                                        /* 2- Creates the process */
1239
                                        AddGeometricInfoProcess iprocess = new AddGeometricInfoProcess(PluginServices.getText(this, "Add_geometric_information_to_layer_process"), PluginServices.getText(this, "Ongoing_process_please_wait"), view, layer, ((DefaultListModel)getAttributesAddedList().getModel()).toArray());
1240

    
1241
                                        IncrementableTask iTask = new IncrementableTask(iprocess, new ProgressPanel(false));
1242
                                        iTask.addIncrementableListener(iprocess);
1243
                                        iprocess.setIncrementableTask(iTask);
1244
                                        final AddGeometricInfoProcess f_iprocess = iprocess;
1245
                                        final IncrementableTask f_iTask = iTask;
1246

    
1247
                                        iTask.getProgressPanel().addComponentListener(new ComponentAdapter() {
1248
                                                /*
1249
                                                 * (non-Javadoc)
1250
                                                 * @see java.awt.event.ComponentAdapter#componentHidden(java.awt.event.ComponentEvent)
1251
                                                 */
1252
                                                public void componentHidden(ComponentEvent e) {
1253
                                                        PluginServices.getMainFrame().enableControls();
1254
                                                        f_iTask.getProgressPanel().dispose();
1255

    
1256
                                                        /* 7- Refreshes all the associated project tables */
1257
                                                        ProjectTable pt = f_iprocess.getLayerProjectTable();
1258
                                                        if (pt != null) {
1259
                                                                   com.iver.andami.ui.mdiManager.IWindow[] views = PluginServices.getMDIManager().getAllWindows();
1260
                                
1261
                                                                for (int i = 0 ; i<views.length ; i++) {
1262
                                                                        if (views[i] instanceof Table) {
1263
                                                                                Table table = (Table)views[i];
1264
                                                                                ProjectTable model = table.getModel();
1265
                                                                                if (model.equals(pt)) {
1266
                                                                                        table.setModel(pt);
1267
                                                                                        table.setVisible(false);
1268

    
1269
                                                                                        if ((wasBeingEdited) && (f_iprocess.getVea() != null))
1270
                                                                                                f_iprocess.getVea().getCommandRecord().addCommandListener(table);
1271
                                                                                        
1272
                                                                                        table.setVisible(true);
1273
                                                                                }
1274
                                                                        }
1275
                                                                }
1276
                                                        }
1277

    
1278
                                                        /* 8- Restores the active layers */
1279
                                                        restoreActiveLayers(view.getTOC(), activeLayers);
1280

    
1281
                                                        /* 9- Writes in the gvSIG log the results of the process */
1282
                                                        String text = "\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n" +
1283
                                                                PluginServices.getText(this, "Summary_of_the_process_of_adding_geometric_information") + ":\n" +
1284
                                                                f_iprocess.getLog() +
1285
                                                                "\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n";
1286
                                                        Logger.getLogger(getClass().getName()).debug(text);
1287
                                                        
1288
                                                        /* 10- If the process has been canceled -> refreshes the view */
1289
                                                        if (f_iprocess.getPercent() < 100)
1290
                                                                mapControl.drawMap(false);
1291
                                                        
1292
                                                        /* 11- Refreshes the toolbars and their controls */
1293
                                                        PluginServices.getMainFrame().enableControls();
1294
                                                }
1295
                                        });
1296

    
1297
                                        /* Starts the process */
1298
                                        iprocess.start();
1299
                                        iTask.start();
1300
                                }
1301
                    };
1302
            }
1303

    
1304
            /**
1305
             * <p>Saves and stops the edition of a vector layer.</p>
1306
             * 
1307
             * @param layer the vector layer to save
1308
             * 
1309
             * @throws DriverException if fails the driver associated to the layer
1310
             * @throws InitializeWriterException if fails starting the writing of the layer
1311
             * @throws InitializeDriverException if fails starting the edition process with the layer driver
1312
             * @throws StopWriterVisitorException if fails stopping the edition process with the layer driver
1313
             */
1314
            private void saveLayer(FLyrVect layer) throws ReadDriverException, InitializeWriterException, InitializeDriverException, StopWriterVisitorException {
1315
                    layer.setProperty("stoppingEditing", new Boolean(true));
1316
                    VectorialEditableAdapter vea = (VectorialEditableAdapter) layer.getSource();
1317
                    
1318
                    ISpatialWriter writer = (ISpatialWriter) vea.getWriter();
1319
                    com.iver.andami.ui.mdiManager.IWindow[] views = PluginServices.getMDIManager().getAllWindows();
1320
                    for (int j = 0; j < views.length; j++) {
1321
                            if (views[j] instanceof Table) {
1322
                                    Table table = (Table) views[j];
1323
                                    if (table.getModel().getAssociatedTable() != null
1324
                                                    && table.getModel().getAssociatedTable().equals(layer)) {
1325
                                            table.stopEditingCell();
1326
                                    }
1327
                            }
1328
                    }
1329
                    vea.cleanSelectableDatasource();
1330
                    layer.setRecordset(vea.getRecordset());
1331

    
1332
                    // Queremos que el recordset del layer
1333
                    // refleje los cambios en los campos.
1334
                    ILayerDefinition lyrDef = EditionUtilities.createLayerDefinition(layer);
1335
                    String aux = "FIELDS:";
1336
                    FieldDescription[] flds = lyrDef.getFieldsDesc();
1337
                    for (int i=0; i < flds.length; i++)        {
1338
                            aux = aux + ", " + flds[i].getFieldAlias();
1339
                    }
1340

    
1341
                    System.err.println("Escribiendo la capa " + lyrDef.getName() + " con los campos " + aux);
1342
                    lyrDef.setShapeType(layer.getShapeType());
1343
                    writer.initialize(lyrDef);
1344
                    vea.stopEdition(writer, EditionEvent.GRAPHIC);
1345
                    layer.setProperty("stoppingEditing", new Boolean(false));
1346
            }
1347

    
1348
            
1349
            /**
1350
             * <p>Create the action that will be executed when user pressed the <i>cancel</i> button.</p>
1351
             * 
1352
             * @return action that will be executed when user pressed the <i>cancel</i> button
1353
             */
1354
            private ActionListener getCancelAction() {
1355
                    // Cancel button action
1356
                    return new ActionListener() {
1357
                            /*
1358
                             * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
1359
                             */
1360
                                public void actionPerformed(ActionEvent e) {
1361
                                        closeThis();
1362
                                }
1363
                    };
1364
            }
1365
    }
1366

    
1367
    /**
1368
     * <p>Wrappers a <code>FLayer</code> overwriting the method <code>toString()</code> in order to would
1369
     *  return the name of the layer.</code>
1370
     * 
1371
     * <p>Also displays icons and label</p>
1372
     * 
1373
     * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
1374
     */
1375
    private class FLayerWrapper extends JPanel {
1376
                private static final long serialVersionUID = -160586150559061104L;
1377

    
1378
                private FLyrVect layer;
1379
            private int level;
1380
            ImageIcon branchIcon;
1381

    
1382
            /**
1383
             * <p>Creates a new <code>FLayerWrapper</code>.</p>
1384
             * 
1385
             * @param layer the vector layer to be wrappered
1386
             * @param level the level in the tree of the layer wrappered
1387
             * @param branchIcon icon that represents the branch
1388
             * @param leafIcon icon that represents the leaf
1389
             */
1390
            public FLayerWrapper(FLyrVect layer, int level, ImageIcon branchIcon, Icon leafIcon) {
1391
                    super();
1392

    
1393
                    this.layer = layer;
1394
                    this.level = level;
1395
                    this.branchIcon = branchIcon;
1396

    
1397
                    setLayout(new SpringLayout());
1398
                    
1399
                    if ((level > 0) && (branchIcon != null)) {
1400
                            for (int i = 0; i < level; i++) {
1401
                                    add(new JLabel(branchIcon));
1402
                            }
1403
                    }
1404

    
1405
                    JLabel layerLabel;
1406
                    
1407
                    if (layer.getName() != null)
1408
                            layerLabel = new JLabel(layer.getName());
1409
                    else
1410
                            layerLabel = new JLabel("");
1411
                    
1412
                    if (leafIcon != null)
1413
                            layerLabel.setIcon(leafIcon);
1414

    
1415
                    layerLabel.setFont(new Font("Helvetica", Font.BOLD, 12));
1416
                    
1417
                    add(layerLabel);
1418
                    
1419
                    if (layer instanceof FLyrVect) {
1420
                        JLabel layerTypeLabel; 
1421
                            try {
1422
                                    layerTypeLabel = new JLabel(PluginServices.getText(null, "Type") + ": " + FShapeTypeNames.getFShapeTypeName(((FLyrVect) layer).getShapeType()));
1423
                                } catch (ReadDriverException e) {
1424
                                        layerTypeLabel = new JLabel(PluginServices.getText(null, "Type") + ": " + PluginServices.getText("", "UNKNOWN"));
1425
                                        NotificationManager.showMessageError(PluginServices.getText(null, "Unknown_layer_shape_type") + " : " + layer.getName(), e);
1426
                                }
1427

    
1428
                                layerTypeLabel.setFont(new Font("Helvetica", Font.ITALIC, 12));
1429

    
1430
                            add(layerTypeLabel);
1431
                    }
1432

    
1433
                        doSpringLayout();
1434
                    setPreferredSize(new Dimension(340, 16));
1435
            }
1436

    
1437
            /**
1438
             * <p>Creates the <code>Spring</code> layout of this component.</p>
1439
             */
1440
            private void doSpringLayout() {
1441
                        Component[] components = getComponents();
1442
                SpringLayout layout = (SpringLayout)getLayout();
1443
                Spring xPad = Spring.constant(5);
1444
                Spring ySpring = Spring.constant(0);
1445
                Spring xSpring = xPad;
1446

    
1447
                // Make every component 5 pixels away from the component to its left.
1448
                for (int i = 0; i < components.length; i++) {
1449
                    SpringLayout.Constraints cons = layout.getConstraints(components[i]);
1450
                    cons.setX(xSpring);
1451
                    xSpring = Spring.sum(xPad, cons.getConstraint("East"));
1452

    
1453
                    cons.setY(ySpring);
1454
                }
1455
            }
1456
            
1457
            /**
1458
             * <p>Gets the layer wrappered.</p> 
1459
             * 
1460
             * @return the layer wrappered
1461
             */
1462
            public FLyrVect getLayer() {
1463
                    return layer;
1464
            }
1465
            
1466
            /**
1467
             * <p>Gets the level in the tree of the layer wrappered.</p> 
1468
             * 
1469
             * @return the level in the tree of the layer wrappered
1470
             */
1471
            public int getLevel() {
1472
                    return level;
1473
            }
1474

    
1475
            /*
1476
             * (non-Javadoc)
1477
             * @see javax.swing.JComponent#setForeground(java.awt.Color)
1478
             */
1479
            public void setForeground(Color fg) {
1480
                    super.setForeground(fg);
1481
               }
1482

    
1483
            /*
1484
             * (non-Javadoc)
1485
             * @see javax.swing.JComponent#setBackground(java.awt.Color)
1486
             */
1487
            public void setBackground(Color bg) {
1488
                    super.setBackground(bg);
1489
            }
1490

    
1491
            /*
1492
             * (non-Javadoc)
1493
             * @see java.lang.Object#toString()
1494
             */
1495
            public String toString() {
1496
                    return layer.getName();
1497
            }
1498
    }
1499

    
1500
    /**
1501
     * <p>Cell renderer of the combo box with information of the visible vector layers in the current active view.</p>
1502
     * 
1503
     * @see ListCellRenderer
1504
     * 
1505
     * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
1506
     */
1507
    private class LayersComboBoxCellRenderer implements ListCellRenderer {
1508
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
1509
                            FLayerWrapper renderer = (FLayerWrapper)value;
1510
                            
1511
                            if (isSelected) {
1512
                                    renderer.setForeground(UIManager.getColor( "ComboBox.selectionForeground" ));
1513
                                    renderer.setBackground(UIManager.getColor( "ComboBox.selectionBackground" ));
1514
                            }
1515
                            else
1516
                                    renderer.setBackground(Color.WHITE);
1517

    
1518
                        return renderer;
1519
            }
1520
    }
1521
    
1522
    /**
1523
     * <p>Closes this window.</p>
1524
     */
1525
        private void closeThis() {
1526
                PluginServices.getMDIManager().closeWindow(this);
1527
        }
1528
        
1529
        /**
1530
         * <p>Gets a list with the indexes of the active layers in the tree.</p>
1531
         * 
1532
         * @param tree the {@link TOC TOC} tree with teh active layers
1533
         * 
1534
         * @return indexes of the active layers
1535
         */
1536
        private FLayer[] getActiveLayers(TOC toc) {
1537
            
1538
            return toc.getSelectedLayers();
1539
//            FLayer[] ll = toc.getSelectedLayers()
1540
//                TreePath tPath;
1541
//                Object node;
1542
//                Object userObject;
1543
//                ArrayList rows = new ArrayList();
1544
//                
1545
//                try {
1546
//                        for (int row = 0; row < tree.getRowCount(); row++) {
1547
//                                tPath = tree.getPathForRow(row);
1548
//                                node = tPath.getLastPathComponent();
1549
//                                
1550
//                                if (node instanceof DefaultMutableTreeNode) {
1551
//                                        userObject = ((DefaultMutableTreeNode)node).getUserObject();
1552
//                                        
1553
//                                        if (userObject instanceof TocItemBranch) {
1554
//                                                FLayer layer = ((TocItemBranch)userObject).getLayer();
1555
//                                                
1556
//                                                if (layer.isActive())
1557
//                                                        rows.add(new Integer(row));
1558
//                                        }
1559
//                                }
1560
//                        }
1561
//                }
1562
//                catch (Exception e) {
1563
//                        JOptionPane.showMessageDialog(this, PluginServices.getText(this, "Failed_saving_active_layer_indexes"), PluginServices.getText(this, "Error"), JOptionPane.ERROR_MESSAGE);
1564
//                }
1565
//                return rows;
1566
        }
1567
        
1568
        /**
1569
         * <p>Restores all active layers.</p>
1570
         * 
1571
         * @param tree view's TOC's tree
1572
         * @param rows the rows to restore
1573
         */
1574
        private void restoreActiveLayers(TOC toc, FLayer[] ll) {
1575
            
1576
            int len = ll.length;
1577
        toc.clearSelection();
1578
            for (int i=0; i<len; i++) {
1579
                toc.selectLayer(ll[i]);
1580
            }
1581
//                TreePath tPath;
1582
//                Object node;
1583
//                Object userObject;
1584
//                int row;
1585
//                FLayer layer;
1586
//
1587
//                try {
1588
//                        if ((tree == null) || (rows == null))
1589
//                                return;
1590
//                        
1591
//                        // First sets all not active
1592
//                        for (row = 0; row < tree.getRowCount(); row ++) {
1593
//                                tPath = tree.getPathForRow(row);
1594
//                                node = tPath.getLastPathComponent();
1595
//        
1596
//                                if (node instanceof DefaultMutableTreeNode) {
1597
//                                        userObject = ((DefaultMutableTreeNode)node).getUserObject();
1598
//        
1599
//                                        if (userObject instanceof TocItemBranch) {
1600
//                                                layer = ((TocItemBranch)userObject).getLayer();
1601
//        
1602
//                                                layer.setActive(false);
1603
//                                        }
1604
//                                }
1605
//                        }
1606
//                        
1607
//                        // Second set active only the required layers
1608
//                        for (int i = 0; i < rows.size(); i ++) {
1609
//                                row = ((Integer)rows.get(i)).intValue();
1610
//                                
1611
//                                tPath = tree.getPathForRow(row);
1612
//                                node = tPath.getLastPathComponent();
1613
//                                
1614
//                                if (node instanceof DefaultMutableTreeNode) {
1615
//                                        userObject = ((DefaultMutableTreeNode)node).getUserObject();
1616
//                                        
1617
//                                        if (userObject instanceof TocItemBranch) {
1618
//                                                layer = ((TocItemBranch)userObject).getLayer();
1619
//                                                
1620
//                                                layer.setActive(true);
1621
//                                        }
1622
//                                }
1623
//                        }
1624
//                }
1625
//                catch (Exception e) {
1626
//                        JOptionPane.showMessageDialog(this, PluginServices.getText(this, "Failed_restoring_active_layers"), PluginServices.getText(this, "Error"), JOptionPane.ERROR_MESSAGE);
1627
//                }
1628
        }
1629

    
1630
        public Object getWindowProfile() {
1631
                return WindowInfo.DIALOG_PROFILE;
1632
        }
1633
}