Statistics
| Revision:

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

History | View | Annotate | Download (7.86 KB)

1 5023 jaume
/* 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$
45
 * $Log$
46 6877 cesar
 * Revision 1.7  2006-08-29 07:13:43  cesar
47
 * Rename class com.iver.andami.ui.mdiManager.View to com.iver.andami.ui.mdiManager.IWindow
48
 *
49
 * Revision 1.6  2006/07/21 10:31:05  jaume
50 6495 jaume
 * *** empty log message ***
51
 *
52
 * Revision 1.5  2006/06/19 11:20:26  jorpiell
53 5900 jorpiell
 * Cambiadas las comprobaciones de una clase con == por instanceof.
54
 *
55
 * Revision 1.4  2006/05/30 08:56:57  jaume
56 5530 jaume
 * fixed bug http://www.gvsig.org/phpcollab/tasks/viewtask.php?id=279&msg=addAssignment&PHPSESSID=3fea791a62107ef5325283641039b311#etDAnchor
57
 *
58
 * Revision 1.3  2006/05/12 07:47:39  jaume
59 5158 jaume
 * removed unnecessary imports
60
 *
61
 * Revision 1.2  2006/05/03 11:10:54  jaume
62 5023 jaume
 * *** empty log message ***
63
 *
64 5034 jaume
 * Revision 1.1  2006/05/03 07:51:21  jaume
65
 * *** empty log message ***
66
 *
67 5023 jaume
 * Revision 1.7  2006/05/02 16:12:12  jorpiell
68
 * 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
69
 *
70
 * Revision 1.6  2006/05/02 15:57:44  jaume
71
 * Few better javadoc
72
 *
73
 * Revision 1.5  2006/04/25 11:40:56  jaume
74
 * *** empty log message ***
75
 *
76
 * Revision 1.4  2006/04/21 10:27:32  jaume
77
 * exporting now supported
78
 *
79
 * Revision 1.3  2006/04/20 17:11:54  jaume
80
 * Attempting to export
81
 *
82
 * Revision 1.2  2006/04/19 16:34:29  jaume
83
 * *** empty log message ***
84
 *
85
 * Revision 1.1  2006/04/19 07:57:29  jaume
86
 * *** empty log message ***
87
 *
88
 * Revision 1.3  2006/04/12 17:10:53  jaume
89
 * *** empty log message ***
90
 *
91
 * Revision 1.2  2006/04/07 12:10:37  jaume
92
 * *** empty log message ***
93
 *
94
 * Revision 1.1  2006/04/04 14:22:22  jaume
95
 * Now exports MapContext (not yet tested)
96
 *
97
 *
98
 */
99
package com.iver.cit.gvsig.wmc;
100
101
import java.io.BufferedWriter;
102
import java.io.File;
103
import java.io.FileWriter;
104
import java.io.IOException;
105
import java.util.ArrayList;
106
107
import com.iver.andami.PluginServices;
108
import com.iver.andami.messages.NotificationManager;
109
import com.iver.andami.plugins.Extension;
110
import com.iver.andami.plugins.IExtension;
111
import com.iver.cit.gvsig.ProjectExtension;
112
import com.iver.cit.gvsig.fmap.layers.FLayer;
113
import com.iver.cit.gvsig.fmap.layers.FLayers;
114
import com.iver.cit.gvsig.fmap.layers.FLyrWMS;
115
import com.iver.cit.gvsig.gui.View;
116
import com.iver.cit.gvsig.gui.panels.WebMapContextSettingsPanel;
117
import com.iver.cit.gvsig.project.Project;
118
import com.iver.cit.gvsig.project.ProjectView;
119
120
/**
121
 * Extension to export a view with WMS layers to a OGC's Web Map Context XML
122
 * file
123 6495 jaume
 *
124 5023 jaume
 * @author jaume dom?nguez faus - jaume.dominguez@iver.es
125
 * @author laura d?az s?nchez - laura.diaz@iver.es
126
 */
127
public class ExportWebMapContextExtension extends Extension {
128
        private static ArrayList supportedVersions;
129
        private View viewToExport;
130
        private WebMapContextSettingsPanel mc;
131
        private static IExtension thisExtension;
132
133
        {
134
                supportedVersions = new ArrayList();
135
                supportedVersions.add("1.1.0");
136
        }
137
138
        public void initialize() {
139
                thisExtension = PluginServices
140
                                .getExtension(ExportWebMapContextExtension.class);
141
        }
142
143
        public void execute(String actionCommand) {
144
                if (actionCommand.equals("EXPORT")) {
145 5158 jaume
                        // Here we collect the info
146 5023 jaume
                        ProjectView[] views = getExportableViews();
147
                        mc = new WebMapContextSettingsPanel(views);
148
                        PluginServices.getMDIManager().addView(mc);
149
150
                } else if (actionCommand.equals("DO_EXPORT")) {
151
                        // Here the target file is produced (called from the WebMapContextSettingsPanel)
152
                        String xml = mc.getXML();
153
                        File f = mc.getTargetFile();
154
                        if (xml != null) {
155
                                createFile(f, xml);
156
                        }
157 6495 jaume
158 5023 jaume
                }
159
        }
160
161
        /**
162
         * Takes a File object and its XML contents and stores it as a regular
163
         * file in the file system.
164
         * @param f
165
         * @param xml
166
         */
167
        public static void createFile(File f, String xml) {
168
                if (xml != null) {
169
                        try {
170
                                if (!f.exists()) {
171
                                        f.createNewFile();
172
                                }
173
                                BufferedWriter bw = new BufferedWriter(new FileWriter(f));
174
                                bw.write(xml);
175
                                bw.close();
176
                                bw = null;
177
                        } catch (IOException e) {
178
                                NotificationManager.addError(PluginServices.getText(
179
                                                thisExtension, "error_writting_file"), e);
180
                        }
181
                }
182
        }
183
184
        public boolean isEnabled() {
185
                return true;
186
        }
187
188
        public boolean isVisible() {
189
                // Will be visible if the current project has, at least, one FLyrWMS.
190
                Project project = ((ProjectExtension) PluginServices
191
                                .getExtension(ProjectExtension.class)).getProject();
192 5530 jaume
                if (project == null)
193
                        return false;
194 6877 cesar
                com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager()
195 5023 jaume
                                .getActiveView();
196
                if (f instanceof View) {
197
                        View v = (View) f;
198 5900 jorpiell
                        if (v != null && v  instanceof View) {
199 5023 jaume
                                // Check if the active contains WMS layers. If so, this view
200
                                // will be the one to be exported.
201
                                FLayers lyrs = v.getMapControl().getMapContext().getLayers();
202
                                for (int i = 0; i < lyrs.getLayersCount(); i++) {
203
                                        FLayer lyr = lyrs.getLayer(i);
204 6495 jaume
                                        if (WebMapContext.containsExportableLayers(lyr)) {
205 5023 jaume
                                                viewToExport = v;
206
                                                return true;
207
                                        }
208
                                }
209
                        }
210
                }
211
212
                // Since the active view does not contain WMS layers then will
213
                // see what about the others. In this case, no view set to be
214
                // the exported one.
215
                viewToExport = null;
216
                ArrayList views = project.getViews();
217
                for (int i = 0; i < views.size(); i++) {
218
                        ProjectView v = ((ProjectView) views.get(i));
219
                        if (v != null) {
220
                                FLayers lyrs = v.getMapContext().getLayers();
221
                                for (int j = 0; j < lyrs.getLayersCount(); j++) {
222
                                        FLayer lyr = lyrs.getLayer(j);
223 6495 jaume
                                        if (WebMapContext.containsExportableLayers(lyr))
224 5023 jaume
                                                return true;
225
                                }
226
                        }
227
                }
228
                return false;
229
        }
230 6495 jaume
231 5023 jaume
        /**
232
         * <p>
233
         * Searches the views in the current project that can be exported to a
234 6495 jaume
         * WebMapContext file (with ".cml" extension) and return them in a
235 5023 jaume
         * ProjectView array.<br>
236
         * </p>
237
         * <p>
238
         * A view is exportable to WebMapContext if it contains at least one FLyrWMS
239
         * and in a near future, any other OGC layer such as WCS, WFS, and so on. Only
240
         * these layers will be exported. Other kind of layers are ignored since they
241 5158 jaume
         * are out of the OGC premises.
242 5023 jaume
         * </p>
243
         * @return
244
         */
245
        private ProjectView[] getExportableViews() {
246
                Project project = ((ProjectExtension) PluginServices
247
                                .getExtension(ProjectExtension.class)).getProject();
248 6495 jaume
249 5023 jaume
                ArrayList views = project.getViews();
250
                ArrayList exportableViews = new ArrayList();
251
                if (viewToExport!=null)
252
                        exportableViews.add(viewToExport.getModel());
253 6495 jaume
254 5023 jaume
                for (int i = 0; i < views.size(); i++) {
255
                        ProjectView v = ((ProjectView) views.get(i));
256
                        if (v != null) {
257
                                FLayers lyrs = v.getMapContext().getLayers();
258
                                for (int j = 0; j < lyrs.getLayersCount(); j++) {
259
                                        FLayer lyr = lyrs.getLayer(j);
260
                                        if (lyr instanceof FLyrWMS && !exportableViews.contains(v)) {
261
                                                exportableViews.add(v);
262
                                                break;
263
                                        }
264
                                }
265
                        }
266
                }
267
                return (ProjectView[]) exportableViews.toArray(new ProjectView[0]);
268
        }
269
270
}