Statistics
| Revision:

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

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

    
92
import java.awt.Component;
93
import java.io.File;
94
import java.io.FileNotFoundException;
95

    
96
import javax.swing.JFileChooser;
97
import javax.swing.JOptionPane;
98
import javax.swing.filechooser.FileFilter;
99

    
100
import com.iver.andami.PluginServices;
101
import com.iver.andami.messages.NotificationListener;
102
import com.iver.andami.messages.NotificationManager;
103
import com.iver.andami.plugins.Extension;
104
import com.iver.andami.ui.mdiManager.IWindow;
105
import com.iver.cit.gvsig.ProjectExtension;
106
import com.iver.cit.gvsig.fmap.drivers.UnsupportedVersionException;
107
import com.iver.cit.gvsig.fmap.exceptions.ImportMapContextException;
108
import com.iver.cit.gvsig.fmap.layers.FLyrWMS;
109
import com.iver.cit.gvsig.gui.panels.WebMapContextFileChooserAccessory;
110
import com.iver.cit.gvsig.project.Project;
111
import com.iver.cit.gvsig.project.ProjectFactory;
112
import com.iver.cit.gvsig.project.documents.view.ProjectView;
113

    
114
public class ImportWebMapContextExtension extends Extension {
115

    
116
        private String lastPath = null;
117

    
118
        public void initialize() {
119
                String[] arguments = PluginServices.getArguments();
120
                if (arguments[arguments.length-1].toLowerCase().endsWith(".cml")) {
121
                        File wmcFile = new File(arguments[arguments.length-1]);
122
                        if (!wmcFile.exists()) {
123
                                NotificationManager.addError(PluginServices.getText(this, "could_not_find_web_map_context_file"), new FileNotFoundException());
124
                                return;
125
                        }
126

    
127
                        readMapContextFile(wmcFile, null);
128
                }
129
        }
130

    
131
        public void execute(String actionCommand) {
132
                if (actionCommand.equals("IMPORT")) {
133
                        JFileChooser fc = new JFileChooser(lastPath);
134
                        fc.setFileFilter(new FileFilter() {
135
                                public boolean accept(File f) {
136
                                        return f.isDirectory() || f.getAbsolutePath().toLowerCase().endsWith(WebMapContext.FILE_EXTENSION);
137
                                }
138

    
139
                                public String getDescription() {
140
                                        return PluginServices.getText(this, "ogc_mapcontext_file")+" (*.cml)";
141
                                }
142
                        });
143
                        IWindow v = PluginServices.getMDIManager().getActiveWindow();
144

    
145
                        // If the current active view is a gvSIG's view, we'll keep its name to
146
                        // show it at the JFileChooser's accessory.
147
                        String currentViewName;
148
                        if (v instanceof com.iver.cit.gvsig.project.documents.view.gui.View)
149
                                currentViewName = ((com.iver.cit.gvsig.project.documents.view.gui.View) v).getModel().getName();
150
                        else
151
                                currentViewName = null;
152

    
153
                        // Create an accessory for the Web Map Context's file chooser.
154
                        WebMapContextFileChooserAccessory acc = new WebMapContextFileChooserAccessory(currentViewName);
155

    
156
                        // Add the accessory to the file chooser
157
                        fc.setAccessory(acc);
158

    
159
                        // Nothing else than Web Map Context files allowed
160
                        fc.setAcceptAllFileFilterUsed(false);
161

    
162
                        if (fc.showOpenDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION){
163
                                File f = fc.getSelectedFile();
164
                                readMapContextFile(f, acc.getSelectedView());
165
                                String fileName = f.getAbsolutePath();
166
                                lastPath  = fileName.substring(0, fileName.lastIndexOf(File.separatorChar));
167

    
168
                        }
169
                        fc = null;
170
                }
171
        }
172

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

    
210
                if (dstView == null) {
211
                        // Since the destination view is null, a new one will be added to
212
                        // the project.
213
                        dstView = ProjectFactory.createView(null);
214
                        dstView.setName(wmc.title);
215
                        dstView.setComment("Created from WebMapContext file: "+wmcFile.getName());
216

    
217
                        ProjectExtension pe = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class);
218
                        Project theProject = pe.getProject();
219
                        theProject.addDocument(dstView);
220

    
221
                        // show the view in a new window
222
                        com.iver.cit.gvsig.project.documents.view.gui.View v = null;
223
                        v = new com.iver.cit.gvsig.project.documents.view.gui.View();
224
                        v.setModel(dstView);
225
                        PluginServices.getMDIManager().addWindow(v);
226
                }
227

    
228
                for (int i = 0; i < wmc.layerList.size(); i++) {
229
                        // WMS layers
230
                        if (wmc.layerList.get(i) instanceof FLyrWMS) {
231
                                FLyrWMS layer = (FLyrWMS) wmc.layerList.get(i);
232
                                dstView.getMapContext().getLayers().addLayer(layer);
233
                        }
234
                }
235
        }
236

    
237
        public boolean isEnabled() {
238
                return true;
239
        }
240

    
241
        public boolean isVisible() {
242
                return true;
243
        }
244

    
245
}