Statistics
| Revision:

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

History | View | Annotate | Download (7.41 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.5.2.2  2006-09-20 10:32:26  jaume
47
* remembers last location
48
*
49
* Revision 1.5.2.1  2006/09/12 06:56:33  jaume
50
* *** empty log message ***
51
*
52
* Revision 1.5  2006/08/29 07:56:15  cesar
53
* Rename the *View* family of classes to *Window* (ie: SingletonView to SingletonWindow, ViewInfo to WindowInfo, etc)
54
*
55
* Revision 1.4  2006/08/29 07:13:43  cesar
56
* Rename class com.iver.andami.ui.mdiManager.View to com.iver.andami.ui.mdiManager.IWindow
57
*
58
* Revision 1.3  2006/07/21 10:31:05  jaume
59
* *** empty log message ***
60
*
61
* Revision 1.2  2006/05/03 11:10:54  jaume
62
* *** empty log message ***
63
*
64
* Revision 1.1  2006/05/03 07:51:21  jaume
65
* *** empty log message ***
66
*
67
* Revision 1.5  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.4  2006/05/02 15:57:43  jaume
71
* Few better javadoc
72
*
73
* Revision 1.3  2006/04/20 17:11:54  jaume
74
* Attempting to export
75
*
76
* Revision 1.2  2006/04/19 16:34:29  jaume
77
* *** empty log message ***
78
*
79
* Revision 1.1  2006/04/19 07:57:29  jaume
80
* *** empty log message ***
81
*
82
* Revision 1.1  2006/04/12 17:10:53  jaume
83
* *** empty log message ***
84
*
85
*
86
*/
87
package com.iver.cit.gvsig.wmc;
88

    
89
import java.awt.Component;
90
import java.io.File;
91
import java.io.FileNotFoundException;
92

    
93
import javax.swing.JFileChooser;
94
import javax.swing.JOptionPane;
95
import javax.swing.filechooser.FileFilter;
96

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

    
110
public class ImportWebMapContextExtension extends Extension {
111
        private String lastPath = null;
112
        public void initialize() {
113
                String[] arguments = PluginServices.getArguments();
114
                if (arguments[arguments.length-1].toLowerCase().endsWith(".cml")) {
115
                        File wmcFile = new File(arguments[arguments.length-1]);
116
                        if (!wmcFile.exists()) {
117
                                NotificationManager.addError(PluginServices.getText(this, "could_not_find_web_map_context_file"), new FileNotFoundException());
118
                                return;
119
                        }
120

    
121
                        readMapContextFile(wmcFile, null);
122
                }
123
        }
124

    
125
        public void execute(String actionCommand) {
126
                if (actionCommand.equals("IMPORT")) {
127
                        JFileChooser fc = new JFileChooser(lastPath);
128
                        fc.setFileFilter(new FileFilter() {
129
                                public boolean accept(File f) {
130
                                        return f.isDirectory() || f.getAbsolutePath().toLowerCase().endsWith(WebMapContext.FILE_EXTENSION);
131
                                }
132

    
133
                                public String getDescription() {
134
                                        return PluginServices.getText(this, "ogc_mapcontext_file")+" (*.cml)";
135
                                }
136
                        });
137
                        IWindow v = PluginServices.getMDIManager().getActiveWindow();
138

    
139
                        // If the current active view is a gvSIG's view, we'll keep its name to
140
                        // show it at the JFileChooser's accessory.
141
                        String currentViewName;
142
                        if (v instanceof com.iver.cit.gvsig.gui.View)
143
                                currentViewName = ((com.iver.cit.gvsig.gui.View) v).getModel().getName();
144
                        else
145
                                currentViewName = null;
146

    
147
                        // Create an accessory for the Web Map Context's file chooser.
148
                        WebMapContextFileChooserAccessory acc = new WebMapContextFileChooserAccessory(currentViewName);
149

    
150
                        // Add the accessory to the file chooser
151
                        fc.setAccessory(acc);
152

    
153
                        // Nothing else than Web Map Context files allowed
154
                        fc.setAcceptAllFileFilterUsed(false);
155

    
156
                        if (fc.showOpenDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION){
157
                                File f = fc.getSelectedFile();
158
                                readMapContextFile(f, acc.getSelectedView());
159
                                String fileName = f.getAbsolutePath();
160
                                lastPath  = fileName.substring(0, fileName.lastIndexOf(File.separatorChar));
161
                        }
162
                        fc = null;
163
                }
164
        }
165

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

    
203
                if (dstView == null) {
204
                        // Since the destination view is null, a new one will be added to
205
                        // the project.
206
                        dstView = ProjectFactory.createView(null);
207
                        dstView.setName(wmc.title);
208
                        dstView.setComment("Created from WebMapContext file: "+wmcFile.getName());
209

    
210
                        ProjectExtension pe = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class);
211
                        Project theProject = pe.getProject();
212
                        theProject.addView(dstView);
213

    
214
                        // show the view in a new window
215
                        com.iver.cit.gvsig.gui.View v = null;
216
                        v = new com.iver.cit.gvsig.gui.View();
217
                        v.setModel(dstView);
218
                        PluginServices.getMDIManager().addWindow(v);
219
                }
220

    
221
                for (int i = 0; i < wmc.layerList.size(); i++) {
222
                        // WMS layers
223
                        if (wmc.layerList.get(i) instanceof FLyrWMS) {
224
                                FLyrWMS layer = (FLyrWMS) wmc.layerList.get(i);
225
                                dstView.getMapContext().getLayers().addLayer(layer);
226
                        }
227
                }
228
        }
229

    
230
        public boolean isEnabled() {
231
                return true;
232
        }
233

    
234
        public boolean isVisible() {
235
                return true;
236
        }
237

    
238
}