Statistics
| Revision:

root / trunk / extensions / extDerivedGeometries / src / es / iver / derivedGeom / gui / AddGeometricInfoPanel.java @ 28223

History | View | Annotate | Download (55.6 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
import org.gvsig.gui.beans.AcceptCancelPanel;
68
import org.gvsig.gui.beans.comboboxconfigurablelookup.DefaultComboBoxConfigurableLookUpModel;
69
import org.gvsig.gui.beans.comboboxconfigurablelookup.JComboBoxConfigurableLookUp;
70
import org.gvsig.gui.beans.incrementabletask.IncrementableTask;
71
import org.gvsig.gui.beans.progresspanel.ProgressPanel;
72
import org.gvsig.gui.beans.specificcaretposition.JTextFieldWithSCP;
73
import org.gvsig.gui.javax.swing.jLabelCellRenderer.JLabelCellRenderer;
74

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

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

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

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

    
157
        private GeomInfo currentField;
158

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

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

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

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

    
203
            getAdaptedAcceptCancelPanel().setOkButtonEnabled(false);
204

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

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

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

    
242
            return northPanel;
243
     }
244

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

    
259
            return isEditableLabel;
260
    }
261

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

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

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

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

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

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

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

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

    
323
            return southPanel;
324
    }
325

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
508
            return southCenterPanel;
509
    }
510

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
796
                            SelectableDataSource sds = layer.getRecordset();
797

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1234
                                        /* 1.2- Stores the active layers */
1235
                                        final ArrayList activeLayers = getActiveLayerIndexes(view.getTOC().getTree());
1236

    
1237
                                        /* 2- Creates the process */
1238
                                        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());
1239

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1429
                            add(layerTypeLabel);
1430
                    }
1431

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

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

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

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

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

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

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

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

    
1517
                        return renderer;
1518
            }
1519
    }
1520
    
1521
    /**
1522
     * <p>Closes this window.</p>
1523
     */
1524
        private void closeThis() {
1525
                PluginServices.getMDIManager().closeWindow(this);
1526
        }
1527
        
1528
        /**
1529
         * <p>Gets a list with the indexes of the active layers in the tree.</p>
1530
         * 
1531
         * @param tree the {@link TOC TOC} tree with teh active layers
1532
         * 
1533
         * @return indexes of the active layers
1534
         */
1535
        private ArrayList getActiveLayerIndexes(JTree tree) {
1536
                TreePath tPath;
1537
                Object node;
1538
                Object userObject;
1539
                ArrayList rows = new ArrayList();
1540
                
1541
                try {
1542
                        for (int row = 0; row < tree.getRowCount(); row++) {
1543
                                tPath = tree.getPathForRow(row);
1544
                                node = tPath.getLastPathComponent();
1545
                                
1546
                                if (node instanceof DefaultMutableTreeNode) {
1547
                                        userObject = ((DefaultMutableTreeNode)node).getUserObject();
1548
                                        
1549
                                        if (userObject instanceof TocItemBranch) {
1550
                                                FLayer layer = ((TocItemBranch)userObject).getLayer();
1551
                                                
1552
                                                if (layer.isActive())
1553
                                                        rows.add(new Integer(row));
1554
                                        }
1555
                                }
1556
                        }
1557
                }
1558
                catch (Exception e) {
1559
                        JOptionPane.showMessageDialog(this, PluginServices.getText(this, "Failed_saving_active_layer_indexes"), PluginServices.getText(this, "Error"), JOptionPane.ERROR_MESSAGE);
1560
                }
1561
                return rows;
1562
        }
1563
        
1564
        /**
1565
         * <p>Restores all active layers.</p>
1566
         * 
1567
         * @param tree view's TOC's tree
1568
         * @param rows the rows to restore
1569
         */
1570
        private void restoreActiveLayers(JTree tree, ArrayList rows) {
1571
                TreePath tPath;
1572
                Object node;
1573
                Object userObject;
1574
                int row;
1575
                FLayer layer;
1576

    
1577
                try {
1578
                        if ((tree == null) || (rows == null))
1579
                                return;
1580
                        
1581
                        // First sets all not active
1582
                        for (row = 0; row < tree.getRowCount(); row ++) {
1583
                                tPath = tree.getPathForRow(row);
1584
                                node = tPath.getLastPathComponent();
1585
        
1586
                                if (node instanceof DefaultMutableTreeNode) {
1587
                                        userObject = ((DefaultMutableTreeNode)node).getUserObject();
1588
        
1589
                                        if (userObject instanceof TocItemBranch) {
1590
                                                layer = ((TocItemBranch)userObject).getLayer();
1591
        
1592
                                                layer.setActive(false);
1593
                                        }
1594
                                }
1595
                        }
1596
                        
1597
                        // Second set active only the required layers
1598
                        for (int i = 0; i < rows.size(); i ++) {
1599
                                row = ((Integer)rows.get(i)).intValue();
1600
                                
1601
                                tPath = tree.getPathForRow(row);
1602
                                node = tPath.getLastPathComponent();
1603
                                
1604
                                if (node instanceof DefaultMutableTreeNode) {
1605
                                        userObject = ((DefaultMutableTreeNode)node).getUserObject();
1606
                                        
1607
                                        if (userObject instanceof TocItemBranch) {
1608
                                                layer = ((TocItemBranch)userObject).getLayer();
1609
                                                
1610
                                                layer.setActive(true);
1611
                                        }
1612
                                }
1613
                        }
1614
                }
1615
                catch (Exception e) {
1616
                        JOptionPane.showMessageDialog(this, PluginServices.getText(this, "Failed_restoring_active_layers"), PluginServices.getText(this, "Error"), JOptionPane.ERROR_MESSAGE);
1617
                }
1618
        }
1619

    
1620
        public Object getWindowProfile() {
1621
                return WindowInfo.DIALOG_PROFILE;
1622
        }
1623
}