Statistics
| Revision:

root / trunk / extensions / extWCS / src-gvsig / com / iver / cit / gvsig / Abrir.java @ 1879

History | View | Annotate | Download (7.78 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.geom.Rectangle2D;
44
import java.io.File;
45
import java.util.ArrayList;
46

    
47
import javax.swing.JOptionPane;
48

    
49
import org.cresques.cts.ICoordTrans;
50
import org.cresques.cts.IProjection;
51
import org.cresques.cts.gt2.CoordSys;
52
import org.cresques.cts.gt2.CoordTrans;
53

    
54
import com.iver.andami.PluginServices;
55
import com.iver.andami.messages.NotificationManager;
56
import com.iver.andami.plugins.Extension;
57
import com.iver.cit.gvsig.fmap.DriverException;
58
import com.iver.cit.gvsig.fmap.drivers.RasterDriver;
59
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
60
import com.iver.cit.gvsig.fmap.layers.FLayer;
61
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
62
import com.iver.cit.gvsig.gui.FOpenDialog;
63
import com.iver.cit.gvsig.gui.FileOpenDialog;
64
import com.iver.cit.gvsig.gui.View;
65
import com.iver.cit.gvsig.gui.WizardPanel;
66
import com.iver.cit.gvsig.gui.wms.WMSWizard;
67

    
68

    
69
/**
70
 * Extensi?n que abre un di?logo para seleccionar la capa o capas que se
71
 * quieren a?adir a la vista.
72
 *
73
 * @author Fernando Gonz?lez Cort?s
74
 */
75
public class Abrir implements Extension {
76
        public FOpenDialog fopen = null;
77
        
78
        private static ArrayList wizardStack = null;
79
        
80
        static {
81
                Abrir.wizardStack = new ArrayList();
82
                Abrir.addWizard(new WMSWizard());
83
        }
84
        
85
        public static void addWizard(WizardPanel wp) {
86
                wp.initWizard();
87
                Abrir.wizardStack.add(wp);
88
        }
89

    
90
        /**
91
         * @see com.iver.mdiApp.plugins.Extension#isVisible()
92
         */
93
        public boolean isVisible() {
94
                com.iver.andami.ui.mdiManager.View f = PluginServices.getMDIManager()
95
                                                                                                                         .getActiveView();
96

    
97
                if (f == null) {
98
                        return false;
99
                }
100

    
101
                return (f.getClass() == View.class);
102
        }
103

    
104
        /**
105
         * @see com.iver.andami.plugins.Extension#inicializar()
106
         */
107
        public void inicializar() {
108
        }
109

    
110
        /**
111
         * @see com.iver.andami.plugins.Extension#execute(java.lang.String)
112
         */
113
        public void execute(String actionCommand) {
114
                View theView = (View) PluginServices.getMDIManager().getActiveView();
115
                fopen = new FOpenDialog();
116

    
117
                FileOpenDialog fileDlg = new FileOpenDialog(new Class[] {
118
                                        VectorialFileDriver.class, RasterDriver.class
119
                                });
120
                // LWS WMSWizard wms = new WMSWizard(); 
121
                // LWS wms.setDataSource(new WMSDataSourceAdapter()); 
122

    
123
                //        DataBaseOpenDialog dbop = new DataBaseOpenDialog();
124
                //        dbop.setClasses(new Class[] { DBDriver.class });
125
                fopen.addTab("Fichero", fileDlg);
126
                // LWS fopen.addWizardTab("WMS", wms);
127
                for (int i=0; i<wizardStack.size(); i++) {
128
                        WizardPanel wp = (WizardPanel) Abrir.wizardStack.get(i);
129
                        fopen.addWizardTab(wp.getTabName(), wp);
130
                }
131
                
132
                // <Jaume>
133
                // Aqu? es donde se deber?a poner el c?digo si queremos ponerlo de manera integrada
134
//                 LWS  WCSWizard wcs = new WCSWizard();
135
//                 LWS  wcs.setDataSource(new WCSDataSourceAdapter()); 
136
//                 LWS  fopen.addWizardTab("WCS", wcs);
137
                // </Jaume>
138
                //        fopen.addTab("Base de datos", dbop);
139
                PluginServices.getMDIManager().addView(fopen);
140

    
141
                if (fopen.isAccepted()) {
142
                        FLayer lyr = null;
143

    
144
                        if (fopen.getSelectedTab() == fileDlg) {
145
                                if (fileDlg.getFiles() == null) {
146
                                        return;
147
                                }
148

    
149
                                IProjection proj = FOpenDialog.getLastProjection();
150
                                File[] files = fileDlg.getFiles();
151
                                String[] driverNames = fileDlg.getDriverNames();
152
                                Rectangle2D[] rects=new Rectangle2D[files.length];
153
                                boolean first=false;
154
                                theView.getMapControl().getMapContext()
155
                                   .beginAtomicEvent();
156

    
157
                                for (int iFile = 0; iFile < files.length; iFile++) {
158
                                        File fich = files[iFile];
159
                                        String layerName = fich.getName();
160
                                        String layerPath = fich.getAbsolutePath();
161

    
162
                                        try {
163
                                                lyr = LayerFactory.createLayer(layerName,
164
                                                                driverNames[iFile], fich, proj);
165

    
166
                                                if (lyr != null) {
167
                                                        lyr.setVisible(true);
168
                                                        if (theView.getMapControl().getMapContext().getViewPort().getExtent()==null){
169
                                                                first=true;
170
                                                        }
171

    
172
                                                        // Comprobar que la projecci?n es la misma que la vista
173
                                                        if (proj != theView.getProjection()) {
174
                                                                int option = JOptionPane.showConfirmDialog(null,
175
                                                                        PluginServices.getText(this,"reproyectar_aviso"),
176
                                                                        PluginServices.getText(this,"reproyectar_pregunta"),
177
                                                                                JOptionPane.YES_NO_OPTION);
178

    
179
                                                                if (option == JOptionPane.NO_OPTION) {
180
                                                                        continue;
181
                                                                } else {
182
                                                                        ICoordTrans ct = new CoordTrans((CoordSys) proj,
183
                                                                                        (CoordSys) theView.getProjection());
184
                                                                        lyr.setCoordTrans(ct);
185
                                                                        System.err.println("coordTrans = " +
186
                                                                                proj.getAbrev() + " " +
187
                                                                                theView.getProjection().getAbrev());
188
                                                                }
189
                                                        }
190

    
191
                                                        theView.getMapControl().getMapContext().getLayers()
192
                                                                   .addLayer(lyr);
193
                                                        rects[iFile]=lyr.getFullExtent();
194
                                                        
195
                                                        // TODO: Poner una variable y dibujar solo cuando
196
                                                        // todas las capas hayan sido cargadas.
197
                                                        // TODO Se deber? de redibujar mediante la captura de los eventos, por
198
                                                        //eso se comenta la parte anterior
199
                                                        //                                                        theView.getMapControl().drawMap();
200
                                                        //                                                        theView.getTOC().refresh();
201
                                                }
202

    
203
                                        } catch (DriverException e) {
204
                                                NotificationManager.addError("Error al crear la capa", e);
205
                                        }
206
                                }
207

    
208
                                //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.
209
                                if (rects.length > 1) {
210
                                        Rectangle2D rect = new Rectangle2D.Double();
211
                                        rect.setRect(rects[0]);
212

    
213
                                        if (first) {
214
                                                for (int i = 0; i < rects.length; i++) {
215
                                                        rect.add(rects[i]);
216
                                                }
217

    
218
                                                theView.getMapControl().getMapContext().getViewPort()
219
                                                           .setExtent(rect);
220
                                        }
221
                                }
222
                                theView.getMapControl().getMapContext()
223
                                   .endAtomicEvent();
224

    
225
                        } else if (fopen.getSelectedTab() instanceof WizardPanel) {
226
                                WizardPanel wp = (WizardPanel) fopen.getSelectedTab();
227
                                
228
                                wp.execute();
229
                                lyr = wp.getLayer();
230
                                
231
                                if (lyr != null) {
232
                                        lyr.setVisible(true);
233
                                        theView.getMapControl().getMapContext().beginAtomicEvent();
234
                                        theView.getMapControl().getMapContext().getLayers()
235
                                                   .addLayer(lyr);
236
                                        theView.getMapControl().getMapContext().endAtomicEvent();
237
                                }
238
                        }/* else if (fopen.getSelectedTab() == wms) {
239
                                String layerName = wms.getLayerName();
240

241
                                lyr = LayerFactory.createLayer(layerName,
242
                                                wms.getLayersRectangle(), wms.getHost(),
243
                                                wms.getFormat(), wms.getLayersQuery(),
244
                                                wms.getQueryableLayerQuery(), wms.getSRS());
245

246
                                if (lyr != null) {
247
                                        lyr.setVisible(true);
248
                                        theView.getMapControl().getMapContext().beginAtomicEvent();
249
                                        theView.getMapControl().getMapContext().getLayers()
250
                                                   .addLayer(lyr);
251
                                        theView.getMapControl().getMapContext().endAtomicEvent();
252
                                }
253
                        } // for*/
254
                }
255
        }
256

    
257
        /**
258
         * @see com.iver.andami.plugins.Extension#isEnabled()
259
         */
260
        public boolean isEnabled() {
261
                return true;
262
        }
263
}