Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGeoProcessing / src / com / iver / cit / gvsig / geoprocess / impl / buffer / gui / GeoProcessingBufferPanel2.java @ 10626

History | View | Annotate | Download (10.5 KB)

1
/*
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: GeoProcessingBufferPanel2.java 10626 2007-03-06 16:55:54Z caballero $
47
 * $Log$
48
 * Revision 1.2  2007-03-06 16:47:58  caballero
49
 * Exceptions
50
 *
51
 * Revision 1.1  2006/08/11 16:14:17  azabala
52
 * first version in cvs
53
 *
54
 *
55
 */
56
package com.iver.cit.gvsig.geoprocess.impl.buffer.gui;
57

    
58
import java.awt.Color;
59
import java.awt.GridBagConstraints;
60
import java.awt.Insets;
61
import java.awt.event.ItemEvent;
62

    
63
import javax.swing.ButtonGroup;
64
import javax.swing.DefaultComboBoxModel;
65
import javax.swing.JCheckBox;
66
import javax.swing.JComboBox;
67
import javax.swing.JFormattedTextField;
68
import javax.swing.JLabel;
69
import javax.swing.JRadioButton;
70
import javax.swing.JSpinner;
71
import javax.swing.JTextField;
72
import javax.swing.SpinnerListModel;
73

    
74
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
75
import com.hardcode.gdbms.engine.data.DataSource;
76
import com.iver.andami.PluginServices;
77
import com.iver.cit.gvsig.fmap.layers.FLayers;
78
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
79
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
80
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
81
import com.iver.cit.gvsig.geoprocess.core.fmap.GeoprocessException;
82
import com.iver.cit.gvsig.geoprocess.core.fmap.XTypes;
83
import com.iver.cit.gvsig.geoprocess.core.gui.AbstractGeoprocessGridbagPanel;
84

    
85
public class GeoProcessingBufferPanel2 extends AbstractGeoprocessGridbagPanel
86
                                                implements BufferPanelIF {
87

    
88

    
89

    
90
        //first row->radio button and text field to entry dists
91
        JRadioButton distanceBufferRadioButton;
92
        JTextField bufferDistanceTextField;
93

    
94
        //second row: field values, combo with fields
95
        JRadioButton attributeBufferRadioButton;
96
        JComboBox layerFieldsComboBox;
97

    
98
        //third row: user selections
99
        JCheckBox dissolveEntitiesJCheckBox;
100
        JCheckBox endCapCheckBox;
101

    
102
        //more user selections
103
        JLabel typeBufferLabel;
104
        JComboBox typeBufferComboBox;
105
        JLabel radialBufferLabel;
106
        JSpinner radialBufferSpinner;
107

    
108

    
109
        /**
110
         * Constructor.
111
         *
112
         */
113
        public GeoProcessingBufferPanel2(FLayers layers) {
114
                super(layers, PluginServices.getText(null,
115
                                "Areas_de_influencia._Introduccion_de_datos")
116
                                + ":");
117
        }
118

    
119
        protected void addSpecificDesign() {
120
                Insets insets = new Insets(5, 5, 5, 5);
121

    
122
                //row of constant distance radio button and text field
123
                this.distanceBufferRadioButton = new JRadioButton();
124
                this.distanceBufferRadioButton.setText(PluginServices.getText(this,
125
                                "Area_de_influencia_definida_por_una_distancia")
126
                                + ":");
127
                this.distanceBufferRadioButton
128
                                .addActionListener(new java.awt.event.ActionListener() {
129
                                        public void actionPerformed(java.awt.event.ActionEvent e) {
130
                                                constantDistanceSelected();
131
                                        }
132
                                }
133
                );
134
                this.bufferDistanceTextField = new JTextField();
135
                addComponent(distanceBufferRadioButton,
136
                                bufferDistanceTextField,
137
                                GridBagConstraints.BOTH,
138
                                insets);
139

    
140

    
141
                //row of attribute based distance and fields combo box
142
                this.attributeBufferRadioButton = new JRadioButton();
143
                this.attributeBufferRadioButton.setText(PluginServices.getText(this,
144
                                "Area_de_influencia_definida_por_un_campo")
145
                                + ":");
146
                this.attributeBufferRadioButton.setBounds(new java.awt.Rectangle(2, 41,
147
                                287, 21));
148
                this.attributeBufferRadioButton
149
                                .addActionListener(new java.awt.event.ActionListener() {
150
                                        public void actionPerformed(java.awt.event.ActionEvent e) {
151
                                                attributeDistanceSelected();
152
                                        }
153
                                });
154
                this.layerFieldsComboBox = new JComboBox();
155
                this.layerFieldsComboBox.setBounds(new java.awt.Rectangle(308, 41, 138,
156
                                21));
157
                DefaultComboBoxModel defaultModel = new DefaultComboBoxModel(
158
                                getFieldNames());
159
                this.layerFieldsComboBox.setModel(defaultModel);
160
                addComponent(attributeBufferRadioButton,
161
                                                        layerFieldsComboBox,
162
                                                        GridBagConstraints.BOTH,
163
                                                        insets);
164
                ButtonGroup buttonGroup = new ButtonGroup();
165
                buttonGroup.add(distanceBufferRadioButton);
166
                buttonGroup.add(attributeBufferRadioButton);
167

    
168

    
169

    
170

    
171
                //row of options check boxes
172
                this.dissolveEntitiesJCheckBox = new JCheckBox();
173
                this.dissolveEntitiesJCheckBox.setText(PluginServices.getText(this,
174
                                "Disolver_entidades"));
175

    
176
                this.endCapCheckBox = new JCheckBox();
177
                this.endCapCheckBox.setText(PluginServices.getText(this,
178
                                "No_usar_buffer_redondeado"));
179
                addComponent(dissolveEntitiesJCheckBox,
180
                                                                endCapCheckBox,
181
                                                                GridBagConstraints.NONE,
182
                                                                insets);
183

    
184
                this.typeBufferLabel = new JLabel();
185
                this.typeBufferLabel.setText(PluginServices
186
                                .getText(this, "Crear_Buffer"));
187

    
188
                this.typeBufferComboBox = new JComboBox();
189
                this.typeBufferComboBox.addItem(BUFFER_INSIDE);
190
                this.typeBufferComboBox.addItem(BUFFER_OUTSIDE);
191
                this.typeBufferComboBox.addItem(BUFFER_INSIDE_OUTSIDE);
192
                this.typeBufferComboBox.setSelectedItem(BUFFER_OUTSIDE);
193
                addComponent(typeBufferLabel,
194
                                        typeBufferComboBox,
195
                                        GridBagConstraints.NONE,
196
                                        insets);
197

    
198

    
199
                this.radialBufferLabel = new JLabel();
200
                this.radialBufferLabel.setText(PluginServices.getText(this,
201
                                "Numero_anillos_concentricos"));
202
                Integer one = new Integer(1);
203
                Integer two = new Integer(2);
204
                Integer three = new Integer(3);
205
                SpinnerListModel listModel = new SpinnerListModel(new Integer[] {
206
                                one, two, three });
207
                this.radialBufferSpinner = new JSpinner(listModel);
208
                this.radialBufferSpinner.setBounds(new java.awt.Rectangle(298, 3, 137,
209
                                19));
210
                // Disable keyboard edits in the spinner
211
                JFormattedTextField tf = ((JSpinner.DefaultEditor) radialBufferSpinner
212
                                .getEditor()).getTextField();
213
                tf.setEditable(false);
214
                tf.setBackground(Color.white);
215
                addComponent(radialBufferLabel,
216
                                        radialBufferSpinner,
217
                                        GridBagConstraints.BOTH,
218
                                        insets);
219

    
220
                initSelectedItemsJCheckBox();
221
                updateNumSelectedFeaturesLabel();
222
                this.distanceBufferRadioButton.setSelected(true);
223
                this.layerFieldsComboBox.setEnabled(false);
224
                this.verifyTypeBufferComboEnabled();
225

    
226

    
227

    
228

    
229
        }
230

    
231
        /**
232
         * Returns the number of radial buffers selected by user (by now, only a
233
         * maximum of three radial buffers allowed)
234
         *
235
         * @return
236
         */
237
        public int getNumberOfRadialBuffers() {
238
                return ((Integer) this.radialBufferSpinner.getValue()).intValue();
239
        }
240

    
241
        private JTextField getBufferDistanceTextField(){
242
                return this.bufferDistanceTextField;
243
        }
244

    
245
        private JComboBox getLayerFieldsComboBox(){
246
                return this.layerFieldsComboBox;
247
        }
248

    
249

    
250
        public void constantDistanceSelected() {
251
                getBufferDistanceTextField().setEnabled(true);
252
                getLayerFieldsComboBox().setEnabled(false);
253

    
254
        }
255

    
256
        public void attributeDistanceSelected() {
257
                getBufferDistanceTextField().setEnabled(false);
258
                getLayerFieldsComboBox().setEnabled(true);
259

    
260
        }
261

    
262

    
263
        public String getTypePolygonBuffer() {
264
                return (String) this.typeBufferComboBox.getSelectedItem();
265
        }
266

    
267
        public boolean isSquareCap() {
268
                return this.endCapCheckBox.isSelected();
269
        }
270

    
271
        private String[] getFieldNames() {
272
                AlphanumericData lyr = (AlphanumericData) (layers
273
                                .getLayer((String) layersComboBox.getSelectedItem()));
274
                DataSource ds;
275
                String[] fieldNames = null;
276
                try {
277
                        ds = lyr.getRecordset();
278
                        fieldNames = new String[ds.getFieldCount()];
279
                        for (int i = 0; i < ds.getFieldCount(); i++) {
280
                                fieldNames[i] = ds.getFieldName(i);
281
                        }
282
                } catch (ReadDriverException 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 (ReadDriverException e) {
308
                        // TODO Auto-generated catch block
309
                        e.printStackTrace();
310
                }
311
                enableTypePolygonBufferPanel(enable);
312
        }
313

    
314
        public void enableTypePolygonBufferPanel(boolean enable) {
315
                this.typeBufferComboBox.setEnabled(enable);
316
        }
317

    
318
        public boolean isConstantDistanceSelected() {
319
                return this.distanceBufferRadioButton.isSelected();
320
        }
321

    
322
        public boolean isAttributeDistanceSelected() {
323
                return this.attributeBufferRadioButton.isSelected();
324
        }
325

    
326
        public double getConstantDistance() throws GeoprocessException {
327
                try {
328
                        String strDist = this.bufferDistanceTextField.getText();
329
                        return Double.parseDouble(strDist);
330
                } catch (NumberFormatException ex) {
331
                        throw new GeoprocessException(
332
                                        "Distancia de buffer introducida no numerica");
333
                }
334
        }
335

    
336
        public String getAttributeDistanceField() throws GeoprocessException {
337
                String attributeField = (String) this.layerFieldsComboBox.getSelectedItem();
338
                FLyrVect selectedLayer = getInputLayer();
339
                try {
340
                        SelectableDataSource selectable = selectedLayer.getRecordset();
341

    
342
                        int fieldIndex = selectable.getFieldIndexByName(attributeField);
343
                        int fieldType = selectable.getFieldType(fieldIndex);
344

    
345
                        if (!XTypes.isNumeric(fieldType))
346
                                throw new GeoprocessException(
347
                                                "Atributo no numerico para distancia de buffer");
348

    
349
                } catch (ReadDriverException e) {
350
                        // TODO Auto-generated catch block
351
                        e.printStackTrace();
352
                }
353
                return attributeField;
354
        }
355

    
356
        public boolean isBufferOnlySelected() {
357
                return super.isFirstOnlySelected();
358
        }
359

    
360
        public boolean isDissolveBuffersSelected() {
361
                return this.dissolveEntitiesJCheckBox.isSelected();
362
        }
363

    
364
}