Statistics
| Revision:

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

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

    
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
     * @param mapControl
195
     * 
196
     * @return FOpenDialog
197
     */
198
        private AddLayerDialog createFOpenDialog(MapControl mapControl) {
199
                fopen = new AddLayerDialog();
200

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

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

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

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

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

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

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

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

    
292
                }
293
                return false;
294
        }
295

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

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

    
308
        }
309
}
310

    
311
// [eiel-gestion-conexiones]