Statistics
| Revision:

svn-gvsig-desktop / branches / pilotoDWG / applications / appgvSIG / src / com / iver / cit / gvsig / Abrir.java @ 2317

History | View | Annotate | Download (6.87 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 com.iver.andami.PluginServices;
44
import com.iver.andami.messages.NotificationManager;
45
import com.iver.andami.plugins.Extension;
46
import com.iver.andami.ui.mdiFrame.MDIFrame;
47

    
48
import com.iver.cit.gvsig.fmap.DriverException;
49
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
50
import com.iver.cit.gvsig.fmap.drivers.RasterDriver;
51
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
52
import com.iver.cit.gvsig.fmap.layers.FLayer;
53
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
54
import com.iver.cit.gvsig.gui.FOpenDialog;
55
import com.iver.cit.gvsig.gui.FileOpenDialog;
56
import com.iver.cit.gvsig.gui.View;
57
import com.iver.cit.gvsig.gui.WMSDataSourceAdapter;
58
import com.iver.cit.gvsig.gui.wms.WMSWizard;
59

    
60
import org.cresques.cts.ICoordTrans;
61
import org.cresques.cts.IProjection;
62
import org.cresques.cts.gt2.CoordSys;
63
import org.cresques.cts.gt2.CoordTrans;
64

    
65
import java.awt.Component;
66
import java.awt.geom.Rectangle2D;
67

    
68
import java.io.File;
69

    
70
import javax.swing.JOptionPane;
71

    
72

    
73
/**
74
 * Extensi?n que abre un di?logo para seleccionar la capa o capas que se
75
 * quieren a?adir a la vista.
76
 *
77
 * @author Fernando Gonz?lez Cort?s
78
 */
79
public class Abrir implements Extension {
80
        public FOpenDialog fopen = null;
81

    
82
        /**
83
         * @see com.iver.mdiApp.plugins.Extension#isVisible()
84
         */
85
        public boolean isVisible() {
86
                com.iver.andami.ui.mdiManager.View f = PluginServices.getMDIManager()
87
                                                                                                                         .getActiveView();
88

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

    
93
                return (f.getClass() == View.class);
94
        }
95

    
96
        /**
97
         * @see com.iver.andami.plugins.Extension#inicializar()
98
         */
99
        public void inicializar() {
100
        }
101

    
102
        /**
103
         * @see com.iver.andami.plugins.Extension#execute(java.lang.String)
104
         */
105
        public void execute(String actionCommand) {
106
                View theView = (View) PluginServices.getMDIManager().getActiveView();
107
                fopen = new FOpenDialog();
108

    
109
                FileOpenDialog fileDlg = new FileOpenDialog(new Class[] {
110
                                        VectorialFileDriver.class, RasterDriver.class
111
                                });
112
                WMSWizard wms = new WMSWizard();
113
                wms.setDataSource(new WMSDataSourceAdapter());
114

    
115
                //        DataBaseOpenDialog dbop = new DataBaseOpenDialog();
116
                //        dbop.setClasses(new Class[] { DBDriver.class });
117
                fopen.addTab("Fichero", fileDlg);
118
                fopen.addWizardTab("WMS", wms);
119

    
120
                //        fopen.addTab("Base de datos", dbop);
121
                PluginServices.getMDIManager().addView(fopen);
122

    
123
                if (fopen.isAccepted()) {
124
                        FLayer lyr = null;
125

    
126
                        if (fopen.getSelectedTab() == fileDlg) {
127
                                if (fileDlg.getFiles() == null) {
128
                                        return;
129
                                }
130

    
131
                                IProjection proj = FOpenDialog.getLastProjection();
132
                                File[] files = fileDlg.getFiles();
133
                                String[] driverNames = fileDlg.getDriverNames();
134
                                Rectangle2D[] rects=new Rectangle2D[files.length];
135
                                boolean first=false;
136
                                theView.getMapControl().getMapContext()
137
                                   .beginAtomicEvent();
138

    
139
                                for (int iFile = 0; iFile < files.length; iFile++) {
140
                                        File fich = files[iFile];
141
                                        String layerName = fich.getName();
142
                                        String layerPath = fich.getAbsolutePath();
143

    
144
                                        try {
145
                                                
146
                                                lyr = LayerFactory.createLayer(layerName,
147
                                                                driverNames[iFile], fich, proj);
148

    
149
                                                if (lyr != null) {
150
                                                        lyr.setVisible(true);
151
                                                        if (theView.getMapControl().getMapContext().getViewPort().getExtent()==null){
152
                                                                first=true;
153
                                                        }
154

    
155
                                                        // Comprobar que la projecci?n es la misma que la vista
156
                                                        if (proj != theView.getProjection()) {
157
                                                                int option = JOptionPane.showConfirmDialog(null,
158
                                                                                "La proyecci?n de la capa no es igual que la de la vista",
159
                                                                                "?Desea reproyectar?",
160
                                                                                JOptionPane.YES_NO_OPTION);
161

    
162
                                                                if (option == JOptionPane.NO_OPTION) {
163
                                                                        continue;
164
                                                                } else {
165
                                                                        ICoordTrans ct = new CoordTrans((CoordSys) proj,
166
                                                                                        (CoordSys) theView.getProjection());
167
                                                                        lyr.setCoordTrans(ct);
168
                                                                        System.err.println("coordTrans = " +
169
                                                                                proj.getAbrev() + " " +
170
                                                                                theView.getProjection().getAbrev());
171
                                                                }
172
                                                        }
173

    
174
                                                        theView.getMapControl().getMapContext().getLayers()
175
                                                                   .addLayer(lyr);
176
                                                        rects[iFile]=lyr.getFullExtent();
177
                                                        
178
                                                        // TODO: Poner una variable y dibujar solo cuando
179
                                                        // todas las capas hayan sido cargadas.
180
                                                        // TODO Se deber? de redibujar mediante la captura de los eventos, por
181
                                                        //eso se comenta la parte anterior
182
                                                        //                                                        theView.getMapControl().drawMap();
183
                                                        //                                                        theView.getTOC().refresh();
184
                                                }
185

    
186
                                        } catch (DriverException e) {
187
                                                e.printStackTrace();
188
                                        }catch (DriverIOException e) {
189
                                                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),"Formato no soportado.");
190
                                        }
191
                                }
192

    
193
                                //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.
194
                                if (rects.length > 1) {
195
                                        Rectangle2D rect = new Rectangle2D.Double();
196
                                        rect.setRect(rects[0]);
197

    
198
                                        if (first) {
199
                                                for (int i = 0; i < rects.length; i++) {
200
                                                        rect.add(rects[i]);
201
                                                }
202

    
203
                                                theView.getMapControl().getMapContext().getViewPort()
204
                                                           .setExtent(rect);
205
                                        }
206
                                }
207
                                theView.getMapControl().getMapContext()
208
                                   .endAtomicEvent();
209

    
210
                        } else if (fopen.getSelectedTab() == wms) {
211
                                String layerName = wms.getLayerName();
212

    
213
                                lyr = LayerFactory.createLayer(layerName,
214
                                                wms.getLayersRectangle(), wms.getHost(),
215
                                                wms.getFormat(), wms.getLayersQuery(),
216
                                                wms.getQueryableLayerQuery(), wms.getSRS());
217

    
218
                                if (lyr != null) {
219
                                        lyr.setVisible(true);
220
                                        theView.getMapControl().getMapContext().beginAtomicEvent();
221
                                        theView.getMapControl().getMapContext().getLayers()
222
                                                   .addLayer(lyr);
223
                                        theView.getMapControl().getMapContext().endAtomicEvent();
224
                                }
225
                        } // for
226
                }
227
        }
228

    
229
        /**
230
         * @see com.iver.andami.plugins.Extension#isEnabled()
231
         */
232
        public boolean isEnabled() {
233
                return true;
234
        }
235
}