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

History | View | Annotate | Download (12.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.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
                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 OutputChannelSelectionPanel getOutputChannelSelectionPanel() {
132
                if(outputChannelSelectionPanel == null) {
133
                        try {
134
                                final OutputObjectsSet ooSet = m_Algorithm.getOutputObjects();
135
                                final Output out = ooSet.getOutput(GridCalculatorAlgorithm.RESULT);
136
                                outputChannelSelectionPanel = new OutputChannelSelectionPanel(out, m_Algorithm.getParameters());
137
                        } catch (final Exception e) {
138
                                Sextante.addErrorToLog(e);
139
                        }
140
                }
141
                return outputChannelSelectionPanel;
142
        }
143
        
144
        /**
145
         * Gets the output panel (DAL)
146
         * @return
147
         */
148
        private AlgorithmOutputPanel getAlgorithmOutputPanel() {
149
                if(output == null) {
150
                        output = new AlgorithmOutputPanel();
151
                }
152
                return output;
153
        }
154
        
155
        /**
156
         * Gets a new JPanel with the text and JComboBox 
157
         * @param text
158
         * @param combo
159
         * @return
160
         */
161
        public JPanel getComboPanel(String text, JComboBox combo) {
162
                JPanel panel = new JPanel();
163
                GridBagLayout gbl = new GridBagLayout();
164
                panel.setLayout(gbl);
165

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

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

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

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

    
338
        @Override
339
        public void setOutputValue(String arg0, String arg1) {
340
                
341
        }
342

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

    
413
}