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

History | View | Annotate | Download (10.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.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.lib.api.GeoProcessLocator;
52
import org.gvsig.geoprocess.sextante.gui.algorithm.AlgorithmOutputPanel;
53
import org.gvsig.gui.beans.table.TableContainer;
54
import org.gvsig.gui.beans.table.exceptions.NotInitializeException;
55

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

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

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

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

    
150
                gbc.fill = GridBagConstraints.HORIZONTAL;
151
                gbc.weightx = 1.0;
152
                gbc.gridx = 1;
153
                gbc.anchor = GridBagConstraints.EAST;
154
                gbc.insets = new Insets(0, 2, 0, 0);
155
                panel.add(combo, gbc);
156
                return panel;
157
        }
158
        
159
        /**
160
         * Gets a ComboBox
161
         * @return
162
         */
163
        public JComboBox getLayersCombo() {
164
                if(layersCombo == null) {
165
                        layersCombo = new JComboBox();
166
                        layersCombo.setPreferredSize(new Dimension(0, 18));
167
                        ComboBoxModel comboModel = new DefaultComboBoxModel(getLayerList());
168
                        layersCombo.setModel(comboModel);
169
                        layersCombo.addActionListener(this);
170
                }
171
                return layersCombo;
172
        }
173
        
174
        /**
175
         * Gets a ComboBox
176
         * @return
177
         */
178
        public JComboBox getFieldsCombo() {
179
                if(fieldsCombo == null) {
180
                        fieldsCombo = new JComboBox();
181
                        fieldsCombo.setPreferredSize(new Dimension(0, 18));
182
                        String[] fieldList = getFieldList();
183
                        fieldsCombo.removeAllItems();
184
                        for (int i = 0; i < fieldList.length; i++) 
185
                                fieldsCombo.addItem(fieldList[i]);
186
                }
187
                return fieldsCombo;
188
        }
189
        
190
        /**
191
         * Gets a CheckBox
192
         * @return
193
         */
194
        public JCheckBox getSelectionCheck() {
195
                if(selectionOnly == null) {
196
                        selectionOnly = new JCheckBox(GeoProcessLocator.getGeoProcessManager().getTranslation("Selected_geometries"));
197
                }
198
                return selectionOnly;
199
        }
200
        
201
        /**
202
         * Gets a CheckBox
203
         * @return
204
         */
205
        public JCheckBox getAdjacentCheck() {
206
                if(adjacentOnly == null) {
207
                        adjacentOnly = new JCheckBox(GeoProcessLocator.getGeoProcessManager().getTranslation("adjacent_geometries_only"));
208
                }
209
                return adjacentOnly;
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
        public void actionPerformed(ActionEvent e) {
233
                if(e.getSource() ==  getLayersCombo()) {
234
                        //Recarga el combo de campos
235
                        String[] fieldList = getFieldList();
236
                        getFieldsCombo().removeAllItems();
237
                        for (int i = 0; i < fieldList.length; i++) 
238
                                getFieldsCombo().addItem(fieldList[i]);
239
                        
240
                        initTable();
241
                }
242
        }
243
        
244
        /**
245
         * Adds to the table one entry for each field
246
         */
247
        private void initTable() {
248
                try {
249
                        getRadioButtonTable().removeAllRows();
250
                        for (int i = 0; i < getSelectedVectorLayer().getFieldCount(); i++)
251
                                addTableRow(getSelectedVectorLayer().getFieldName(i));
252
                } catch (NotInitializeException e) {
253
                        Sextante.addErrorToLog(e);
254
                }
255
        }
256

    
257
        /**
258
         * A�ade una banda a la tabla bandas de la imagen asignandole un nombre y
259
         * valor a los checkbox
260
         * @param bandName Nombre de la banda
261
         * @throws NotInitializeException 
262
         */
263
        private void addTableRow(String fieldName) throws NotInitializeException {
264
                Object[] row = {        new Boolean(false), 
265
                                                        new Boolean(false), 
266
                                                        new Boolean(false), 
267
                                                        new Boolean(false), 
268
                                                        fieldName };
269
                getRadioButtonTable().addRow(row);
270
        }
271
        
272
        @Override
273
    public void assignParameters() {
274
                try {
275
                        ParametersSet params = m_Algorithm.getParameters();
276
                        params.getParameter(DissolveAlgorithm.LAYER).setParameterValue(getSelectedVectorLayer());
277
                        params.getParameter(DissolveAlgorithm.FIELD).setParameterValue(fieldsCombo.getSelectedIndex());
278
                        params.getParameter(DissolveAlgorithm.SELECTED_GEOM).setParameterValue(getSelectionCheck().isSelected());
279
                        params.getParameter(DissolveAlgorithm.DISSOLV_ADJ).setParameterValue(getAdjacentCheck().isSelected());
280
                        
281
                        params.getParameter(DissolveAlgorithm.FUNCTION_LIST).setParameterValue(getValues());
282
                        
283
                        OutputObjectsSet ooSet = m_Algorithm.getOutputObjects();
284
                        Output out = ooSet.getOutput(DissolveAlgorithm.RESULT);
285
                        out.setOutputChannel(new CompositeSourceOutputChannel(getAlgorithmOutputPanel().getOutputParameters()));
286
                } catch (Exception e) {
287
                        Sextante.addErrorToLog(e);
288
                }
289
        }
290
        
291
        /**
292
         * Formats the content of the table
293
         * @return
294
         */
295
        private String getValues() {
296
            String str = "";
297
            try {
298
                        for (int i = 0; i < getRadioButtonTable().getRowCount(); i++) {
299
                                str = str + (String)getRadioButtonTable().getModel().getValueAt(i, 4) + ",";
300
                                for (int j = 0; j < getRadioButtonTable().getModel().getColumnCount() - 1; j++)
301
                                        if(((Boolean)getRadioButtonTable().getModel().getValueAt(i, j)).booleanValue())
302
                                                str = str + DissolveAlgorithm.Summary[j] + ",";
303
                                str = str.substring(0, str.length() - 1) + ";";
304
                        }
305
                } catch (NotInitializeException e) {
306
                        Sextante.addErrorToLog(e);
307
                }
308
            return str.substring(0, str.length() - 1);
309
    }
310

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

    
316
        @Override
317
        public void setParameterValue(String arg0, String arg1) {
318
                
319
        }
320
        
321
        /**
322
         * Gets the input layer list
323
         * @return
324
         */
325
        private ObjectAndDescription[] getLayerList() {
326
                IVectorLayer[] layers = SextanteGUI.getInputFactory()
327
                                        .getVectorLayers(IVectorLayer.SHAPE_TYPE_POLYGON);
328
                ObjectAndDescription[] oad = new ObjectAndDescription[layers.length];
329
                for (int i = 0; i < layers.length; i++)
330
                        oad[i] = new ObjectAndDescription(layers[i].getName(), layers[i]);
331
                return oad;
332
        }
333
        
334
        /**
335
         * Gets the selected vector layer in the JComboBox
336
         * @return
337
         */
338
        private IVectorLayer getSelectedVectorLayer() {
339
                if(layersCombo.getSelectedItem() != null)
340
                        return (IVectorLayer)((ObjectAndDescription)layersCombo.getSelectedItem()).getObject();
341
                return null;
342
        }
343
        
344
        /**
345
         * Gets the field list of the selected layer
346
         * @return
347
         */
348
        public String[] getFieldList() {
349
                IVectorLayer layer = getSelectedVectorLayer();
350
                String[] data = new String[layer.getFieldCount()];
351
                for (int i = 0; i < layer.getFieldCount(); i++) 
352
                        data[i] = layer.getFieldName(i);
353
                return data;
354
        }
355
}