Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / Abrir.java @ 1889

History | View | Annotate | Download (10.7 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

    
80

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

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

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

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

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

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

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

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

    
137
                if (fopen.isAccepted()) {
138
                        FLayer lyr = null;
139

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
293
                        } else if (fopen.getSelectedTab() == wfs) {
294
                                //String layerName = wfs.getLayerName();
295
                                String layerName = wfs.getLayerName();
296
                                String user = wfs.getUser();
297
                                String pwd = wfs.getPassword();
298
                                String dbUrl = wfs.getUrlConnect();
299
                                //String fields = wfs.getSqlFields();
300
                                //String whereClause = dbWiz.getSqlWhere();
301
                                WFSDriver driver = new WFSDriver();
302
                                driver.setData(2);
303
                                lyr = LayerFactory.createWFSLayer(layerName,driver,wfs.getHost(),
304
                                                wfs.getProjection());
305

    
306
                                if (lyr != null) {
307
                                        lyr.setVisible(true);
308
                                        theView.getMapControl().getMapContext().beginAtomicEvent();
309
                                        theView.getMapControl().getMapContext().getLayers()
310
                                                   .addLayer(lyr);
311
                                        theView.getMapControl().getMapContext().endAtomicEvent();
312
                                }
313
                        }
314

    
315
                }
316
        }
317

    
318
        /**
319
         * @see com.iver.andami.plugins.Extension#isEnabled()
320
         */
321
        public boolean isEnabled() {
322
                return true;
323
        }
324
}