Statistics
| Revision:

root / branches / v10 / extensions / extWMS / src / com / iver / cit / gvsig / wmc / ImportWebMapContextExtension.java @ 8981

History | View | Annotate | Download (7.77 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 8981 2006-11-27 08:02:33Z cesar $
45
* $Log$
46
* Revision 1.5.2.4  2006-11-27 08:02:33  cesar
47
* Call initialize method after object creation, it is required now
48
*
49
* Revision 1.5.2.3  2006/11/15 00:08:21  jjdelcerro
50
* *** empty log message ***
51
*
52
* Revision 1.9  2006/09/27 13:28:51  jaume
53
* *** empty log message ***
54
*
55
* Revision 1.8  2006/09/20 10:32:15  jaume
56
* remembers last location
57
*
58
* Revision 1.7  2006/09/15 10:44:24  caballero
59
* extensibilidad de documentos
60
*
61
* Revision 1.6  2006/09/11 15:57:38  jaume
62
* now accepts loading cml files from command line
63
*
64
* Revision 1.5  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.4  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.3  2006/07/21 10:31:05  jaume
71
* *** empty log message ***
72
*
73
* Revision 1.2  2006/05/03 11:10:54  jaume
74
* *** empty log message ***
75
*
76
* Revision 1.1  2006/05/03 07:51:21  jaume
77
* *** empty log message ***
78
*
79
* Revision 1.5  2006/05/02 16:12:12  jorpiell
80
* 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
81
*
82
* Revision 1.4  2006/05/02 15:57:43  jaume
83
* Few better javadoc
84
*
85
* Revision 1.3  2006/04/20 17:11:54  jaume
86
* Attempting to export
87
*
88
* Revision 1.2  2006/04/19 16:34:29  jaume
89
* *** empty log message ***
90
*
91
* Revision 1.1  2006/04/19 07:57:29  jaume
92
* *** empty log message ***
93
*
94
* Revision 1.1  2006/04/12 17:10:53  jaume
95
* *** empty log message ***
96
*
97
*
98
*/
99
package com.iver.cit.gvsig.wmc;
100

    
101
import java.awt.Component;
102
import java.io.File;
103
import java.io.FileNotFoundException;
104

    
105
import javax.swing.JFileChooser;
106
import javax.swing.JOptionPane;
107
import javax.swing.filechooser.FileFilter;
108

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

    
123
public class ImportWebMapContextExtension extends Extension {
124

    
125
        private String lastPath = null;
126

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

    
136
                        readMapContextFile(wmcFile, null);
137
                }
138
        }
139

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

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

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

    
162
                        // Create an accessory for the Web Map Context's file chooser.
163
                        WebMapContextFileChooserAccessory acc = new WebMapContextFileChooserAccessory(currentViewName);
164

    
165
                        // Add the accessory to the file chooser
166
                        fc.setAccessory(acc);
167

    
168
                        // Nothing else than Web Map Context files allowed
169
                        fc.setAcceptAllFileFilterUsed(false);
170

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

    
177
                        }
178
                        fc = null;
179
                }
180
        }
181

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

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

    
226
                        ProjectExtension pe = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class);
227
                        Project theProject = pe.getProject();
228
                        theProject.addDocument(dstView);
229

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

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

    
247
        public boolean isEnabled() {
248
                return true;
249
        }
250

    
251
        public boolean isVisible() {
252
                return true;
253
        }
254

    
255
}