Statistics
| Revision:

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

History | View | Annotate | Download (9.39 KB)

1
/*
2
 * Created on 09-mar-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: AbstractGeoprocessPanel.java 6489 2006-07-21 09:13:55Z azabala $
47
* $Log$
48
* Revision 1.6  2006-07-21 09:10:34  azabala
49
* fixed bug 608: user doesnt enter any result file to the geoprocess panel
50
*
51
* Revision 1.5  2006/06/29 17:29:34  azabala
52
* Added common functionality to all geoprocess panels (result layer dialog)
53
*
54
* Revision 1.4  2006/06/08 18:22:02  azabala
55
* Arreglado pete cuando abr?amos un dialogo de geoprocessing habiendo agrupaciones en el TOC
56
*
57
* Revision 1.3  2006/06/02 18:21:28  azabala
58
* *** empty log message ***
59
*
60
* Revision 1.2  2006/05/25 08:21:14  jmvivo
61
* A?adido metodo de peticion de confirmacion para sobreescribir el fichero de salida
62
*
63
* Revision 1.1  2006/05/24 21:13:09  azabala
64
* primera version en cvs despues de refactoring orientado a crear un framework extensible de geoprocessing
65
*
66
* Revision 1.2  2006/05/08 15:36:33  azabala
67
* check and ask for spatial index creation
68
*
69
* Revision 1.1  2006/04/11 18:01:23  azabala
70
* primera version en cvs
71
*
72
* Revision 1.5  2006/04/07 19:00:58  azabala
73
* *** empty log message ***
74
*
75
* Revision 1.4  2006/03/23 21:02:37  azabala
76
* *** empty log message ***
77
*
78
* Revision 1.3  2006/03/21 19:26:08  azabala
79
* *** empty log message ***
80
*
81
* Revision 1.2  2006/03/14 19:32:22  azabala
82
* *** empty log message ***
83
*
84
* Revision 1.1  2006/03/09 17:03:59  azabala
85
* *** empty log message ***
86
*
87
*
88
*/
89
package com.iver.cit.gvsig.geoprocess.core.gui;
90

    
91
import java.awt.Component;
92
import java.io.File;
93
import java.io.FileNotFoundException;
94
import java.util.ArrayList;
95
import java.util.Arrays;
96

    
97
import javax.swing.JComboBox;
98
import javax.swing.JFileChooser;
99
import javax.swing.JFrame;
100
import javax.swing.JOptionPane;
101
import javax.swing.JPanel;
102
import javax.swing.JTextField;
103

    
104
import com.iver.andami.PluginServices;
105
import com.iver.cit.gvsig.fmap.DriverException;
106
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
107
import com.iver.cit.gvsig.fmap.layers.FLayer;
108
import com.iver.cit.gvsig.fmap.layers.FLayers;
109
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
110
import com.iver.cit.gvsig.gui.thememanager.CreateSpatialIndexMonitorableTask;
111
import com.iver.utiles.GenericFileFilter;
112
import com.iver.utiles.swing.threads.IMonitorableTask;
113

    
114
/**
115
 * Abstract base class with utility methods
116
 * commons to all GeoprocessXXXPanel
117
 * @author azabala
118
 *
119
 */
120
public abstract class AbstractGeoprocessPanel extends JPanel implements IGeoprocessPanel{
121
        protected FLayers layers = null;
122
        protected JComboBox layersComboBox = null;
123
        protected JTextField fileNameResultTextField = null;
124
        
125
        public FLyrVect getInputLayer() {
126
                FLyrVect solution = null;
127
                String selectedLayer = (String)layersComboBox.getSelectedItem();
128
        solution = (FLyrVect)layers.getLayer(selectedLayer);
129
        return solution;
130
        }
131
        
132
        public void setFLayers(FLayers layers){
133
                this.layers = layers;
134
        }
135
        
136
        public FLayers getFLayers(){
137
                return layers;
138
        }
139
        
140
        protected FLyrVect[] getVectorialLayers(FLayers layers){
141
                FLyrVect[] solution = null;
142
                ArrayList list = new ArrayList();
143
                int numLayers = layers.getLayersCount();
144
                for(int i = 0; i < numLayers; i++){
145
                        FLayer layer = layers.getLayer(i);
146
                        if(layer instanceof FLyrVect)
147
                                list.add(layer);
148
                        else if(layer instanceof FLayers)
149
                                list.addAll(Arrays.asList(getVectorialLayers((FLayers)layer)));
150
                }
151
                solution = new FLyrVect[list.size()];
152
                list.toArray(solution);
153
                return solution;
154
                
155
        }
156
        
157
        protected String[] getLayerNames() {
158
                String[] solution = null;
159
                int numLayers = layers.getLayersCount();
160
                if(layers != null &&  numLayers > 0){
161
                        ArrayList list = new ArrayList();
162
                        for(int i = 0; i < numLayers; i++){
163
                                FLayer layer = layers.getLayer(i);
164
                                if(layer instanceof FLyrVect)
165
                                        list.add(layer.getName());
166
                                if(layer instanceof FLayers){
167
                                        FLayers layers = (FLayers) layer;
168
                                        FLyrVect[] vectorials = getVectorialLayers(layers);
169
                                        for(int j = 0; j < vectorials.length; j++){
170
                                                list.add(vectorials[j].getName());
171
                                        }
172
                                }
173
                        }//for
174
                        solution = new String[list.size()];
175
                        list.toArray(solution);
176
                }
177
                return solution;
178
        }
179
        
180
        /**
181
         * Shows to the user a dialog error, whith the title and message body
182
         * specified as parameters.
183
         * 
184
         * @param message body of the error message
185
         * @param title title of the error message
186
         */
187
        public void error(String message, String title) {
188
                JFrame parentComponent = (JFrame)PluginServices.
189
                                                                                        getMainFrame();
190
                JOptionPane.showMessageDialog(parentComponent, message, title,
191
                                JOptionPane.ERROR_MESSAGE);
192
        }
193
        
194
        
195
        /**
196
         * Asks to the user for the creation of a spatial index for the
197
         * specified layer.
198
         * It will be of help for certain geoprocesses.
199
         * 
200
         * FLyrVect default spatial index is MapServer quadtree, that hasnt
201
         * the ability to do nearest neighbour searches.
202
         * 
203
         * For those geoprocesses that needs it (NN searches) overwrite this
204
         * method and use JSI RTree, or SIL RTree.
205
         * 
206
         * It returns an IMonitorableTask, a task to build the spatial
207
         * index in background.
208
         * 
209
         * @param layer
210
         * @return an ITask
211
         */
212
        public IMonitorableTask askForSpatialIndexCreation(FLyrVect layer){
213
                String title = PluginServices.getText(this, "Crear_Indice");
214
                String confirmDialogText = 
215
                        PluginServices.getText(this, "Crear_Indice_Pregunta_1")+
216
                                        " " +
217
                                        layer.getName()+
218
                                        " " +
219
                        PluginServices.getText(this, "Crear_Indice_Pregunta_2");
220
                int option = JOptionPane.showConfirmDialog(this, 
221
                                        confirmDialogText,
222
                                        title, 
223
                                        JOptionPane.YES_NO_OPTION, 
224
                                        JOptionPane.QUESTION_MESSAGE, null);
225
                if(option == JOptionPane.YES_OPTION){
226
                        //Usually we want task like spatial index creation dont block
227
                        //GUI responses. Now, we dont want to start geoprocess execution
228
                        //until spatial index creation would finish, to have advantage
229
                        //of the spatial index
230
                        try {
231
                                CreateSpatialIndexMonitorableTask task
232
                                        = new CreateSpatialIndexMonitorableTask(layer);
233
                                return task;
234
                        } catch (DriverIOException e) {
235
                                // TODO Auto-generated catch block
236
                                e.printStackTrace();
237
                                return null;
238
                        } catch (DriverException e) {
239
                                // TODO Auto-generated catch block
240
                                e.printStackTrace();
241
                                return null;
242
                        }
243
                }else
244
                        return null;
245
                                        
246
        }
247
        /**
248
         * Confirm overwrite the output file
249
         * if it allready exist.
250
         * 
251
         * 
252
         * @param outputFile
253
         * @return answer
254
         */
255
        public boolean askForOverwriteOutputFile(File outputFile){
256
                String title = PluginServices.getText(this, "Sobreescribir_fichero");
257
                String confirmDialogText = 
258
                        PluginServices.getText(this, "Sobreescribir_fichero_Pregunta_1")+
259
                                        "\n'" +
260
                                        outputFile.getAbsolutePath()+
261
                                        "'\n" +
262
                        PluginServices.getText(this, "Sobreescribir_fichero_Pregunta_2");
263
                int option = JOptionPane.showConfirmDialog(this, 
264
                                        confirmDialogText,
265
                                        title, 
266
                                        JOptionPane.YES_NO_OPTION, 
267
                                        JOptionPane.QUESTION_MESSAGE, null);
268
                if(option == JOptionPane.YES_OPTION){
269
                        return true;
270
                }
271
                return false;
272
        }
273
        
274
         public void openResultFile() {
275
                        JFileChooser jfc = new JFileChooser();
276
                jfc.addChoosableFileFilter(new GenericFileFilter("shp",
277
                                                                                        "Ficheros SHP"));
278
                if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == 
279
                                                                                                JFileChooser.APPROVE_OPTION) {
280
                    File file = jfc.getSelectedFile();
281
                    if (!(file.getPath().endsWith(".shp") || file.getPath().endsWith(".SHP"))){
282
                        file = new File(file.getPath()+".shp");
283
                    }
284
                }//if
285
                if (jfc.getSelectedFile()!=null) {
286
                        getFileNameResultTextField().setText(
287
                                        jfc.getSelectedFile().getAbsolutePath());
288
                }
289
                        
290
          }
291
         
292
                 /**
293
                  * Gives access to the text field where user could enter the result
294
                  * file path.
295
                  * If this method is called during the concrete gui subclasses building,
296
                  * caller must sets bounds of this component.
297
                  * @return
298
                  */
299
                public JTextField getFileNameResultTextField() {
300
                        if (fileNameResultTextField == null) {
301
                                fileNameResultTextField = new JTextField();
302
                        }
303
                        return fileNameResultTextField;
304
                }
305
                
306
                
307
                public File getOutputFile() throws FileNotFoundException{
308
                        String fileName = getFileNameResultTextField().getText();
309
                        if(fileName.length() == 0){
310
                                throw new FileNotFoundException("No se ha seleccionado ningun fichero de salida");
311
                        }
312
                        if(! fileName.endsWith(".shp")){
313
                                if(! fileName.endsWith("."))
314
                                        fileName += ".";
315
                                fileName += "shp";
316
                        }
317
                        return new File(fileName);
318
                }
319

    
320
}
321

    
322

    
323