Statistics
| Revision:

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

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

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

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

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

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

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

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

    
169
        }
170

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

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

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

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

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

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

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

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

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

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

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

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

    
301
                PluginServices.getIconTheme().registerDefault(
302
                                "layer-add",
303
                                this.getClass().getClassLoader().getResource("images/addlayer.png")
304
                        );
305

    
306
        }
307
}
308

    
309
// [eiel-gestion-conexiones]