root / trunk / extensions / extWMS / src / com / iver / cit / gvsig / webmapcontext / ExportWebMapContextExtension.java @ 4915
History | View | Annotate | Download (7.1 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: ExportWebMapContextExtension.java 4915 2006-04-21 10:27:32Z jaume $
|
45 |
* $Log$
|
46 |
* Revision 1.4 2006-04-21 10:27:32 jaume
|
47 |
* exporting now supported
|
48 |
*
|
49 |
* Revision 1.3 2006/04/20 17:11:54 jaume
|
50 |
* Attempting to export
|
51 |
*
|
52 |
* Revision 1.2 2006/04/19 16:34:29 jaume
|
53 |
* *** empty log message ***
|
54 |
*
|
55 |
* Revision 1.1 2006/04/19 07:57:29 jaume
|
56 |
* *** empty log message ***
|
57 |
*
|
58 |
* Revision 1.3 2006/04/12 17:10:53 jaume
|
59 |
* *** empty log message ***
|
60 |
*
|
61 |
* Revision 1.2 2006/04/07 12:10:37 jaume
|
62 |
* *** empty log message ***
|
63 |
*
|
64 |
* Revision 1.1 2006/04/04 14:22:22 jaume
|
65 |
* Now exports MapContext (not yet tested)
|
66 |
*
|
67 |
*
|
68 |
*/
|
69 |
package com.iver.cit.gvsig.webmapcontext; |
70 |
|
71 |
import java.awt.Dimension; |
72 |
import java.awt.geom.Rectangle2D; |
73 |
import java.io.BufferedWriter; |
74 |
import java.io.File; |
75 |
import java.io.FileWriter; |
76 |
import java.io.IOException; |
77 |
import java.util.ArrayList; |
78 |
import java.util.HashMap; |
79 |
|
80 |
import com.iver.andami.PluginServices; |
81 |
import com.iver.andami.messages.NotificationManager; |
82 |
import com.iver.andami.plugins.Extension; |
83 |
import com.iver.cit.gvsig.ProjectExtension; |
84 |
import com.iver.cit.gvsig.fmap.MapControl; |
85 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
86 |
import com.iver.cit.gvsig.fmap.layers.FLayers; |
87 |
import com.iver.cit.gvsig.fmap.layers.FLyrWMS; |
88 |
import com.iver.cit.gvsig.fmap.rendering.XmlBuilder; |
89 |
import com.iver.cit.gvsig.gui.View; |
90 |
import com.iver.cit.gvsig.gui.panels.WebMapContextSettingsPanel; |
91 |
import com.iver.cit.gvsig.project.Project; |
92 |
import com.iver.cit.gvsig.project.ProjectView; |
93 |
|
94 |
/**
|
95 |
* Extension to export a view with WMS layers to a OGC's Web Map Context XML
|
96 |
* file
|
97 |
*
|
98 |
* @author jaume dom?nguez faus - jaume.dominguez@iver.es
|
99 |
* @author laura d?az s?nchez - laura.diaz@iver.es
|
100 |
*/
|
101 |
public class ExportWebMapContextExtension implements Extension { |
102 |
private static final String FILE_EXTENSION = ".cml"; |
103 |
private static ArrayList supportedVersions; |
104 |
private View viewToExport; |
105 |
private File dstFile; |
106 |
private WebMapContextSettingsPanel mc;
|
107 |
private static Extension thisExtension; |
108 |
|
109 |
{ |
110 |
supportedVersions = new ArrayList(); |
111 |
supportedVersions.add("1.1.0");
|
112 |
} |
113 |
|
114 |
public void inicializar() { |
115 |
thisExtension = PluginServices |
116 |
.getExtension(ExportWebMapContextExtension.class); |
117 |
} |
118 |
|
119 |
public void execute(String actionCommand) { |
120 |
if (actionCommand.equals("EXPORT")) { |
121 |
// Here we grab the info
|
122 |
ProjectView[] views = getExportableViews();
|
123 |
mc = new WebMapContextSettingsPanel(views);
|
124 |
PluginServices.getMDIManager().addView(mc); |
125 |
|
126 |
} else if (actionCommand.equals("DO_EXPORT")) { |
127 |
// Here the target file is produced (called from the WebMapContextSettingsPanel)
|
128 |
String xml = mc.getXML();
|
129 |
File f = mc.getTargetFile();
|
130 |
if (xml != null) { |
131 |
createFile(f, xml); |
132 |
} |
133 |
|
134 |
} |
135 |
} |
136 |
|
137 |
public static void createFile(File f, String xml) { |
138 |
if (xml != null) { |
139 |
try {
|
140 |
if (!f.exists()) {
|
141 |
f.createNewFile(); |
142 |
} |
143 |
BufferedWriter bw = new BufferedWriter(new FileWriter(f)); |
144 |
bw.write(xml); |
145 |
bw.close(); |
146 |
bw = null;
|
147 |
} catch (IOException e) { |
148 |
NotificationManager.addError(PluginServices.getText( |
149 |
thisExtension, "error_writting_file"), e);
|
150 |
} |
151 |
} |
152 |
} |
153 |
|
154 |
public boolean isEnabled() { |
155 |
return true; |
156 |
} |
157 |
|
158 |
public boolean isVisible() { |
159 |
// Will be visible if the current project has, at least, one FLyrWMS.
|
160 |
Project project = ((ProjectExtension) PluginServices |
161 |
.getExtension(ProjectExtension.class)).getProject(); |
162 |
com.iver.andami.ui.mdiManager.View f = PluginServices.getMDIManager() |
163 |
.getActiveView(); |
164 |
if (f instanceof View) { |
165 |
View v = (View) f; |
166 |
if (v != null && v.getClass() == View.class) { |
167 |
// Check if the active contains WMS layers. If so, this view
|
168 |
// will be the one to be exported.
|
169 |
FLayers lyrs = v.getMapControl().getMapContext().getLayers(); |
170 |
for (int i = 0; i < lyrs.getLayersCount(); i++) { |
171 |
FLayer lyr = lyrs.getLayer(i); |
172 |
if (lyr instanceof FLyrWMS) { |
173 |
viewToExport = v; |
174 |
return true; |
175 |
} |
176 |
} |
177 |
} |
178 |
} |
179 |
|
180 |
// Since the active view does not contain WMS layers then will
|
181 |
// see what about the others. In this case, no view set to be
|
182 |
// the exported one.
|
183 |
viewToExport = null;
|
184 |
ArrayList views = project.getViews();
|
185 |
for (int i = 0; i < views.size(); i++) { |
186 |
ProjectView v = ((ProjectView) views.get(i)); |
187 |
if (v != null) { |
188 |
FLayers lyrs = v.getMapContext().getLayers(); |
189 |
for (int j = 0; j < lyrs.getLayersCount(); j++) { |
190 |
FLayer lyr = lyrs.getLayer(j); |
191 |
if (lyr instanceof FLyrWMS) |
192 |
return true; |
193 |
} |
194 |
} |
195 |
} |
196 |
return false; |
197 |
} |
198 |
|
199 |
private ProjectView[] getExportableViews() { |
200 |
Project project = ((ProjectExtension) PluginServices |
201 |
.getExtension(ProjectExtension.class)).getProject(); |
202 |
|
203 |
ArrayList views = project.getViews();
|
204 |
ArrayList exportableViews = new ArrayList(); |
205 |
if (viewToExport!=null) |
206 |
exportableViews.add(viewToExport.getModel()); |
207 |
|
208 |
for (int i = 0; i < views.size(); i++) { |
209 |
ProjectView v = ((ProjectView) views.get(i)); |
210 |
if (v != null) { |
211 |
FLayers lyrs = v.getMapContext().getLayers(); |
212 |
for (int j = 0; j < lyrs.getLayersCount(); j++) { |
213 |
FLayer lyr = lyrs.getLayer(j); |
214 |
if (lyr instanceof FLyrWMS && !exportableViews.contains(v)) { |
215 |
exportableViews.add(v); |
216 |
break;
|
217 |
} |
218 |
} |
219 |
} |
220 |
} |
221 |
return (ProjectView[]) exportableViews.toArray(new ProjectView[0]); |
222 |
} |
223 |
|
224 |
/**
|
225 |
* Establishes the (new or not) destination file.
|
226 |
*
|
227 |
* @param dst
|
228 |
*/
|
229 |
public void setDestinationFile(File dst) { |
230 |
dstFile = new File(dst.getAbsolutePath() + FILE_EXTENSION); |
231 |
} |
232 |
|
233 |
// /**
|
234 |
// * Sets the MapContext version to be applied.
|
235 |
// *
|
236 |
// * @param ver
|
237 |
// */
|
238 |
// public void setVersion(String ver) {
|
239 |
// version = ver;
|
240 |
// }
|
241 |
//
|
242 |
// /**
|
243 |
// * Sets the name used to identify the contents of the MapContext document
|
244 |
// *
|
245 |
// * @param id
|
246 |
// */
|
247 |
// public void setId(String id) {
|
248 |
// this.id = id;
|
249 |
// }
|
250 |
//
|
251 |
// /**
|
252 |
// * Sets the size of the map that will be considered to create the MapContext
|
253 |
// * document.
|
254 |
// *
|
255 |
// * @param Dimension,
|
256 |
// * the size in pixels (floating point values will be rounded to
|
257 |
// * integers)
|
258 |
// */
|
259 |
// public void setMapSize(Dimension d) {
|
260 |
// sz = d;
|
261 |
// }
|
262 |
} |