Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / AddLayer.java @ 24759

History | View | Annotate | Download (12.4 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.dal.DALFileLibrary;
30
import org.gvsig.fmap.dal.DALLibrary;
31
import org.gvsig.fmap.dal.store.dbf.DBFLibrary;
32
import org.gvsig.fmap.dal.store.dxf.DXFLibrary;
33
import org.gvsig.fmap.dal.store.shp.SHPLibrary;
34
import org.gvsig.fmap.data.feature.db.DBStoreParameters;
35
import org.gvsig.fmap.mapcontext.MapContext;
36
import org.gvsig.fmap.mapcontext.ViewPort;
37
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
38
import org.gvsig.fmap.mapcontext.layers.CancelationException;
39
import org.gvsig.fmap.mapcontext.layers.FLayer;
40
import org.gvsig.fmap.mapcontext.layers.FLayers;
41
import org.gvsig.fmap.mapcontext.layers.LayerFactory;
42
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
43
import org.gvsig.fmap.mapcontrol.MapControl;
44

    
45
import com.iver.andami.PluginServices;
46
import com.iver.andami.plugins.Extension;
47
import com.iver.cit.gvsig.addlayer.AddLayerDialog;
48
import com.iver.cit.gvsig.addlayer.fileopen.FileOpenWizard;
49
import com.iver.cit.gvsig.addlayer.fileopen.vectorial.VectorialFileOpen;
50
import com.iver.cit.gvsig.gui.WizardPanel;
51
import com.iver.cit.gvsig.project.documents.ProjectDocument;
52
import com.iver.cit.gvsig.project.documents.view.gui.BaseView;
53
import com.iver.cit.gvsig.project.documents.view.gui.IView;
54
import com.iver.utiles.extensionPoints.ExtensionPoints;
55
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton;
56

    
57

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

    
67
        private DALLibrary dalLibrary;
68

    
69
        private DALFileLibrary dalFileLibrary;
70

    
71
        private DXFLibrary dxf;
72

    
73
        private DBFLibrary dbf;
74

    
75
        private SHPLibrary shp;
76

    
77
        private static ArrayList wizardStack = null;
78

    
79
        static {
80
                AddLayer.wizardStack = new ArrayList();
81
                // A�adimos el panel al wizard de cargar capa. (Esto es temporal hasta que
82
    // el actual sea totalmente extensible)
83
                AddLayer.addWizard(FileOpenWizard.class);
84
        }
85

    
86
        public static void addWizard(Class wpClass) {
87
                AddLayer.wizardStack.add(wpClass);
88
        }
89

    
90
        public static WizardPanel getInstance(int i)
91
                        throws IllegalArgumentException, SecurityException,
92
                        InstantiationException, IllegalAccessException,
93
                        InvocationTargetException, NoSuchMethodException {
94
                Class wpClass = (Class) AddLayer.wizardStack.get(i);
95
                Class[] args = {};
96
                Object[] params = {};
97
                WizardPanel wp = (WizardPanel) wpClass.getConstructor(args)
98
                                .newInstance(params);
99

    
100
                wp.initWizard();
101

    
102
                return wp;
103
        }
104

    
105
        /**
106
         * @see com.iver.mdiApp.plugins.IExtension#isVisible()
107
         */
108
        public boolean isVisible() {
109
                com.iver.andami.ui.mdiManager.IWindow window = PluginServices.getMDIManager()
110
                                                                                                                         .getActiveWindow();
111

    
112
                if (window == null) {
113
                        return false;
114
                }
115

    
116
                // Any view derived from BaseView should have AddLayer available
117

    
118
                IView view;
119
                try {
120
                        view = (IView)window;
121
                }
122
                catch (ClassCastException e) {
123
                    return false;
124
                }
125

    
126
                if (view == null) {
127
                        return false;
128
                }
129

    
130
                BaseView baseView = (BaseView)view;
131
                return (baseView != null);
132
        }
133

    
134
        /**
135
         * @see com.iver.andami.plugins.IExtension#postInitialize()
136
         */
137
        public void postInitialize() {
138
//                LayerFactory.initialize();
139
//                DriverManager dm=LayerFactory.getDM();
140
//                PluginServices.addLoaders(dm.getDriverClassLoaders());
141
//                WriterManager wm=LayerFactory.getWM();
142
//                PluginServices.addLoaders(wm.getWriterClassLoaders());
143

    
144
                dalLibrary.postInitialize();
145
                dalFileLibrary.postInitialize();
146

    
147
                dxf.postInitialize();
148
                dbf.postInitialize();
149
                shp.postInitialize();
150

    
151
                ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
152
                extensionPoints.add("FileExtendingOpenDialog", "FileOpenVectorial", VectorialFileOpen.class);
153
        }
154

    
155
        public static void checkProjection(FLayer lyr, ViewPort viewPort) {
156
                if (lyr instanceof FLayers){
157
                        FLayers layers=(FLayers)lyr;
158
                        for (int i=0;i<layers.getLayersCount();i++){
159
                                checkProjection(layers.getLayer(i),viewPort);
160
                        }
161
                }
162
                if (lyr instanceof FLyrVect) {
163
                        FLyrVect lyrVect = (FLyrVect) lyr;
164
                        IProjection proj = lyr.getProjection();
165
                        // Comprobar que la projecci�n es la misma que la vista
166
                        if (proj == null) {
167
                                // SUPONEMOS que la capa est� en la proyecci�n que
168
                                // estamos pidiendo (que ya es mucho suponer, ya).
169
                                lyrVect.setProjection(viewPort.getProjection());
170
                                return;
171
                        }
172
                        int option = JOptionPane.YES_OPTION;
173
                        if (proj != viewPort.getProjection()) {
174
                                option = JOptionPane.showConfirmDialog((Component)PluginServices.getMainFrame(), PluginServices
175
                                                .getText(AddLayer.class, "reproyectar_aviso")+"\n"+ PluginServices.getText(AddLayer.class,"Capa")+": "+lyrVect.getName(), PluginServices
176
                                                .getText(AddLayer.class, "reproyectar_pregunta"),
177
                                                JOptionPane.YES_NO_OPTION);
178

    
179
                                if (option != JOptionPane.OK_OPTION) {
180
                                        return;
181
                                }
182

    
183
                                ICoordTrans ct = proj.getCT(viewPort.getProjection());
184
                                lyrVect.setCoordTrans(ct);
185
                                System.err.println("coordTrans = " + proj.getAbrev() + " "
186
                                                + viewPort.getProjection().getAbrev());
187
                        }
188
                }
189

    
190
        }
191

    
192
        /**
193
         * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
194
         */
195
        public void execute(String actionCommand) {
196
                // Project project = ((ProjectExtension)
197
                // PluginServices.getExtension(ProjectExtension.class)).getProject();
198
                BaseView theView = (BaseView) PluginServices.getMDIManager().getActiveWindow();
199
                MapControl mapControl=theView.getMapControl();
200
                this.addLayers(mapControl);
201
                mapControl.getMapContext().callLegendChanged();
202
                ((ProjectDocument)theView.getModel()).setModified(true);
203
        }
204

    
205
        /**
206
         * @see com.iver.andami.plugins.IExtension#isEnabled()
207
         */
208
        public boolean isEnabled() {
209
                return true;
210
        }
211

    
212
        /**
213
         * Creates FOpenDialog, and adds file tab, and additional registered tabs
214
         *
215
         * @return FOpenDialog
216
         */
217
        private AddLayerDialog createFOpenDialog() {
218
                fopen = new AddLayerDialog();
219

    
220
                // after that, all registerez tabs (wizardpanels implementations)
221
                for (int i = 0; i < wizardStack.size(); i++) {
222
                        WizardPanel wp;
223
                        try {
224
                                wp = AddLayer.getInstance(i);
225
                                fopen.addWizardTab(wp.getTabName(), wp);
226
                        } catch (IllegalArgumentException e) {
227
                                e.printStackTrace();
228
                        } catch (SecurityException e) {
229
                                e.printStackTrace();
230
                        } catch (InstantiationException e) {
231
                                e.printStackTrace();
232
                        } catch (IllegalAccessException e) {
233
                                e.printStackTrace();
234
                        } catch (InvocationTargetException e) {
235
                                e.printStackTrace();
236
                        } catch (NoSuchMethodException e) {
237
                                e.printStackTrace();
238
                        }
239
                }// for
240
                return fopen;
241
        }
242

    
243
        /**
244
         * Adds to mapcontrol all layers selected by user in the specified WizardPanel.
245
         *
246
         * @param mapControl
247
         *         MapControl on which we want to load user selected layers.
248
         * @param wizardPanel
249
         *         WizardPanel where user selected the layers to load
250
         * @return
251
         */
252
        private boolean loadGenericWizardPanelLayers(MapControl mapControl, WizardPanel wp) {
253
                wp.setMapCtrl(mapControl);
254
                wp.execute();
255
                IProjection proj = null;
256
                DBStoreParameters[] parameters = (DBStoreParameters[]) wp
257
                                .getParameters();
258
                if (parameters==null) {
259
                        return true;
260
                }
261
                FLayer[] all_layers = new FLayer[parameters.length];
262
                String strEPSG = mapControl.getViewPort().getProjection()
263
                .getAbrev();
264
                LayerFactory layerFactory = LayerFactory.getInstance();
265
                String groupName = null;
266
                for (int i = 0; i < parameters.length; i++) {
267
                        if (i==0){
268
                                groupName = parameters[i].getDb() + " (" +
269
                            parameters[i].getHost() + ")";
270
                        }
271
                         try {
272
                                all_layers[i] = layerFactory.createLayer(
273
                                                        parameters[i]
274
                                                .tableID(), parameters[i],
275
                                            proj);
276
                        } catch (LoadLayerException e) {
277
                                // TODO Auto-generated catch block
278
                                e.printStackTrace();
279
                        }
280
                }
281

    
282
        MapContext mc = mapControl.getMapContext();
283
        FLayers root = mapControl.getMapContext().getLayers();
284
        if (all_layers.length>1){
285
        FLayers group = new FLayers();//(mc,root);
286
                group.setMapContext(mc);
287
                group.setParentLayer(root);
288
        group.setName(groupName);
289

    
290
        for (int i = 0; i < all_layers.length; i++) {
291
            group.addLayer(all_layers[i]);
292
        }
293

    
294
        if((group != null) && !(group.isOk())){
295
                        //if the layer is not okay (it has errors) process them
296
                        processErrorsOfLayer(group, mapControl);
297
                }
298

    
299
                if (group != null) {
300
                        group.setVisible(true);
301
                        mapControl.getMapContext().beginAtomicEvent();
302
                        checkProjection(group, mapControl.getViewPort());
303
                        try {
304
                                mapControl.getMapContext().getLayers().addLayer(group);
305
                        } catch (CancelationException e) {
306
                                // TODO Auto-generated catch block
307
                                e.printStackTrace();
308
                        }
309
                        mapControl.getMapContext().endAtomicEvent();
310
                        return true;
311
                }
312
        }else{
313
                 if((all_layers[0] != null) && !(all_layers[0].isOk())){
314
                             //if the layer is not okay (it has errors) process them
315
                             processErrorsOfLayer(all_layers[0], mapControl);
316
                     }
317

    
318
                     if (all_layers[0] != null) {
319
                             all_layers[0].setVisible(true);
320
                             mapControl.getMapContext().beginAtomicEvent();
321
                             checkProjection(all_layers[0], mapControl.getViewPort());
322
                             try {
323
                                     mapControl.getMapContext().getLayers().addLayer(all_layers[0]);
324
                             } catch (CancelationException e) {
325
                                     // TODO Auto-generated catch block
326
                                     e.printStackTrace();
327
                             }
328
                             mapControl.getMapContext().endAtomicEvent();
329
                             return true;
330
                     }
331
        }
332
                return false;
333
        }
334

    
335
        /**
336
         * This method process the errors found in a layer
337
         * @param lyr
338
         * @param mapControl
339
         */
340

    
341
        private void processErrorsOfLayer(FLayer lyr, MapControl mapControl){
342
//                List errors = lyr.getErrors();
343
//                wp.callError(null);
344
                mapControl.getMapContext().callNewErrorEvent(null);
345
        }
346

    
347
        /**
348
         * Abre dialogo para a�adir capas y las a�ade en mapControl
349
         *
350
         * Devuelve true si se han a�adido capas.
351
         */
352
        public boolean addLayers(MapControl mapControl) {
353
                // create and show the modal fopen dialog
354
                fopen = createFOpenDialog();
355
                PluginServices.getMDIManager().addWindow(fopen);
356

    
357
                if (fopen.isAccepted()) {
358
                                if (fopen.getSelectedTab() instanceof WizardPanel) {
359
                                WizardPanel wp = (WizardPanel) fopen.getSelectedTab();
360
                                return loadGenericWizardPanelLayers(mapControl, wp);
361
                        } else {
362
                                JOptionPane.showMessageDialog((Component) PluginServices
363
                                                .getMainFrame(), PluginServices.getText(this,"ninguna_capa_seleccionada"));
364
                        }
365
                }
366
                return false;
367
        }
368

    
369
        /* (non-Javadoc)
370
         * @see com.iver.andami.plugins.IExtension#initialize()
371
         */
372
        public void initialize() {
373
                //Listener para resolver un FileDriverNotFoundException
374
//                LayerFactory.addSolveErrorForLayer(FileNotFoundDriverException.class,new FileNotFoundSolve());
375

    
376
                dalLibrary = new DALLibrary();
377
                dalLibrary.initialize();
378

    
379
                dalFileLibrary = new DALFileLibrary();
380
                dalFileLibrary.initialize();
381

    
382
                dxf = new DXFLibrary();
383
                dxf.initialize();
384

    
385
                dbf = new DBFLibrary();
386
                dbf.initialize();
387

    
388
                shp = new SHPLibrary();
389
                shp.initialize();
390

    
391

    
392
                //                Register.selfRegister();
393
                //                org.gvsig.fmap.data.feature.file.dgn.Register.selfRegister();
394
                //                org.gvsig.fmap.data.feature.file.dgn.operation.Register.selfRegister();
395
                //                org.gvsig.fmap.data.feature.file.dxf.Register.selfRegister();
396
                //                org.gvsig.fmap.data.feature.file.dxf.operation.Register.selfRegister();
397
                //                org.gvsig.fmap.data.feature.file.shp.Register.selfRegister();
398
                //                org.gvsig.fmap.data.feature.db.jdbc.postgresql.Register.selfRegister();
399
                //                org.gvsig.fmap.data.feature.db.jdbc.h2.Register.selfRegister();
400
                PluginServices.getIconTheme().registerDefault(
401
                                "layer-add",
402
                                this.getClass().getClassLoader().getResource("images/addlayer.png")
403
                        );
404

    
405
        }
406
}