Statistics
| Revision:

gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.algorithm / org.gvsig.geoprocess.algorithm.buffer / src / main / java / org / gvsig / geoprocess / algorithm / buffer / BufferParametersPanel.java @ 332

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

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

    
35
import javax.swing.BorderFactory;
36
import javax.swing.ButtonGroup;
37
import javax.swing.ComboBoxModel;
38
import javax.swing.DefaultComboBoxModel;
39
import javax.swing.JCheckBox;
40
import javax.swing.JComboBox;
41
import javax.swing.JLabel;
42
import javax.swing.JPanel;
43
import javax.swing.JRadioButton;
44
import javax.swing.JScrollPane;
45
import javax.swing.JTextField;
46

    
47
import org.gvsig.geoprocess.algorithm.dissolve.DissolveAlgorithm;
48
import org.gvsig.geoprocess.lib.api.GeoProcessLocator;
49
import org.gvsig.geoprocess.sextante.gui.algorithm.AlgorithmOutputPanel;
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

    
62
/**
63
 * Panel for buffer algorithm
64
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
65
 */
66
public class BufferParametersPanel extends GeoAlgorithmParametersPanel implements ActionListener {
67
        private static final long                serialVersionUID   = 1L;
68
        private final int                        marginSides        = 15;
69
        private final int                        marginBottom       = 8;
70
        private GeoAlgorithm                     m_Algorithm        = null;
71
        private JComboBox                        layers             = null;
72
        private JComboBox                        fields             = null;
73
        private JComboBox                        influenceAreas     = null;
74
        private JComboBox                        radialBuffers      = null;
75
        private JCheckBox                        selectionOnly      = null;
76
        private JCheckBox                        dissolveEntities   = null;
77
        private JCheckBox                        roundBorder        = null;
78
        private JTextField                       distance           = null;
79
        private JRadioButton                     selectDistance     = null;
80
        private JRadioButton                     selectField        = null;
81
        
82
        private List<String>                     fieldList             = new ArrayList<String>();
83
        private AlgorithmOutputPanel             algorithmOutputPanel  = null;
84
        private OutputChannelSelectionPanel      outputChannelSelectionPanel;
85
        private JPanel                           outputPanel;
86
         
87
        public BufferParametersPanel() {
88
                super();
89
        }
90

    
91
    public void init(GeoAlgorithm algorithm) {
92
            m_Algorithm = algorithm;
93
            initGUI();
94
    }
95

    
96
        private void initGUI() {
97
                this.setLayout(new BorderLayout());
98
                this.add(getMainJScrollPane(), BorderLayout.CENTER);
99
        }
100
        
101
        private JScrollPane getMainJScrollPane() {
102
                JPanel panel = new JPanel();
103
                GridBagLayout gbl = new GridBagLayout();
104
                panel.setLayout(gbl);
105
                
106
                GridBagConstraints gbc = new GridBagConstraints();
107
                gbc.fill = GridBagConstraints.HORIZONTAL;
108
                gbc.weightx = 1.0;
109
                gbc.gridx = 0;
110
                gbc.gridy = 0;
111
                gbc.insets = new Insets(0, marginSides, 0, marginSides);
112
                panel.add(getInputPanel(), gbc);
113
                
114
                gbc.gridy = 1;
115
                panel.add(getOptionsPanel(), gbc);
116
                
117
                gbc.gridy = 2;
118
                panel.add(getOutputsPanel(), gbc);
119
                JScrollPane scrollPane = new JScrollPane(panel);
120
                return scrollPane;
121
        }
122
        
123
        /**
124
         * Gets the output panel (SEXTANTE)
125
         * @return
126
         */
127
        private JPanel getOutputChannelSelectionPanel() {
128
                if(outputPanel == null) {
129
                        try {
130
                                outputPanel = new JPanel();
131
                                outputPanel.setLayout(new BorderLayout());
132
                                final OutputObjectsSet ooSet = m_Algorithm.getOutputObjects();
133
                                final Output out = ooSet.getOutput(DissolveAlgorithm.RESULT);
134
                                outputChannelSelectionPanel = new OutputChannelSelectionPanel(out, m_Algorithm.getParameters());
135
                                outputPanel.add(new JLabel(" Buffer [Vectorial]               "), BorderLayout.WEST);
136
                                outputPanel.add(outputChannelSelectionPanel, BorderLayout.CENTER);
137
                        } catch (final Exception e) {
138
                                Sextante.addErrorToLog(e);
139
                        }
140
                }
141
                return outputPanel;
142
        }
143
        
144
        /**
145
         * Gets the output panel
146
         * @return
147
         */
148
        @SuppressWarnings("unused")
149
        private AlgorithmOutputPanel getAlgorithmOutputPanel() {
150
                if(algorithmOutputPanel == null)
151
                    algorithmOutputPanel = new AlgorithmOutputPanel();
152
                return algorithmOutputPanel;
153
        }
154
        
155
        /**
156
         * Gets a new input panel
157
         * @param text
158
         * @param combo
159
         * @return
160
         */
161
        public JPanel getOutputsPanel() {
162
                JPanel panel = new JPanel();
163
                GridBagLayout gbl = new GridBagLayout();
164
                panel.setLayout(gbl);
165
                panel.setBorder(BorderFactory.createTitledBorder(GeoProcessLocator.getGeoProcessManager().getTranslation("outputs")));
166
                
167
                GridBagConstraints gbc = new GridBagConstraints();
168
                gbc.fill = GridBagConstraints.HORIZONTAL;
169
                gbc.weightx = 1.0;
170
                gbc.insets = new Insets(0, marginSides, marginBottom, marginSides);
171
                panel.add(getOutputChannelSelectionPanel(), gbc);
172
                
173
                return panel;
174
        }
175
        
176
        /**
177
         * Gets a new input panel
178
         * @param text
179
         * @param combo
180
         * @return
181
         */
182
        public JPanel getInputPanel() {
183
                JPanel panel = new JPanel();
184
                GridBagLayout gbl = new GridBagLayout();
185
                panel.setLayout(gbl);
186
                panel.setBorder(BorderFactory.createTitledBorder(GeoProcessLocator.getGeoProcessManager().getTranslation("input")));
187
                
188
                GridBagConstraints gbc = new GridBagConstraints();
189
                gbc.fill = GridBagConstraints.HORIZONTAL;
190
                gbc.weightx = 1.0;
191
                gbc.insets = new Insets(0, marginSides, marginBottom, marginSides);
192
                panel.add(getComboLayers(), gbc);
193
                
194
                return panel;
195
        }
196
        
197
        /**
198
         * Gets a new options panel
199
         * @param text
200
         * @param combo
201
         * @return
202
         */
203
        public JPanel getOptionsPanel() {
204
                JPanel panel = new JPanel();
205
                GridBagLayout gbl = new GridBagLayout();
206
                panel.setLayout(gbl);
207
                panel.setBorder(BorderFactory.createTitledBorder(GeoProcessLocator.getGeoProcessManager().getTranslation("options")));
208
                
209
                ButtonGroup group = new ButtonGroup();
210
                group.add(getRadioSelectDistance());
211
            group.add(getRadioSelectField());
212

    
213
                GridBagConstraints gbc = new GridBagConstraints();
214
                gbc.fill = GridBagConstraints.HORIZONTAL;
215
                gbc.weightx = 1.0;
216
                gbc.insets = new Insets(0, marginSides, marginBottom, marginSides);
217
                panel.add(getRadioSelectField(), gbc);
218
                
219
                gbc.gridy = 1;
220
                gbc.insets = new Insets(0, marginSides + 10, marginBottom, marginSides);
221
                panel.add(getComboFields(), gbc);
222
                
223
                gbc.gridy = 2;
224
                gbc.insets = new Insets(0, marginSides, marginBottom, marginSides);
225
                panel.add(getRadioSelectDistance(), gbc);
226
                
227
                gbc.gridy = 3;
228
                gbc.insets = new Insets(0, marginSides + 10, marginBottom, marginSides);
229
                panel.add(getTextDistance(), gbc);
230
                
231
                gbc.gridy = 4;
232
                gbc.insets = new Insets(0, marginSides, marginBottom, marginSides);
233
                panel.add(getCheckSelectedGeom(), gbc);
234
                
235
                gbc.gridy = 5;
236
                panel.add(getCheckDissolveEntities(), gbc);
237
                
238
                gbc.gridy = 6;
239
                panel.add(getCheckRoundBorder(), gbc);
240
                
241
                gbc.gridy = 7;
242
                panel.add(getComboInfluenceAreas(), gbc);
243
                
244
                gbc.gridy = 8;
245
                panel.add(getComboRadialBuffers(), gbc);
246
                
247
                return panel;
248
        }
249
        
250
        /**
251
         * Gets a ComboBox
252
         * @return
253
         */
254
        public JComboBox getComboLayers() {
255
                if(layers == null) {
256
                        layers = new JComboBox();
257
                        ComboBoxModel comboModel = new DefaultComboBoxModel(getLayerList());
258
                        layers.setModel(comboModel);
259
                        layers.addActionListener(this);
260
                }
261
                return layers;
262
        }
263
        
264
        /**
265
         * Gets a influence areas ComboBox
266
         * @return
267
         */
268
        public JComboBox getComboInfluenceAreas() {
269
                if(influenceAreas == null) {
270
                        influenceAreas = new JComboBox();
271
                        for (int i = 0; i < BufferAlgorithm.sOptions.length; i++) {
272
                                influenceAreas.addItem(BufferAlgorithm.sOptions[i]);
273
                        }
274
                        influenceAreas.addActionListener(this);
275
                }
276
                return influenceAreas;
277
        }
278
        
279
        /**
280
         * Gets a influence areas ComboBox
281
         * @return
282
         */
283
        public JComboBox getComboRadialBuffers() {
284
                if(radialBuffers == null) {
285
                        radialBuffers = new JComboBox();
286
                        radialBuffers.addItem("1");
287
                        radialBuffers.addItem("2");
288
                        radialBuffers.addItem("3");
289
                        radialBuffers.addActionListener(this);
290
                }
291
                return radialBuffers;
292
        }
293
        
294
        /**
295
         * Gets a CheckBox
296
         * @return
297
         */
298
        public JCheckBox getCheckSelectedGeom() {
299
                if(selectionOnly == null) {
300
                        selectionOnly = new JCheckBox(GeoProcessLocator.getGeoProcessManager().getTranslation("Selected_geometries"));
301
                }
302
                return selectionOnly;
303
        }
304
        
305
        /**
306
         * Gets a CheckBox
307
         * @return
308
         */
309
        public JCheckBox getCheckDissolveEntities() {
310
                if(dissolveEntities == null) {
311
                        dissolveEntities = new JCheckBox(GeoProcessLocator.getGeoProcessManager().getTranslation("Dissolve_entities"));
312
                }
313
                return dissolveEntities;
314
        }
315
        
316
        /**
317
         * Gets a CheckBox
318
         * @return
319
         */
320
        public JCheckBox getCheckRoundBorder() {
321
                if(roundBorder == null) {
322
                        roundBorder = new JCheckBox(GeoProcessLocator.getGeoProcessManager().getTranslation("Round_border"));
323
                }
324
                return roundBorder;
325
        }
326
        
327
        /**
328
         * Gets a ComboBox
329
         * @return
330
         */
331
        public JComboBox getComboFields() {
332
                if(fields == null) {
333
                        fields = new JComboBox();
334
                        List<String> fieldList = getFieldList();
335
                        fields.removeAllItems();
336
                        for (int i = 0; i < fieldList.size(); i++) 
337
                                fields.addItem(fieldList.get(i));
338
                        fields.setEnabled(true);
339
                }
340
                return fields;
341
        }
342
        
343
        /**
344
         * Gets a CheckBox
345
         * @return
346
         */
347
        public JTextField getTextDistance() {
348
                if(distance == null) {
349
                        distance = new JTextField("0.0");
350
                        distance.setEnabled(false);
351
                }
352
                return distance;
353
        }
354
        
355
        /**
356
         * Gets a JRadioButton
357
         * @return
358
         */
359
        public JRadioButton getRadioSelectDistance() {
360
                if(selectDistance == null) {
361
                        selectDistance = new JRadioButton(GeoProcessLocator.getGeoProcessManager().getTranslation("area_distance"));
362
                        selectDistance.addActionListener(this);
363
                        selectDistance.setSelected(false);
364
                }
365
                return selectDistance;
366
        }
367
        
368
        /**
369
         * Gets a JRadioButton
370
         * @return
371
         */
372
        public JRadioButton getRadioSelectField() {
373
                if(selectField == null) {
374
                        selectField = new JRadioButton(GeoProcessLocator.getGeoProcessManager().getTranslation("area_field"));
375
                        selectField.addActionListener(this);
376
                        selectField.setSelected(true);
377
                }
378
                return selectField;
379
        }
380
        
381
        //------------------------------------------------------------
382
        
383
        /*
384
         * (non-Javadoc)
385
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
386
         */
387
        public void actionPerformed(ActionEvent e) {
388
                if(e.getSource() == getRadioSelectDistance()) {
389
                        getTextDistance().setEnabled(true);
390
                        getComboFields().setEnabled(false);
391
                }
392
                
393
                if(e.getSource() == getRadioSelectField()) {
394
                        getTextDistance().setEnabled(false);
395
                        getTextDistance().setText("0.0");
396
                        getComboFields().setEnabled(true);
397
                }
398
        }
399
        
400
        
401
        
402
        @Override
403
    public void assignParameters() {
404
                try {
405
                        ParametersSet params = m_Algorithm.getParameters();
406
                        params.getParameter(BufferAlgorithm.LAYER).setParameterValue(getSelectedVectorLayer());
407
                        params.getParameter(BufferAlgorithm.FIELD).setParameterValue(getFieldPosition());
408
                        params.getParameter(BufferAlgorithm.SELECTED_GEOM).setParameterValue(getCheckSelectedGeom().isSelected());
409
                        double dist = 0;
410
                        try {
411
                                dist = new Double(getTextDistance().getText());
412
                        } catch(NumberFormatException e) {
413
                        }
414
                        params.getParameter(BufferAlgorithm.DISTANCE).setParameterValue(dist);
415
                        params.getParameter(BufferAlgorithm.DISSOLVE).setParameterValue(getCheckDissolveEntities().isSelected());
416
                        params.getParameter(BufferAlgorithm.ROUND_BORDER).setParameterValue(getCheckRoundBorder().isSelected());
417
                        params.getParameter(BufferAlgorithm.AREA).setParameterValue(getComboInfluenceAreas().getSelectedIndex());
418
                        params.getParameter(BufferAlgorithm.RING_NUMBER).setParameterValue(getComboRadialBuffers().getSelectedIndex());
419
                        
420
                        OutputObjectsSet ooSet = m_Algorithm.getOutputObjects();
421
                        Output out = ooSet.getOutput(BufferAlgorithm.RESULT);
422
                        
423
                        //Reponer estas l?neas para cambiar el panel de salida y comentar la siguiente
424
                        //AlgorithmOutputPanel fsp = getAlgorithmOutputPanel();
425
                        //out.setOutputChannel(new CompositeSourceOutputChannel(fsp.getOutputParameters()));
426
                 out.setOutputChannel(outputChannelSelectionPanel.getOutputChannel());
427
                } catch (Exception e) {
428
                        Sextante.addErrorToLog(e);
429
                }
430
        }
431
        
432
        
433

    
434
        @Override
435
        public void setOutputValue(String arg0, String arg1) {
436
                
437
        }
438

    
439
        @Override
440
        public void setParameterValue(String arg0, String arg1) {
441
                
442
        }
443
        
444
        /**
445
         * Gets the input layer list
446
         * @return
447
         */
448
        private ObjectAndDescription[] getLayerList() {
449
                IVectorLayer[] layers = SextanteGUI.getInputFactory()
450
                                        .getVectorLayers(IVectorLayer.SHAPE_TYPE_WRONG);
451
                ObjectAndDescription[] oad = new ObjectAndDescription[layers.length];
452
                for (int i = 0; i < layers.length; i++)
453
                        oad[i] = new ObjectAndDescription(layers[i].getName(), layers[i]);
454
                return oad;
455
        }
456
        
457
        /**
458
         * Gets the selected vector layer in the JComboBox
459
         * @return
460
         */
461
        private IVectorLayer getSelectedVectorLayer() {
462
                if(layers.getSelectedItem() != null)
463
                        return (IVectorLayer)((ObjectAndDescription)layers.getSelectedItem()).getObject();
464
                return null;
465
        }
466
        
467
        /**
468
         * Gets the field list of the selected layer
469
         * @return
470
         */
471
        @SuppressWarnings("unchecked")
472
        public List<String> getFieldList() {
473
                IVectorLayer layer = getSelectedVectorLayer();
474
                List<String> data = new ArrayList<String>();
475
                fieldList.clear();
476
                for (int i = 0; i < layer.getFieldCount(); i++) {
477
                        Class type = layer.getFieldType(i);
478
                        fieldList.add(layer.getFieldName(i));
479
                        if(Number.class.isAssignableFrom(type))
480
                                data.add(layer.getFieldName(i));
481
                }
482
                return data;
483
        }
484
        
485
        private int getFieldPosition() {
486
                String label = getComboFields().getSelectedItem().toString();
487
                for (int i = 0; i < fieldList.size(); i++) {
488
                        if(fieldList.get(i).equals(label)) {
489
                                return i;
490
                        }
491
                }
492
                return -1;
493
        }
494
}