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