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

History | View | Annotate | Download (12.1 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.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 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(DissolveAlgorithm.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
        @SuppressWarnings("unused")
151
        private AlgorithmOutputPanel getAlgorithmOutputPanel() {
152
                if(algorithmOutputPanel == null)
153
                    algorithmOutputPanel = new AlgorithmOutputPanel();
154
                return algorithmOutputPanel;
155
        }
156
        
157
        /**
158
         * Gets a new JPanel with the text and JComboBox 
159
         * @param text
160
         * @param combo
161
         * @return
162
         */
163
        public JPanel getComboPanel(String text, JComboBox combo) {
164
                JPanel panel = new JPanel();
165
                GridBagLayout gbl = new GridBagLayout();
166
                panel.setLayout(gbl);
167

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

    
177
                gbc.fill = GridBagConstraints.HORIZONTAL;
178
                gbc.weightx = 1.0;
179
                gbc.gridx = 1;
180
                gbc.anchor = GridBagConstraints.EAST;
181
                gbc.insets = new Insets(0, 2, 0, 0);
182
                panel.add(combo, gbc);
183
                return panel;
184
        }
185
        
186
        /**
187
         * Gets a ComboBox
188
         * @return
189
         */
190
        public JComboBox getLayersCombo() {
191
                if(layersCombo == null) {
192
                        layersCombo = new JComboBox();
193
                        layersCombo.setPreferredSize(new Dimension(0, 18));
194
                        ComboBoxModel comboModel = new DefaultComboBoxModel(getLayerList());
195
                        layersCombo.setModel(comboModel);
196
                        layersCombo.addActionListener(this);
197
                }
198
                return layersCombo;
199
        }
200
        
201
        /**
202
         * Gets a ComboBox
203
         * @return
204
         */
205
        public JComboBox getFieldsCombo() {
206
                if(fieldsCombo == null) {
207
                        fieldsCombo = new JComboBox();
208
                        fieldsCombo.setPreferredSize(new Dimension(0, 18));
209
                        List<String> fieldList = getFieldList();
210
                        fieldsCombo.removeAllItems();
211
                        for (int i = 0; i < fieldList.size(); i++) 
212
                                fieldsCombo.addItem(fieldList.get(i));
213
                }
214
                return fieldsCombo;
215
        }
216
        
217
        /**
218
         * Gets a CheckBox
219
         * @return
220
         */
221
        public JCheckBox getSelectionCheck() {
222
                if(selectionOnly == null) {
223
                        selectionOnly = new JCheckBox(GeoProcessLocator.getGeoProcessManager().getTranslation("Selected_geometries"));
224
                }
225
                return selectionOnly;
226
        }
227
        
228
        /**
229
         * Gets a CheckBox
230
         * @return
231
         */
232
        public JCheckBox getAdjacentCheck() {
233
                if(adjacentOnly == null) {
234
                        adjacentOnly = new JCheckBox(GeoProcessLocator.getGeoProcessManager().getTranslation("adjacent_geometries_only"));
235
                }
236
                return adjacentOnly;
237
        }
238

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

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

    
344
        @Override
345
        public void setOutputValue(String arg0, String arg1) {
346
                
347
        }
348

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