Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / extension / AddLayer.java @ 30580

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