Statistics
| Revision:

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

History | View | Annotate | Download (8.55 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.postgis.PostGisDriver;
52
import com.iver.cit.gvsig.fmap.layers.FLayer;
53
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
54
import com.iver.cit.gvsig.gui.FOpenDialog;
55
import com.iver.cit.gvsig.gui.FileOpenDialog;
56
import com.iver.cit.gvsig.gui.View;
57
import com.iver.cit.gvsig.gui.WMSDataSourceAdapter;
58
import com.iver.cit.gvsig.gui.wizards.JDBCWizard;
59
import com.iver.cit.gvsig.gui.wms.WMSWizard;
60

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

    
66
import java.awt.geom.Rectangle2D;
67

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

    
73
import javax.swing.JOptionPane;
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
                View theView = (View) PluginServices.getMDIManager().getActiveView();
110
                fopen = new FOpenDialog();
111

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

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

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

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

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

    
137
                                IProjection proj = FOpenDialog.getLastProjection();
138
                                File[] files = fileDlg.getFiles();
139
                                String[] driverNames = fileDlg.getDriverNames();
140
                                Rectangle2D[] rects=new Rectangle2D[files.length];
141
                                boolean first=false;
142
                                theView.getMapControl().getMapContext()
143
                                   .beginAtomicEvent();
144

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
265
                        } // for
266

    
267
                }
268
        }
269

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