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