Statistics
| Revision:

root / trunk / extensions / extQuickInfo / src / org / gvsig / quickInfo / gui / QuickInfoDataSelectionPanel.java @ 28223

History | View | Annotate | Download (50.7 KB)

1
package org.gvsig.quickInfo.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.Graphics;
31
import java.awt.GridLayout;
32
import java.awt.event.ActionEvent;
33
import java.awt.event.ActionListener;
34
import java.awt.event.ComponentAdapter;
35
import java.awt.event.ComponentEvent;
36
import java.awt.event.ItemEvent;
37
import java.awt.event.ItemListener;
38
import java.awt.event.MouseAdapter;
39
import java.awt.event.MouseEvent;
40
import java.sql.Types;
41
import java.util.Arrays;
42
import java.util.Collections;
43
import java.util.Comparator;
44
import java.util.Vector;
45

    
46
import javax.swing.BorderFactory;
47
import javax.swing.Icon;
48
import javax.swing.ImageIcon;
49
import javax.swing.JButton;
50
import javax.swing.JCheckBox;
51
import javax.swing.JComboBox;
52
import javax.swing.JLabel;
53
import javax.swing.JList;
54
import javax.swing.JOptionPane;
55
import javax.swing.JPanel;
56
import javax.swing.JScrollPane;
57
import javax.swing.JTable;
58
import javax.swing.ListCellRenderer;
59
import javax.swing.ListSelectionModel;
60
import javax.swing.Spring;
61
import javax.swing.SpringLayout;
62
import javax.swing.UIManager;
63
import javax.swing.table.DefaultTableModel;
64
import javax.swing.table.TableCellRenderer;
65
import javax.swing.table.TableModel;
66

    
67
import org.gvsig.gui.beans.AcceptCancelPanel;
68
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel;
69
import org.gvsig.gui.beans.incrementabletask.IncrementableEvent;
70
import org.gvsig.gui.beans.incrementabletask.IncrementableProcess;
71
import org.gvsig.gui.beans.incrementabletask.IncrementableTask;
72
import org.gvsig.gui.beans.progresspanel.ProgressPanel;
73
import org.gvsig.quickInfo.QuickInfoFLayerSelected;
74
import org.gvsig.quickInfo.tools.QuickInfoListener;
75
import org.gvsig.quickInfo.utils.FShapeTypeNames;
76
import org.gvsig.quickInfo.utils.SQLTypeNames;
77

    
78
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
79
import com.hardcode.gdbms.engine.data.driver.DriverException;
80
import com.iver.andami.PluginServices;
81
import com.iver.andami.messages.NotificationManager;
82
import com.iver.andami.ui.mdiManager.IWindow;
83
import com.iver.andami.ui.mdiManager.WindowInfo;
84
import com.iver.cit.gvsig.fmap.MapControl;
85
import com.iver.cit.gvsig.fmap.core.FShape;
86
import com.iver.cit.gvsig.fmap.layers.FLayer;
87
import com.iver.cit.gvsig.fmap.layers.FLayers;
88
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
89
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
90
import com.iver.cit.gvsig.fmap.tools.CompoundBehavior;
91
import com.iver.cit.gvsig.fmap.tools.Behavior.Behavior;
92
import com.iver.cit.gvsig.fmap.tools.Behavior.MouseMovementBehavior;
93
import com.iver.cit.gvsig.fmap.tools.Listeners.PointListener;
94
import com.iver.cit.gvsig.project.documents.view.toc.TocItemBranch;
95

    
96
/**
97
 * <p>Panel where user will select the layer and its fields to display as a <i>quick information tool</i>
98
 *  on a <code>MapControl</code> object.</p>
99
 * 
100
 * @version 06/03/08
101
 * 
102
 * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
103
 */
104
public class QuickInfoDataSelectionPanel extends JPanel implements IWindow {
105

    
106
        private MapControl mapControl;
107
        private JLabel layersLabel = null;
108
    private JComboBox layersComboBox = null;
109
    private javax.swing.JScrollPane layerFieldsScrollPane = null;
110
    private JPanel northPanel = null;
111
    private JPanel layersPanel = null;
112
    private JPanel layerFieldsPanel = null;
113
    private JPanel optionalFieldsPanel = null;
114
    private JPanel optionButtonsPanel = null;
115
    private JButton fieldsModeButton = null;
116
    private JButton selectAllButton = null;
117
    private JButton unselectAllButton = null;
118
        private AdaptedAcceptCancelPanel acceptCancelPanel = null;
119
        
120
        private JTable fieldsTable = null;
121
        private TableModel fieldsTableModel = null;
122
        
123
        private ImageIcon leafIcon;
124
        private ImageIcon complexIcon;
125
        private ImageIcon childIcon;        
126
        private ImageIcon bIcon;
127
        private JLabel childIconLabel;        
128
//        private JLabel bIconLabel;
129

    
130
        private QuickInfoListener qIListener;
131
        private QuickInfoFLayerSelected infoLayerSelected;
132

    
133
    private WindowInfo viewInfo = null;
134
    private final short Window_Width = 480;
135
    private final short Window_Height = 440;
136
    private final int layersComboBox_Width = 390;
137
    private final int layersComboBox_Height = 22;
138
    
139
    private int selectedFields = 0;
140

    
141
    // Layer types:
142
    private final short UNDEFINED = -1;
143
    // End Layer types
144
    
145
    private int previous_Type = UNDEFINED;
146
    
147
    private boolean cancelled = false;
148
     
149
    /**
150
     * <p>Creates a new form where user could select the quick information to display.</p>
151
     */
152
    public QuickInfoDataSelectionPanel(MapControl mapControl) {
153
            super();
154

    
155
            this.mapControl = mapControl;
156
            
157
            // Removes any previous combinated tool
158
            removeQuickInfoTool(mapControl);
159
                mapControl.setToolTipText(null);
160
            
161
            initialize();
162
    }
163
    
164
    /**
165
     * <p>Initializes this component.</p>
166
     */                        
167
    private void initialize() {
168
                IncrementableTask iTask;
169
                InitQIPanelProcess iprocess = new InitQIPanelProcess(PluginServices.getText(this, "Initializing_configuration_quick_info_tool"), PluginServices.getText(this, "loading_layers_please_wait"), this);
170
                final InitQIPanelProcess f_iprocess = iprocess; 
171
                
172
                iTask = new IncrementableTask(iprocess, new ProgressPanel(false));
173
                iTask.addIncrementableListener(iprocess);
174
                iprocess.setIncrementableTask(iTask);
175

    
176
                /* Opens maximized the window created */
177
                final QuickInfoDataSelectionPanel f_qIPanel = this;
178
                final IncrementableTask f_iTask = iTask;
179

    
180
                iTask.getProgressPanel().addComponentListener(new ComponentAdapter() {
181
                        /*
182
                         * (non-Javadoc)
183
                         * @see java.awt.event.ComponentAdapter#componentHidden(java.awt.event.ComponentEvent)
184
                         */
185
                        public void componentHidden(ComponentEvent e) {
186
                                if ((! cancelled) && (f_iprocess.getPercent() == 100)) {
187
                                        f_qIPanel.setVisible(true);
188
                                        f_iTask.getProgressPanel().dispose();
189
                                        PluginServices.getMDIManager().addWindow(f_qIPanel);
190
                                }
191
                        }
192
                });
193

    
194
                iprocess.start();
195
                iTask.start();
196
//                setLayout(new FlowLayout());
197
//                
198
//                bIcon = new ImageIcon(QuickInfoDataSelectionPanel.class.getClassLoader().getResource("images/layerGroup.png"));
199
//                 leafIcon = new ImageIcon(QuickInfoDataSelectionPanel.class.getClassLoader().getResource("images/field-leaf-icon.png"));
200
//                childIcon = new ImageIcon(QuickInfoDataSelectionPanel.class.getClassLoader().getResource("images/field-child-icon.png"));
201
//                complexIcon = new ImageIcon(QuickInfoDataSelectionPanel.class.getClassLoader().getResource("images/field-complex-icon.png"));
202
//                
203
//            add(getNorthPanel());
204
//            add(getAdaptedAcceptCancelPanel());
205
//
206
//                acceptCancelPanel.setOkButtonEnabled(false);
207
//            
208
//            refreshVisibleVectorLayers();
209
    }
210

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

    
224
    /**
225
         * <p>This method initializes northPanel.</p>        
226
         *         
227
         * @return javax.swing.JPanel
228
     */
229
    private JPanel getNorthPanel() {
230
            if (northPanel == null) {
231
                    northPanel = new JPanel();
232
                    northPanel.setBorder(BorderFactory.createTitledBorder(PluginServices.getText(this, "select_layer_and_fields")));
233

    
234
                    northPanel.setPreferredSize(new Dimension(470, 435));
235
                    northPanel.add(getLayersPanel());
236
                    northPanel.add(getLayerFieldsPanel());
237
                northPanel.add(getOptionalFieldsPanel());
238
                northPanel.add(getOptionButtonsPanel());
239
            }
240
            
241
            return northPanel;
242
     }
243

    
244
    /**
245
         * <p>This method initializes layersPanel.</p>        
246
         *         
247
         * @return javax.swing.JPanel
248
     */
249
    private JPanel getLayersPanel() {
250
            if (layersPanel == null) {
251
                    layersPanel = new JPanel();
252
                    layersPanel.setLayout(new FlowLayout());
253
                    layersPanel.setPreferredSize(new Dimension(460, 28));
254
                    layersPanel.add(getLayersLabel());
255
                    layersPanel.add(getLayersComboBox());
256
            }
257
            
258
            return layersPanel;
259
    }
260

    
261
    /**
262
         * <p>This method initializes layersLabel.</p>        
263
         *         
264
         * @return javax.swing.JLabel
265
     */
266
    private JLabel getLayersLabel() {
267
            if (layersLabel == null) {
268
                    layersLabel = new JLabel(PluginServices.getText(this, "Layer"));
269
                    layersLabel.setPreferredSize(new Dimension(40, 20));
270
            }
271
            
272
            return layersLabel;
273
    }
274
    
275
    /**
276
         * <p>This method initializes layersComboBox.</p>        
277
         *         
278
         * @return javax.swing.JComboBox
279
     */
280
    private JComboBox getLayersComboBox() {
281
            if (layersComboBox == null) {
282
                    layersComboBox = new JComboBox();
283
                    layersComboBox.setPreferredSize(new Dimension(layersComboBox_Width, layersComboBox_Height));
284
                    layersComboBox.addItemListener(new ItemListener() {
285
                            /*
286
                             * (non-Javadoc)
287
                             * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
288
                             */
289
                                public void itemStateChanged(ItemEvent e) {
290
                                        if (e.getStateChange() == ItemEvent.SELECTED) {
291
                                                infoLayerSelected = null;
292
                                                selectedFields = 0;
293
                                                
294
                                                try {
295
                                                        FLayerWrapper fW = (FLayerWrapper)e.getItem();
296
                                                        
297
                                                        if (fW != null) {
298
                                                                FLayer layer = fW.getLayer();
299
                                                                
300
                                                                if (layer != null) {
301
                                                                        infoLayerSelected = null;
302
                                                                        infoLayerSelected = new QuickInfoFLayerSelected(layer);
303
                                                                        getFieldsModeButton().setToolTipText(PluginServices.getText(this, "Ascending"));
304
                                                                        layersComboBox.setToolTipText("<html>" + PluginServices.getText(this, "Layer") + ": " + layer.getName() + "<br>" + 
305
                                                                                PluginServices.getText(this, "Type") + ": " + FShapeTypeNames.getFShapeTypeName(((FLyrVect)layer).getShapeType()) + "</html>");
306
                                                                        
307
                                                                        refreshSelectedLayerFields();
308
                                                                }
309
                                                        }
310
                                                        
311
                                                        getFieldsModeButton().setText(PluginServices.getText(this, "Ascending"));
312
                                                        getFieldsModeButton().setToolTipText(PluginServices.getText(this, "Ascending"));
313
                                                        updateAcceptButton();
314
                                                }
315
                                                catch(ReadDriverException dE) {
316
                                                        NotificationManager.showMessageError(PluginServices.getText(null, "problem_loading_layers"), dE);
317
                                                }
318
                                        }
319
                                }
320
                    });
321
            }
322
            
323
            return layersComboBox;
324
    }
325
    
326
    /**
327
         * <p>This method initializes layersPanel.</p>        
328
         *         
329
         * @return javax.swing.JPanel
330
     */
331
    private JPanel getLayerFieldsPanel() {
332
               if (layerFieldsPanel == null) {
333
                       layerFieldsPanel = new JPanel();
334
                       layerFieldsPanel.setLayout(new FlowLayout());
335
                       layerFieldsPanel.setPreferredSize(new Dimension(460, 230));
336
                       layerFieldsPanel.setBorder(BorderFactory.createTitledBorder(PluginServices.getText(this, "Fields")));
337
                       layerFieldsPanel.add(getLayerFieldsScrollPane());
338
            }
339
            
340
            return layerFieldsPanel;
341
    }
342
    
343
    /**
344
         * <p>This method initializes layerFieldsScrollPane.</p>        
345
         *         
346
         * @return javax.swing.JScrollPane
347
     */
348
        private JScrollPane getLayerFieldsScrollPane() {
349
                if (layerFieldsScrollPane == null) {
350
                        layerFieldsScrollPane = new JScrollPane();
351
                        layerFieldsScrollPane.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
352
                        layerFieldsScrollPane.setPreferredSize(new java.awt.Dimension(446, 196));
353
                        layerFieldsScrollPane.setViewportView(getFieldsTable());
354
                }
355
                
356
                return layerFieldsScrollPane;
357
        }
358
        
359
    /**
360
         * <p>This method initializes optionButtonsPanel.</p>        
361
         *         
362
         * @return javax.swing.JPanel
363
     */
364
        private JPanel getOptionButtonsPanel() {
365
                if (optionButtonsPanel == null) {
366
                        optionButtonsPanel = new JPanel();
367
                        
368
                        optionButtonsPanel.setLayout(new FlowLayout());
369
                        optionButtonsPanel.setPreferredSize(new Dimension(420, 32));
370
                        optionButtonsPanel.add(getSelectAllButton());
371
                        optionButtonsPanel.add(getFieldsModeButton());
372
                        optionButtonsPanel.add(getUnselectAllButton());
373
                }
374
                
375
                return optionButtonsPanel;
376
        }
377
        
378
    /**
379
         * <p>This method initializes selectAllButton.</p>        
380
         *         
381
         * @return javax.swing.JButton
382
     */
383
        private JButton getSelectAllButton() {
384
                if (selectAllButton == null) {
385
                        selectAllButton = new JButton(PluginServices.getText(this, "Select_all"));
386
                        selectAllButton.setToolTipText(PluginServices.getText(this, "Select_all"));
387
                        selectAllButton.setPreferredSize(new Dimension(150, 22));
388
                        selectAllButton.setLocation(0, 10);
389
                        selectAllButton.addMouseListener(new MouseAdapter() {
390
                                /*
391
                                 * @see java.awt.event.MouseAdapter#mouseReleased(java.awt.event.MouseEvent)
392
                                 */
393
                                public void mouseReleased(MouseEvent e) {
394
                                        for (int i = 0; i < getFieldsTable().getRowCount(); i++) {
395
                                                fieldsTable.getModel().setValueAt(Boolean.TRUE, i, 0);
396
                                                infoLayerSelected.getLayerFields().put(((FieldNameWrapper)fieldsTable.getModel().getValueAt(i, 1)).getField(), new Vector());
397
                                        }
398
                                        
399
                                        selectedFields = fieldsTable.getRowCount();
400
                                        
401
                                        for (int i = 0; i < getOptionalFieldsPanel().getComponentCount(); i++) {
402
                                                Component child = getOptionalFieldsPanel().getComponent(i);
403
                                                if (child instanceof JCheckBox) {
404
                                                        ((JCheckBox)child).setSelected(true);
405
                                                        infoLayerSelected.getCalculatedLayerFields().put(((JCheckBox)child).getText(), new Vector());
406
                                                        selectedFields ++;
407
                                                }
408
                                        }
409
                                        
410
                                        updateAcceptButton();
411
                                }
412
                        });
413
                }
414
                
415
                return selectAllButton;
416
        }
417

    
418
        /**
419
         * <p>This method initializes fieldsModeButton.</p>        
420
         *         
421
         * @return javax.swing.JButton
422
         */
423
        private JButton getFieldsModeButton() {
424
                if (fieldsModeButton == null) {
425
                        fieldsModeButton = new JButton(PluginServices.getText(this, "Ascending"));
426
                        fieldsModeButton.setToolTipText(PluginServices.getText(this, "Ascending"));
427
                        fieldsModeButton.setPreferredSize(new Dimension(100, 22));
428
                        fieldsModeButton.addMouseListener(new MouseAdapter() {
429
                                /*
430
                                 * @see java.awt.event.MouseAdapter#mouseReleased(java.awt.event.MouseEvent)
431
                                 */
432
                                public void mouseReleased(MouseEvent e) {
433
                                        if (fieldsModeButton.getText().compareTo(PluginServices.getText(this, "Ascending")) == 0) {
434
                                            // Sort the values in the second column of the model
435
                                            // in descending order
436
                                                sortFieldColumns(((DefaultTableModel)getFieldsTable().getModel()), ColumnSorter.ASCENDING);
437
                                                fieldsModeButton.setText(PluginServices.getText(this, "Descending"));
438
                                                fieldsModeButton.setToolTipText(PluginServices.getText(this, "Descending"));
439
                                        }
440
                                        else {
441
                                                if (fieldsModeButton.getText().compareTo(PluginServices.getText(this, "Descending")) == 0) {
442
                                                        sortFieldColumns(((DefaultTableModel)getFieldsTable().getModel()), ColumnSorter.DESCENDING);
443
                                                        fieldsModeButton.setText(PluginServices.getText(this, "Reload"));
444
                                                        fieldsModeButton.setToolTipText(PluginServices.getText(this, "Reload"));
445
                                                }
446
                                                else {
447
                                                        if (fieldsModeButton.getText().compareTo(PluginServices.getText(this, "Reload")) == 0) {
448
                                                                int index = getLayersComboBox().getSelectedIndex();
449
                                                                getLayersComboBox().setSelectedIndex(-1);
450
                                                                getLayersComboBox().setSelectedIndex(index);
451
                                                                fieldsModeButton.setText(PluginServices.getText(this, "Ascending"));
452
                                                                fieldsModeButton.setToolTipText(PluginServices.getText(this, "Ascending"));
453
                                                        }
454
                                                }
455
                                        }
456
                                }
457
                        });
458
                }
459
                
460
                return fieldsModeButton;
461
        }
462
        
463
    /**
464
         * <p>This method initializes unselectAllButton.</p>        
465
         *         
466
         * @return javax.swing.JButton
467
     */
468
        private JButton getUnselectAllButton() {
469
                if (unselectAllButton == null) {
470
                        unselectAllButton = new JButton(PluginServices.getText(this, "Clean_selection"));
471
                        unselectAllButton.setToolTipText(PluginServices.getText(this, "Clean_selection"));
472
                        unselectAllButton.setPreferredSize(new Dimension(150, 22));
473
                        unselectAllButton.addMouseListener(new MouseAdapter() {
474
                                /*
475
                                 * @see java.awt.event.MouseAdapter#mouseReleased(java.awt.event.MouseEvent)
476
                                 */
477
                                public void mouseReleased(MouseEvent e) {
478
                                        for (int i = 0; i < getFieldsTable().getRowCount(); i++) {
479
                                                fieldsTable.getModel().setValueAt(Boolean.FALSE, i, 0);
480
                                                infoLayerSelected.getLayerFields().remove(((FieldNameWrapper)fieldsTable.getModel().getValueAt(i, 1)).getField());
481
                                        }
482
                                        
483
                                        for (int i = 0; i < getOptionalFieldsPanel().getComponentCount(); i++) {
484
                                                Component child = getOptionalFieldsPanel().getComponent(i);
485
                                                if (child instanceof JCheckBox) {
486
                                                        ((JCheckBox)child).setSelected(false);
487
                                                        infoLayerSelected.getCalculatedLayerFields().remove(((JCheckBox)child).getText());
488
                                                        selectedFields --;
489
                                                }
490
                                        }
491
                                        
492
                                        selectedFields = 0;
493
                                        updateAcceptButton();
494
                                }
495
                        });
496
                }
497
                
498
                return unselectAllButton;
499
        }
500
        
501
    /**
502
         * <p>This method initializes optionalFieldsPanel.</p>        
503
         *         
504
         * @return javax.swing.JPanel
505
     */
506
        private JPanel getOptionalFieldsPanel() {
507
                if (optionalFieldsPanel == null) {
508
                        optionalFieldsPanel = new JPanel();
509

    
510
                        optionalFieldsPanel.setPreferredSize(new Dimension(460, 100));
511
                        optionalFieldsPanel.setBorder(BorderFactory.createTitledBorder(PluginServices.getText(this, "calculated_fields")));
512
                        
513
                        GridLayout layout = new GridLayout(4, 4, 5, 5);
514
                        optionalFieldsPanel.setLayout(layout);
515
                }
516
                
517
                return optionalFieldsPanel;
518
        }
519

    
520
        /**
521
         * <p>Returns a reference to the table that stores the fields.</p>
522
         * 
523
         * @return reference to the table that stores the fields
524
         */
525
        private JTable getFieldsTable() {
526
                if (fieldsTable == null) {
527
                        fieldsTable = new JTable(getFieldsTableModel()) {
528
                                private static final long serialVersionUID = 6100282856104273588L;
529

    
530
                                /*
531
                                 * (non-Javadoc)
532
                                 * @see javax.swing.JTable#isCellEditable(int, int)
533
                                 */
534
                                public boolean isCellEditable(int row, int column) {
535
                                        return false;
536
                                }
537
                        };
538
                        
539
                        fieldsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
540
                        
541
                        fieldsTable.getColumnModel().getColumn(0).setMaxWidth(15);
542
                        
543
                        JLabel l=(JLabel)fieldsTable.getTableHeader().getDefaultRenderer();
544
                        l.setPreferredSize(new Dimension(15,20));
545
                        
546
                        fieldsTable.getColumnModel().getColumn(1).setCellRenderer(new FieldNameRenderer());
547
                        
548
                        fieldsTable.addMouseListener(new MouseAdapter() {
549
                        
550
                                public void mouseReleased(MouseEvent e) {
551
                                        int row = fieldsTable.rowAtPoint(e.getPoint());
552
                                        
553
                                        if (row >= 0) {
554
                                                 Boolean b = ((Boolean)fieldsTable.getModel().getValueAt(row, 0));
555

    
556
                                                 if (b.booleanValue() == true) {
557
                                                         fieldsTable.getModel().setValueAt(Boolean.FALSE, row, 0);
558
                                                         infoLayerSelected.getLayerFields().remove(((FieldNameWrapper)fieldsTable.getModel().getValueAt(row, 1)).getField());
559
                                                         selectedFields --;
560
                                                         updateAcceptButton();
561
                                                 }
562
                                                 else {
563
                                                         fieldsTable.getModel().setValueAt(Boolean.TRUE, row, 0);
564
                                                         infoLayerSelected.getLayerFields().put(((FieldNameWrapper)fieldsTable.getModel().getValueAt(row, 1)).getField(), new Vector());
565
                                                         selectedFields ++;
566
                                                         updateAcceptButton();
567
                                                 }
568
                                        }                                        
569
                                }                                
570
                        });
571
                }
572
                
573
                return fieldsTable;
574
        }
575

    
576
        /**
577
         * <p>Enables / disables the <i>accept</i> button according the number of selected fields.</p>
578
         * 
579
         * <p>If there is no field selected, disables that button, is there is any selected, enables it.</p>
580
         */
581
        private void updateAcceptButton() {
582
                if (selectedFields > 0) {
583
                        getAdaptedAcceptCancelPanel().setOkButtonEnabled(true);
584
                }
585
                else {
586
                        getAdaptedAcceptCancelPanel().setOkButtonEnabled(false);
587
                }
588
        }
589

    
590
        /**
591
         * <p>Gets the table model that lists the fields of the selected layer.</p>
592
         * 
593
         * @return the table model that lists the fields of the selected layer
594
         */
595
        private TableModel getFieldsTableModel() {
596
                if (fieldsTableModel == null) {
597
                        fieldsTableModel = new LayerTypesTableModel();
598
                        ((DefaultTableModel)fieldsTableModel).addColumn("", new Object[] {});
599
                        ((DefaultTableModel)fieldsTableModel).addColumn(PluginServices.getText(this, "Name"), new Object[] {} );
600
                        ((DefaultTableModel)fieldsTableModel).addColumn(PluginServices.getText(this, "Type"), new Object[] {});
601
                }
602
                
603
                return fieldsTableModel;
604
        }
605
        
606
        /**
607
         * <p>Updates "layersComboBox" with the name of visible layers of the associated <code>MapControl</code> instance.</p>
608
         * 
609
         * @see #refreshVisibleVectorLayers(FLayer, int)
610
         */
611
        private void refreshVisibleVectorLayers() {
612
                FLayer rootNode = mapControl.getMapContext().getLayers();
613

    
614
                getLayersComboBox().removeAllItems();
615
                
616
                refreshVisibleVectorLayers(rootNode, -1);
617

    
618
                // The selectedLayer will be, by default, the first being added.
619
                if (getLayersComboBox().getItemCount() > 0) {
620
                        getLayersComboBox().setRenderer(new LayersComboBoxCellRenderer());
621
                        getLayersComboBox().setSelectedIndex(0);
622
                }
623
        }
624
        
625
        /**
626
         * <p>Updates recursively the combo box with the layers name and property.</p>
627
         * 
628
         * @param node parent node
629
         * @param level <code>node</code> level from the root
630
         */
631
        private void refreshVisibleVectorLayers(FLayer node, int level) {
632
                if (node instanceof FLayers) {
633
                        FLayers root = (FLayers)node;
634

    
635
                        for (int i = root.getLayersCount() - 1; i >= 0 ; i--) {
636
                                if (root.getLayer(i).isVisible()) {
637
                                        refreshVisibleVectorLayers(root.getLayer(i), level + 1);
638
                                }
639
                        }
640
                }
641
                else {
642
                        String layerName = node.getName();
643
                        
644
                        if ((node.isVisible()) && (node instanceof FLyrVect) && (layerName != null)) { 
645
                                getLayersComboBox().addItem(new FLayerWrapper(node, level, getLayerIcon(node)));
646
                        }
647
                }
648
        }
649

    
650
        /**
651
         * <p>Returns the icon that represents the layer in the current active view's TOC.</p>
652
         * 
653
         * @param layer the layer
654
         * @return the layer's icon in the current active view's TOC
655
         */
656
        private Icon getLayerIcon(FLayer layer) {
657
            if (layer.getTocImageIcon() != null) {
658
                    return layer.getTocImageIcon();
659
            }
660
            else {
661
                    TocItemBranch branch = new TocItemBranch(layer);
662

    
663
                    return branch.getIcon();
664
            }
665
        }
666
        
667
        /**
668
         * <p>Updates "layerFieldsList" with the fields of the selected vector layer.</p>
669
         */
670
        private void refreshSelectedLayerFields() {
671
                Object item = getLayersComboBox().getSelectedItem();
672

    
673
                if (item != null) {
674
                        try {
675
                                FLyrVect selectedLayer = (FLyrVect)((FLayerWrapper)item).getLayer();
676
                                
677
                                SelectableDataSource sds = selectedLayer.getRecordset();
678
                                
679
                                ((DefaultTableModel)getFieldsTable().getModel()).setRowCount(0);
680
                                fillTable(sds);
681
                                
682
                                refreshOptionalLayerFields(selectedLayer);
683
                                getFieldsModeButton().getText().compareTo(PluginServices.getText(this, "Ascending"));
684
                        } catch (Exception e) {
685
                                NotificationManager.showMessageError(PluginServices.getText(null, "problem_loading_fields_of_the_layer"), e);
686
                        }
687
                }
688
         }
689

    
690
        /**
691
         * <p>Fills the table with the fields of the layer.</p>
692
         * 
693
         * @param sds object with the selected data of the data source
694
         * @throws com.hardcode.gdbms.engine.data.driver.DriverException throws DriverException
695
         */
696
        private void fillTable(SelectableDataSource sds) throws ReadDriverException {
697
                DefaultTableModel tableModel = (DefaultTableModel)getFieldsTableModel();
698
                
699
                for (int i = 0; i < sds.getFieldCount(); i ++) {
700
                        if (sds.getFieldType(i) == Types.STRUCT)
701
                                tableModel.addRow(new Object[] {Boolean.FALSE, new FieldNameWrapper(sds.getFieldName(i), null, true), SQLTypeNames.getSQLTypeName(sds.getFieldType(i))});
702
                        else
703
                                tableModel.addRow(new Object[] {Boolean.FALSE, new FieldNameWrapper(sds.getFieldName(i), null, false), SQLTypeNames.getSQLTypeName(sds.getFieldType(i))});
704
                }
705
        }
706

    
707
        /**
708
         * <p>Updates "optionalFieldsPanel" with the check boxes that are optional fields for the selected layer,
709
         *  according its type.</p>
710
         *
711
         * @throws DriverException if fails working with the data driver
712
         */
713
        private void refreshOptionalLayerFields(FLyrVect seletedLayer) throws ReadDriverException {
714
                previous_Type = seletedLayer.getShapeType();
715
                
716
                getOptionalFieldsPanel().removeAll();
717
                
718
                switch (previous_Type) {
719
                        case FShape.NULL:
720
                                break;
721
                        case FShape.POINT:
722
                                getOptionalFieldsPanel().add(getNewOptionalCheckBox("Coordinates"));
723
                                break;
724
                        case FShape.LINE:
725
                                // Disabled:
726
                                // getOptionalFieldsPanel().add(getNewOptionalCheckBox("First_point_coordinates"));
727
                                // getOptionalFieldsPanel().add(getNewOptionalCheckBox("Second_point_coordinates"));
728
                                getOptionalFieldsPanel().add(getNewOptionalCheckBox("Length"));
729
                                break;
730
                        case FShape.POLYGON:
731
                                getOptionalFieldsPanel().add(getNewOptionalCheckBox("Area"));
732
                                getOptionalFieldsPanel().add(getNewOptionalCheckBox("Perimeter"));
733
                                break;
734
                        case FShape.TEXT:
735
                                break;
736
                        case FShape.MULTI: // Can have points, lines and polygons
737
                                getOptionalFieldsPanel().add(getNewLabel(PluginServices.getText(this, "Points") + ":"));
738
                                getOptionalFieldsPanel().add(getNewOptionalCheckBox("Coordinates"));
739
                                getOptionalFieldsPanel().add(getNewLabel("  "));
740
                                getOptionalFieldsPanel().add(getNewLabel(PluginServices.getText(this, "Lines") + ":"));
741
                                getOptionalFieldsPanel().add(getNewOptionalCheckBox("Length"));
742
                                getOptionalFieldsPanel().add(getNewLabel("  "));
743
                                getOptionalFieldsPanel().add(getNewLabel(PluginServices.getText(this, "Polygons") + ":"));
744
                                getOptionalFieldsPanel().add(getNewOptionalCheckBox("Area"));
745
                                getOptionalFieldsPanel().add(getNewOptionalCheckBox("Perimeter"));
746
                                getOptionalFieldsPanel().add(getNewLabel(PluginServices.getText(this, "Multipoints") + ":"));
747
                                getOptionalFieldsPanel().add(getNewOptionalCheckBox("Number_of_points"));
748
                                break;
749
                        case FShape.MULTIPOINT:
750
                                getOptionalFieldsPanel().add(getNewOptionalCheckBox("Number_of_points"));
751
                                break;
752
                        case FShape.CIRCLE:
753
                                // Disabled:
754
                                // getOptionalFieldsPanel().add(getNewOptionalCheckBox("Coordinates_of_the_centre"));
755
                                // getOptionalFieldsPanel().add(getNewOptionalCheckBox("Radius"));
756
                                // getOptionalFieldsPanel().add(getNewOptionalCheckBox("Circumference"));
757
                                break;
758
                        case FShape.ARC:
759
                                break;
760
                        case FShape.ELLIPSE:
761
                                break;
762
                        case FShape.Z:
763
                                break;
764
                        default : // UNDEFINED
765
                }
766

    
767
                updateUI();
768
        }
769

    
770
        /**
771
         * <p>Creates a <code>JCheckBox</code> with <code>text</code> as a <i>text</i> and <i>tool tip text</i> of that
772
         *  component.</p>
773
         * 
774
         * @param text text to assign
775
         * @return the new component
776
         */
777
        private JCheckBox getNewOptionalCheckBox(String text) {
778
                JCheckBox cb = new JCheckBox(PluginServices.getText(this, text));
779
                cb.setToolTipText(PluginServices.getText(this, text));
780
                cb.addMouseListener(new MouseAdapter() {
781
                        public void mouseReleased(MouseEvent ev) {
782
                                JCheckBox source = (JCheckBox)ev.getSource(); 
783
                                if (source.isSelected()) {
784
                                        infoLayerSelected.getCalculatedLayerFields().put(source.getText(), new Vector());
785
                                        selectedFields ++;
786
                                        updateAcceptButton();
787
                                }
788
                                else {
789
                                        infoLayerSelected.getCalculatedLayerFields().remove(source.getText());
790
                                        selectedFields --;
791
                                        updateAcceptButton();
792
                                }
793
                        }
794
                });
795
                
796
                return cb;
797
        }
798
        
799
        /**
800
         * <p>Creates a <code>JLabel</code> with <code>text</code> as a <i>text</i> and <i>tool tip text</i> of that
801
         *  component.</p>
802
         * 
803
         * @param text text to assign
804
         * @return the new component
805
         */
806
        private JLabel getNewLabel(String text) {
807
                JLabel cl = new JLabel("<html><body><p><i>" + PluginServices.getText(this, text) + "</i></p></body></html>");
808
                cl.setToolTipText("<html><body><p><i>" + PluginServices.getText(this, text) + "</i></p></body></html>");
809
                
810
                return cl;
811
        }
812

    
813
        /*
814
     * @see com.iver.andami.ui.mdiManager.View#getViewInfo()
815
     */
816
    public WindowInfo getWindowInfo() {
817
        if (viewInfo == null) {
818
            viewInfo=new WindowInfo(WindowInfo.MODALDIALOG);
819
            viewInfo.setTitle(PluginServices.getText(this, "quick_information"));
820
            viewInfo.setWidth(Window_Width);
821
            viewInfo.setHeight(Window_Height);
822
        }
823
        return viewInfo;
824
    }
825

    
826
    /**
827
     * <p>Adapts {@link AcceptCancelPanel AcceptCancelPanel} to be used as a component of the <code>QInfoDataSelectionPanel</code> panel.</p>
828
     * 
829
     * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
830
     */
831
    private class AdaptedAcceptCancelPanel extends AcceptCancelPanel {
832
                 private static final long serialVersionUID = -6112053411624411449L;
833

    
834
                public AdaptedAcceptCancelPanel () {
835
                    super();
836

    
837
                    addOkButtonActionListener(getOKAction());
838
                    addCancelButtonActionListener(getCancelAction());
839
                    setPreferredSize(new Dimension(474, 28));
840
            }
841
            
842
            /**
843
              * <p>Create the action that will be executed when user pressed the <i>ok</i> button.</p>
844
             * 
845
             * @return action that will be executed when user pressed the <i>cancel</i> button
846
             */
847
            private ActionListener getOKAction() {
848
                    // OK button action
849
                    return new ActionListener() {
850
                            /*
851
                             * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
852
                             */
853
                                public void actionPerformed(ActionEvent e) {
854
                                        closeThis();
855

    
856
                                        try {
857
                                                int type = ((FLyrVect)infoLayerSelected.getLayer()).getShapeType();
858

    
859
                                                switch (type) {
860
                                                        case FShape.POINT: case FShape.MULTIPOINT:
861
                                                                getQuickInfoListener().setPixelTolerance(20);
862
                                                                break;
863
                                                        default:
864
                                                                getQuickInfoListener().setPixelTolerance(QuickInfoListener.DEFAULT_PIXEL_TOLERANCE);
865
                                                }
866
                                        }
867
                                        catch (Exception ex) {
868
                                                getQuickInfoListener().setPixelTolerance(15); 
869
                                        }
870

    
871
                                // Quick Information by point
872
                                mapControl.addCombinedTool(new AdaptedMouseMovementBehavior(getQuickInfoListener()));
873
                                }
874
                    };
875
            }
876

    
877
            private QuickInfoListener getQuickInfoListener() {
878
                    if (qIListener == null) {
879
                            qIListener = new QuickInfoListener(mapControl, infoLayerSelected, QuickInfoListener.DEFAULT_PIXEL_TOLERANCE);
880
                    }
881
                    
882
                    return qIListener;
883
            }
884

    
885
            /**
886
             * <p>Create the action that will be executed when user pressed the <i>cancel</i> button.</p>
887
             * 
888
             * @return action that will be executed when user pressed the <i>cancel</i> button
889
             */
890
            private ActionListener getCancelAction() {
891
                    // Cancel button action
892
                    return new ActionListener() {
893
                            /*
894
                             * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
895
                             */
896
                                public void actionPerformed(ActionEvent e) {
897
                                        closeThis();
898
                                }
899
                    };
900
            }
901
    }
902

    
903
    /**
904
     * <p>Wrappers a <code>FLayer</code> field.</code>
905
     * <p>Represents a leaf as a green icon, a complex feature as a red icon, an other kind of nodes with a blue icon.</p>
906
     * 
907
     * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
908
     */
909
    private class FieldNameWrapper extends JPanel {
910
                 private static final long serialVersionUID = -2241805610138310550L;
911
                private String field;
912
            private String[] ancestors;
913
            private boolean hasChildren;
914

    
915
            /**
916
             * <p>Creates a new <code>FieldNameWrapper.</p>
917
             * 
918
             * @param field a layer field to be wrappered
919
             * @param ancestors ancestors of that layer field
920
             * @param hasChildren <code>true</code> if has children; otherwise <code>false</code>
921
             */
922
            public FieldNameWrapper(String field, String[] ancestors, boolean hasChildren) {
923
                    super();
924
                    
925
                    this.field = field;
926
                    this.ancestors = ancestors;
927
                    this.hasChildren = hasChildren;
928
                    setLayout(new SpringLayout());
929
                    
930
                    if ((ancestors != null) && (ancestors.length > 0) && (childIconLabel != null)) {
931
                            for (int i = 0; i < ancestors.length; i++)
932
                                    add(childIconLabel);
933
                    }
934
                                    
935
                    JLabel name = new JLabel(field);
936
                                                        
937
                    if (hasChildren) {
938
                            if (complexIcon != null)
939
                                    name.setIcon(complexIcon);
940
                    }
941
                    else {
942
                            if (leafIcon != null)
943
                                    name.setIcon(leafIcon);
944
                    }
945
                    
946
                    add(name);
947
            }
948

    
949
            /**
950
             * <p>Determines if this field has children.</p>
951
              * <p>This is prepared to <i>complex features</i>, but meanwhile that functionality isn't supported.</p>
952
              * 
953
             * @return <code>true</code> if this field has children; otherwise <code>false</code>
954
             */
955
            public boolean hasChildren() {
956
                    return hasChildren;
957
            }
958

    
959
            /**
960
             * <p>Gets the name of the field wrappered.</p>
961
             * 
962
             * @return name of the field wrappered
963
             */
964
            public String getField() {
965
                    return field;
966
            }
967
            
968
            /**
969
             * <p>Gets the level of this field in the <i>TOC</i> tree.</p>
970
             * <p>This is prepared to <i>complex features</i>, but meanwhile that functionality isn't supported.</p>
971
             * 
972
             * @return level of this layer in the <i>TOC</i> tree, 0 if it's the root
973
             */
974
            public int getLevel() {
975
                    if (ancestors == null)
976
                            return 0;
977
                    else
978
                            return ancestors.length;
979
            }
980

    
981
            /*
982
             * (non-Javadoc)
983
             * @see java.awt.Component#toString()
984
             */
985
            public String toString() {
986
                    return field;
987
            }
988
    }
989

    
990
    /**
991
     * <p>Wrappers a <code>FLayer</code> overwriting the method <code>toString()</code> in order to would
992
     *  return the name of the layer.</code>
993
     * 
994
     * <p>Also displays icons and label</p>
995
     * 
996
     * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
997
     */
998
    private class FLayerWrapper extends JPanel {
999
                 private static final long serialVersionUID = -2392779605211085414L;
1000
                private FLayer layer;
1001
            private int level;
1002
//            JLabel branchIconLabel;
1003
//            private JPanel leftPanel;
1004

    
1005
            /**
1006
             * <p>Creates a new <code>FLayerWrapper</code>.</p>
1007
             * 
1008
             * @param layer the layer to be wrappered
1009
             * @param level the level in the tree of the layer wrappered
1010
             * @param leafIcon icon that represents the leaf
1011
             */
1012
            public FLayerWrapper(FLayer layer, int level,Icon leafIcon) {
1013
                    super();
1014

    
1015
                    this.layer = layer;
1016
                    this.level = level;
1017

    
1018
                    setLayout(new SpringLayout());
1019
                    
1020
                    if ((level > 0) && (bIcon != null)) {
1021
                            for (int i = 0; i < level; i++) {
1022
                                    add(new JLabel(bIcon));
1023
                            }
1024
                    }
1025

    
1026
                    JLabel layerLabel;
1027
                    
1028
                    if (layer.getName() != null)
1029
                            layerLabel = new JLabel(layer.getName());
1030
                    else
1031
                            layerLabel = new JLabel("");
1032
                    
1033
                    if (leafIcon != null)
1034
                            layerLabel.setIcon(leafIcon);
1035

    
1036
                    layerLabel.setFont(new Font("Helvetica", Font.BOLD, 12));
1037
                    
1038
                    add(layerLabel);
1039
                    
1040
                    if (layer instanceof FLyrVect) {
1041
                        JLabel layerTypeLabel; 
1042
                            try {
1043
                                    layerTypeLabel = new JLabel(PluginServices.getText(null, "Type") + ": " + FShapeTypeNames.getFShapeTypeName(((FLyrVect) layer).getShapeType()));
1044
                                } catch (ReadDriverException e) {
1045
                                        layerTypeLabel = new JLabel(PluginServices.getText(null, "Type") + ": " + PluginServices.getText("", "UNKNOWN"));
1046
                                        NotificationManager.showMessageError("Driver exception", e);
1047
                                }
1048

    
1049
                                layerTypeLabel.setFont(new Font("Helvetica", Font.ITALIC, 12));
1050

    
1051
                            add(layerTypeLabel);
1052
                    }
1053
                        
1054
                        doSpringLayout();
1055
                    setPreferredSize(new Dimension(340, 16));
1056
            }
1057

    
1058
            /**
1059
             * <p>Creates the <code>Spring</code> layout of this component.</p>
1060
             */
1061
            private void doSpringLayout() {
1062
                        Component[] components = getComponents();
1063
                SpringLayout layout = (SpringLayout)getLayout();
1064
                Spring xPad = Spring.constant(5);
1065
                Spring ySpring = Spring.constant(0);
1066
                Spring xSpring = xPad;
1067

    
1068
                // Make every component 5 pixels away from the component to its left.
1069
                for (int i = 0; i < components.length; i++) {
1070
                    SpringLayout.Constraints cons = layout.getConstraints(components[i]);
1071
                    cons.setX(xSpring);
1072
                    xSpring = Spring.sum(xPad, cons.getConstraint("East"));
1073

    
1074
                    cons.setY(ySpring);
1075
                }
1076
            }
1077
            
1078
            /**
1079
             * <p>Gets the layer wrappered.</p> 
1080
             * 
1081
             * @return the layer wrappered
1082
             */
1083
            public FLayer getLayer() {
1084
                    return layer;
1085
            }
1086
            
1087
            /**
1088
             * <p>Gets the level in the tree of the layer wrappered.</p> 
1089
             * 
1090
             * @return the level in the tree of the layer wrappered
1091
             */
1092
            public int getLevel() {
1093
                    return level;
1094
            }
1095

    
1096
            /*
1097
             * (non-Javadoc)
1098
             * @see javax.swing.JComponent#setForeground(java.awt.Color)
1099
             */
1100
            public void setForeground(Color fg) {
1101
                    super.setForeground(fg);
1102
               }
1103

    
1104
            /*
1105
             * (non-Javadoc)
1106
             * @see javax.swing.JComponent#setBackground(java.awt.Color)
1107
             */
1108
            public void setBackground(Color bg) {
1109
                    super.setBackground(bg);
1110
            }
1111

    
1112
            /*
1113
             * (non-Javadoc)
1114
             * @see java.lang.Object#toString()
1115
             */
1116
            public String toString() {
1117
                    return layer.getName();
1118
            }
1119
    }
1120

    
1121
    /**
1122
     * <p>Cell renderer of the combo box with information of the visible vector layers in the current active view.</p>
1123
     * 
1124
     * @see ListCellRenderer
1125
     * 
1126
     * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
1127
     */
1128
    private class LayersComboBoxCellRenderer implements ListCellRenderer {
1129
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
1130
                            FLayerWrapper renderer = (FLayerWrapper)value;
1131
                            
1132
                            if (isSelected) {
1133
                                    renderer.setForeground(UIManager.getColor( "ComboBox.selectionForeground" ));
1134
                                    renderer.setBackground(UIManager.getColor( "ComboBox.selectionBackground" ));
1135
                            }
1136
                            else
1137
                                    renderer.setBackground(Color.WHITE);
1138

    
1139
                        return renderer;
1140
            }
1141
    }
1142

    
1143
    /**
1144
     * <p>Cell renderer of the table with information of the fields of a layer.</p>
1145
     * 
1146
     * @see TableCellRenderer
1147
     * 
1148
     * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
1149
     */
1150
    private class FieldNameRenderer implements TableCellRenderer {
1151
            /*
1152
             * (non-Javadoc)
1153
             * @see javax.swing.table.TableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int)
1154
             */
1155
                public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
1156
                        FieldNameWrapper renderer = (FieldNameWrapper)value;
1157
                        
1158
                        if (isSelected) {
1159
                                renderer.setForeground(UIManager.getColor( "Table.selectionForeground" ));
1160
                                renderer.setBackground(UIManager.getColor( "Table.selectionBackground" ));
1161
                        }
1162
                        else
1163
                                renderer.setBackground(Color.WHITE);
1164

    
1165
                    return renderer;
1166
                }
1167
    }
1168
    
1169
    /**
1170
     * <p>Closes this window.</p>
1171
     */
1172
        private void closeThis() {
1173
                PluginServices.getMDIManager().closeWindow(this);
1174
        }
1175

    
1176
        /**
1177
         * <p>Sorts the three columns of the <code>model</code> bearing in mind the <code>ascending</code> order
1178
         *  according the second column, which has the field names.</p>
1179
         * 
1180
         * @param model the model to be sort ordered
1181
         * @param ascending <code>true</code> if it's an ascending order, <code>false</code> if its descending
1182
         */
1183
        public void sortFieldColumns(DefaultTableModel model, boolean ascending) {
1184
          Vector data = model.getDataVector();
1185
          Object[] firstColData = new Object[model.getRowCount()];
1186
      Object[] secondColData = new Object[model.getRowCount()];
1187
      Object[] thirdColData = new Object[model.getRowCount()];
1188
      
1189
      // Copy the column data in an array
1190
      for (int i=0; i<secondColData.length; i++) {
1191
              firstColData[i] = ((Vector)data.get(i)).get(0);
1192
              secondColData[i] = ((Vector)data.get(i)).get(1);
1193
              thirdColData[i] = ((Vector)data.get(i)).get(2);
1194
      }
1195
      
1196
      Vector oldSecondColData = new Vector(Arrays.asList(secondColData));
1197

    
1198
      // Sort the array of column data
1199
      Collections.sort(Arrays.asList(secondColData), new ColumnSorter(ascending, ColumnSorter.WITHOUT_CASE_SENSITIVE));
1200
  
1201
      // Store the previous positions of the other columns
1202
      int index;
1203
      
1204
      for (int i=0; i<secondColData.length; i++) {
1205
              index = oldSecondColData.indexOf(secondColData[i]);
1206

    
1207
              // First Column
1208
              ((Vector)data.get(i)).set(0, firstColData[index]);
1209

    
1210
              // Second Column
1211
              ((Vector)data.get(i)).set(1, secondColData[i]);
1212

    
1213
              // Third Column
1214
              ((Vector)data.get(i)).set(2, thirdColData[index]);
1215
      }
1216

    
1217
      model.fireTableStructureChanged();
1218
      
1219
      getFieldsTable().getColumnModel().getColumn(0).setMaxWidth(15);
1220
      getFieldsTable().getColumnModel().getColumn(1).setCellRenderer(new FieldNameRenderer());
1221
        }
1222

    
1223
        /**
1224
         * <p>Compares two <code>String</code> bearing in mind <i>ascending order</i> and <i>case sensitive</i>.</p>
1225
         * 
1226
         * @see Comparator
1227
         * 
1228
         * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
1229
         */
1230
    public class ColumnSorter implements Comparator {
1231
            public static final boolean ASCENDING = true;
1232
            public static final boolean DESCENDING = false;
1233
            public static final boolean WITH_CASE_SENSITIVE = true;
1234
            public static final boolean WITHOUT_CASE_SENSITIVE = false;
1235
            
1236
        private boolean ascending;
1237
        private boolean caseSensitive;
1238

    
1239
        ColumnSorter(boolean ascending, boolean caseSensitive) {
1240
            this.ascending = ascending;
1241
            this.caseSensitive = caseSensitive;
1242
        }
1243

    
1244
        public int compare(Object a, Object b) {
1245
            // Treat empty strains like nulls
1246
            if (a instanceof String && ((String)a).length() == 0) {
1247
                a = null;
1248
            }
1249
            if (b instanceof String && ((String)b).length() == 0) {
1250
                b = null;
1251
            }
1252
    
1253
            // Sort nulls so they appear last, regardless
1254
            // of sort order
1255
            if (a == null && b == null) {
1256
                return 0;
1257
            } else if (a == null) {
1258
                return 1;
1259
            } else if (b == null) {
1260
                return -1;
1261
            } else {
1262
                    if (caseSensitive) {
1263
                            if (ascending)
1264
                                    return a.toString().compareTo(b.toString());
1265
                            else
1266
                                    return b.toString().compareTo(a.toString());
1267
                    }
1268
                    else
1269
                            if (ascending)
1270
                                    return a.toString().toLowerCase().compareTo(b.toString().toLowerCase());
1271
                            else
1272
                                    return b.toString().toLowerCase().compareTo(a.toString().toLowerCase());
1273
            }
1274
        }
1275
    }
1276
    
1277
    /**
1278
     * <p>Inherits from {@link DefaultTableModel DefaultTableModel} adding support for having different
1279
     *  column renderers.</p>
1280
     * 
1281
     * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
1282
     */
1283
    private class LayerTypesTableModel extends DefaultTableModel {
1284
                private static final long serialVersionUID = 6583028930017616307L;
1285

    
1286
                /**
1287
             * <p>Instances <code>LayerTypesTableModel</code>.</p>
1288
             */
1289
            public LayerTypesTableModel() {
1290
                    super();
1291
            }
1292
            
1293
        /*
1294
         * <p>JTable uses this method to determine the default renderer/
1295
         * editor for each cell.  If we didn't implement this method,
1296
         * then the last column would contain text ("true"/"false"),
1297
         * rather than a check box.</p>
1298
         */
1299
        public Class getColumnClass(int c) {
1300
            return getValueAt(0, c).getClass();
1301
        }
1302
    }
1303
    
1304
    /**
1305
     * Process that initializes this component, loading the fields of all visible vector layers in the
1306
     *  current active view. 
1307
     *
1308
     * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
1309
     */
1310
    private class InitQIPanelProcess extends IncrementableProcess {
1311
            private QuickInfoDataSelectionPanel qIPanel                 = null;
1312

    
1313
//            protected ActionListener      openWindowListener  = null; 
1314

    
1315
            /**
1316
             * Creates a new <p>InitQIPanelProcess</p>.
1317
             * 
1318
             * @param title title for the dialog that displays the evolution of the importation
1319
             * @param label brief of this process, that will be displayed in the dialog
1320
             * @param qIPanel reference to the panel that will initialize
1321
             */
1322
            public InitQIPanelProcess(String title, String label, QuickInfoDataSelectionPanel qIPanel) {
1323
                    super(title);
1324

    
1325
                    this.label = label;
1326
                    this.qIPanel = qIPanel;
1327
                    this.isPausable = true;
1328
            }
1329

    
1330
            /**
1331
             * Sets the object that will display the evolution of this loading process as a progress dialog.
1332
             * 
1333
             * @param iTask the object that will display the evolution of this loading process
1334
             */
1335
            public void setIncrementableTask(IncrementableTask iTask) {
1336
                    this.iTask = iTask;
1337
                    iTask.setAskCancel(true);
1338
                    iTask.getButtonsPanel().addAccept();
1339
                    iTask.getButtonsPanel().setEnabled(ButtonsPanel.BUTTON_ACCEPT, false);
1340

    
1341
                    JButton jButton = iTask.getButtonsPanel().getButton(ButtonsPanel.BUTTON_ACCEPT);
1342
                    jButton.addMouseListener(new MouseAdapter() {
1343
                            /*
1344
                             * (non-Javadoc)
1345
                             * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
1346
                             */
1347
                            public void mouseClicked(MouseEvent e) {
1348
                                    processFinalize();
1349
                            }
1350
                    });
1351
            }
1352

    
1353
            /*
1354
             * (non-Javadoc)
1355
             * @see java.lang.Runnable#run()
1356
             */
1357
            public synchronized void run() {
1358
                    String text = null;
1359

    
1360
                    try {
1361
                            percentage = 5;
1362
                            qIPanel.setVisible(false);
1363
                            process();
1364
                            while (! ended) {
1365
                                    t0 += 500;
1366
                        Thread.currentThread().sleep(150);
1367
                            }
1368
                            log.addLine(PluginServices.getText(null, "Load_successful"));
1369
                    } catch (InterruptedException ie) {
1370
                            PluginServices.getLogger().error(ie);
1371
                            label = PluginServices.getText(null, "Process_canceled");
1372
                            text = PluginServices.getText(null, "Failed_the_load");
1373
                    }
1374
                    finally {
1375
                            iTask.setAskCancel(false);
1376
                            iTask.getButtonsPanel().setEnabled(ButtonsPanel.BUTTON_ACCEPT, true);
1377
                            iTask.getButtonsPanel().setEnabled(ButtonsPanel.BUTTON_CANCEL, false);
1378
                            ended = true;
1379

    
1380
                            if (text != null) {
1381
                                    log.addLine(PluginServices.getText(null, "Percent_of_load") + ": " + getPercent());
1382
                                    log.addLine(text);
1383
                                    JOptionPane.showMessageDialog(iTask.getButtonsPanel(), text, PluginServices.getText(this, "Error"), JOptionPane.ERROR_MESSAGE);
1384
                            }
1385
                            
1386
                            if (percentage == 100) {
1387
                                    iTask.getProgressPanel().setPercent(100); // Forces setting the progress bar at 100 %
1388
                                    iTask.getProgressPanel().setLabel(PluginServices.getText(null, "Process_finished"));
1389
                                    iTask.hideWindow();
1390
                            }
1391

    
1392
                            iTask.stop();
1393
                    }
1394
            }
1395

    
1396
            /**
1397
             * Importation process.
1398
             * 
1399
             * @throws InterruptedException any exception produced initializing this component
1400
             */
1401
            public void process() throws InterruptedException {
1402
//                    GeoVisorProject project = null;
1403

    
1404
                    try {
1405
                            setLayout(new FlowLayout());
1406
                            
1407
                            bIcon = PluginServices.getIconTheme().get("layerGroup");
1408
//                            if (bIcon != null)
1409
//                                    bIconLabel = new JLabel(bIcon);
1410

    
1411
                            leafIcon = PluginServices.getIconTheme().get("field-leaf-icon");
1412
                            childIcon = PluginServices.getIconTheme().get("field-child-icon");
1413
                            if (childIcon != null)
1414
                                    childIconLabel = new JLabel(childIcon);
1415

    
1416
                            complexIcon = PluginServices.getIconTheme().get("field-complex-icon");
1417
                            percentage = 15;
1418

    
1419
                        log.addLine(PluginServices.getText(null, "Layers_fields_loaded_successfully"));
1420
                            percentage = 60;
1421
                            add(getNorthPanel());
1422

    
1423
                            add(getAdaptedAcceptCancelPanel());
1424
                            percentage = 65;
1425
                            acceptCancelPanel.setOkButtonEnabled(false);
1426

    
1427
                        refreshVisibleVectorLayers();
1428
                            percentage = 95;         
1429
                            log.addLine(PluginServices.getText(null, "Optional_layers_fields_loaded_successfully"));
1430

    
1431
                            percentage = 100;
1432
                    }
1433
                    catch (Exception e) {
1434
                            PluginServices.getLogger().error(PluginServices.getText(null, "Failed_loding_quick_info_tool_data_selection_panel"), e);
1435

    
1436
//                            log.addLine(PluginServices.getText(null, "Failed_loading_the_project"));
1437
//                            ended = true;
1438
                            throw new InterruptedException();
1439
                    }
1440
            }
1441
    //
1442
//            public void setOpenWindowListener(ActionListener openWindowListener) {
1443
//                    this.openWindowListener = openWindowListener;
1444
//            }
1445

    
1446
            /*
1447
             * (non-Javadoc)
1448
             * @see org.gvsig.gui.beans.incrementabletask.IncrementableListener#actionCanceled(org.gvsig.gui.beans.incrementabletask.IncrementableEvent)
1449
             */
1450
            public void actionCanceled(IncrementableEvent e) {
1451
                    if (percentage < 100) {
1452
                               ended = true;
1453
                        blinker.interrupt();
1454
                        
1455
                        // Forces to cancel
1456
                        cancelled = true;
1457

    
1458
                        log.addLine(PluginServices.getText(null, "Process_cancelled"));
1459
                       }
1460
                    else {
1461
                            JOptionPane.showMessageDialog(null, PluginServices.getText(this, "Process_finished_wont_be_cancelled"), PluginServices.getText(this, "Warning"), JOptionPane.WARNING_MESSAGE);
1462
                    }
1463
            }
1464
            
1465
            /*
1466
             * (non-Javadoc)
1467
             * @see org.gvsig.gui.beans.incrementabletask.IncrementableListener#actionResumed(org.gvsig.gui.beans.incrementabletask.IncrementableEvent)
1468
             */
1469
            public void actionResumed(IncrementableEvent e) {
1470
            }
1471
            
1472
            /*
1473
             * (non-Javadoc)
1474
             * @see org.gvsig.gui.beans.incrementabletask.IncrementableListener#actionSuspended(org.gvsig.gui.beans.incrementabletask.IncrementableEvent)
1475
             */
1476
            public void actionSuspended(IncrementableEvent e) {
1477
            }
1478
    }
1479
    
1480
    /**
1481
     * As combined tool, in a CompoundBehavior its "paintComponent" method is always called. The idea is painting nothing to
1482
     * accelerate the MapControl painting process.
1483
     * 
1484
     * @see MouseMovementBehavior
1485
     * 
1486
         * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
1487
     */
1488
    private class AdaptedMouseMovementBehavior extends MouseMovementBehavior {
1489
            /**
1490
             * @see MouseMovementBehavior#MouseMovementBehavior(PointListener)
1491
             */
1492
                public AdaptedMouseMovementBehavior(PointListener mli) {
1493
                        super(mli);
1494
                }
1495
            
1496
                /*
1497
                 * (non-Javadoc)
1498
                 * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#paintComponent(java.awt.Graphics)
1499
                 */
1500
                public void paintComponent(Graphics g) {
1501
                        //super.paintComponent(g);
1502
                }
1503
    }
1504
    
1505
    public static void removeQuickInfoTool(MapControl mapControl) {
1506
            Behavior ctools = mapControl.getCurrentMapTool();
1507
            
1508
            if (ctools instanceof CompoundBehavior) {
1509
                    CompoundBehavior cb_ctools = (CompoundBehavior)ctools;
1510
                    Behavior behavior;
1511
                    
1512
                    for (int i = 0; i < cb_ctools.size(); i++) {
1513
                            behavior = cb_ctools.getBehavior(i);
1514

    
1515
                            if (behavior instanceof AdaptedMouseMovementBehavior) {
1516
                                    mapControl.removeCombinedTool(behavior);
1517
                                    return;
1518
                            }
1519
                            else {
1520
                                    if (behavior instanceof CompoundBehavior) {
1521
                                            if (removeQuickInfoTool(mapControl, (CompoundBehavior)behavior))
1522
                                                    return;
1523
                                    }
1524
                            }
1525
                    }
1526
            }
1527
    }
1528
    
1529
    private static boolean removeQuickInfoTool(MapControl mapControl, CompoundBehavior compoundBehavior) {
1530
            if (compoundBehavior != null) {
1531
                    Behavior behavior;
1532
                    
1533
                    for (int i = 0; i < compoundBehavior.size(); i++) {
1534
                            behavior = compoundBehavior.getBehavior(i);
1535

    
1536
                            if (behavior instanceof AdaptedMouseMovementBehavior) {
1537
                                    mapControl.removeCombinedTool(behavior);
1538
                                    return true;
1539
                            }
1540
                    }
1541
            }
1542
            
1543
            return false;
1544
    }
1545

    
1546
        public Object getWindowProfile() {
1547
                return WindowInfo.DIALOG_PROFILE;
1548
        }
1549
}