svn-gvsig-desktop / branches / gvSIG_03_SLD / applications / appgvSIG / src / com / iver / cit / gvsig / Abrir.java @ 2131
History | View | Annotate | Download (11 KB)
1 | 1103 | fjp | /* 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 | 312 | fernando | package com.iver.cit.gvsig; |
42 | |||
43 | 1830 | fernando | 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 | 596 | fernando | import com.iver.andami.PluginServices; |
59 | 755 | fernando | import com.iver.andami.messages.NotificationManager; |
60 | 596 | fernando | import com.iver.andami.plugins.Extension; |
61 | 652 | fernando | import com.iver.cit.gvsig.fmap.DriverException; |
62 | 671 | luisw | import com.iver.cit.gvsig.fmap.drivers.RasterDriver; |
63 | 1691 | fjp | import com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver; |
64 | 1889 | vcaballero | import com.iver.cit.gvsig.fmap.drivers.VectorialDriver; |
65 | 378 | fjp | import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver; |
66 | 2131 | fjp | import com.iver.cit.gvsig.fmap.drivers.jdbc.mysql.MySQLDriver; |
67 | 1855 | fjp | import com.iver.cit.gvsig.fmap.drivers.jdbc.postgis.PostGisDriver; |
68 | 1889 | vcaballero | import com.iver.cit.gvsig.fmap.drivers.wfs.WFSDriver; |
69 | 357 | fjp | import com.iver.cit.gvsig.fmap.layers.FLayer; |
70 | import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
||
71 | 312 | fernando | import com.iver.cit.gvsig.gui.FOpenDialog; |
72 | 429 | fernando | import com.iver.cit.gvsig.gui.FileOpenDialog; |
73 | 312 | fernando | import com.iver.cit.gvsig.gui.View; |
74 | 854 | fernando | import com.iver.cit.gvsig.gui.WMSDataSourceAdapter; |
75 | 1908 | vcaballero | import com.iver.cit.gvsig.gui.wfs.WFSDataSourceAdapter; |
76 | 1691 | fjp | import com.iver.cit.gvsig.gui.wizards.JDBCWizard; |
77 | 1889 | vcaballero | import com.iver.cit.gvsig.gui.wizards.WFSWizard; |
78 | 854 | fernando | import com.iver.cit.gvsig.gui.wms.WMSWizard; |
79 | 1830 | fernando | import com.iver.cit.gvsig.project.Project; |
80 | 312 | fernando | |
81 | |||
82 | /**
|
||
83 | 1219 | vcaballero | * Extensi?n que abre un di?logo para seleccionar la capa o capas que se
|
84 | * quieren a?adir a la vista.
|
||
85 | 312 | fernando | *
|
86 | * @author Fernando Gonz?lez Cort?s
|
||
87 | */
|
||
88 | 596 | fernando | public class Abrir implements Extension { |
89 | 854 | fernando | public FOpenDialog fopen = null; |
90 | 312 | fernando | |
91 | 854 | fernando | /**
|
92 | * @see com.iver.mdiApp.plugins.Extension#isVisible()
|
||
93 | */
|
||
94 | public boolean isVisible() { |
||
95 | com.iver.andami.ui.mdiManager.View f = PluginServices.getMDIManager() |
||
96 | .getActiveView(); |
||
97 | 596 | fernando | |
98 | 854 | fernando | if (f == null) { |
99 | return false; |
||
100 | } |
||
101 | 596 | fernando | |
102 | 854 | fernando | return (f.getClass() == View.class); |
103 | } |
||
104 | 596 | fernando | |
105 | /**
|
||
106 | * @see com.iver.andami.plugins.Extension#inicializar()
|
||
107 | */
|
||
108 | public void inicializar() { |
||
109 | } |
||
110 | |||
111 | /**
|
||
112 | * @see com.iver.andami.plugins.Extension#execute(java.lang.String)
|
||
113 | */
|
||
114 | 854 | fernando | public void execute(String actionCommand) { |
115 | 1830 | fernando | Project project = ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).getProject(); |
116 | 854 | fernando | View theView = (View) PluginServices.getMDIManager().getActiveView(); |
117 | fopen = new FOpenDialog();
|
||
118 | 438 | fernando | |
119 | 854 | fernando | FileOpenDialog fileDlg = new FileOpenDialog(new Class[] { |
120 | VectorialFileDriver.class, RasterDriver.class |
||
121 | }); |
||
122 | WMSWizard wms = new WMSWizard();
|
||
123 | wms.setDataSource(new WMSDataSourceAdapter());
|
||
124 | 1691 | fjp | |
125 | JDBCWizard dbWiz = new JDBCWizard();
|
||
126 | 1889 | vcaballero | WFSWizard wfs = new WFSWizard();
|
127 | 1904 | vcaballero | wfs.setDataSource(new WFSDataSourceAdapter());
|
128 | 854 | fernando | // DataBaseOpenDialog dbop = new DataBaseOpenDialog();
|
129 | // dbop.setClasses(new Class[] { DBDriver.class });
|
||
130 | fopen.addTab("Fichero", fileDlg);
|
||
131 | fopen.addWizardTab("WMS", wms);
|
||
132 | 1889 | vcaballero | fopen.addWizardTab("WFS",wfs);
|
133 | 1691 | fjp | fopen.addWizardTab("JDBC", dbWiz);
|
134 | 1889 | vcaballero | |
135 | 438 | fernando | |
136 | 854 | fernando | // fopen.addTab("Base de datos", dbop);
|
137 | PluginServices.getMDIManager().addView(fopen); |
||
138 | 438 | fernando | |
139 | 854 | fernando | if (fopen.isAccepted()) {
|
140 | FLayer lyr = null;
|
||
141 | 438 | fernando | |
142 | 854 | fernando | if (fopen.getSelectedTab() == fileDlg) {
|
143 | if (fileDlg.getFiles() == null) { |
||
144 | return;
|
||
145 | } |
||
146 | 438 | fernando | |
147 | 904 | fjp | IProjection proj = FOpenDialog.getLastProjection(); |
148 | 854 | fernando | File[] files = fileDlg.getFiles(); |
149 | 1171 | fjp | String[] driverNames = fileDlg.getDriverNames(); |
150 | 1830 | fernando | Driver[] drivers = new Driver[driverNames.length]; |
151 | for (int i = 0; i < drivers.length; i++) { |
||
152 | try {
|
||
153 | drivers[i] = LayerFactory.getDM().getDriver(driverNames[i]); |
||
154 | } catch (DriverLoadException e) {
|
||
155 | NotificationManager.addError("No se pudo cargar el driver", e);
|
||
156 | } |
||
157 | } |
||
158 | 870 | vcaballero | Rectangle2D[] rects=new Rectangle2D[files.length]; |
159 | boolean first=false; |
||
160 | 1216 | fjp | theView.getMapControl().getMapContext() |
161 | .beginAtomicEvent(); |
||
162 | |||
163 | 854 | fernando | for (int iFile = 0; iFile < files.length; iFile++) { |
164 | File fich = files[iFile];
|
||
165 | String layerName = fich.getName();
|
||
166 | String layerPath = fich.getAbsolutePath();
|
||
167 | 438 | fernando | |
168 | 854 | fernando | try {
|
169 | 1830 | fernando | if (drivers[iFile] instanceof VectorialFileDriver){ |
170 | lyr = LayerFactory.createLayer(layerName, |
||
171 | (VectorialFileDriver) drivers[iFile], fich, proj); |
||
172 | }else if (drivers[iFile] instanceof RasterDriver){ |
||
173 | /* lyr = LayerFactory.createLayer(layerName,
|
||
174 | driverNames[iFile], fich, proj);
|
||
175 | */ }
|
||
176 | 854 | fernando | |
177 | if (lyr != null) { |
||
178 | lyr.setVisible(true);
|
||
179 | 870 | vcaballero | if (theView.getMapControl().getMapContext().getViewPort().getExtent()==null){ |
180 | first=true;
|
||
181 | } |
||
182 | 1219 | vcaballero | |
183 | 904 | fjp | // Comprobar que la projecci?n es la misma que la vista
|
184 | 1219 | vcaballero | if (proj != theView.getProjection()) {
|
185 | int option = JOptionPane.showConfirmDialog(null, |
||
186 | 1470 | fjp | PluginServices.getText(this,"reproyectar_aviso"), |
187 | PluginServices.getText(this,"reproyectar_pregunta"), |
||
188 | 1219 | vcaballero | JOptionPane.YES_NO_OPTION);
|
189 | 904 | fjp | |
190 | 1219 | vcaballero | if (option == JOptionPane.NO_OPTION) { |
191 | 904 | fjp | continue;
|
192 | 1219 | vcaballero | } else {
|
193 | ICoordTrans ct = new CoordTrans((CoordSys) proj,
|
||
194 | (CoordSys) theView.getProjection()); |
||
195 | lyr.setCoordTrans(ct); |
||
196 | System.err.println("coordTrans = " + |
||
197 | proj.getAbrev() + " " +
|
||
198 | theView.getProjection().getAbrev()); |
||
199 | } |
||
200 | 904 | fjp | } |
201 | 1219 | vcaballero | |
202 | 854 | fernando | theView.getMapControl().getMapContext().getLayers() |
203 | .addLayer(lyr); |
||
204 | 870 | vcaballero | rects[iFile]=lyr.getFullExtent(); |
205 | |||
206 | 854 | fernando | // TODO: Poner una variable y dibujar solo cuando
|
207 | // todas las capas hayan sido cargadas.
|
||
208 | // TODO Se deber? de redibujar mediante la captura de los eventos, por
|
||
209 | //eso se comenta la parte anterior
|
||
210 | 1219 | vcaballero | // theView.getMapControl().drawMap();
|
211 | // theView.getTOC().refresh();
|
||
212 | 854 | fernando | } |
213 | 1216 | fjp | |
214 | 854 | fernando | } catch (DriverException e) {
|
215 | NotificationManager.addError("Error al crear la capa", e);
|
||
216 | 1219 | vcaballero | } |
217 | 854 | fernando | } |
218 | 1219 | vcaballero | |
219 | 870 | vcaballero | //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.
|
220 | 1219 | vcaballero | if (rects.length > 1) { |
221 | Rectangle2D rect = new Rectangle2D.Double(); |
||
222 | 870 | vcaballero | rect.setRect(rects[0]);
|
223 | 1219 | vcaballero | |
224 | if (first) {
|
||
225 | for (int i = 0; i < rects.length; i++) { |
||
226 | 870 | vcaballero | rect.add(rects[i]); |
227 | } |
||
228 | 1219 | vcaballero | |
229 | theView.getMapControl().getMapContext().getViewPort() |
||
230 | .setExtent(rect); |
||
231 | 870 | vcaballero | } |
232 | } |
||
233 | 1216 | fjp | theView.getMapControl().getMapContext() |
234 | .endAtomicEvent(); |
||
235 | |||
236 | 854 | fernando | } else if (fopen.getSelectedTab() == wms) { |
237 | String layerName = wms.getLayerName();
|
||
238 | |||
239 | lyr = LayerFactory.createLayer(layerName, |
||
240 | wms.getLayersRectangle(), wms.getHost(), |
||
241 | wms.getFormat(), wms.getLayersQuery(), |
||
242 | wms.getQueryableLayerQuery(), wms.getSRS()); |
||
243 | |||
244 | if (lyr != null) { |
||
245 | lyr.setVisible(true);
|
||
246 | theView.getMapControl().getMapContext().beginAtomicEvent(); |
||
247 | theView.getMapControl().getMapContext().getLayers() |
||
248 | .addLayer(lyr); |
||
249 | theView.getMapControl().getMapContext().endAtomicEvent(); |
||
250 | } |
||
251 | 1691 | fjp | } else if (fopen.getSelectedTab() == dbWiz) { |
252 | String layerName = dbWiz.getLayerName();
|
||
253 | String user = dbWiz.getUser();
|
||
254 | String pwd = dbWiz.getPassword();
|
||
255 | String dbUrl = dbWiz.getUrlConnect();
|
||
256 | String fields = dbWiz.getSqlFields();
|
||
257 | String whereClause = dbWiz.getSqlWhere();
|
||
258 | 1836 | fernando | |
259 | 1852 | fjp | |
260 | 1691 | fjp | try {
|
261 | |||
262 | // TODO: ?C?mo se hace esto para que sea gen?rico?.
|
||
263 | // Supongo que el wizard te tiene que decir qu? driver
|
||
264 | // quieres usar....
|
||
265 | Class.forName("org.postgresql.Driver"); |
||
266 | 1827 | fjp | Class.forName("com.mysql.jdbc.Driver"); |
267 | 1691 | fjp | Connection conn = DriverManager.getConnection(dbUrl, user, pwd); |
268 | conn.setAutoCommit(false);
|
||
269 | |||
270 | 1855 | fjp | VectorialDatabaseDriver driver = null;
|
271 | if (dbUrl.indexOf("mysql") != -1) |
||
272 | 2131 | fjp | driver = new MySQLDriver();
|
273 | 1855 | fjp | if (dbUrl.indexOf("postgresql") != -1) |
274 | driver = new PostGisDriver();
|
||
275 | |||
276 | 1862 | fjp | driver.setData(conn, layerName, fields, whereClause, 2);
|
277 | 1691 | fjp | lyr = LayerFactory.createDBLayer(driver, layerName, null);
|
278 | |||
279 | if (lyr != null) { |
||
280 | lyr.setVisible(true);
|
||
281 | theView.getMapControl().getMapContext().beginAtomicEvent(); |
||
282 | theView.getMapControl().getMapContext().getLayers() |
||
283 | .addLayer(lyr); |
||
284 | theView.getMapControl().getMapContext().endAtomicEvent(); |
||
285 | } |
||
286 | |||
287 | } catch (SQLException e) { |
||
288 | // TODO Auto-generated catch block
|
||
289 | e.printStackTrace(); |
||
290 | } catch (ClassNotFoundException e) { |
||
291 | // TODO Auto-generated catch block
|
||
292 | e.printStackTrace(); |
||
293 | } |
||
294 | 1852 | fjp | |
295 | 1889 | vcaballero | } else if (fopen.getSelectedTab() == wfs) { |
296 | //String layerName = wfs.getLayerName();
|
||
297 | String layerName = wfs.getLayerName();
|
||
298 | String user = wfs.getUser();
|
||
299 | String pwd = wfs.getPassword();
|
||
300 | 1904 | vcaballero | String url = wfs.getUrlConnect();
|
301 | String[] layers=wfs.getLayers(); |
||
302 | boolean protocol=wfs.getProtocol();
|
||
303 | int numfeatures=wfs.getNumFeatures();
|
||
304 | int timeout=wfs.getTimeout();
|
||
305 | 1889 | vcaballero | //String fields = wfs.getSqlFields();
|
306 | //String whereClause = dbWiz.getSqlWhere();
|
||
307 | WFSDriver driver = new WFSDriver();
|
||
308 | 1904 | vcaballero | driver.setData(url,user,pwd,layers,protocol,numfeatures,timeout,2);
|
309 | 1889 | vcaballero | lyr = LayerFactory.createWFSLayer(layerName,driver,wfs.getHost(), |
310 | wfs.getProjection()); |
||
311 | 1691 | fjp | |
312 | 1889 | vcaballero | 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 | |||
321 | 854 | fernando | } |
322 | 596 | fernando | } |
323 | 438 | fernando | |
324 | 596 | fernando | /**
|
325 | * @see com.iver.andami.plugins.Extension#isEnabled()
|
||
326 | */
|
||
327 | public boolean isEnabled() { |
||
328 | return true; |
||
329 | } |
||
330 | 312 | fernando | } |