Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / AddLayer.java @ 22360

History | View | Annotate | Download (9.71 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
import org.gvsig.fmap.data.datastores.feature.file.dbf.Register;
30
import org.gvsig.fmap.mapcontext.ViewPort;
31
import org.gvsig.fmap.mapcontext.layers.CancelationException;
32
import org.gvsig.fmap.mapcontext.layers.FLayer;
33
import org.gvsig.fmap.mapcontext.layers.FLayers;
34
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
35
import org.gvsig.fmap.mapcontrol.MapControl;
36

    
37
import com.iver.andami.PluginServices;
38
import com.iver.andami.plugins.Extension;
39
import com.iver.cit.gvsig.addlayer.AddLayerDialog;
40
import com.iver.cit.gvsig.addlayer.fileopen.FileOpenWizard;
41
import com.iver.cit.gvsig.addlayer.fileopen.vectorial.VectorialFileOpen;
42
import com.iver.cit.gvsig.gui.WizardPanel;
43
import com.iver.cit.gvsig.project.documents.ProjectDocument;
44
import com.iver.cit.gvsig.project.documents.view.gui.BaseView;
45
import com.iver.cit.gvsig.project.documents.view.gui.IView;
46
import com.iver.utiles.extensionPoints.ExtensionPoints;
47
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton;
48

    
49

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

    
59
        private static ArrayList wizardStack = null;
60

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

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

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

    
82
                wp.initWizard();
83

    
84
                return wp;
85
        }
86

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

    
94
                if (window == null) {
95
                        return false;
96
                }
97

    
98
                // Any view derived from BaseView should have AddLayer available
99

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

    
108
                if (view == null)
109
                        return false;
110

    
111
                BaseView baseView = (BaseView)view;
112
                return (baseView != null);
113
        }
114

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

    
125
                ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
126
                extensionPoints.add("FileExtendingOpenDialog", "FileOpenVectorial", VectorialFileOpen.class);
127
        }
128

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

    
153
                                if (option != JOptionPane.OK_OPTION) {
154
                                        return;
155
                                }
156

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

    
164
        }
165

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

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

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

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

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

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

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

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

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

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

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

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

    
294

    
295
                Register.selfRegister();
296
                org.gvsig.fmap.data.datastores.feature.file.dgn.Register.selfRegister();
297
                org.gvsig.fmap.data.datastores.feature.file.dgn.operation.Register.selfRegister();
298
                org.gvsig.fmap.data.datastores.feature.file.dxf.Register.selfRegister();
299
                org.gvsig.fmap.data.datastores.feature.file.dxf.operation.Register.selfRegister();
300
                org.gvsig.fmap.data.datastores.feature.file.shp.Register.selfRegister();
301
                org.gvsig.fmap.data.datastores.feature.db.jdbc.postgresql.Register.selfRegister();
302
                org.gvsig.fmap.data.datastores.feature.db.jdbc.h2.Register.selfRegister();
303
                PluginServices.getIconTheme().registerDefault(
304
                                "layer-add",
305
                                this.getClass().getClassLoader().getResource("images/addlayer.png")
306
                        );
307

    
308
        }
309
}