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 @ 40558

History | View | Annotate | Download (7.91 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 org.gvsig.app.extension;
25

    
26
import java.awt.Component;
27
import java.lang.reflect.InvocationTargetException;
28
import java.util.ArrayList;
29

    
30
import javax.swing.JOptionPane;
31

    
32
import org.cresques.cts.ICoordTrans;
33
import org.cresques.cts.IProjection;
34
import org.gvsig.andami.IconThemeHelper;
35
import org.gvsig.andami.PluginServices;
36
import org.gvsig.andami.plugins.Extension;
37
import org.gvsig.andami.ui.mdiManager.IWindow;
38
import org.gvsig.app.ApplicationLocator;
39
import org.gvsig.app.addlayer.AddLayerDialog;
40
import org.gvsig.app.gui.WizardPanel;
41
import org.gvsig.app.project.documents.AbstractDocument;
42
import org.gvsig.app.project.documents.view.gui.AbstractViewPanel;
43
import org.gvsig.app.project.documents.view.gui.IView;
44
import org.gvsig.fmap.dal.serverexplorer.filesystem.swing.FilesystemExplorerAddLayerWizardPanel;
45
import org.gvsig.fmap.mapcontext.ViewPort;
46
import org.gvsig.fmap.mapcontext.layers.FLayer;
47
import org.gvsig.fmap.mapcontext.layers.FLayers;
48
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
49
import org.gvsig.fmap.mapcontrol.MapControl;
50

    
51

    
52

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

    
62
        private static ArrayList<Class<? extends WizardPanel>> wizardStack = null;
63

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

    
72
        public static void addWizard(Class<? extends WizardPanel> wpClass) {
73
                AddLayer.wizardStack.add(wpClass);
74
        }
75

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

    
87
                return wp;
88
        }
89

    
90
        /**
91
         * @see com.iver.mdiApp.plugins.IExtension#isVisible()
92
         */
93
        public boolean isVisible() {
94
                
95
                IWindow iw = ApplicationLocator.getManager().getActiveWindow();
96
                return (iw instanceof IView);
97
        }
98

    
99

    
100
        /**
101
         * @see org.gvsig.andami.plugins.IExtension#postInitialize()
102
         */
103
        public void postInitialize() {
104
//                LayerFactory.initialize();
105
//                DriverManager dm=LayerFactory.getDM();
106
//                PluginServices.addLoaders(dm.getDriverClassLoaders());
107
//                WriterManager wm=LayerFactory.getWM();
108
//                PluginServices.addLoaders(wm.getWriterClassLoaders());
109

    
110
                //                ToolsLocator.getExtensionPointManager().add("FileExtendingOpenDialog",
111
                //                                "").append("FileOpenVectorial", "", VectorialFileOpen.class);
112
        }
113

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

    
138
                                if (option != JOptionPane.OK_OPTION) {
139
                                        return;
140
                                }
141

    
142
                                ICoordTrans ct = proj.getCT(viewPort.getProjection());
143
                                lyrVect.setCoordTrans(ct);
144
                                System.err.println("coordTrans = " + proj.getAbrev() + " "
145
                                                + viewPort.getProjection().getAbrev());
146
                        }
147
                }
148

    
149
        }
150

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

    
164
        /**
165
         * @see org.gvsig.andami.plugins.IExtension#isEnabled()
166
         */
167
        public boolean isEnabled() {
168
                return true;
169
        }
170

    
171
        /**
172
         * Creates FOpenDialog, and adds file tab, and additional registered tabs
173
         *
174
         * @return FOpenDialog
175
         */
176
    private AddLayerDialog createFOpenDialog(MapControl mapControl) {
177
                fopen = new AddLayerDialog();
178

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

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

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

    
228
                if (fopen.isAccepted()) {
229
                                if (fopen.getSelectedTab() instanceof WizardPanel) {
230
                                WizardPanel wp = (WizardPanel) fopen.getSelectedTab();
231
                                return loadGenericWizardPanelLayers(mapControl, wp);
232
                        } else {
233
                                JOptionPane.showMessageDialog((Component) PluginServices
234
                                                .getMainFrame(), PluginServices.getText(this,"ninguna_capa_seleccionada"));
235
                        }
236
                }
237
                return false;
238
        }
239

    
240
        /* (non-Javadoc)
241
         * @see com.iver.andami.plugins.IExtension#initialize()
242
         */
243
        public void initialize() {
244
                   IconThemeHelper.registerIcon("action", "view-layer-add", this);
245
        }
246
}