Statistics
| Revision:

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 / ExportWebMapContextExtension.java

History | View | Annotate | Download (6.68 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.io.BufferedWriter;
26
import java.io.File;
27
import java.io.FileWriter;
28
import java.io.IOException;
29
import java.util.ArrayList;
30
import java.util.List;
31

    
32
import org.gvsig.andami.PluginServices;
33
import org.gvsig.andami.messages.NotificationManager;
34
import org.gvsig.andami.plugins.Extension;
35
import org.gvsig.andami.plugins.IExtension;
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.Document;
40
import org.gvsig.app.project.documents.view.DefaultViewDocument;
41
import org.gvsig.app.project.documents.view.ViewManager;
42
import org.gvsig.app.project.documents.view.gui.AbstractViewPanel;
43
import org.gvsig.fmap.mapcontext.layers.FLayer;
44
import org.gvsig.fmap.mapcontext.layers.FLayers;
45
import org.gvsig.raster.wms.app.wmsclient.gui.panel.WebMapContextSettingsPanel;
46
import org.gvsig.raster.wms.app.wmsclient.layer.FLyrWMS;
47

    
48

    
49
/**
50
 * Extension to export a view with WMS layers to a OGC's Web Map Context XML
51
 * file
52
 *
53
 * @author jaume dom?nguez faus - jaume.dominguez@iver.es
54
 * @author laura d?az s?nchez - laura.diaz@iver.es
55
 */
56
public class ExportWebMapContextExtension extends Extension {
57
        private static ArrayList<String>   supportedVersions;
58
        private AbstractViewPanel          viewToExport;
59
        private WebMapContextSettingsPanel mc;
60
        private static IExtension          thisExtension;
61

    
62
        {
63
                supportedVersions = new ArrayList<String>();
64
                supportedVersions.add("1.1.0");
65
        }
66

    
67
        public void initialize() {
68
                thisExtension = PluginServices
69
                                .getExtension(ExportWebMapContextExtension.class);
70
        }
71

    
72
        public void execute(String actionCommand) {
73
                if (actionCommand.equals("EXPORT")) {
74
                        // Here we collect the info
75
                        DefaultViewDocument[] views = getExportableViews();
76
                        if (views.length <= 0) {
77
                                return;
78
                        }
79
                        mc = new WebMapContextSettingsPanel(views);
80
                        PluginServices.getMDIManager().addWindow(mc);
81

    
82
                } else if (actionCommand.equals("DO_EXPORT")) {
83
                        // Here the target file is produced (called from the WebMapContextSettingsPanel)
84
                        String xml = mc.getXML();
85
                        File f = mc.getTargetFile();
86
                        if (xml != null) {
87
                                createFile(f, xml);
88
                        }
89

    
90
                }
91
        }
92

    
93
        /**
94
         * Takes a File object and its XML contents and stores it as a regular
95
         * file in the file system.
96
         * @param f
97
         * @param xml
98
         */
99
        public static void createFile(File f, String xml) {
100
                if (xml != null) {
101
                        try {
102
                                if (!f.exists()) {
103
                                        f.createNewFile();
104
                                }
105
                                BufferedWriter bw = new BufferedWriter(new FileWriter(f));
106
                                bw.write(xml);
107
                                bw.close();
108
                                bw = null;
109
                        } catch (IOException e) {
110
                                NotificationManager.addError(PluginServices.getText(
111
                                                thisExtension, "error_writting_file"), e);
112
                        }
113
                }
114
        }
115

    
116
        public boolean isEnabled() {
117
                return true;
118
        }
119

    
120
        public boolean isVisible() {
121
                // Will be visible if the current project has, at least, one FLyrWMS.
122
//                Project project = ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).getProject();
123
                final Project project = ProjectManager.getInstance().getCurrentProject();
124

    
125
                if (project == null) {
126
                        return false;
127
                }
128
                IWindow f = PluginServices.getMDIManager().getActiveWindow();
129
                if (f instanceof AbstractViewPanel) {
130
                        AbstractViewPanel v = (AbstractViewPanel) f;
131
                        if (v != null && v  instanceof AbstractViewPanel) {
132
                                // Check if the active contains WMS layers. If so, this view
133
                                // will be the one to be exported.
134
                                FLayers lyrs = v.getMapControl().getMapContext().getLayers();
135
                                for (int i = 0; i < lyrs.getLayersCount(); i++) {
136
                                        FLayer lyr = lyrs.getLayer(i);
137
                                        if (WebMapContext.containsExportableLayers(lyr)) {
138
                                                viewToExport = v;
139
                                                return true;
140
                                        }
141
                                }
142
                        }
143
                }
144

    
145
                // Since the active view does not contain WMS layers then will
146
                // see what about the others. In this case, no view is set to be
147
                // the exported one.
148
                viewToExport = null;
149
                List<Document> views = project.getDocuments(ViewManager.TYPENAME);
150
                for (int i = 0; i < views.size(); i++) {
151
                        DefaultViewDocument v = ((DefaultViewDocument) views.get(i));
152
                        if (v != null) {
153
                                FLayers lyrs = v.getMapContext().getLayers();
154
                                for (int j = 0; j < lyrs.getLayersCount(); j++) {
155
                                        FLayer lyr = lyrs.getLayer(j);
156
                                        if (WebMapContext.containsExportableLayers(lyr)) {
157
                                                return true;
158
                                        }
159
                                }
160
                        }
161
                }
162
                return false;
163
        }
164

    
165
        /**
166
         * <p>
167
         * Searches the views in the current project that can be exported to a
168
         * WebMapContext file (with ".cml" extension) and return them in a
169
         * ProjectView array.<br>
170
         * </p>
171
         * <p>
172
         * A view is exportable to WebMapContext if it contains at least one FLyrWMS
173
         * and in a near future, any other OGC layer such as WebCoverageService, WFS, and so on. Only
174
         * these layers will be exported. Other kind of layers are ignored since they
175
         * are out of the OGC premises.
176
         * </p>
177
         * @return
178
         */
179
        @SuppressWarnings({ "unchecked", "deprecation" })
180
        private DefaultViewDocument[] getExportableViews() {
181
//                Project project = ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).getProject();
182
//                ArrayList views = project.getDocumentsByType(ProjectViewFactory.registerName);
183
                final Project project = ProjectManager.getInstance().getCurrentProject();
184

    
185
                List<Document> views = project.getDocuments(ViewManager.TYPENAME);
186

    
187
                List exportableViews = new ArrayList();
188
                if (viewToExport!=null) {
189
                        exportableViews.add(viewToExport.getModel());
190
                }
191

    
192
                for (int i = 0; i < views.size(); i++) {
193
                        DefaultViewDocument v = ((DefaultViewDocument) views.get(i));
194
                        if (v != null) {
195
                                FLayers lyrs = v.getMapContext().getLayers();
196
                                for (int j = 0; j < lyrs.getLayersCount(); j++) {
197
                                        FLayer lyr = lyrs.getLayer(j);
198
                                        if (lyr instanceof FLyrWMS && !exportableViews.contains(v)) {
199
                                                exportableViews.add(v);
200
                                                break;
201
                                        }
202
                                }
203
                        }
204
                }
205
                return (DefaultViewDocument[]) exportableViews.toArray(new DefaultViewDocument[0]);
206
        }
207

    
208
}