Statistics
| Revision:

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

History | View | Annotate | Download (8.65 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 com.iver.andami.PluginServices;
44
import com.iver.andami.messages.NotificationManager;
45
import com.iver.andami.plugins.Extension;
46

    
47
import com.iver.cit.gvsig.fmap.DriverException;
48
import com.iver.cit.gvsig.fmap.drivers.RasterDriver;
49
import com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver;
50
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
51
import com.iver.cit.gvsig.fmap.drivers.jdbc.mysql.MySQLdriver;
52
import com.iver.cit.gvsig.fmap.drivers.jdbc.postgis.PostGisDriver;
53
import com.iver.cit.gvsig.fmap.layers.FLayer;
54
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
55
import com.iver.cit.gvsig.gui.FOpenDialog;
56
import com.iver.cit.gvsig.gui.FileOpenDialog;
57
import com.iver.cit.gvsig.gui.View;
58
import com.iver.cit.gvsig.gui.WMSDataSourceAdapter;
59
import com.iver.cit.gvsig.gui.wizards.JDBCWizard;
60
import com.iver.cit.gvsig.gui.wms.WMSWizard;
61

    
62
import org.cresques.cts.ICoordTrans;
63
import org.cresques.cts.IProjection;
64
import org.cresques.cts.gt2.CoordSys;
65
import org.cresques.cts.gt2.CoordTrans;
66

    
67
import java.awt.geom.Rectangle2D;
68

    
69
import java.io.File;
70
import java.sql.Connection;
71
import java.sql.DriverManager;
72
import java.sql.SQLException;
73

    
74
import javax.swing.JOptionPane;
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
        /**
87
         * @see com.iver.mdiApp.plugins.Extension#isVisible()
88
         */
89
        public boolean isVisible() {
90
                com.iver.andami.ui.mdiManager.View f = PluginServices.getMDIManager()
91
                                                                                                                         .getActiveView();
92

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

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

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

    
106
        /**
107
         * @see com.iver.andami.plugins.Extension#execute(java.lang.String)
108
         */
109
        public void execute(String actionCommand) {
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
                                Rectangle2D[] rects=new Rectangle2D[files.length];
142
                                boolean first=false;
143
                                theView.getMapControl().getMapContext()
144
                                   .beginAtomicEvent();
145

    
146
                                for (int iFile = 0; iFile < files.length; iFile++) {
147
                                        File fich = files[iFile];
148
                                        String layerName = fich.getName();
149
                                        String layerPath = fich.getAbsolutePath();
150

    
151
                                        try {
152
                                                lyr = LayerFactory.createLayer(layerName,
153
                                                                driverNames[iFile], fich, proj);
154

    
155
                                                if (lyr != null) {
156
                                                        lyr.setVisible(true);
157
                                                        if (theView.getMapControl().getMapContext().getViewPort().getExtent()==null){
158
                                                                first=true;
159
                                                        }
160

    
161
                                                        // Comprobar que la projecci?n es la misma que la vista
162
                                                        if (proj != theView.getProjection()) {
163
                                                                int option = JOptionPane.showConfirmDialog(null,
164
                                                                        PluginServices.getText(this,"reproyectar_aviso"),
165
                                                                        PluginServices.getText(this,"reproyectar_pregunta"),
166
                                                                                JOptionPane.YES_NO_OPTION);
167

    
168
                                                                if (option == JOptionPane.NO_OPTION) {
169
                                                                        continue;
170
                                                                } else {
171
                                                                        ICoordTrans ct = new CoordTrans((CoordSys) proj,
172
                                                                                        (CoordSys) theView.getProjection());
173
                                                                        lyr.setCoordTrans(ct);
174
                                                                        System.err.println("coordTrans = " +
175
                                                                                proj.getAbrev() + " " +
176
                                                                                theView.getProjection().getAbrev());
177
                                                                }
178
                                                        }
179

    
180
                                                        theView.getMapControl().getMapContext().getLayers()
181
                                                                   .addLayer(lyr);
182
                                                        rects[iFile]=lyr.getFullExtent();
183
                                                        
184
                                                        // TODO: Poner una variable y dibujar solo cuando
185
                                                        // todas las capas hayan sido cargadas.
186
                                                        // TODO Se deber? de redibujar mediante la captura de los eventos, por
187
                                                        //eso se comenta la parte anterior
188
                                                        //                                                        theView.getMapControl().drawMap();
189
                                                        //                                                        theView.getTOC().refresh();
190
                                                }
191

    
192
                                        } catch (DriverException e) {
193
                                                NotificationManager.addError("Error al crear la capa", e);
194
                                        }
195
                                }
196

    
197
                                //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.
198
                                if (rects.length > 1) {
199
                                        Rectangle2D rect = new Rectangle2D.Double();
200
                                        rect.setRect(rects[0]);
201

    
202
                                        if (first) {
203
                                                for (int i = 0; i < rects.length; i++) {
204
                                                        rect.add(rects[i]);
205
                                                }
206

    
207
                                                theView.getMapControl().getMapContext().getViewPort()
208
                                                           .setExtent(rect);
209
                                        }
210
                                }
211
                                theView.getMapControl().getMapContext()
212
                                   .endAtomicEvent();
213

    
214
                        } else if (fopen.getSelectedTab() == wms) {
215
                                String layerName = wms.getLayerName();
216

    
217
                                lyr = LayerFactory.createLayer(layerName,
218
                                                wms.getLayersRectangle(), wms.getHost(),
219
                                                wms.getFormat(), wms.getLayersQuery(),
220
                                                wms.getQueryableLayerQuery(), wms.getSRS());
221

    
222
                                if (lyr != null) {
223
                                        lyr.setVisible(true);
224
                                        theView.getMapControl().getMapContext().beginAtomicEvent();
225
                                        theView.getMapControl().getMapContext().getLayers()
226
                                                   .addLayer(lyr);
227
                                        theView.getMapControl().getMapContext().endAtomicEvent();
228
                                }
229
                        } else if (fopen.getSelectedTab() == dbWiz) {
230
                                String layerName = dbWiz.getLayerName();
231
                                String user = dbWiz.getUser();
232
                                String pwd = dbWiz.getPassword();
233
                                String dbUrl = dbWiz.getUrlConnect();
234
                                String fields = dbWiz.getSqlFields();
235
                                String whereClause = dbWiz.getSqlWhere();
236
                                
237
                                try {
238
                    
239
                    // TODO: ?C?mo se hace esto para que sea gen?rico?.
240
                    // Supongo que el wizard te tiene que decir qu? driver
241
                    // quieres usar....
242
                                    Class.forName("org.postgresql.Driver");
243
                                    Class.forName("com.mysql.jdbc.Driver");
244
                    Connection conn = DriverManager.getConnection(dbUrl, user, pwd);
245
                    conn.setAutoCommit(false);
246

    
247
                    VectorialDatabaseDriver driver = new MySQLdriver();
248
                    driver.setData(conn, layerName, fields, whereClause);
249
                    lyr = LayerFactory.createDBLayer(driver, layerName, null);
250
                    
251
                                    if (lyr != null) {
252
                                            lyr.setVisible(true);
253
                                            theView.getMapControl().getMapContext().beginAtomicEvent();
254
                                            theView.getMapControl().getMapContext().getLayers()
255
                                                       .addLayer(lyr);
256
                                            theView.getMapControl().getMapContext().endAtomicEvent();
257
                                    }
258
                    
259
                } catch (SQLException e) {
260
                    // TODO Auto-generated catch block
261
                    e.printStackTrace();
262
                } catch (ClassNotFoundException e) {
263
                    // TODO Auto-generated catch block
264
                    e.printStackTrace();
265
                }
266

    
267
                        } // for
268

    
269
                }
270
        }
271

    
272
        /**
273
         * @see com.iver.andami.plugins.Extension#isEnabled()
274
         */
275
        public boolean isEnabled() {
276
                return true;
277
        }
278
}