Statistics
| Revision:

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

History | View | Annotate | Download (8.08 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.app.addlayer.AddLayerDialog;
33
import org.gvsig.app.gui.WizardPanel;
34
import org.gvsig.app.project.documents.AbstractDocument;
35
import org.gvsig.app.project.documents.view.gui.AbstractViewPanel;
36
import org.gvsig.app.project.documents.view.gui.IView;
37
import org.gvsig.fmap.dal.serverexplorer.filesystem.swing.FilesystemExplorerAddLayerWizardPanel;
38
import org.gvsig.fmap.mapcontext.ViewPort;
39
import org.gvsig.fmap.mapcontext.layers.FLayer;
40
import org.gvsig.fmap.mapcontext.layers.FLayers;
41
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
42
import org.gvsig.fmap.mapcontrol.MapControl;
43

    
44

    
45

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

    
55
        private static ArrayList<Class<? extends WizardPanel>> wizardStack = null;
56

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

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

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

    
80
                return wp;
81
        }
82

    
83
        /**
84
         * @see com.iver.mdiApp.plugins.IExtension#isVisible()
85
         */
86
        public boolean isVisible() {
87
                org.gvsig.andami.ui.mdiManager.IWindow window = PluginServices.getMDIManager()
88
                                                                                                                         .getActiveWindow();
89

    
90
                if (window == null) {
91
                        return false;
92
                }
93

    
94
                // Any view derived from BaseView should have AddLayer available
95

    
96
                IView view;
97
                try {
98
                        view = (IView)window;
99
                }
100
                catch (ClassCastException e) {
101
                    return false;
102
                }
103

    
104
                if (view == null) {
105
                        return false;
106
                }
107

    
108
                AbstractViewPanel baseView = (AbstractViewPanel)view;
109
                return (baseView != null);
110
        }
111

    
112
        /**
113
         * @see org.gvsig.andami.plugins.IExtension#postInitialize()
114
         */
115
        public void postInitialize() {
116
//                LayerFactory.initialize();
117
//                DriverManager dm=LayerFactory.getDM();
118
//                PluginServices.addLoaders(dm.getDriverClassLoaders());
119
//                WriterManager wm=LayerFactory.getWM();
120
//                PluginServices.addLoaders(wm.getWriterClassLoaders());
121

    
122
                //                ToolsLocator.getExtensionPointManager().add("FileExtendingOpenDialog",
123
                //                                "").append("FileOpenVectorial", "", VectorialFileOpen.class);
124
        }
125

    
126
        public static void checkProjection(FLayer lyr, ViewPort viewPort) {
127
                if (lyr instanceof FLayers){
128
                        FLayers layers=(FLayers)lyr;
129
                        for (int i=0;i<layers.getLayersCount();i++){
130
                                checkProjection(layers.getLayer(i),viewPort);
131
                        }
132
                }
133
                if (lyr instanceof FLyrVect) {
134
                        FLyrVect lyrVect = (FLyrVect) lyr;
135
                        IProjection proj = lyr.getProjection();
136
                        // Comprobar que la projecci�n es la misma que la vista
137
                        if (proj == null) {
138
                                // SUPONEMOS que la capa est� en la proyecci�n que
139
                                // estamos pidiendo (que ya es mucho suponer, ya).
140
                                lyrVect.setProjection(viewPort.getProjection());
141
                                return;
142
                        }
143
                        int option = JOptionPane.YES_OPTION;
144
                        if (proj != viewPort.getProjection()) {
145
                                option = JOptionPane.showConfirmDialog((Component)PluginServices.getMainFrame(), PluginServices
146
                                                .getText(AddLayer.class, "reproyectar_aviso")+"\n"+ PluginServices.getText(AddLayer.class,"Capa")+": "+lyrVect.getName(), PluginServices
147
                                                .getText(AddLayer.class, "reproyectar_pregunta"),
148
                                                JOptionPane.YES_NO_OPTION);
149

    
150
                                if (option != JOptionPane.OK_OPTION) {
151
                                        return;
152
                                }
153

    
154
                                ICoordTrans ct = proj.getCT(viewPort.getProjection());
155
                                lyrVect.setCoordTrans(ct);
156
                                System.err.println("coordTrans = " + proj.getAbrev() + " "
157
                                                + viewPort.getProjection().getAbrev());
158
                        }
159
                }
160

    
161
        }
162

    
163
        /**
164
         * @see org.gvsig.andami.plugins.IExtension#execute(java.lang.String)
165
         */
166
        public void execute(String actionCommand) {
167
                // Project project = ((ProjectExtension)
168
                // PluginServices.getExtension(ProjectExtension.class)).getProject();
169
                AbstractViewPanel theView = (AbstractViewPanel) PluginServices.getMDIManager().getActiveWindow();
170
                MapControl mapControl=theView.getMapControl();
171
                this.addLayers(mapControl);
172
                mapControl.getMapContext().callLegendChanged();
173
                ((AbstractDocument)theView.getModel()).setModified(true);
174
        }
175

    
176
        /**
177
         * @see org.gvsig.andami.plugins.IExtension#isEnabled()
178
         */
179
        public boolean isEnabled() {
180
                return true;
181
        }
182

    
183
        /**
184
         * Creates FOpenDialog, and adds file tab, and additional registered tabs
185
         *
186
         * @return FOpenDialog
187
         */
188
    private AddLayerDialog createFOpenDialog(MapControl mapControl) {
189
                fopen = new AddLayerDialog();
190

    
191
                // after that, all registerez tabs (wizardpanels implementations)
192
                for (int i = 0; i < wizardStack.size(); i++) {
193
                        WizardPanel wp;
194
                        try {
195
                                wp = AddLayer.getInstance(i, mapControl);
196
                
197
                                fopen.addWizardTab(wp.getTabName(), wp);
198
                        } catch (IllegalArgumentException e) {
199
                                e.printStackTrace();
200
                        } catch (SecurityException e) {
201
                                e.printStackTrace();
202
                        } catch (InstantiationException e) {
203
                                e.printStackTrace();
204
                        } catch (IllegalAccessException e) {
205
                                e.printStackTrace();
206
                        } catch (InvocationTargetException e) {
207
                                e.printStackTrace();
208
                        } catch (NoSuchMethodException e) {
209
                                e.printStackTrace();
210
                        }
211
                }// for
212
                return fopen;
213
        }
214

    
215
        /**
216
         * Adds to mapcontrol all layers selected by user in the specified WizardPanel.
217
         *
218
         * @param mapControl
219
         *         MapControl on which we want to load user selected layers.
220
         * @param wizardPanel
221
         *         WizardPanel where user selected the layers to load
222
         * @return
223
         */
224
        private boolean loadGenericWizardPanelLayers(MapControl mapControl, WizardPanel wp) {
225
                wp.setMapCtrl(mapControl);
226
                wp.execute();
227
                return true;
228
        }
229

    
230
        /**
231
         * Abre dialogo para a�adir capas y las a�ade en mapControl
232
         *
233
         * Devuelve true si se han a�adido capas.
234
         */
235
        public boolean addLayers(MapControl mapControl) {
236
                // create and show the modal fopen dialog
237
        fopen = createFOpenDialog(mapControl);
238
                PluginServices.getMDIManager().addWindow(fopen);
239

    
240
                if (fopen.isAccepted()) {
241
                                if (fopen.getSelectedTab() instanceof WizardPanel) {
242
                                WizardPanel wp = (WizardPanel) fopen.getSelectedTab();
243
                                return loadGenericWizardPanelLayers(mapControl, wp);
244
                        } else {
245
                                JOptionPane.showMessageDialog((Component) PluginServices
246
                                                .getMainFrame(), PluginServices.getText(this,"ninguna_capa_seleccionada"));
247
                        }
248
                }
249
                return false;
250
        }
251

    
252
        /* (non-Javadoc)
253
         * @see com.iver.andami.plugins.IExtension#initialize()
254
         */
255
        public void initialize() {
256
                   IconThemeHelper.registerIcon("action", "view-layer-add", this);
257
        }
258
}