Statistics
| Revision:

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

History | View | Annotate | Download (10.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.geom.Rectangle2D;
44
import java.io.File;
45
import java.lang.reflect.InvocationTargetException;
46
import java.sql.Connection;
47
import java.sql.DriverManager;
48
import java.sql.SQLException;
49
import java.util.ArrayList;
50

    
51
import javax.swing.JOptionPane;
52

    
53
import org.cresques.cts.ICoordTrans;
54
import org.cresques.cts.IProjection;
55
import org.cresques.cts.gt2.CoordSys;
56
import org.cresques.cts.gt2.CoordTrans;
57

    
58
import com.iver.andami.PluginServices;
59
import com.iver.andami.messages.NotificationManager;
60
import com.iver.andami.plugins.Extension;
61
import com.iver.cit.gvsig.fmap.DriverException;
62
import com.iver.cit.gvsig.fmap.drivers.RasterDriver;
63
import com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver;
64
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
65
import com.iver.cit.gvsig.fmap.drivers.jdbc.postgis.PostGisDriver;
66
import com.iver.cit.gvsig.fmap.layers.FLayer;
67
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
68
import com.iver.cit.gvsig.gui.FOpenDialog;
69
import com.iver.cit.gvsig.gui.FileOpenDialog;
70
import com.iver.cit.gvsig.gui.View;
71
import com.iver.cit.gvsig.gui.WizardPanel;
72
import com.iver.cit.gvsig.gui.wizards.JDBCWizard;
73
import com.iver.cit.gvsig.gui.wms.WMSWizard;
74

    
75

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

    
102
                wp.initWizard();
103
                
104
                return wp;
105
        }
106

    
107
        /**
108
         * @see com.iver.mdiApp.plugins.Extension#isVisible()
109
         */
110
        public boolean isVisible() {
111
                com.iver.andami.ui.mdiManager.View f = PluginServices.getMDIManager()
112
                                                                                                                         .getActiveView();
113

    
114
                if (f == null) {
115
                        return false;
116
                }
117

    
118
                return (f.getClass() == View.class);
119
        }
120

    
121
        /**
122
         * @see com.iver.andami.plugins.Extension#inicializar()
123
         */
124
        public void inicializar() {
125
        }
126
        
127
        /**
128
         * @see com.iver.andami.plugins.Extension#execute(java.lang.String)
129
         */
130
        public void execute(String actionCommand) {
131
                View theView = (View) PluginServices.getMDIManager().getActiveView();
132
                fopen = new FOpenDialog();
133

    
134
                FileOpenDialog fileDlg = new FileOpenDialog(new Class[] {
135
                                        VectorialFileDriver.class, RasterDriver.class
136
                                });
137
                
138
                // LWS JDBCWizard dbWiz = new JDBCWizard();
139

    
140
                // DataBaseOpenDialog dbop = new DataBaseOpenDialog();
141
                // dbop.setClasses(new Class[] { DBDriver.class });
142
                fopen.addTab("Fichero", fileDlg);
143
                for (int i=0; i<wizardStack.size(); i++) {
144
                        WizardPanel wp;
145
                        try {
146
                                wp = Abrir.getInstance(i);
147
                                fopen.addWizardTab(wp.getTabName(), wp);                        
148
                        } catch (IllegalArgumentException e) {
149
                                // TODO Auto-generated catch block
150
                                e.printStackTrace();
151
                        } catch (SecurityException e) {
152
                                // TODO Auto-generated catch block
153
                                e.printStackTrace();
154
                        } catch (InstantiationException e) {
155
                                // TODO Auto-generated catch block
156
                                e.printStackTrace();
157
                        } catch (IllegalAccessException e) {
158
                                // TODO Auto-generated catch block
159
                                e.printStackTrace();
160
                        } catch (InvocationTargetException e) {
161
                                // TODO Auto-generated catch block
162
                                e.printStackTrace();
163
                        } catch (NoSuchMethodException e) {
164
                                // TODO Auto-generated catch block
165
                                e.printStackTrace();
166
                        }
167
                }
168
                // LWS fopen.addWizardTab("WMS", wms);
169
                // LWS fopen.addWizardTab("JDBC", dbWiz);
170

    
171
                //        fopen.addTab("Base de datos", dbop);
172
                PluginServices.getMDIManager().addView(fopen);
173

    
174
                if (fopen.isAccepted()) {
175
                        FLayer lyr = null;
176

    
177
                        if (fopen.getSelectedTab() == fileDlg) {
178
                                if (fileDlg.getFiles() == null) {
179
                                        return;
180
                                }
181

    
182
                                IProjection proj = FOpenDialog.getLastProjection();
183
                                File[] files = fileDlg.getFiles();
184
                                String[] driverNames = fileDlg.getDriverNames();
185
                                Rectangle2D[] rects=new Rectangle2D[files.length];
186
                                boolean first=false;
187
                                theView.getMapControl().getMapContext()
188
                                   .beginAtomicEvent();
189

    
190
                                for (int iFile = 0; iFile < files.length; iFile++) {
191
                                        File fich = files[iFile];
192
                                        String layerName = fich.getName();
193
                                        String layerPath = fich.getAbsolutePath();
194

    
195
                                        try {
196
                                                lyr = LayerFactory.createLayer(layerName,
197
                                                                driverNames[iFile], fich, proj);
198

    
199
                                                if (lyr != null) {
200
                                                        lyr.setVisible(true);
201
                                                        if (theView.getMapControl().getMapContext().getViewPort().getExtent()==null){
202
                                                                first=true;
203
                                                        }
204

    
205
                                                        // Comprobar que la projecci?n es la misma que la vista
206
                                                        if (proj != theView.getProjection()) {
207
                                                                int option = JOptionPane.showConfirmDialog(null,
208
                                                                        PluginServices.getText(this,"reproyectar_aviso"),
209
                                                                        PluginServices.getText(this,"reproyectar_pregunta"),
210
                                                                                JOptionPane.YES_NO_OPTION);
211

    
212
                                                                if (option == JOptionPane.NO_OPTION) {
213
                                                                        continue;
214
                                                                } else {
215
                                                                        ICoordTrans ct = new CoordTrans((CoordSys) proj,
216
                                                                                        (CoordSys) theView.getProjection());
217
                                                                        lyr.setCoordTrans(ct);
218
                                                                        System.err.println("coordTrans = " +
219
                                                                                proj.getAbrev() + " " +
220
                                                                                theView.getProjection().getAbrev());
221
                                                                }
222
                                                        }
223

    
224
                                                        theView.getMapControl().getMapContext().getLayers()
225
                                                                   .addLayer(lyr);
226
                                                        rects[iFile]=lyr.getFullExtent();
227
                                                        
228
                                                        // TODO: Poner una variable y dibujar solo cuando
229
                                                        // todas las capas hayan sido cargadas.
230
                                                        // TODO Se deber? de redibujar mediante la captura de los eventos, por
231
                                                        //eso se comenta la parte anterior
232
                                                        //                                                        theView.getMapControl().drawMap();
233
                                                        //                                                        theView.getTOC().refresh();
234
                                                }
235

    
236
                                        } catch (DriverException e) {
237
                                                NotificationManager.addError("Error al crear la capa", e);
238
                                        }
239
                                }
240

    
241
                                //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.
242
                                if (rects.length > 1) {
243
                                        Rectangle2D rect = new Rectangle2D.Double();
244
                                        rect.setRect(rects[0]);
245

    
246
                                        if (first) {
247
                                                for (int i = 0; i < rects.length; i++) {
248
                                                        rect.add(rects[i]);
249
                                                }
250

    
251
                                                theView.getMapControl().getMapContext().getViewPort()
252
                                                           .setExtent(rect);
253
                                        }
254
                                }
255
                                theView.getMapControl().getMapContext()
256
                                   .endAtomicEvent();
257
                        } else if (fopen.getSelectedTab() instanceof WizardPanel) {
258
                                WizardPanel wp = (WizardPanel) fopen.getSelectedTab();
259

    
260
                                wp.execute();
261
                                lyr = wp.getLayer();
262

    
263
                                if (lyr != null) {
264
                                        lyr.setVisible(true);
265
                                        theView.getMapControl().getMapContext().beginAtomicEvent();
266
                                        theView.getMapControl().getMapContext().getLayers()
267
                                                   .addLayer(lyr);
268
                                        theView.getMapControl().getMapContext().endAtomicEvent();
269
                                }
270
                        /*} else if (fopen.getSelectedTab() == wms) {
271
                                String layerName = wms.getLayerName();
272

273
                                lyr = LayerFactory.createLayer(layerName,
274
                                                wms.getLayersRectangle(), wms.getHost(),
275
                                                wms.getFormat(), wms.getLayersQuery(),
276
                                                wms.getQueryableLayerQuery(), wms.getSRS());
277

278
                                if (lyr != null) {
279
                                        lyr.setVisible(true);
280
                                        theView.getMapControl().getMapContext().beginAtomicEvent();
281
                                        theView.getMapControl().getMapContext().getLayers()
282
                                                   .addLayer(lyr);
283
                                        theView.getMapControl().getMapContext().endAtomicEvent();
284
                                }
285
                        *//*} else if (fopen.getSelectedTab() == dbWiz) {
286
                                String layerName = dbWiz.getLayerName();
287
                                String user = dbWiz.getUser();
288
                                String pwd = dbWiz.getPassword();
289
                                String dbUrl = dbWiz.getUrlConnect();
290
                                String fields = dbWiz.getSqlFields();
291
                                String whereClause = dbWiz.getSqlWhere();
292
                                
293
                                try {
294
                    
295
                    // TODO: ?C?mo se hace esto para que sea gen?rico?.
296
                    // Supongo que el wizard te tiene que decir qu? driver
297
                    // quieres usar....
298
                                    Class.forName("org.postgresql.Driver");
299
                    Connection conn = DriverManager.getConnection(dbUrl, user, pwd);
300
                    conn.setAutoCommit(false);
301

302
                    VectorialDatabaseDriver driver = new PostGisDriver();
303
                    driver.setData(conn, layerName, fields, whereClause);
304
                    lyr = LayerFactory.createDBLayer(driver, layerName, null);
305
                    
306
                                    if (lyr != null) {
307
                                            lyr.setVisible(true);
308
                                            theView.getMapControl().getMapContext().beginAtomicEvent();
309
                                            theView.getMapControl().getMapContext().getLayers()
310
                                                       .addLayer(lyr);
311
                                            theView.getMapControl().getMapContext().endAtomicEvent();
312
                                    }
313
                    
314
                } catch (SQLException e) {
315
                    // TODO Auto-generated catch block
316
                    e.printStackTrace();
317
                } catch (ClassNotFoundException e) {
318
                    // TODO Auto-generated catch block
319
                    e.printStackTrace();
320
                }
321

322
                        */} // for
323
                }
324
        }
325

    
326
        /**
327
         * @see com.iver.andami.plugins.Extension#isEnabled()
328
         */
329
        public boolean isEnabled() {
330
                return true;
331
        }
332
}