Statistics
| Revision:

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

History | View | Annotate | Download (11.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
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig;
42

    
43
import java.awt.Component;
44
import java.awt.geom.Rectangle2D;
45
import java.io.File;
46
import java.lang.reflect.InvocationTargetException;
47
import java.util.ArrayList;
48

    
49
import javax.swing.JOptionPane;
50

    
51
import org.cresques.cts.ICoordTrans;
52
import org.cresques.cts.IProjection;
53
import org.cresques.cts.gt2.CoordSys;
54
import org.cresques.cts.gt2.CoordTrans;
55
import org.opengis.layer.Layer;
56

    
57
import com.hardcode.driverManager.Driver;
58
import com.hardcode.driverManager.DriverLoadException;
59
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
60
import com.iver.andami.PluginServices;
61
import com.iver.andami.messages.NotificationManager;
62
import com.iver.andami.plugins.Extension;
63
import com.iver.cit.gvsig.fmap.DriverException;
64
import com.iver.cit.gvsig.fmap.MapControl;
65
import com.iver.cit.gvsig.fmap.ViewPort;
66
import com.iver.cit.gvsig.fmap.drivers.RasterDriver;
67
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
68
import com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend;
69
import com.iver.cit.gvsig.fmap.layers.FLayer;
70
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
71
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
72
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
73
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
74
import com.iver.cit.gvsig.gui.FOpenDialog;
75
import com.iver.cit.gvsig.gui.FileOpenDialog;
76
import com.iver.cit.gvsig.gui.View;
77
import com.iver.cit.gvsig.gui.WizardPanel;
78

    
79

    
80
/**
81
 * Extensi?n que abre un di?logo para seleccionar la capa o capas que se
82
 * quieren a?adir a la vista.
83
 *
84
 * @author Fernando Gonz?lez Cort?s
85
 */
86
public class AddLayer extends Extension {
87
        public FOpenDialog fopen = null;
88
        
89
        private static ArrayList wizardStack = null;
90
        
91
        static {
92
                AddLayer.wizardStack = new ArrayList();
93
        }
94
        
95
        public static void addWizard(Class wpClass) {
96
                AddLayer.wizardStack.add(wpClass);
97
        }
98
        
99
        public static WizardPanel getInstance(int i) throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
100
                Class wpClass = (Class) AddLayer.wizardStack.get(i);
101
                Class [] args = {};
102
                Object [] params = {};
103
                WizardPanel wp = (WizardPanel) wpClass.getConstructor(args).newInstance(params);
104

    
105
                wp.initWizard();
106
                
107
                return wp;
108
        }
109

    
110

    
111
        /**
112
         * @see com.iver.mdiApp.plugins.IExtension#isVisible()
113
         */
114
        public boolean isVisible() {
115
                com.iver.andami.ui.mdiManager.View f = PluginServices.getMDIManager()
116
                                                                                                                         .getActiveView();
117

    
118
                if (f == null) {
119
                        return false;
120
                }
121

    
122
                return (f.getClass() == View.class);
123
        }
124

    
125
        /**
126
         * @see com.iver.andami.plugins.IExtension#initialize()
127
         */
128
        public void initialize() {
129
        }
130

    
131
    private void checkProjection(FLayer lyr, ViewPort viewPort)
132
    {
133
        if (lyr instanceof FLyrVect)
134
        {
135
            FLyrVect lyrVect = (FLyrVect) lyr;
136
            IProjection proj = lyr.getProjection();
137
            // Comprobar que la projecci?n es la misma que la vista
138
            if (proj == null)
139
            {
140
                // SUPONEMOS que la capa est? en la proyecci?n que 
141
                // estamos pidiendo (que ya es mucho suponer, ya).
142
                lyrVect.setProjection(viewPort.getProjection());
143
                return;
144
            }
145
            if (proj != viewPort.getProjection()) {
146
                int option = JOptionPane.showConfirmDialog(null,
147
                        PluginServices.getText(this, "reproyectar_aviso"),
148
                        PluginServices.getText(this, "reproyectar_pregunta"),
149
                        JOptionPane.YES_NO_OPTION);
150

    
151
                if (option == JOptionPane.NO_OPTION) {
152
                    return;
153
                } else {
154
                    ICoordTrans ct = new CoordTrans((CoordSys) proj,
155
                            (CoordSys) viewPort.getProjection());
156
                    lyrVect.setCoordTrans(ct);
157
                    System.err.println("coordTrans = " +
158
                        proj.getAbrev() + " " +
159
                        viewPort.getProjection().getAbrev());
160
                }
161
            }
162
        }                    
163

    
164
    }
165
        /**
166
         * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
167
         */
168
        public void execute(String actionCommand) {
169
                //Project project = ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).getProject();
170
                View theView = (View) PluginServices.getMDIManager().getActiveView();
171
                
172
                this.addLayers(theView.getMapControl());
173
                return;
174

    
175
        }
176

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

    
186
        /**
187
         * Abre dialogo para a?adir capas y las a?ade
188
         * en mapControl
189
         * 
190
         * Devuelve true si se han a?adido capas.
191
         */        
192
        public boolean addLayers(MapControl mapControl) {                
193
                fopen = new FOpenDialog();
194

    
195
                FileOpenDialog fileDlg = new FileOpenDialog(new Class[] {
196
                                        VectorialFileDriver.class, RasterDriver.class
197
                                });
198
                fopen.addTab(PluginServices.getText(this, "Fichero"), fileDlg);
199
                for (int i=0; i<wizardStack.size(); i++) {
200
                        WizardPanel wp;
201
                        try {
202
                                wp = AddLayer.getInstance(i);
203
                                fopen.addWizardTab(wp.getTabName(), wp);                        
204
                        } catch (IllegalArgumentException e) {
205
                                e.printStackTrace();
206
                        } catch (SecurityException e) {
207
                                e.printStackTrace();
208
                        } catch (InstantiationException e) {
209
                                e.printStackTrace();
210
                        } catch (IllegalAccessException e) {
211
                                e.printStackTrace();
212
                        } catch (InvocationTargetException e) {
213
                                e.printStackTrace();
214
                        } catch (NoSuchMethodException e) {
215
                                e.printStackTrace();
216
                        }
217
                }
218
                
219
                PluginServices.getMDIManager().addView(fopen);
220

    
221
                if (fopen.isAccepted()) {
222
                        FLayer lyr = null;
223

    
224
                        if (fopen.getSelectedTab() == fileDlg) {
225
                                if (fileDlg.getFiles() == null) {
226
                                        return false;
227
                                }
228

    
229
                                IProjection proj = FOpenDialog.getLastProjection();
230
                                File[] files = fileDlg.getFiles();
231
                                String[] driverNames = fileDlg.getDriverNames();
232
                                Driver[] drivers = new Driver[driverNames.length];
233
                                for (int i = 0; i < drivers.length; i++) {
234
                                        try {
235
                                                drivers[i] = LayerFactory.getDM().getDriver(driverNames[i]);
236
                                        } catch (DriverLoadException e) {
237
                                                NotificationManager.addError("No se pudo cargar el driver", e);
238
                                        }
239
                                }
240
                                Rectangle2D[] rects=new Rectangle2D[files.length];
241
                                boolean first=false;
242
                                mapControl.getMapContext()
243
                                   .beginAtomicEvent();
244

    
245
                                for (int iFile = 0; iFile < files.length; iFile++) {
246
                                        File fich = files[iFile];
247
                                        String layerName = fich.getName();
248
                                        //String layerPath = fich.getAbsolutePath();
249

    
250
                                        try {
251
                                                // FJP: Comento esto (if (fileDlg.accept(fich))) para resolver el bug 75. ?Esto estaba antes
252
                        // o lo apuesto alguien por algo en concreto?.
253
                                                // if (fileDlg.accept(fich))
254
                                                    if (drivers[iFile] instanceof VectorialFileDriver){
255
                                                            lyr = LayerFactory.createLayer(layerName,
256
                                                                            (VectorialFileDriver) drivers[iFile], fich, proj);
257
                                                    }else if (drivers[iFile] instanceof RasterDriver){
258
                                                            lyr = LayerFactory.createLayer(layerName,
259
                                                                            (RasterDriver) drivers[iFile], fich, proj);
260
                                                    }
261

    
262
                                                if (lyr != null) {
263
                                                        lyr.setVisible(true);
264
                                                        if (mapControl.getMapContext().getViewPort().getExtent()==null){
265
                                                                first=true;
266
                                                        }
267

    
268

    
269
                            
270
                            try {
271
                                
272
                                // Le asignamos tambi?n una legenda por defecto acorde con
273
                                // el tipo de shape que tenga. Tampoco s? si es aqu? el
274
                                // sitio adecuado, pero en fin....
275
                                checkProjection(lyr, mapControl.getViewPort());
276
                                mapControl.getMapContext().getLayers()
277
                                   .addLayer(lyr);
278
                                
279
                                if (lyr instanceof FLyrVect)
280
                                {
281
                                    FLyrVect lyrVect = (FLyrVect) lyr;
282
                                    if (drivers[iFile] instanceof WithDefaultLegend) {
283
                                        WithDefaultLegend aux = (WithDefaultLegend) drivers[iFile];
284
                                        lyrVect.setLegend((VectorialLegend) aux.getDefaultLegend());                                        
285
                                    } else {
286
                                        lyrVect.setLegend(LegendFactory.createSingleSymbolLegend(
287
                                                lyrVect.getShapeType()));
288
                                    }
289
                                }
290
                            } catch (FieldNotFoundException e) {
291
                                //Esta no puede saltar
292
                            }                            
293
                                                        rects[iFile]=lyr.getFullExtent();
294
                                                        
295
                                                        // TODO: Poner una variable y dibujar solo cuando
296
                                                        // todas las capas hayan sido cargadas.
297
                                                        // TODO Se deber? de redibujar mediante la captura de los eventos, por
298
                                                        //eso se comenta la parte anterior
299
                                                        //                                                        theView.getMapControl().drawMap();
300
                                                        //                                                        theView.getTOC().refresh();
301
                                                }
302

    
303
                                        } catch (DriverException e) {
304
                                                NotificationManager.addError("Error al crear la capa", e);
305
                                        }
306
                                }
307

    
308
                                //Esto permite que cuando se cargan varias capas de golpe y la vista est? vacia,se ponga como extent la suma de todos sus extents.
309
                                if (rects.length > 1) {
310
                                        Rectangle2D rect = new Rectangle2D.Double();
311
                                        rect.setRect(rects[0]);
312

    
313
                                        if (first) {
314
                                                for (int i = 0; i < rects.length; i++) {
315
                                                        rect.add(rects[i]);
316
                                                }
317

    
318
                                                mapControl.getMapContext().getViewPort()
319
                                                           .setExtent(rect);
320
                                        }
321
                                }
322
                                mapControl.getMapContext()
323
                                   .endAtomicEvent();
324
                                return true;
325

    
326
                        } else if (fopen.getSelectedTab() instanceof WizardPanel) {
327
                                WizardPanel wp = (WizardPanel) fopen.getSelectedTab();
328
                                wp.setMapCtrl(mapControl);
329
                                wp.execute();
330
                                lyr = wp.getLayer();
331
                                
332
                                if (lyr != null) {
333
                                        lyr.setVisible(true);
334
                                        mapControl.getMapContext().beginAtomicEvent();
335
                    checkProjection(lyr, mapControl.getViewPort());
336
                                        mapControl.getMapContext().getLayers()
337
                                                   .addLayer(lyr);
338
                                        mapControl.getMapContext().endAtomicEvent();
339
                                        return true;
340
                    
341
                                }
342
                        } else {
343
                                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),"Ninguna capa seleccionada");
344
                        }
345
                }
346
                return false;
347
        }
348
}