root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / AddLayer.java @ 9212
History | View | Annotate | Download (13.3 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 | 7476 | azabala | import java.util.List; |
49 | 2268 | fjp | |
50 | import javax.swing.JOptionPane; |
||
51 | |||
52 | import org.cresques.cts.ICoordTrans; |
||
53 | import org.cresques.cts.IProjection; |
||
54 | |||
55 | import com.hardcode.driverManager.Driver; |
||
56 | import com.hardcode.driverManager.DriverLoadException; |
||
57 | import com.iver.andami.PluginServices; |
||
58 | import com.iver.andami.plugins.Extension; |
||
59 | import com.iver.cit.gvsig.fmap.DriverException; |
||
60 | 7476 | azabala | import com.iver.cit.gvsig.fmap.DriverIOExceptionType; |
61 | import com.iver.cit.gvsig.fmap.DriverNotLoadedExceptionType; |
||
62 | import com.iver.cit.gvsig.fmap.GenericDriverExceptionType; |
||
63 | 5190 | jmvivo | import com.iver.cit.gvsig.fmap.MapControl; |
64 | 3095 | fjp | import com.iver.cit.gvsig.fmap.ViewPort; |
65 | 2268 | fjp | import com.iver.cit.gvsig.fmap.drivers.RasterDriver; |
66 | import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver; |
||
67 | 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 | import com.iver.cit.gvsig.gui.WizardPanel; |
||
71 | 7304 | caballero | import com.iver.cit.gvsig.project.documents.gui.FOpenDialog; |
72 | import com.iver.cit.gvsig.project.documents.gui.FileOpenDialog; |
||
73 | 7537 | sbayarri | import com.iver.cit.gvsig.project.documents.view.gui.BaseView; |
74 | 7738 | jaume | import com.iver.cit.gvsig.project.documents.view.gui.IView; |
75 | 2268 | fjp | |
76 | |||
77 | /**
|
||
78 | 7476 | azabala | * Extensi?n que abre un di?logo para seleccionar la capa o capas que se quieren
|
79 | * a?adir a la vista.
|
||
80 | 7537 | sbayarri | *
|
81 | 2268 | fjp | * @author Fernando Gonz?lez Cort?s
|
82 | */
|
||
83 | 5005 | jorpiell | public class AddLayer extends Extension { |
84 | 2268 | fjp | public FOpenDialog fopen = null; |
85 | 7476 | azabala | |
86 | 2268 | fjp | private static ArrayList wizardStack = null; |
87 | 7476 | azabala | |
88 | 2268 | fjp | static {
|
89 | AddLayer.wizardStack = new ArrayList(); |
||
90 | } |
||
91 | 7476 | azabala | |
92 | 2268 | fjp | public static void addWizard(Class wpClass) { |
93 | AddLayer.wizardStack.add(wpClass); |
||
94 | } |
||
95 | 7476 | azabala | |
96 | public static WizardPanel getInstance(int i) |
||
97 | throws IllegalArgumentException, SecurityException, |
||
98 | InstantiationException, IllegalAccessException, |
||
99 | InvocationTargetException, NoSuchMethodException { |
||
100 | 2268 | fjp | Class wpClass = (Class) AddLayer.wizardStack.get(i); |
101 | 7476 | azabala | Class[] args = {}; |
102 | Object[] params = {}; |
||
103 | WizardPanel wp = (WizardPanel) wpClass.getConstructor(args) |
||
104 | .newInstance(params); |
||
105 | 2268 | fjp | |
106 | wp.initWizard(); |
||
107 | 7476 | azabala | |
108 | 2268 | fjp | return wp;
|
109 | } |
||
110 | |||
111 | /**
|
||
112 | 5005 | jorpiell | * @see com.iver.mdiApp.plugins.IExtension#isVisible()
|
113 | 2268 | fjp | */
|
114 | public boolean isVisible() { |
||
115 | 7537 | sbayarri | com.iver.andami.ui.mdiManager.IWindow window = PluginServices.getMDIManager() |
116 | .getActiveWindow(); |
||
117 | 2268 | fjp | |
118 | 7537 | sbayarri | if (window == null) { |
119 | 2268 | fjp | return false; |
120 | } |
||
121 | |||
122 | 7537 | sbayarri | // Any view derived from BaseView should have AddLayer available
|
123 | |||
124 | IView view; |
||
125 | try {
|
||
126 | view = (IView)window; |
||
127 | } |
||
128 | catch (ClassCastException e) { |
||
129 | return false; |
||
130 | } |
||
131 | |||
132 | if (view == null) |
||
133 | return false; |
||
134 | |||
135 | BaseView baseView = (BaseView)view; |
||
136 | return (baseView != null); |
||
137 | 2268 | fjp | } |
138 | |||
139 | /**
|
||
140 | 5005 | jorpiell | * @see com.iver.andami.plugins.IExtension#initialize()
|
141 | 2268 | fjp | */
|
142 | 5005 | jorpiell | public void initialize() { |
143 | 2268 | fjp | } |
144 | |||
145 | 7476 | azabala | private void checkProjection(FLayer lyr, ViewPort viewPort) { |
146 | if (lyr instanceof FLyrVect) { |
||
147 | FLyrVect lyrVect = (FLyrVect) lyr; |
||
148 | IProjection proj = lyr.getProjection(); |
||
149 | // Comprobar que la projecci?n es la misma que la vista
|
||
150 | if (proj == null) { |
||
151 | // SUPONEMOS que la capa est? en la proyecci?n que
|
||
152 | // estamos pidiendo (que ya es mucho suponer, ya).
|
||
153 | lyrVect.setProjection(viewPort.getProjection()); |
||
154 | return;
|
||
155 | } |
||
156 | if (proj != viewPort.getProjection()) {
|
||
157 | 8916 | caballero | int option = JOptionPane.showConfirmDialog((Component)PluginServices.getMainFrame(), PluginServices |
158 | 7476 | azabala | .getText(this, "reproyectar_aviso"), PluginServices |
159 | .getText(this, "reproyectar_pregunta"), |
||
160 | JOptionPane.YES_NO_OPTION);
|
||
161 | 3095 | fjp | |
162 | 8916 | caballero | if (option != JOptionPane.OK_OPTION) { |
163 | 7476 | azabala | return;
|
164 | } else {
|
||
165 | ICoordTrans ct = proj.getCT(viewPort.getProjection()); |
||
166 | lyrVect.setCoordTrans(ct); |
||
167 | System.err.println("coordTrans = " + proj.getAbrev() + " " |
||
168 | + viewPort.getProjection().getAbrev()); |
||
169 | } |
||
170 | } |
||
171 | } |
||
172 | 3095 | fjp | |
173 | 7476 | azabala | } |
174 | |||
175 | 2268 | fjp | /**
|
176 | 5005 | jorpiell | * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
|
177 | 2268 | fjp | */
|
178 | public void execute(String actionCommand) { |
||
179 | 7476 | azabala | // Project project = ((ProjectExtension)
|
180 | // PluginServices.getExtension(ProjectExtension.class)).getProject();
|
||
181 | 7537 | sbayarri | BaseView theView = (BaseView) PluginServices.getMDIManager().getActiveWindow(); |
182 | 7675 | caballero | MapControl mapControl=theView.getMapControl(); |
183 | this.addLayers(mapControl);
|
||
184 | mapControl.getMapContext().callLegendChanged(); |
||
185 | 5190 | jmvivo | return;
|
186 | |||
187 | } |
||
188 | |||
189 | /**
|
||
190 | * @see com.iver.andami.plugins.IExtension#isEnabled()
|
||
191 | */
|
||
192 | public boolean isEnabled() { |
||
193 | return true; |
||
194 | } |
||
195 | |||
196 | /**
|
||
197 | 7476 | azabala | * Creates FOpenDialog, and adds file tab, and additional registered tabs
|
198 | 7537 | sbayarri | *
|
199 | 7476 | azabala | * @return FOpenDialog
|
200 | */
|
||
201 | private FOpenDialog createFOpenDialog() {
|
||
202 | 2268 | fjp | fopen = new FOpenDialog();
|
203 | FileOpenDialog fileDlg = new FileOpenDialog(new Class[] { |
||
204 | 7476 | azabala | VectorialFileDriver.class, RasterDriver.class }); |
205 | // first, file wizard tab
|
||
206 | 3437 | jmorell | fopen.addTab(PluginServices.getText(this, "Fichero"), fileDlg); |
207 | 7476 | azabala | |
208 | // after that, all registerez tabs (wizardpanels implementations)
|
||
209 | for (int i = 0; i < wizardStack.size(); i++) { |
||
210 | 2268 | fjp | WizardPanel wp; |
211 | try {
|
||
212 | wp = AddLayer.getInstance(i); |
||
213 | 7476 | azabala | fopen.addWizardTab(wp.getTabName(), wp); |
214 | 2268 | fjp | } catch (IllegalArgumentException e) { |
215 | e.printStackTrace(); |
||
216 | } catch (SecurityException e) { |
||
217 | e.printStackTrace(); |
||
218 | } catch (InstantiationException e) { |
||
219 | e.printStackTrace(); |
||
220 | } catch (IllegalAccessException e) { |
||
221 | e.printStackTrace(); |
||
222 | } catch (InvocationTargetException e) { |
||
223 | e.printStackTrace(); |
||
224 | } catch (NoSuchMethodException e) { |
||
225 | e.printStackTrace(); |
||
226 | } |
||
227 | 7476 | azabala | }// for
|
228 | return fopen;
|
||
229 | } |
||
230 | |||
231 | /**
|
||
232 | * Adds to mapcontrol all the file based layers selected by user in
|
||
233 | * fileOpenDialog instance.
|
||
234 | 7537 | sbayarri | *
|
235 | 7476 | azabala | * @param mapControl
|
236 | * MapControl where we want to add the selected layers
|
||
237 | 7537 | sbayarri | *
|
238 | 7476 | azabala | * @param fileDlg
|
239 | * FileOpenDialog where user selected file based layers
|
||
240 | 7537 | sbayarri | *
|
241 | 7476 | azabala | * @return boolean flag to report sucess of the operation
|
242 | */
|
||
243 | private boolean loadFileLayers(MapControl mapControl, FileOpenDialog fileDlg) { |
||
244 | |||
245 | if (fileDlg.getFiles() == null) { |
||
246 | return false; |
||
247 | 2268 | fjp | } |
248 | 7476 | azabala | |
249 | FLayer lyr = null;
|
||
250 | IProjection proj = FOpenDialog.getLastProjection(); |
||
251 | File[] files = fileDlg.getFiles(); |
||
252 | String[] driverNames = fileDlg.getDriverNames(); |
||
253 | Driver[] drivers = new Driver[driverNames.length]; |
||
254 | |||
255 | //all catched errors will be saved here, to show user at the end
|
||
256 | //of the method
|
||
257 | ArrayList errors = new ArrayList(); |
||
258 | 7537 | sbayarri | |
259 | |||
260 | 7476 | azabala | // try to load the drivers referenced by the file dialog
|
261 | for (int i = 0; i < drivers.length; i++) { |
||
262 | try {
|
||
263 | drivers[i] = LayerFactory.getDM().getDriver(driverNames[i]); |
||
264 | } catch (DriverLoadException e) {
|
||
265 | DriverNotLoadedExceptionType type = new DriverNotLoadedExceptionType();
|
||
266 | type.setDriverName(driverNames[i]); |
||
267 | DriverException exception = new DriverException(e, type);
|
||
268 | errors.add(exception); |
||
269 | // NotificationManager.addError("No se pudo cargar el driver", e);
|
||
270 | } |
||
271 | } |
||
272 | 7537 | sbayarri | |
273 | |||
274 | 7476 | azabala | // Envelope de cada fichero seleccionado por el usuario
|
275 | Rectangle2D[] rects = new Rectangle2D[files.length]; |
||
276 | boolean first = false; |
||
277 | 2268 | fjp | |
278 | 7476 | azabala | // A?adir capas al mapControl se trata como una transaccion
|
279 | mapControl.getMapContext().beginAtomicEvent(); |
||
280 | 2268 | fjp | |
281 | 7476 | azabala | for (int iFile = 0; iFile < files.length; iFile++) { |
282 | File fich = files[iFile];
|
||
283 | String layerName = fich.getName();
|
||
284 | try {
|
||
285 | // FJP: Comento esto (if (fileDlg.accept(fich))) para resolver
|
||
286 | // el bug 75. ?Esto estaba antes
|
||
287 | // o lo apuesto alguien por algo en concreto?.
|
||
288 | // if (fileDlg.accept(fich))
|
||
289 | |||
290 | if (drivers[iFile] instanceof VectorialFileDriver) { |
||
291 | lyr = LayerFactory.createLayer(layerName, |
||
292 | (VectorialFileDriver) drivers[iFile], fich, proj); |
||
293 | } else if (drivers[iFile] instanceof RasterDriver) { |
||
294 | lyr = LayerFactory.createLayer(layerName, |
||
295 | (RasterDriver) drivers[iFile], fich, proj); |
||
296 | 2268 | fjp | } |
297 | |||
298 | 7476 | azabala | if (lyr != null) { |
299 | lyr.setVisible(true);
|
||
300 | if (mapControl.getMapContext().getViewPort().getExtent() == null) { |
||
301 | first = true;
|
||
302 | 2268 | fjp | } |
303 | 7476 | azabala | checkProjection(lyr, mapControl.getViewPort()); |
304 | mapControl.getMapContext().getLayers().addLayer(lyr); |
||
305 | 2268 | fjp | |
306 | 7476 | azabala | //esto ya se hace en layerfactory ?lo dejamos? (azabala)
|
307 | /*
|
||
308 | if (lyr instanceof FLyrVect) {
|
||
309 | FLyrVect lyrVect = (FLyrVect) lyr;
|
||
310 | if (drivers[iFile] instanceof WithDefaultLegend) {
|
||
311 | WithDefaultLegend aux = (WithDefaultLegend) drivers[iFile];
|
||
312 | lyrVect.setLegend((VectorialLegend) aux
|
||
313 | .getDefaultLegend());
|
||
314 | } else {
|
||
315 | lyrVect.setLegend(LegendFactory
|
||
316 | .createSingleSymbolLegend(lyrVect
|
||
317 | .getShapeType()));
|
||
318 | }
|
||
319 | }// if
|
||
320 | */
|
||
321 | rects[iFile] = lyr.getFullExtent(); |
||
322 | 2268 | fjp | |
323 | 7476 | azabala | // TODO: Poner una variable y dibujar solo cuando
|
324 | // todas las capas hayan sido cargadas.
|
||
325 | 7537 | sbayarri | |
326 | 7476 | azabala | // TODO Se deber? de redibujar mediante la captura de los
|
327 | // eventos, por
|
||
328 | // eso se comenta la parte anterior
|
||
329 | // theView.getMapControl().drawMap();
|
||
330 | // theView.getTOC().refresh();
|
||
331 | 7537 | sbayarri | |
332 | 7476 | azabala | }// if
|
333 | 2268 | fjp | |
334 | 7476 | azabala | } catch (DriverException e) {
|
335 | //This exception is produced in the layer.getFullExtent() call
|
||
336 | DriverIOExceptionType type = |
||
337 | new DriverIOExceptionType();
|
||
338 | type.setFile(fich); |
||
339 | DriverException newException = new DriverException(e, type);
|
||
340 | lyr.addError(newException); |
||
341 | lyr.setAvailable(false);
|
||
342 | // NotificationManager.addError("Error al crear la capa", e);
|
||
343 | 7537 | sbayarri | |
344 | 7476 | azabala | errors.add(newException); |
345 | 2268 | fjp | |
346 | 7476 | azabala | } |
347 | 7537 | sbayarri | |
348 | |||
349 | /* si no seteamos la leyenda, pq ya se hace en el factory, esto quitarlo (azabala)
|
||
350 | 7476 | azabala | catch (FieldNotFoundException e) {
|
351 | // Esta no puede saltar
|
||
352 | 7537 | sbayarri | }
|
353 | */
|
||
354 | |||
355 | |||
356 | 7476 | azabala | catch (Exception e) { |
357 | //azabala: usamos el notificationManager?? a mi no me gusta demasiado
|
||
358 | //NotificationManager.addError("Error al crear la capa", e);
|
||
359 | //we catch any possible error
|
||
360 | GenericDriverExceptionType type = new GenericDriverExceptionType();
|
||
361 | DriverException exception = new DriverException(e, type);
|
||
362 | lyr.addError(exception); |
||
363 | lyr.setAvailable(false);
|
||
364 | 7537 | sbayarri | |
365 | 7476 | azabala | errors.add(exception); |
366 | } |
||
367 | }// for
|
||
368 | 2268 | fjp | |
369 | 7537 | sbayarri | |
370 | |||
371 | 7476 | azabala | //now, if it is the first time, we put the extent of the mapcontrol
|
372 | //to the global extent of all the loaded layers.
|
||
373 | if (rects.length > 1) { |
||
374 | Rectangle2D rect = new Rectangle2D.Double(); |
||
375 | rect.setRect(rects[0]);
|
||
376 | 2268 | fjp | |
377 | 7476 | azabala | //first is true only if the extent of the mapcontrol was null
|
378 | if (first) {
|
||
379 | for (int i = 0; i < rects.length; i++) { |
||
380 | if (rects[i] != null) { |
||
381 | rect.add(rects[i]); |
||
382 | 2268 | fjp | } |
383 | 7476 | azabala | }//for
|
384 | mapControl.getMapContext().getViewPort().setExtent(rect); |
||
385 | }// if first
|
||
386 | 7537 | sbayarri | }// if
|
387 | 7476 | azabala | mapControl.getMapContext().endAtomicEvent(); |
388 | 7537 | sbayarri | |
389 | 7476 | azabala | return true; |
390 | } |
||
391 | 7537 | sbayarri | |
392 | |||
393 | 7476 | azabala | /**
|
394 | * Adds to mapcontrol all layers selected by user in the specified WizardPanel.
|
||
395 | 7537 | sbayarri | *
|
396 | * @param mapControl
|
||
397 | 7476 | azabala | * MapControl on which we want to load user selected layers.
|
398 | 7537 | sbayarri | * @param wizardPanel
|
399 | 7476 | azabala | * WizardPanel where user selected the layers to load
|
400 | * @return
|
||
401 | */
|
||
402 | private boolean loadGenericWizardPanelLayers(MapControl mapControl, WizardPanel wp) { |
||
403 | FLayer lyr = null;
|
||
404 | wp.setMapCtrl(mapControl); |
||
405 | wp.execute(); |
||
406 | lyr = wp.getLayer(); |
||
407 | 7537 | sbayarri | |
408 | 9026 | jorpiell | if((lyr != null) && !(lyr.isOk())){ |
409 | 7476 | azabala | //if the layer is not okay (it has errors) process them
|
410 | processErrorsOfLayer(lyr, mapControl); |
||
411 | } |
||
412 | 7537 | sbayarri | |
413 | 7476 | azabala | if (lyr != null) { |
414 | lyr.setVisible(true);
|
||
415 | mapControl.getMapContext().beginAtomicEvent(); |
||
416 | checkProjection(lyr, mapControl.getViewPort()); |
||
417 | mapControl.getMapContext().getLayers().addLayer(lyr); |
||
418 | mapControl.getMapContext().endAtomicEvent(); |
||
419 | return true; |
||
420 | } |
||
421 | return false; |
||
422 | } |
||
423 | 7537 | sbayarri | |
424 | 7476 | azabala | /**
|
425 | * This method process the errors found in a layer
|
||
426 | * @param lyr
|
||
427 | * @param mapControl
|
||
428 | */
|
||
429 | 7537 | sbayarri | |
430 | 7476 | azabala | private void processErrorsOfLayer(FLayer lyr, MapControl mapControl){ |
431 | List errors = lyr.getErrors();
|
||
432 | // wp.callError(null);
|
||
433 | mapControl.getMapContext().callNewErrorEvent(null);
|
||
434 | } |
||
435 | /**
|
||
436 | * Abre dialogo para a?adir capas y las a?ade en mapControl
|
||
437 | 7537 | sbayarri | *
|
438 | 7476 | azabala | * Devuelve true si se han a?adido capas.
|
439 | */
|
||
440 | public boolean addLayers(MapControl mapControl) { |
||
441 | 7537 | sbayarri | |
442 | |||
443 | |||
444 | 7476 | azabala | // create and show the modal fopen dialog
|
445 | fopen = createFOpenDialog(); |
||
446 | PluginServices.getMDIManager().addWindow(fopen); |
||
447 | 2268 | fjp | |
448 | 7476 | azabala | if (fopen.isAccepted()) {
|
449 | if (fopen.getSelectedTab() instanceof FileOpenDialog) { |
||
450 | FileOpenDialog fileDlg = (FileOpenDialog) fopen |
||
451 | .getSelectedTab(); |
||
452 | 8790 | jmvivo | return loadFileLayers(mapControl, fileDlg);
|
453 | 2268 | fjp | |
454 | } else if (fopen.getSelectedTab() instanceof WizardPanel) { |
||
455 | WizardPanel wp = (WizardPanel) fopen.getSelectedTab(); |
||
456 | 8790 | jmvivo | return loadGenericWizardPanelLayers(mapControl, wp);
|
457 | 2268 | fjp | } else {
|
458 | 7476 | azabala | JOptionPane.showMessageDialog((Component) PluginServices |
459 | .getMainFrame(), "Ninguna capa seleccionada");
|
||
460 | 2268 | fjp | } |
461 | } |
||
462 | 5190 | jmvivo | return false; |
463 | 2268 | fjp | } |
464 | } |