Statistics
| Revision:

gvsig-raster / org.gvsig.raster.wms / trunk / org.gvsig.raster.wms / org.gvsig.raster.wms.app.wmsclient / src / main / java / org / gvsig / raster / wms / app / wmsclient / wmc / ImportWebMapContextExtension.java @ 2484

History | View | Annotate | Download (8.08 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
* 
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
* 
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
* 
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
* MA  02110-1301, USA.
20
* 
21
*/
22
 
23
package org.gvsig.raster.wms.app.wmsclient.wmc;
24

    
25
import java.awt.Component;
26
import java.awt.geom.Rectangle2D;
27
import java.io.File;
28
import java.io.FileNotFoundException;
29
import java.util.Date;
30

    
31
import javax.swing.JOptionPane;
32
import javax.swing.filechooser.FileFilter;
33

    
34
import org.gvsig.andami.PluginServices;
35
import org.gvsig.andami.messages.NotificationManager;
36
import org.gvsig.andami.plugins.Extension;
37
import org.gvsig.andami.ui.mdiManager.IWindow;
38
import org.gvsig.app.project.Project;
39
import org.gvsig.app.project.ProjectManager;
40
import org.gvsig.app.project.documents.view.DefaultViewDocument;
41
import org.gvsig.app.project.documents.view.ViewManager;
42
import org.gvsig.app.project.documents.view.gui.AbstractViewPanel;
43
import org.gvsig.app.project.documents.view.gui.DefaultViewPanel;
44
import org.gvsig.fmap.dal.DALLocator;
45
import org.gvsig.fmap.dal.DataManager;
46
import org.gvsig.fmap.dal.coverage.exception.ImportMapContextException;
47
import org.gvsig.fmap.geom.Geometry;
48
import org.gvsig.fmap.geom.GeometryLocator;
49
import org.gvsig.fmap.geom.primitive.Envelope;
50
import org.gvsig.fmap.mapcontext.exceptions.UnsupportedVersionLayerException;
51
import org.gvsig.gui.beans.swing.JFileChooser;
52
import org.gvsig.raster.wms.app.wmsclient.gui.panel.WebMapContextFileChooserAccessory;
53
import org.gvsig.raster.wms.app.wmsclient.layer.FLyrWMS;
54
import org.gvsig.raster.wms.io.WMSDataParameters;
55
import org.gvsig.raster.wms.io.WMSServerExplorer;
56
import org.gvsig.raster.wms.io.WMSServerExplorerParameters;
57

    
58

    
59
public class ImportWebMapContextExtension extends Extension {
60

    
61
        public static final String WMC_FILECHOOSER_ID = "WMC_FILECHOOSER_ID";
62
        private String lastPath = null;
63

    
64
        public void initialize() {
65
                String[] arguments = PluginServices.getArguments();
66
                if (arguments[arguments.length-1].toLowerCase().endsWith(".cml")) {
67
                        File wmcFile = new File(arguments[arguments.length-1]);
68
                        if (!wmcFile.exists()) {
69
                                NotificationManager.addError(PluginServices.getText(this, "could_not_find_web_map_context_file"), new FileNotFoundException());
70
                                return;
71
                        }
72

    
73
                        readMapContextFile(wmcFile, null);
74
                }
75
        }
76

    
77
        @SuppressWarnings("deprecation")
78
        public void execute(String actionCommand) {
79
                if (actionCommand.equals("IMPORT")) {
80
                        JFileChooser fc = new JFileChooser(WMC_FILECHOOSER_ID, lastPath);
81
                        fc.setFileFilter(new FileFilter() {
82
                                public boolean accept(File f) {
83
                                        return f.isDirectory() || f.getAbsolutePath().toLowerCase().endsWith(WebMapContext.FILE_EXTENSION);
84
                                }
85

    
86
                                public String getDescription() {
87
                                        return PluginServices.getText(this, "ogc_mapcontext_file")+" (*.cml)";
88
                                }
89
                        });
90
                        IWindow v = PluginServices.getMDIManager().getActiveWindow();
91

    
92
                        // If the current active view is a gvSIG's view, we'll keep its name to
93
                        // show it at the JFileChooser's accessory.
94
                        String currentViewName;
95
                        if (v instanceof AbstractViewPanel) {
96
                                currentViewName = ((AbstractViewPanel) v).getModel().getName();
97
                        } else {
98
                                currentViewName = null;
99
                        }
100

    
101
                        // Create an accessory for the Web Map Context's file chooser.
102
                        WebMapContextFileChooserAccessory acc = new WebMapContextFileChooserAccessory(currentViewName);
103

    
104
                        // Add the accessory to the file chooser
105
                        fc.setAccessory(acc);
106

    
107
                        // Nothing else than Web Map Context files allowed
108
                        fc.setAcceptAllFileFilterUsed(false);
109

    
110
                        if (fc.showOpenDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION){
111
                                File f = fc.getSelectedFile();
112
                                readMapContextFile(f, acc.getSelectedView());
113
                                String fileName = f.getAbsolutePath();
114
                                lastPath  = fileName.substring(0, fileName.lastIndexOf(File.separatorChar));
115

    
116
                        }
117
                        fc = null;
118
                }
119
        }
120

    
121
        /**
122
         * Reads a WebMapContext (.cml) file and loads its layers into the destination
123
         * view.
124
         * @param wmcFile
125
         * @param dstView
126
         */
127
        @SuppressWarnings("unchecked")
128
        private void readMapContextFile(File wmcFile, DefaultViewDocument dstView) {
129
                WebMapContext wmc = new WebMapContext();
130
                try {
131
                        wmc.readFile(wmcFile);
132
                } catch (UnsupportedVersionLayerException e) {
133
                        JOptionPane.showMessageDialog(
134
                                        (Component) PluginServices.getMainFrame(),
135
                                        e.getMessage(),
136
                                        PluginServices.getText(this, "unsupported_version"),
137
                                        JOptionPane.ERROR_MESSAGE);
138
                        return;
139
                } catch (ImportMapContextException e) {
140
                        if (e.isCritical()) {
141
                                // import impossible: show message and quit
142
                                JOptionPane.showMessageDialog(
143
                                                (Component) PluginServices.getMainFrame(),
144
                                                e.getMessage(),
145
                                                PluginServices.getText(this, "problems_encountered_while_importing"),
146
                                                JOptionPane.ERROR_MESSAGE);
147
                                return;
148
                        } else {
149
                                JOptionPane.showMessageDialog(
150
                                                (Component) PluginServices.getMainFrame(),
151
                                                e.getMessage() + "\n\n" +
152
                                                PluginServices.getText(this, "edit_layer_properties_to_fix_them"),
153
                                                PluginServices.getText(this, "problems_encountered_while_importing"),
154
                                                JOptionPane.ERROR_MESSAGE);
155
                        }
156
                }
157

    
158
                if (dstView == null) {
159
                        // Since the destination view is null, a new one will be added to
160
                        // the project.
161
//                        dstView = ProjectFactory.createView(null);
162
                        dstView = (DefaultViewDocument) ProjectManager.getInstance().createDocument(ViewManager.TYPENAME);
163
                        
164
                        dstView.setName(wmc.title);
165
                        dstView.setComment("Created from WebMapContext file: " + wmcFile.getName());
166

    
167
//                        ProjectExtension pe = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class);
168
//                        Project theProject = pe.getProject();
169
                        final Project theProject = ProjectManager.getInstance().getCurrentProject();
170

    
171
                        theProject.add(dstView);
172

    
173
                        // show the view in a new window
174
                        AbstractViewPanel v = null;
175
                        v = new DefaultViewPanel(dstView);
176
                        PluginServices.getMDIManager().addWindow(v);
177
                }
178

    
179
                for (int i = 0; i < wmc.layerList.size(); i++) {
180
                        // WMS layers
181
                        if (wmc.layerList.get(i) instanceof FLyrWMS) {
182
                                FLyrWMS layer = wmc.layerList.get(i);
183
                                WMSDataParameters params = wmc.paramsList.get(i);
184
                                
185
                                DataManager datamanager = DALLocator.getDataManager();
186
                                
187
                                /*
188
                                 * will connect to get the online resources defined by
189
                                 * server, because WMC only defines the original URL for
190
                                 * the server but not for the operations.
191
                                 */
192
                                try {
193
                                        WMSServerExplorerParameters explorerParams = (WMSServerExplorerParameters) datamanager.createServerExplorerParameters(WMSServerExplorer.NAME);
194
                                        explorerParams.setHost(params.getURI());
195
                                        
196
                                        WMSServerExplorer explorer = (WMSServerExplorer) datamanager.createServerExplorer(explorerParams);
197
                                        
198
                                        layer.setExplorer(explorer);
199
                                        params.setOverrideHost(false);
200
                                        explorer.connect(null, true);
201
                                        
202
                                        params.setOnlineResources(explorer.getOnlineResources());
203
                                        layer.setParameters(params);
204
                                        
205
                                        Rectangle2D rec = wmc.bBox;
206
                                        Envelope envelope = GeometryLocator.getGeometryManager().createEnvelope(rec.getX(),rec.getY(),rec.getMaxX(),rec.getMaxY(), Geometry.SUBTYPES.GEOM2D);
207
                                        dstView.getMapContext().getViewPort().setEnvelope(envelope);
208
                                } catch (Exception e) {
209
                                        NotificationManager.addInfo(PluginServices.getText(this, "connect_error")+"\n"+
210
                                                        PluginServices.getText(this, "failed_restoring_online_resource_values")+
211
                                        " ["+new Date(System.currentTimeMillis()).toString()+"]",
212
                                        e);
213
                                }
214
                                
215
                                dstView.getMapContext().getLayers().addLayer(layer);
216
                        }
217
                }
218
        }
219

    
220
        public boolean isEnabled() {
221
                return true;
222
        }
223

    
224
        public boolean isVisible() {
225
                return true;
226
        }
227

    
228
}