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

History | View | Annotate | Download (12.4 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.Color;
27
import java.awt.Dimension;
28
import java.awt.GridBagConstraints;
29
import java.awt.GridBagLayout;
30
import java.awt.Insets;
31
import java.awt.event.ActionEvent;
32
import java.awt.event.ActionListener;
33

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

    
42
import org.gvsig.geoprocess.core.CompositeSourceOutputChannel;
43
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess;
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.IOutputChannel;
59
import es.unex.sextante.outputs.Output;
60

    
61
/**
62
 * Panel for dissolve algorithm
63
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
64
 */
65
public class SpatialJoinParametersPanel extends GeoAlgorithmParametersPanel implements ActionListener {
66
        private static final long                serialVersionUID   = 1L;
67
        private GeoAlgorithm                     m_Algorithm        = null;
68
        private JComboBox                        layersCombo        = null;
69
        private JComboBox                        layersJoinCombo    = null;
70
        private JCheckBox                        selectionOnly      = 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
        
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(Sextante.getText("input_layers"), getLayers1Combo()), gbc);
99
                
100
                gbc.gridy = 1;
101
                this.add(getComboPanel(Sextante.getText("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(Sextante.getText("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
                //Salida propia
119
                /*gbc.gridy = 6;
120
                gbc.fill = GridBagConstraints.HORIZONTAL;
121
                gbc.weighty = 0.0;
122
                this.add(getAlgorithmOutputPanel(), gbc);*/
123
                
124
                //Salida de SEXTANTE
125
                try {
126
                        final OutputObjectsSet ooSet = m_Algorithm.getOutputObjects();
127
                        final Output out = ooSet.getOutput(GridCalculatorAlgorithm.RESULT);
128
                        outputChannelSelectionPanel = new OutputChannelSelectionPanel(out, m_Algorithm.getParameters());
129
                        gbc.gridy = 6;
130
                        gbc.fill = GridBagConstraints.HORIZONTAL;
131
                        gbc.weighty = 0.0;
132
                        this.add(outputChannelSelectionPanel, gbc);
133
                } catch (final Exception e) {
134
                        Sextante.addErrorToLog(e);
135
                }
136

    
137
                initTable();
138
                getRadioButtonTable().setVisible(false);
139
        }
140
        
141
        /**
142
         * Gets the output panel
143
         * @return
144
         */
145
        private AlgorithmOutputPanel getAlgorithmOutputPanel() {
146
                if(output == null)
147
                        output = new AlgorithmOutputPanel();
148
                return output;
149
        }
150
        
151
        /**
152
         * Gets a new JPanel with the text and JComboBox 
153
         * @param text
154
         * @param combo
155
         * @return
156
         */
157
        public JPanel getComboPanel(String text, JComboBox combo) {
158
                JPanel panel = new JPanel();
159
                GridBagLayout gbl = new GridBagLayout();
160
                panel.setLayout(gbl);
161

    
162
                GridBagConstraints gbc = new GridBagConstraints();
163
                gbc.fill = GridBagConstraints.NONE;
164
                gbc.weightx = 0;
165
                gbc.gridx = 0;
166
                gbc.insets = new Insets(0, 2, 0, 5);
167
                JLabel label = new JLabel(text);
168
                label.setPreferredSize(new Dimension(180, 18));
169
                panel.add(label, gbc);
170

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

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

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

    
334
        @Override
335
        public void setOutputValue(String arg0, String arg1) {
336
                
337
        }
338

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

    
409
}