Statistics
| Revision:

root / trunk / extensions / extGeoProcessing / src / com / iver / cit / gvsig / geoprocess / impl / buffer / gui / GeoProcessingBufferPanel2.java @ 6746

History | View | Annotate | Download (10.8 KB)

1 6746 azabala
/*
2
 * Created on 28-jul-2006
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
21
 *
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
/* CVS MESSAGES:
45
 *
46
 * $Id$
47
 * $Log$
48
 * Revision 1.1  2006-08-11 16:14:17  azabala
49
 * first version in cvs
50
 *
51
 *
52
 */
53
package com.iver.cit.gvsig.geoprocess.impl.buffer.gui;
54
55
import java.awt.Color;
56
import java.awt.GridBagConstraints;
57
import java.awt.Insets;
58
import java.awt.event.ItemEvent;
59
60
import javax.swing.ButtonGroup;
61
import javax.swing.DefaultComboBoxModel;
62
import javax.swing.JCheckBox;
63
import javax.swing.JComboBox;
64
import javax.swing.JFormattedTextField;
65
import javax.swing.JLabel;
66
import javax.swing.JRadioButton;
67
import javax.swing.JSpinner;
68
import javax.swing.JTextField;
69
import javax.swing.SpinnerListModel;
70
71
import com.hardcode.gdbms.engine.data.DataSource;
72
import com.iver.andami.PluginServices;
73
import com.iver.cit.gvsig.fmap.DriverException;
74
import com.iver.cit.gvsig.fmap.layers.FLayers;
75
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
76
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
77
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
78
import com.iver.cit.gvsig.geoprocess.core.fmap.GeoprocessException;
79
import com.iver.cit.gvsig.geoprocess.core.fmap.XTypes;
80
import com.iver.cit.gvsig.geoprocess.core.gui.AbstractGeoprocessGridbagPanel;
81
82
public class GeoProcessingBufferPanel2 extends AbstractGeoprocessGridbagPanel
83
                                                implements BufferPanelIF {
84
85
86
87
        //first row->radio button and text field to entry dists
88
        JRadioButton distanceBufferRadioButton;
89
        JTextField bufferDistanceTextField;
90
91
        //second row: field values, combo with fields
92
        JRadioButton attributeBufferRadioButton;
93
        JComboBox layerFieldsComboBox;
94
95
        //third row: user selections
96
        JCheckBox dissolveEntitiesJCheckBox;
97
        JCheckBox endCapCheckBox;
98
99
        //more user selections
100
        JLabel typeBufferLabel;
101
        JComboBox typeBufferComboBox;
102
        JLabel radialBufferLabel;
103
        JSpinner radialBufferSpinner;
104
105
106
        /**
107
         * Constructor.
108
         *
109
         */
110
        public GeoProcessingBufferPanel2(FLayers layers) {
111
                super(layers, PluginServices.getText(null,
112
                                "Areas_de_influencia._Introduccion_de_datos")
113
                                + ":");
114
        }
115
116
        protected void addSpecificDesign() {
117
                Insets insets = new Insets(5, 5, 5, 5);
118
119
                //row of constant distance radio button and text field
120
                this.distanceBufferRadioButton = new JRadioButton();
121
                this.distanceBufferRadioButton.setText(PluginServices.getText(this,
122
                                "Area_de_influencia_definida_por_una_distancia")
123
                                + ":");
124
                this.distanceBufferRadioButton
125
                                .addActionListener(new java.awt.event.ActionListener() {
126
                                        public void actionPerformed(java.awt.event.ActionEvent e) {
127
                                                constantDistanceSelected();
128
                                        }
129
                                }
130
                );
131
                this.bufferDistanceTextField = new JTextField();
132
                addComponent(distanceBufferRadioButton,
133
                                bufferDistanceTextField,
134
                                GridBagConstraints.BOTH,
135
                                insets);
136
137
138
                //row of attribute based distance and fields combo box
139
                this.attributeBufferRadioButton = new JRadioButton();
140
                this.attributeBufferRadioButton.setText(PluginServices.getText(this,
141
                                "Area_de_influencia_definida_por_un_campo")
142
                                + ":");
143
                this.attributeBufferRadioButton.setBounds(new java.awt.Rectangle(2, 41,
144
                                287, 21));
145
                this.attributeBufferRadioButton
146
                                .addActionListener(new java.awt.event.ActionListener() {
147
                                        public void actionPerformed(java.awt.event.ActionEvent e) {
148
                                                attributeDistanceSelected();
149
                                        }
150
                                });
151
                this.layerFieldsComboBox = new JComboBox();
152
                this.layerFieldsComboBox.setBounds(new java.awt.Rectangle(308, 41, 138,
153
                                21));
154
                DefaultComboBoxModel defaultModel = new DefaultComboBoxModel(
155
                                getFieldNames());
156
                this.layerFieldsComboBox.setModel(defaultModel);
157
                addComponent(attributeBufferRadioButton,
158
                                                        layerFieldsComboBox,
159
                                                        GridBagConstraints.BOTH,
160
                                                        insets);
161
                ButtonGroup buttonGroup = new ButtonGroup();
162
                buttonGroup.add(distanceBufferRadioButton);
163
                buttonGroup.add(attributeBufferRadioButton);
164
165
166
167
168
                //row of options check boxes
169
                this.dissolveEntitiesJCheckBox = new JCheckBox();
170
                this.dissolveEntitiesJCheckBox.setText(PluginServices.getText(this,
171
                                "Disolver_entidades"));
172
173
                this.endCapCheckBox = new JCheckBox();
174
                this.endCapCheckBox.setText(PluginServices.getText(this,
175
                                "No_usar_buffer_redondeado"));
176
                addComponent(dissolveEntitiesJCheckBox,
177
                                                                endCapCheckBox,
178
                                                                GridBagConstraints.NONE,
179
                                                                insets);
180
181
                this.typeBufferLabel = new JLabel();
182
                this.typeBufferLabel.setText(PluginServices
183
                                .getText(this, "Crear_Buffer"));
184
185
                this.typeBufferComboBox = new JComboBox();
186
                this.typeBufferComboBox.addItem(BUFFER_INSIDE);
187
                this.typeBufferComboBox.addItem(BUFFER_OUTSIDE);
188
                this.typeBufferComboBox.addItem(BUFFER_INSIDE_OUTSIDE);
189
                this.typeBufferComboBox.setSelectedItem(BUFFER_OUTSIDE);
190
                addComponent(typeBufferLabel,
191
                                        typeBufferComboBox,
192
                                        GridBagConstraints.NONE,
193
                                        insets);
194
195
196
                this.radialBufferLabel = new JLabel();
197
                this.radialBufferLabel.setText(PluginServices.getText(this,
198
                                "Numero_anillos_concentricos"));
199
                Integer one = new Integer(1);
200
                Integer two = new Integer(2);
201
                Integer three = new Integer(3);
202
                SpinnerListModel listModel = new SpinnerListModel(new Integer[] {
203
                                one, two, three });
204
                this.radialBufferSpinner = new JSpinner(listModel);
205
                this.radialBufferSpinner.setBounds(new java.awt.Rectangle(298, 3, 137,
206
                                19));
207
                // Disable keyboard edits in the spinner
208
                JFormattedTextField tf = ((JSpinner.DefaultEditor) radialBufferSpinner
209
                                .getEditor()).getTextField();
210
                tf.setEditable(false);
211
                tf.setBackground(Color.white);
212
                addComponent(radialBufferLabel,
213
                                        radialBufferSpinner,
214
                                        GridBagConstraints.BOTH,
215
                                        insets);
216
217
                initSelectedItemsJCheckBox();
218
                updateNumSelectedFeaturesLabel();
219
                this.distanceBufferRadioButton.setSelected(true);
220
                this.layerFieldsComboBox.setEnabled(false);
221
                this.verifyTypeBufferComboEnabled();
222
223
224
225
226
        }
227
228
        /**
229
         * Returns the number of radial buffers selected by user (by now, only a
230
         * maximum of three radial buffers allowed)
231
         *
232
         * @return
233
         */
234
        public int getNumberOfRadialBuffers() {
235
                return ((Integer) this.radialBufferSpinner.getValue()).intValue();
236
        }
237
238
        private JTextField getBufferDistanceTextField(){
239
                return this.bufferDistanceTextField;
240
        }
241
242
        private JComboBox getLayerFieldsComboBox(){
243
                return this.layerFieldsComboBox;
244
        }
245
246
247
        public void constantDistanceSelected() {
248
                getBufferDistanceTextField().setEnabled(true);
249
                getLayerFieldsComboBox().setEnabled(false);
250
251
        }
252
253
        public void attributeDistanceSelected() {
254
                getBufferDistanceTextField().setEnabled(false);
255
                getLayerFieldsComboBox().setEnabled(true);
256
257
        }
258
259
260
        public String getTypePolygonBuffer() {
261
                return (String) this.typeBufferComboBox.getSelectedItem();
262
        }
263
264
        public boolean isSquareCap() {
265
                return this.endCapCheckBox.isSelected();
266
        }
267
268
        private String[] getFieldNames() {
269
                AlphanumericData lyr = (AlphanumericData) (layers
270
                                .getLayer((String) layersComboBox.getSelectedItem()));
271
                DataSource ds;
272
                String[] fieldNames = null;
273
                try {
274
                        ds = lyr.getRecordset();
275
                        fieldNames = new String[ds.getFieldCount()];
276
                        for (int i = 0; i < ds.getFieldCount(); i++) {
277
                                fieldNames[i] = ds.getFieldName(i);
278
                        }
279
                } catch (DriverException e) {
280
                        // TODO Auto-generated catch block
281
                        e.printStackTrace();
282
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
283
                        // TODO Auto-generated catch block
284
                        e.printStackTrace();
285
                }
286
                return fieldNames;
287
        }
288
289
        protected void processLayerComboBoxStateChange(ItemEvent e) {
290
//                 Cambiar el estado del CheckBox
291
//                lo comento pq esto se ha a?adido a la clase padre
292
//                initSelectedItemsJCheckBox();
293
294
                // Cambiar el estado del layerFieldsComboBox
295
                DefaultComboBoxModel defaultModel = new DefaultComboBoxModel(
296
                                getFieldNames());
297
                layerFieldsComboBox.setModel(defaultModel);
298
                verifyTypeBufferComboEnabled();
299
        }
300
301
        private void verifyTypeBufferComboEnabled(){
302
                String layerName = (String) this.layersComboBox.getSelectedItem();
303
                FLyrVect layer = (FLyrVect) this.layers.getLayer(layerName);
304
                boolean enable = false;
305
                try {
306
                        enable = layer.getShapeType() == XTypes.POLYGON;
307
                } catch (DriverException e1) {
308
                        e1.printStackTrace();
309
                }
310
                enableTypePolygonBufferPanel(enable);
311
        }
312
313
        public void enableTypePolygonBufferPanel(boolean enable) {
314
                this.typeBufferComboBox.setEnabled(enable);
315
        }
316
317
        public boolean isConstantDistanceSelected() {
318
                return this.distanceBufferRadioButton.isSelected();
319
        }
320
321
        public boolean isAttributeDistanceSelected() {
322
                return this.attributeBufferRadioButton.isSelected();
323
        }
324
325
        public double getConstantDistance() throws GeoprocessException {
326
                try {
327
                        String strDist = this.bufferDistanceTextField.getText();
328
                        return Double.parseDouble(strDist);
329
                } catch (NumberFormatException ex) {
330
                        throw new GeoprocessException(
331
                                        "Distancia de buffer introducida no numerica");
332
                }
333
        }
334
335
        public String getAttributeDistanceField() throws GeoprocessException {
336
                String attributeField = (String) this.layerFieldsComboBox.getSelectedItem();
337
                FLyrVect selectedLayer = getInputLayer();
338
                try {
339
                        SelectableDataSource selectable = selectedLayer.getRecordset();
340
341
                        int fieldIndex = selectable.getFieldIndexByName(attributeField);
342
                        int fieldType = selectable.getFieldType(fieldIndex);
343
344
                        if (!XTypes.isNumeric(fieldType))
345
                                throw new GeoprocessException(
346
                                                "Atributo no numerico para distancia de buffer");
347
348
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
349
                        throw new GeoprocessException(
350
                                        "Problemas accediendo al campo que define la distancia de buffer");
351
                } catch (DriverException e) {
352
                        throw new GeoprocessException(
353
                                        "Problemas accediendo al campo que define la distancia de buffer");
354
                }
355
                return attributeField;
356
        }
357
358
        public boolean isBufferOnlySelected() {
359
                return super.isFirstOnlySelected();
360
        }
361
362
        public boolean isDissolveBuffersSelected() {
363
                return this.dissolveEntitiesJCheckBox.isSelected();
364
        }
365
366
}