Statistics
| Revision:

root / trunk / extensions / extGeoProcessing / src / com / iver / cit / gvsig / geoprocess / core / gui / AbstractGeoprocessGridbagPanel.java @ 8235

History | View | Annotate | Download (15.1 KB)

1
/*
2
 * Created on 30-jun-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: AbstractGeoprocessGridbagPanel.java 7304 2006-09-15 10:44:24Z caballero $
47
 * $Log$
48
 * Revision 1.2  2006-09-15 10:42:54  caballero
49
 * extensibilidad de documentos
50
 *
51
 * Revision 1.1  2006/08/11 16:11:38  azabala
52
 * first version in cvs
53
 *
54
 * Revision 1.2  2006/07/04 16:43:18  azabala
55
 * *** empty log message ***
56
 *
57
 * Revision 1.1  2006/07/03 20:28:20  azabala
58
 * *** empty log message ***
59
 *
60
 *
61
 */
62
package com.iver.cit.gvsig.geoprocess.core.gui;
63

    
64
import java.awt.BorderLayout;
65
import java.awt.Component;
66
import java.awt.GridBagConstraints;
67
import java.awt.Insets;
68
import java.awt.Rectangle;
69
import java.awt.event.ItemEvent;
70
import java.awt.event.ItemListener;
71
import java.io.File;
72
import java.io.FileNotFoundException;
73
import java.util.ArrayList;
74
import java.util.Arrays;
75

    
76
import javax.swing.BorderFactory;
77
import javax.swing.DefaultComboBoxModel;
78
import javax.swing.JButton;
79
import javax.swing.JCheckBox;
80
import javax.swing.JComboBox;
81
import javax.swing.JFileChooser;
82
import javax.swing.JLabel;
83
import javax.swing.JOptionPane;
84
import javax.swing.JPanel;
85
import javax.swing.JTextField;
86
import javax.swing.border.EtchedBorder;
87

    
88
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
89

    
90
import com.iver.andami.PluginServices;
91
import com.iver.cit.gvsig.fmap.DriverException;
92
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
93
import com.iver.cit.gvsig.fmap.layers.FBitSet;
94
import com.iver.cit.gvsig.fmap.layers.FLayer;
95
import com.iver.cit.gvsig.fmap.layers.FLayers;
96
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
97
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
98
import com.iver.cit.gvsig.fmap.layers.layerOperations.SingleLayer;
99
import com.iver.cit.gvsig.project.documents.view.legend.CreateSpatialIndexMonitorableTask;
100
import com.iver.utiles.GenericFileFilter;
101
import com.iver.utiles.swing.threads.IMonitorableTask;
102

    
103
/**
104
 * Abstract base panel to easily build GeoprocessPanels
105
 * (View GUI component with which user could launch a given
106
 * geoprocess).
107
 * 
108
 * All panels that extends this class will have a header 
109
 * (textual label that describes the geoprocess).
110
 * 
111
 * In the next row, they will have a layer combo box, where users could
112
 * select the input layer of the geoprocess (all geoprocesses at least
113
 * will work with an input layer), and a check box to specify working only
114
 * with input layer selection.
115
 * 
116
 * Nex, each descendant panels must implement addSpecificDesign()
117
 * abstract method, to add specific components with which users could
118
 * introduce the information needed by the geoprocess.
119
 * 
120
 * Finally, the last row has a panel to allow users to select where
121
 * to save the results of the geoprocess.
122
 * 
123
 * 
124
 * 
125
 * @author azabala
126
 *
127
 */
128
public abstract class AbstractGeoprocessGridbagPanel extends GridBagLayoutPanel implements
129
IGeoprocessPanel{
130
        
131
        protected final int DEFAULT_FILL = GridBagConstraints.BOTH;
132

    
133
        /**
134
         * textual description of the associated geoprocess
135
         */
136
        protected String titleText;
137
        
138
        /**
139
         * View's layers showed in TOC
140
         */
141
        protected FLayers layers;
142

    
143
        /**
144
         * Combo box to show layer names to user
145
         */
146
        protected JComboBox layersComboBox;
147
        
148
        /**
149
         * Check box to specify that geoprocess will only process
150
         * input layer selection 
151
         */
152
        protected JCheckBox selectedOnlyCheckBox;
153
        
154
        /**
155
         * Shows the number of selected features of input layer
156
         */
157
        protected JLabel numSelectedLabel;
158
                
159
        /**
160
         * Text field to show user the full path (or a representative string)
161
         * of the result layer selection
162
         */
163
        
164
        protected JTextField resultTf;
165
        
166
        /**
167
         * Default constructor
168
         *
169
         */
170

    
171
        public AbstractGeoprocessGridbagPanel(FLayers layers, String titleText) {
172
                super();
173
                this.layers = layers;
174
                this.titleText = titleText;
175
                initialize();
176
        }
177
        
178
        protected void initialize(){
179
                Insets insets = new Insets(5, 5, 5, 5);
180
                addComponent(new JLabel(titleText), insets);
181
                JLabel firstLayerLab = new JLabel(PluginServices.
182
                                getText(this, "Cobertura_de_entrada")+":");
183
                JComboBox layersComboBox = getLayersComboBox();
184
                addComponent(firstLayerLab, layersComboBox, GridBagConstraints.BOTH, insets);
185
                addComponent(getSelectedOnlyCheckBox(), GridBagConstraints.BOTH, insets);
186
                
187
                String numSelectedText = PluginServices.
188
                        getText(this, "Numero_de_elementos_seleccionados") + ":";
189
                numSelectedLabel = new JLabel("00");
190
                addComponent(numSelectedText, numSelectedLabel, insets);
191
                
192
                addSpecificDesign();
193
                
194
                JPanel aux = new JPanel(new BorderLayout());
195
                String resultLayerText = PluginServices.
196
                                getText(this, "Cobertura_de_salida") + ":";
197
                resultTf = getFileNameResultTextField();
198
                JButton openButton = getOpenResultButton();
199
        aux.add(resultTf, BorderLayout.WEST);
200
        aux.add(new JLabel(" "), BorderLayout.CENTER);
201
        aux.add(openButton, BorderLayout.EAST);
202
        addComponent(resultLayerText, aux, GridBagConstraints.HORIZONTAL, insets );
203
                setBounds(0, 0, 520, 410);
204
        }
205
        
206
        
207
        protected void initSelectedItemsJCheckBox() {
208
                String selectedLayer = (String) layersComboBox.getSelectedItem();
209
                FLyrVect inputLayer = (FLyrVect) layers.getLayer(selectedLayer);
210
                FBitSet fBitSet = null;
211
                try {
212
                        fBitSet = inputLayer.getRecordset().getSelection();
213
                } catch (DriverException e) {
214
                        // TODO Auto-generated catch block
215
                        e.printStackTrace();
216
                }
217
                
218
                if (fBitSet.cardinality() == 0) {
219
                        selectedOnlyCheckBox.setEnabled(false);
220
                        selectedOnlyCheckBox.setSelected(false);
221
                } else {
222
                        selectedOnlyCheckBox.setEnabled(true);
223
                        selectedOnlyCheckBox.setSelected(true);
224
                }
225
                selectedOnlyCheckBox.setSelected(false);
226
        
227
                updateNumSelectedFeaturesLabel();
228
        }
229
        
230
        
231
    protected void updateNumSelectedFeaturesLabel() {
232
        if (selectedOnlyCheckBox.isSelected()) {
233
            FLyrVect inputSelectable = (FLyrVect)(layers.getLayer((String)layersComboBox.getSelectedItem()));
234
            FBitSet fBitSet = null;
235
                        try {
236
                                fBitSet = inputSelectable.getRecordset().getSelection();
237
                        } catch (DriverException e) {
238
                                // TODO Auto-generated catch block
239
                                e.printStackTrace();
240
                        }
241
                        numSelectedLabel.setText(new Integer(fBitSet.cardinality()).toString());
242
        } else {
243
                ReadableVectorial va = ((SingleLayer)(layers.
244
                                getLayer((String)layersComboBox.
245
                                                getSelectedItem()))).
246
                                                                getSource();
247
            try {
248
                    numSelectedLabel.setText(new Integer(va.getShapeCount()).toString());
249
            } catch (DriverIOException e) {
250
                // TODO Auto-generated catch block
251
                e.printStackTrace();
252
            }
253
        }
254
    }
255
        
256
        
257
        private JButton getOpenResultButton() {
258
                JButton        openResultButton = new JButton();
259
                openResultButton.setText(PluginServices.getText(this, "Abrir"));
260
                openResultButton.addActionListener(new java.awt.event.ActionListener() {
261
                                public void actionPerformed(java.awt.event.ActionEvent e) {
262
                                        openResultFile();
263
                                }
264
                        }
265
                );
266
                return openResultButton;
267
        }
268
        
269
        
270
        /**
271
         * This method must be overwrited by all descendant classes to
272
         * add specific desing of each Geoprocess Panel.
273
         */
274
        protected abstract void addSpecificDesign();
275

    
276
        /**
277
         * Returns the layer selected in layer combo box
278
         * 
279
         * @return
280
         */
281
        public FLyrVect getInputLayer() {
282
                FLyrVect solution = null;
283
                String selectedLayer = (String) layersComboBox.getSelectedItem();
284
                solution = (FLyrVect) layers.getLayer(selectedLayer);
285
                return solution;
286
        }
287

    
288
        /**
289
         * Sets view's layers from the TOC
290
         * 
291
         * @param layers
292
         */
293
        public void setFLayers(FLayers layers) {
294
                this.layers = layers;
295
        }
296

    
297
        /**
298
         * Returns layers.
299
         * 
300
         * @return
301
         */
302
        public FLayers getFLayers() {
303
                return layers;
304
        }
305

    
306
        /**
307
         * Filters TOC layers to get only FLyrVect layers.
308
         * 
309
         * @param layers
310
         * @return
311
         * 
312
         * FIXME Mover a una utility class
313
         */
314
        protected FLyrVect[] getVectorialLayers(FLayers layers) {
315
                FLyrVect[] solution = null;
316
                ArrayList list = new ArrayList();
317
                int numLayers = layers.getLayersCount();
318
                for (int i = 0; i < numLayers; i++) {
319
                        FLayer layer = layers.getLayer(i);
320
                        if (layer instanceof FLyrVect)
321
                                list.add(layer);
322
                        else if (layer instanceof FLayers)
323
                                list.addAll(Arrays.asList(getVectorialLayers((FLayers) layer)));
324
                }
325
                solution = new FLyrVect[list.size()];
326
                list.toArray(solution);
327
                return solution;
328

    
329
        }
330

    
331
        /**
332
         * Obtains layer names from FLayers and returns like an array of strings.
333
         * 
334
         * @return
335
         * 
336
         * TODO Llevar a una utility class
337
         */
338
        protected String[] getLayerNames() {
339
                String[] solution = null;
340
                int numLayers = layers.getLayersCount();
341
                if (layers != null && numLayers > 0) {
342
                        ArrayList list = new ArrayList();
343
                        for (int i = 0; i < numLayers; i++) {
344
                                FLayer layer = layers.getLayer(i);
345
                                if (layer instanceof FLyrVect)
346
                                        list.add(layer.getName());
347
                                if (layer instanceof FLayers) {
348
                                        FLayers layers = (FLayers) layer;
349
                                        FLyrVect[] vectorials = getVectorialLayers(layers);
350
                                        for (int j = 0; j < vectorials.length; j++) {
351
                                                list.add(vectorials[j].getName());
352
                                        }
353
                                }
354
                        }// for
355
                        solution = new String[list.size()];
356
                        list.toArray(solution);
357
                }
358
                return solution;
359
        }
360

    
361
        /**
362
         * Shows to the user a dialog error, whith the title and message body
363
         * specified as parameters.
364
         * 
365
         * @param message
366
         *            body of the error message
367
         * @param title
368
         *            title of the error message
369
         */
370
        public void error(String message, String title) {
371
                JOptionPane.showMessageDialog(this, message, title,
372
                                JOptionPane.ERROR_MESSAGE);
373
        }
374

    
375
        /**
376
         * Asks to the user for the creation of a spatial index for the specified
377
         * layer. It will be of help for certain geoprocesses.
378
         * 
379
         * FLyrVect default spatial index is MapServer quadtree, that hasnt the
380
         * ability to do nearest neighbour searches.
381
         * 
382
         * For those geoprocesses that needs it (NN searches) overwrite this method
383
         * and use JSI RTree, or SIL RTree.
384
         * 
385
         * It returns an IMonitorableTask, a task to build the spatial index in
386
         * background.
387
         * 
388
         * @param layer
389
         * @return an ITask
390
         */
391
        public IMonitorableTask askForSpatialIndexCreation(FLyrVect layer) {
392
                String title = PluginServices.getText(this, "Crear_Indice");
393
                String confirmDialogText = PluginServices.getText(this,
394
                                "Crear_Indice_Pregunta_1")
395
                                + " "
396
                                + layer.getName()
397
                                + " "
398
                                + PluginServices.getText(this, "Crear_Indice_Pregunta_2");
399
                int option = JOptionPane.showConfirmDialog(this, confirmDialogText,
400
                                title, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
401
                                null);
402
                if (option == JOptionPane.YES_OPTION) {
403
                        // Usually we want task like spatial index creation dont block
404
                        // GUI responses. Now, we dont want to start geoprocess execution
405
                        // until spatial index creation would finish, to have advantage
406
                        // of the spatial index
407
                        try {
408
                                CreateSpatialIndexMonitorableTask task = new CreateSpatialIndexMonitorableTask(
409
                                                layer);
410
                                return task;
411
                        } catch (DriverIOException e) {
412
                                // TODO Auto-generated catch block
413
                                e.printStackTrace();
414
                                return null;
415
                        } catch (DriverException e) {
416
                                // TODO Auto-generated catch block
417
                                e.printStackTrace();
418
                                return null;
419
                        }
420
                } else
421
                        return null;
422

    
423
        }
424

    
425
        /**
426
         * Confirm overwrite the output file if it allready exist.
427
         * 
428
         * 
429
         * @param outputFile
430
         * @return answer
431
         */
432
        public boolean askForOverwriteOutputFile(File outputFile) {
433
                String title = PluginServices.getText(this, "Sobreescribir_fichero");
434
                String confirmDialogText = PluginServices.getText(this,
435
                                "Sobreescribir_fichero_Pregunta_1")
436
                                + "\n'"
437
                                + outputFile.getAbsolutePath()
438
                                + "'\n"
439
                                + PluginServices.getText(this,
440
                                                "Sobreescribir_fichero_Pregunta_2");
441
                int option = JOptionPane.showConfirmDialog(this, confirmDialogText,
442
                                title, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
443
                                null);
444
                if (option == JOptionPane.YES_OPTION) {
445
                        return true;
446
                }
447
                return false;
448
        }
449
        
450
        
451
        /**
452
         * Opens a dialog to select where (file, database, etc)
453
         * to save the result layer.
454
         *
455
         */
456
        public void openResultFile() {
457
                JFileChooser jfc = new JFileChooser();
458
                jfc
459
                                .addChoosableFileFilter(new GenericFileFilter("shp",
460
                                                "Ficheros SHP"));
461
                if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
462
                        File file = jfc.getSelectedFile();
463
                        if (!(file.getPath().endsWith(".shp") || file.getPath().endsWith(
464
                                        ".SHP"))) {
465
                                file = new File(file.getPath() + ".shp");
466
                        }
467
                }// if
468
                if (jfc.getSelectedFile() != null) {
469
                        getFileNameResultTextField().setText(
470
                                        jfc.getSelectedFile().getAbsolutePath());
471
                }
472

    
473
        }
474

    
475
        
476
        protected JTextField getFileNameResultTextField() {
477
                if(resultTf == null)
478
                        resultTf = new JTextField(25);
479
                return resultTf;
480
        }
481

    
482
        public File getOutputFile() throws FileNotFoundException{
483
                String fileName = getFileNameResultTextField().getText();
484
                if(fileName.length() == 0){
485
                        throw new FileNotFoundException("No se ha seleccionado ningun fichero de salida");
486
                }
487
                if(! fileName.endsWith(".shp")){
488
                        if(! fileName.endsWith("."))
489
                                fileName += ".";
490
                        fileName += "shp";
491
                }
492
                return new File(fileName);
493
        }
494
        
495
        protected JComboBox getLayersComboBox() {
496
                if (layersComboBox == null) {
497
                        layersComboBox = new JComboBox();
498
                        DefaultComboBoxModel defaultModel = new DefaultComboBoxModel(
499
                                        getLayerNames());
500
                        layersComboBox.setModel(defaultModel);
501
                        layersComboBox.setBounds(142, 63, 260, 21);
502
                        layersComboBox.addItemListener(new java.awt.event.ItemListener() {
503
                                public void itemStateChanged(ItemEvent e) {
504
                                        if (e.getStateChange() == ItemEvent.SELECTED) {
505
                                                initSelectedItemsJCheckBox();
506
                                                updateNumSelectedFeaturesLabel();
507
                                                processLayerComboBoxStateChange(e);
508
                                        }
509
                                }// itemStateChange
510
                        });
511
                }
512
                return layersComboBox;
513
        }
514
        
515
        protected JCheckBox getSelectedOnlyCheckBox() {
516
                if (selectedOnlyCheckBox == null) {
517
                        selectedOnlyCheckBox = new JCheckBox();
518
                        selectedOnlyCheckBox.addItemListener(new ItemListener(){
519
                                public void itemStateChanged(ItemEvent arg0) {
520
                                        updateNumSelectedFeaturesLabel();
521
                                }});
522
                        selectedOnlyCheckBox.setText(PluginServices.getText(this, "Usar_solamente_los_elementos_seleccionados"));
523
                }
524
                return selectedOnlyCheckBox;
525
        }
526
        
527
        public boolean isFirstOnlySelected(){
528
                return getSelectedOnlyCheckBox().isSelected();
529
        }
530
        
531
        protected abstract void processLayerComboBoxStateChange(ItemEvent e);
532
}