Statistics
| Revision:

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

History | View | Annotate | Download (11.5 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.serverexplorer.filesystem.swing.FilesystemExplorerAddViewWizardPanel;
30
import org.gvsig.fmap.mapcontext.ViewPort;
31
import org.gvsig.fmap.mapcontext.layers.FLayer;
32
import org.gvsig.fmap.mapcontext.layers.FLayers;
33
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
34
import org.gvsig.fmap.mapcontrol.MapControl;
35

    
36
import com.iver.andami.PluginServices;
37
import com.iver.andami.plugins.Extension;
38
import com.iver.cit.gvsig.addlayer.AddLayerDialog;
39
import com.iver.cit.gvsig.gui.WizardPanel;
40
import com.iver.cit.gvsig.project.documents.ProjectDocument;
41
import com.iver.cit.gvsig.project.documents.view.gui.BaseView;
42
import com.iver.cit.gvsig.project.documents.view.gui.IView;
43

    
44

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

    
54
        private static ArrayList<Class<? extends WizardPanel>> wizardStack = null;
55

    
56
        static {
57
                AddLayer.wizardStack = new ArrayList<Class<? extends WizardPanel>>();
58
                // A�adimos el panel al wizard de cargar capa. (Esto es temporal hasta que
59
    // el actual sea totalmente extensible)
60
                //                AddLayer.addWizard(FileOpenWizard.class);
61
                AddLayer.addWizard(FilesystemExplorerAddViewWizardPanel.class);
62
        }
63

    
64
        public static void addWizard(Class<? extends WizardPanel> wpClass) {
65
                AddLayer.wizardStack.add(wpClass);
66
        }
67

    
68
        public static WizardPanel getInstance(int i)
69
                        throws IllegalArgumentException, SecurityException,
70
                        InstantiationException, IllegalAccessException,
71
                        InvocationTargetException, NoSuchMethodException {
72
                Class<? extends WizardPanel> wpClass = AddLayer.wizardStack.get(i);
73
                Object[] params = {};
74
                WizardPanel wp = wpClass.getConstructor()
75
                                .newInstance(params);
76

    
77
                wp.initWizard();
78

    
79
                return wp;
80
        }
81

    
82
        /**
83
         * @see com.iver.mdiApp.plugins.IExtension#isVisible()
84
         */
85
        public boolean isVisible() {
86
                com.iver.andami.ui.mdiManager.IWindow window = PluginServices.getMDIManager()
87
                                                                                                                         .getActiveWindow();
88

    
89
                if (window == null) {
90
                        return false;
91
                }
92

    
93
                // Any view derived from BaseView should have AddLayer available
94

    
95
                IView view;
96
                try {
97
                        view = (IView)window;
98
                }
99
                catch (ClassCastException e) {
100
                    return false;
101
                }
102

    
103
                if (view == null) {
104
                        return false;
105
                }
106

    
107
                BaseView baseView = (BaseView)view;
108
                return (baseView != null);
109
        }
110

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

    
121
                //                ToolsLocator.getExtensionPointManager().add("FileExtendingOpenDialog",
122
                //                                "").append("FileOpenVectorial", "", VectorialFileOpen.class);
123
        }
124

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

    
149
                                if (option != JOptionPane.OK_OPTION) {
150
                                        return;
151
                                }
152

    
153
                                ICoordTrans ct = proj.getCT(viewPort.getProjection());
154
                                lyrVect.setCoordTrans(ct);
155
                                System.err.println("coordTrans = " + proj.getAbrev() + " "
156
                                                + viewPort.getProjection().getAbrev());
157
                        }
158
                }
159

    
160
        }
161

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

    
175
        /**
176
         * @see com.iver.andami.plugins.IExtension#isEnabled()
177
         */
178
        public boolean isEnabled() {
179
                return true;
180
        }
181

    
182
        /**
183
         * Creates FOpenDialog, and adds file tab, and additional registered tabs
184
         *
185
         * @return FOpenDialog
186
         */
187
        private AddLayerDialog createFOpenDialog() {
188
                fopen = new AddLayerDialog();
189

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

    
213
        /**
214
         * Adds to mapcontrol all layers selected by user in the specified WizardPanel.
215
         *
216
         * @param mapControl
217
         *         MapControl on which we want to load user selected layers.
218
         * @param wizardPanel
219
         *         WizardPanel where user selected the layers to load
220
         * @return
221
         */
222
        private boolean loadGenericWizardPanelLayers(MapControl mapControl, WizardPanel wp) {
223
                wp.setMapCtrl(mapControl);
224
                wp.execute();
225
//                FIXME
226
//                IProjection proj = null;
227
//                DBStoreParameters[] parameters = (DBStoreParameters[]) wp
228
//                                .getParameters();
229
//                if (parameters==null) {
230
//                        return true;
231
//                }
232
//                FLayer[] all_layers = new FLayer[parameters.length];
233
//                String strEPSG = mapControl.getViewPort().getProjection()
234
//                .getAbrev();
235
//                LayerFactory layerFactory = LayerFactory.getInstance();
236
//                String groupName = null;
237
//                for (int i = 0; i < parameters.length; i++) {
238
//                        if (i==0){
239
//                                groupName = parameters[i].getDb() + " (" +
240
//                            parameters[i].getHost() + ")";
241
//                        }
242
//                         try {
243
//                                all_layers[i] = layerFactory.createLayer(
244
//                                                        parameters[i]
245
//                                                .tableID(), parameters[i],
246
//                                            proj);
247
//                        } catch (LoadLayerException e) {
248
//                                // TODO Auto-generated catch block
249
//                                e.printStackTrace();
250
//                        }
251
//                }
252
//
253
//        MapContext mc = mapControl.getMapContext();
254
//        FLayers root = mapControl.getMapContext().getLayers();
255
//        if (all_layers.length>1){
256
//        FLayers group = new FLayers();//(mc,root);
257
//                group.setMapContext(mc);
258
//                group.setParentLayer(root);
259
//        group.setName(groupName);
260
//
261
//        for (int i = 0; i < all_layers.length; i++) {
262
//            group.addLayer(all_layers[i]);
263
//        }
264
//
265
//        if((group != null) && !(group.isOk())){
266
//                        //if the layer is not okay (it has errors) process them
267
//                        processErrorsOfLayer(group, mapControl);
268
//                }
269
//
270
//                if (group != null) {
271
//                        group.setVisible(true);
272
//                        mapControl.getMapContext().beginAtomicEvent();
273
//                        checkProjection(group, mapControl.getViewPort());
274
//                        try {
275
//                                mapControl.getMapContext().getLayers().addLayer(group);
276
//                        } catch (CancelationException e) {
277
//                                // TODO Auto-generated catch block
278
//                                e.printStackTrace();
279
//                        }
280
//                        mapControl.getMapContext().endAtomicEvent();
281
//                        return true;
282
//                }
283
//        }else{
284
//                 if((all_layers[0] != null) && !(all_layers[0].isOk())){
285
//                             //if the layer is not okay (it has errors) process them
286
//                             processErrorsOfLayer(all_layers[0], mapControl);
287
//                     }
288
//
289
//                     if (all_layers[0] != null) {
290
//                             all_layers[0].setVisible(true);
291
//                             mapControl.getMapContext().beginAtomicEvent();
292
//                             checkProjection(all_layers[0], mapControl.getViewPort());
293
//                             try {
294
//                                     mapControl.getMapContext().getLayers().addLayer(all_layers[0]);
295
//                             } catch (CancelationException e) {
296
//                                     // TODO Auto-generated catch block
297
//                                     e.printStackTrace();
298
//                             }
299
//                             mapControl.getMapContext().endAtomicEvent();
300
//                             return true;
301
//                     }
302
//        }
303
                return true;
304
        }
305

    
306
        /**
307
         * This method process the errors found in a layer
308
         * @param lyr
309
         * @param mapControl
310
         */
311

    
312
        private void processErrorsOfLayer(FLayer lyr, MapControl mapControl){
313
//                List errors = lyr.getErrors();
314
//                wp.callError(null);
315
                mapControl.getMapContext().callNewErrorEvent(null);
316
        }
317

    
318
        /**
319
         * Abre dialogo para a�adir capas y las a�ade en mapControl
320
         *
321
         * Devuelve true si se han a�adido capas.
322
         */
323
        public boolean addLayers(MapControl mapControl) {
324
                // create and show the modal fopen dialog
325
                fopen = createFOpenDialog();
326
                PluginServices.getMDIManager().addWindow(fopen);
327

    
328
                if (fopen.isAccepted()) {
329
                                if (fopen.getSelectedTab() instanceof WizardPanel) {
330
                                WizardPanel wp = (WizardPanel) fopen.getSelectedTab();
331
                                return loadGenericWizardPanelLayers(mapControl, wp);
332
                        } else {
333
                                JOptionPane.showMessageDialog((Component) PluginServices
334
                                                .getMainFrame(), PluginServices.getText(this,"ninguna_capa_seleccionada"));
335
                        }
336
                }
337
                return false;
338
        }
339

    
340
        /* (non-Javadoc)
341
         * @see com.iver.andami.plugins.IExtension#initialize()
342
         */
343
        public void initialize() {
344
                //Listener para resolver un FileDriverNotFoundException
345
//                LayerFactory.addSolveErrorForLayer(FileNotFoundDriverException.class,new FileNotFoundSolve());
346

    
347

    
348

    
349
                //                Register.selfRegister();
350
                //                org.gvsig.fmap.data.feature.file.dgn.Register.selfRegister();
351
                //                org.gvsig.fmap.data.feature.file.dgn.operation.Register.selfRegister();
352
                //                org.gvsig.fmap.data.feature.file.dxf.Register.selfRegister();
353
                //                org.gvsig.fmap.data.feature.file.dxf.operation.Register.selfRegister();
354
                //                org.gvsig.fmap.data.feature.file.shp.Register.selfRegister();
355
                //                org.gvsig.fmap.data.feature.db.jdbc.postgresql.Register.selfRegister();
356
                //                org.gvsig.fmap.data.feature.db.jdbc.h2.Register.selfRegister();
357
                PluginServices.getIconTheme().registerDefault(
358
                                "layer-add",
359
                                this.getClass().getClassLoader().getResource("images/addlayer.png")
360
                        );
361

    
362
        }
363
}