Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / extension / AddLayer.java @ 40485

History | View | Annotate | Download (7.81 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
package org.gvsig.app.extension;
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
import org.gvsig.andami.IconThemeHelper;
30
import org.gvsig.andami.PluginServices;
31
import org.gvsig.andami.plugins.Extension;
32
import org.gvsig.andami.ui.mdiManager.IWindow;
33
import org.gvsig.app.ApplicationLocator;
34
import org.gvsig.app.addlayer.AddLayerDialog;
35
import org.gvsig.app.gui.WizardPanel;
36
import org.gvsig.app.project.documents.AbstractDocument;
37
import org.gvsig.app.project.documents.view.gui.AbstractViewPanel;
38
import org.gvsig.app.project.documents.view.gui.IView;
39
import org.gvsig.fmap.dal.serverexplorer.filesystem.swing.FilesystemExplorerAddLayerWizardPanel;
40
import org.gvsig.fmap.mapcontext.ViewPort;
41
import org.gvsig.fmap.mapcontext.layers.FLayer;
42
import org.gvsig.fmap.mapcontext.layers.FLayers;
43
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
44
import org.gvsig.fmap.mapcontrol.MapControl;
45

    
46

    
47

    
48
/**
49
 * Extensi�n que abre un di�logo para seleccionar la capa o capas que se quieren
50
 * a�adir a la vista.
51
 *
52
 * @author Fernando Gonz�lez Cort�s
53
 */
54
public class AddLayer extends Extension {
55
        public AddLayerDialog fopen = null;
56

    
57
        private static ArrayList<Class<? extends WizardPanel>> wizardStack = null;
58

    
59
        static {
60
                AddLayer.wizardStack = new ArrayList<Class<? extends WizardPanel>>();
61
                // A�adimos el panel al wizard de cargar capa. (Esto es temporal hasta que
62
    // el actual sea totalmente extensible)
63
                //                AddLayer.addWizard(FileOpenWizard.class);
64
                AddLayer.addWizard(FilesystemExplorerAddLayerWizardPanel.class);
65
        }
66

    
67
        public static void addWizard(Class<? extends WizardPanel> wpClass) {
68
                AddLayer.wizardStack.add(wpClass);
69
        }
70

    
71
        private static WizardPanel getInstance(int i, MapControl mapControl)
72
                        throws IllegalArgumentException, SecurityException,
73
                        InstantiationException, IllegalAccessException,
74
                        InvocationTargetException, NoSuchMethodException {
75
                Class<? extends WizardPanel> wpClass = AddLayer.wizardStack.get(i);
76
                Object[] params = {};
77
                WizardPanel wp = wpClass.getConstructor()
78
                                .newInstance(params);
79
                wp.setMapCtrl(mapControl);
80
                wp.initWizard();
81

    
82
                return wp;
83
        }
84

    
85
        /**
86
         * @see com.iver.mdiApp.plugins.IExtension#isVisible()
87
         */
88
        public boolean isVisible() {
89
                
90
                IWindow iw = ApplicationLocator.getManager().getActiveWindow();
91
                return (iw instanceof IView);
92
        }
93

    
94

    
95
        /**
96
         * @see org.gvsig.andami.plugins.IExtension#postInitialize()
97
         */
98
        public void postInitialize() {
99
//                LayerFactory.initialize();
100
//                DriverManager dm=LayerFactory.getDM();
101
//                PluginServices.addLoaders(dm.getDriverClassLoaders());
102
//                WriterManager wm=LayerFactory.getWM();
103
//                PluginServices.addLoaders(wm.getWriterClassLoaders());
104

    
105
                //                ToolsLocator.getExtensionPointManager().add("FileExtendingOpenDialog",
106
                //                                "").append("FileOpenVectorial", "", VectorialFileOpen.class);
107
        }
108

    
109
        public static void checkProjection(FLayer lyr, ViewPort viewPort) {
110
                if (lyr instanceof FLayers){
111
                        FLayers layers=(FLayers)lyr;
112
                        for (int i=0;i<layers.getLayersCount();i++){
113
                                checkProjection(layers.getLayer(i),viewPort);
114
                        }
115
                }
116
                if (lyr instanceof FLyrVect) {
117
                        FLyrVect lyrVect = (FLyrVect) lyr;
118
                        IProjection proj = lyr.getProjection();
119
                        // Comprobar que la projecci�n es la misma que la vista
120
                        if (proj == null) {
121
                                // SUPONEMOS que la capa est� en la proyecci�n que
122
                                // estamos pidiendo (que ya es mucho suponer, ya).
123
                                lyrVect.setProjection(viewPort.getProjection());
124
                                return;
125
                        }
126
                        int option = JOptionPane.YES_OPTION;
127
                        if (proj != viewPort.getProjection()) {
128
                                option = JOptionPane.showConfirmDialog((Component)PluginServices.getMainFrame(), PluginServices
129
                                                .getText(AddLayer.class, "reproyectar_aviso")+"\n"+ PluginServices.getText(AddLayer.class,"Capa")+": "+lyrVect.getName(), PluginServices
130
                                                .getText(AddLayer.class, "reproyectar_pregunta"),
131
                                                JOptionPane.YES_NO_OPTION);
132

    
133
                                if (option != JOptionPane.OK_OPTION) {
134
                                        return;
135
                                }
136

    
137
                                ICoordTrans ct = proj.getCT(viewPort.getProjection());
138
                                lyrVect.setCoordTrans(ct);
139
                                System.err.println("coordTrans = " + proj.getAbrev() + " "
140
                                                + viewPort.getProjection().getAbrev());
141
                        }
142
                }
143

    
144
        }
145

    
146
        /**
147
         * @see org.gvsig.andami.plugins.IExtension#execute(java.lang.String)
148
         */
149
        public void execute(String actionCommand) {
150
                // Project project = ((ProjectExtension)
151
                // PluginServices.getExtension(ProjectExtension.class)).getProject();
152
                AbstractViewPanel theView = (AbstractViewPanel) PluginServices.getMDIManager().getActiveWindow();
153
                MapControl mapControl=theView.getMapControl();
154
                this.addLayers(mapControl);
155
                mapControl.getMapContext().callLegendChanged();
156
                ((AbstractDocument)theView.getModel()).setModified(true);
157
        }
158

    
159
        /**
160
         * @see org.gvsig.andami.plugins.IExtension#isEnabled()
161
         */
162
        public boolean isEnabled() {
163
                return true;
164
        }
165

    
166
        /**
167
         * Creates FOpenDialog, and adds file tab, and additional registered tabs
168
         *
169
         * @return FOpenDialog
170
         */
171
    private AddLayerDialog createFOpenDialog(MapControl mapControl) {
172
                fopen = new AddLayerDialog();
173

    
174
                // after that, all registerez tabs (wizardpanels implementations)
175
                for (int i = 0; i < wizardStack.size(); i++) {
176
                        WizardPanel wp;
177
                        try {
178
                                wp = AddLayer.getInstance(i, mapControl);
179
                
180
                                fopen.addWizardTab(wp.getTabName(), wp);
181
                        } catch (IllegalArgumentException e) {
182
                                e.printStackTrace();
183
                        } catch (SecurityException e) {
184
                                e.printStackTrace();
185
                        } catch (InstantiationException e) {
186
                                e.printStackTrace();
187
                        } catch (IllegalAccessException e) {
188
                                e.printStackTrace();
189
                        } catch (InvocationTargetException e) {
190
                                e.printStackTrace();
191
                        } catch (NoSuchMethodException e) {
192
                                e.printStackTrace();
193
                        }
194
                }// for
195
                return fopen;
196
        }
197

    
198
        /**
199
         * Adds to mapcontrol all layers selected by user in the specified WizardPanel.
200
         *
201
         * @param mapControl
202
         *         MapControl on which we want to load user selected layers.
203
         * @param wizardPanel
204
         *         WizardPanel where user selected the layers to load
205
         * @return
206
         */
207
        private boolean loadGenericWizardPanelLayers(MapControl mapControl, WizardPanel wp) {
208
                wp.setMapCtrl(mapControl);
209
                wp.execute();
210
                return true;
211
        }
212

    
213
        /**
214
         * Abre dialogo para a�adir capas y las a�ade en mapControl
215
         *
216
         * Devuelve true si se han a�adido capas.
217
         */
218
        public boolean addLayers(MapControl mapControl) {
219
                // create and show the modal fopen dialog
220
        fopen = createFOpenDialog(mapControl);
221
                PluginServices.getMDIManager().addWindow(fopen);
222

    
223
                if (fopen.isAccepted()) {
224
                                if (fopen.getSelectedTab() instanceof WizardPanel) {
225
                                WizardPanel wp = (WizardPanel) fopen.getSelectedTab();
226
                                return loadGenericWizardPanelLayers(mapControl, wp);
227
                        } else {
228
                                JOptionPane.showMessageDialog((Component) PluginServices
229
                                                .getMainFrame(), PluginServices.getText(this,"ninguna_capa_seleccionada"));
230
                        }
231
                }
232
                return false;
233
        }
234

    
235
        /* (non-Javadoc)
236
         * @see com.iver.andami.plugins.IExtension#initialize()
237
         */
238
        public void initialize() {
239
                   IconThemeHelper.registerIcon("action", "view-layer-add", this);
240
        }
241
}