Statistics
| Revision:

root / trunk / extensions / extWMS / src / com / iver / cit / gvsig / wmc / ImportWebMapContextExtension.java @ 9044

History | View | Annotate | Download (7.68 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: ImportWebMapContextExtension.java 9044 2006-11-28 11:46:59Z cesar $
45
* $Log$
46
* Revision 1.10  2006-11-28 11:46:59  cesar
47
* Add support to persist the document internal layout
48
*
49
* Revision 1.9  2006/09/27 13:28:51  jaume
50
* *** empty log message ***
51
*
52
* Revision 1.8  2006/09/20 10:32:15  jaume
53
* remembers last location
54
*
55
* Revision 1.7  2006/09/15 10:44:24  caballero
56
* extensibilidad de documentos
57
*
58
* Revision 1.6  2006/09/11 15:57:38  jaume
59
* now accepts loading cml files from command line
60
*
61
* Revision 1.5  2006/08/29 07:56:15  cesar
62
* Rename the *View* family of classes to *Window* (ie: SingletonView to SingletonWindow, ViewInfo to WindowInfo, etc)
63
*
64
* Revision 1.4  2006/08/29 07:13:43  cesar
65
* Rename class com.iver.andami.ui.mdiManager.View to com.iver.andami.ui.mdiManager.IWindow
66
*
67
* Revision 1.3  2006/07/21 10:31:05  jaume
68
* *** empty log message ***
69
*
70
* Revision 1.2  2006/05/03 11:10:54  jaume
71
* *** empty log message ***
72
*
73
* Revision 1.1  2006/05/03 07:51:21  jaume
74
* *** empty log message ***
75
*
76
* Revision 1.5  2006/05/02 16:12:12  jorpiell
77
* 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
78
*
79
* Revision 1.4  2006/05/02 15:57:43  jaume
80
* Few better javadoc
81
*
82
* Revision 1.3  2006/04/20 17:11:54  jaume
83
* Attempting to export
84
*
85
* Revision 1.2  2006/04/19 16:34:29  jaume
86
* *** empty log message ***
87
*
88
* Revision 1.1  2006/04/19 07:57:29  jaume
89
* *** empty log message ***
90
*
91
* Revision 1.1  2006/04/12 17:10:53  jaume
92
* *** empty log message ***
93
*
94
*
95
*/
96
package com.iver.cit.gvsig.wmc;
97

    
98
import java.awt.Component;
99
import java.io.File;
100
import java.io.FileNotFoundException;
101

    
102
import javax.swing.JFileChooser;
103
import javax.swing.JOptionPane;
104
import javax.swing.filechooser.FileFilter;
105

    
106
import com.iver.andami.PluginServices;
107
import com.iver.andami.messages.NotificationManager;
108
import com.iver.andami.plugins.Extension;
109
import com.iver.andami.ui.mdiManager.IWindow;
110
import com.iver.cit.gvsig.ProjectExtension;
111
import com.iver.cit.gvsig.fmap.drivers.UnsupportedVersionException;
112
import com.iver.cit.gvsig.fmap.exceptions.ImportMapContextException;
113
import com.iver.cit.gvsig.fmap.layers.FLyrWMS;
114
import com.iver.cit.gvsig.gui.panels.WebMapContextFileChooserAccessory;
115
import com.iver.cit.gvsig.project.Project;
116
import com.iver.cit.gvsig.project.ProjectFactory;
117
import com.iver.cit.gvsig.project.documents.view.ProjectView;
118
import com.iver.cit.gvsig.project.documents.view.gui.View;
119

    
120
public class ImportWebMapContextExtension extends Extension {
121

    
122
        private String lastPath = null;
123

    
124
        public void initialize() {
125
                String[] arguments = PluginServices.getArguments();
126
                if (arguments[arguments.length-1].toLowerCase().endsWith(".cml")) {
127
                        File wmcFile = new File(arguments[arguments.length-1]);
128
                        if (!wmcFile.exists()) {
129
                                NotificationManager.addError(PluginServices.getText(this, "could_not_find_web_map_context_file"), new FileNotFoundException());
130
                                return;
131
                        }
132

    
133
                        readMapContextFile(wmcFile, null);
134
                }
135
        }
136

    
137
        public void execute(String actionCommand) {
138
                if (actionCommand.equals("IMPORT")) {
139
                        JFileChooser fc = new JFileChooser(lastPath);
140
                        fc.setFileFilter(new FileFilter() {
141
                                public boolean accept(File f) {
142
                                        return f.isDirectory() || f.getAbsolutePath().toLowerCase().endsWith(WebMapContext.FILE_EXTENSION);
143
                                }
144

    
145
                                public String getDescription() {
146
                                        return PluginServices.getText(this, "ogc_mapcontext_file")+" (*.cml)";
147
                                }
148
                        });
149
                        IWindow v = PluginServices.getMDIManager().getActiveWindow();
150

    
151
                        // If the current active view is a gvSIG's view, we'll keep its name to
152
                        // show it at the JFileChooser's accessory.
153
                        String currentViewName;
154
                        if (v instanceof View)
155
                                currentViewName = ((View) v).getModel().getName();
156
                        else
157
                                currentViewName = null;
158

    
159
                        // Create an accessory for the Web Map Context's file chooser.
160
                        WebMapContextFileChooserAccessory acc = new WebMapContextFileChooserAccessory(currentViewName);
161

    
162
                        // Add the accessory to the file chooser
163
                        fc.setAccessory(acc);
164

    
165
                        // Nothing else than Web Map Context files allowed
166
                        fc.setAcceptAllFileFilterUsed(false);
167

    
168
                        if (fc.showOpenDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION){
169
                                File f = fc.getSelectedFile();
170
                                readMapContextFile(f, acc.getSelectedView());
171
                                String fileName = f.getAbsolutePath();
172
                                lastPath  = fileName.substring(0, fileName.lastIndexOf(File.separatorChar));
173

    
174
                        }
175
                        fc = null;
176
                }
177
        }
178

    
179
        /**
180
         * Reads a WebMapContext (.cml) file and loads its layers into the destination
181
         * view.
182
         * @param wmcFile
183
         * @param dstView
184
         */
185
        private void readMapContextFile(File wmcFile, ProjectView dstView) {
186
                WebMapContext wmc = new WebMapContext();
187
                try {
188
                        wmc.readFile(wmcFile);
189
                } catch (UnsupportedVersionException e) {
190
                        JOptionPane.showMessageDialog(
191
                                        (Component) PluginServices.getMainFrame(),
192
                                        e.getMessage(),
193
                                        PluginServices.getText(this, "unsupported_version"),
194
                                        JOptionPane.ERROR_MESSAGE);
195
                        return;
196
                } catch (ImportMapContextException e) {
197
                        e.printStackTrace();
198
                        if (e.isCritical()) {
199
                                // import impossible: show message and quit
200
                                JOptionPane.showMessageDialog(
201
                                                (Component) PluginServices.getMainFrame(),
202
                                                e.getMessage(),
203
                                                PluginServices.getText(this, "problems_encountered_while_importing"),
204
                                                JOptionPane.ERROR_MESSAGE);
205
                                return;
206
                        } else {
207
                                JOptionPane.showMessageDialog(
208
                                                (Component) PluginServices.getMainFrame(),
209
                                                e.getMessage() + "\n\n" +
210
                                                PluginServices.getText(this, "edit_layer_properties_to_fix_them"),
211
                                                PluginServices.getText(this, "problems_encountered_while_importing"),
212
                                                JOptionPane.ERROR_MESSAGE);
213
                        }
214
                }
215

    
216
                if (dstView == null) {
217
                        // Since the destination view is null, a new one will be added to
218
                        // the project.
219
                        dstView = ProjectFactory.createView(null);
220
                        dstView.setName(wmc.title);
221
                        dstView.setComment("Created from WebMapContext file: "+wmcFile.getName());
222

    
223
                        ProjectExtension pe = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class);
224
                        Project theProject = pe.getProject();
225
                        theProject.addDocument(dstView);
226

    
227
                        // show the view in a new window
228
                        View v = null;
229
                        v = new View();
230
                        v.initialize();
231
                        v.setModel(dstView);
232
                        PluginServices.getMDIManager().addWindow(v);
233
                }
234

    
235
                for (int i = 0; i < wmc.layerList.size(); i++) {
236
                        // WMS layers
237
                        if (wmc.layerList.get(i) instanceof FLyrWMS) {
238
                                FLyrWMS layer = (FLyrWMS) wmc.layerList.get(i);
239
                                dstView.getMapContext().getLayers().addLayer(layer);
240
                        }
241
                }
242
        }
243

    
244
        public boolean isEnabled() {
245
                return true;
246
        }
247

    
248
        public boolean isVisible() {
249
                return true;
250
        }
251

    
252
}