Statistics
| Revision:

gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.algorithm / org.gvsig.geoprocess.algorithm.dissolve / src / main / java / org / gvsig / geoprocess / algorithm / dissolve / DissolveParametersPanel.java @ 1259

History | View | Annotate | Download (12.3 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.dissolve;
25

    
26
import java.awt.BorderLayout;
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
import java.util.ArrayList;
34
import java.util.List;
35

    
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.fmap.dal.exception.DataException;
44
import org.gvsig.fmap.dal.feature.FeatureStore;
45
import org.gvsig.geoprocess.lib.api.GeoProcessLocator;
46
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer;
47
import org.gvsig.geoprocess.sextante.gui.algorithm.AlgorithmOutputPanel;
48
import org.gvsig.gui.beans.table.TableContainer;
49
import org.gvsig.gui.beans.table.exceptions.NotInitializeException;
50

    
51
import es.unex.sextante.core.GeoAlgorithm;
52
import es.unex.sextante.core.ObjectAndDescription;
53
import es.unex.sextante.core.OutputObjectsSet;
54
import es.unex.sextante.core.ParametersSet;
55
import es.unex.sextante.core.Sextante;
56
import es.unex.sextante.dataObjects.IVectorLayer;
57
import es.unex.sextante.gui.algorithm.GeoAlgorithmParametersPanel;
58
import es.unex.sextante.gui.algorithm.OutputChannelSelectionPanel;
59
import es.unex.sextante.gui.core.SextanteGUI;
60
import es.unex.sextante.outputs.Output;
61
import java.util.Arrays;
62

    
63
/**
64
 * Panel for dissolve algorithm
65
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
66
 */
67
public class DissolveParametersPanel extends GeoAlgorithmParametersPanel implements ActionListener {
68
        private static final long                serialVersionUID   = 1L;
69
        private GeoAlgorithm                     m_Algorithm        = null;
70
        private JComboBox                        layersCombo        = null;
71
        private JComboBox                        fieldsCombo        = null;
72
        private JCheckBox                        selectionOnly      = null;
73
        private JCheckBox                        adjacentOnly       = null;
74
        //private AlgorithmOutputPanel             output             = null;
75
        private final String[]                   columnNames        = { "Min", "Max", "Sum", "Avg", "Field ID" };
76
        private final int[]                      columnWidths       = { 35, 35, 35, 35, 334 };
77
        private TableContainer                   table              = null;
78
        private AlgorithmOutputPanel             algorithmOutputPanel  = null;
79
        private OutputChannelSelectionPanel      outputChannelSelectionPanel;
80
        private JPanel                           outputPanel;
81
         
82
        public DissolveParametersPanel() {
83
                super();
84
        }
85

    
86
    public void init(GeoAlgorithm algorithm) {
87
            m_Algorithm = algorithm;
88
            initGUI();
89
    }
90

    
91
        private void initGUI() {
92
                GridBagLayout gbl = new GridBagLayout();
93
                this.setLayout(gbl);
94
                
95
                GridBagConstraints gbc = new GridBagConstraints();
96
                gbc.fill = GridBagConstraints.HORIZONTAL;
97
                gbc.weightx = 1.0;
98
                gbc.gridx = 0;
99
                gbc.gridy = 0;
100
                gbc.insets = new Insets(0, 0, 8, 0);
101
                this.add(getComboPanel(GeoProcessLocator.getGeoProcessManager().getTranslation("Input_layer"), getLayersCombo()), gbc);
102
                
103
                gbc.gridy = 1;
104
                this.add(getComboPanel(GeoProcessLocator.getGeoProcessManager().getTranslation("Field"), getFieldsCombo()), gbc);
105
                
106
                gbc.gridy = 2;
107
                this.add(getSelectionCheck(), gbc);
108
                
109
                gbc.gridy = 3;
110
                this.add(getAdjacentCheck(), gbc);
111
                
112
                gbc.gridy = 4;
113
                this.add(new JLabel(GeoProcessLocator.getGeoProcessManager().getTranslation("summary_function")), gbc);
114
                
115
                gbc.gridy = 5;
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 = 6;
122
                gbc.fill = GridBagConstraints.HORIZONTAL;
123
                gbc.weighty = 0.0;
124
                this.add(getOutputChannelSelectionPanel(), gbc);
125
                
126
                initTable();
127
        }
128
        
129
        /**
130
         * Gets the output panel (SEXTANTE)
131
         * @return
132
         */
133
        private JPanel getOutputChannelSelectionPanel() {
134
                if(outputPanel == null) {
135
                        try {
136
                                outputPanel = new JPanel();
137
                                outputPanel.setLayout(new BorderLayout());
138
                                final OutputObjectsSet ooSet = m_Algorithm.getOutputObjects();
139
                                final Output out = ooSet.getOutput(DissolveAlgorithm.RESULT);
140
                                outputChannelSelectionPanel = new OutputChannelSelectionPanel(out, m_Algorithm.getParameters());
141
                                outputPanel.add(new JLabel(" " + GeoProcessLocator.getGeoProcessManager().getTranslation("Dissolve") +
142
                                                " [" + GeoProcessLocator.getGeoProcessManager().getTranslation("Vectorial") +
143
                                                "]               "), BorderLayout.WEST);
144
                                outputPanel.add(outputChannelSelectionPanel, BorderLayout.CENTER);
145
                        } catch (final Exception e) {
146
                                Sextante.addErrorToLog(e);
147
                        }
148
                }
149
                return outputPanel;
150
        }
151
        
152
        /**
153
         * Gets the output panel
154
         * @return
155
         */
156
        @SuppressWarnings("unused")
157
        private AlgorithmOutputPanel getAlgorithmOutputPanel() {
158
                if(algorithmOutputPanel == null)
159
                    algorithmOutputPanel = new AlgorithmOutputPanel();
160
                return algorithmOutputPanel;
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 getLayersCombo() {
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 getFieldsCombo() {
212
                if(fieldsCombo == null) {
213
                        fieldsCombo = new JComboBox();
214
                        fieldsCombo.setPreferredSize(new Dimension(0, 18));
215
                        List<String> fieldList = getFieldList();
216
                        fieldsCombo.removeAllItems();
217
                        for (int i = 0; i < fieldList.size(); i++) 
218
                                fieldsCombo.addItem(fieldList.get(i));
219
                }
220
                return fieldsCombo;
221
        }
222
        
223
        /**
224
         * Gets a CheckBox
225
         * @return
226
         */
227
        public JCheckBox getSelectionCheck() {
228
                if(selectionOnly == null) {
229
                        selectionOnly = new JCheckBox(GeoProcessLocator.getGeoProcessManager().getTranslation("Selected_geometries"));
230
                }
231
                return selectionOnly;
232
        }
233
        
234
        /**
235
         * Gets a CheckBox
236
         * @return
237
         */
238
        public JCheckBox getAdjacentCheck() {
239
                if(adjacentOnly == null) {
240
                        adjacentOnly = new JCheckBox(GeoProcessLocator.getGeoProcessManager().getTranslation("adjacent_geometries_only"));
241
                }
242
                return adjacentOnly;
243
        }
244

    
245
        /**
246
         * Gets the summary table
247
         * @return TableContainer
248
         */
249
        public TableContainer getRadioButtonTable() {
250
                if (table == null) {
251
                        table = new TableContainer(columnNames, columnWidths, null);
252
                        table.setModel("ARGBBandSelectorModel");
253
                        table.setControlVisible(false);
254
                        table.initialize();
255
                }
256
                return table;
257
        }
258
        
259
        //------------------------------------------------------------
260
        
261
        /*
262
         * (non-Javadoc)
263
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
264
         */
265
        public void actionPerformed(ActionEvent e) {
266
                if(e.getSource() ==  getLayersCombo()) {
267
                        //Recarga el combo de campos
268
                        List<String> fieldList = getFieldList();
269
                        getFieldsCombo().removeAllItems();
270
                        for (int i = 0; i < fieldList.size(); i++) 
271
                                getFieldsCombo().addItem(fieldList.get(i));
272
                        
273
                        initTable();
274
                }
275
        }
276
        
277
        /**
278
         * Adds to the table one entry for each field
279
         */
280
        private void initTable() {
281
                try {
282
                        getRadioButtonTable().removeAllRows();
283
                        for (int i = 0; i < getSelectedVectorLayer().getFieldCount(); i++) {
284
                                if(Number.class.isAssignableFrom(getSelectedVectorLayer().getFieldType(i)))
285
                                        addTableRow(getSelectedVectorLayer().getFieldName(i));
286
                        }
287
                } catch (NotInitializeException e) {
288
                        Sextante.addErrorToLog(e);
289
                }
290
        }
291

    
292
        /**
293
         * A?ade una banda a la tabla bandas de la imagen asignandole un nombre y
294
         * valor a los checkbox
295
         * @param bandName Nombre de la banda
296
         * @throws NotInitializeException 
297
         */
298
        private void addTableRow(String fieldName) throws NotInitializeException {
299
                Object[] row = {        new Boolean(false), 
300
                                                        new Boolean(false), 
301
                                                        new Boolean(false), 
302
                                                        new Boolean(false), 
303
                                                        fieldName };
304
                getRadioButtonTable().addRow(row);
305
        }
306
        
307
        @Override
308
    public void assignParameters() {
309
                try {
310
                        ParametersSet params = m_Algorithm.getParameters();
311
                        params.getParameter(DissolveAlgorithm.LAYER).setParameterValue(getSelectedVectorLayer());
312
                        params.getParameter(DissolveAlgorithm.FIELD).setParameterValue(fieldsCombo.getSelectedIndex());
313
                        params.getParameter(DissolveAlgorithm.SELECTED_GEOM).setParameterValue(getSelectionCheck().isSelected());
314
                        params.getParameter(DissolveAlgorithm.DISSOLV_ADJ).setParameterValue(getAdjacentCheck().isSelected());
315
                        
316
                        params.getParameter(DissolveAlgorithm.FUNCTION_LIST).setParameterValue(getValues());
317
                        
318
                        OutputObjectsSet ooSet = m_Algorithm.getOutputObjects();
319
                        Output out = ooSet.getOutput(DissolveAlgorithm.RESULT);
320
                        
321
                        //Reponer estas l?neas para cambiar el panel de salida y comentar la siguiente
322
                        //AlgorithmOutputPanel fsp = getAlgorithmOutputPanel();
323
                        //out.setOutputChannel(new CompositeSourceOutputChannel(fsp.getOutputParameters()));
324
                 out.setOutputChannel(outputChannelSelectionPanel.getOutputChannel());
325
                } catch (Exception e) {
326
                        Sextante.addErrorToLog(e);
327
                }
328
        }
329
        
330
        /**
331
         * Formats the content of the table
332
         * @return
333
         */
334
        private String getValues() {
335
            String str = "";
336
            try {
337
                        for (int i = 0; i < getRadioButtonTable().getRowCount(); i++) {
338
                                str = str + (String)getRadioButtonTable().getModel().getValueAt(i, 4) + ",";
339
                                for (int j = 0; j < getRadioButtonTable().getModel().getColumnCount() - 1; j++)
340
                                        if(((Boolean)getRadioButtonTable().getModel().getValueAt(i, j)).booleanValue())
341
                                                str = str + DissolveAlgorithm.Summary[j] + ",";
342
                                str = str.substring(0, str.length() - 1) + ";";
343
                        }
344
                } catch (NotInitializeException e) {
345
                        Sextante.addErrorToLog(e);
346
                }
347
            return (str.equals("")) ? str : str.substring(0, str.length() - 1);
348
    }
349

    
350
        @Override
351
        public void setOutputValue(String arg0, String arg1) {
352
                
353
        }
354

    
355
        @Override
356
        public void setParameterValue(String arg0, String arg1) {
357
                
358
        }
359
        
360
        /**
361
         * Gets the input layer list
362
         * @return
363
         */
364
        private ObjectAndDescription[] getLayerList() {
365
                IVectorLayer[] layers = SextanteGUI.getInputFactory()
366
                                        .getVectorLayers(IVectorLayer.SHAPE_TYPE_WRONG);
367
                ObjectAndDescription[] oad = new ObjectAndDescription[layers.length];
368
                for (int i = 0; i < layers.length; i++)
369
                        oad[i] = new ObjectAndDescription(layers[i].getName(), layers[i]);
370
                return oad;
371
        }
372
        
373
        /**
374
         * Gets the selected vector layer in the JComboBox
375
         * @return
376
         */
377
        private IVectorLayer getSelectedVectorLayer() {
378
                if(layersCombo.getSelectedItem() != null)
379
                        return (IVectorLayer)((ObjectAndDescription)layersCombo.getSelectedItem()).getObject();
380
                return null;
381
        }
382
        
383
        /**
384
         * Gets the field list of the selected layer
385
         * @return
386
         */
387
        public List<String> getFieldList() {
388
                IVectorLayer layer = getSelectedVectorLayer();
389
                return Arrays.asList(layer.getFieldNames());
390
        }
391
}