Statistics
| Revision:

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

History | View | Annotate | Download (9.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.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.layers.FLayer;
66
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
67
import com.iver.cit.gvsig.gui.FOpenDialog;
68
import com.iver.cit.gvsig.gui.FileOpenDialog;
69
import com.iver.cit.gvsig.gui.View;
70
import com.iver.cit.gvsig.gui.WMSDataSourceAdapter;
71
import com.iver.cit.gvsig.gui.wizards.JDBCWizard;
72
import com.iver.cit.gvsig.gui.wms.WMSWizard;
73
import com.iver.cit.gvsig.project.Project;
74

    
75

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

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

    
92
                if (f == null) {
93
                        return false;
94
                }
95

    
96
                return (f.getClass() == View.class);
97
        }
98

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

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

    
113
                FileOpenDialog fileDlg = new FileOpenDialog(new Class[] {
114
                                        VectorialFileDriver.class, RasterDriver.class
115
                                });
116
                WMSWizard wms = new WMSWizard();
117
                wms.setDataSource(new WMSDataSourceAdapter());
118
                
119
                JDBCWizard dbWiz = new JDBCWizard();
120

    
121
                //        DataBaseOpenDialog dbop = new DataBaseOpenDialog();
122
                //        dbop.setClasses(new Class[] { DBDriver.class });
123
                fopen.addTab("Fichero", fileDlg);
124
                fopen.addWizardTab("WMS", wms);
125
                fopen.addWizardTab("JDBC", dbWiz);
126

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

    
130
                if (fopen.isAccepted()) {
131
                        FLayer lyr = null;
132

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
235
                                if (lyr != null) {
236
                                        lyr.setVisible(true);
237
                                        theView.getMapControl().getMapContext().beginAtomicEvent();
238
                                        theView.getMapControl().getMapContext().getLayers()
239
                                                   .addLayer(lyr);
240
                                        theView.getMapControl().getMapContext().endAtomicEvent();
241
                                }
242
                        } else if (fopen.getSelectedTab() == dbWiz) {
243
                                String layerName = dbWiz.getLayerName();
244
                                String user = dbWiz.getUser();
245
                                String pwd = dbWiz.getPassword();
246
                                String dbUrl = dbWiz.getUrlConnect();
247
                                String fields = dbWiz.getSqlFields();
248
                                String whereClause = dbWiz.getSqlWhere();
249

    
250
/*                                
251
                                try {
252
                    
253
                    // TODO: ?C?mo se hace esto para que sea gen?rico?.
254
                    // Supongo que el wizard te tiene que decir qu? driver
255
                    // quieres usar....
256
                                    Class.forName("org.postgresql.Driver");
257
                                    Class.forName("com.mysql.jdbc.Driver");
258
                    Connection conn = DriverManager.getConnection(dbUrl, user, pwd);
259
                    conn.setAutoCommit(false);
260

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

    
283
                }
284
        }
285

    
286
        /**
287
         * @see com.iver.andami.plugins.Extension#isEnabled()
288
         */
289
        public boolean isEnabled() {
290
                return true;
291
        }
292
}