Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / AddLayer.java @ 14219

History | View | Annotate | Download (8.79 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 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

    
30
import com.hardcode.driverManager.DriverManager;
31
import com.hardcode.driverManager.WriterManager;
32
import com.iver.andami.PluginServices;
33
import com.iver.andami.plugins.Extension;
34
import com.iver.cit.gvsig.addlayer.AddLayerDialog;
35
import com.iver.cit.gvsig.addlayer.fileopen.FileOpenWizard;
36
import com.iver.cit.gvsig.addlayer.fileopen.vectorial.VectorialFileOpen;
37
import com.iver.cit.gvsig.fmap.MapControl;
38
import com.iver.cit.gvsig.fmap.ViewPort;
39
import com.iver.cit.gvsig.fmap.layers.CancelationException;
40
import com.iver.cit.gvsig.fmap.layers.FLayer;
41
import com.iver.cit.gvsig.fmap.layers.FLayers;
42
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
43
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
44
import com.iver.cit.gvsig.gui.WizardPanel;
45
import com.iver.cit.gvsig.project.documents.ProjectDocument;
46
import com.iver.cit.gvsig.project.documents.view.gui.BaseView;
47
import com.iver.cit.gvsig.project.documents.view.gui.IView;
48
import com.iver.utiles.extensionPoints.ExtensionPoints;
49
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton;
50

    
51

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

    
61
        private static ArrayList wizardStack = null;
62

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

    
70
        public static void addWizard(Class wpClass) {
71
                AddLayer.wizardStack.add(wpClass);
72
        }
73

    
74
        public static WizardPanel getInstance(int i)
75
                        throws IllegalArgumentException, SecurityException,
76
                        InstantiationException, IllegalAccessException,
77
                        InvocationTargetException, NoSuchMethodException {
78
                Class wpClass = (Class) AddLayer.wizardStack.get(i);
79
                Class[] args = {};
80
                Object[] params = {};
81
                WizardPanel wp = (WizardPanel) wpClass.getConstructor(args)
82
                                .newInstance(params);
83

    
84
                wp.initWizard();
85

    
86
                return wp;
87
        }
88

    
89
        /**
90
         * @see com.iver.mdiApp.plugins.IExtension#isVisible()
91
         */
92
        public boolean isVisible() {
93
                com.iver.andami.ui.mdiManager.IWindow window = PluginServices.getMDIManager()
94
                                                                                                                         .getActiveWindow();
95

    
96
                if (window == null) {
97
                        return false;
98
                }
99

    
100
                // Any view derived from BaseView should have AddLayer available
101

    
102
                IView view;
103
                try {
104
                        view = (IView)window;
105
                }
106
                catch (ClassCastException e) {
107
                    return false;
108
                }
109

    
110
                if (view == null)
111
                        return false;
112

    
113
                BaseView baseView = (BaseView)view;
114
                return (baseView != null);
115
        }
116

    
117
        /**
118
         * @see com.iver.andami.plugins.IExtension#postInitialize()
119
         */
120
        public void postInitialize() {
121
                LayerFactory.initialize();
122
                DriverManager dm=LayerFactory.getDM();
123
                PluginServices.addLoaders(dm.getDriverClassLoaders());
124
                WriterManager wm=LayerFactory.getWM();
125
                PluginServices.addLoaders(wm.getWriterClassLoaders());
126
                
127
                ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
128
                extensionPoints.add("FileExtendingOpenDialog", "FileOpenVectorial", VectorialFileOpen.class);
129
        }
130

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

    
155
                                if (option != JOptionPane.OK_OPTION) {
156
                                        return;
157
                                }
158

    
159
                                ICoordTrans ct = proj.getCT(viewPort.getProjection());
160
                                lyrVect.setCoordTrans(ct);
161
                                System.err.println("coordTrans = " + proj.getAbrev() + " "
162
                                                + viewPort.getProjection().getAbrev());
163
                        }
164
                }
165

    
166
        }
167

    
168
        /**
169
         * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
170
         */
171
        public void execute(String actionCommand) {
172
                // Project project = ((ProjectExtension)
173
                // PluginServices.getExtension(ProjectExtension.class)).getProject();
174
                BaseView theView = (BaseView) PluginServices.getMDIManager().getActiveWindow();
175
                MapControl mapControl=theView.getMapControl();
176
                this.addLayers(mapControl);
177
                mapControl.getMapContext().callLegendChanged();
178
                ((ProjectDocument)theView.getModel()).setModified(true);
179
        }
180

    
181
        /**
182
         * @see com.iver.andami.plugins.IExtension#isEnabled()
183
         */
184
        public boolean isEnabled() {
185
                return true;
186
        }
187

    
188
        /**
189
         * Creates FOpenDialog, and adds file tab, and additional registered tabs
190
         *
191
         * @return FOpenDialog
192
         */
193
        private AddLayerDialog createFOpenDialog() {
194
                fopen = new AddLayerDialog();
195

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

    
219
        /**
220
         * Adds to mapcontrol all layers selected by user in the specified WizardPanel.
221
         *
222
         * @param mapControl
223
         *         MapControl on which we want to load user selected layers.
224
         * @param wizardPanel
225
         *         WizardPanel where user selected the layers to load
226
         * @return
227
         */
228
        private boolean loadGenericWizardPanelLayers(MapControl mapControl, WizardPanel wp) {
229
                FLayer lyr = null;
230
                wp.setMapCtrl(mapControl);
231
                wp.execute();
232
                lyr = wp.getLayer();
233

    
234
                if((lyr != null) && !(lyr.isOk())){
235
                        //if the layer is not okay (it has errors) process them
236
                        processErrorsOfLayer(lyr, mapControl);
237
                }
238

    
239
                if (lyr != null) {
240
                        lyr.setVisible(true);
241
                        mapControl.getMapContext().beginAtomicEvent();
242
                        checkProjection(lyr, mapControl.getViewPort());
243
                        try {
244
                                mapControl.getMapContext().getLayers().addLayer(lyr);
245
                        } catch (CancelationException e) {
246
                                // TODO Auto-generated catch block
247
                                e.printStackTrace();
248
                        }
249
                        mapControl.getMapContext().endAtomicEvent();
250
                        return true;
251
                }
252
                return false;
253
        }
254

    
255
        /**
256
         * This method process the errors found in a layer
257
         * @param lyr
258
         * @param mapControl
259
         */
260

    
261
        private void processErrorsOfLayer(FLayer lyr, MapControl mapControl){
262
//                List errors = lyr.getErrors();
263
//                wp.callError(null);
264
                mapControl.getMapContext().callNewErrorEvent(null);
265
        }
266

    
267
        /**
268
         * Abre dialogo para a?adir capas y las a?ade en mapControl
269
         *
270
         * Devuelve true si se han a?adido capas.
271
         */
272
        public boolean addLayers(MapControl mapControl) {
273
                // create and show the modal fopen dialog
274
                fopen = createFOpenDialog();
275
                PluginServices.getMDIManager().addWindow(fopen);
276

    
277
                if (fopen.isAccepted()) {
278
                                if (fopen.getSelectedTab() instanceof WizardPanel) {
279
                                WizardPanel wp = (WizardPanel) fopen.getSelectedTab();
280
                                return loadGenericWizardPanelLayers(mapControl, wp);
281
                        } else {
282
                                JOptionPane.showMessageDialog((Component) PluginServices
283
                                                .getMainFrame(), PluginServices.getText(this,"ninguna_capa_seleccionada"));
284
                        }
285
                }
286
                return false;
287
        }
288

    
289
        public void initialize() {
290

    
291
        }
292
}