Statistics
| Revision:

gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.algorithm / org.gvsig.geoprocess.algorithm.spatialjoin / src / main / java / org / gvsig / geoprocess / algorithm / spatialjoin / SpatialJoinParametersPanel.java @ 336

History | View | Annotate | Download (13.5 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.spatialjoin;
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

    
35
import javax.swing.BorderFactory;
36
import javax.swing.ComboBoxModel;
37
import javax.swing.DefaultComboBoxModel;
38
import javax.swing.JCheckBox;
39
import javax.swing.JComboBox;
40
import javax.swing.JLabel;
41
import javax.swing.JPanel;
42

    
43
import org.gvsig.geoprocess.lib.api.GeoProcessLocator;
44
import org.gvsig.geoprocess.sextante.gui.algorithm.AlgorithmOutputPanel;
45
import org.gvsig.gui.beans.table.TableContainer;
46
import org.gvsig.gui.beans.table.exceptions.NotInitializeException;
47

    
48
import es.unex.sextante.core.GeoAlgorithm;
49
import es.unex.sextante.core.ObjectAndDescription;
50
import es.unex.sextante.core.OutputObjectsSet;
51
import es.unex.sextante.core.ParametersSet;
52
import es.unex.sextante.core.Sextante;
53
import es.unex.sextante.dataObjects.IVectorLayer;
54
import es.unex.sextante.gridCalculus.gridCalculator.GridCalculatorAlgorithm;
55
import es.unex.sextante.gui.algorithm.GeoAlgorithmParametersPanel;
56
import es.unex.sextante.gui.algorithm.OutputChannelSelectionPanel;
57
import es.unex.sextante.gui.core.SextanteGUI;
58
import es.unex.sextante.outputs.Output;
59

    
60
/**
61
 * Panel for dissolve algorithm
62
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
63
 */
64
public class SpatialJoinParametersPanel extends GeoAlgorithmParametersPanel implements ActionListener {
65
        private static final long                serialVersionUID   = 1L;
66
        private GeoAlgorithm                     m_Algorithm        = null;
67
        private JComboBox                        layersCombo        = null;
68
        private JComboBox                        layersJoinCombo    = null;
69
        private JCheckBox                        selectionInput     = null;
70
        private JCheckBox                        selectionOverlay   = null;
71
        private JCheckBox                        nearest       = null;
72
        private AlgorithmOutputPanel             output             = null;
73
        private final String[]                   columnNames        = { "Min", "Max", "Sum", "Avg", "Field ID" };
74
        private final int[]                      columnWidths       = { 35, 35, 35, 35, 334 };
75
        private TableContainer                   table              = null;
76
        private OutputChannelSelectionPanel      outputChannelSelectionPanel;
77
        private JPanel                           outputPanel;
78
        
79
        public SpatialJoinParametersPanel() {
80
                super();
81
        }
82

    
83
    public void init(GeoAlgorithm algorithm) {
84
            m_Algorithm = algorithm;
85
            initGUI();
86
    }
87

    
88
        private void initGUI() {
89
                GridBagLayout gbl = new GridBagLayout();
90
                this.setLayout(gbl);
91
                this.setBorder(BorderFactory.createLineBorder(Color.gray));
92
                
93
                GridBagConstraints gbc = new GridBagConstraints();
94
                gbc.fill = GridBagConstraints.HORIZONTAL;
95
                gbc.weightx = 1.0;
96
                gbc.gridx = 0;
97
                gbc.gridy = 0;
98
                gbc.insets = new Insets(0, 0, 8, 0);
99
                this.add(getComboPanel(GeoProcessLocator.getGeoProcessManager().getTranslation("input_layers"), getLayers1Combo()), gbc);
100
                
101
                gbc.gridy = 1;
102
                this.add(getComboPanel(GeoProcessLocator.getGeoProcessManager().getTranslation("input_layers_join"), getLayers2Combo()), gbc);
103
                
104
                gbc.gridy = 2;
105
                this.add(getSelectionInputCheck(), gbc);
106
                
107
                gbc.gridy = 3;
108
                this.add(getSelectionOverlayCheck(), gbc);
109
                
110
                gbc.gridy = 4;
111
                this.add(getNearestCheck(), gbc);
112
                
113
                gbc.gridy = 5;
114
                this.add(new JLabel(GeoProcessLocator.getGeoProcessManager().getTranslation("summary_function")), gbc);
115
                
116
                gbc.gridy = 6;
117
                gbc.fill = GridBagConstraints.BOTH;
118
                gbc.insets = new Insets(0, 0, 12, 0);
119
                gbc.weighty = 1.0;
120
                this.add(getRadioButtonTable(), gbc);
121

    
122
                gbc.gridy = 7;
123
                gbc.fill = GridBagConstraints.HORIZONTAL;
124
                gbc.weighty = 0.0;
125
                this.add(getOutputChannelSelectionPanel(), gbc);
126

    
127
                initTable();
128
                getRadioButtonTable().setVisible(false);
129
        }
130
        
131
        /**
132
         * Gets the output panel (SEXTANTE)
133
         * @return
134
         */
135
        private JPanel getOutputChannelSelectionPanel() {
136
                if(outputPanel == null) {
137
                        try {
138
                                outputPanel = new JPanel();
139
                                outputPanel.setLayout(new BorderLayout());
140
                                final OutputObjectsSet ooSet = m_Algorithm.getOutputObjects();
141
                                final Output out = ooSet.getOutput(GridCalculatorAlgorithm.RESULT);
142
                                outputChannelSelectionPanel = new OutputChannelSelectionPanel(out, m_Algorithm.getParameters());
143
                                outputPanel.add(new JLabel("Join [Vectorial]                        "), BorderLayout.WEST);
144
                                outputPanel.add(outputChannelSelectionPanel, BorderLayout.CENTER);
145
                        } catch (final Exception e) {
146
                                Sextante.addErrorToLog(e);
147
                        }
148
                }
149
                return outputPanel;
150
        }
151
        
152
        /**
153
         * Gets the output panel (DAL)
154
         * @return
155
         */
156
        @SuppressWarnings("unused")
157
        private AlgorithmOutputPanel getAlgorithmOutputPanel() {
158
                if(output == null) {
159
                        output = new AlgorithmOutputPanel();
160
                }
161
                return output;
162
        }
163
        
164
        /**
165
         * Gets a new JPanel with the text and JComboBox 
166
         * @param text
167
         * @param combo
168
         * @return
169
         */
170
        public JPanel getComboPanel(String text, JComboBox combo) {
171
                JPanel panel = new JPanel();
172
                GridBagLayout gbl = new GridBagLayout();
173
                panel.setLayout(gbl);
174

    
175
                GridBagConstraints gbc = new GridBagConstraints();
176
                gbc.fill = GridBagConstraints.NONE;
177
                gbc.weightx = 0;
178
                gbc.gridx = 0;
179
                gbc.insets = new Insets(0, 2, 0, 5);
180
                JLabel label = new JLabel(text);
181
                label.setPreferredSize(new Dimension(180, 18));
182
                panel.add(label, gbc);
183

    
184
                gbc.fill = GridBagConstraints.HORIZONTAL;
185
                gbc.weightx = 1.0;
186
                gbc.gridx = 1;
187
                gbc.anchor = GridBagConstraints.EAST;
188
                gbc.insets = new Insets(0, 2, 0, 0);
189
                panel.add(combo, gbc);
190
                return panel;
191
        }
192
        
193
        /**
194
         * Gets a ComboBox
195
         * @return
196
         */
197
        public JComboBox getLayers1Combo() {
198
                if(layersCombo == null) {
199
                        layersCombo = new JComboBox();
200
                        layersCombo.setPreferredSize(new Dimension(0, 18));
201
                        ComboBoxModel comboModel = new DefaultComboBoxModel(getLayerList());
202
                        layersCombo.setModel(comboModel);
203
                        layersCombo.addActionListener(this);
204
                }
205
                return layersCombo;
206
        }
207
        
208
        /**
209
         * Gets a ComboBox
210
         * @return
211
         */
212
        public JComboBox getLayers2Combo() {
213
                if(layersJoinCombo == null) {
214
                        layersJoinCombo = new JComboBox();
215
                        layersJoinCombo.setPreferredSize(new Dimension(0, 18));
216
                        ComboBoxModel comboModel = new DefaultComboBoxModel(getLayerList());
217
                        layersJoinCombo.setModel(comboModel);
218
                        layersJoinCombo.addActionListener(this);
219
                }
220
                return layersJoinCombo;
221
        }
222
        
223
        /**
224
         * Gets a CheckBox
225
         * @return
226
         */
227
        public JCheckBox getSelectionInputCheck() {
228
                if(selectionInput == null) {
229
                        selectionInput = new JCheckBox(GeoProcessLocator.getGeoProcessManager().getTranslation("Selected_geometries_input_layer"));
230
                }
231
                return selectionInput;
232
        }
233
        
234
        /**
235
         * Gets a CheckBox
236
         * @return
237
         */
238
        public JCheckBox getSelectionOverlayCheck() {
239
                if(selectionOverlay == null) {
240
                        selectionOverlay = new JCheckBox(GeoProcessLocator.getGeoProcessManager().getTranslation("Selected_geometries_overlay_layer"));
241
                }
242
                return selectionOverlay;
243
        }
244
        
245
        /**
246
         * Gets a CheckBox
247
         * @return
248
         */
249
        public JCheckBox getNearestCheck() {
250
                if(nearest == null) {
251
                        nearest = new JCheckBox(GeoProcessLocator.getGeoProcessManager().getTranslation("use_the_nearest"));
252
                        nearest.setSelected(true);
253
                        nearest.addActionListener(this);
254
                }
255
                return nearest;
256
        }
257

    
258
        /**
259
         * Gets the summary table
260
         * @return TableContainer
261
         */
262
        public TableContainer getRadioButtonTable() {
263
                if (table == null) {
264
                        table = new TableContainer(columnNames, columnWidths, null);
265
                        table.setModel("ARGBBandSelectorModel");
266
                        table.setControlVisible(false);
267
                        table.initialize();
268
                }
269
                return table;
270
        }
271
        
272
        //------------------------------------------------------------
273
        
274
        /*
275
         * (non-Javadoc)
276
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
277
         */
278
        @SuppressWarnings("unchecked")
279
        public void actionPerformed(ActionEvent e) {
280
                if(e.getSource() == getNearestCheck()) {
281
                        if(!getNearestCheck().isSelected())
282
                                getRadioButtonTable().setVisible(true);
283
                        else
284
                                getRadioButtonTable().setVisible(false);
285
                }
286
                
287
                if(e.getSource() ==  getLayers2Combo()) {
288
                        initTable();
289
                        
290
                        //If the second layer has not numerical fields only the nearest method can be applied
291
                        IVectorLayer lyr = getSelectedVectorLayer2();
292
                        Class[] types = lyr.getFieldTypes();
293
                        boolean hasNumericField = false;
294
                        for (int i = 0; i < types.length; i++) {
295
                                if(types[i] == Integer.class 
296
                                                || types[i] == Double.class 
297
                                                || types[i] == Float.class 
298
                                                || types[i] == Short.class) {
299
                                        hasNumericField = true;
300
                                        break;
301
                                }
302
                        }
303
                        if(!hasNumericField)
304
                                getNearestCheck().setSelected(true);
305
                }
306
        }
307
        
308
        /**
309
         * Adds to the table one entry for each field
310
         */
311
        private void initTable() {
312
                try {
313
                        getRadioButtonTable().removeAllRows();
314
                        if(getSelectedVectorLayer() == null)
315
                                return;
316
                        for (int i = 0; i < getSelectedVectorLayer().getFieldCount(); i++)
317
                                addTableRow(getSelectedVectorLayer().getFieldName(i));
318
                } catch (NotInitializeException e) {
319
                        Sextante.addErrorToLog(e);
320
                }
321
        }
322

    
323
        /**
324
         * A?ade una banda a la tabla bandas de la imagen asignandole un nombre y
325
         * valor a los checkbox
326
         * @param bandName Nombre de la banda
327
         * @throws NotInitializeException 
328
         */
329
        private void addTableRow(String fieldName) throws NotInitializeException {
330
                Object[] row = {        new Boolean(false), 
331
                                                        new Boolean(false), 
332
                                                        new Boolean(false), 
333
                                                        new Boolean(false), 
334
                                                        fieldName };
335
                getRadioButtonTable().addRow(row);
336
        }
337
        
338
        @Override
339
        public void assignParameters() {
340
                try {
341
                        ParametersSet params = m_Algorithm.getParameters();
342
                        params.getParameter(SpatialJoinAlgorithm.LAYER1).setParameterValue(getSelectedVectorLayer());
343
                        params.getParameter(SpatialJoinAlgorithm.LAYER2).setParameterValue(getSelectedVectorLayer2());
344
                        params.getParameter(SpatialJoinAlgorithm.SELECTGEOM_INPUT).setParameterValue(getSelectionInputCheck().isSelected());
345
                        params.getParameter(SpatialJoinAlgorithm.SELECTGEOM_OVERLAY).setParameterValue(getSelectionOverlayCheck().isSelected());
346
                        params.getParameter(SpatialJoinAlgorithm.NEAREST).setParameterValue(getNearestCheck().isSelected());
347
                        params.getParameter(SpatialJoinAlgorithm.FUNCTION_LIST).setParameterValue(getValues());
348
                        
349
                        OutputObjectsSet ooSet = m_Algorithm.getOutputObjects();
350
                        Output out = ooSet.getOutput(SpatialJoinAlgorithm.RESULT);
351
                        
352
                        //Reponer estas l?neas para cambiar el panel de salida y comentar la siguiente
353
                        //AlgorithmOutputPanel fsp = getAlgorithmOutputPanel();
354
                        //out.setOutputChannel(new CompositeSourceOutputChannel(fsp.getOutputParameters()));
355
                 out.setOutputChannel(outputChannelSelectionPanel.getOutputChannel());
356
                } catch (Exception e) {
357
                        Sextante.addErrorToLog(e);
358
        }
359
        }
360

    
361
        @Override
362
        public void setOutputValue(String arg0, String arg1) {
363
                
364
        }
365

    
366
        @Override
367
        public void setParameterValue(String arg0, String arg1) {
368
                
369
        }
370
        
371
        /**
372
         * Gets the input layer list
373
         * @return
374
         */
375
        private ObjectAndDescription[] getLayerList() {
376
                IVectorLayer[] layers = SextanteGUI.getInputFactory()
377
                                        .getVectorLayers(IVectorLayer.SHAPE_TYPE_WRONG);
378
                ObjectAndDescription[] oad = new ObjectAndDescription[layers.length];
379
                for (int i = 0; i < layers.length; i++)
380
                        oad[i] = new ObjectAndDescription(layers[i].getName(), layers[i]);
381
                return oad;
382
        }
383
        
384
        /**
385
         * Gets the selected vector layer in the JComboBox
386
         * @return
387
         */
388
        private IVectorLayer getSelectedVectorLayer() {
389
                if(layersCombo.getSelectedItem() != null)
390
                        return (IVectorLayer)((ObjectAndDescription)layersCombo.getSelectedItem()).getObject();
391
                return null;
392
        }
393
        
394
        /**
395
         * Gets the selected vector layer in the JComboBox
396
         * @return
397
         */
398
        private IVectorLayer getSelectedVectorLayer2() {
399
                if(layersJoinCombo.getSelectedItem() != null)
400
                        return (IVectorLayer)((ObjectAndDescription)layersJoinCombo.getSelectedItem()).getObject();
401
                return null;
402
        }
403
        
404
        /**
405
         * Gets the field list of the selected layer
406
         * @return
407
         */
408
        public String[] getFieldList() {
409
                IVectorLayer layer = getSelectedVectorLayer2();
410
                String[] data = new String[layer.getFieldCount()];
411
                for (int i = 0; i < layer.getFieldCount(); i++) 
412
                        data[i] = layer.getFieldName(i);
413
                return data;
414
        }
415
        
416
        /**
417
         * Formats the content of the table
418
         * @return
419
         */
420
        private String getValues() {
421
            String str = "";
422
            try {
423
                        for (int i = 0; i < getRadioButtonTable().getRowCount(); i++) {
424
                                str = str + (String)getRadioButtonTable().getModel().getValueAt(i, 4) + ",";
425
                                for (int j = 0; j < getRadioButtonTable().getModel().getColumnCount() - 1; j++)
426
                                        if(((Boolean)getRadioButtonTable().getModel().getValueAt(i, j)).booleanValue())
427
                                                str = str + SpatialJoinAlgorithm.Summary[j] + ",";
428
                                str = str.substring(0, str.length() - 1) + ";";
429
                        }
430
                } catch (NotInitializeException e) {
431
                        Sextante.addErrorToLog(e);
432
                }
433
            return str.substring(0, str.length() - 1);
434
    }
435

    
436
}