Statistics
| Revision:

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

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

    
97
import java.awt.Component;
98
import java.io.File;
99
import java.io.FileNotFoundException;
100
import java.util.ArrayList;
101
import java.util.Arrays;
102

    
103
import javax.swing.JComboBox;
104
import javax.swing.JFileChooser;
105
import javax.swing.JFrame;
106
import javax.swing.JOptionPane;
107
import javax.swing.JPanel;
108
import javax.swing.JTextField;
109

    
110
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
111
import com.iver.andami.PluginServices;
112
import com.iver.cit.gvsig.fmap.layers.FLayer;
113
import com.iver.cit.gvsig.fmap.layers.FLayers;
114
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
115
import com.iver.cit.gvsig.project.documents.view.legend.CreateSpatialIndexMonitorableTask;
116
import com.iver.utiles.GenericFileFilter;
117
import com.iver.utiles.swing.threads.IMonitorableTask;
118

    
119
/**
120
 * Abstract base class with utility methods
121
 * commons to all GeoprocessXXXPanel
122
 * @author azabala
123
 *
124
 */
125
public abstract class AbstractGeoprocessPanel extends JPanel 
126
                                implements IGeoprocessPanel, IGeoprocessUserEntries{
127
        protected FLayers layers = null;
128
        protected JComboBox layersComboBox = null;
129
        protected JTextField fileNameResultTextField = null;
130

    
131
        public FLyrVect getInputLayer() {
132
                FLyrVect solution = null;
133
                String selectedLayer = (String)layersComboBox.getSelectedItem();
134
        solution = (FLyrVect)layers.getLayer(selectedLayer);
135
        return solution;
136
        }
137

    
138
        public void setFLayers(FLayers layers){
139
                this.layers = layers;
140
        }
141

    
142
        public FLayers getFLayers(){
143
                return layers;
144
        }
145

    
146
        protected FLyrVect[] getVectorialLayers(FLayers layers){
147
                FLyrVect[] solution = null;
148
                ArrayList list = new ArrayList();
149
                int numLayers = layers.getLayersCount();
150
                for(int i = 0; i < numLayers; i++){
151
                        FLayer layer = layers.getLayer(i);
152
                        if(layer instanceof FLyrVect)
153
                                list.add(layer);
154
                        else if(layer instanceof FLayers)
155
                                list.addAll(Arrays.asList(getVectorialLayers((FLayers)layer)));
156
                }
157
                solution = new FLyrVect[list.size()];
158
                list.toArray(solution);
159
                return solution;
160

    
161
        }
162

    
163
        protected String[] getLayerNames() {
164
                String[] solution = null;
165
                int numLayers = layers.getLayersCount();
166
                if(layers != null &&  numLayers > 0){
167
                        ArrayList list = new ArrayList();
168
                        for(int i = 0; i < numLayers; i++){
169
                                FLayer layer = layers.getLayer(i);
170
                                if(layer instanceof FLyrVect)
171
                                        list.add(layer.getName());
172
                                if(layer instanceof FLayers){
173
                                        FLayers layers = (FLayers) layer;
174
                                        FLyrVect[] vectorials = getVectorialLayers(layers);
175
                                        for(int j = 0; j < vectorials.length; j++){
176
                                                list.add(vectorials[j].getName());
177
                                        }
178
                                }
179
                        }//for
180
                        solution = new String[list.size()];
181
                        list.toArray(solution);
182
                }
183
                return solution;
184
        }
185

    
186
        /**
187
         * Shows to the user a dialog error, whith the title and message body
188
         * specified as parameters.
189
         *
190
         * @param message body of the error message
191
         * @param title title of the error message
192
         */
193
        public void error(String message, String title) {
194
                JFrame parentComponent = (JFrame)PluginServices.
195
                                                                                        getMainFrame();
196
                JOptionPane.showMessageDialog(parentComponent, message, title,
197
                                JOptionPane.ERROR_MESSAGE);
198
        }
199

    
200

    
201
        /**
202
         * Asks to the user for the creation of a spatial index for the
203
         * specified layer.
204
         * It will be of help for certain geoprocesses.
205
         *
206
         * FLyrVect default spatial index is MapServer quadtree, that hasnt
207
         * the ability to do nearest neighbour searches.
208
         *
209
         * For those geoprocesses that needs it (NN searches) overwrite this
210
         * method and use JSI RTree, or SIL RTree.
211
         *
212
         * It returns an IMonitorableTask, a task to build the spatial
213
         * index in background.
214
         *
215
         * @param layer
216
         * @return an ITask
217
         */
218
        public IMonitorableTask askForSpatialIndexCreation(FLyrVect layer){
219
                String title = PluginServices.getText(this, "Crear_Indice");
220
                String confirmDialogText =
221
                        PluginServices.getText(this, "Crear_Indice_Pregunta_1")+
222
                                        " " +
223
                                        layer.getName()+
224
                                        " " +
225
                        PluginServices.getText(this, "Crear_Indice_Pregunta_2");
226
                int option = JOptionPane.showConfirmDialog(this,
227
                                        confirmDialogText,
228
                                        title,
229
                                        JOptionPane.YES_NO_OPTION,
230
                                        JOptionPane.QUESTION_MESSAGE, null);
231
                if(option == JOptionPane.YES_OPTION){
232
                        //Usually we want task like spatial index creation dont block
233
                        //GUI responses. Now, we dont want to start geoprocess execution
234
                        //until spatial index creation would finish, to have advantage
235
                        //of the spatial index
236
                        try {
237
                                CreateSpatialIndexMonitorableTask task
238
                                        = new CreateSpatialIndexMonitorableTask(layer);
239
                                return task;
240
                        } catch (ReadDriverException e) {
241
                                e.printStackTrace();
242
                                return null;
243
                        }
244
                }
245
                return null;
246

    
247
        }
248
        /**
249
         * Confirm overwrite the output file
250
         * if it allready exist.
251
         *
252
         *
253
         * @param outputFile
254
         * @return answer
255
         */
256
        public boolean askForOverwriteOutputFile(File outputFile){
257
                String title = PluginServices.getText(this, "Sobreescribir_fichero");
258
                String confirmDialogText =
259
                        PluginServices.getText(this, "Sobreescribir_fichero_Pregunta_1")+
260
                                        "\n'" +
261
                                        outputFile.getAbsolutePath()+
262
                                        "'\n" +
263
                        PluginServices.getText(this, "Sobreescribir_fichero_Pregunta_2");
264
                int option = JOptionPane.showConfirmDialog(this,
265
                                        confirmDialogText,
266
                                        title,
267
                                        JOptionPane.YES_NO_OPTION,
268
                                        JOptionPane.QUESTION_MESSAGE, null);
269
                if(option == JOptionPane.YES_OPTION){
270
                        return true;
271
                }
272
                return false;
273
        }
274

    
275
         public void openResultFile() {
276
                        JFileChooser jfc = new JFileChooser();
277
                jfc.addChoosableFileFilter(new GenericFileFilter("shp",
278
                                                                                        "Ficheros SHP"));
279
                if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) ==
280
                                                                                                JFileChooser.APPROVE_OPTION) {
281
                    File file = jfc.getSelectedFile();
282
                    if (!(file.getPath().endsWith(".shp") || file.getPath().endsWith(".SHP"))){
283
                        file = new File(file.getPath()+".shp");
284
                    }
285
                }//if
286
                if (jfc.getSelectedFile()!=null) {
287
                        getFileNameResultTextField().setText(
288
                                        jfc.getSelectedFile().getAbsolutePath());
289
                }
290

    
291
          }
292

    
293
                 /**
294
                  * Gives access to the text field where user could enter the result
295
                  * file path.
296
                  * If this method is called during the concrete gui subclasses building,
297
                  * caller must sets bounds of this component.
298
                  * @return
299
                  */
300
                public JTextField getFileNameResultTextField() {
301
                        if (fileNameResultTextField == null) {
302
                                fileNameResultTextField = new JTextField();
303
                        }
304
                        return fileNameResultTextField;
305
                }
306

    
307

    
308
                public File getOutputFile() throws FileNotFoundException{
309
                        String fileName = getFileNameResultTextField().getText();
310
                        if(fileName.length() == 0){
311
                                throw new FileNotFoundException("No se ha seleccionado ningun fichero de salida");
312
                        }
313
                        if(! fileName.endsWith(".shp")){
314
                                if(! fileName.endsWith("."))
315
                                        fileName += ".";
316
                                fileName += "shp";
317
                        }
318
                        return new File(fileName);
319
                }
320

    
321
}
322

    
323

    
324