root / tags / v10_RC2c / applications / appgvSIG / src / com / iver / cit / gvsig / AddLayer.java @ 8745
History | View | Annotate | Download (11.3 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.iver.andami.PluginServices; |
57 |
import com.iver.andami.messages.NotificationManager; |
58 |
import com.iver.andami.plugins.Extension; |
59 |
import com.iver.cit.gvsig.fmap.DriverException; |
60 |
import com.iver.cit.gvsig.fmap.MapControl; |
61 |
import com.iver.cit.gvsig.fmap.ViewPort; |
62 |
import com.iver.cit.gvsig.fmap.drivers.RasterDriver; |
63 |
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver; |
64 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
65 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
66 |
import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
67 |
import com.iver.cit.gvsig.gui.FOpenDialog; |
68 |
import com.iver.cit.gvsig.gui.FileOpenDialog; |
69 |
import com.iver.cit.gvsig.gui.View; |
70 |
import com.iver.cit.gvsig.gui.WizardPanel; |
71 |
|
72 |
|
73 |
/**
|
74 |
* Extensi?n que abre un di?logo para seleccionar la capa o capas que se
|
75 |
* quieren a?adir a la vista.
|
76 |
*
|
77 |
* @author Fernando Gonz?lez Cort?s
|
78 |
*/
|
79 |
public class AddLayer extends Extension { |
80 |
public FOpenDialog fopen = null; |
81 |
|
82 |
private static ArrayList wizardStack = null; |
83 |
|
84 |
static {
|
85 |
AddLayer.wizardStack = new ArrayList(); |
86 |
} |
87 |
|
88 |
public static void addWizard(Class wpClass) { |
89 |
AddLayer.wizardStack.add(wpClass); |
90 |
} |
91 |
|
92 |
public static WizardPanel getInstance(int i) throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { |
93 |
Class wpClass = (Class) AddLayer.wizardStack.get(i); |
94 |
Class [] args = {}; |
95 |
Object [] params = {}; |
96 |
WizardPanel wp = (WizardPanel) wpClass.getConstructor(args).newInstance(params); |
97 |
|
98 |
wp.initWizard(); |
99 |
|
100 |
return wp;
|
101 |
} |
102 |
|
103 |
|
104 |
/**
|
105 |
* @see com.iver.mdiApp.plugins.IExtension#isVisible()
|
106 |
*/
|
107 |
public boolean isVisible() { |
108 |
com.iver.andami.ui.mdiManager.IWindow view = PluginServices.getMDIManager() |
109 |
.getActiveWindow(); |
110 |
|
111 |
if (view == null) { |
112 |
return false; |
113 |
} |
114 |
|
115 |
return (view instanceof View); |
116 |
} |
117 |
|
118 |
/**
|
119 |
* @see com.iver.andami.plugins.IExtension#initialize()
|
120 |
*/
|
121 |
public void initialize() { |
122 |
} |
123 |
|
124 |
private void checkProjection(FLayer lyr, ViewPort viewPort) |
125 |
{ |
126 |
if (lyr instanceof FLyrVect) |
127 |
{ |
128 |
FLyrVect lyrVect = (FLyrVect) lyr; |
129 |
IProjection proj = lyr.getProjection(); |
130 |
// Comprobar que la projecci?n es la misma que la vista
|
131 |
if (proj == null) |
132 |
{ |
133 |
// SUPONEMOS que la capa est? en la proyecci?n que
|
134 |
// estamos pidiendo (que ya es mucho suponer, ya).
|
135 |
lyrVect.setProjection(viewPort.getProjection()); |
136 |
return;
|
137 |
} |
138 |
if (proj != viewPort.getProjection()) {
|
139 |
int option = JOptionPane.showConfirmDialog(null, |
140 |
PluginServices.getText(this, "reproyectar_aviso"), |
141 |
PluginServices.getText(this, "reproyectar_pregunta"), |
142 |
JOptionPane.YES_NO_OPTION);
|
143 |
|
144 |
if (option == JOptionPane.NO_OPTION) { |
145 |
return;
|
146 |
} else {
|
147 |
ICoordTrans ct = proj.getCT(viewPort.getProjection()); |
148 |
lyrVect.setCoordTrans(ct); |
149 |
System.err.println("coordTrans = " + |
150 |
proj.getAbrev() + " " +
|
151 |
viewPort.getProjection().getAbrev()); |
152 |
} |
153 |
} |
154 |
} |
155 |
|
156 |
} |
157 |
/**
|
158 |
* @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
|
159 |
*/
|
160 |
public void execute(String actionCommand) { |
161 |
//Project project = ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).getProject();
|
162 |
View theView = (View) PluginServices.getMDIManager().getActiveWindow(); |
163 |
|
164 |
this.addLayers(theView.getMapControl());
|
165 |
return;
|
166 |
|
167 |
} |
168 |
|
169 |
/**
|
170 |
* @see com.iver.andami.plugins.IExtension#isEnabled()
|
171 |
*/
|
172 |
public boolean isEnabled() { |
173 |
return true; |
174 |
} |
175 |
|
176 |
|
177 |
|
178 |
/**
|
179 |
* Abre dialogo para a?adir capas y las a?ade
|
180 |
* en mapControl
|
181 |
*
|
182 |
* Devuelve true si se han a?adido capas.
|
183 |
*/
|
184 |
public boolean addLayers(MapControl mapControl) { |
185 |
fopen = new FOpenDialog();
|
186 |
|
187 |
FileOpenDialog fileDlg = new FileOpenDialog(new Class[] { |
188 |
VectorialFileDriver.class, RasterDriver.class |
189 |
}); |
190 |
fopen.addTab(PluginServices.getText(this, "Fichero"), fileDlg); |
191 |
for (int i=0; i<wizardStack.size(); i++) { |
192 |
WizardPanel wp; |
193 |
try {
|
194 |
wp = AddLayer.getInstance(i); |
195 |
fopen.addWizardTab(wp.getTabName(), wp); |
196 |
} catch (IllegalArgumentException e) { |
197 |
e.printStackTrace(); |
198 |
} catch (SecurityException e) { |
199 |
e.printStackTrace(); |
200 |
} catch (InstantiationException e) { |
201 |
e.printStackTrace(); |
202 |
} catch (IllegalAccessException e) { |
203 |
e.printStackTrace(); |
204 |
} catch (InvocationTargetException e) { |
205 |
e.printStackTrace(); |
206 |
} catch (NoSuchMethodException e) { |
207 |
e.printStackTrace(); |
208 |
} |
209 |
} |
210 |
|
211 |
PluginServices.getMDIManager().addWindow(fopen); |
212 |
|
213 |
if (fopen.isAccepted()) {
|
214 |
FLayer lyr = null;
|
215 |
|
216 |
if (fopen.getSelectedTab() == fileDlg) {
|
217 |
if (fileDlg.getFiles() == null) { |
218 |
return false; |
219 |
} |
220 |
|
221 |
IProjection proj = FOpenDialog.getLastProjection(); |
222 |
File[] files = fileDlg.getFiles(); |
223 |
String[] driverNames = fileDlg.getDriverNames(); |
224 |
Driver[] drivers = new Driver[driverNames.length]; |
225 |
for (int i = 0; i < drivers.length; i++) { |
226 |
try {
|
227 |
drivers[i] = LayerFactory.getDM().getDriver(driverNames[i]); |
228 |
} catch (DriverLoadException e) {
|
229 |
NotificationManager.addError("No se pudo cargar el driver", e);
|
230 |
} |
231 |
} |
232 |
Rectangle2D[] rects=new Rectangle2D[files.length]; |
233 |
boolean first=false; |
234 |
mapControl.getMapContext() |
235 |
.beginAtomicEvent(); |
236 |
|
237 |
for (int iFile = 0; iFile < files.length; iFile++) { |
238 |
File fich = files[iFile];
|
239 |
String layerName = fich.getName();
|
240 |
//String layerPath = fich.getAbsolutePath();
|
241 |
|
242 |
try {
|
243 |
// FJP: Comento esto (if (fileDlg.accept(fich))) para resolver el bug 75. ?Esto estaba antes
|
244 |
// o lo apuesto alguien por algo en concreto?.
|
245 |
// if (fileDlg.accept(fich))
|
246 |
if (drivers[iFile] instanceof VectorialFileDriver){ |
247 |
lyr = LayerFactory.createLayer(layerName, |
248 |
(VectorialFileDriver) drivers[iFile], fich, proj); |
249 |
}else if (drivers[iFile] instanceof RasterDriver){ |
250 |
lyr = LayerFactory.createLayer(layerName, |
251 |
(RasterDriver) drivers[iFile], fich, proj); |
252 |
} |
253 |
|
254 |
if (lyr != null) { |
255 |
lyr.setVisible(true);
|
256 |
if (mapControl.getMapContext().getViewPort().getExtent()==null){ |
257 |
first=true;
|
258 |
} |
259 |
|
260 |
|
261 |
|
262 |
// try {
|
263 |
|
264 |
// Le asignamos tambi?n una legenda por defecto acorde con
|
265 |
// el tipo de shape que tenga. Tampoco s? si es aqu? el
|
266 |
// sitio adecuado, pero en fin....
|
267 |
checkProjection(lyr, mapControl.getViewPort()); |
268 |
mapControl.getMapContext().getLayers() |
269 |
.addLayer(lyr); |
270 |
//azabala. lo comento pq esto ya se hace en LayerFactory
|
271 |
// if (lyr instanceof FLyrVect)
|
272 |
// {
|
273 |
// FLyrVect lyrVect = (FLyrVect) lyr;
|
274 |
// if (drivers[iFile] instanceof WithDefaultLegend) {
|
275 |
// WithDefaultLegend aux = (WithDefaultLegend) drivers[iFile];
|
276 |
// lyrVect.setLegend((VectorialLegend) aux.getDefaultLegend());
|
277 |
// } else {
|
278 |
// lyrVect.setLegend(LegendFactory.createSingleSymbolLegend(
|
279 |
// lyrVect.getShapeType()));
|
280 |
// }
|
281 |
// }
|
282 |
// } catch (FieldNotFoundException e) {
|
283 |
// //Esta no puede saltar
|
284 |
// }
|
285 |
rects[iFile]=lyr.getFullExtent(); |
286 |
|
287 |
// TODO: Poner una variable y dibujar solo cuando
|
288 |
// todas las capas hayan sido cargadas.
|
289 |
// TODO Se deber? de redibujar mediante la captura de los eventos, por
|
290 |
//eso se comenta la parte anterior
|
291 |
// theView.getMapControl().drawMap();
|
292 |
// theView.getTOC().refresh();
|
293 |
} |
294 |
|
295 |
} catch (DriverException e) {
|
296 |
lyr.setAvailable(false);
|
297 |
NotificationManager.addError("Error al crear la capa", e);
|
298 |
|
299 |
} catch (Exception e) { |
300 |
NotificationManager.addError("Error al crear la capa", e);
|
301 |
lyr.setAvailable(false);
|
302 |
} |
303 |
} |
304 |
|
305 |
//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.
|
306 |
if (rects.length > 1) { |
307 |
Rectangle2D rect = new Rectangle2D.Double(); |
308 |
rect.setRect(rects[0]);
|
309 |
|
310 |
if (first) {
|
311 |
for (int i = 0; i < rects.length; i++) { |
312 |
if (rects[i] != null) { |
313 |
rect.add(rects[i]); |
314 |
} |
315 |
} |
316 |
|
317 |
mapControl.getMapContext().getViewPort() |
318 |
.setExtent(rect); |
319 |
} |
320 |
} |
321 |
mapControl.getMapContext() |
322 |
.endAtomicEvent(); |
323 |
return true; |
324 |
|
325 |
} else if (fopen.getSelectedTab() instanceof WizardPanel) { |
326 |
WizardPanel wp = (WizardPanel) fopen.getSelectedTab(); |
327 |
wp.setMapCtrl(mapControl); |
328 |
wp.execute(); |
329 |
lyr = wp.getLayer(); |
330 |
|
331 |
if (lyr != null) { |
332 |
lyr.setVisible(true);
|
333 |
mapControl.getMapContext().beginAtomicEvent(); |
334 |
checkProjection(lyr, mapControl.getViewPort()); |
335 |
mapControl.getMapContext().getLayers() |
336 |
.addLayer(lyr); |
337 |
mapControl.getMapContext().endAtomicEvent(); |
338 |
return true; |
339 |
|
340 |
} |
341 |
} else {
|
342 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),"Ninguna capa seleccionada"); |
343 |
} |
344 |
} |
345 |
return false; |
346 |
} |
347 |
} |