Statistics
| Revision:

root / branches / gvSIG_GisPlanet / applications / appgvSIG / src / com / iver / cit / gvsig / Abrir.java @ 1874

History | View | Annotate | Download (9.58 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.VectorialFileDriver;
65
import com.iver.cit.gvsig.fmap.drivers.jdbc.mysql.MySQLDriver;
66
import com.iver.cit.gvsig.fmap.layers.FLayer;
67
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
68
import com.iver.cit.gvsig.gui.FOpenDialog;
69
import com.iver.cit.gvsig.gui.FileOpenDialog;
70
import com.iver.cit.gvsig.gui.View;
71
import com.iver.cit.gvsig.gui.WMSDataSourceAdapter;
72
import com.iver.cit.gvsig.gui.jdbcwizard.Wizard;
73
import com.iver.cit.gvsig.gui.wms.WMSWizard;
74
import com.iver.cit.gvsig.project.Project;
75
import com.iver.utiles.StringUtilities;
76

    
77

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

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

    
94
                if (f == null) {
95
                        return false;
96
                }
97

    
98
                return (f.getClass() == View.class);
99
        }
100

    
101
        /**
102
         * @see com.iver.andami.plugins.Extension#inicializar()
103
         */
104
        public void inicializar() {
105
        }
106

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

    
115
                FileOpenDialog fileDlg = new FileOpenDialog(new Class[] {
116
                                        VectorialFileDriver.class, RasterDriver.class
117
                                });
118
                WMSWizard wms = new WMSWizard();
119
                wms.setDataSource(new WMSDataSourceAdapter());
120
                
121
                Wizard wiz = new Wizard();
122
                //        DataBaseOpenDialog dbop = new DataBaseOpenDialog();
123
                //        dbop.setClasses(new Class[] { DBDriver.class });
124
                fopen.addTab("Fichero", fileDlg);
125
                fopen.addWizardTab("JDBC", wiz);
126
                fopen.addWizardTab("WMS", wms);
127
//                fopen.addWizardTab("JDBC", dbWiz);
128

    
129
                //        fopen.addTab("Base de datos", dbop);
130
                PluginServices.getMDIManager().addView(fopen);
131

    
132
                if (fopen.isAccepted()) {
133
                        FLayer lyr = null;
134

    
135
                        if (fopen.getSelectedTab() == fileDlg) {
136
                                if (fileDlg.getFiles() == null) {
137
                                        return;
138
                                }
139

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

    
156
                                for (int iFile = 0; iFile < files.length; iFile++) {
157
                                        File fich = files[iFile];
158
                                        String layerName = fich.getName();
159
                                        String layerPath = fich.getAbsolutePath();
160

    
161
                                        try {
162
                                                if (drivers[iFile] instanceof VectorialFileDriver){
163
                                                        lyr = LayerFactory.createLayer(layerName,
164
                                                                        (VectorialFileDriver) drivers[iFile], fich, proj);
165
                                                }else if (drivers[iFile] instanceof RasterDriver){
166
/*                                                        lyr = LayerFactory.createLayer(layerName,
167
                                                                        driverNames[iFile], fich, proj);
168
        */                                        }
169

    
170
                                                if (lyr != null) {
171
                                                        lyr.setVisible(true);
172
                                                        if (theView.getMapControl().getMapContext().getViewPort().getExtent()==null){
173
                                                                first=true;
174
                                                        }
175

    
176
                                                        // Comprobar que la projecci?n es la misma que la vista
177
                                                        if (proj != theView.getProjection()) {
178
                                                                int option = JOptionPane.showConfirmDialog(null,
179
                                                                        PluginServices.getText(this,"reproyectar_aviso"),
180
                                                                        PluginServices.getText(this,"reproyectar_pregunta"),
181
                                                                                JOptionPane.YES_NO_OPTION);
182

    
183
                                                                if (option == JOptionPane.NO_OPTION) {
184
                                                                        continue;
185
                                                                } else {
186
                                                                        ICoordTrans ct = new CoordTrans((CoordSys) proj,
187
                                                                                        (CoordSys) theView.getProjection());
188
                                                                        lyr.setCoordTrans(ct);
189
                                                                        System.err.println("coordTrans = " +
190
                                                                                proj.getAbrev() + " " +
191
                                                                                theView.getProjection().getAbrev());
192
                                                                }
193
                                                        }
194

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

    
207
                                        } catch (DriverException e) {
208
                                                NotificationManager.addError("Error al crear la capa", e);
209
                                        }
210
                                }
211

    
212
                                //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.
213
                                if (rects.length > 1) {
214
                                        Rectangle2D rect = new Rectangle2D.Double();
215
                                        rect.setRect(rects[0]);
216

    
217
                                        if (first) {
218
                                                for (int i = 0; i < rects.length; i++) {
219
                                                        rect.add(rects[i]);
220
                                                }
221

    
222
                                                theView.getMapControl().getMapContext().getViewPort()
223
                                                           .setExtent(rect);
224
                                        }
225
                                }
226
                                theView.getMapControl().getMapContext()
227
                                   .endAtomicEvent();
228

    
229
                        } else if (fopen.getSelectedTab() == wms) {
230
                                String layerName = wms.getLayerName();
231

    
232
                                lyr = LayerFactory.createLayer(layerName,
233
                                                wms.getLayersRectangle(), wms.getHost(),
234
                                                wms.getFormat(), wms.getLayersQuery(),
235
                                                wms.getQueryableLayerQuery(), wms.getSRS());
236

    
237
                                if (lyr != null) {
238
                                        lyr.setVisible(true);
239
                                        theView.getMapControl().getMapContext().beginAtomicEvent();
240
                                        theView.getMapControl().getMapContext().getLayers()
241
                                                   .addLayer(lyr);
242
                                        theView.getMapControl().getMapContext().endAtomicEvent();
243
                                }
244
                        } else if (fopen.getSelectedTab() == wiz) {
245
                                try {
246
                                        String dbURL = wiz.getConnectionString();
247
                                        String user = wiz.getUser();
248
                                        String pwd = wiz.getPassword();
249
                                        String layerName = wiz.getLayerName();
250
                                        String fields = StringUtilities.getComaSeparated(wiz.getFields());
251
                                        String tableName = wiz.getTable();
252
                                        String whereClause = "";
253
                                        int fidField = wiz.getFID();;
254
                                        
255
                    
256
                    // TODO: ?C?mo se hace esto para que sea gen?rico?.
257
                    // Supongo que el wizard te tiene que decir qu? driver
258
                    // quieres usar....
259
                                    Class.forName("org.postgresql.Driver");
260
                                    Class.forName("com.mysql.jdbc.Driver");
261
                    Connection conn = DriverManager.getConnection(dbURL, user, pwd);
262
                    conn.setAutoCommit(false);
263

    
264
                    VectorialDatabaseDriver driver = wiz.getDriver();
265
                    driver.setData(conn, tableName, fields, whereClause, fidField);
266
                    lyr = LayerFactory.createDBLayer(driver, layerName, null);
267
                    
268
                                    if (lyr != null) {
269
                                            lyr.setVisible(true);
270
                                            theView.getMapControl().getMapContext().beginAtomicEvent();
271
                                            theView.getMapControl().getMapContext().getLayers()
272
                                                       .addLayer(lyr);
273
                                            theView.getMapControl().getMapContext().endAtomicEvent();
274
                                    }
275
                    
276
                } catch (SQLException e) {
277
                    // TODO Auto-generated catch block
278
                    e.printStackTrace();
279
                } catch (ClassNotFoundException e) {
280
                    // TODO Auto-generated catch block
281
                    e.printStackTrace();
282
                } catch (DriverLoadException e) {
283
                                        e.printStackTrace();
284
                                }
285

    
286
                        } // for
287

    
288
                }
289
        }
290

    
291
        /**
292
         * @see com.iver.andami.plugins.Extension#isEnabled()
293
         */
294
        public boolean isEnabled() {
295
                return true;
296
        }
297
}