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 @ 1944
History | View | Annotate | Download (7.18 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.io.File; |
27 |
import java.io.FileNotFoundException; |
28 |
import java.util.Date; |
29 |
|
30 |
import javax.swing.JOptionPane; |
31 |
import javax.swing.filechooser.FileFilter; |
32 |
|
33 |
import org.gvsig.andami.PluginServices; |
34 |
import org.gvsig.andami.messages.NotificationManager; |
35 |
import org.gvsig.andami.plugins.Extension; |
36 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
37 |
import org.gvsig.app.project.Project; |
38 |
import org.gvsig.app.project.ProjectManager; |
39 |
import org.gvsig.app.project.documents.view.DefaultViewDocument; |
40 |
import org.gvsig.app.project.documents.view.ViewManager; |
41 |
import org.gvsig.app.project.documents.view.gui.AbstractViewPanel; |
42 |
import org.gvsig.app.project.documents.view.gui.DefaultViewPanel; |
43 |
import org.gvsig.fmap.dal.coverage.exception.ImportMapContextException; |
44 |
import org.gvsig.fmap.mapcontext.exceptions.UnsupportedVersionLayerException; |
45 |
import org.gvsig.gui.beans.swing.JFileChooser; |
46 |
import org.gvsig.raster.wms.app.wmsclient.gui.panel.WebMapContextFileChooserAccessory; |
47 |
import org.gvsig.raster.wms.app.wmsclient.layer.FLyrWMS; |
48 |
import org.gvsig.raster.wms.io.WMSServerExplorer; |
49 |
|
50 |
|
51 |
public class ImportWebMapContextExtension extends Extension { |
52 |
|
53 |
public static final String WMC_FILECHOOSER_ID = "WMC_FILECHOOSER_ID"; |
54 |
private String lastPath = null; |
55 |
|
56 |
public void initialize() { |
57 |
String[] arguments = PluginServices.getArguments(); |
58 |
if (arguments[arguments.length-1].toLowerCase().endsWith(".cml")) { |
59 |
File wmcFile = new File(arguments[arguments.length-1]); |
60 |
if (!wmcFile.exists()) {
|
61 |
NotificationManager.addError(PluginServices.getText(this, "could_not_find_web_map_context_file"), new FileNotFoundException()); |
62 |
return;
|
63 |
} |
64 |
|
65 |
readMapContextFile(wmcFile, null);
|
66 |
} |
67 |
} |
68 |
|
69 |
@SuppressWarnings("deprecation") |
70 |
public void execute(String actionCommand) { |
71 |
if (actionCommand.equals("IMPORT")) { |
72 |
JFileChooser fc = new JFileChooser(WMC_FILECHOOSER_ID, lastPath); |
73 |
fc.setFileFilter(new FileFilter() { |
74 |
public boolean accept(File f) { |
75 |
return f.isDirectory() || f.getAbsolutePath().toLowerCase().endsWith(WebMapContext.FILE_EXTENSION);
|
76 |
} |
77 |
|
78 |
public String getDescription() { |
79 |
return PluginServices.getText(this, "ogc_mapcontext_file")+" (*.cml)"; |
80 |
} |
81 |
}); |
82 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
83 |
|
84 |
// If the current active view is a gvSIG's view, we'll keep its name to
|
85 |
// show it at the JFileChooser's accessory.
|
86 |
String currentViewName;
|
87 |
if (v instanceof AbstractViewPanel) { |
88 |
currentViewName = ((AbstractViewPanel) v).getModel().getName(); |
89 |
} else {
|
90 |
currentViewName = null;
|
91 |
} |
92 |
|
93 |
// Create an accessory for the Web Map Context's file chooser.
|
94 |
WebMapContextFileChooserAccessory acc = new WebMapContextFileChooserAccessory(currentViewName);
|
95 |
|
96 |
// Add the accessory to the file chooser
|
97 |
fc.setAccessory(acc); |
98 |
|
99 |
// Nothing else than Web Map Context files allowed
|
100 |
fc.setAcceptAllFileFilterUsed(false);
|
101 |
|
102 |
if (fc.showOpenDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION){ |
103 |
File f = fc.getSelectedFile();
|
104 |
readMapContextFile(f, acc.getSelectedView()); |
105 |
String fileName = f.getAbsolutePath();
|
106 |
lastPath = fileName.substring(0, fileName.lastIndexOf(File.separatorChar)); |
107 |
|
108 |
} |
109 |
fc = null;
|
110 |
} |
111 |
} |
112 |
|
113 |
/**
|
114 |
* Reads a WebMapContext (.cml) file and loads its layers into the destination
|
115 |
* view.
|
116 |
* @param wmcFile
|
117 |
* @param dstView
|
118 |
*/
|
119 |
@SuppressWarnings("unchecked") |
120 |
private void readMapContextFile(File wmcFile, DefaultViewDocument dstView) { |
121 |
WebMapContext wmc = new WebMapContext();
|
122 |
try {
|
123 |
wmc.readFile(wmcFile); |
124 |
} catch (UnsupportedVersionLayerException e) {
|
125 |
JOptionPane.showMessageDialog(
|
126 |
(Component) PluginServices.getMainFrame(),
|
127 |
e.getMessage(), |
128 |
PluginServices.getText(this, "unsupported_version"), |
129 |
JOptionPane.ERROR_MESSAGE);
|
130 |
return;
|
131 |
} catch (ImportMapContextException e) {
|
132 |
e.printStackTrace(); |
133 |
if (e.isCritical()) {
|
134 |
// import impossible: show message and quit
|
135 |
JOptionPane.showMessageDialog(
|
136 |
(Component) PluginServices.getMainFrame(),
|
137 |
e.getMessage(), |
138 |
PluginServices.getText(this, "problems_encountered_while_importing"), |
139 |
JOptionPane.ERROR_MESSAGE);
|
140 |
return;
|
141 |
} else {
|
142 |
JOptionPane.showMessageDialog(
|
143 |
(Component) PluginServices.getMainFrame(),
|
144 |
e.getMessage() + "\n\n" +
|
145 |
PluginServices.getText(this, "edit_layer_properties_to_fix_them"), |
146 |
PluginServices.getText(this, "problems_encountered_while_importing"), |
147 |
JOptionPane.ERROR_MESSAGE);
|
148 |
} |
149 |
} |
150 |
|
151 |
if (dstView == null) { |
152 |
// Since the destination view is null, a new one will be added to
|
153 |
// the project.
|
154 |
// dstView = ProjectFactory.createView(null);
|
155 |
dstView = (DefaultViewDocument) ProjectManager.getInstance().createDocument(ViewManager.TYPENAME); |
156 |
|
157 |
dstView.setName(wmc.title); |
158 |
dstView.setComment("Created from WebMapContext file: "+wmcFile.getName());
|
159 |
|
160 |
// ProjectExtension pe = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class);
|
161 |
// Project theProject = pe.getProject();
|
162 |
final Project theProject = ProjectManager.getInstance().getCurrentProject();
|
163 |
|
164 |
theProject.add(dstView); |
165 |
|
166 |
// show the view in a new window
|
167 |
AbstractViewPanel v = null;
|
168 |
v = new DefaultViewPanel(dstView);
|
169 |
PluginServices.getMDIManager().addWindow(v); |
170 |
} |
171 |
|
172 |
for (int i = 0; i < wmc.layerList.size(); i++) { |
173 |
// WMS layers
|
174 |
if (wmc.layerList.get(i) instanceof FLyrWMS) { |
175 |
FLyrWMS layer = wmc.layerList.get(i); |
176 |
/*
|
177 |
* will connect to get the online resources defined by
|
178 |
* server, because WMC only defines the original URL for
|
179 |
* the server but not for the operations.
|
180 |
*/
|
181 |
try {
|
182 |
WMSServerExplorer explorer = (WMSServerExplorer)layer.getExplorer(); |
183 |
layer.getParameters().setOverrideHost(false);
|
184 |
//TODO: Poner un control de cancelar la conexi?n y pasarlo aqui
|
185 |
explorer.connect(null, true); |
186 |
|
187 |
layer.getParameters().setOnlineResources(explorer.getOnlineResources()); |
188 |
} catch (Exception e) { |
189 |
NotificationManager.addInfo(PluginServices.getText(this, "connect_error")+"\n"+ |
190 |
PluginServices.getText(this, "failed_restoring_online_resource_values")+ |
191 |
" ["+new Date(System.currentTimeMillis()).toString()+"]", |
192 |
e); |
193 |
} |
194 |
dstView.getMapContext().getLayers().addLayer(layer); |
195 |
} |
196 |
} |
197 |
} |
198 |
|
199 |
public boolean isEnabled() { |
200 |
return true; |
201 |
} |
202 |
|
203 |
public boolean isVisible() { |
204 |
return true; |
205 |
} |
206 |
|
207 |
} |