Statistics
| Revision:

root / trunk / extensions / extWMS / src / com / iver / cit / gvsig / wmc / ImportWebMapContextExtension.java @ 37961

History | View | Annotate | Download (8.62 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 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

    
42
/* CVS MESSAGES:
43
*
44
* $Id: ImportWebMapContextExtension.java 37961 2012-02-15 16:56:30Z fpenarrubia $
45
* $Log$
46
* Revision 1.11  2007-03-06 17:06:43  caballero
47
* Exceptions
48
*
49
* Revision 1.10  2006/11/28 11:46:59  cesar
50
* Add support to persist the document internal layout
51
*
52
* Revision 1.9  2006/09/27 13:28:51  jaume
53
* *** empty log message ***
54
*
55
* Revision 1.8  2006/09/20 10:32:15  jaume
56
* remembers last location
57
*
58
* Revision 1.7  2006/09/15 10:44:24  caballero
59
* extensibilidad de documentos
60
*
61
* Revision 1.6  2006/09/11 15:57:38  jaume
62
* now accepts loading cml files from command line
63
*
64
* Revision 1.5  2006/08/29 07:56:15  cesar
65
* Rename the *View* family of classes to *Window* (ie: SingletonView to SingletonWindow, ViewInfo to WindowInfo, etc)
66
*
67
* Revision 1.4  2006/08/29 07:13:43  cesar
68
* Rename class com.iver.andami.ui.mdiManager.View to com.iver.andami.ui.mdiManager.IWindow
69
*
70
* Revision 1.3  2006/07/21 10:31:05  jaume
71
* *** empty log message ***
72
*
73
* Revision 1.2  2006/05/03 11:10:54  jaume
74
* *** empty log message ***
75
*
76
* Revision 1.1  2006/05/03 07:51:21  jaume
77
* *** empty log message ***
78
*
79
* Revision 1.5  2006/05/02 16:12:12  jorpiell
80
* Se ha cambiado la interfaz Extension por dos clases: una interfaz (IExtension) y una clase abstract(Extension). A partir de ahora todas las extensiones deben heredar de Extension
81
*
82
* Revision 1.4  2006/05/02 15:57:43  jaume
83
* Few better javadoc
84
*
85
* Revision 1.3  2006/04/20 17:11:54  jaume
86
* Attempting to export
87
*
88
* Revision 1.2  2006/04/19 16:34:29  jaume
89
* *** empty log message ***
90
*
91
* Revision 1.1  2006/04/19 07:57:29  jaume
92
* *** empty log message ***
93
*
94
* Revision 1.1  2006/04/12 17:10:53  jaume
95
* *** empty log message ***
96
*
97
*
98
*/
99
package com.iver.cit.gvsig.wmc;
100

    
101
import java.awt.Component;
102
import java.io.File;
103
import java.io.FileNotFoundException;
104
import java.util.Date;
105

    
106
import javax.swing.JOptionPane;
107
import javax.swing.filechooser.FileFilter;
108

    
109
import org.gvsig.gui.beans.swing.JFileChooser;
110
import org.gvsig.remoteClient.wms.ICancellable;
111

    
112
import com.iver.andami.PluginServices;
113
import com.iver.andami.messages.NotificationManager;
114
import com.iver.andami.plugins.Extension;
115
import com.iver.andami.ui.mdiManager.IWindow;
116
import com.iver.cit.gvsig.ProjectExtension;
117
import com.iver.cit.gvsig.exceptions.layers.UnsupportedVersionLayerException;
118
import com.iver.cit.gvsig.fmap.drivers.wms.FMapWMSDriver;
119
import com.iver.cit.gvsig.fmap.exceptions.ImportMapContextException;
120
import com.iver.cit.gvsig.fmap.layers.FLyrWMS;
121
import com.iver.cit.gvsig.gui.panels.WebMapContextFileChooserAccessory;
122
import com.iver.cit.gvsig.project.Project;
123
import com.iver.cit.gvsig.project.ProjectFactory;
124
import com.iver.cit.gvsig.project.documents.view.ProjectView;
125
import com.iver.cit.gvsig.project.documents.view.gui.View;
126

    
127
public class ImportWebMapContextExtension extends Extension {
128

    
129
        public static final String WMC_FILECHOOSER_ID = "WMC_FILECHOOSER_ID";
130
        private String lastPath = null;
131

    
132
        public void initialize() {
133
                String[] arguments = PluginServices.getArguments();
134
                if (arguments[arguments.length-1].toLowerCase().endsWith(".cml")) {
135
                        File wmcFile = new File(arguments[arguments.length-1]);
136
                        if (!wmcFile.exists()) {
137
                                NotificationManager.addError(PluginServices.getText(this, "could_not_find_web_map_context_file"), new FileNotFoundException());
138
                                return;
139
                        }
140

    
141
                        readMapContextFile(wmcFile, null);
142
                }
143
        }
144

    
145
        public void execute(String actionCommand) {
146
                if (actionCommand.equals("IMPORT")) {
147
                        JFileChooser fc = new JFileChooser(WMC_FILECHOOSER_ID, lastPath);
148
                        fc.setFileFilter(new FileFilter() {
149
                                public boolean accept(File f) {
150
                                        return f.isDirectory() || f.getAbsolutePath().toLowerCase().endsWith(WebMapContext.FILE_EXTENSION);
151
                                }
152

    
153
                                public String getDescription() {
154
                                        return PluginServices.getText(this, "ogc_mapcontext_file")+" (*.cml)";
155
                                }
156
                        });
157
                        IWindow v = PluginServices.getMDIManager().getActiveWindow();
158

    
159
                        // If the current active view is a gvSIG's view, we'll keep its name to
160
                        // show it at the JFileChooser's accessory.
161
                        String currentViewName;
162
                        if (v instanceof View)
163
                                currentViewName = ((View) v).getModel().getName();
164
                        else
165
                                currentViewName = null;
166

    
167
                        // Create an accessory for the Web Map Context's file chooser.
168
                        WebMapContextFileChooserAccessory acc = new WebMapContextFileChooserAccessory(currentViewName);
169

    
170
                        // Add the accessory to the file chooser
171
                        fc.setAccessory(acc);
172

    
173
                        // Nothing else than Web Map Context files allowed
174
                        fc.setAcceptAllFileFilterUsed(false);
175

    
176
                        if (fc.showOpenDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION){
177
                                File f = fc.getSelectedFile();
178
                                readMapContextFile(f, acc.getSelectedView());
179
                                String fileName = f.getAbsolutePath();
180
                                lastPath  = fileName.substring(0, fileName.lastIndexOf(File.separatorChar));
181

    
182
                        }
183
                        fc = null;
184
                }
185
        }
186

    
187
        /**
188
         * Reads a WebMapContext (.cml) file and loads its layers into the destination
189
         * view.
190
         * @param wmcFile
191
         * @param dstView
192
         */
193
        private void readMapContextFile(File wmcFile, ProjectView dstView) {
194
                WebMapContext wmc = new WebMapContext();
195
                try {
196
                        wmc.readFile(wmcFile);
197
                } catch (UnsupportedVersionLayerException e) {
198
                        JOptionPane.showMessageDialog(
199
                                        (Component) PluginServices.getMainFrame(),
200
                                        e.getMessage(),
201
                                        PluginServices.getText(this, "unsupported_version"),
202
                                        JOptionPane.ERROR_MESSAGE);
203
                        return;
204
                } catch (ImportMapContextException e) {
205
                        e.printStackTrace();
206
                        if (e.isCritical()) {
207
                                // import impossible: show message and quit
208
                                JOptionPane.showMessageDialog(
209
                                                (Component) PluginServices.getMainFrame(),
210
                                                e.getMessage(),
211
                                                PluginServices.getText(this, "problems_encountered_while_importing"),
212
                                                JOptionPane.ERROR_MESSAGE);
213
                                return;
214
                        } else {
215
                                JOptionPane.showMessageDialog(
216
                                                (Component) PluginServices.getMainFrame(),
217
                                                e.getMessage() + "\n\n" +
218
                                                PluginServices.getText(this, "edit_layer_properties_to_fix_them"),
219
                                                PluginServices.getText(this, "problems_encountered_while_importing"),
220
                                                JOptionPane.ERROR_MESSAGE);
221
                        }
222
                }
223

    
224
                if (dstView == null) {
225
                        // Since the destination view is null, a new one will be added to
226
                        // the project.
227
                        dstView = ProjectFactory.createView(null);
228
                        dstView.setName(wmc.title);
229
                        dstView.setComment("Created from WebMapContext file: "+wmcFile.getName());
230

    
231
                        ProjectExtension pe = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class);
232
                        Project theProject = pe.getProject();
233
                        theProject.addDocument(dstView);
234

    
235
                        // show the view in a new window
236
                        View v = null;
237
                        v = new View();
238
                        v.initialize(dstView.getMapContext());
239
                        v.setModel(dstView);
240
                        PluginServices.getMDIManager().addWindow(v);
241
                }
242

    
243
                for (int i = 0; i < wmc.layerList.size(); i++) {
244
                        // WMS layers
245
                        if (wmc.layerList.get(i) instanceof FLyrWMS) {
246
                                FLyrWMS layer = (FLyrWMS) wmc.layerList.get(i);
247
                                /*
248
                                 * will connect to get the online resources defined by
249
                                 * server, because WMC only defines the original URL for
250
                                 * the server but not for the operations.
251
                                 */
252
                                try {
253
                                        FMapWMSDriver dr = layer.getDriver();
254
                                        dr.connect(null);
255
                                        dr.getCapabilities(layer.getHost());
256
                                        layer.setOnlineResources(dr.getOnlineResources());
257
                                } catch (Exception e) {
258
                                        NotificationManager.addInfo(PluginServices.getText(this, "connect_error")+"\n"+
259
                                                        PluginServices.getText(this, "failed_restoring_online_resource_values")+ 
260
                                        " ["+new Date(System.currentTimeMillis()).toString()+"]",
261
                                        e);
262
                                }
263
                                dstView.getMapContext().getLayers().addLayer(layer);
264
                        }
265
                }
266
        }
267

    
268
        public boolean isEnabled() {
269
                return true;
270
        }
271

    
272
        public boolean isVisible() {
273
                return true;
274
        }
275

    
276
}