Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / AddLayer.java @ 10008

History | View | Annotate | Download (13.8 KB)

1 2268 fjp
/* 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 7476 azabala
import java.util.List;
49 2268 fjp
50
import javax.swing.JOptionPane;
51
52
import org.cresques.cts.ICoordTrans;
53
import org.cresques.cts.IProjection;
54
55
import com.hardcode.driverManager.Driver;
56
import com.hardcode.driverManager.DriverLoadException;
57 9634 caballero
import com.hardcode.driverManager.DriverManager;
58
import com.hardcode.driverManager.WriterManager;
59 2268 fjp
import com.iver.andami.PluginServices;
60
import com.iver.andami.plugins.Extension;
61
import com.iver.cit.gvsig.fmap.DriverException;
62 7476 azabala
import com.iver.cit.gvsig.fmap.DriverIOExceptionType;
63
import com.iver.cit.gvsig.fmap.DriverNotLoadedExceptionType;
64
import com.iver.cit.gvsig.fmap.GenericDriverExceptionType;
65 5190 jmvivo
import com.iver.cit.gvsig.fmap.MapControl;
66 3095 fjp
import com.iver.cit.gvsig.fmap.ViewPort;
67 2268 fjp
import com.iver.cit.gvsig.fmap.drivers.RasterDriver;
68
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
69
import com.iver.cit.gvsig.fmap.layers.FLayer;
70 2318 fjp
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
71 2268 fjp
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
72
import com.iver.cit.gvsig.gui.WizardPanel;
73 9532 caballero
import com.iver.cit.gvsig.project.documents.ProjectDocument;
74 7304 caballero
import com.iver.cit.gvsig.project.documents.gui.FOpenDialog;
75
import com.iver.cit.gvsig.project.documents.gui.FileOpenDialog;
76 7537 sbayarri
import com.iver.cit.gvsig.project.documents.view.gui.BaseView;
77 7738 jaume
import com.iver.cit.gvsig.project.documents.view.gui.IView;
78 2268 fjp
79
80
/**
81 7476 azabala
 * Extensi?n que abre un di?logo para seleccionar la capa o capas que se quieren
82
 * a?adir a la vista.
83 7537 sbayarri
 *
84 2268 fjp
 * @author Fernando Gonz?lez Cort?s
85
 */
86 5005 jorpiell
public class AddLayer extends Extension {
87 2268 fjp
        public FOpenDialog fopen = null;
88 7476 azabala
89 2268 fjp
        private static ArrayList wizardStack = null;
90 7476 azabala
91 2268 fjp
        static {
92
                AddLayer.wizardStack = new ArrayList();
93
        }
94 7476 azabala
95 2268 fjp
        public static void addWizard(Class wpClass) {
96
                AddLayer.wizardStack.add(wpClass);
97
        }
98 7476 azabala
99
        public static WizardPanel getInstance(int i)
100
                        throws IllegalArgumentException, SecurityException,
101
                        InstantiationException, IllegalAccessException,
102
                        InvocationTargetException, NoSuchMethodException {
103 2268 fjp
                Class wpClass = (Class) AddLayer.wizardStack.get(i);
104 7476 azabala
                Class[] args = {};
105
                Object[] params = {};
106
                WizardPanel wp = (WizardPanel) wpClass.getConstructor(args)
107
                                .newInstance(params);
108 2268 fjp
109
                wp.initWizard();
110 7476 azabala
111 2268 fjp
                return wp;
112
        }
113
114
        /**
115 5005 jorpiell
         * @see com.iver.mdiApp.plugins.IExtension#isVisible()
116 2268 fjp
         */
117
        public boolean isVisible() {
118 7537 sbayarri
                com.iver.andami.ui.mdiManager.IWindow window = PluginServices.getMDIManager()
119
                                                                                                                         .getActiveWindow();
120 2268 fjp
121 7537 sbayarri
                if (window == null) {
122 2268 fjp
                        return false;
123
                }
124
125 7537 sbayarri
                // Any view derived from BaseView should have AddLayer available
126
127
                IView view;
128
                try {
129
                        view = (IView)window;
130
                }
131
                catch (ClassCastException e) {
132
                    return false;
133
                }
134
135
                if (view == null)
136
                        return false;
137
138
                BaseView baseView = (BaseView)view;
139
                return (baseView != null);
140 2268 fjp
        }
141
142
        /**
143 9634 caballero
         * @see com.iver.andami.plugins.IExtension#postInitialize()
144 2268 fjp
         */
145 9634 caballero
        public void postInitialize() {
146
                LayerFactory.initialize();
147
                DriverManager dm=LayerFactory.getDM();
148
                PluginServices.addLoaders(dm.getDriverClassLoaders());
149
                WriterManager wm=LayerFactory.getWM();
150
                PluginServices.addLoaders(wm.getWriterClassLoaders());
151 2268 fjp
        }
152
153 7476 azabala
        private void checkProjection(FLayer lyr, ViewPort viewPort) {
154
                if (lyr instanceof FLyrVect) {
155
                        FLyrVect lyrVect = (FLyrVect) lyr;
156
                        IProjection proj = lyr.getProjection();
157
                        // Comprobar que la projecci?n es la misma que la vista
158
                        if (proj == null) {
159
                                // SUPONEMOS que la capa est? en la proyecci?n que
160
                                // estamos pidiendo (que ya es mucho suponer, ya).
161
                                lyrVect.setProjection(viewPort.getProjection());
162
                                return;
163
                        }
164
                        if (proj != viewPort.getProjection()) {
165 8916 caballero
                                int option = JOptionPane.showConfirmDialog((Component)PluginServices.getMainFrame(), PluginServices
166 7476 azabala
                                                .getText(this, "reproyectar_aviso"), PluginServices
167
                                                .getText(this, "reproyectar_pregunta"),
168
                                                JOptionPane.YES_NO_OPTION);
169 3095 fjp
170 8916 caballero
                                if (option != JOptionPane.OK_OPTION) {
171 7476 azabala
                                        return;
172
                                } else {
173
                                        ICoordTrans ct = proj.getCT(viewPort.getProjection());
174
                                        lyrVect.setCoordTrans(ct);
175
                                        System.err.println("coordTrans = " + proj.getAbrev() + " "
176
                                                        + viewPort.getProjection().getAbrev());
177
                                }
178
                        }
179
                }
180 3095 fjp
181 7476 azabala
        }
182
183 2268 fjp
        /**
184 5005 jorpiell
         * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
185 2268 fjp
         */
186
        public void execute(String actionCommand) {
187 7476 azabala
                // Project project = ((ProjectExtension)
188
                // PluginServices.getExtension(ProjectExtension.class)).getProject();
189 7537 sbayarri
                BaseView theView = (BaseView) PluginServices.getMDIManager().getActiveWindow();
190 7675 caballero
                MapControl mapControl=theView.getMapControl();
191
                this.addLayers(mapControl);
192
                mapControl.getMapContext().callLegendChanged();
193 9532 caballero
                ((ProjectDocument)theView.getModel()).setModified(true);
194 5190 jmvivo
        }
195
196
        /**
197
         * @see com.iver.andami.plugins.IExtension#isEnabled()
198
         */
199
        public boolean isEnabled() {
200
                return true;
201
        }
202
203
        /**
204 7476 azabala
         * Creates FOpenDialog, and adds file tab, and additional registered tabs
205 7537 sbayarri
         *
206 7476 azabala
         * @return FOpenDialog
207
         */
208
        private FOpenDialog createFOpenDialog() {
209 2268 fjp
                fopen = new FOpenDialog();
210
                FileOpenDialog fileDlg = new FileOpenDialog(new Class[] {
211 7476 azabala
                                VectorialFileDriver.class, RasterDriver.class });
212
                // first, file wizard tab
213 3437 jmorell
                fopen.addTab(PluginServices.getText(this, "Fichero"), fileDlg);
214 7476 azabala
215
                // after that, all registerez tabs (wizardpanels implementations)
216
                for (int i = 0; i < wizardStack.size(); i++) {
217 2268 fjp
                        WizardPanel wp;
218
                        try {
219
                                wp = AddLayer.getInstance(i);
220 7476 azabala
                                fopen.addWizardTab(wp.getTabName(), wp);
221 2268 fjp
                        } catch (IllegalArgumentException e) {
222
                                e.printStackTrace();
223
                        } catch (SecurityException e) {
224
                                e.printStackTrace();
225
                        } catch (InstantiationException e) {
226
                                e.printStackTrace();
227
                        } catch (IllegalAccessException e) {
228
                                e.printStackTrace();
229
                        } catch (InvocationTargetException e) {
230
                                e.printStackTrace();
231
                        } catch (NoSuchMethodException e) {
232
                                e.printStackTrace();
233
                        }
234 7476 azabala
                }// for
235
                return fopen;
236
        }
237
238
        /**
239
         * Adds to mapcontrol all the file based layers selected by user in
240
         * fileOpenDialog  instance.
241 7537 sbayarri
         *
242 7476 azabala
         * @param mapControl
243
         *            MapControl where we want to add the selected layers
244 7537 sbayarri
         *
245 7476 azabala
         * @param fileDlg
246
         *            FileOpenDialog where user selected file based layers
247 7537 sbayarri
         *
248 7476 azabala
         * @return boolean flag to report sucess of the operation
249
         */
250
        private boolean loadFileLayers(MapControl mapControl, FileOpenDialog fileDlg) {
251
252
                if (fileDlg.getFiles() == null) {
253
                        return false;
254 2268 fjp
                }
255 7476 azabala
256
                FLayer lyr = null;
257
                IProjection proj = FOpenDialog.getLastProjection();
258
                File[] files = fileDlg.getFiles();
259
                String[] driverNames = fileDlg.getDriverNames();
260
                Driver[] drivers = new Driver[driverNames.length];
261
262
                //all catched errors will be saved here, to show user at the end
263
                //of the method
264
                ArrayList errors = new ArrayList();
265 7537 sbayarri
266
267 7476 azabala
                // try to load the drivers referenced by the file dialog
268
                for (int i = 0; i < drivers.length; i++) {
269
                        try {
270
                                drivers[i] = LayerFactory.getDM().getDriver(driverNames[i]);
271
                        } catch (DriverLoadException e) {
272
                                DriverNotLoadedExceptionType type = new DriverNotLoadedExceptionType();
273
                                type.setDriverName(driverNames[i]);
274
                                DriverException exception = new DriverException(e, type);
275
                                errors.add(exception);
276
//                                NotificationManager.addError("No se pudo cargar el driver", e);
277
                        }
278
                }
279 7537 sbayarri
280
281 7476 azabala
                // Envelope de cada fichero seleccionado por el usuario
282
                Rectangle2D[] rects = new Rectangle2D[files.length];
283
                boolean first = false;
284 2268 fjp
285 7476 azabala
                // A?adir capas al mapControl se trata como una transaccion
286
                mapControl.getMapContext().beginAtomicEvent();
287 2268 fjp
288 7476 azabala
                for (int iFile = 0; iFile < files.length; iFile++) {
289
                        File fich = files[iFile];
290
                        String layerName = fich.getName();
291
                        try {
292
// FJP: Comento esto (if (fileDlg.accept(fich))) para resolver
293
// el bug 75. ?Esto estaba antes
294
// o lo apuesto alguien por algo en concreto?.
295
// if (fileDlg.accept(fich))
296
297
                                if (drivers[iFile] instanceof VectorialFileDriver) {
298
                                        lyr = LayerFactory.createLayer(layerName,
299
                                                        (VectorialFileDriver) drivers[iFile], fich, proj);
300
                                } else if (drivers[iFile] instanceof RasterDriver) {
301
                                        lyr = LayerFactory.createLayer(layerName,
302
                                                        (RasterDriver) drivers[iFile], fich, proj);
303 2268 fjp
                                }
304
305 7476 azabala
                                if (lyr != null) {
306
                                        lyr.setVisible(true);
307
                                        if (mapControl.getMapContext().getViewPort().getExtent() == null) {
308
                                                first = true;
309 2268 fjp
                                        }
310 7476 azabala
                                        checkProjection(lyr, mapControl.getViewPort());
311
                                        mapControl.getMapContext().getLayers().addLayer(lyr);
312 2268 fjp
313 7476 azabala
        //esto ya se hace en layerfactory ?lo dejamos? (azabala)
314
        /*
315
        if (lyr instanceof FLyrVect) {
316
                FLyrVect lyrVect = (FLyrVect) lyr;
317
                if (drivers[iFile] instanceof WithDefaultLegend) {
318
                        WithDefaultLegend aux = (WithDefaultLegend) drivers[iFile];
319
                        lyrVect.setLegend((VectorialLegend) aux
320
                                        .getDefaultLegend());
321
                } else {
322
                        lyrVect.setLegend(LegendFactory
323
                                        .createSingleSymbolLegend(lyrVect
324
                                                        .getShapeType()));
325
                }
326
        }// if
327
        */
328
                                        rects[iFile] = lyr.getFullExtent();
329 2268 fjp
330 7476 azabala
                                        // TODO: Poner una variable y dibujar solo cuando
331
                                        // todas las capas hayan sido cargadas.
332 7537 sbayarri
333 7476 azabala
                                        // TODO Se deber? de redibujar mediante la captura de los
334
                                        // eventos, por
335
                                        // eso se comenta la parte anterior
336
                                        // theView.getMapControl().drawMap();
337
                                        // theView.getTOC().refresh();
338 7537 sbayarri
339 7476 azabala
                                }// if
340 2268 fjp
341 7476 azabala
                        } catch (DriverException e) {
342
                                //This exception is produced in the layer.getFullExtent() call
343
                                DriverIOExceptionType type =
344
                                        new DriverIOExceptionType();
345
                                type.setFile(fich);
346
                                DriverException newException = new DriverException(e, type);
347
                                lyr.addError(newException);
348
                                lyr.setAvailable(false);
349
//                                NotificationManager.addError("Error al crear la capa", e);
350 7537 sbayarri
351 7476 azabala
                                errors.add(newException);
352 2268 fjp
353 7476 azabala
                        }
354 7537 sbayarri
355
356
        /* si no seteamos la leyenda, pq ya se hace en el factory, esto quitarlo (azabala)
357 7476 azabala
                        catch (FieldNotFoundException e) {
358
                                // Esta no puede saltar
359 7537 sbayarri
                        }
360
        */
361
362
363 7476 azabala
                        catch (Exception e) {
364
//azabala: usamos el notificationManager?? a mi no me gusta demasiado
365
//NotificationManager.addError("Error al crear la capa", e);
366
                                //we catch any possible error
367
                                GenericDriverExceptionType type = new GenericDriverExceptionType();
368
                                DriverException exception = new DriverException(e, type);
369
                                lyr.addError(exception);
370
                                lyr.setAvailable(false);
371 7537 sbayarri
372 7476 azabala
                                errors.add(exception);
373
                        }
374
                }// for
375 2268 fjp
376 7537 sbayarri
377
378 7476 azabala
                //now, if it is the first time, we put the extent of the mapcontrol
379
                //to the global extent of all the loaded layers.
380
                if (rects.length > 1) {
381
                        Rectangle2D rect = new Rectangle2D.Double();
382
                        rect.setRect(rects[0]);
383 2268 fjp
384 7476 azabala
                        //first is true only if the extent of the mapcontrol was null
385
                        if (first) {
386
                                for (int i = 0; i < rects.length; i++) {
387
                                        if (rects[i] != null) {
388
                                                rect.add(rects[i]);
389 2268 fjp
                                        }
390 7476 azabala
                                }//for
391
                                mapControl.getMapContext().getViewPort().setExtent(rect);
392
                        }// if first
393 7537 sbayarri
                }// if
394 7476 azabala
                mapControl.getMapContext().endAtomicEvent();
395 7537 sbayarri
396 7476 azabala
                return true;
397
        }
398 7537 sbayarri
399
400 7476 azabala
        /**
401
         * Adds to mapcontrol all layers selected by user in the specified WizardPanel.
402 7537 sbayarri
         *
403
         * @param mapControl
404 7476 azabala
         *         MapControl on which we want to load user selected layers.
405 7537 sbayarri
         * @param wizardPanel
406 7476 azabala
         *         WizardPanel where user selected the layers to load
407
         * @return
408
         */
409
        private boolean loadGenericWizardPanelLayers(MapControl mapControl, WizardPanel wp) {
410
                FLayer lyr = null;
411
                wp.setMapCtrl(mapControl);
412
                wp.execute();
413
                lyr = wp.getLayer();
414 7537 sbayarri
415 9026 jorpiell
                if((lyr != null) && !(lyr.isOk())){
416 7476 azabala
                        //if the layer is not okay (it has errors) process them
417
                        processErrorsOfLayer(lyr, mapControl);
418
                }
419 7537 sbayarri
420 7476 azabala
                if (lyr != null) {
421
                        lyr.setVisible(true);
422
                        mapControl.getMapContext().beginAtomicEvent();
423
                        checkProjection(lyr, mapControl.getViewPort());
424
                        mapControl.getMapContext().getLayers().addLayer(lyr);
425
                        mapControl.getMapContext().endAtomicEvent();
426
                        return true;
427
                }
428
                return false;
429
        }
430 7537 sbayarri
431 7476 azabala
        /**
432
         * This method process the errors found in a layer
433
         * @param lyr
434
         * @param mapControl
435
         */
436 7537 sbayarri
437 7476 azabala
        private void processErrorsOfLayer(FLayer lyr, MapControl mapControl){
438
                List errors = lyr.getErrors();
439
//                wp.callError(null);
440
                mapControl.getMapContext().callNewErrorEvent(null);
441
        }
442
        /**
443
         * Abre dialogo para a?adir capas y las a?ade en mapControl
444 7537 sbayarri
         *
445 7476 azabala
         * Devuelve true si se han a?adido capas.
446
         */
447
        public boolean addLayers(MapControl mapControl) {
448 7537 sbayarri
449
450
451 7476 azabala
                // create and show the modal fopen dialog
452
                fopen = createFOpenDialog();
453
                PluginServices.getMDIManager().addWindow(fopen);
454 2268 fjp
455 7476 azabala
                if (fopen.isAccepted()) {
456
                        if (fopen.getSelectedTab() instanceof FileOpenDialog) {
457
                                FileOpenDialog fileDlg = (FileOpenDialog) fopen
458
                                                .getSelectedTab();
459 8790 jmvivo
                                return loadFileLayers(mapControl, fileDlg);
460 2268 fjp
461
                        } else if (fopen.getSelectedTab() instanceof WizardPanel) {
462
                                WizardPanel wp = (WizardPanel) fopen.getSelectedTab();
463 8790 jmvivo
                                return loadGenericWizardPanelLayers(mapControl, wp);
464 2268 fjp
                        } else {
465 7476 azabala
                                JOptionPane.showMessageDialog((Component) PluginServices
466
                                                .getMainFrame(), "Ninguna capa seleccionada");
467 2268 fjp
                        }
468
                }
469 5190 jmvivo
                return false;
470 2268 fjp
        }
471 9634 caballero
472
        public void initialize() {
473
                // TODO Auto-generated method stub
474
475
        }
476 2268 fjp
}