Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / AddLayer.java @ 22360

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