Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / Abrir.java @ 1038

History | View | Annotate | Download (5.52 KB)

1
package com.iver.cit.gvsig;
2

    
3
import java.awt.geom.Rectangle2D;
4
import java.io.File;
5

    
6
import javax.swing.JOptionPane;
7

    
8
import org.cresques.cts.ICoordTrans;
9
import org.cresques.cts.IProjection;
10
import org.cresques.cts.gt2.CoordSys;
11
import org.cresques.cts.gt2.CoordTrans;
12

    
13
import com.iver.andami.PluginServices;
14
import com.iver.andami.messages.NotificationManager;
15
import com.iver.andami.plugins.Extension;
16
import com.iver.cit.gvsig.fmap.DriverException;
17
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
18
import com.iver.cit.gvsig.fmap.drivers.RasterDriver;
19
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
20
import com.iver.cit.gvsig.fmap.layers.FLayer;
21
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
22
import com.iver.cit.gvsig.gui.FOpenDialog;
23
import com.iver.cit.gvsig.gui.FileOpenDialog;
24
import com.iver.cit.gvsig.gui.View;
25
import com.iver.cit.gvsig.gui.WMSDataSourceAdapter;
26
import com.iver.cit.gvsig.gui.wms.WMSWizard;
27

    
28

    
29
/**
30
 * DOCUMENT ME!
31
 *
32
 * @author Fernando Gonz?lez Cort?s
33
 */
34
public class Abrir implements Extension {
35
        public FOpenDialog fopen = null;
36

    
37
        /**
38
         * @see com.iver.mdiApp.plugins.Extension#isVisible()
39
         */
40
        public boolean isVisible() {
41
                com.iver.andami.ui.mdiManager.View f = PluginServices.getMDIManager()
42
                                                                                                                         .getActiveView();
43

    
44
                if (f == null) {
45
                        return false;
46
                }
47

    
48
                return (f.getClass() == View.class);
49
        }
50

    
51
        /**
52
         * @see com.iver.andami.plugins.Extension#inicializar()
53
         */
54
        public void inicializar() {
55
        }
56

    
57
        /**
58
         * @see com.iver.andami.plugins.Extension#execute(java.lang.String)
59
         */
60
        public void execute(String actionCommand) {
61
                View theView = (View) PluginServices.getMDIManager().getActiveView();
62
                fopen = new FOpenDialog();
63

    
64
                FileOpenDialog fileDlg = new FileOpenDialog(new Class[] {
65
                                        VectorialFileDriver.class, RasterDriver.class
66
                                });
67
                WMSWizard wms = new WMSWizard();
68
                wms.setDataSource(new WMSDataSourceAdapter());
69

    
70
                //        DataBaseOpenDialog dbop = new DataBaseOpenDialog();
71
                //        dbop.setClasses(new Class[] { DBDriver.class });
72
                fopen.addTab("Fichero", fileDlg);
73
                fopen.addWizardTab("WMS", wms);
74

    
75
                //        fopen.addTab("Base de datos", dbop);
76
                PluginServices.getMDIManager().addView(fopen);
77

    
78
                if (fopen.isAccepted()) {
79
                        FLayer lyr = null;
80

    
81
                        if (fopen.getSelectedTab() == fileDlg) {
82
                                if (fileDlg.getFiles() == null) {
83
                                        return;
84
                                }
85

    
86
                                IProjection proj = FOpenDialog.getLastProjection();
87
                                File[] files = fileDlg.getFiles();
88
                                Rectangle2D[] rects=new Rectangle2D[files.length];
89
                                boolean first=false;
90
                                for (int iFile = 0; iFile < files.length; iFile++) {
91
                                        File fich = files[iFile];
92
                                        String layerName = fich.getName();
93
                                        String layerPath = fich.getAbsolutePath();
94

    
95
                                        try {
96
                                                lyr = LayerFactory.createLayer(layerName,
97
                                                                fileDlg.getDriverName(), fich, proj);
98

    
99
                                                if (lyr != null) {
100
                                                        lyr.setVisible(true);
101
                                                        theView.getMapControl().getMapContext()
102
                                                                   .beginAtomicEvent();
103
                                                        if (theView.getMapControl().getMapContext().getViewPort().getExtent()==null){
104
                                                                first=true;
105
                                                        }
106
                                                        // Comprobar que la projecci?n es la misma que la vista
107
                                                        if (proj != theView.getProjection())
108
                                                        {
109
                                                        int option = JOptionPane.showConfirmDialog(null, 
110
                                                                        "La proyecci?n de la capa no es igual que la de la vista", "?Desea reproyectar?", JOptionPane.YES_NO_OPTION);
111

    
112
                                                        if (option == JOptionPane.NO_OPTION)
113
                                                        {
114
                                                                        continue;
115
                                                        }
116
                                                        else
117
                                                        {
118
                                                                ICoordTrans ct = new CoordTrans((CoordSys) proj, (CoordSys) theView.getProjection());
119
                                                                lyr.setCoordTrans(ct);
120
                                                                System.err.println("coordTrans = " + 
121
                                                                                proj.getAbrev() + " " + theView.getProjection().getAbrev());
122
                                                        }
123
                                
124
                                                        
125
                                                        }
126
                                                        theView.getMapControl().getMapContext().getLayers()
127
                                                                   .addLayer(lyr);
128
                                                        rects[iFile]=lyr.getFullExtent();
129
                                                        theView.getMapControl().getMapContext()
130
                                                                   .endAtomicEvent();
131
                                                        
132
                                                        // TODO: Poner una variable y dibujar solo cuando
133
                                                        // todas las capas hayan sido cargadas.
134
                                                        // TODO Se deber? de redibujar mediante la captura de los eventos, por
135
                                                        //eso se comenta la parte anterior
136
//                                                        theView.getMapControl().drawMap();
137
//                                                        theView.getTOC().refresh();
138
                                                }
139
                                        } catch (DriverException e) {
140
                                                NotificationManager.addError("Error al crear la capa", e);
141
                                        }catch (DriverIOException e2) {
142
                                            NotificationManager.addError("Error al crear la capa",e2);
143
                                    }
144
                                }
145
                                //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.
146
                                if (rects.length>1){
147
                                        Rectangle2D rect=new Rectangle2D.Double();
148
                                        rect.setRect(rects[0]);
149
                                        if (first){
150
                                                for (int i=0;i<rects.length;i++){
151
                                                        rect.add(rects[i]);
152
                                                }
153
                                                theView.getMapControl().getMapContext().getViewPort().setExtent(rect);
154
                                        }
155
                                }
156
                        } else if (fopen.getSelectedTab() == wms) {
157
                                String layerName = wms.getLayerName();
158

    
159
                                lyr = LayerFactory.createLayer(layerName,
160
                                                wms.getLayersRectangle(), wms.getHost(),
161
                                                wms.getFormat(), wms.getLayersQuery(),
162
                                                wms.getQueryableLayerQuery(), wms.getSRS());
163

    
164
                                if (lyr != null) {
165
                                        lyr.setVisible(true);
166
                                        theView.getMapControl().getMapContext().beginAtomicEvent();
167
                                        theView.getMapControl().getMapContext().getLayers()
168
                                                   .addLayer(lyr);
169
                                        theView.getMapControl().getMapContext().endAtomicEvent();
170

    
171
                                }
172
                        } // for
173
                }
174
        }
175

    
176
        /**
177
         * @see com.iver.andami.plugins.Extension#isEnabled()
178
         */
179
        public boolean isEnabled() {
180
                return true;
181
        }
182
}