svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / AddLayer.java @ 3718
History | View | Annotate | Download (16.1 KB)
1 | 2268 | 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 | package com.iver.cit.gvsig; |
||
42 | |||
43 | import java.awt.Component; |
||
44 | import java.awt.geom.Rectangle2D; |
||
45 | import java.io.File; |
||
46 | import java.lang.reflect.InvocationTargetException; |
||
47 | import java.util.ArrayList; |
||
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 | 2318 | fjp | import com.hardcode.gdbms.engine.instruction.FieldNotFoundException; |
59 | 2268 | fjp | import com.iver.andami.PluginServices; |
60 | import com.iver.andami.messages.NotificationManager; |
||
61 | import com.iver.andami.plugins.Extension; |
||
62 | import com.iver.cit.gvsig.fmap.DriverException; |
||
63 | 3095 | fjp | import com.iver.cit.gvsig.fmap.ViewPort; |
64 | 2268 | fjp | import com.iver.cit.gvsig.fmap.drivers.RasterDriver; |
65 | import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver; |
||
66 | 2318 | fjp | import com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend; |
67 | 2268 | fjp | import com.iver.cit.gvsig.fmap.layers.FLayer; |
68 | 2318 | fjp | import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
69 | 2268 | fjp | import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
70 | 2318 | fjp | import com.iver.cit.gvsig.fmap.rendering.LegendFactory; |
71 | import com.iver.cit.gvsig.fmap.rendering.VectorialLegend; |
||
72 | 2268 | fjp | import com.iver.cit.gvsig.gui.FOpenDialog; |
73 | import com.iver.cit.gvsig.gui.FileOpenDialog; |
||
74 | import com.iver.cit.gvsig.gui.View; |
||
75 | import com.iver.cit.gvsig.gui.WizardPanel; |
||
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 AddLayer implements Extension { |
||
85 | public FOpenDialog fopen = null; |
||
86 | |||
87 | private static ArrayList wizardStack = null; |
||
88 | |||
89 | static {
|
||
90 | AddLayer.wizardStack = new ArrayList(); |
||
91 | } |
||
92 | |||
93 | public static void addWizard(Class wpClass) { |
||
94 | AddLayer.wizardStack.add(wpClass); |
||
95 | } |
||
96 | |||
97 | public static WizardPanel getInstance(int i) throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { |
||
98 | Class wpClass = (Class) AddLayer.wizardStack.get(i); |
||
99 | Class [] args = {}; |
||
100 | Object [] params = {}; |
||
101 | WizardPanel wp = (WizardPanel) wpClass.getConstructor(args).newInstance(params); |
||
102 | |||
103 | wp.initWizard(); |
||
104 | |||
105 | return wp;
|
||
106 | } |
||
107 | |||
108 | |||
109 | /**
|
||
110 | * @see com.iver.mdiApp.plugins.Extension#isVisible()
|
||
111 | */
|
||
112 | public boolean isVisible() { |
||
113 | com.iver.andami.ui.mdiManager.View f = PluginServices.getMDIManager() |
||
114 | .getActiveView(); |
||
115 | |||
116 | if (f == null) { |
||
117 | return false; |
||
118 | } |
||
119 | |||
120 | return (f.getClass() == View.class); |
||
121 | } |
||
122 | |||
123 | /**
|
||
124 | * @see com.iver.andami.plugins.Extension#inicializar()
|
||
125 | */
|
||
126 | public void inicializar() { |
||
127 | } |
||
128 | |||
129 | 3095 | fjp | private void checkProjection(FLayer lyr, ViewPort viewPort) |
130 | { |
||
131 | if (lyr instanceof FLyrVect) |
||
132 | { |
||
133 | FLyrVect lyrVect = (FLyrVect) lyr; |
||
134 | IProjection proj = lyr.getProjection(); |
||
135 | // Comprobar que la projecci?n es la misma que la vista
|
||
136 | if (proj == null) |
||
137 | { |
||
138 | // SUPONEMOS que la capa est? en la proyecci?n que
|
||
139 | // estamos pidiendo (que ya es mucho suponer, ya).
|
||
140 | lyrVect.setProjection(viewPort.getProjection()); |
||
141 | return;
|
||
142 | } |
||
143 | if (proj != viewPort.getProjection()) {
|
||
144 | int option = JOptionPane.showConfirmDialog(null, |
||
145 | PluginServices.getText(this, "reproyectar_aviso"), |
||
146 | PluginServices.getText(this, "reproyectar_pregunta"), |
||
147 | JOptionPane.YES_NO_OPTION);
|
||
148 | |||
149 | if (option == JOptionPane.NO_OPTION) { |
||
150 | return;
|
||
151 | } else {
|
||
152 | ICoordTrans ct = new CoordTrans((CoordSys) proj,
|
||
153 | (CoordSys) viewPort.getProjection()); |
||
154 | lyrVect.setCoordTrans(ct); |
||
155 | System.err.println("coordTrans = " + |
||
156 | proj.getAbrev() + " " +
|
||
157 | viewPort.getProjection().getAbrev()); |
||
158 | } |
||
159 | } |
||
160 | } |
||
161 | |||
162 | } |
||
163 | 2268 | fjp | /**
|
164 | * @see com.iver.andami.plugins.Extension#execute(java.lang.String)
|
||
165 | */
|
||
166 | public void execute(String actionCommand) { |
||
167 | 2429 | caballero | //Project project = ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).getProject();
|
168 | 2268 | fjp | View theView = (View) PluginServices.getMDIManager().getActiveView(); |
169 | fopen = new FOpenDialog();
|
||
170 | |||
171 | FileOpenDialog fileDlg = new FileOpenDialog(new Class[] { |
||
172 | VectorialFileDriver.class, RasterDriver.class |
||
173 | }); |
||
174 | 3437 | jmorell | fopen.addTab(PluginServices.getText(this, "Fichero"), fileDlg); |
175 | 2268 | fjp | for (int i=0; i<wizardStack.size(); i++) { |
176 | WizardPanel wp; |
||
177 | try {
|
||
178 | wp = AddLayer.getInstance(i); |
||
179 | fopen.addWizardTab(wp.getTabName(), wp); |
||
180 | } catch (IllegalArgumentException e) { |
||
181 | e.printStackTrace(); |
||
182 | } catch (SecurityException e) { |
||
183 | e.printStackTrace(); |
||
184 | } catch (InstantiationException e) { |
||
185 | e.printStackTrace(); |
||
186 | } catch (IllegalAccessException e) { |
||
187 | e.printStackTrace(); |
||
188 | } catch (InvocationTargetException e) { |
||
189 | e.printStackTrace(); |
||
190 | } catch (NoSuchMethodException e) { |
||
191 | e.printStackTrace(); |
||
192 | } |
||
193 | } |
||
194 | |||
195 | PluginServices.getMDIManager().addView(fopen); |
||
196 | |||
197 | if (fopen.isAccepted()) {
|
||
198 | FLayer lyr = null;
|
||
199 | |||
200 | if (fopen.getSelectedTab() == fileDlg) {
|
||
201 | if (fileDlg.getFiles() == null) { |
||
202 | return;
|
||
203 | } |
||
204 | |||
205 | IProjection proj = FOpenDialog.getLastProjection(); |
||
206 | File[] files = fileDlg.getFiles(); |
||
207 | String[] driverNames = fileDlg.getDriverNames(); |
||
208 | Driver[] drivers = new Driver[driverNames.length]; |
||
209 | for (int i = 0; i < drivers.length; i++) { |
||
210 | try {
|
||
211 | drivers[i] = LayerFactory.getDM().getDriver(driverNames[i]); |
||
212 | } catch (DriverLoadException e) {
|
||
213 | NotificationManager.addError("No se pudo cargar el driver", e);
|
||
214 | } |
||
215 | } |
||
216 | Rectangle2D[] rects=new Rectangle2D[files.length]; |
||
217 | boolean first=false; |
||
218 | theView.getMapControl().getMapContext() |
||
219 | .beginAtomicEvent(); |
||
220 | |||
221 | for (int iFile = 0; iFile < files.length; iFile++) { |
||
222 | File fich = files[iFile];
|
||
223 | String layerName = fich.getName();
|
||
224 | 2429 | caballero | //String layerPath = fich.getAbsolutePath();
|
225 | 2268 | fjp | |
226 | try {
|
||
227 | 2515 | fjp | // FJP: Comento esto (if (fileDlg.accept(fich))) para resolver el bug 75. ?Esto estaba antes
|
228 | // o lo apuesto alguien por algo en concreto?.
|
||
229 | // if (fileDlg.accept(fich))
|
||
230 | if (drivers[iFile] instanceof VectorialFileDriver){ |
||
231 | lyr = LayerFactory.createLayer(layerName, |
||
232 | (VectorialFileDriver) drivers[iFile], fich, proj); |
||
233 | }else if (drivers[iFile] instanceof RasterDriver){ |
||
234 | lyr = LayerFactory.createLayer(layerName, |
||
235 | (RasterDriver) drivers[iFile], fich, proj); |
||
236 | } |
||
237 | 2268 | fjp | |
238 | if (lyr != null) { |
||
239 | lyr.setVisible(true);
|
||
240 | if (theView.getMapControl().getMapContext().getViewPort().getExtent()==null){ |
||
241 | first=true;
|
||
242 | } |
||
243 | |||
244 | |||
245 | 2318 | fjp | |
246 | try {
|
||
247 | |||
248 | // Le asignamos tambi?n una legenda por defecto acorde con
|
||
249 | // el tipo de shape que tenga. Tampoco s? si es aqu? el
|
||
250 | // sitio adecuado, pero en fin....
|
||
251 | 3095 | fjp | checkProjection(lyr, theView.getMapControl().getViewPort()); |
252 | 3445 | fjp | theView.getMapControl().getMapContext().getLayers() |
253 | .addLayer(lyr); |
||
254 | |||
255 | 2318 | fjp | if (lyr instanceof FLyrVect) |
256 | { |
||
257 | FLyrVect lyrVect = (FLyrVect) lyr; |
||
258 | if (drivers[iFile] instanceof WithDefaultLegend) { |
||
259 | WithDefaultLegend aux = (WithDefaultLegend) drivers[iFile]; |
||
260 | lyrVect.setLegend((VectorialLegend) aux.getDefaultLegend()); |
||
261 | } else {
|
||
262 | lyrVect.setLegend(LegendFactory.createSingleSymbolLegend( |
||
263 | lyrVect.getShapeType())); |
||
264 | } |
||
265 | } |
||
266 | } catch (FieldNotFoundException e) {
|
||
267 | //Esta no puede saltar
|
||
268 | } |
||
269 | 2268 | fjp | rects[iFile]=lyr.getFullExtent(); |
270 | |||
271 | // TODO: Poner una variable y dibujar solo cuando
|
||
272 | // todas las capas hayan sido cargadas.
|
||
273 | // TODO Se deber? de redibujar mediante la captura de los eventos, por
|
||
274 | //eso se comenta la parte anterior
|
||
275 | // theView.getMapControl().drawMap();
|
||
276 | // theView.getTOC().refresh();
|
||
277 | } |
||
278 | |||
279 | } catch (DriverException e) {
|
||
280 | NotificationManager.addError("Error al crear la capa", e);
|
||
281 | } |
||
282 | } |
||
283 | |||
284 | //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.
|
||
285 | if (rects.length > 1) { |
||
286 | Rectangle2D rect = new Rectangle2D.Double(); |
||
287 | rect.setRect(rects[0]);
|
||
288 | |||
289 | if (first) {
|
||
290 | for (int i = 0; i < rects.length; i++) { |
||
291 | rect.add(rects[i]); |
||
292 | } |
||
293 | |||
294 | theView.getMapControl().getMapContext().getViewPort() |
||
295 | .setExtent(rect); |
||
296 | } |
||
297 | } |
||
298 | theView.getMapControl().getMapContext() |
||
299 | .endAtomicEvent(); |
||
300 | |||
301 | } else if (fopen.getSelectedTab() instanceof WizardPanel) { |
||
302 | WizardPanel wp = (WizardPanel) fopen.getSelectedTab(); |
||
303 | 3083 | fjp | wp.setMapCtrl(theView.getMapControl()); |
304 | 2268 | fjp | wp.execute(); |
305 | lyr = wp.getLayer(); |
||
306 | |||
307 | if (lyr != null) { |
||
308 | lyr.setVisible(true);
|
||
309 | theView.getMapControl().getMapContext().beginAtomicEvent(); |
||
310 | 3418 | fjp | checkProjection(lyr, theView.getMapControl().getViewPort()); |
311 | 2268 | fjp | theView.getMapControl().getMapContext().getLayers() |
312 | .addLayer(lyr); |
||
313 | theView.getMapControl().getMapContext().endAtomicEvent(); |
||
314 | 3418 | fjp | |
315 | 2268 | fjp | } |
316 | /* } else if (fopen.getSelectedTab() == wms) {
|
||
317 | String layerName = wms.getLayerName();
|
||
318 | |||
319 | lyr = LayerFactory.createLayer(layerName,
|
||
320 | wms.getLayersRectangle(), wms.getHost(),
|
||
321 | wms.getFormat(), wms.getLayersQuery(),
|
||
322 | wms.getQueryableLayerQuery(), wms.getSRS());
|
||
323 | |||
324 | if (lyr != null) {
|
||
325 | lyr.setVisible(true);
|
||
326 | theView.getMapControl().getMapContext().beginAtomicEvent();
|
||
327 | theView.getMapControl().getMapContext().getLayers()
|
||
328 | .addLayer(lyr);
|
||
329 | theView.getMapControl().getMapContext().endAtomicEvent();
|
||
330 | }
|
||
331 | } else if (fopen.getSelectedTab() == wiz) {
|
||
332 | try {
|
||
333 | String dbURL = wiz.getConnectionString();
|
||
334 | String user = wiz.getUser();
|
||
335 | String pwd = wiz.getPassword();
|
||
336 | String layerName = wiz.getLayerName();
|
||
337 | String fields = StringUtilities.getComaSeparated(wiz.getFields());
|
||
338 | String tableName = wiz.getTable();
|
||
339 | String whereClause = "";
|
||
340 | int fidField = wiz.getFID();;
|
||
341 | |||
342 | |||
343 | // TODO: ?C?mo se hace esto para que sea gen?rico?.
|
||
344 | // Supongo que el wizard te tiene que decir qu? driver
|
||
345 | // quieres usar....
|
||
346 | // Class.forName("org.postgresql.Driver");
|
||
347 | // Class.forName("com.mysql.jdbc.Driver");
|
||
348 | Connection conn = DriverManager.getConnection(dbURL, user, pwd);
|
||
349 | conn.setAutoCommit(false);
|
||
350 | |||
351 | VectorialDatabaseDriver driver = wiz.getDriver();
|
||
352 | if (driver instanceof DefaultDBDriver)
|
||
353 | {
|
||
354 | DefaultDBDriver dbDriver = (DefaultDBDriver) driver;
|
||
355 | dbDriver.setData(conn, tableName, fields, whereClause, fidField);
|
||
356 | }
|
||
357 | |||
358 | lyr = LayerFactory.createDBLayer(driver, layerName, null);
|
||
359 | |||
360 | if (lyr != null) {
|
||
361 | lyr.setVisible(true);
|
||
362 | theView.getMapControl().getMapContext().beginAtomicEvent();
|
||
363 | theView.getMapControl().getMapContext().getLayers()
|
||
364 | .addLayer(lyr);
|
||
365 | theView.getMapControl().getMapContext().endAtomicEvent();
|
||
366 | }
|
||
367 | |||
368 | } catch (SQLException e) {
|
||
369 | // TODO Auto-generated catch block
|
||
370 | e.printStackTrace();
|
||
371 | // } catch (ClassNotFoundException e) {
|
||
372 | // TODO Auto-generated catch block
|
||
373 | //e.printStackTrace();
|
||
374 | } catch (DriverLoadException e) {
|
||
375 | e.printStackTrace();
|
||
376 | }
|
||
377 | |||
378 | |||
379 | } else if (fopen.getSelectedTab() == wfs)
|
||
380 | {
|
||
381 | String[] layers=wfs.getLayers();
|
||
382 | if (layers.length>0)
|
||
383 | {
|
||
384 | String layerName = wfs.getLayerName();
|
||
385 | String user = wfs.getUser();
|
||
386 | String pwd = wfs.getPassword();
|
||
387 | String url = wfs.getUrlConnect();
|
||
388 | |||
389 | boolean protocol=wfs.getProtocol();
|
||
390 | int numfeatures=wfs.getNumFeatures();
|
||
391 | int timeout=wfs.getTimeout();
|
||
392 | //String fields = wfs.getSqlFields();
|
||
393 | //String whereClause = dbWiz.getSqlWhere();
|
||
394 | WFSDriver driver = new WFSDriver();
|
||
395 | driver.setData(url,user,pwd,layers,protocol,numfeatures,timeout,2);
|
||
396 | lyr = LayerFactory.createWFSLayer(layerName,driver,wfs.getHost(),
|
||
397 | wfs.getProjection());
|
||
398 | |||
399 | if (lyr != null) {
|
||
400 | lyr.setVisible(true);
|
||
401 | theView.getMapControl().getMapContext().beginAtomicEvent();
|
||
402 | theView.getMapControl().getMapContext().getLayers()
|
||
403 | .addLayer(lyr);
|
||
404 | theView.getMapControl().getMapContext().endAtomicEvent();
|
||
405 | }
|
||
406 | }
|
||
407 | } else if (fopen.getSelectedTab() == arcsde_wizard) {
|
||
408 | String dbHost = arcsde_wizard.getHost();
|
||
409 | String port = arcsde_wizard.getPort();
|
||
410 | String user = arcsde_wizard.getUser();
|
||
411 | String pwd = arcsde_wizard.getPassword();
|
||
412 | // String layerName = arcsde_wizard.getLayerName();
|
||
413 | // String fields = StringUtilities.getComaSeparated(wiz.getFields());
|
||
414 | String tableName = arcsde_wizard.getLayerName();
|
||
415 | String schema = arcsde_wizard.getSchema();
|
||
416 | String whereClause = "";
|
||
417 | String [] fields = null;
|
||
418 | |||
419 | |||
420 | ArcSdeDriver driver = new ArcSdeDriver();
|
||
421 | driver.setData(dbHost, Integer.parseInt(port), schema, user, pwd, tableName, fields, whereClause);
|
||
422 | |||
423 | lyr = LayerFactory.createDBLayer(driver, tableName, null);
|
||
424 | |||
425 | if (lyr != null) {
|
||
426 | lyr.setVisible(true);
|
||
427 | theView.getMapControl().getMapContext().beginAtomicEvent();
|
||
428 | theView.getMapControl().getMapContext().getLayers()
|
||
429 | .addLayer(lyr);
|
||
430 | theView.getMapControl().getMapContext().endAtomicEvent();
|
||
431 | }
|
||
432 | |||
433 | } // for*/
|
||
434 | } else {
|
||
435 | JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),"Ninguna capa seleccionada"); |
||
436 | } |
||
437 | } |
||
438 | } |
||
439 | |||
440 | /**
|
||
441 | * @see com.iver.andami.plugins.Extension#isEnabled()
|
||
442 | */
|
||
443 | public boolean isEnabled() { |
||
444 | return true; |
||
445 | } |
||
446 | } |