Statistics
| Revision:

gvsig-geoprocess / org.gvsig.sextante / trunk / org.gvsig.sextante.app / org.gvsig.sextante.app.algorithm / org.gvsig.sextante.app.algorithm.spatialjoin / src / main / java / org / gvsig / sextante / app / algorithm / spatialjoin / SpatialJoinParametersPanel.java @ 172

History | View | Annotate | Download (11.2 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2010 Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 */
19
package org.gvsig.sextante.app.algorithm.spatialjoin;
20

    
21
import java.awt.Dimension;
22
import java.awt.GridBagConstraints;
23
import java.awt.GridBagLayout;
24
import java.awt.Insets;
25
import java.awt.event.ActionEvent;
26
import java.awt.event.ActionListener;
27

    
28
import javax.swing.ComboBoxModel;
29
import javax.swing.DefaultComboBoxModel;
30
import javax.swing.JCheckBox;
31
import javax.swing.JComboBox;
32
import javax.swing.JLabel;
33
import javax.swing.JPanel;
34

    
35
import es.unex.sextante.core.GeoAlgorithm;
36
import es.unex.sextante.core.ObjectAndDescription;
37
import es.unex.sextante.core.OutputObjectsSet;
38
import es.unex.sextante.core.ParametersSet;
39
import es.unex.sextante.core.Sextante;
40
import es.unex.sextante.dataObjects.IVectorLayer;
41
import es.unex.sextante.gui.algorithm.GeoAlgorithmParametersPanel;
42
import es.unex.sextante.gui.core.SextanteGUI;
43
import es.unex.sextante.outputs.Output;
44

    
45
import org.gvsig.geoprocess.core.CompositeSourceOutputChannel;
46
import org.gvsig.geoprocess.gui.AlgorithmOutputPanel;
47
import org.gvsig.gui.beans.table.TableContainer;
48
import org.gvsig.gui.beans.table.exceptions.NotInitializeException;
49

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

    
70
    public void init(GeoAlgorithm algorithm) {
71
            m_Algorithm = algorithm;
72
            initGUI();
73
    }
74

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

    
135
                GridBagConstraints gbc = new GridBagConstraints();
136
                gbc.fill = GridBagConstraints.NONE;
137
                gbc.weightx = 0;
138
                gbc.gridx = 0;
139
                gbc.insets = new Insets(0, 2, 0, 5);
140
                JLabel label = new JLabel(text);
141
                label.setPreferredSize(new Dimension(180, 18));
142
                panel.add(label, gbc);
143

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

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

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

    
304
        @Override
305
        public void setOutputValue(String arg0, String arg1) {
306
                
307
        }
308

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

    
379
}