Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGeoProcessing / src / com / iver / cit / gvsig / geoprocess / core / gui / AbstractGeoprocessPanel.java @ 10626

History | View | Annotate | Download (9.31 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 10626 2007-03-06 16:55:54Z caballero $
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 implements IGeoprocessPanel{
126
        protected FLayers layers = null;
127
        protected JComboBox layersComboBox = null;
128
        protected JTextField fileNameResultTextField = null;
129

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

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

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

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

    
160
        }
161

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

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

    
199

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