Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / AddLayer.java @ 2277

History | View | Annotate | Download (13.3 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.project.Project;
71

    
72

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

    
98
                wp.initWizard();
99
                
100
                return wp;
101
        }
102

    
103

    
104
        /**
105
         * @see com.iver.mdiApp.plugins.Extension#isVisible()
106
         */
107
        public boolean isVisible() {
108
                com.iver.andami.ui.mdiManager.View f = PluginServices.getMDIManager()
109
                                                                                                                         .getActiveView();
110

    
111
                if (f == null) {
112
                        return false;
113
                }
114

    
115
                return (f.getClass() == View.class);
116
        }
117

    
118
        /**
119
         * @see com.iver.andami.plugins.Extension#inicializar()
120
         */
121
        public void inicializar() {
122
        }
123

    
124
        /**
125
         * @see com.iver.andami.plugins.Extension#execute(java.lang.String)
126
         */
127
        public void execute(String actionCommand) {
128
                Project project = ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).getProject();
129
                View theView = (View) PluginServices.getMDIManager().getActiveView();
130
                fopen = new FOpenDialog();
131

    
132
                FileOpenDialog fileDlg = new FileOpenDialog(new Class[] {
133
                                        VectorialFileDriver.class, RasterDriver.class
134
                                });
135
                fopen.addTab("Fichero", fileDlg);
136
                for (int i=0; i<wizardStack.size(); i++) {
137
                        WizardPanel wp;
138
                        try {
139
                                wp = AddLayer.getInstance(i);
140
                                fopen.addWizardTab(wp.getTabName(), wp);                        
141
                        } catch (IllegalArgumentException e) {
142
                                e.printStackTrace();
143
                        } catch (SecurityException e) {
144
                                e.printStackTrace();
145
                        } catch (InstantiationException e) {
146
                                e.printStackTrace();
147
                        } catch (IllegalAccessException e) {
148
                                e.printStackTrace();
149
                        } catch (InvocationTargetException e) {
150
                                e.printStackTrace();
151
                        } catch (NoSuchMethodException e) {
152
                                e.printStackTrace();
153
                        }
154
                }
155
                
156
                PluginServices.getMDIManager().addView(fopen);
157

    
158
                if (fopen.isAccepted()) {
159
                        FLayer lyr = null;
160

    
161
                        if (fopen.getSelectedTab() == fileDlg) {
162
                                if (fileDlg.getFiles() == null) {
163
                                        return;
164
                                }
165

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

    
182
                                for (int iFile = 0; iFile < files.length; iFile++) {
183
                                        File fich = files[iFile];
184
                                        String layerName = fich.getName();
185
                                        String layerPath = fich.getAbsolutePath();
186

    
187
                                        try {
188
                                                if (drivers[iFile] instanceof VectorialFileDriver){
189
                                                        lyr = LayerFactory.createLayer(layerName,
190
                                                                        (VectorialFileDriver) drivers[iFile], fich, proj);
191
                                                }else if (drivers[iFile] instanceof RasterDriver){
192
                                                        lyr = LayerFactory.createLayer(layerName,
193
                                                                        (RasterDriver) drivers[iFile], fich, proj);
194
                                                }
195

    
196
                                                if (lyr != null) {
197
                                                        lyr.setVisible(true);
198
                                                        if (theView.getMapControl().getMapContext().getViewPort().getExtent()==null){
199
                                                                first=true;
200
                                                        }
201

    
202
                                                        // Comprobar que la projecci?n es la misma que la vista
203
                                                        if (proj != theView.getProjection()) {
204
                                                                int option = JOptionPane.showConfirmDialog(null,
205
                                                                        PluginServices.getText(this,"reproyectar_aviso"),
206
                                                                        PluginServices.getText(this,"reproyectar_pregunta"),
207
                                                                                JOptionPane.YES_NO_OPTION);
208

    
209
                                                                if (option == JOptionPane.NO_OPTION) {
210
                                                                        continue;
211
                                                                } else {
212
                                                                        ICoordTrans ct = new CoordTrans((CoordSys) proj,
213
                                                                                        (CoordSys) theView.getProjection());
214
                                                                        lyr.setCoordTrans(ct);
215
                                                                        System.err.println("coordTrans = " +
216
                                                                                proj.getAbrev() + " " +
217
                                                                                theView.getProjection().getAbrev());
218
                                                                }
219
                                                        }
220

    
221
                                                        theView.getMapControl().getMapContext().getLayers()
222
                                                                   .addLayer(lyr);
223
                                                        rects[iFile]=lyr.getFullExtent();
224
                                                        
225
                                                        // TODO: Poner una variable y dibujar solo cuando
226
                                                        // todas las capas hayan sido cargadas.
227
                                                        // TODO Se deber? de redibujar mediante la captura de los eventos, por
228
                                                        //eso se comenta la parte anterior
229
                                                        //                                                        theView.getMapControl().drawMap();
230
                                                        //                                                        theView.getTOC().refresh();
231
                                                }
232

    
233
                                        } catch (DriverException e) {
234
                                                NotificationManager.addError("Error al crear la capa", e);
235
                                        }
236
                                }
237

    
238
                                //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.
239
                                if (rects.length > 1) {
240
                                        Rectangle2D rect = new Rectangle2D.Double();
241
                                        rect.setRect(rects[0]);
242

    
243
                                        if (first) {
244
                                                for (int i = 0; i < rects.length; i++) {
245
                                                        rect.add(rects[i]);
246
                                                }
247

    
248
                                                theView.getMapControl().getMapContext().getViewPort()
249
                                                           .setExtent(rect);
250
                                        }
251
                                }
252
                                theView.getMapControl().getMapContext()
253
                                   .endAtomicEvent();
254

    
255
                        } else if (fopen.getSelectedTab() instanceof WizardPanel) {
256
                                WizardPanel wp = (WizardPanel) fopen.getSelectedTab();
257
                                
258
                                wp.execute();
259
                                lyr = wp.getLayer();
260
                                
261
                                if (lyr != null) {
262
                                        lyr.setVisible(true);
263
                                        theView.getMapControl().getMapContext().beginAtomicEvent();
264
                                        theView.getMapControl().getMapContext().getLayers()
265
                                                   .addLayer(lyr);
266
                                        theView.getMapControl().getMapContext().endAtomicEvent();
267
                                }
268
                        /* } else if (fopen.getSelectedTab() == wms) {
269
                                String layerName = wms.getLayerName();
270

271
                                lyr = LayerFactory.createLayer(layerName,
272
                                                wms.getLayersRectangle(), wms.getHost(),
273
                                                wms.getFormat(), wms.getLayersQuery(),
274
                                                wms.getQueryableLayerQuery(), wms.getSRS());
275

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

303
                    VectorialDatabaseDriver driver = wiz.getDriver();
304
                    if (driver instanceof DefaultDBDriver)
305
                    {
306
                        DefaultDBDriver dbDriver = (DefaultDBDriver) driver;
307
                        dbDriver.setData(conn, tableName, fields, whereClause, fidField);
308
                    }
309
                    
310
                    lyr = LayerFactory.createDBLayer(driver, layerName, null);
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
                } catch (SQLException e) {
321
                    // TODO Auto-generated catch block
322
                    e.printStackTrace();
323
                // } catch (ClassNotFoundException e) {
324
                    // TODO Auto-generated catch block
325
                    //e.printStackTrace(); 
326
                } catch (DriverLoadException e) {
327
                                        e.printStackTrace();
328
                                }
329

330

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

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

    
392
        /**
393
         * @see com.iver.andami.plugins.Extension#isEnabled()
394
         */
395
        public boolean isEnabled() {
396
                return true;
397
        }
398
}