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 @ 313

History | View | Annotate | Download (12.9 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                        selectionOnly      = null;
70
        private JCheckBox                        nearest       = null;
71
        private AlgorithmOutputPanel             output             = null;
72
        private final String[]                   columnNames        = { "Min", "Max", "Sum", "Avg", "Field ID" };
73
        private final int[]                      columnWidths       = { 35, 35, 35, 35, 334 };
74
        private TableContainer                   table              = null;
75
        private OutputChannelSelectionPanel      outputChannelSelectionPanel;
76
        private JPanel                           outputPanel;
77
        
78
        public SpatialJoinParametersPanel() {
79
                super();
80
        }
81

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

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

    
118
                gbc.gridy = 6;
119
                gbc.fill = GridBagConstraints.HORIZONTAL;
120
                gbc.weighty = 0.0;
121
                this.add(getOutputChannelSelectionPanel(), gbc);
122

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

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

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

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

    
308
        /**
309
         * A?ade una banda a la tabla bandas de la imagen asignandole un nombre y
310
         * valor a los checkbox
311
         * @param bandName Nombre de la banda
312
         * @throws NotInitializeException 
313
         */
314
        private void addTableRow(String fieldName) throws NotInitializeException {
315
                Object[] row = {        new Boolean(false), 
316
                                                        new Boolean(false), 
317
                                                        new Boolean(false), 
318
                                                        new Boolean(false), 
319
                                                        fieldName };
320
                getRadioButtonTable().addRow(row);
321
        }
322
        
323
        @Override
324
        public void assignParameters() {
325
                try {
326
                        ParametersSet params = m_Algorithm.getParameters();
327
                        params.getParameter(SpatialJoinAlgorithm.LAYER1).setParameterValue(getSelectedVectorLayer());
328
                        params.getParameter(SpatialJoinAlgorithm.LAYER2).setParameterValue(getSelectedVectorLayer2());
329
                        params.getParameter(SpatialJoinAlgorithm.SELECTED_GEOM).setParameterValue(getSelectionCheck().isSelected());
330
                        params.getParameter(SpatialJoinAlgorithm.NEAREST).setParameterValue(getNearestCheck().isSelected());
331
                        params.getParameter(SpatialJoinAlgorithm.FUNCTION_LIST).setParameterValue(getValues());
332
                        
333
                        OutputObjectsSet ooSet = m_Algorithm.getOutputObjects();
334
                        Output out = ooSet.getOutput(SpatialJoinAlgorithm.RESULT);
335
                        
336
                        //Reponer estas l?neas para cambiar el panel de salida y comentar la siguiente
337
                        //AlgorithmOutputPanel fsp = getAlgorithmOutputPanel();
338
                        //out.setOutputChannel(new CompositeSourceOutputChannel(fsp.getOutputParameters()));
339
                 out.setOutputChannel(outputChannelSelectionPanel.getOutputChannel());
340
                } catch (Exception e) {
341
                        Sextante.addErrorToLog(e);
342
        }
343
        }
344

    
345
        @Override
346
        public void setOutputValue(String arg0, String arg1) {
347
                
348
        }
349

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

    
420
}