Statistics
| Revision:

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

History | View | Annotate | Download (13.6 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.lang.reflect.InvocationTargetException;
47
import java.util.ArrayList;
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.VectorialFileDriver;
64
import com.iver.cit.gvsig.fmap.layers.FLayer;
65
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
66
import com.iver.cit.gvsig.gui.FOpenDialog;
67
import com.iver.cit.gvsig.gui.FileOpenDialog;
68
import com.iver.cit.gvsig.gui.View;
69
import com.iver.cit.gvsig.gui.WizardPanel;
70
import com.iver.cit.gvsig.gui.jdbcwizard.Wizard;
71
import com.iver.cit.gvsig.gui.wizards.ArcSdeWizard;
72
import com.iver.cit.gvsig.gui.wizards.WFSWizard;
73
import com.iver.cit.gvsig.gui.wms.WMSWizard;
74
import com.iver.cit.gvsig.project.Project;
75

    
76

    
77
/**
78
 * Extensi?n que abre un di?logo para seleccionar la capa o capas que se
79
 * quieren a?adir a la vista.
80
 *
81
 * @author Fernando Gonz?lez Cort?s
82
 */
83
public class Abrir implements Extension {
84
        public FOpenDialog fopen = null;
85
        
86
        private static ArrayList wizardStack = null;
87
        
88
        static {
89
                Abrir.wizardStack = new ArrayList();
90
                Abrir.addWizard(WMSWizard.class);
91
                Abrir.addWizard(WFSWizard.class);
92
                Abrir.addWizard(Wizard.class);
93
                Abrir.addWizard(ArcSdeWizard.class);
94
        }
95
        
96
        public static void addWizard(Class wpClass) {
97
                Abrir.wizardStack.add(wpClass);
98
        }
99
        
100
        public static WizardPanel getInstance(int i) throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
101
                Class wpClass = (Class) Abrir.wizardStack.get(i);
102
                Class [] args = {};
103
                Object [] params = {};
104
                WizardPanel wp = (WizardPanel) wpClass.getConstructor(args).newInstance(params);
105

    
106
                wp.initWizard();
107
                
108
                return wp;
109
        }
110

    
111

    
112
        /**
113
         * @see com.iver.mdiApp.plugins.Extension#isVisible()
114
         */
115
        public boolean isVisible() {
116
                com.iver.andami.ui.mdiManager.View f = PluginServices.getMDIManager()
117
                                                                                                                         .getActiveView();
118

    
119
                if (f == null) {
120
                        return false;
121
                }
122

    
123
                return (f.getClass() == View.class);
124
        }
125

    
126
        /**
127
         * @see com.iver.andami.plugins.Extension#inicializar()
128
         */
129
        public void inicializar() {
130
        }
131

    
132
        /**
133
         * @see com.iver.andami.plugins.Extension#execute(java.lang.String)
134
         */
135
        public void execute(String actionCommand) {
136
                Project project = ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).getProject();
137
                View theView = (View) PluginServices.getMDIManager().getActiveView();
138
                fopen = new FOpenDialog();
139

    
140
                FileOpenDialog fileDlg = new FileOpenDialog(new Class[] {
141
                                        VectorialFileDriver.class, RasterDriver.class
142
                                });
143
                fopen.addTab("Fichero", fileDlg);
144
                for (int i=0; i<wizardStack.size(); i++) {
145
                        WizardPanel wp;
146
                        try {
147
                                wp = Abrir.getInstance(i);
148
                                fopen.addWizardTab(wp.getTabName(), wp);                        
149
                        } catch (IllegalArgumentException e) {
150
                                e.printStackTrace();
151
                        } catch (SecurityException e) {
152
                                e.printStackTrace();
153
                        } catch (InstantiationException e) {
154
                                e.printStackTrace();
155
                        } catch (IllegalAccessException e) {
156
                                e.printStackTrace();
157
                        } catch (InvocationTargetException e) {
158
                                e.printStackTrace();
159
                        } catch (NoSuchMethodException e) {
160
                                e.printStackTrace();
161
                        }
162
                }
163
                
164
                PluginServices.getMDIManager().addView(fopen);
165

    
166
                if (fopen.isAccepted()) {
167
                        FLayer lyr = null;
168

    
169
                        if (fopen.getSelectedTab() == fileDlg) {
170
                                if (fileDlg.getFiles() == null) {
171
                                        return;
172
                                }
173

    
174
                                IProjection proj = FOpenDialog.getLastProjection();
175
                                File[] files = fileDlg.getFiles();
176
                                String[] driverNames = fileDlg.getDriverNames();
177
                                Driver[] drivers = new Driver[driverNames.length];
178
                                for (int i = 0; i < drivers.length; i++) {
179
                                        try {
180
                                                drivers[i] = LayerFactory.getDM().getDriver(driverNames[i]);
181
                                        } catch (DriverLoadException e) {
182
                                                NotificationManager.addError("No se pudo cargar el driver", e);
183
                                        }
184
                                }
185
                                Rectangle2D[] rects=new Rectangle2D[files.length];
186
                                boolean first=false;
187
                                theView.getMapControl().getMapContext()
188
                                   .beginAtomicEvent();
189

    
190
                                for (int iFile = 0; iFile < files.length; iFile++) {
191
                                        File fich = files[iFile];
192
                                        String layerName = fich.getName();
193
                                        String layerPath = fich.getAbsolutePath();
194

    
195
                                        try {
196
                                                if (drivers[iFile] instanceof VectorialFileDriver){
197
                                                        lyr = LayerFactory.createLayer(layerName,
198
                                                                        (VectorialFileDriver) drivers[iFile], fich, proj);
199
                                                }else if (drivers[iFile] instanceof RasterDriver){
200
                                                        lyr = LayerFactory.createLayer(layerName,
201
                                                                        (RasterDriver) drivers[iFile], fich, proj);
202
                                                }
203

    
204
                                                if (lyr != null) {
205
                                                        lyr.setVisible(true);
206
                                                        if (theView.getMapControl().getMapContext().getViewPort().getExtent()==null){
207
                                                                first=true;
208
                                                        }
209

    
210
                                                        // Comprobar que la projecci?n es la misma que la vista
211
                                                        if (proj != theView.getProjection()) {
212
                                                                int option = JOptionPane.showConfirmDialog(null,
213
                                                                        PluginServices.getText(this,"reproyectar_aviso"),
214
                                                                        PluginServices.getText(this,"reproyectar_pregunta"),
215
                                                                                JOptionPane.YES_NO_OPTION);
216

    
217
                                                                if (option == JOptionPane.NO_OPTION) {
218
                                                                        continue;
219
                                                                } else {
220
                                                                        ICoordTrans ct = new CoordTrans((CoordSys) proj,
221
                                                                                        (CoordSys) theView.getProjection());
222
                                                                        lyr.setCoordTrans(ct);
223
                                                                        System.err.println("coordTrans = " +
224
                                                                                proj.getAbrev() + " " +
225
                                                                                theView.getProjection().getAbrev());
226
                                                                }
227
                                                        }
228

    
229
                                                        theView.getMapControl().getMapContext().getLayers()
230
                                                                   .addLayer(lyr);
231
                                                        rects[iFile]=lyr.getFullExtent();
232
                                                        
233
                                                        // TODO: Poner una variable y dibujar solo cuando
234
                                                        // todas las capas hayan sido cargadas.
235
                                                        // TODO Se deber? de redibujar mediante la captura de los eventos, por
236
                                                        //eso se comenta la parte anterior
237
                                                        //                                                        theView.getMapControl().drawMap();
238
                                                        //                                                        theView.getTOC().refresh();
239
                                                }
240

    
241
                                        } catch (DriverException e) {
242
                                                NotificationManager.addError("Error al crear la capa", e);
243
                                        }
244
                                }
245

    
246
                                //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.
247
                                if (rects.length > 1) {
248
                                        Rectangle2D rect = new Rectangle2D.Double();
249
                                        rect.setRect(rects[0]);
250

    
251
                                        if (first) {
252
                                                for (int i = 0; i < rects.length; i++) {
253
                                                        rect.add(rects[i]);
254
                                                }
255

    
256
                                                theView.getMapControl().getMapContext().getViewPort()
257
                                                           .setExtent(rect);
258
                                        }
259
                                }
260
                                theView.getMapControl().getMapContext()
261
                                   .endAtomicEvent();
262

    
263
                        } else if (fopen.getSelectedTab() instanceof WizardPanel) {
264
                                WizardPanel wp = (WizardPanel) fopen.getSelectedTab();
265
                                
266
                                wp.execute();
267
                                lyr = wp.getLayer();
268
                                
269
                                if (lyr != null) {
270
                                        lyr.setVisible(true);
271
                                        theView.getMapControl().getMapContext().beginAtomicEvent();
272
                                        theView.getMapControl().getMapContext().getLayers()
273
                                                   .addLayer(lyr);
274
                                        theView.getMapControl().getMapContext().endAtomicEvent();
275
                                }
276
                        /* } else if (fopen.getSelectedTab() == wms) {
277
                                String layerName = wms.getLayerName();
278

279
                                lyr = LayerFactory.createLayer(layerName,
280
                                                wms.getLayersRectangle(), wms.getHost(),
281
                                                wms.getFormat(), wms.getLayersQuery(),
282
                                                wms.getQueryableLayerQuery(), wms.getSRS());
283

284
                                if (lyr != null) {
285
                                        lyr.setVisible(true);
286
                                        theView.getMapControl().getMapContext().beginAtomicEvent();
287
                                        theView.getMapControl().getMapContext().getLayers()
288
                                                   .addLayer(lyr);
289
                                        theView.getMapControl().getMapContext().endAtomicEvent();
290
                                }
291
                        } else if (fopen.getSelectedTab() == wiz) {
292
                try {
293
                                        String dbURL = wiz.getConnectionString();
294
                                        String user = wiz.getUser();
295
                                        String pwd = wiz.getPassword();
296
                                        String layerName = wiz.getLayerName();
297
                                        String fields = StringUtilities.getComaSeparated(wiz.getFields());
298
                                        String tableName = wiz.getTable();
299
                                        String whereClause = "";
300
                                        int fidField = wiz.getFID();;
301
                                        
302
                    
303
                    // TODO: ?C?mo se hace esto para que sea gen?rico?.
304
                    // Supongo que el wizard te tiene que decir qu? driver
305
                    // quieres usar....
306
                                    // Class.forName("org.postgresql.Driver");
307
                                    // Class.forName("com.mysql.jdbc.Driver");
308
                    Connection conn = DriverManager.getConnection(dbURL, user, pwd);
309
                    conn.setAutoCommit(false);
310

311
                    VectorialDatabaseDriver driver = wiz.getDriver();
312
                    if (driver instanceof DefaultDBDriver)
313
                    {
314
                        DefaultDBDriver dbDriver = (DefaultDBDriver) driver;
315
                        dbDriver.setData(conn, tableName, fields, whereClause, fidField);
316
                    }
317
                    
318
                    lyr = LayerFactory.createDBLayer(driver, layerName, null);
319
                    
320
                                    if (lyr != null) {
321
                                            lyr.setVisible(true);
322
                                            theView.getMapControl().getMapContext().beginAtomicEvent();
323
                                            theView.getMapControl().getMapContext().getLayers()
324
                                                       .addLayer(lyr);
325
                                            theView.getMapControl().getMapContext().endAtomicEvent();
326
                                    }
327
                    
328
                } catch (SQLException e) {
329
                    // TODO Auto-generated catch block
330
                    e.printStackTrace();
331
                // } catch (ClassNotFoundException e) {
332
                    // TODO Auto-generated catch block
333
                    //e.printStackTrace(); 
334
                } catch (DriverLoadException e) {
335
                                        e.printStackTrace();
336
                                }
337

338

339
                        } else if (fopen.getSelectedTab() == wfs) 
340
            {
341
                                String[] layers=wfs.getLayers();
342
                                if (layers.length>0)
343
                {
344
                                        String layerName = wfs.getLayerName();
345
                                        String user = wfs.getUser();
346
                                        String pwd = wfs.getPassword();
347
                                        String url = wfs.getUrlConnect();
348
                                
349
                                        boolean protocol=wfs.getProtocol();
350
                                        int numfeatures=wfs.getNumFeatures();
351
                                        int timeout=wfs.getTimeout();
352
                                //String fields = wfs.getSqlFields();
353
                                //String whereClause = dbWiz.getSqlWhere();
354
                                        WFSDriver driver = new WFSDriver();
355
                                        driver.setData(url,user,pwd,layers,protocol,numfeatures,timeout,2);
356
                                        lyr = LayerFactory.createWFSLayer(layerName,driver,wfs.getHost(),
357
                                                wfs.getProjection());
358

359
                                        if (lyr != null) {
360
                                                lyr.setVisible(true);
361
                                                theView.getMapControl().getMapContext().beginAtomicEvent();
362
                                                theView.getMapControl().getMapContext().getLayers()
363
                                                   .addLayer(lyr);
364
                                                theView.getMapControl().getMapContext().endAtomicEvent();
365
                                        }
366
                }
367
            } else if (fopen.getSelectedTab() == arcsde_wizard) {
368
                    String dbHost = arcsde_wizard.getHost();
369
                    String port = arcsde_wizard.getPort();
370
                    String user = arcsde_wizard.getUser();
371
                    String pwd = arcsde_wizard.getPassword();
372
                    // String layerName = arcsde_wizard.getLayerName();
373
                    // String fields = StringUtilities.getComaSeparated(wiz.getFields());
374
                    String tableName = arcsde_wizard.getLayerName();
375
                    String schema = arcsde_wizard.getSchema();
376
                    String whereClause = "";
377
                    String [] fields = null;
378
                    
379
                    
380
                    ArcSdeDriver driver = new ArcSdeDriver();
381
                    driver.setData(dbHost, Integer.parseInt(port), schema, user, pwd, tableName, fields, whereClause);
382
                    
383
                    lyr = LayerFactory.createDBLayer(driver, tableName, null);
384
                    
385
                    if (lyr != null) {
386
                        lyr.setVisible(true);
387
                        theView.getMapControl().getMapContext().beginAtomicEvent();
388
                        theView.getMapControl().getMapContext().getLayers()
389
                               .addLayer(lyr);
390
                        theView.getMapControl().getMapContext().endAtomicEvent();
391
                    }                   
392
                    
393
                        } // for*/
394
                        } else {
395
                                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),"Ninguna capa seleccionada");
396
                        }
397
                }
398
        }
399

    
400
        /**
401
         * @see com.iver.andami.plugins.Extension#isEnabled()
402
         */
403
        public boolean isEnabled() {
404
                return true;
405
        }
406
}