svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.i18n / src / test / data / src / appgvSIG / com / iver / cit / gvsig / AddLayer.java @ 40559
History | View | Annotate | Download (11.1 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package com.iver.cit.gvsig; |
25 |
|
26 |
import java.awt.Component; |
27 |
import java.awt.geom.Rectangle2D; |
28 |
import java.io.File; |
29 |
import java.lang.reflect.InvocationTargetException; |
30 |
import java.util.ArrayList; |
31 |
|
32 |
import javax.swing.JOptionPane; |
33 |
|
34 |
import org.cresques.cts.ICoordTrans; |
35 |
import org.cresques.cts.IProjection; |
36 |
import org.cresques.cts.gt2.CoordSys; |
37 |
import org.cresques.cts.gt2.CoordTrans; |
38 |
import org.opengis.layer.Layer; |
39 |
|
40 |
import com.hardcode.driverManager.Driver; |
41 |
import com.hardcode.driverManager.DriverLoadException; |
42 |
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException; |
43 |
import com.iver.andami.PluginServices; |
44 |
import com.iver.andami.messages.NotificationManager; |
45 |
import com.iver.andami.plugins.Extension; |
46 |
import com.iver.cit.gvsig.fmap.DriverException; |
47 |
import com.iver.cit.gvsig.fmap.MapControl; |
48 |
import com.iver.cit.gvsig.fmap.ViewPort; |
49 |
import com.iver.cit.gvsig.fmap.drivers.RasterDriver; |
50 |
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver; |
51 |
import com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend; |
52 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
53 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
54 |
import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
55 |
import com.iver.cit.gvsig.fmap.rendering.LegendFactory; |
56 |
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend; |
57 |
import com.iver.cit.gvsig.gui.FOpenDialog; |
58 |
import com.iver.cit.gvsig.gui.FileOpenDialog; |
59 |
import com.iver.cit.gvsig.gui.View; |
60 |
import com.iver.cit.gvsig.gui.WizardPanel; |
61 |
|
62 |
|
63 |
/**
|
64 |
* Extensi?n que abre un di?logo para seleccionar la capa o capas que se
|
65 |
* quieren a?adir a la vista.
|
66 |
*
|
67 |
* @author Fernando Gonz?lez Cort?s
|
68 |
*/
|
69 |
public class AddLayer extends Extension { |
70 |
public FOpenDialog fopen = null; |
71 |
|
72 |
private static ArrayList wizardStack = null; |
73 |
|
74 |
static {
|
75 |
AddLayer.wizardStack = new ArrayList(); |
76 |
} |
77 |
|
78 |
public static void addWizard(Class wpClass) { |
79 |
AddLayer.wizardStack.add(wpClass); |
80 |
} |
81 |
|
82 |
public static WizardPanel getInstance(int i) throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { |
83 |
Class wpClass = (Class) AddLayer.wizardStack.get(i); |
84 |
Class [] args = {}; |
85 |
Object [] params = {}; |
86 |
WizardPanel wp = (WizardPanel) wpClass.getConstructor(args).newInstance(params); |
87 |
|
88 |
wp.initWizard(); |
89 |
|
90 |
return wp;
|
91 |
} |
92 |
|
93 |
|
94 |
/**
|
95 |
* @see com.iver.mdiApp.plugins.IExtension#isVisible()
|
96 |
*/
|
97 |
public boolean isVisible() { |
98 |
com.iver.andami.ui.mdiManager.View f = PluginServices.getMDIManager() |
99 |
.getActiveView(); |
100 |
|
101 |
if (f == null) { |
102 |
return false; |
103 |
} |
104 |
|
105 |
return (f.getClass() == View.class); |
106 |
} |
107 |
|
108 |
/**
|
109 |
* @see com.iver.andami.plugins.IExtension#initialize()
|
110 |
*/
|
111 |
public void initialize() { |
112 |
} |
113 |
|
114 |
private void checkProjection(FLayer lyr, ViewPort viewPort) |
115 |
{ |
116 |
if (lyr instanceof FLyrVect) |
117 |
{ |
118 |
FLyrVect lyrVect = (FLyrVect) lyr; |
119 |
IProjection proj = lyr.getProjection(); |
120 |
// Comprobar que la projecci?n es la misma que la vista
|
121 |
if (proj == null) |
122 |
{ |
123 |
// SUPONEMOS que la capa est? en la proyecci?n que
|
124 |
// estamos pidiendo (que ya es mucho suponer, ya).
|
125 |
lyrVect.setProjection(viewPort.getProjection()); |
126 |
return;
|
127 |
} |
128 |
if (proj != viewPort.getProjection()) {
|
129 |
int option = JOptionPane.showConfirmDialog(null, |
130 |
PluginServices.getText(this, "reproyectar_aviso"), |
131 |
PluginServices.getText(this, "reproyectar_pregunta"), |
132 |
JOptionPane.YES_NO_OPTION);
|
133 |
|
134 |
if (option == JOptionPane.NO_OPTION) { |
135 |
return;
|
136 |
} else {
|
137 |
ICoordTrans ct = new CoordTrans((CoordSys) proj,
|
138 |
(CoordSys) viewPort.getProjection()); |
139 |
lyrVect.setCoordTrans(ct); |
140 |
System.err.println("coordTrans = " + |
141 |
proj.getAbrev() + " " +
|
142 |
viewPort.getProjection().getAbrev()); |
143 |
} |
144 |
} |
145 |
} |
146 |
|
147 |
} |
148 |
/**
|
149 |
* @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
|
150 |
*/
|
151 |
public void execute(String actionCommand) { |
152 |
//Project project = ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).getProject();
|
153 |
View theView = (View) PluginServices.getMDIManager().getActiveView(); |
154 |
|
155 |
this.addLayers(theView.getMapControl());
|
156 |
return;
|
157 |
|
158 |
} |
159 |
|
160 |
/**
|
161 |
* @see com.iver.andami.plugins.IExtension#isEnabled()
|
162 |
*/
|
163 |
public boolean isEnabled() { |
164 |
return true; |
165 |
} |
166 |
|
167 |
|
168 |
|
169 |
/**
|
170 |
* Abre dialogo para a?adir capas y las a?ade
|
171 |
* en mapControl
|
172 |
*
|
173 |
* Devuelve true si se han a?adido capas.
|
174 |
*/
|
175 |
public boolean addLayers(MapControl mapControl) { |
176 |
fopen = new FOpenDialog();
|
177 |
|
178 |
FileOpenDialog fileDlg = new FileOpenDialog(new Class[] { |
179 |
VectorialFileDriver.class, RasterDriver.class |
180 |
}); |
181 |
fopen.addTab(PluginServices.getText(this, "Fichero"), fileDlg); |
182 |
for (int i=0; i<wizardStack.size(); i++) { |
183 |
WizardPanel wp; |
184 |
try {
|
185 |
wp = AddLayer.getInstance(i); |
186 |
fopen.addWizardTab(wp.getTabName(), wp); |
187 |
} catch (IllegalArgumentException e) { |
188 |
e.printStackTrace(); |
189 |
} catch (SecurityException e) { |
190 |
e.printStackTrace(); |
191 |
} catch (InstantiationException e) { |
192 |
e.printStackTrace(); |
193 |
} catch (IllegalAccessException e) { |
194 |
e.printStackTrace(); |
195 |
} catch (InvocationTargetException e) { |
196 |
e.printStackTrace(); |
197 |
} catch (NoSuchMethodException e) { |
198 |
e.printStackTrace(); |
199 |
} |
200 |
} |
201 |
|
202 |
PluginServices.getMDIManager().addView(fopen); |
203 |
|
204 |
if (fopen.isAccepted()) {
|
205 |
FLayer lyr = null;
|
206 |
|
207 |
if (fopen.getSelectedTab() == fileDlg) {
|
208 |
if (fileDlg.getFiles() == null) { |
209 |
return false; |
210 |
} |
211 |
|
212 |
IProjection proj = FOpenDialog.getLastProjection(); |
213 |
File[] files = fileDlg.getFiles(); |
214 |
String[] driverNames = fileDlg.getDriverNames(); |
215 |
Driver[] drivers = new Driver[driverNames.length]; |
216 |
for (int i = 0; i < drivers.length; i++) { |
217 |
try {
|
218 |
drivers[i] = LayerFactory.getDM().getDriver(driverNames[i]); |
219 |
} catch (DriverLoadException e) {
|
220 |
NotificationManager.addError("No se pudo cargar el driver", e);
|
221 |
} |
222 |
} |
223 |
Rectangle2D[] rects=new Rectangle2D[files.length]; |
224 |
boolean first=false; |
225 |
mapControl.getMapContext() |
226 |
.beginAtomicEvent(); |
227 |
|
228 |
for (int iFile = 0; iFile < files.length; iFile++) { |
229 |
File fich = files[iFile];
|
230 |
String layerName = fich.getName();
|
231 |
//String layerPath = fich.getAbsolutePath();
|
232 |
|
233 |
try {
|
234 |
// FJP: Comento esto (if (fileDlg.accept(fich))) para resolver el bug 75. ?Esto estaba antes
|
235 |
// o lo apuesto alguien por algo en concreto?.
|
236 |
// if (fileDlg.accept(fich))
|
237 |
if (drivers[iFile] instanceof VectorialFileDriver){ |
238 |
lyr = LayerFactory.createLayer(layerName, |
239 |
(VectorialFileDriver) drivers[iFile], fich, proj); |
240 |
}else if (drivers[iFile] instanceof RasterDriver){ |
241 |
lyr = LayerFactory.createLayer(layerName, |
242 |
(RasterDriver) drivers[iFile], fich, proj); |
243 |
} |
244 |
|
245 |
if (lyr != null) { |
246 |
lyr.setVisible(true);
|
247 |
if (mapControl.getMapContext().getViewPort().getExtent()==null){ |
248 |
first=true;
|
249 |
} |
250 |
|
251 |
|
252 |
|
253 |
try {
|
254 |
|
255 |
// Le asignamos tambi?n una legenda por defecto acorde con
|
256 |
// el tipo de shape que tenga. Tampoco s? si es aqu? el
|
257 |
// sitio adecuado, pero en fin....
|
258 |
checkProjection(lyr, mapControl.getViewPort()); |
259 |
mapControl.getMapContext().getLayers() |
260 |
.addLayer(lyr); |
261 |
|
262 |
if (lyr instanceof FLyrVect) |
263 |
{ |
264 |
FLyrVect lyrVect = (FLyrVect) lyr; |
265 |
if (drivers[iFile] instanceof WithDefaultLegend) { |
266 |
WithDefaultLegend aux = (WithDefaultLegend) drivers[iFile]; |
267 |
lyrVect.setLegend((VectorialLegend) aux.getDefaultLegend()); |
268 |
} else {
|
269 |
lyrVect.setLegend(LegendFactory.createSingleSymbolLegend( |
270 |
lyrVect.getShapeType())); |
271 |
} |
272 |
} |
273 |
} catch (FieldNotFoundException e) {
|
274 |
//Esta no puede saltar
|
275 |
} |
276 |
rects[iFile]=lyr.getFullExtent(); |
277 |
|
278 |
// TODO: Poner una variable y dibujar solo cuando
|
279 |
// todas las capas hayan sido cargadas.
|
280 |
// TODO Se deber? de redibujar mediante la captura de los eventos, por
|
281 |
//eso se comenta la parte anterior
|
282 |
// theView.getMapControl().drawMap();
|
283 |
// theView.getTOC().refresh();
|
284 |
} |
285 |
|
286 |
} catch (DriverException e) {
|
287 |
NotificationManager.addError("Error al crear la capa", e);
|
288 |
} |
289 |
} |
290 |
|
291 |
//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.
|
292 |
if (rects.length > 1) { |
293 |
Rectangle2D rect = new Rectangle2D.Double(); |
294 |
rect.setRect(rects[0]);
|
295 |
|
296 |
if (first) {
|
297 |
for (int i = 0; i < rects.length; i++) { |
298 |
rect.add(rects[i]); |
299 |
} |
300 |
|
301 |
mapControl.getMapContext().getViewPort() |
302 |
.setExtent(rect); |
303 |
} |
304 |
} |
305 |
mapControl.getMapContext() |
306 |
.endAtomicEvent(); |
307 |
return true; |
308 |
|
309 |
} else if (fopen.getSelectedTab() instanceof WizardPanel) { |
310 |
WizardPanel wp = (WizardPanel) fopen.getSelectedTab(); |
311 |
wp.setMapCtrl(mapControl); |
312 |
wp.execute(); |
313 |
lyr = wp.getLayer(); |
314 |
|
315 |
if (lyr != null) { |
316 |
lyr.setVisible(true);
|
317 |
mapControl.getMapContext().beginAtomicEvent(); |
318 |
checkProjection(lyr, mapControl.getViewPort()); |
319 |
mapControl.getMapContext().getLayers() |
320 |
.addLayer(lyr); |
321 |
mapControl.getMapContext().endAtomicEvent(); |
322 |
return true; |
323 |
|
324 |
} |
325 |
} else {
|
326 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),"Ninguna capa seleccionada"); |
327 |
} |
328 |
} |
329 |
return false; |
330 |
} |
331 |
} |