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

History | View | Annotate | Download (13.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.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.gui.algorithm.GeoAlgorithmParametersPanel;
55
import es.unex.sextante.gui.algorithm.OutputChannelSelectionPanel;
56
import es.unex.sextante.gui.core.SextanteGUI;
57
import es.unex.sextante.outputs.Output;
58

    
59
/**
60
 * Panel for dissolve algorithm
61
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
62
 */
63
public class SpatialJoinParametersPanel extends GeoAlgorithmParametersPanel implements ActionListener {
64
        private static final long                serialVersionUID   = 1L;
65
        private GeoAlgorithm                     m_Algorithm        = null;
66
        private JComboBox                        layersCombo        = null;
67
        private JComboBox                        layersJoinCombo    = null;
68
        private JCheckBox                        selectionInput     = null;
69
        private JCheckBox                        selectionOverlay   = 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(getSelectionInputCheck(), gbc);
105

    
106
                gbc.gridy = 3;
107
                this.add(getSelectionOverlayCheck(), gbc);
108

    
109
                gbc.gridy = 4;
110
                this.add(getNearestCheck(), gbc);
111

    
112
                gbc.gridy = 5;
113
                this.add(new JLabel(GeoProcessLocator.getGeoProcessManager().getTranslation("summary_function")), gbc);
114

    
115
                gbc.gridy = 6;
116
                gbc.fill = GridBagConstraints.BOTH;
117
                gbc.insets = new Insets(0, 0, 12, 0);
118
                gbc.weighty = 1.0;
119
                this.add(getRadioButtonTable(), gbc);
120

    
121
                gbc.gridy = 7;
122
                gbc.fill = GridBagConstraints.HORIZONTAL;
123
                gbc.weighty = 0.0;
124
                this.add(getOutputChannelSelectionPanel(), gbc);
125

    
126
                initTable();
127
                getRadioButtonTable().setVisible(false);
128
        }
129

    
130
        /**
131
         * Gets the output panel (SEXTANTE)
132
         * @return
133
         */
134
        private JPanel getOutputChannelSelectionPanel() {
135
                if(outputPanel == null) {
136
                        try {
137
                                outputPanel = new JPanel();
138
                                outputPanel.setLayout(new BorderLayout());
139
                                final OutputObjectsSet ooSet = m_Algorithm.getOutputObjects();
140
                                final Output out = ooSet.getOutput(SpatialJoinAlgorithm.RESULT);
141
                                outputChannelSelectionPanel = new OutputChannelSelectionPanel(out, m_Algorithm.getParameters());
142
                                outputPanel.add(new JLabel("Join [Vectorial]                        "), BorderLayout.WEST);
143
                                outputPanel.add(outputChannelSelectionPanel, BorderLayout.CENTER);
144
                        } catch (final Exception e) {
145
                                Sextante.addErrorToLog(e);
146
                        }
147
                }
148
                return outputPanel;
149
        }
150

    
151
        /**
152
         * Gets the output panel (DAL)
153
         * @return
154
         */
155
        @SuppressWarnings("unused")
156
        private AlgorithmOutputPanel getAlgorithmOutputPanel() {
157
                if(output == null) {
158
                        output = new AlgorithmOutputPanel();
159
                }
160
                return output;
161
        }
162

    
163
        /**
164
         * Gets a new JPanel with the text and JComboBox
165
         * @param text
166
         * @param combo
167
         * @return
168
         */
169
        public JPanel getComboPanel(String text, JComboBox combo) {
170
                JPanel panel = new JPanel();
171
                GridBagLayout gbl = new GridBagLayout();
172
                panel.setLayout(gbl);
173

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

    
183
                gbc.fill = GridBagConstraints.HORIZONTAL;
184
                gbc.weightx = 1.0;
185
                gbc.gridx = 1;
186
                gbc.anchor = GridBagConstraints.EAST;
187
                gbc.insets = new Insets(0, 2, 0, 0);
188
                panel.add(combo, gbc);
189
                return panel;
190
        }
191

    
192
        /**
193
         * Gets a ComboBox
194
         * @return
195
         */
196
        public JComboBox getLayers1Combo() {
197
                if(layersCombo == null) {
198
                        layersCombo = new JComboBox();
199
                        layersCombo.setPreferredSize(new Dimension(0, 18));
200
                        ComboBoxModel comboModel = new DefaultComboBoxModel(getLayerList());
201
                        layersCombo.setModel(comboModel);
202
                        layersCombo.addActionListener(this);
203
                }
204
                return layersCombo;
205
        }
206

    
207
        /**
208
         * Gets a ComboBox
209
         * @return
210
         */
211
        public JComboBox getLayers2Combo() {
212
                if(layersJoinCombo == null) {
213
                        layersJoinCombo = new JComboBox();
214
                        layersJoinCombo.setPreferredSize(new Dimension(0, 18));
215
                        ComboBoxModel comboModel = new DefaultComboBoxModel(getLayerList());
216
                        layersJoinCombo.setModel(comboModel);
217
                        layersJoinCombo.addActionListener(this);
218
                }
219
                return layersJoinCombo;
220
        }
221

    
222
        /**
223
         * Gets a CheckBox
224
         * @return
225
         */
226
        public JCheckBox getSelectionInputCheck() {
227
                if(selectionInput == null) {
228
                        selectionInput = new JCheckBox(GeoProcessLocator.getGeoProcessManager().getTranslation("Selected_geometries_input_layer"));
229
                }
230
                return selectionInput;
231
        }
232

    
233
        /**
234
         * Gets a CheckBox
235
         * @return
236
         */
237
        public JCheckBox getSelectionOverlayCheck() {
238
                if(selectionOverlay == null) {
239
                        selectionOverlay = new JCheckBox(GeoProcessLocator.getGeoProcessManager().getTranslation("Selected_geometries_overlay_layer"));
240
                }
241
                return selectionOverlay;
242
        }
243

    
244
        /**
245
         * Gets a CheckBox
246
         * @return
247
         */
248
        public JCheckBox getNearestCheck() {
249
                if(nearest == null) {
250
                        nearest = new JCheckBox(GeoProcessLocator.getGeoProcessManager().getTranslation("use_the_nearest"));
251
                        nearest.setSelected(true);
252
                        nearest.addActionListener(this);
253
                }
254
                return nearest;
255
        }
256

    
257
        /**
258
         * Gets the summary table
259
         * @return TableContainer
260
         */
261
        public TableContainer getRadioButtonTable() {
262
                if (table == null) {
263
                        table = new TableContainer(columnNames, columnWidths, null);
264
                        table.setModel("ARGBBandSelectorModel");
265
                        table.setControlVisible(false);
266
                        table.initialize();
267
                }
268
                return table;
269
        }
270

    
271
        //------------------------------------------------------------
272

    
273
        public void actionPerformed(ActionEvent e) {
274
                if(e.getSource() == getNearestCheck()) {
275
                        if(!getNearestCheck().isSelected())
276
                                getRadioButtonTable().setVisible(true);
277
                        else
278
                                getRadioButtonTable().setVisible(false);
279
                }
280

    
281
                if(e.getSource() ==  getLayers2Combo()) {
282
                        initTable();
283

    
284
                        //If the second layer has not numerical fields only the nearest method can be applied
285
                        IVectorLayer lyr = getSelectedVectorLayer2();
286
                        Class[] types = lyr.getFieldTypes();
287
                        boolean hasNumericField = false;
288
                        for (int i = 0; i < types.length; i++) {
289
                                if(types[i] == Integer.class
290
                                                || types[i] == Double.class
291
                                                || types[i] == Float.class
292
                                                || types[i] == Short.class) {
293
                                        hasNumericField = true;
294
                                        break;
295
                                }
296
                        }
297
                        if(!hasNumericField){
298
                                getNearestCheck().setSelected(true);
299
                        }
300
                }
301
        }
302

    
303
        /**
304
         * Adds to the table one entry for each field
305
         */
306
        private void initTable() {
307
                try {
308
                        getRadioButtonTable().removeAllRows();
309
                        if(getSelectedVectorLayer2() == null)
310
                                return;
311
                        Class<?>[] types = getSelectedVectorLayer2().getFieldTypes();
312
                        for (int i = 0; i < getSelectedVectorLayer2().getFieldCount(); i++) {
313
                                if(Number.class.isAssignableFrom(types[i])) {
314
                                        addTableRow(getSelectedVectorLayer2().getFieldName(i));
315
                                }
316
                        }
317
                } catch (NotInitializeException e) {
318
                        Sextante.addErrorToLog(e);
319
                }
320
        }
321

    
322
        /**
323
         * A?ade una banda a la tabla bandas de la imagen asignandole un nombre y
324
         * valor a los checkbox
325
         * @param bandName Nombre de la banda
326
         * @throws NotInitializeException
327
         */
328
        private void addTableRow(String fieldName) throws NotInitializeException {
329
                Object[] row = {        new Boolean(false),
330
                                                        new Boolean(false),
331
                                                        new Boolean(false),
332
                                                        new Boolean(false),
333
                                                        fieldName };
334
                getRadioButtonTable().addRow(row);
335
        }
336

    
337
        @Override
338
        public void assignParameters() {
339
                try {
340
                        ParametersSet params = m_Algorithm.getParameters();
341
                        params.getParameter(SpatialJoinAlgorithm.LAYER1).setParameterValue(getSelectedVectorLayer());
342
                        params.getParameter(SpatialJoinAlgorithm.LAYER2).setParameterValue(getSelectedVectorLayer2());
343
                        params.getParameter(SpatialJoinAlgorithm.SELECTGEOM_INPUT).setParameterValue(getSelectionInputCheck().isSelected());
344
                        params.getParameter(SpatialJoinAlgorithm.SELECTGEOM_OVERLAY).setParameterValue(getSelectionOverlayCheck().isSelected());
345
                        params.getParameter(SpatialJoinAlgorithm.NEAREST).setParameterValue(getNearestCheck().isSelected());
346
                        params.getParameter(SpatialJoinAlgorithm.FUNCTION_LIST).setParameterValue(getValues());
347

    
348
                        OutputObjectsSet ooSet = m_Algorithm.getOutputObjects();
349
                        Output out = ooSet.getOutput(SpatialJoinAlgorithm.RESULT);
350

    
351
                        //Reponer estas l?neas para cambiar el panel de salida y comentar la siguiente
352
                        //AlgorithmOutputPanel fsp = getAlgorithmOutputPanel();
353
                        //out.setOutputChannel(new CompositeSourceOutputChannel(fsp.getOutputParameters()));
354
                 out.setOutputChannel(outputChannelSelectionPanel.getOutputChannel());
355
                } catch (Exception e) {
356
                        Sextante.addErrorToLog(e);
357
        }
358
        }
359

    
360
        @Override
361
        public void setOutputValue(String arg0, String arg1) {
362

    
363
        }
364

    
365
        @Override
366
        public void setParameterValue(String arg0, String arg1) {
367

    
368
        }
369

    
370
        /**
371
         * Gets the input layer list
372
         * @return
373
         */
374
        private ObjectAndDescription[] getLayerList() {
375
                IVectorLayer[] layers = SextanteGUI.getInputFactory()
376
                                        .getVectorLayers(IVectorLayer.SHAPE_TYPE_WRONG);
377
                ObjectAndDescription[] oad = new ObjectAndDescription[layers.length];
378
                for (int i = 0; i < layers.length; i++)
379
                        oad[i] = new ObjectAndDescription(layers[i].getName(), layers[i]);
380
                return oad;
381
        }
382

    
383
        /**
384
         * Gets the selected vector layer in the JComboBox
385
         * @return
386
         */
387
        private IVectorLayer getSelectedVectorLayer() {
388
                if(layersCombo.getSelectedItem() != null)
389
                        return (IVectorLayer)((ObjectAndDescription)layersCombo.getSelectedItem()).getObject();
390
                return null;
391
        }
392

    
393
        /**
394
         * Gets the selected vector layer in the JComboBox
395
         * @return
396
         */
397
        private IVectorLayer getSelectedVectorLayer2() {
398
                if(layersJoinCombo.getSelectedItem() != null)
399
                        return (IVectorLayer)((ObjectAndDescription)layersJoinCombo.getSelectedItem()).getObject();
400
                return null;
401
        }
402

    
403
        /**
404
         * Gets the field list of the selected layer
405
         * @return
406
         */
407
        public String[] getFieldList() {
408
                IVectorLayer layer = getSelectedVectorLayer2();
409
                String[] data = new String[layer.getFieldCount()];
410
                for (int i = 0; i < layer.getFieldCount(); i++)
411
                        data[i] = layer.getFieldName(i);
412
                return data;
413
        }
414

    
415
        /**
416
         * Formats the content of the table
417
         * @return
418
         */
419
        private String getValues() {
420
            String str = "";
421
            try {
422
                        for (int i = 0; i < getRadioButtonTable().getRowCount(); i++) {
423
                                str = str + (String)getRadioButtonTable().getModel().getValueAt(i, 4) + ",";
424
                                for (int j = 0; j < getRadioButtonTable().getModel().getColumnCount() - 1; j++)
425
                                        if(((Boolean)getRadioButtonTable().getModel().getValueAt(i, j)).booleanValue())
426
                                                str = str + SpatialJoinAlgorithm.Summary[j] + ",";
427
                                str = str.substring(0, str.length() - 1) + ";";
428
                        }
429
                } catch (NotInitializeException e) {
430
                        Sextante.addErrorToLog(e);
431
                }
432
            if (str.isEmpty()) {
433
                    return str;
434
            }
435
            return str.substring(0, str.length() - 1);
436
    }
437

    
438
}