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

History | View | Annotate | Download (11.8 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

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

    
41
import org.gvsig.geoprocess.core.CompositeSourceOutputChannel;
42
import org.gvsig.geoprocess.lib.api.GeoProcessLocator;
43
import org.gvsig.geoprocess.sextante.gui.algorithm.AlgorithmOutputPanel;
44
import org.gvsig.gui.beans.table.TableContainer;
45
import org.gvsig.gui.beans.table.exceptions.NotInitializeException;
46

    
47
import es.unex.sextante.core.GeoAlgorithm;
48
import es.unex.sextante.core.ObjectAndDescription;
49
import es.unex.sextante.core.OutputObjectsSet;
50
import es.unex.sextante.core.ParametersSet;
51
import es.unex.sextante.core.Sextante;
52
import es.unex.sextante.dataObjects.IVectorLayer;
53
import es.unex.sextante.gridCalculus.gridCalculator.GridCalculatorAlgorithm;
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 DissolveParametersPanel 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                        fieldsCombo        = null;
68
        private JCheckBox                        selectionOnly      = null;
69
        private JCheckBox                        adjacentOnly       = null;
70
        //private AlgorithmOutputPanel             output             = null;
71
        private final String[]                   columnNames        = { "Min", "Max", "Sum", "Avg", "Field ID" };
72
        private final int[]                      columnWidths       = { 35, 35, 35, 35, 334 };
73
        private TableContainer                   table              = null;
74
        private AlgorithmOutputPanel             algorithmOutputPanel  = null;
75
        private OutputChannelSelectionPanel      outputChannelSelectionPanel;
76
        private JPanel                           outputPanel;
77
         
78
        public DissolveParametersPanel() {
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
                
91
                GridBagConstraints gbc = new GridBagConstraints();
92
                gbc.fill = GridBagConstraints.HORIZONTAL;
93
                gbc.weightx = 1.0;
94
                gbc.gridx = 0;
95
                gbc.gridy = 0;
96
                gbc.insets = new Insets(0, 0, 8, 0);
97
                this.add(getComboPanel(GeoProcessLocator.getGeoProcessManager().getTranslation("Input_layer"), getLayersCombo()), gbc);
98
                
99
                gbc.gridy = 1;
100
                this.add(getComboPanel(GeoProcessLocator.getGeoProcessManager().getTranslation("Field"), getFieldsCombo()), gbc);
101
                
102
                gbc.gridy = 2;
103
                this.add(getSelectionCheck(), gbc);
104
                
105
                gbc.gridy = 3;
106
                this.add(getAdjacentCheck(), gbc);
107
                
108
                gbc.gridy = 4;
109
                this.add(new JLabel(GeoProcessLocator.getGeoProcessManager().getTranslation("summary_function")), gbc);
110
                
111
                gbc.gridy = 5;
112
                gbc.fill = GridBagConstraints.BOTH;
113
                gbc.insets = new Insets(0, 0, 12, 0);
114
                gbc.weighty = 1.0;
115
                this.add(getRadioButtonTable(), gbc);
116
                
117
                gbc.gridy = 6;
118
                gbc.fill = GridBagConstraints.HORIZONTAL;
119
                gbc.weighty = 0.0;
120
                this.add(getOutputChannelSelectionPanel(), gbc);
121
                
122
                initTable();
123
        }
124
        
125
        /**
126
         * Gets the output panel (SEXTANTE)
127
         * @return
128
         */
129
        private JPanel getOutputChannelSelectionPanel() {
130
                if(outputPanel == null) {
131
                        try {
132
                                outputPanel = new JPanel();
133
                                outputPanel.setLayout(new BorderLayout());
134
                                final OutputObjectsSet ooSet = m_Algorithm.getOutputObjects();
135
                                final Output out = ooSet.getOutput(GridCalculatorAlgorithm.RESULT);
136
                                outputChannelSelectionPanel = new OutputChannelSelectionPanel(out, m_Algorithm.getParameters());
137
                                outputPanel.add(new JLabel(" Dissolve [Vectorial]               "), BorderLayout.WEST);
138
                                outputPanel.add(outputChannelSelectionPanel, BorderLayout.CENTER);
139
                        } catch (final Exception e) {
140
                                Sextante.addErrorToLog(e);
141
                        }
142
                }
143
                return outputPanel;
144
        }
145
        
146
        /**
147
         * Gets the output panel
148
         * @return
149
         */
150
        private AlgorithmOutputPanel getAlgorithmOutputPanel() {
151
                if(algorithmOutputPanel == null)
152
                    algorithmOutputPanel = new AlgorithmOutputPanel();
153
                return algorithmOutputPanel;
154
        }
155
        
156
        /**
157
         * Gets a new JPanel with the text and JComboBox 
158
         * @param text
159
         * @param combo
160
         * @return
161
         */
162
        public JPanel getComboPanel(String text, JComboBox combo) {
163
                JPanel panel = new JPanel();
164
                GridBagLayout gbl = new GridBagLayout();
165
                panel.setLayout(gbl);
166

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

    
176
                gbc.fill = GridBagConstraints.HORIZONTAL;
177
                gbc.weightx = 1.0;
178
                gbc.gridx = 1;
179
                gbc.anchor = GridBagConstraints.EAST;
180
                gbc.insets = new Insets(0, 2, 0, 0);
181
                panel.add(combo, gbc);
182
                return panel;
183
        }
184
        
185
        /**
186
         * Gets a ComboBox
187
         * @return
188
         */
189
        public JComboBox getLayersCombo() {
190
                if(layersCombo == null) {
191
                        layersCombo = new JComboBox();
192
                        layersCombo.setPreferredSize(new Dimension(0, 18));
193
                        ComboBoxModel comboModel = new DefaultComboBoxModel(getLayerList());
194
                        layersCombo.setModel(comboModel);
195
                        layersCombo.addActionListener(this);
196
                }
197
                return layersCombo;
198
        }
199
        
200
        /**
201
         * Gets a ComboBox
202
         * @return
203
         */
204
        public JComboBox getFieldsCombo() {
205
                if(fieldsCombo == null) {
206
                        fieldsCombo = new JComboBox();
207
                        fieldsCombo.setPreferredSize(new Dimension(0, 18));
208
                        String[] fieldList = getFieldList();
209
                        fieldsCombo.removeAllItems();
210
                        for (int i = 0; i < fieldList.length; i++) 
211
                                fieldsCombo.addItem(fieldList[i]);
212
                }
213
                return fieldsCombo;
214
        }
215
        
216
        /**
217
         * Gets a CheckBox
218
         * @return
219
         */
220
        public JCheckBox getSelectionCheck() {
221
                if(selectionOnly == null) {
222
                        selectionOnly = new JCheckBox(GeoProcessLocator.getGeoProcessManager().getTranslation("Selected_geometries"));
223
                }
224
                return selectionOnly;
225
        }
226
        
227
        /**
228
         * Gets a CheckBox
229
         * @return
230
         */
231
        public JCheckBox getAdjacentCheck() {
232
                if(adjacentOnly == null) {
233
                        adjacentOnly = new JCheckBox(GeoProcessLocator.getGeoProcessManager().getTranslation("adjacent_geometries_only"));
234
                }
235
                return adjacentOnly;
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
        public void actionPerformed(ActionEvent e) {
259
                if(e.getSource() ==  getLayersCombo()) {
260
                        //Recarga el combo de campos
261
                        String[] fieldList = getFieldList();
262
                        getFieldsCombo().removeAllItems();
263
                        for (int i = 0; i < fieldList.length; i++) 
264
                                getFieldsCombo().addItem(fieldList[i]);
265
                        
266
                        initTable();
267
                }
268
        }
269
        
270
        /**
271
         * Adds to the table one entry for each field
272
         */
273
        private void initTable() {
274
                try {
275
                        getRadioButtonTable().removeAllRows();
276
                        for (int i = 0; i < getSelectedVectorLayer().getFieldCount(); i++)
277
                                addTableRow(getSelectedVectorLayer().getFieldName(i));
278
                } catch (NotInitializeException e) {
279
                        Sextante.addErrorToLog(e);
280
                }
281
        }
282

    
283
        /**
284
         * A�ade una banda a la tabla bandas de la imagen asignandole un nombre y
285
         * valor a los checkbox
286
         * @param bandName Nombre de la banda
287
         * @throws NotInitializeException 
288
         */
289
        private void addTableRow(String fieldName) throws NotInitializeException {
290
                Object[] row = {        new Boolean(false), 
291
                                                        new Boolean(false), 
292
                                                        new Boolean(false), 
293
                                                        new Boolean(false), 
294
                                                        fieldName };
295
                getRadioButtonTable().addRow(row);
296
        }
297
        
298
        @Override
299
    public void assignParameters() {
300
                try {
301
                        ParametersSet params = m_Algorithm.getParameters();
302
                        params.getParameter(DissolveAlgorithm.LAYER).setParameterValue(getSelectedVectorLayer());
303
                        params.getParameter(DissolveAlgorithm.FIELD).setParameterValue(fieldsCombo.getSelectedIndex());
304
                        params.getParameter(DissolveAlgorithm.SELECTED_GEOM).setParameterValue(getSelectionCheck().isSelected());
305
                        params.getParameter(DissolveAlgorithm.DISSOLV_ADJ).setParameterValue(getAdjacentCheck().isSelected());
306
                        
307
                        params.getParameter(DissolveAlgorithm.FUNCTION_LIST).setParameterValue(getValues());
308
                        
309
                        OutputObjectsSet ooSet = m_Algorithm.getOutputObjects();
310
                        Output out = ooSet.getOutput(DissolveAlgorithm.RESULT);
311
                        out.setOutputChannel(new CompositeSourceOutputChannel(getAlgorithmOutputPanel().getOutputParameters()));
312
                } catch (Exception e) {
313
                        Sextante.addErrorToLog(e);
314
                }
315
        }
316
        
317
        /**
318
         * Formats the content of the table
319
         * @return
320
         */
321
        private String getValues() {
322
            String str = "";
323
            try {
324
                        for (int i = 0; i < getRadioButtonTable().getRowCount(); i++) {
325
                                str = str + (String)getRadioButtonTable().getModel().getValueAt(i, 4) + ",";
326
                                for (int j = 0; j < getRadioButtonTable().getModel().getColumnCount() - 1; j++)
327
                                        if(((Boolean)getRadioButtonTable().getModel().getValueAt(i, j)).booleanValue())
328
                                                str = str + DissolveAlgorithm.Summary[j] + ",";
329
                                str = str.substring(0, str.length() - 1) + ";";
330
                        }
331
                } catch (NotInitializeException e) {
332
                        Sextante.addErrorToLog(e);
333
                }
334
            return str.substring(0, str.length() - 1);
335
    }
336

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

    
342
        @Override
343
        public void setParameterValue(String arg0, String arg1) {
344
                
345
        }
346
        
347
        /**
348
         * Gets the input layer list
349
         * @return
350
         */
351
        private ObjectAndDescription[] getLayerList() {
352
                IVectorLayer[] layers = SextanteGUI.getInputFactory()
353
                                        .getVectorLayers(IVectorLayer.SHAPE_TYPE_POLYGON);
354
                ObjectAndDescription[] oad = new ObjectAndDescription[layers.length];
355
                for (int i = 0; i < layers.length; i++)
356
                        oad[i] = new ObjectAndDescription(layers[i].getName(), layers[i]);
357
                return oad;
358
        }
359
        
360
        /**
361
         * Gets the selected vector layer in the JComboBox
362
         * @return
363
         */
364
        private IVectorLayer getSelectedVectorLayer() {
365
                if(layersCombo.getSelectedItem() != null)
366
                        return (IVectorLayer)((ObjectAndDescription)layersCombo.getSelectedItem()).getObject();
367
                return null;
368
        }
369
        
370
        /**
371
         * Gets the field list of the selected layer
372
         * @return
373
         */
374
        public String[] getFieldList() {
375
                IVectorLayer layer = getSelectedVectorLayer();
376
                String[] data = new String[layer.getFieldCount()];
377
                for (int i = 0; i < layer.getFieldCount(); i++) 
378
                        data[i] = layer.getFieldName(i);
379
                return data;
380
        }
381
}