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

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

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

    
40
import es.unex.sextante.core.GeoAlgorithm;
41
import es.unex.sextante.core.ObjectAndDescription;
42
import es.unex.sextante.core.OutputObjectsSet;
43
import es.unex.sextante.core.ParametersSet;
44
import es.unex.sextante.core.Sextante;
45
import es.unex.sextante.dataObjects.IVectorLayer;
46
import es.unex.sextante.gui.algorithm.GeoAlgorithmParametersPanel;
47
import es.unex.sextante.gui.core.SextanteGUI;
48
import es.unex.sextante.outputs.Output;
49

    
50
import org.gvsig.geoprocess.core.CompositeSourceOutputChannel;
51
import org.gvsig.geoprocess.sextante.gui.algorithm.AlgorithmOutputPanel;
52
import org.gvsig.gui.beans.table.TableContainer;
53
import org.gvsig.gui.beans.table.exceptions.NotInitializeException;
54

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

    
75
    public void init(GeoAlgorithm algorithm) {
76
            m_Algorithm = algorithm;
77
            initGUI();
78
    }
79

    
80
        private void initGUI() {
81
                GridBagLayout gbl = new GridBagLayout();
82
                this.setLayout(gbl);
83
                
84
                GridBagConstraints gbc = new GridBagConstraints();
85
                gbc.fill = GridBagConstraints.HORIZONTAL;
86
                gbc.weightx = 1.0;
87
                gbc.gridx = 0;
88
                gbc.gridy = 0;
89
                gbc.insets = new Insets(0, 0, 8, 0);
90
                this.add(getComboPanel(Sextante.getText("input_layers"), getLayers1Combo()), gbc);
91
                
92
                gbc.gridy = 1;
93
                this.add(getComboPanel(Sextante.getText("input_layers_join"), getLayers2Combo()), gbc);
94
                
95
                gbc.gridy = 2;
96
                this.add(getSelectionCheck(), gbc);
97
                
98
                gbc.gridy = 3;
99
                this.add(getNearestCheck(), gbc);
100
                
101
                gbc.gridy = 4;
102
                this.add(new JLabel(Sextante.getText("summary_function")), gbc);
103
                
104
                gbc.gridy = 5;
105
                gbc.fill = GridBagConstraints.BOTH;
106
                gbc.insets = new Insets(0, 0, 12, 0);
107
                gbc.weighty = 1.0;
108
                this.add(getRadioButtonTable(), gbc);
109
                
110
                gbc.gridy = 6;
111
                gbc.fill = GridBagConstraints.HORIZONTAL;
112
                gbc.weighty = 0.0;
113
                this.add(getAlgorithmOutputPanel(), gbc);
114
                
115
                initTable();
116
                getRadioButtonTable().setVisible(false);
117
        }
118
        
119
        /**
120
         * Gets the output panel
121
         * @return
122
         */
123
        private AlgorithmOutputPanel getAlgorithmOutputPanel() {
124
                if(output == null)
125
                        output = new AlgorithmOutputPanel();
126
                return output;
127
        }
128
        
129
        /**
130
         * Gets a new JPanel with the text and JComboBox 
131
         * @param text
132
         * @param combo
133
         * @return
134
         */
135
        public JPanel getComboPanel(String text, JComboBox combo) {
136
                JPanel panel = new JPanel();
137
                GridBagLayout gbl = new GridBagLayout();
138
                panel.setLayout(gbl);
139

    
140
                GridBagConstraints gbc = new GridBagConstraints();
141
                gbc.fill = GridBagConstraints.NONE;
142
                gbc.weightx = 0;
143
                gbc.gridx = 0;
144
                gbc.insets = new Insets(0, 2, 0, 5);
145
                JLabel label = new JLabel(text);
146
                label.setPreferredSize(new Dimension(180, 18));
147
                panel.add(label, gbc);
148

    
149
                gbc.fill = GridBagConstraints.HORIZONTAL;
150
                gbc.weightx = 1.0;
151
                gbc.gridx = 1;
152
                gbc.anchor = GridBagConstraints.EAST;
153
                gbc.insets = new Insets(0, 2, 0, 0);
154
                panel.add(combo, gbc);
155
                return panel;
156
        }
157
        
158
        /**
159
         * Gets a ComboBox
160
         * @return
161
         */
162
        public JComboBox getLayers1Combo() {
163
                if(layersCombo == null) {
164
                        layersCombo = new JComboBox();
165
                        layersCombo.setPreferredSize(new Dimension(0, 18));
166
                        ComboBoxModel comboModel = new DefaultComboBoxModel(getLayerList());
167
                        layersCombo.setModel(comboModel);
168
                        layersCombo.addActionListener(this);
169
                }
170
                return layersCombo;
171
        }
172
        
173
        /**
174
         * Gets a ComboBox
175
         * @return
176
         */
177
        public JComboBox getLayers2Combo() {
178
                if(layersJoinCombo == null) {
179
                        layersJoinCombo = new JComboBox();
180
                        layersJoinCombo.setPreferredSize(new Dimension(0, 18));
181
                        ComboBoxModel comboModel = new DefaultComboBoxModel(getLayerList());
182
                        layersJoinCombo.setModel(comboModel);
183
                        layersJoinCombo.addActionListener(this);
184
                }
185
                return layersJoinCombo;
186
        }
187
        
188
        /**
189
         * Gets a CheckBox
190
         * @return
191
         */
192
        public JCheckBox getSelectionCheck() {
193
                if(selectionOnly == null) {
194
                        selectionOnly = new JCheckBox(Sextante.getText("selected_geometries"));
195
                }
196
                return selectionOnly;
197
        }
198
        
199
        /**
200
         * Gets a CheckBox
201
         * @return
202
         */
203
        public JCheckBox getNearestCheck() {
204
                if(nearest == null) {
205
                        nearest = new JCheckBox(Sextante.getText("use_the_nearest"));
206
                        nearest.setSelected(true);
207
                        nearest.addActionListener(this);
208
                }
209
                return nearest;
210
        }
211

    
212
        /**
213
         * Gets the summary table
214
         * @return TableContainer
215
         */
216
        public TableContainer getRadioButtonTable() {
217
                if (table == null) {
218
                        table = new TableContainer(columnNames, columnWidths, null);
219
                        table.setModel("ARGBBandSelectorModel");
220
                        table.setControlVisible(false);
221
                        table.initialize();
222
                }
223
                return table;
224
        }
225
        
226
        //------------------------------------------------------------
227
        
228
        /*
229
         * (non-Javadoc)
230
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
231
         */
232
        @SuppressWarnings("unchecked")
233
        public void actionPerformed(ActionEvent e) {
234
                if(e.getSource() == getNearestCheck()) {
235
                        if(!getNearestCheck().isSelected())
236
                                getRadioButtonTable().setVisible(true);
237
                        else
238
                                getRadioButtonTable().setVisible(false);
239
                }
240
                
241
                if(e.getSource() ==  getLayers2Combo()) {
242
                        initTable();
243
                        
244
                        //If the second layer has not numerical fields only the nearest method can be applied
245
                        IVectorLayer lyr = getSelectedVectorLayer2();
246
                        Class[] types = lyr.getFieldTypes();
247
                        boolean hasNumericField = false;
248
                        for (int i = 0; i < types.length; i++) {
249
                                if(types[i] == Integer.class 
250
                                                || types[i] == Double.class 
251
                                                || types[i] == Float.class 
252
                                                || types[i] == Short.class) {
253
                                        hasNumericField = true;
254
                                        break;
255
                                }
256
                        }
257
                        if(!hasNumericField)
258
                                getNearestCheck().setSelected(true);
259
                }
260
        }
261
        
262
        /**
263
         * Adds to the table one entry for each field
264
         */
265
        private void initTable() {
266
                try {
267
                        getRadioButtonTable().removeAllRows();
268
                        for (int i = 0; i < getSelectedVectorLayer().getFieldCount(); i++)
269
                                addTableRow(getSelectedVectorLayer().getFieldName(i));
270
                } catch (NotInitializeException e) {
271
                        Sextante.addErrorToLog(e);
272
                }
273
        }
274

    
275
        /**
276
         * A?ade una banda a la tabla bandas de la imagen asignandole un nombre y
277
         * valor a los checkbox
278
         * @param bandName Nombre de la banda
279
         * @throws NotInitializeException 
280
         */
281
        private void addTableRow(String fieldName) throws NotInitializeException {
282
                Object[] row = {        new Boolean(false), 
283
                                                        new Boolean(false), 
284
                                                        new Boolean(false), 
285
                                                        new Boolean(false), 
286
                                                        fieldName };
287
                getRadioButtonTable().addRow(row);
288
        }
289
        
290
        @Override
291
        public void assignParameters() {
292
                try {
293
                        ParametersSet params = m_Algorithm.getParameters();
294
                        params.getParameter(SpatialJoinAlgorithm.LAYER1).setParameterValue(getSelectedVectorLayer());
295
                        params.getParameter(SpatialJoinAlgorithm.LAYER2).setParameterValue(getSelectedVectorLayer2());
296
                        params.getParameter(SpatialJoinAlgorithm.SELECTED_GEOM).setParameterValue(getSelectionCheck().isSelected());
297
                        params.getParameter(SpatialJoinAlgorithm.NEAREST).setParameterValue(getNearestCheck().isSelected());
298
                        params.getParameter(SpatialJoinAlgorithm.FUNCTION_LIST).setParameterValue(getValues());
299
                        
300
                        OutputObjectsSet ooSet = m_Algorithm.getOutputObjects();
301
                        Output out = ooSet.getOutput(SpatialJoinAlgorithm.RESULT);
302
                        AlgorithmOutputPanel fsp = getAlgorithmOutputPanel();
303
                        out.setOutputChannel(new CompositeSourceOutputChannel(fsp.getOutputParameters()));
304
                } catch (Exception e) {
305
                        Sextante.addErrorToLog(e);
306
        }
307
        }
308

    
309
        @Override
310
        public void setOutputValue(String arg0, String arg1) {
311
                
312
        }
313

    
314
        @Override
315
        public void setParameterValue(String arg0, String arg1) {
316
                
317
        }
318
        
319
        /**
320
         * Gets the input layer list
321
         * @return
322
         */
323
        private ObjectAndDescription[] getLayerList() {
324
                IVectorLayer[] layers = SextanteGUI.getInputFactory()
325
                                        .getVectorLayers(IVectorLayer.SHAPE_TYPE_POLYGON);
326
                ObjectAndDescription[] oad = new ObjectAndDescription[layers.length];
327
                for (int i = 0; i < layers.length; i++)
328
                        oad[i] = new ObjectAndDescription(layers[i].getName(), layers[i]);
329
                return oad;
330
        }
331
        
332
        /**
333
         * Gets the selected vector layer in the JComboBox
334
         * @return
335
         */
336
        private IVectorLayer getSelectedVectorLayer() {
337
                if(layersCombo.getSelectedItem() != null)
338
                        return (IVectorLayer)((ObjectAndDescription)layersCombo.getSelectedItem()).getObject();
339
                return null;
340
        }
341
        
342
        /**
343
         * Gets the selected vector layer in the JComboBox
344
         * @return
345
         */
346
        private IVectorLayer getSelectedVectorLayer2() {
347
                if(layersJoinCombo.getSelectedItem() != null)
348
                        return (IVectorLayer)((ObjectAndDescription)layersJoinCombo.getSelectedItem()).getObject();
349
                return null;
350
        }
351
        
352
        /**
353
         * Gets the field list of the selected layer
354
         * @return
355
         */
356
        public String[] getFieldList() {
357
                IVectorLayer layer = getSelectedVectorLayer2();
358
                String[] data = new String[layer.getFieldCount()];
359
                for (int i = 0; i < layer.getFieldCount(); i++) 
360
                        data[i] = layer.getFieldName(i);
361
                return data;
362
        }
363
        
364
        /**
365
         * Formats the content of the table
366
         * @return
367
         */
368
        private String getValues() {
369
            String str = "";
370
            try {
371
                        for (int i = 0; i < getRadioButtonTable().getRowCount(); i++) {
372
                                str = str + (String)getRadioButtonTable().getModel().getValueAt(i, 4) + ",";
373
                                for (int j = 0; j < getRadioButtonTable().getModel().getColumnCount() - 1; j++)
374
                                        if(((Boolean)getRadioButtonTable().getModel().getValueAt(i, j)).booleanValue())
375
                                                str = str + SpatialJoinAlgorithm.Summary[j] + ",";
376
                                str = str.substring(0, str.length() - 1) + ";";
377
                        }
378
                } catch (NotInitializeException e) {
379
                        Sextante.addErrorToLog(e);
380
                }
381
            return str.substring(0, str.length() - 1);
382
    }
383

    
384
}