root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / AddLayer.java @ 6537
History | View | Annotate | Download (11.5 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 |
|
49 |
import javax.swing.JOptionPane; |
50 |
|
51 |
import org.cresques.cts.ICoordTrans; |
52 |
import org.cresques.cts.IProjection; |
53 |
|
54 |
import com.hardcode.driverManager.Driver; |
55 |
import com.hardcode.driverManager.DriverLoadException; |
56 |
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException; |
57 |
import com.iver.andami.PluginServices; |
58 |
import com.iver.andami.messages.NotificationManager; |
59 |
import com.iver.andami.plugins.Extension; |
60 |
import com.iver.cit.gvsig.fmap.DriverException; |
61 |
import com.iver.cit.gvsig.fmap.MapControl; |
62 |
import com.iver.cit.gvsig.fmap.ViewPort; |
63 |
import com.iver.cit.gvsig.fmap.drivers.RasterDriver; |
64 |
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver; |
65 |
import com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend; |
66 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
67 |
import com.iver.cit.gvsig.fmap.layers.FLyrDefault; |
68 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
69 |
import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
70 |
import com.iver.cit.gvsig.fmap.rendering.LegendFactory; |
71 |
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend; |
72 |
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 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) 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.IExtension#isVisible()
|
111 |
*/
|
112 |
public boolean isVisible() { |
113 |
com.iver.andami.ui.mdiManager.View view = PluginServices.getMDIManager() |
114 |
.getActiveView(); |
115 |
|
116 |
if (view == null) { |
117 |
return false; |
118 |
} |
119 |
|
120 |
return (view instanceof View); |
121 |
} |
122 |
|
123 |
/**
|
124 |
* @see com.iver.andami.plugins.IExtension#initialize()
|
125 |
*/
|
126 |
public void initialize() { |
127 |
} |
128 |
|
129 |
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 = proj.getCT(viewPort.getProjection()); |
153 |
lyrVect.setCoordTrans(ct); |
154 |
System.err.println("coordTrans = " + |
155 |
proj.getAbrev() + " " +
|
156 |
viewPort.getProjection().getAbrev()); |
157 |
} |
158 |
} |
159 |
} |
160 |
|
161 |
} |
162 |
/**
|
163 |
* @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
|
164 |
*/
|
165 |
public void execute(String actionCommand) { |
166 |
//Project project = ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).getProject();
|
167 |
View theView = (View) PluginServices.getMDIManager().getActiveView(); |
168 |
|
169 |
this.addLayers(theView.getMapControl());
|
170 |
return;
|
171 |
|
172 |
} |
173 |
|
174 |
/**
|
175 |
* @see com.iver.andami.plugins.IExtension#isEnabled()
|
176 |
*/
|
177 |
public boolean isEnabled() { |
178 |
return true; |
179 |
} |
180 |
|
181 |
|
182 |
|
183 |
/**
|
184 |
* Abre dialogo para a?adir capas y las a?ade
|
185 |
* en mapControl
|
186 |
*
|
187 |
* Devuelve true si se han a?adido capas.
|
188 |
*/
|
189 |
public boolean addLayers(MapControl mapControl) { |
190 |
fopen = new FOpenDialog();
|
191 |
|
192 |
FileOpenDialog fileDlg = new FileOpenDialog(new Class[] { |
193 |
VectorialFileDriver.class, RasterDriver.class |
194 |
}); |
195 |
fopen.addTab(PluginServices.getText(this, "Fichero"), fileDlg); |
196 |
for (int i=0; i<wizardStack.size(); i++) { |
197 |
WizardPanel wp; |
198 |
try {
|
199 |
wp = AddLayer.getInstance(i); |
200 |
fopen.addWizardTab(wp.getTabName(), wp); |
201 |
} catch (IllegalArgumentException e) { |
202 |
e.printStackTrace(); |
203 |
} catch (SecurityException e) { |
204 |
e.printStackTrace(); |
205 |
} catch (InstantiationException e) { |
206 |
e.printStackTrace(); |
207 |
} catch (IllegalAccessException e) { |
208 |
e.printStackTrace(); |
209 |
} catch (InvocationTargetException e) { |
210 |
e.printStackTrace(); |
211 |
} catch (NoSuchMethodException e) { |
212 |
e.printStackTrace(); |
213 |
} |
214 |
} |
215 |
|
216 |
PluginServices.getMDIManager().addView(fopen); |
217 |
|
218 |
if (fopen.isAccepted()) {
|
219 |
FLayer lyr = null;
|
220 |
|
221 |
if (fopen.getSelectedTab() == fileDlg) {
|
222 |
if (fileDlg.getFiles() == null) { |
223 |
return false; |
224 |
} |
225 |
|
226 |
IProjection proj = FOpenDialog.getLastProjection(); |
227 |
File[] files = fileDlg.getFiles(); |
228 |
String[] driverNames = fileDlg.getDriverNames(); |
229 |
Driver[] drivers = new Driver[driverNames.length]; |
230 |
for (int i = 0; i < drivers.length; i++) { |
231 |
try {
|
232 |
drivers[i] = LayerFactory.getDM().getDriver(driverNames[i]); |
233 |
} catch (DriverLoadException e) {
|
234 |
NotificationManager.addError("No se pudo cargar el driver", e);
|
235 |
} |
236 |
} |
237 |
Rectangle2D[] rects=new Rectangle2D[files.length]; |
238 |
boolean first=false; |
239 |
mapControl.getMapContext() |
240 |
.beginAtomicEvent(); |
241 |
|
242 |
for (int iFile = 0; iFile < files.length; iFile++) { |
243 |
File fich = files[iFile];
|
244 |
String layerName = fich.getName();
|
245 |
//String layerPath = fich.getAbsolutePath();
|
246 |
|
247 |
try {
|
248 |
// FJP: Comento esto (if (fileDlg.accept(fich))) para resolver el bug 75. ?Esto estaba antes
|
249 |
// o lo apuesto alguien por algo en concreto?.
|
250 |
// if (fileDlg.accept(fich))
|
251 |
if (drivers[iFile] instanceof VectorialFileDriver){ |
252 |
lyr = LayerFactory.createLayer(layerName, |
253 |
(VectorialFileDriver) drivers[iFile], fich, proj); |
254 |
}else if (drivers[iFile] instanceof RasterDriver){ |
255 |
lyr = LayerFactory.createLayer(layerName, |
256 |
(RasterDriver) drivers[iFile], fich, proj); |
257 |
} |
258 |
|
259 |
if (lyr != null) { |
260 |
lyr.setVisible(true);
|
261 |
if (mapControl.getMapContext().getViewPort().getExtent()==null){ |
262 |
first=true;
|
263 |
} |
264 |
|
265 |
|
266 |
|
267 |
try {
|
268 |
|
269 |
// Le asignamos tambi?n una legenda por defecto acorde con
|
270 |
// el tipo de shape que tenga. Tampoco s? si es aqu? el
|
271 |
// sitio adecuado, pero en fin....
|
272 |
checkProjection(lyr, mapControl.getViewPort()); |
273 |
mapControl.getMapContext().getLayers() |
274 |
.addLayer(lyr); |
275 |
|
276 |
if (lyr instanceof FLyrVect) |
277 |
{ |
278 |
FLyrVect lyrVect = (FLyrVect) lyr; |
279 |
if (drivers[iFile] instanceof WithDefaultLegend) { |
280 |
WithDefaultLegend aux = (WithDefaultLegend) drivers[iFile]; |
281 |
lyrVect.setLegend((VectorialLegend) aux.getDefaultLegend()); |
282 |
} else {
|
283 |
lyrVect.setLegend(LegendFactory.createSingleSymbolLegend( |
284 |
lyrVect.getShapeType())); |
285 |
} |
286 |
} |
287 |
} catch (FieldNotFoundException e) {
|
288 |
//Esta no puede saltar
|
289 |
} |
290 |
rects[iFile]=lyr.getFullExtent(); |
291 |
|
292 |
// TODO: Poner una variable y dibujar solo cuando
|
293 |
// todas las capas hayan sido cargadas.
|
294 |
// TODO Se deber? de redibujar mediante la captura de los eventos, por
|
295 |
//eso se comenta la parte anterior
|
296 |
// theView.getMapControl().drawMap();
|
297 |
// theView.getTOC().refresh();
|
298 |
} |
299 |
|
300 |
} catch (DriverException e) {
|
301 |
lyr.setAvailable(false);
|
302 |
NotificationManager.addError("Error al crear la capa", e);
|
303 |
|
304 |
} catch (Exception e) { |
305 |
NotificationManager.addError("Error al crear la capa", e);
|
306 |
lyr.setAvailable(false);
|
307 |
} |
308 |
} |
309 |
|
310 |
//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.
|
311 |
if (rects.length > 1) { |
312 |
Rectangle2D rect = new Rectangle2D.Double(); |
313 |
rect.setRect(rects[0]);
|
314 |
|
315 |
if (first) {
|
316 |
for (int i = 0; i < rects.length; i++) { |
317 |
if (rects[i] != null) { |
318 |
rect.add(rects[i]); |
319 |
} |
320 |
} |
321 |
|
322 |
mapControl.getMapContext().getViewPort() |
323 |
.setExtent(rect); |
324 |
} |
325 |
} |
326 |
mapControl.getMapContext() |
327 |
.endAtomicEvent(); |
328 |
return true; |
329 |
|
330 |
} else if (fopen.getSelectedTab() instanceof WizardPanel) { |
331 |
WizardPanel wp = (WizardPanel) fopen.getSelectedTab(); |
332 |
wp.setMapCtrl(mapControl); |
333 |
wp.execute(); |
334 |
lyr = wp.getLayer(); |
335 |
|
336 |
if (lyr != null) { |
337 |
lyr.setVisible(true);
|
338 |
mapControl.getMapContext().beginAtomicEvent(); |
339 |
checkProjection(lyr, mapControl.getViewPort()); |
340 |
mapControl.getMapContext().getLayers() |
341 |
.addLayer(lyr); |
342 |
mapControl.getMapContext().endAtomicEvent(); |
343 |
return true; |
344 |
|
345 |
} |
346 |
} else {
|
347 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),"Ninguna capa seleccionada"); |
348 |
} |
349 |
} |
350 |
return false; |
351 |
} |
352 |
} |