Statistics
| Revision:

gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.algorithm / org.gvsig.geoprocess.algorithm.groupby / src / main / java / org / gvsig / geoprocess / algorithm / groupby / GroupByParametersPanel.java @ 385

History | View | Annotate | Download (22.7 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2012 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.geoprocess.algorithm.groupby;
25

    
26
import java.awt.BorderLayout;
27
import java.awt.Color;
28
import java.awt.Dimension;
29
import java.awt.GridBagConstraints;
30
import java.awt.GridBagLayout;
31
import java.awt.Insets;
32
import java.awt.event.ActionEvent;
33
import java.awt.event.ActionListener;
34
import java.awt.event.MouseEvent;
35
import java.awt.event.MouseListener;
36
import java.util.ArrayList;
37
import java.util.List;
38

    
39
import javax.swing.BorderFactory;
40
import javax.swing.ButtonGroup;
41
import javax.swing.ComboBoxModel;
42
import javax.swing.DefaultComboBoxModel;
43
import javax.swing.DefaultListModel;
44
import javax.swing.JButton;
45
import javax.swing.JComboBox;
46
import javax.swing.JLabel;
47
import javax.swing.JList;
48
import javax.swing.JPanel;
49
import javax.swing.JRadioButton;
50
import javax.swing.JScrollPane;
51
import javax.swing.JTable;
52
import javax.swing.ListSelectionModel;
53
import javax.swing.table.DefaultTableModel;
54

    
55
import org.gvsig.fmap.dal.exception.DataException;
56
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
57
import org.gvsig.fmap.dal.feature.FeatureStore;
58
import org.gvsig.fmap.dal.feature.FeatureType;
59
import org.gvsig.geoprocess.lib.api.GeoProcessLocator;
60
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer;
61
import org.gvsig.geoprocess.sextante.gui.algorithm.AlgorithmOutputPanel;
62
import org.gvsig.gui.beans.Messages;
63
import org.gvsig.timesupport.Instant;
64
import org.gvsig.tools.dataTypes.DataTypes;
65

    
66
import es.unex.sextante.core.GeoAlgorithm;
67
import es.unex.sextante.core.ObjectAndDescription;
68
import es.unex.sextante.core.OutputObjectsSet;
69
import es.unex.sextante.core.ParametersSet;
70
import es.unex.sextante.core.Sextante;
71
import es.unex.sextante.dataObjects.IVectorLayer;
72
import es.unex.sextante.gui.algorithm.GeoAlgorithmParametersPanel;
73
import es.unex.sextante.gui.algorithm.OutputChannelSelectionPanel;
74
import es.unex.sextante.gui.core.SextanteGUI;
75
import es.unex.sextante.outputs.Output;
76

    
77
/**
78
 * Panel for dissolve algorithm
79
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
80
 */
81
public class GroupByParametersPanel extends GeoAlgorithmParametersPanel implements ActionListener, MouseListener {
82
        private static final long                serialVersionUID   = 1L;
83
        private GeoAlgorithm                     m_Algorithm        = null;
84
        private JButton                          buttonAdd          = null;
85
        private JButton                          buttonDel          = null;
86
        private JComboBox                        groupingField      = null;
87
        private JComboBox                        timeField          = null;
88
        private JComboBox                        layersCombo        = null;
89
        private JList                            fieldList          = null;
90
        private JList                            operationJList     = null;
91
        private JScrollPane                      jlistScroll1       = null;
92
        private JScrollPane                      jlistScroll2       = null;
93
        private AlgorithmOutputPanel             output             = null;
94
        private String[]                         opList             = null;
95
        private String[]                         opListLast         = null;
96
        private String[]                         opListString       = null;
97
        private String[]                         opListStringLast   = null;
98
        private final String[]                   columnNames        = { Messages.getText("Field"), Messages.getText("Function") };
99
        private JTable                           table              = null;
100
        private JScrollPane                      jtableScroll       = null;
101
        private JRadioButton                     firstGeom          = null;
102
        private JRadioButton                     multiGeom          = null;
103
        private JRadioButton                     spatialFusion      = null;
104
        private OutputChannelSelectionPanel      outputChannelSelectionPanel;
105
        private JPanel                           outputPanel;
106
        
107
        public GroupByParametersPanel() {
108
                super();
109
        }
110

    
111
    public void init(GeoAlgorithm algorithm) {
112
            m_Algorithm        = algorithm;
113
            opList             = new String[]{ "first", "sum", "average", "max", "min"};
114
            opListLast         = new String[]{ "first", "sum", "average", "max", "min", "last"};
115
            opListString       = new String[]{ "first"};
116
            opListStringLast   = new String[]{ "first", "last"};
117
            initGUI();
118
    }
119
    
120
        private void initGUI() {
121
                GridBagLayout gbl = new GridBagLayout();
122
                this.setLayout(gbl);
123
                this.setBorder(BorderFactory.createLineBorder(Color.gray));
124
                
125
                GridBagConstraints gbc = new GridBagConstraints();
126
                gbc.fill = GridBagConstraints.HORIZONTAL;
127
                gbc.weightx = 1.0;
128
                gbc.gridx = 0;
129
                gbc.insets = new Insets(0, 4, 4, 4);
130
                
131
                gbc.gridy = 0;                
132
                this.add(getComboPanel(GeoProcessLocator.getGeoProcessManager().getTranslation("grouping_layer"), getLayers1Combo()), gbc);
133
                
134
                gbc.gridy = 1;                
135
                this.add(getComboPanel(GeoProcessLocator.getGeoProcessManager().getTranslation("grouping_field"), getFields1Combo()), gbc);
136
                
137
                gbc.gridy = 2;
138
                this.add(getComboPanel(GeoProcessLocator.getGeoProcessManager().getTranslation("time_field"), getFields2Combo()), gbc);
139
                
140
                gbc.gridy = 3;
141
                gbc.fill = GridBagConstraints.BOTH;
142
                gbc.weighty = 1.0;
143
                this.add(getTablePanel(), gbc);
144
                
145
                gbc.fill = GridBagConstraints.HORIZONTAL;
146
                gbc.weighty = 0;
147
                gbc.gridy = 4;
148
                this.add(getGeometryPanel(), gbc);
149

    
150
                gbc.gridy = 5;
151
                this.add(getOutputChannelSelectionPanel(), gbc);
152
        }
153
        
154
        /**
155
         * Gets the output panel (SEXTANTE)
156
         * @return
157
         */
158
        private JPanel getOutputChannelSelectionPanel() {
159
                if(outputPanel == null) {
160
                        try {
161
                                outputPanel = new JPanel();
162
                                outputPanel.setLayout(new BorderLayout());
163
                                final OutputObjectsSet ooSet = m_Algorithm.getOutputObjects();
164
                                final Output out = ooSet.getOutput(GroupByAlgorithm.RESULT);
165
                                outputChannelSelectionPanel = new OutputChannelSelectionPanel(out, m_Algorithm.getParameters());
166
                                outputPanel.add(new JLabel("GroupBy [Vectorial]                        "), BorderLayout.WEST);
167
                                outputPanel.add(outputChannelSelectionPanel, BorderLayout.CENTER);
168
                        } catch (final Exception e) {
169
                                Sextante.addErrorToLog(e);
170
                        }
171
                }
172
                return outputPanel;
173
        }
174
        
175
        /**
176
         * Gets the output panel (DAL)
177
         * @return
178
         */
179
        @SuppressWarnings("unused")
180
        private AlgorithmOutputPanel getAlgorithmOutputPanel() {
181
                if(output == null) {
182
                        output = new AlgorithmOutputPanel();
183
                }
184
                return output;
185
        }
186
        
187
        /**
188
         * Gets a new JPanel with the text and JComboBox 
189
         * @param text
190
         * @param combo
191
         * @return
192
         */
193
        public JPanel getComboPanel(String text, JComboBox combo) {
194
                JPanel panel = new JPanel();
195
                GridBagLayout gbl = new GridBagLayout();
196
                panel.setLayout(gbl);
197

    
198
                GridBagConstraints gbc = new GridBagConstraints();
199
                gbc.fill = GridBagConstraints.NONE;
200
                gbc.weightx = 0;
201
                gbc.gridx = 0;
202
                gbc.insets = new Insets(0, 2, 0, 5);
203
                JLabel label = new JLabel(text);
204
                label.setPreferredSize(new Dimension(180, 18));
205
                panel.add(label, gbc);
206

    
207
                gbc.fill = GridBagConstraints.HORIZONTAL;
208
                gbc.weightx = 1.0;
209
                gbc.gridx = 1;
210
                gbc.anchor = GridBagConstraints.EAST;
211
                gbc.insets = new Insets(0, 2, 0, 0);
212
                panel.add(combo, gbc);
213
                return panel;
214
        }
215
        
216
        /**
217
         * Gets a ComboBox
218
         * @return
219
         */
220
        public JComboBox getFields1Combo() {
221
                if(groupingField == null) {
222
                        groupingField = new JComboBox();
223
                        groupingField.setPreferredSize(new Dimension(0, 18));
224
                        ComboBoxModel comboModel = new DefaultComboBoxModel(getFieldList().toArray());
225
                        groupingField.setModel(comboModel);
226
                        groupingField.addActionListener(this);
227
                }
228
                return groupingField;
229
        }
230
        
231
        /**
232
         * Gets a ComboBox
233
         * @return
234
         */
235
        public JComboBox getFields2Combo() {
236
                if(timeField == null) {
237
                        timeField = new JComboBox();
238
                        timeField.setPreferredSize(new Dimension(0, 18));
239
                        ComboBoxModel comboModel = new DefaultComboBoxModel(getTimeFieldList().toArray());
240
                        timeField.setModel(comboModel);
241
                        timeField.addActionListener(this);
242
                }
243
                return timeField;
244
        }
245
        
246
        public JPanel getTablePanel() {
247
                JPanel p = new JPanel();
248
                p.setLayout(new GridBagLayout());
249
                
250
                GridBagConstraints gbc = new GridBagConstraints();
251
                gbc.fill = GridBagConstraints.BOTH;
252
                gbc.weighty = 1.0;
253
                gbc.weightx = 1.0;
254
                gbc.gridx = 0;
255
                p.add(getJList(), gbc);
256
                
257
                gbc.gridx = 2;
258
                p.add(getJTable(), gbc);
259
                
260
                gbc.gridx = 3;
261
                p.add(getOperationJList(), gbc);
262
                
263
                gbc.fill = GridBagConstraints.VERTICAL;
264
                gbc.weightx = 0;
265
                gbc.gridx = 1;
266
                p.add(getButtonsPanel(), gbc);
267
                
268
                return p;
269
        }
270
        
271
        /**
272
         * Gets a ComboBox
273
         * @return
274
         */
275
        public JComboBox getLayers1Combo() {
276
                if(layersCombo == null) {
277
                        layersCombo = new JComboBox();
278
                        layersCombo.setPreferredSize(new Dimension(0, 18));
279
                        ComboBoxModel comboModel = new DefaultComboBoxModel(getLayerList());
280
                        layersCombo.setModel(comboModel);
281
                        layersCombo.addActionListener(this);
282
                }
283
                return layersCombo;
284
        }
285
        
286
        private JPanel getButtonsPanel() {
287
                JPanel p = new JPanel();
288
                p.setLayout(new GridBagLayout());
289
                GridBagConstraints gbc = new GridBagConstraints();
290
                gbc.fill = GridBagConstraints.NONE;
291
                gbc.gridx = 0;
292
                gbc.gridy = 0;
293
                p.add(getButtonAdd(), gbc);
294
                gbc.gridx = 0;
295
                gbc.gridy = 1;
296
                p.add(getButtonDel(), gbc);
297
                return p;
298
        }
299
        
300
        public JButton getButtonAdd() {
301
                if(buttonAdd == null) {
302
                        buttonAdd = new JButton(">>");
303
                        buttonAdd.setPreferredSize(new Dimension(32, 32));
304
                        buttonAdd.addActionListener(this);
305
                }
306
                return buttonAdd;
307
        }
308
        
309
        public JButton getButtonDel() {
310
                if(buttonDel == null) {
311
                        buttonDel = new JButton("<<");
312
                        buttonDel.setPreferredSize(new Dimension(32, 32));
313
                        buttonDel.addActionListener(this);
314
                }
315
                return buttonDel;
316
        }
317

    
318
        public JScrollPane getJTable() {
319
                if (table == null) {
320
                        DefaultTableModel model = new DefaultTableModel();
321
                        model.setColumnIdentifiers(columnNames);
322
                        table = new JTable(model);
323
                        table.addMouseListener(this);
324
                        table.setRowHeight(24);
325
                        jtableScroll = new JScrollPane(table);
326
                }
327
                return jtableScroll;
328
        }
329
        
330

    
331
        public JScrollPane getJList() {
332
                if (fieldList == null) {
333
                        fieldList = new JList();
334
                        fieldList.setModel(new DefaultListModel());
335
                        jlistScroll1 = new JScrollPane(fieldList);
336
                        fieldList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
337
                        fieldList.addMouseListener(this);
338
                        List<String> list = getFieldList();
339
                        for (int i = 0; i < list.size(); i++) {
340
                                ((DefaultListModel)this.fieldList.getModel()).addElement(list.get(i));
341
                        }        
342
                }
343
                return jlistScroll1;
344
        }
345
        
346
        private JPanel panelOpList = null;
347
        public JPanel getOperationJList() {
348
                if (operationJList == null) {
349
                        operationJList = new JList();
350
                        operationJList.setModel(new DefaultListModel());
351
                        operationJList.setPreferredSize(new Dimension(70, 0));
352
                        jlistScroll2 = new JScrollPane(operationJList);
353
                        operationJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
354
                        operationJList.addMouseListener(this);
355
                        panelOpList = new JPanel(new GridBagLayout());
356
                        GridBagConstraints gbc = new GridBagConstraints();
357
                        gbc.fill = GridBagConstraints.VERTICAL;
358
                        gbc.weighty = 1;
359
                        panelOpList.add(operationJList, gbc);
360
                        panelOpList.setBorder(BorderFactory.createLineBorder(Color.black));
361
                }
362
                return panelOpList;
363
        }
364
        
365
        /**
366
         * Gets a new JPanel with the text and JComboBox 
367
         * @param text
368
         * @param combo
369
         * @return
370
         */
371
        public JPanel getGeometryPanel() {
372
                ButtonGroup group = new ButtonGroup();
373
                group.add(getButtonFirstGeom());
374
                group.add(getButtonMultiGeom());
375
                group.add(getButtonSpatialFusion());
376
                
377
                JPanel panel = new JPanel();
378
                String text = GeoProcessLocator.getGeoProcessManager().getTranslation("geom");
379
                panel.setBorder(BorderFactory.createTitledBorder(text));
380
                GridBagLayout gbl = new GridBagLayout();
381
                panel.setLayout(gbl);
382

    
383
                GridBagConstraints gbc = new GridBagConstraints();
384
                gbc.fill = GridBagConstraints.HORIZONTAL;
385
                gbc.weightx = 1.0;
386
                gbc.insets = new Insets(0, 2, 4, 0);
387
                panel.add(getButtonFirstGeom(), gbc);
388
                
389
                gbc.gridy = 1;
390
                panel.add(getButtonMultiGeom(), gbc);
391
                
392
                gbc.gridy = 2;
393
                panel.add(getButtonSpatialFusion(), gbc);
394
                
395
                return panel;
396
        }
397
        
398
        private JRadioButton getButtonFirstGeom() {
399
                if(firstGeom == null) {
400
                        String text = GeoProcessLocator.getGeoProcessManager().getTranslation("first_geom");
401
                        firstGeom = new JRadioButton(text);
402
                }
403
                return firstGeom;
404
        }
405
        
406
        private JRadioButton getButtonMultiGeom() {
407
                if(multiGeom == null) {
408
                        String text = GeoProcessLocator.getGeoProcessManager().getTranslation("multi_geom");
409
                        multiGeom = new JRadioButton(text);
410
                }
411
                return multiGeom;
412
        }
413
        
414
        private JRadioButton getButtonSpatialFusion() {
415
                if(spatialFusion == null) {
416
                        String text = GeoProcessLocator.getGeoProcessManager().getTranslation("spatial_fusion");
417
                        spatialFusion = new JRadioButton(text);
418
                }
419
                return spatialFusion;
420
        }
421
        
422
        
423
        //------------------------------------------------------------
424
        
425
        private void reloadFields() {
426
                for (int i = ((DefaultTableModel)table.getModel()).getRowCount() - 1; i >= 0 ; i--) {
427
                        ((DefaultTableModel)table.getModel()).removeRow(i);
428
                }
429
                List<String> list = getFieldList();
430
                groupingField.removeAllItems();
431
                ((DefaultListModel)this.fieldList.getModel()).clear();
432
                for (int i = 0; i < list.size(); i++) {
433
                        ((DefaultListModel)this.fieldList.getModel()).addElement(list.get(i));
434
                        groupingField.addItem(list.get(i));
435
                }        
436
                
437
                DefaultTableModel model = new DefaultTableModel();
438
                model.setColumnIdentifiers(columnNames);
439
                table = new JTable(model);
440
                table.setRowHeight(24);
441
                
442
                getFields2Combo().removeAllItems();
443
                List<String> timeFieldList = getTimeFieldList();
444
                for (int i = 0; i < timeFieldList.size(); i++) {
445
                        getFields2Combo().addItem(timeFieldList.get(i));
446
                }
447
                
448
                IVectorLayer layer = getSelectedVectorLayer();
449
                if(layer.getShapeType() != IVectorLayer.SHAPE_TYPE_POLYGON)
450
                        getButtonSpatialFusion().setEnabled(false);
451
                else
452
                        getButtonSpatialFusion().setEnabled(true);
453
        }
454
        
455
        /**
456
         * Gets the selected vector layer in the JComboBox
457
         * @return
458
         */
459
        private IVectorLayer getSelectedVectorLayer() {
460
                if(layersCombo.getSelectedItem() != null)
461
                        return (IVectorLayer)((ObjectAndDescription)layersCombo.getSelectedItem()).getObject();
462
                return null;
463
        }
464
        
465
        /*
466
         * (non-Javadoc)
467
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
468
         */
469
        public void actionPerformed(ActionEvent e) {
470
                if(e.getSource() == getLayers1Combo()) {
471
                        reloadFields();
472
                }
473
                
474
                if(e.getSource() == getButtonAdd()) {
475
                        addRowToTable();
476
                }
477
                
478
                if(e.getSource() == getButtonDel()) {
479
                        removeRowTable();
480
                }
481
        }
482

    
483
        
484
        @Override
485
        public void assignParameters() {
486
                try {
487
                        ParametersSet params = m_Algorithm.getParameters();
488
                        params.getParameter(GroupByAlgorithm.LAYER_NAME).setParameterValue("GroupBy_Layer");
489
                        params.getParameter(GroupByAlgorithm.LAYER).setParameterValue(getSelectedVectorLayer());
490
                        params.getParameter(GroupByAlgorithm.FIELD).setParameterValue((String)getFields1Combo().getSelectedItem());
491
                        //List<String> fList = getFieldList();
492
                        String dateField = (String)getFields2Combo().getSelectedItem();
493
                        /*int dateFieldPosition = -1;
494
                        if(getFields2Combo().getItemCount() > 0) {
495
                                for (int i = 0; i < fList.size(); i++) {
496
                                        if(fList.get(i).equals(dateField))
497
                                                dateFieldPosition = i;
498
                                }
499
                        }*/
500
                        params.getParameter(GroupByAlgorithm.FIELD_DATE).setParameterValue(dateField);
501
                        params.getParameter(GroupByAlgorithm.FIELD_LIST).setParameterValue(getFieldStringList());
502
                        params.getParameter(GroupByAlgorithm.FUNCTION_LIST).setParameterValue(getFunctionStringList());
503
                        int geom = getButtonFirstGeom().isSelected() ? 0 : getButtonMultiGeom().isSelected() ? 1 : getButtonSpatialFusion().isSelected() ? 2 : -1;
504
                        params.getParameter(GroupByAlgorithm.GEOM_OPTION).setParameterValue(geom);
505
                        
506
                        OutputObjectsSet ooSet = m_Algorithm.getOutputObjects();
507
                        Output out = ooSet.getOutput(GroupByAlgorithm.RESULT);
508
                        
509
                        //Reponer estas l?neas para cambiar el panel de salida y comentar la siguiente
510
                        //AlgorithmOutputPanel fsp = getAlgorithmOutputPanel();
511
                        //out.setOutputChannel(new CompositeSourceOutputChannel(fsp.getOutputParameters()));
512
                 out.setOutputChannel(outputChannelSelectionPanel.getOutputChannel());
513
                } catch (Exception e) {
514
                        Sextante.addErrorToLog(e);
515
        }
516
        }
517

    
518
        private String getFunctionStringList() {
519
                if(table != null) {
520
                        String value = "";
521
                        for (int i = 0; i < ((DefaultTableModel)table.getModel()).getRowCount(); i++) {
522
                                Object obj = ((DefaultTableModel)table.getModel()).getValueAt(i, 1);
523
                                if(i == 0)
524
                                        value = obj + "";
525
                                else
526
                                        value += ";" + obj;
527
                        }
528
                        return value;
529
                }
530
                return null;
531
        }
532

    
533
        private String getFieldStringList() {
534
                if(table != null) {
535
                        String value = "";
536
                        for (int i = 0; i < ((DefaultTableModel)table.getModel()).getRowCount(); i++) {
537
                                Object obj = ((DefaultTableModel)table.getModel()).getValueAt(i, 0);
538
                                if(obj instanceof String) {
539
                                        List<String> fList = getFieldList();
540
                                        for (int j = 0; j < fList.size(); j++) {
541
                                                if(fList.get(j).compareTo((String)obj) == 0) {
542
                                                        if(value.compareTo("") == 0)
543
                                                                value = obj + "";
544
                                                        else
545
                                                                value += ";" + obj;
546
                                                }
547
                                        }
548
                                }
549
                        }
550
                        return value;
551
                }
552
                return null;
553
        }
554

    
555
        @Override
556
        public void setOutputValue(String arg0, String arg1) {
557
                
558
        }
559

    
560
        @Override
561
        public void setParameterValue(String arg0, String arg1) {
562
                
563
        }
564
        ;
565
        /**
566
         * Gets the input layer list
567
         * @return
568
         */
569
        private ObjectAndDescription[] getLayerList() {
570
                IVectorLayer[] layers = SextanteGUI.getInputFactory()
571
                                        .getVectorLayers(IVectorLayer.SHAPE_TYPE_WRONG);
572
                ObjectAndDescription[] oad = new ObjectAndDescription[layers.length];
573
                for (int i = 0; i < layers.length; i++)
574
                        oad[i] = new ObjectAndDescription(layers[i].getName(), layers[i]);
575
                return oad;
576
        }
577
        
578
        /**
579
         * Gets the field list of the selected layer
580
         * @return
581
         */
582
        public List<String> getFieldList() {
583
                IVectorLayer layer = getSelectedVectorLayer();
584
                List<String> data = new ArrayList<String>();
585
                FeatureStore fs = ((FlyrVectIVectorLayer)layer).getFeatureStore();
586
                for (int i = 0; i < layer.getFieldCount(); i++) {
587
                        FeatureType ftype;
588
                        try {
589
                                ftype = fs.getDefaultFeatureType();
590
                        } catch (DataException e) {
591
                                return data;
592
                        }
593
                        if(ftype.getAttributeDescriptor(i).getDataType().isNumeric() ||
594
                           ftype.getAttributeDescriptor(i).getDataType().getType() == DataTypes.STRING)
595
                        data.add(layer.getFieldName(i)); 
596
                }
597
                return data;
598
        }
599
        
600
        /**
601
         * Gets the field list of the selected layer of time data type
602
         * @return
603
         */
604
        @SuppressWarnings("unchecked")
605
        public List<String> getTimeFieldList() {
606
                IVectorLayer layer = getSelectedVectorLayer();
607
                List<String> data = new ArrayList<String>();
608
                for (int i = 0; i < layer.getFieldCount(); i++) {
609
                        Class type = layer.getFieldType(i);
610
                        if(Instant.class.isAssignableFrom(type))
611
                                data.add(layer.getFieldName(i));
612
                }
613
                return data;
614
        }
615

    
616
        private void addRowToTable() {
617
                Object[] values = fieldList.getSelectedValues();
618
                for (int i = 0; i < values.length; i++) {
619
                        Object[] obj = new Object[]{values[i], opList[0]};
620
                        ((DefaultTableModel)table.getModel()).addRow(obj);
621
                }        
622
                //Carga la lista de operaciones
623
                loadOperationInJList(getFieldSelectedInTable());
624
        }
625
        
626
        private void removeRowTable() {
627
                int row = table.getSelectedRow();
628
                if(row >= 0 && row < table.getRowCount()) {
629
                        ((DefaultTableModel)table.getModel()).removeRow(row);
630
                        int rowSelected = ((DefaultTableModel)table.getModel()).getRowCount() - 1;
631
                        if(rowSelected >= 0) {
632
                                table.setRowSelectionInterval(rowSelected, rowSelected);
633
                        }
634
                        loadOperationInJList(getFieldSelectedInTable());
635
                }
636
        }
637
        
638
        public void mouseClicked(MouseEvent e) {
639
                if(e.getSource() == fieldList) {
640
                        if(e.getClickCount() == 2) {
641
                                addRowToTable();
642
                        }
643
                }
644
                if(e.getSource() == table) {
645
                        loadOperationInJList(getFieldSelectedInTable());
646
                }
647
                if(e.getSource() == operationJList) {
648
                        changeFunctionInTable();
649
                }
650
         }
651
        
652
        private void changeFunctionInTable() {
653
                int rowSelected = table.getSelectedRow();
654
                if(rowSelected < 0) {
655
                        return;
656
                }
657
                String value = (String)operationJList.getSelectedValue();
658
                ((DefaultTableModel)table.getModel()).setValueAt(value, rowSelected, 1);
659
        }
660
        
661
        private String getFieldSelectedInTable() {
662
                if(table.getRowCount() == 0)
663
                        return null;
664
                int rowSelected = table.getSelectedRow();
665
                if(rowSelected < 0) {
666
                        rowSelected = ((DefaultTableModel)table.getModel()).getRowCount() - 1;
667
                        table.setRowSelectionInterval(rowSelected, rowSelected);
668
                }
669
                return (String)((DefaultTableModel)table.getModel()).getValueAt(rowSelected, 0);
670
        }
671
        
672
        private void loadOperationInJList(String field) {
673
                operationJList.setModel(new DefaultListModel());
674
                DefaultListModel model = (DefaultListModel)operationJList.getModel();
675
                
676
                if(field == null)
677
                        return;
678
                
679
                if(existsTimeField()) {
680
                        if(isFieldNumeric(field)) {
681
                                for (int i = 0; i < opListLast.length; i++) {
682
                                        model.addElement(opListLast[i]);                                
683
                                }
684
                        } else {
685
                                for (int i = 0; i < opListStringLast.length; i++) {
686
                                        model.addElement(opListStringLast[i]);                                
687
                                }
688
                        }                                
689
                } else {
690
                        if(isFieldNumeric(field)) {
691
                                for (int i = 0; i < opList.length; i++) {
692
                                        model.addElement(opList[i]);                                
693
                                }
694
                        } else {
695
                                for (int i = 0; i < opListString.length; i++) {
696
                                        model.addElement(opListString[i]);                                
697
                                }
698
                        }        
699
                }
700
        }
701
        
702
        public boolean isFieldNumeric(String field) {
703
                IVectorLayer layer = getSelectedVectorLayer();
704
                FeatureStore fs = ((FlyrVectIVectorLayer)layer).getFeatureStore();
705
                FeatureType ftype;
706
                try {
707
                        ftype = fs.getDefaultFeatureType();
708
                        FeatureAttributeDescriptor desc = ftype.getAttributeDescriptor(field);
709
                        if(desc != null && desc.getDataType().isNumeric())
710
                                return true;
711
                } catch (DataException e) {
712
                        return false;
713
                }
714
                return false;
715
        }
716
        
717
        @SuppressWarnings("unchecked")
718
        private boolean existsTimeField() {
719
                IVectorLayer layer = getSelectedVectorLayer();
720
                if(layer != null ) {
721
                        boolean existsTimeField = false;
722
                        for (int i = 0; i < layer.getFieldCount(); i++) {
723
                                Class type = layer.getFieldType(i);
724
                                if(Instant.class.isAssignableFrom(type))
725
                                        existsTimeField = true;
726
                        }
727

    
728
                        if(layer.getShapeType() != IVectorLayer.SHAPE_TYPE_POLYGON)
729
                                getButtonSpatialFusion().setEnabled(false);
730

    
731
                        return existsTimeField;
732
                }
733
                return false;
734
        }
735

    
736
        public void mouseEntered(MouseEvent e) {
737
        }
738

    
739
        public void mouseExited(MouseEvent e) {
740
        }
741

    
742
        public void mousePressed(MouseEvent e) {
743
        }
744

    
745
        public void mouseReleased(MouseEvent e) {
746
        }
747

    
748
}