Statistics
| Revision:

svn-gvsig-desktop / branches / gvSIG_GisPlanet / applications / appgvSIG / src / com / iver / cit / gvsig / Abrir.java @ 1960

History | View | Annotate | Download (15 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig;
42

    
43
import java.awt.Component;
44
import java.awt.geom.Rectangle2D;
45
import java.io.File;
46
import java.io.IOException;
47
import java.sql.Connection;
48
import java.sql.DriverManager;
49
import java.sql.SQLException;
50

    
51
import javax.swing.JOptionPane;
52

    
53
import org.cresques.cts.ICoordTrans;
54
import org.cresques.cts.IProjection;
55
import org.cresques.cts.gt2.CoordSys;
56
import org.cresques.cts.gt2.CoordTrans;
57

    
58
import com.hardcode.driverManager.Driver;
59
import com.hardcode.driverManager.DriverLoadException;
60
import com.hardcode.gdbms.engine.data.NoSuchTableException;
61
import com.iver.andami.PluginServices;
62
import com.iver.andami.messages.NotificationManager;
63
import com.iver.andami.plugins.Extension;
64
import com.iver.cit.gvsig.fmap.DriverException;
65
import com.iver.cit.gvsig.fmap.drivers.DefaultDBDriver;
66
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
67
import com.iver.cit.gvsig.fmap.drivers.RasterDriver;
68
import com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver;
69
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
70
import com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver;
71
import com.iver.cit.gvsig.fmap.drivers.arcsde.ArcSdeDriver;
72
import com.iver.cit.gvsig.fmap.drivers.wfs.WFSDriver;
73
import com.iver.cit.gvsig.fmap.layers.FLayer;
74
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
75
import com.iver.cit.gvsig.gui.FOpenDialog;
76
import com.iver.cit.gvsig.gui.FileOpenDialog;
77
import com.iver.cit.gvsig.gui.View;
78
import com.iver.cit.gvsig.gui.WMSDataSourceAdapter;
79
import com.iver.cit.gvsig.gui.jdbcwizard.Wizard;
80
import com.iver.cit.gvsig.gui.wfs.WFSDataSourceAdapter;
81
import com.iver.cit.gvsig.gui.wizards.ArcSdeWizard;
82
import com.iver.cit.gvsig.gui.wizards.WFSWizard;
83
import com.iver.cit.gvsig.gui.wms.WMSWizard;
84
import com.iver.cit.gvsig.project.Project;
85
import com.iver.utiles.StringUtilities;
86

    
87

    
88
/**
89
 * Extensi?n que abre un di?logo para seleccionar la capa o capas que se
90
 * quieren a?adir a la vista.
91
 *
92
 * @author Fernando Gonz?lez Cort?s
93
 */
94
public class Abrir implements Extension {
95
        public FOpenDialog fopen = null;
96

    
97
        /**
98
         * @see com.iver.mdiApp.plugins.Extension#isVisible()
99
         */
100
        public boolean isVisible() {
101
                com.iver.andami.ui.mdiManager.View f = PluginServices.getMDIManager()
102
                                                                                                                         .getActiveView();
103

    
104
                if (f == null) {
105
                        return false;
106
                }
107

    
108
                return (f.getClass() == View.class);
109
        }
110

    
111
        /**
112
         * @see com.iver.andami.plugins.Extension#inicializar()
113
         */
114
        public void inicializar() {
115
        }
116

    
117
        /**
118
         * @see com.iver.andami.plugins.Extension#execute(java.lang.String)
119
         */
120
        public void execute(String actionCommand) {
121
                Project project = ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).getProject();
122
                View theView = (View) PluginServices.getMDIManager().getActiveView();
123
                fopen = new FOpenDialog();
124

    
125
                FileOpenDialog fileDlg = new FileOpenDialog(new Class[] {
126
                                        VectorialFileDriver.class, RasterDriver.class
127
                                });
128
                WMSWizard wms = new WMSWizard();
129
                wms.setDataSource(new WMSDataSourceAdapter());
130
                
131
                Wizard wiz = new Wizard();
132
                WFSWizard wfs = new WFSWizard();
133
                wfs.setDataSource(new WFSDataSourceAdapter());
134

    
135
        ArcSdeWizard arcsde_wizard = new ArcSdeWizard();
136

    
137
                //        DataBaseOpenDialog dbop = new DataBaseOpenDialog();
138
                //        dbop.setClasses(new Class[] { DBDriver.class });
139
                fopen.addTab("Fichero", fileDlg);
140
                fopen.addWizardTab("WMS", wms);
141
                fopen.addWizardTab("WFS",wfs);
142
                fopen.addWizardTab("JDBC", wiz);
143
        fopen.addWizardTab("ArcSDE", arcsde_wizard);
144
                
145

    
146
                //        fopen.addTab("Base de datos", dbop);
147
                PluginServices.getMDIManager().addView(fopen);
148

    
149
                if (fopen.isAccepted())
150
        {
151
                        FLayer lyr = null;
152

    
153
                        if (fopen.getSelectedTab() == fileDlg) {
154
                                if (fileDlg.getFiles() == null) {
155
                                        return;
156
                                }
157

    
158
                                IProjection proj = FOpenDialog.getLastProjection();
159
                                File[] files = fileDlg.getFiles();
160
                                String[] driverNames = fileDlg.getDriverNames();
161
                                Driver[] drivers = new Driver[driverNames.length];
162
                                for (int i = 0; i < drivers.length; i++) {
163
                                        try {
164
                                                drivers[i] = LayerFactory.getDM().getDriver(driverNames[i]);
165
                                        } catch (DriverLoadException e) {
166
                                                NotificationManager.addError("No se pudo cargar el driver", e);
167
                                        }
168
                                }
169
                                Rectangle2D[] rects=new Rectangle2D[files.length];
170
                                boolean first=false;
171
                                theView.getMapControl().getMapContext()
172
                                   .beginAtomicEvent();
173

    
174
                                for (int iFile = 0; iFile < files.length; iFile++) {
175
                                        File fich = files[iFile];
176
                                        String layerName = fich.getName();
177
                                        String layerPath = fich.getAbsolutePath();
178

    
179
                                        try {
180
                                                if (drivers[iFile] instanceof VectorialFileDriver){
181
                                                        lyr = LayerFactory.createLayer(layerName,
182
                                                                        (VectorialFileDriver) drivers[iFile], fich, proj);
183
                                                }else if (drivers[iFile] instanceof RasterDriver){
184
/*                                                        lyr = LayerFactory.createLayer(layerName,
185
                                                                        driverNames[iFile], fich, proj);
186
        */                                        }
187

    
188
                                                if (lyr != null) {
189
                                                        lyr.setVisible(true);
190
                                                        if (theView.getMapControl().getMapContext().getViewPort().getExtent()==null){
191
                                                                first=true;
192
                                                        }
193

    
194
                                                        // Comprobar que la projecci?n es la misma que la vista
195
                                                        if (proj != theView.getProjection()) {
196
                                                                int option = JOptionPane.showConfirmDialog(null,
197
                                                                        PluginServices.getText(this,"reproyectar_aviso"),
198
                                                                        PluginServices.getText(this,"reproyectar_pregunta"),
199
                                                                                JOptionPane.YES_NO_OPTION);
200

    
201
                                                                if (option == JOptionPane.NO_OPTION) {
202
                                                                        continue;
203
                                                                } else {
204
                                                                        ICoordTrans ct = new CoordTrans((CoordSys) proj,
205
                                                                                        (CoordSys) theView.getProjection());
206
                                                                        lyr.setCoordTrans(ct);
207
                                                                        System.err.println("coordTrans = " +
208
                                                                                proj.getAbrev() + " " +
209
                                                                                theView.getProjection().getAbrev());
210
                                                                }
211
                                                        }
212

    
213
                                                        theView.getMapControl().getMapContext().getLayers()
214
                                                                   .addLayer(lyr);
215
                                                        rects[iFile]=lyr.getFullExtent();
216
                                                        
217
                                                        // TODO: Poner una variable y dibujar solo cuando
218
                                                        // todas las capas hayan sido cargadas.
219
                                                        // TODO Se deber? de redibujar mediante la captura de los eventos, por
220
                                                        //eso se comenta la parte anterior
221
                                                        //                                                        theView.getMapControl().drawMap();
222
                                                        //                                                        theView.getTOC().refresh();
223
                                                }
224

    
225
                                        } catch (DriverException e) {
226
                                                NotificationManager.addError("Error al crear la capa", e);
227
                                        }
228
                                }
229

    
230
                                //Esto permite que cuando se cargan varias capas de golpe y la vista est? vacia,se ponga como extent la suma de todos sus extents.
231
                                if (rects.length > 1) {
232
                                        Rectangle2D rect = new Rectangle2D.Double();
233
                                        rect.setRect(rects[0]);
234

    
235
                                        if (first) {
236
                                                for (int i = 0; i < rects.length; i++) {
237
                                                        rect.add(rects[i]);
238
                                                }
239

    
240
                                                theView.getMapControl().getMapContext().getViewPort()
241
                                                           .setExtent(rect);
242
                                        }
243
                                }
244
                                theView.getMapControl().getMapContext()
245
                                   .endAtomicEvent();
246

    
247
                        } else if (fopen.getSelectedTab() == wms) {
248
                                String layerName = wms.getLayerName();
249

    
250
                                lyr = LayerFactory.createLayer(layerName,
251
                                                wms.getLayersRectangle(), wms.getHost(),
252
                                                wms.getFormat(), wms.getLayersQuery(),
253
                                                wms.getQueryableLayerQuery(), wms.getSRS());
254

    
255
                                if (lyr != null) {
256
                                        lyr.setVisible(true);
257
                                        theView.getMapControl().getMapContext().beginAtomicEvent();
258
                                        theView.getMapControl().getMapContext().getLayers()
259
                                                   .addLayer(lyr);
260
                                        theView.getMapControl().getMapContext().endAtomicEvent();
261
                                }
262
                        } else if (fopen.getSelectedTab() == wiz) {
263
                            //true -> desconectado
264
                            if (false){
265
                                        try {
266
                                                String dbURL = wiz.getConnectionString();
267
                                                String user = wiz.getUser();
268
                                                String pwd = wiz.getPassword();
269
                                                String layerName = wiz.getLayerName();
270
                                                String fields = StringUtilities.getComaSeparated(wiz.getFields());
271
                                                String tableName = wiz.getTable();
272
                                                String whereClause = "";
273
                                                int fidField = wiz.getFID();;
274
                                                
275
                            
276
                            // TODO: ?C?mo se hace esto para que sea gen?rico?.
277
                            // Supongo que el wizard te tiene que decir qu? driver
278
                            // quieres usar....
279
                            Connection conn = DriverManager.getConnection(dbURL, user, pwd);
280
                            conn.setAutoCommit(false);
281

    
282
                            VectorialJDBCDriver driver = wiz.getDriver();
283
                            driver.setData(conn, tableName, fields, whereClause, fidField);
284
                            lyr = LayerFactory.createDisconnectedDBLayer(driver, layerName, null);
285
                            
286
                                            if (lyr != null) {
287
                                                    lyr.setVisible(true);
288
                                                    theView.getMapControl().getMapContext().beginAtomicEvent();
289
                                                    theView.getMapControl().getMapContext().getLayers()
290
                                                               .addLayer(lyr);
291
                                                    theView.getMapControl().getMapContext().endAtomicEvent();
292
                                            }
293
                            
294
                        } catch (SQLException e) {
295
                            // TODO Auto-generated catch block
296
                            e.printStackTrace();
297
                        } catch (DriverLoadException e) {
298
                                                e.printStackTrace();
299
                        } catch (IOException e) {
300
                                                e.printStackTrace();
301
                        } catch (DriverIOException e) {
302
                                                e.printStackTrace();
303
                        } catch (NoSuchTableException e) {
304
                                                e.printStackTrace();
305
                        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
306
                                                e.printStackTrace();
307
                        } catch (ClassNotFoundException e) {
308
                                                e.printStackTrace();
309
                        }
310
                            }else{
311
                try {
312
                                        String dbURL = wiz.getConnectionString();
313
                                        String user = wiz.getUser();
314
                                        String pwd = wiz.getPassword();
315
                                        String layerName = wiz.getLayerName();
316
                                        String fields = StringUtilities.getComaSeparated(wiz.getFields());
317
                                        String tableName = wiz.getTable();
318
                                        String whereClause = "";
319
                                        int fidField = wiz.getFID();;
320
                                        
321
                    
322
                    // TODO: ?C?mo se hace esto para que sea gen?rico?.
323
                    // Supongo que el wizard te tiene que decir qu? driver
324
                    // quieres usar....
325
                                    // Class.forName("org.postgresql.Driver");
326
                                    // Class.forName("com.mysql.jdbc.Driver");
327
                    Connection conn = DriverManager.getConnection(dbURL, user, pwd);
328
                    conn.setAutoCommit(false);
329

    
330
                    VectorialDatabaseDriver driver = wiz.getDriver();
331
                    if (driver instanceof DefaultDBDriver)
332
                    {
333
                        DefaultDBDriver dbDriver = (DefaultDBDriver) driver;
334
                        dbDriver.setData(conn, tableName, fields, whereClause, fidField);
335
                    }
336
                    
337
                    lyr = LayerFactory.createDBLayer(driver, layerName, null);
338
                    
339
                                    if (lyr != null) {
340
                                            lyr.setVisible(true);
341
                                            theView.getMapControl().getMapContext().beginAtomicEvent();
342
                                            theView.getMapControl().getMapContext().getLayers()
343
                                                       .addLayer(lyr);
344
                                            theView.getMapControl().getMapContext().endAtomicEvent();
345
                                    }
346
                    
347
                } catch (SQLException e) {
348
                    // TODO Auto-generated catch block
349
                    e.printStackTrace();
350
                /* } catch (ClassNotFoundException e) {
351
                    // TODO Auto-generated catch block
352
                    e.printStackTrace(); */
353
                } catch (DriverLoadException e) {
354
                                        e.printStackTrace();
355
                                }
356

    
357
                            }
358
                        } else if (fopen.getSelectedTab() == wfs) 
359
            {
360
                                String[] layers=wfs.getLayers();
361
                                if (layers.length>0)
362
                {
363
                                        String layerName = wfs.getLayerName();
364
                                        String user = wfs.getUser();
365
                                        String pwd = wfs.getPassword();
366
                                        String url = wfs.getUrlConnect();
367
                                
368
                                        boolean protocol=wfs.getProtocol();
369
                                        int numfeatures=wfs.getNumFeatures();
370
                                        int timeout=wfs.getTimeout();
371
                                //String fields = wfs.getSqlFields();
372
                                //String whereClause = dbWiz.getSqlWhere();
373
                                        WFSDriver driver = new WFSDriver();
374
                                        driver.setData(url,user,pwd,layers,protocol,numfeatures,timeout,2);
375
                                        lyr = LayerFactory.createWFSLayer(layerName,driver,wfs.getHost(),
376
                                                wfs.getProjection());
377

    
378
                                        if (lyr != null) {
379
                                                lyr.setVisible(true);
380
                                                theView.getMapControl().getMapContext().beginAtomicEvent();
381
                                                theView.getMapControl().getMapContext().getLayers()
382
                                                   .addLayer(lyr);
383
                                                theView.getMapControl().getMapContext().endAtomicEvent();
384
                                        }
385
                }
386
            } else if (fopen.getSelectedTab() == arcsde_wizard) {
387
                    String dbHost = arcsde_wizard.getHost();
388
                    String port = arcsde_wizard.getPort();
389
                    String user = arcsde_wizard.getUser();
390
                    String pwd = arcsde_wizard.getPassword();
391
                    // String layerName = arcsde_wizard.getLayerName();
392
                    // String fields = StringUtilities.getComaSeparated(wiz.getFields());
393
                    String tableName = arcsde_wizard.getLayerName();
394
                    String schema = arcsde_wizard.getSchema();
395
                    String whereClause = "";
396
                    String [] fields = null;
397
                    
398
                    
399
                    ArcSdeDriver driver = new ArcSdeDriver();
400
                    driver.setData(dbHost, Integer.parseInt(port), schema, user, pwd, tableName, fields, whereClause);
401
                    
402
                    lyr = LayerFactory.createDBLayer(driver, tableName, null);
403
                    
404
                    if (lyr != null) {
405
                        lyr.setVisible(true);
406
                        theView.getMapControl().getMapContext().beginAtomicEvent();
407
                        theView.getMapControl().getMapContext().getLayers()
408
                               .addLayer(lyr);
409
                        theView.getMapControl().getMapContext().endAtomicEvent();
410
                    }                   
411
                    
412
                                } else {
413
                                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),"Ninguna capa seleccionada");
414
                        }
415
                }
416
        }
417
    
418
    /**
419
         * @see com.iver.andami.plugins.Extension#isEnabled()
420
         */
421
        public boolean isEnabled() {
422
                return true;
423
        }
424
}