svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / AddLayer.java @ 28960
History | View | Annotate | Download (9.35 KB)
1 | 14821 | jmvivo | /* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
|
---|---|---|---|
2 | 2268 | fjp | *
|
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 | package com.iver.cit.gvsig; |
||
20 | |||
21 | import java.awt.Component; |
||
22 | import java.lang.reflect.InvocationTargetException; |
||
23 | import java.util.ArrayList; |
||
24 | |||
25 | import javax.swing.JOptionPane; |
||
26 | |||
27 | import org.cresques.cts.ICoordTrans; |
||
28 | import org.cresques.cts.IProjection; |
||
29 | |||
30 | 9634 | caballero | import com.hardcode.driverManager.DriverManager; |
31 | import com.hardcode.driverManager.WriterManager; |
||
32 | 17120 | vcaballero | import com.hardcode.gdbms.driver.exceptions.FileNotFoundDriverException; |
33 | 2268 | fjp | import com.iver.andami.PluginServices; |
34 | import com.iver.andami.plugins.Extension; |
||
35 | 13541 | bsanchez | import com.iver.cit.gvsig.addlayer.AddLayerDialog; |
36 | import com.iver.cit.gvsig.addlayer.fileopen.FileOpenWizard; |
||
37 | 17120 | vcaballero | import com.iver.cit.gvsig.addlayer.fileopen.solve.FileNotFoundSolve; |
38 | 13541 | bsanchez | import com.iver.cit.gvsig.addlayer.fileopen.vectorial.VectorialFileOpen; |
39 | 5190 | jmvivo | import com.iver.cit.gvsig.fmap.MapControl; |
40 | 3095 | fjp | import com.iver.cit.gvsig.fmap.ViewPort; |
41 | 10626 | caballero | import com.iver.cit.gvsig.fmap.layers.CancelationException; |
42 | 2268 | fjp | import com.iver.cit.gvsig.fmap.layers.FLayer; |
43 | 10840 | caballero | import com.iver.cit.gvsig.fmap.layers.FLayers; |
44 | 2318 | fjp | import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
45 | 2268 | fjp | import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
46 | import com.iver.cit.gvsig.gui.WizardPanel; |
||
47 | 9532 | caballero | import com.iver.cit.gvsig.project.documents.ProjectDocument; |
48 | 7537 | sbayarri | import com.iver.cit.gvsig.project.documents.view.gui.BaseView; |
49 | 7738 | jaume | import com.iver.cit.gvsig.project.documents.view.gui.IView; |
50 | 13541 | bsanchez | import com.iver.utiles.extensionPoints.ExtensionPoints; |
51 | import com.iver.utiles.extensionPoints.ExtensionPointsSingleton; |
||
52 | 2268 | fjp | |
53 | |||
54 | /**
|
||
55 | 14821 | jmvivo | * Extensi�n que abre un di�logo para seleccionar la capa o capas que se quieren
|
56 | * a�adir a la vista.
|
||
57 | 7537 | sbayarri | *
|
58 | 14821 | jmvivo | * @author Fernando Gonz�lez Cort�s
|
59 | 2268 | fjp | */
|
60 | 5005 | jorpiell | public class AddLayer extends Extension { |
61 | 13541 | bsanchez | public AddLayerDialog fopen = null; |
62 | 7476 | azabala | |
63 | 2268 | fjp | private static ArrayList wizardStack = null; |
64 | 7476 | azabala | |
65 | 2268 | fjp | static {
|
66 | AddLayer.wizardStack = new ArrayList(); |
||
67 | 14821 | jmvivo | // A�adimos el panel al wizard de cargar capa. (Esto es temporal hasta que
|
68 | 13541 | bsanchez | // el actual sea totalmente extensible)
|
69 | 14821 | jmvivo | AddLayer.addWizard(FileOpenWizard.class); |
70 | 2268 | fjp | } |
71 | 7476 | azabala | |
72 | 2268 | fjp | public static void addWizard(Class wpClass) { |
73 | AddLayer.wizardStack.add(wpClass); |
||
74 | } |
||
75 | 7476 | azabala | |
76 | public static WizardPanel getInstance(int i) |
||
77 | throws IllegalArgumentException, SecurityException, |
||
78 | InstantiationException, IllegalAccessException, |
||
79 | InvocationTargetException, NoSuchMethodException { |
||
80 | 2268 | fjp | Class wpClass = (Class) AddLayer.wizardStack.get(i); |
81 | 7476 | azabala | Class[] args = {}; |
82 | Object[] params = {}; |
||
83 | WizardPanel wp = (WizardPanel) wpClass.getConstructor(args) |
||
84 | .newInstance(params); |
||
85 | 2268 | fjp | |
86 | wp.initWizard(); |
||
87 | 7476 | azabala | |
88 | 2268 | fjp | return wp;
|
89 | } |
||
90 | |||
91 | /**
|
||
92 | 5005 | jorpiell | * @see com.iver.mdiApp.plugins.IExtension#isVisible()
|
93 | 2268 | fjp | */
|
94 | public boolean isVisible() { |
||
95 | 7537 | sbayarri | com.iver.andami.ui.mdiManager.IWindow window = PluginServices.getMDIManager() |
96 | .getActiveWindow(); |
||
97 | 2268 | fjp | |
98 | 7537 | sbayarri | if (window == null) { |
99 | 2268 | fjp | return false; |
100 | } |
||
101 | |||
102 | 7537 | sbayarri | // Any view derived from BaseView should have AddLayer available
|
103 | |||
104 | IView view; |
||
105 | try {
|
||
106 | view = (IView)window; |
||
107 | } |
||
108 | catch (ClassCastException e) { |
||
109 | return false; |
||
110 | } |
||
111 | |||
112 | if (view == null) |
||
113 | return false; |
||
114 | |||
115 | BaseView baseView = (BaseView)view; |
||
116 | return (baseView != null); |
||
117 | 2268 | fjp | } |
118 | |||
119 | /**
|
||
120 | 9634 | caballero | * @see com.iver.andami.plugins.IExtension#postInitialize()
|
121 | 2268 | fjp | */
|
122 | 9634 | caballero | public void postInitialize() { |
123 | LayerFactory.initialize(); |
||
124 | DriverManager dm=LayerFactory.getDM();
|
||
125 | PluginServices.addLoaders(dm.getDriverClassLoaders()); |
||
126 | WriterManager wm=LayerFactory.getWM(); |
||
127 | PluginServices.addLoaders(wm.getWriterClassLoaders()); |
||
128 | 14821 | jmvivo | |
129 | 13541 | bsanchez | ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance(); |
130 | 14219 | bsanchez | extensionPoints.add("FileExtendingOpenDialog", "FileOpenVectorial", VectorialFileOpen.class); |
131 | 2268 | fjp | } |
132 | |||
133 | 11359 | caballero | public static void checkProjection(FLayer lyr, ViewPort viewPort) { |
134 | 10840 | caballero | if (lyr instanceof FLayers){ |
135 | FLayers layers=(FLayers)lyr; |
||
136 | for (int i=0;i<layers.getLayersCount();i++){ |
||
137 | checkProjection(layers.getLayer(i),viewPort); |
||
138 | } |
||
139 | } |
||
140 | 7476 | azabala | if (lyr instanceof FLyrVect) { |
141 | FLyrVect lyrVect = (FLyrVect) lyr; |
||
142 | IProjection proj = lyr.getProjection(); |
||
143 | 14821 | jmvivo | // Comprobar que la projecci�n es la misma que la vista
|
144 | 7476 | azabala | if (proj == null) { |
145 | 14821 | jmvivo | // SUPONEMOS que la capa est� en la proyecci�n que
|
146 | 7476 | azabala | // estamos pidiendo (que ya es mucho suponer, ya).
|
147 | lyrVect.setProjection(viewPort.getProjection()); |
||
148 | return;
|
||
149 | } |
||
150 | 10840 | caballero | int option = JOptionPane.YES_OPTION; |
151 | 27177 | jpiera | if (!viewPort.getProjection().getAbrev().equals(proj.getAbrev())) {
|
152 | 10840 | caballero | option = JOptionPane.showConfirmDialog((Component)PluginServices.getMainFrame(), PluginServices |
153 | 11359 | caballero | .getText(AddLayer.class, "reproyectar_aviso")+"\n"+ PluginServices.getText(AddLayer.class,"Capa")+": "+lyrVect.getName(), PluginServices |
154 | .getText(AddLayer.class, "reproyectar_pregunta"),
|
||
155 | 7476 | azabala | JOptionPane.YES_NO_OPTION);
|
156 | 3095 | fjp | |
157 | 8916 | caballero | if (option != JOptionPane.OK_OPTION) { |
158 | 7476 | azabala | return;
|
159 | } |
||
160 | 10840 | caballero | |
161 | ICoordTrans ct = proj.getCT(viewPort.getProjection()); |
||
162 | lyrVect.setCoordTrans(ct); |
||
163 | System.err.println("coordTrans = " + proj.getAbrev() + " " |
||
164 | + viewPort.getProjection().getAbrev()); |
||
165 | 7476 | azabala | } |
166 | } |
||
167 | 3095 | fjp | |
168 | 7476 | azabala | } |
169 | |||
170 | 2268 | fjp | /**
|
171 | 5005 | jorpiell | * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
|
172 | 2268 | fjp | */
|
173 | public void execute(String actionCommand) { |
||
174 | 7476 | azabala | // Project project = ((ProjectExtension)
|
175 | // PluginServices.getExtension(ProjectExtension.class)).getProject();
|
||
176 | 7537 | sbayarri | BaseView theView = (BaseView) PluginServices.getMDIManager().getActiveWindow(); |
177 | 7675 | caballero | MapControl mapControl=theView.getMapControl(); |
178 | this.addLayers(mapControl);
|
||
179 | mapControl.getMapContext().callLegendChanged(); |
||
180 | 9532 | caballero | ((ProjectDocument)theView.getModel()).setModified(true);
|
181 | 5190 | jmvivo | } |
182 | |||
183 | /**
|
||
184 | * @see com.iver.andami.plugins.IExtension#isEnabled()
|
||
185 | */
|
||
186 | public boolean isEnabled() { |
||
187 | return true; |
||
188 | } |
||
189 | |||
190 | /**
|
||
191 | 7476 | azabala | * Creates FOpenDialog, and adds file tab, and additional registered tabs
|
192 | 7537 | sbayarri | *
|
193 | 7476 | azabala | * @return FOpenDialog
|
194 | */
|
||
195 | 13541 | bsanchez | private AddLayerDialog createFOpenDialog() {
|
196 | fopen = new AddLayerDialog();
|
||
197 | 7476 | azabala | |
198 | // after that, all registerez tabs (wizardpanels implementations)
|
||
199 | for (int i = 0; i < wizardStack.size(); i++) { |
||
200 | 2268 | fjp | WizardPanel wp; |
201 | try {
|
||
202 | wp = AddLayer.getInstance(i); |
||
203 | 7476 | azabala | fopen.addWizardTab(wp.getTabName(), wp); |
204 | 2268 | fjp | } catch (IllegalArgumentException e) { |
205 | e.printStackTrace(); |
||
206 | } catch (SecurityException e) { |
||
207 | e.printStackTrace(); |
||
208 | } catch (InstantiationException e) { |
||
209 | e.printStackTrace(); |
||
210 | } catch (IllegalAccessException e) { |
||
211 | e.printStackTrace(); |
||
212 | } catch (InvocationTargetException e) { |
||
213 | e.printStackTrace(); |
||
214 | } catch (NoSuchMethodException e) { |
||
215 | e.printStackTrace(); |
||
216 | } |
||
217 | 7476 | azabala | }// for
|
218 | return fopen;
|
||
219 | } |
||
220 | |||
221 | /**
|
||
222 | * Adds to mapcontrol all layers selected by user in the specified WizardPanel.
|
||
223 | 7537 | sbayarri | *
|
224 | * @param mapControl
|
||
225 | 7476 | azabala | * MapControl on which we want to load user selected layers.
|
226 | 7537 | sbayarri | * @param wizardPanel
|
227 | 7476 | azabala | * WizardPanel where user selected the layers to load
|
228 | * @return
|
||
229 | */
|
||
230 | private boolean loadGenericWizardPanelLayers(MapControl mapControl, WizardPanel wp) { |
||
231 | FLayer lyr = null;
|
||
232 | wp.setMapCtrl(mapControl); |
||
233 | wp.execute(); |
||
234 | lyr = wp.getLayer(); |
||
235 | 7537 | sbayarri | |
236 | 9026 | jorpiell | if((lyr != null) && !(lyr.isOk())){ |
237 | 7476 | azabala | //if the layer is not okay (it has errors) process them
|
238 | processErrorsOfLayer(lyr, mapControl); |
||
239 | } |
||
240 | 7537 | sbayarri | |
241 | 7476 | azabala | if (lyr != null) { |
242 | lyr.setVisible(true);
|
||
243 | mapControl.getMapContext().beginAtomicEvent(); |
||
244 | checkProjection(lyr, mapControl.getViewPort()); |
||
245 | 10626 | caballero | try {
|
246 | mapControl.getMapContext().getLayers().addLayer(lyr); |
||
247 | } catch (CancelationException e) {
|
||
248 | // TODO Auto-generated catch block
|
||
249 | e.printStackTrace(); |
||
250 | } |
||
251 | 7476 | azabala | mapControl.getMapContext().endAtomicEvent(); |
252 | return true; |
||
253 | } |
||
254 | return false; |
||
255 | } |
||
256 | 7537 | sbayarri | |
257 | 7476 | azabala | /**
|
258 | * This method process the errors found in a layer
|
||
259 | * @param lyr
|
||
260 | * @param mapControl
|
||
261 | */
|
||
262 | 7537 | sbayarri | |
263 | 7476 | azabala | private void processErrorsOfLayer(FLayer lyr, MapControl mapControl){ |
264 | 13541 | bsanchez | // List errors = lyr.getErrors();
|
265 | 7476 | azabala | // wp.callError(null);
|
266 | mapControl.getMapContext().callNewErrorEvent(null);
|
||
267 | } |
||
268 | 13541 | bsanchez | |
269 | 7476 | azabala | /**
|
270 | 14821 | jmvivo | * Abre dialogo para a�adir capas y las a�ade en mapControl
|
271 | 7537 | sbayarri | *
|
272 | 14821 | jmvivo | * Devuelve true si se han a�adido capas.
|
273 | 7476 | azabala | */
|
274 | public boolean addLayers(MapControl mapControl) { |
||
275 | // create and show the modal fopen dialog
|
||
276 | fopen = createFOpenDialog(); |
||
277 | PluginServices.getMDIManager().addWindow(fopen); |
||
278 | 2268 | fjp | |
279 | 7476 | azabala | if (fopen.isAccepted()) {
|
280 | 13541 | bsanchez | if (fopen.getSelectedTab() instanceof WizardPanel) { |
281 | 2268 | fjp | WizardPanel wp = (WizardPanel) fopen.getSelectedTab(); |
282 | 8790 | jmvivo | return loadGenericWizardPanelLayers(mapControl, wp);
|
283 | 2268 | fjp | } else {
|
284 | 7476 | azabala | JOptionPane.showMessageDialog((Component) PluginServices |
285 | 12323 | caballero | .getMainFrame(), PluginServices.getText(this,"ninguna_capa_seleccionada")); |
286 | 2268 | fjp | } |
287 | } |
||
288 | 5190 | jmvivo | return false; |
289 | 2268 | fjp | } |
290 | 9634 | caballero | |
291 | 17120 | vcaballero | /* (non-Javadoc)
|
292 | * @see com.iver.andami.plugins.IExtension#initialize()
|
||
293 | */
|
||
294 | 9634 | caballero | public void initialize() { |
295 | 17120 | vcaballero | //Listener para resolver un FileDriverNotFoundException
|
296 | LayerFactory.addSolveErrorForLayer(FileNotFoundDriverException.class,new FileNotFoundSolve());
|
||
297 | 9634 | caballero | |
298 | 15640 | jmvivo | PluginServices.getIconTheme().registerDefault( |
299 | 14821 | jmvivo | "layer-add",
|
300 | this.getClass().getClassLoader().getResource("images/addlayer.png") |
||
301 | ); |
||
302 | 17120 | vcaballero | |
303 | 9634 | caballero | } |
304 | 2268 | fjp | } |