Statistics
| Revision:

root / trunk / extensions / extWMS / src / com / iver / cit / gvsig / wmc / ExportWebMapContextExtension.java @ 32876

History | View | Annotate | Download (8.74 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 32876 2010-06-24 10:57:33Z jpiera $
45
 * $Log$
46
 * Revision 1.14  2007-01-08 07:57:34  jaume
47
 * *** empty log message ***
48
 *
49
 * Revision 1.13  2006/09/27 13:28:38  jaume
50
 * *** empty log message ***
51
 *
52
 * Revision 1.12  2006/09/20 10:38:43  jaume
53
 * *** empty log message ***
54
 *
55
 * Revision 1.11  2006/09/20 07:45:21  caballero
56
 * constante registerName
57
 *
58
 * Revision 1.10  2006/09/18 08:28:44  caballero
59
 * cambio de nombre
60
 *
61
 * Revision 1.9  2006/09/15 10:44:24  caballero
62
 * extensibilidad de documentos
63
 *
64
 * Revision 1.8  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.7  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.6  2006/07/21 10:31:05  jaume
71
 * *** empty log message ***
72
 *
73
 * Revision 1.5  2006/06/19 11:20:26  jorpiell
74
 * Cambiadas las comprobaciones de una clase con == por instanceof.
75
 *
76
 * Revision 1.4  2006/05/30 08:56:57  jaume
77
 * fixed bug http://www.gvsig.org/phpcollab/tasks/viewtask.php?id=279&msg=addAssignment&PHPSESSID=3fea791a62107ef5325283641039b311#etDAnchor
78
 *
79
 * Revision 1.3  2006/05/12 07:47:39  jaume
80
 * removed unnecessary imports
81
 *
82
 * Revision 1.2  2006/05/03 11:10:54  jaume
83
 * *** empty log message ***
84
 *
85
 * Revision 1.1  2006/05/03 07:51:21  jaume
86
 * *** empty log message ***
87
 *
88
 * Revision 1.7  2006/05/02 16:12:12  jorpiell
89
 * 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
90
 *
91
 * Revision 1.6  2006/05/02 15:57:44  jaume
92
 * Few better javadoc
93
 *
94
 * Revision 1.5  2006/04/25 11:40:56  jaume
95
 * *** empty log message ***
96
 *
97
 * Revision 1.4  2006/04/21 10:27:32  jaume
98
 * exporting now supported
99
 *
100
 * Revision 1.3  2006/04/20 17:11:54  jaume
101
 * Attempting to export
102
 *
103
 * Revision 1.2  2006/04/19 16:34:29  jaume
104
 * *** empty log message ***
105
 *
106
 * Revision 1.1  2006/04/19 07:57:29  jaume
107
 * *** empty log message ***
108
 *
109
 * Revision 1.3  2006/04/12 17:10:53  jaume
110
 * *** empty log message ***
111
 *
112
 * Revision 1.2  2006/04/07 12:10:37  jaume
113
 * *** empty log message ***
114
 *
115
 * Revision 1.1  2006/04/04 14:22:22  jaume
116
 * Now exports MapContext (not yet tested)
117
 *
118
 *
119
 */
120
package com.iver.cit.gvsig.wmc;
121

    
122
import java.io.BufferedWriter;
123
import java.io.File;
124
import java.io.FileWriter;
125
import java.io.IOException;
126
import java.util.ArrayList;
127

    
128
import com.iver.andami.PluginServices;
129
import com.iver.andami.messages.NotificationManager;
130
import com.iver.andami.plugins.Extension;
131
import com.iver.andami.plugins.IExtension;
132
import com.iver.andami.ui.mdiManager.IWindow;
133
import com.iver.cit.gvsig.ProjectExtension;
134
import com.iver.cit.gvsig.fmap.layers.FLayer;
135
import com.iver.cit.gvsig.fmap.layers.FLayers;
136
import com.iver.cit.gvsig.fmap.layers.FLyrWMS;
137
import com.iver.cit.gvsig.gui.panels.WebMapContextSettingsPanel;
138
import com.iver.cit.gvsig.project.Project;
139
import com.iver.cit.gvsig.project.documents.view.ProjectView;
140
import com.iver.cit.gvsig.project.documents.view.ProjectViewFactory;
141
import com.iver.cit.gvsig.project.documents.view.gui.BaseView;
142

    
143
/**
144
 * Extension to export a view with WMS layers to a OGC's Web Map Context XML
145
 * file
146
 *
147
 * @author jaume dom?nguez faus - jaume.dominguez@iver.es
148
 * @author laura d?az s?nchez - laura.diaz@iver.es
149
 */
150
public class ExportWebMapContextExtension extends Extension {
151
        private static ArrayList supportedVersions;
152
        private BaseView viewToExport;
153
        private WebMapContextSettingsPanel mc;
154
        private static IExtension thisExtension;
155

    
156
        {
157
                supportedVersions = new ArrayList();
158
                supportedVersions.add("1.1.0");
159
        }
160

    
161
        public void initialize() {
162
                thisExtension = PluginServices
163
                                .getExtension(ExportWebMapContextExtension.class);
164
        }
165

    
166
        public void execute(String actionCommand) {
167
                if (actionCommand.equals("EXPORT")) {
168
                        // Here we collect the info
169
                        ProjectView[] views = getExportableViews();
170
                        if (views.length <= 0)
171
                                return;
172
                        mc = new WebMapContextSettingsPanel(views);
173
                        PluginServices.getMDIManager().addWindow(mc);
174

    
175
                } else if (actionCommand.equals("DO_EXPORT")) {
176
                        // Here the target file is produced (called from the WebMapContextSettingsPanel)
177
                        String xml = mc.getXML();
178
                        File f = mc.getTargetFile();
179
                        if (xml != null) {
180
                                createFile(f, xml);
181
                        }
182

    
183
                }
184
        }
185

    
186
        /**
187
         * Takes a File object and its XML contents and stores it as a regular
188
         * file in the file system.
189
         * @param f
190
         * @param xml
191
         */
192
        public static void createFile(File f, String xml) {
193
                if (xml != null) {
194
                        try {
195
                                if (!f.exists()) {
196
                                        f.createNewFile();
197
                                }
198
                                BufferedWriter bw = new BufferedWriter(new FileWriter(f));
199
                                bw.write(xml);
200
                                bw.close();
201
                                bw = null;
202
                        } catch (IOException e) {
203
                                NotificationManager.addError(PluginServices.getText(
204
                                                thisExtension, "error_writting_file"), e);
205
                        }
206
                }
207
        }
208

    
209
        public boolean isEnabled() {
210
                return true;
211
        }
212

    
213
        public boolean isVisible() {
214
                // Will be visible if the current project has, at least, one FLyrWMS.
215
                Project project = ((ProjectExtension) PluginServices
216
                                .getExtension(ProjectExtension.class)).getProject();
217
                if (project == null)
218
                        return false;
219
                IWindow f = PluginServices.getMDIManager().getActiveWindow();
220
                if (f instanceof BaseView) {
221
                        BaseView v = (BaseView) f;
222
                        if (v != null && v  instanceof BaseView) {
223
                                // Check if the active contains WMS layers. If so, this view
224
                                // will be the one to be exported.
225
                                FLayers lyrs = v.getMapControl().getMapContext().getLayers();
226
                                for (int i = 0; i < lyrs.getLayersCount(); i++) {
227
                                        FLayer lyr = lyrs.getLayer(i);
228
                                        if (WebMapContext.containsExportableLayers(lyr)) {
229
                                                viewToExport = v;
230
                                                return true;
231
                                        }
232
                                }
233
                        }
234
                }
235

    
236
                // Since the active view does not contain WMS layers then will
237
                // see what about the others. In this case, no view is set to be
238
                // the exported one.
239
                viewToExport = null;
240
                ArrayList views = project.getDocumentsByType(ProjectViewFactory.registerName);
241
                for (int i = 0; i < views.size(); i++) {
242
                        ProjectView v = ((ProjectView) views.get(i));
243
                        if (v != null) {
244
                                FLayers lyrs = v.getMapContext().getLayers();
245
                                for (int j = 0; j < lyrs.getLayersCount(); j++) {
246
                                        FLayer lyr = lyrs.getLayer(j);
247
                                        if (WebMapContext.containsExportableLayers(lyr))
248
                                                return true;
249
                                }
250
                        }
251
                }
252
                return false;
253
        }
254

    
255
        /**
256
         * <p>
257
         * Searches the views in the current project that can be exported to a
258
         * WebMapContext file (with ".cml" extension) and return them in a
259
         * ProjectView array.<br>
260
         * </p>
261
         * <p>
262
         * A view is exportable to WebMapContext if it contains at least one FLyrWMS
263
         * and in a near future, any other OGC layer such as WCS, WFS, and so on. Only
264
         * these layers will be exported. Other kind of layers are ignored since they
265
         * are out of the OGC premises.
266
         * </p>
267
         * @return
268
         */
269
        private ProjectView[] getExportableViews() {
270
                Project project = ((ProjectExtension) PluginServices
271
                                .getExtension(ProjectExtension.class)).getProject();
272

    
273
                ArrayList views = project.getDocumentsByType(ProjectViewFactory.registerName);
274
                ArrayList exportableViews = new ArrayList();
275
                if (viewToExport!=null)
276
                        exportableViews.add(viewToExport.getModel());
277

    
278
                for (int i = 0; i < views.size(); i++) {
279
                        ProjectView v = ((ProjectView) views.get(i));
280
                        if (v != null) {
281
                                FLayers lyrs = v.getMapContext().getLayers();
282
                                for (int j = 0; j < lyrs.getLayersCount(); j++) {
283
                                        FLayer lyr = lyrs.getLayer(j);
284
                                        if (lyr instanceof FLyrWMS && !exportableViews.contains(v)) {
285
                                                exportableViews.add(v);
286
                                                break;
287
                                        }
288
                                }
289
                        }
290
                }
291
                return (ProjectView[]) exportableViews.toArray(new ProjectView[0]);
292
        }
293

    
294
}