Statistics
| Revision:

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

History | View | Annotate | Download (9.35 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.hardcode.gdbms.driver.exceptions.FileNotFoundDriverException;
33
import com.iver.andami.PluginServices;
34
import com.iver.andami.plugins.Extension;
35
import com.iver.cit.gvsig.addlayer.AddLayerDialog;
36
import com.iver.cit.gvsig.addlayer.fileopen.FileOpenWizard;
37
import com.iver.cit.gvsig.addlayer.fileopen.solve.FileNotFoundSolve;
38
import com.iver.cit.gvsig.addlayer.fileopen.vectorial.VectorialFileOpen;
39
import com.iver.cit.gvsig.fmap.MapControl;
40
import com.iver.cit.gvsig.fmap.ViewPort;
41
import com.iver.cit.gvsig.fmap.layers.CancelationException;
42
import com.iver.cit.gvsig.fmap.layers.FLayer;
43
import com.iver.cit.gvsig.fmap.layers.FLayers;
44
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
45
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
46
import com.iver.cit.gvsig.gui.WizardPanel;
47
import com.iver.cit.gvsig.project.documents.ProjectDocument;
48
import com.iver.cit.gvsig.project.documents.view.gui.BaseView;
49
import com.iver.cit.gvsig.project.documents.view.gui.IView;
50
import com.iver.utiles.extensionPoints.ExtensionPoints;
51
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton;
52

    
53

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

    
63
        private static ArrayList wizardStack = null;
64

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

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

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

    
86
                wp.initWizard();
87

    
88
                return wp;
89
        }
90

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

    
98
                if (window == null) {
99
                        return false;
100
                }
101

    
102
                // Any view derived from BaseView should have AddLayer available
103

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

    
112
                if (view == null)
113
                        return false;
114

    
115
                BaseView baseView = (BaseView)view;
116
                return (baseView != null);
117
        }
118

    
119
        /**
120
         * @see com.iver.andami.plugins.IExtension#postInitialize()
121
         */
122
        public void postInitialize() {
123
                LayerFactory.initialize();
124
                DriverManager dm=LayerFactory.getDM();
125
                PluginServices.addLoaders(dm.getDriverClassLoaders());
126
                WriterManager wm=LayerFactory.getWM();
127
                PluginServices.addLoaders(wm.getWriterClassLoaders());
128

    
129
                ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
130
                extensionPoints.add("FileExtendingOpenDialog", "FileOpenVectorial", VectorialFileOpen.class);
131
        }
132

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

    
157
                                if (option != JOptionPane.OK_OPTION) {
158
                                        return;
159
                                }
160

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

    
168
        }
169

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

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

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

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

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

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

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

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

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

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

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

    
291
        /* (non-Javadoc)
292
         * @see com.iver.andami.plugins.IExtension#initialize()
293
         */
294
        public void initialize() {
295
                //Listener para resolver un FileDriverNotFoundException
296
                LayerFactory.addSolveErrorForLayer(FileNotFoundDriverException.class,new FileNotFoundSolve());
297

    
298
                PluginServices.getIconTheme().registerDefault(
299
                                "layer-add",
300
                                this.getClass().getClassLoader().getResource("images/addlayer.png")
301
                        );
302

    
303
        }
304
}