Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / Abrir.java @ 1670

History | View | Annotate | Download (6.67 KB)

1 1103 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 312 fernando
package com.iver.cit.gvsig;
42
43 596 fernando
import com.iver.andami.PluginServices;
44 755 fernando
import com.iver.andami.messages.NotificationManager;
45 596 fernando
import com.iver.andami.plugins.Extension;
46 1219 vcaballero
47 652 fernando
import com.iver.cit.gvsig.fmap.DriverException;
48 671 luisw
import com.iver.cit.gvsig.fmap.drivers.RasterDriver;
49 378 fjp
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
50 357 fjp
import com.iver.cit.gvsig.fmap.layers.FLayer;
51
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
52 312 fernando
import com.iver.cit.gvsig.gui.FOpenDialog;
53 429 fernando
import com.iver.cit.gvsig.gui.FileOpenDialog;
54 312 fernando
import com.iver.cit.gvsig.gui.View;
55 854 fernando
import com.iver.cit.gvsig.gui.WMSDataSourceAdapter;
56
import com.iver.cit.gvsig.gui.wms.WMSWizard;
57 312 fernando
58 1219 vcaballero
import org.cresques.cts.ICoordTrans;
59
import org.cresques.cts.IProjection;
60
import org.cresques.cts.gt2.CoordSys;
61
import org.cresques.cts.gt2.CoordTrans;
62 312 fernando
63 1219 vcaballero
import java.awt.geom.Rectangle2D;
64
65
import java.io.File;
66
67
import javax.swing.JOptionPane;
68
69
70 312 fernando
/**
71 1219 vcaballero
 * Extensi?n que abre un di?logo para seleccionar la capa o capas que se
72
 * quieren a?adir a la vista.
73 312 fernando
 *
74
 * @author Fernando Gonz?lez Cort?s
75
 */
76 596 fernando
public class Abrir implements Extension {
77 854 fernando
        public FOpenDialog fopen = null;
78 312 fernando
79 854 fernando
        /**
80
         * @see com.iver.mdiApp.plugins.Extension#isVisible()
81
         */
82
        public boolean isVisible() {
83
                com.iver.andami.ui.mdiManager.View f = PluginServices.getMDIManager()
84
                                                                                                                         .getActiveView();
85 596 fernando
86 854 fernando
                if (f == null) {
87
                        return false;
88
                }
89 596 fernando
90 854 fernando
                return (f.getClass() == View.class);
91
        }
92 596 fernando
93
        /**
94
         * @see com.iver.andami.plugins.Extension#inicializar()
95
         */
96
        public void inicializar() {
97
        }
98
99
        /**
100
         * @see com.iver.andami.plugins.Extension#execute(java.lang.String)
101
         */
102 854 fernando
        public void execute(String actionCommand) {
103
                View theView = (View) PluginServices.getMDIManager().getActiveView();
104
                fopen = new FOpenDialog();
105 438 fernando
106 854 fernando
                FileOpenDialog fileDlg = new FileOpenDialog(new Class[] {
107
                                        VectorialFileDriver.class, RasterDriver.class
108
                                });
109
                WMSWizard wms = new WMSWizard();
110
                wms.setDataSource(new WMSDataSourceAdapter());
111 438 fernando
112 854 fernando
                //        DataBaseOpenDialog dbop = new DataBaseOpenDialog();
113
                //        dbop.setClasses(new Class[] { DBDriver.class });
114
                fopen.addTab("Fichero", fileDlg);
115
                fopen.addWizardTab("WMS", wms);
116 438 fernando
117 854 fernando
                //        fopen.addTab("Base de datos", dbop);
118
                PluginServices.getMDIManager().addView(fopen);
119 438 fernando
120 854 fernando
                if (fopen.isAccepted()) {
121
                        FLayer lyr = null;
122 438 fernando
123 854 fernando
                        if (fopen.getSelectedTab() == fileDlg) {
124
                                if (fileDlg.getFiles() == null) {
125
                                        return;
126
                                }
127 438 fernando
128 904 fjp
                                IProjection proj = FOpenDialog.getLastProjection();
129 854 fernando
                                File[] files = fileDlg.getFiles();
130 1171 fjp
                                String[] driverNames = fileDlg.getDriverNames();
131 870 vcaballero
                                Rectangle2D[] rects=new Rectangle2D[files.length];
132
                                boolean first=false;
133 1216 fjp
                                theView.getMapControl().getMapContext()
134
                                   .beginAtomicEvent();
135
136 854 fernando
                                for (int iFile = 0; iFile < files.length; iFile++) {
137
                                        File fich = files[iFile];
138
                                        String layerName = fich.getName();
139
                                        String layerPath = fich.getAbsolutePath();
140 438 fernando
141 854 fernando
                                        try {
142
                                                lyr = LayerFactory.createLayer(layerName,
143 1171 fjp
                                                                driverNames[iFile], fich, proj);
144 854 fernando
145
                                                if (lyr != null) {
146
                                                        lyr.setVisible(true);
147 870 vcaballero
                                                        if (theView.getMapControl().getMapContext().getViewPort().getExtent()==null){
148
                                                                first=true;
149
                                                        }
150 1219 vcaballero
151 904 fjp
                                                        // Comprobar que la projecci?n es la misma que la vista
152 1219 vcaballero
                                                        if (proj != theView.getProjection()) {
153
                                                                int option = JOptionPane.showConfirmDialog(null,
154 1470 fjp
                                                                        PluginServices.getText(this,"reproyectar_aviso"),
155
                                                                        PluginServices.getText(this,"reproyectar_pregunta"),
156 1219 vcaballero
                                                                                JOptionPane.YES_NO_OPTION);
157 904 fjp
158 1219 vcaballero
                                                                if (option == JOptionPane.NO_OPTION) {
159 904 fjp
                                                                        continue;
160 1219 vcaballero
                                                                } else {
161
                                                                        ICoordTrans ct = new CoordTrans((CoordSys) proj,
162
                                                                                        (CoordSys) theView.getProjection());
163
                                                                        lyr.setCoordTrans(ct);
164
                                                                        System.err.println("coordTrans = " +
165
                                                                                proj.getAbrev() + " " +
166
                                                                                theView.getProjection().getAbrev());
167
                                                                }
168 904 fjp
                                                        }
169 1219 vcaballero
170 854 fernando
                                                        theView.getMapControl().getMapContext().getLayers()
171
                                                                   .addLayer(lyr);
172 870 vcaballero
                                                        rects[iFile]=lyr.getFullExtent();
173
174 854 fernando
                                                        // TODO: Poner una variable y dibujar solo cuando
175
                                                        // todas las capas hayan sido cargadas.
176
                                                        // TODO Se deber? de redibujar mediante la captura de los eventos, por
177
                                                        //eso se comenta la parte anterior
178 1219 vcaballero
                                                        //                                                        theView.getMapControl().drawMap();
179
                                                        //                                                        theView.getTOC().refresh();
180 854 fernando
                                                }
181 1216 fjp
182 854 fernando
                                        } catch (DriverException e) {
183
                                                NotificationManager.addError("Error al crear la capa", e);
184 1219 vcaballero
                                        }
185 854 fernando
                                }
186 1219 vcaballero
187 870 vcaballero
                                //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.
188 1219 vcaballero
                                if (rects.length > 1) {
189
                                        Rectangle2D rect = new Rectangle2D.Double();
190 870 vcaballero
                                        rect.setRect(rects[0]);
191 1219 vcaballero
192
                                        if (first) {
193
                                                for (int i = 0; i < rects.length; i++) {
194 870 vcaballero
                                                        rect.add(rects[i]);
195
                                                }
196 1219 vcaballero
197
                                                theView.getMapControl().getMapContext().getViewPort()
198
                                                           .setExtent(rect);
199 870 vcaballero
                                        }
200
                                }
201 1216 fjp
                                theView.getMapControl().getMapContext()
202
                                   .endAtomicEvent();
203
204 854 fernando
                        } else if (fopen.getSelectedTab() == wms) {
205
                                String layerName = wms.getLayerName();
206
207
                                lyr = LayerFactory.createLayer(layerName,
208
                                                wms.getLayersRectangle(), wms.getHost(),
209
                                                wms.getFormat(), wms.getLayersQuery(),
210
                                                wms.getQueryableLayerQuery(), wms.getSRS());
211
212
                                if (lyr != null) {
213
                                        lyr.setVisible(true);
214
                                        theView.getMapControl().getMapContext().beginAtomicEvent();
215
                                        theView.getMapControl().getMapContext().getLayers()
216
                                                   .addLayer(lyr);
217
                                        theView.getMapControl().getMapContext().endAtomicEvent();
218
                                }
219
                        } // for
220
                }
221 596 fernando
        }
222 438 fernando
223 596 fernando
        /**
224
         * @see com.iver.andami.plugins.Extension#isEnabled()
225
         */
226
        public boolean isEnabled() {
227
                return true;
228
        }
229 312 fernando
}