Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / Abrir.java @ 1904

History | View | Annotate | Download (10.9 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.geom.Rectangle2D;
44
import java.io.File;
45
import java.sql.Connection;
46
import java.sql.DriverManager;
47
import java.sql.SQLException;
48

    
49
import javax.swing.JOptionPane;
50

    
51
import org.cresques.cts.ICoordTrans;
52
import org.cresques.cts.IProjection;
53
import org.cresques.cts.gt2.CoordSys;
54
import org.cresques.cts.gt2.CoordTrans;
55

    
56
import com.hardcode.driverManager.Driver;
57
import com.hardcode.driverManager.DriverLoadException;
58
import com.iver.andami.PluginServices;
59
import com.iver.andami.messages.NotificationManager;
60
import com.iver.andami.plugins.Extension;
61
import com.iver.cit.gvsig.fmap.DriverException;
62
import com.iver.cit.gvsig.fmap.drivers.RasterDriver;
63
import com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver;
64
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
65
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
66
import com.iver.cit.gvsig.fmap.drivers.jdbc.mysql.MySQLdriver;
67
import com.iver.cit.gvsig.fmap.drivers.jdbc.postgis.PostGisDriver;
68
import com.iver.cit.gvsig.fmap.drivers.wfs.WFSDriver;
69
import com.iver.cit.gvsig.fmap.layers.FLayer;
70
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
71
import com.iver.cit.gvsig.gui.FOpenDialog;
72
import com.iver.cit.gvsig.gui.FileOpenDialog;
73
import com.iver.cit.gvsig.gui.View;
74
import com.iver.cit.gvsig.gui.WMSDataSourceAdapter;
75
import com.iver.cit.gvsig.gui.wizards.JDBCWizard;
76
import com.iver.cit.gvsig.gui.wizards.WFSWizard;
77
import com.iver.cit.gvsig.gui.wms.WMSWizard;
78
import com.iver.cit.gvsig.project.Project;
79
import com.iver.cit.gvsig.wfs.WFSDataSourceAdapter;
80

    
81

    
82
/**
83
 * Extensi?n que abre un di?logo para seleccionar la capa o capas que se
84
 * quieren a?adir a la vista.
85
 *
86
 * @author Fernando Gonz?lez Cort?s
87
 */
88
public class Abrir implements Extension {
89
        public FOpenDialog fopen = null;
90

    
91
        /**
92
         * @see com.iver.mdiApp.plugins.Extension#isVisible()
93
         */
94
        public boolean isVisible() {
95
                com.iver.andami.ui.mdiManager.View f = PluginServices.getMDIManager()
96
                                                                                                                         .getActiveView();
97

    
98
                if (f == null) {
99
                        return false;
100
                }
101

    
102
                return (f.getClass() == View.class);
103
        }
104

    
105
        /**
106
         * @see com.iver.andami.plugins.Extension#inicializar()
107
         */
108
        public void inicializar() {
109
        }
110

    
111
        /**
112
         * @see com.iver.andami.plugins.Extension#execute(java.lang.String)
113
         */
114
        public void execute(String actionCommand) {
115
                Project project = ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).getProject();
116
                View theView = (View) PluginServices.getMDIManager().getActiveView();
117
                fopen = new FOpenDialog();
118

    
119
                FileOpenDialog fileDlg = new FileOpenDialog(new Class[] {
120
                                        VectorialFileDriver.class, RasterDriver.class
121
                                });
122
                WMSWizard wms = new WMSWizard();
123
                wms.setDataSource(new WMSDataSourceAdapter());
124
                
125
                JDBCWizard dbWiz = new JDBCWizard();
126
                WFSWizard wfs = new WFSWizard();
127
                wfs.setDataSource(new WFSDataSourceAdapter());
128
                //        DataBaseOpenDialog dbop = new DataBaseOpenDialog();
129
                //        dbop.setClasses(new Class[] { DBDriver.class });
130
                fopen.addTab("Fichero", fileDlg);
131
                fopen.addWizardTab("WMS", wms);
132
                fopen.addWizardTab("WFS",wfs);
133
                fopen.addWizardTab("JDBC", dbWiz);
134
                
135

    
136
                //        fopen.addTab("Base de datos", dbop);
137
                PluginServices.getMDIManager().addView(fopen);
138

    
139
                if (fopen.isAccepted()) {
140
                        FLayer lyr = null;
141

    
142
                        if (fopen.getSelectedTab() == fileDlg) {
143
                                if (fileDlg.getFiles() == null) {
144
                                        return;
145
                                }
146

    
147
                                IProjection proj = FOpenDialog.getLastProjection();
148
                                File[] files = fileDlg.getFiles();
149
                                String[] driverNames = fileDlg.getDriverNames();
150
                                Driver[] drivers = new Driver[driverNames.length];
151
                                for (int i = 0; i < drivers.length; i++) {
152
                                        try {
153
                                                drivers[i] = LayerFactory.getDM().getDriver(driverNames[i]);
154
                                        } catch (DriverLoadException e) {
155
                                                NotificationManager.addError("No se pudo cargar el driver", e);
156
                                        }
157
                                }
158
                                Rectangle2D[] rects=new Rectangle2D[files.length];
159
                                boolean first=false;
160
                                theView.getMapControl().getMapContext()
161
                                   .beginAtomicEvent();
162

    
163
                                for (int iFile = 0; iFile < files.length; iFile++) {
164
                                        File fich = files[iFile];
165
                                        String layerName = fich.getName();
166
                                        String layerPath = fich.getAbsolutePath();
167

    
168
                                        try {
169
                                                if (drivers[iFile] instanceof VectorialFileDriver){
170
                                                        lyr = LayerFactory.createLayer(layerName,
171
                                                                        (VectorialFileDriver) drivers[iFile], fich, proj);
172
                                                }else if (drivers[iFile] instanceof RasterDriver){
173
/*                                                        lyr = LayerFactory.createLayer(layerName,
174
                                                                        driverNames[iFile], fich, proj);
175
        */                                        }
176

    
177
                                                if (lyr != null) {
178
                                                        lyr.setVisible(true);
179
                                                        if (theView.getMapControl().getMapContext().getViewPort().getExtent()==null){
180
                                                                first=true;
181
                                                        }
182

    
183
                                                        // Comprobar que la projecci?n es la misma que la vista
184
                                                        if (proj != theView.getProjection()) {
185
                                                                int option = JOptionPane.showConfirmDialog(null,
186
                                                                        PluginServices.getText(this,"reproyectar_aviso"),
187
                                                                        PluginServices.getText(this,"reproyectar_pregunta"),
188
                                                                                JOptionPane.YES_NO_OPTION);
189

    
190
                                                                if (option == JOptionPane.NO_OPTION) {
191
                                                                        continue;
192
                                                                } else {
193
                                                                        ICoordTrans ct = new CoordTrans((CoordSys) proj,
194
                                                                                        (CoordSys) theView.getProjection());
195
                                                                        lyr.setCoordTrans(ct);
196
                                                                        System.err.println("coordTrans = " +
197
                                                                                proj.getAbrev() + " " +
198
                                                                                theView.getProjection().getAbrev());
199
                                                                }
200
                                                        }
201

    
202
                                                        theView.getMapControl().getMapContext().getLayers()
203
                                                                   .addLayer(lyr);
204
                                                        rects[iFile]=lyr.getFullExtent();
205
                                                        
206
                                                        // TODO: Poner una variable y dibujar solo cuando
207
                                                        // todas las capas hayan sido cargadas.
208
                                                        // TODO Se deber? de redibujar mediante la captura de los eventos, por
209
                                                        //eso se comenta la parte anterior
210
                                                        //                                                        theView.getMapControl().drawMap();
211
                                                        //                                                        theView.getTOC().refresh();
212
                                                }
213

    
214
                                        } catch (DriverException e) {
215
                                                NotificationManager.addError("Error al crear la capa", e);
216
                                        }
217
                                }
218

    
219
                                //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.
220
                                if (rects.length > 1) {
221
                                        Rectangle2D rect = new Rectangle2D.Double();
222
                                        rect.setRect(rects[0]);
223

    
224
                                        if (first) {
225
                                                for (int i = 0; i < rects.length; i++) {
226
                                                        rect.add(rects[i]);
227
                                                }
228

    
229
                                                theView.getMapControl().getMapContext().getViewPort()
230
                                                           .setExtent(rect);
231
                                        }
232
                                }
233
                                theView.getMapControl().getMapContext()
234
                                   .endAtomicEvent();
235

    
236
                        } else if (fopen.getSelectedTab() == wms) {
237
                                String layerName = wms.getLayerName();
238

    
239
                                lyr = LayerFactory.createLayer(layerName,
240
                                                wms.getLayersRectangle(), wms.getHost(),
241
                                                wms.getFormat(), wms.getLayersQuery(),
242
                                                wms.getQueryableLayerQuery(), wms.getSRS());
243

    
244
                                if (lyr != null) {
245
                                        lyr.setVisible(true);
246
                                        theView.getMapControl().getMapContext().beginAtomicEvent();
247
                                        theView.getMapControl().getMapContext().getLayers()
248
                                                   .addLayer(lyr);
249
                                        theView.getMapControl().getMapContext().endAtomicEvent();
250
                                }
251
                        } else if (fopen.getSelectedTab() == dbWiz) {
252
                                String layerName = dbWiz.getLayerName();
253
                                String user = dbWiz.getUser();
254
                                String pwd = dbWiz.getPassword();
255
                                String dbUrl = dbWiz.getUrlConnect();
256
                                String fields = dbWiz.getSqlFields();
257
                                String whereClause = dbWiz.getSqlWhere();
258

    
259
                                
260
                                try {
261
                    
262
                    // TODO: ?C?mo se hace esto para que sea gen?rico?.
263
                    // Supongo que el wizard te tiene que decir qu? driver
264
                    // quieres usar....
265
                                    Class.forName("org.postgresql.Driver");
266
                                    Class.forName("com.mysql.jdbc.Driver");
267
                    Connection conn = DriverManager.getConnection(dbUrl, user, pwd);
268
                    conn.setAutoCommit(false);
269

    
270
                    VectorialDatabaseDriver driver = null;
271
                    if (dbUrl.indexOf("mysql") != -1)                        
272
                        driver = new MySQLdriver();
273
                    if (dbUrl.indexOf("postgresql") != -1)                        
274
                        driver = new PostGisDriver();
275
                    
276
                    driver.setData(conn, layerName, fields, whereClause, 2);
277
                    lyr = LayerFactory.createDBLayer(driver, layerName, null);
278
                    
279
                                    if (lyr != null) {
280
                                            lyr.setVisible(true);
281
                                            theView.getMapControl().getMapContext().beginAtomicEvent();
282
                                            theView.getMapControl().getMapContext().getLayers()
283
                                                       .addLayer(lyr);
284
                                            theView.getMapControl().getMapContext().endAtomicEvent();
285
                                    }
286
                    
287
                } catch (SQLException e) {
288
                    // TODO Auto-generated catch block
289
                    e.printStackTrace();
290
                } catch (ClassNotFoundException e) {
291
                    // TODO Auto-generated catch block
292
                    e.printStackTrace();
293
                }
294

    
295
                        } else if (fopen.getSelectedTab() == wfs) {
296
                                //String layerName = wfs.getLayerName();
297
                                String layerName = wfs.getLayerName();
298
                                String user = wfs.getUser();
299
                                String pwd = wfs.getPassword();
300
                                String url = wfs.getUrlConnect();
301
                                String[] layers=wfs.getLayers();
302
                                boolean protocol=wfs.getProtocol();
303
                                int numfeatures=wfs.getNumFeatures();
304
                                int timeout=wfs.getTimeout();
305
                                //String fields = wfs.getSqlFields();
306
                                //String whereClause = dbWiz.getSqlWhere();
307
                                WFSDriver driver = new WFSDriver();
308
                                driver.setData(url,user,pwd,layers,protocol,numfeatures,timeout,2);
309
                                lyr = LayerFactory.createWFSLayer(layerName,driver,wfs.getHost(),
310
                                                wfs.getProjection());
311

    
312
                                if (lyr != null) {
313
                                        lyr.setVisible(true);
314
                                        theView.getMapControl().getMapContext().beginAtomicEvent();
315
                                        theView.getMapControl().getMapContext().getLayers()
316
                                                   .addLayer(lyr);
317
                                        theView.getMapControl().getMapContext().endAtomicEvent();
318
                                }
319
                        }
320

    
321
                }
322
        }
323

    
324
        /**
325
         * @see com.iver.andami.plugins.Extension#isEnabled()
326
         */
327
        public boolean isEnabled() {
328
                return true;
329
        }
330
}