Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extWMS / src / com / iver / cit / gvsig / wmc / ImportWebMapContextExtension.java @ 5034

History | View | Annotate | Download (5.55 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 5034 2006-05-03 11:10:54Z jaume $
45
* $Log$
46
* Revision 1.2  2006-05-03 11:10:54  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.1  2006/05/03 07:51:21  jaume
50
* *** empty log message ***
51
*
52
* Revision 1.5  2006/05/02 16:12:12  jorpiell
53
* 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
54
*
55
* Revision 1.4  2006/05/02 15:57:43  jaume
56
* Few better javadoc
57
*
58
* Revision 1.3  2006/04/20 17:11:54  jaume
59
* Attempting to export
60
*
61
* Revision 1.2  2006/04/19 16:34:29  jaume
62
* *** empty log message ***
63
*
64
* Revision 1.1  2006/04/19 07:57:29  jaume
65
* *** empty log message ***
66
*
67
* Revision 1.1  2006/04/12 17:10:53  jaume
68
* *** empty log message ***
69
*
70
*
71
*/
72
package com.iver.cit.gvsig.wmc;
73

    
74
import java.awt.Component;
75
import java.io.File;
76

    
77
import javax.swing.JFileChooser;
78
import javax.swing.JOptionPane;
79
import javax.swing.filechooser.FileFilter;
80

    
81
import com.iver.andami.PluginServices;
82
import com.iver.andami.plugins.Extension;
83
import com.iver.andami.ui.mdiManager.View;
84
import com.iver.cit.gvsig.ProjectExtension;
85
import com.iver.cit.gvsig.fmap.drivers.UnsupportedVersionException;
86
import com.iver.cit.gvsig.fmap.layers.FLyrWMS;
87
import com.iver.cit.gvsig.gui.panels.WebMapContextFileChooserAccessory;
88
import com.iver.cit.gvsig.project.Project;
89
import com.iver.cit.gvsig.project.ProjectFactory;
90
import com.iver.cit.gvsig.project.ProjectView;
91

    
92
public class ImportWebMapContextExtension extends Extension {
93
        
94
        public void initialize() {
95
                
96
        }
97

    
98
        public void execute(String actionCommand) {
99
                if (actionCommand.equals("IMPORT")) {
100
                        JFileChooser fc = new JFileChooser();
101
                        fc.setFileFilter(new FileFilter() {
102
                                public boolean accept(File f) {
103
                                        return f.isDirectory() || f.getAbsolutePath().toLowerCase().endsWith(WebMapContext.FILE_EXTENSION);
104
                                }
105

    
106
                                public String getDescription() {
107
                                        return PluginServices.getText(this, "ogc_mapcontext_file")+" (*.cml)";
108
                                }
109
                        });
110
                        View v = PluginServices.getMDIManager().getActiveView();
111
                        
112
                        // If the current active view is a gvSIG's view, we'll keep its name to
113
                        // show it at the JFileChooser's accessory.
114
                        String currentViewName;
115
                        if (v instanceof com.iver.cit.gvsig.gui.View) 
116
                                currentViewName = ((com.iver.cit.gvsig.gui.View) v).getModel().getName();
117
                        else 
118
                                currentViewName = null;
119
                                
120
                        // Create an accessory for the Web Map Context's file chooser.
121
                        WebMapContextFileChooserAccessory acc = new WebMapContextFileChooserAccessory(currentViewName);
122
                        
123
                        // Add the accessory to the file chooser
124
                        fc.setAccessory(acc);
125
                        
126
                        // Nothing else than Web Map Context files allowed
127
                        fc.setAcceptAllFileFilterUsed(false);
128
                        
129
                        if (fc.showOpenDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION){
130
                                readMapContextFile(fc.getSelectedFile(), acc.getSelectedView());
131
                        }
132
                        fc = null;
133
                }
134
        }
135
        
136
        /**
137
         * Reads a WebMapContext (.cml) file and loads its layers into the destination
138
         * view.
139
         * @param wmcFile
140
         * @param dstView
141
         */
142
        private void readMapContextFile(File wmcFile, ProjectView dstView) {
143
                try {
144
                        WebMapContext wmc = new WebMapContext(wmcFile);
145
                        
146
                        if (dstView == null) {
147
                                // Since the destination view is null, a new one will be added to
148
                                // the project.
149
                                dstView = ProjectFactory.createView(null);
150
                                dstView.setName(wmc.title);
151
                                dstView.setComment("Created from WebMapContext file: "+wmcFile.getName());
152
                                
153
                                ProjectExtension pe = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class);
154
                                Project theProject = pe.getProject();
155
                                theProject.addView(dstView);
156
                                
157
                                // show the view in a new window
158
                                com.iver.cit.gvsig.gui.View v = null;
159
                                v = new com.iver.cit.gvsig.gui.View();
160
                                v.setModel(dstView);
161
                                PluginServices.getMDIManager().addView(v);
162
                        }
163
                        
164
                        for (int i = 0; i < wmc.layerList.size(); i++) {
165
                                // WMS layers
166
                                if (wmc.layerList.get(i) instanceof FLyrWMS) {
167
                                        FLyrWMS layer = (FLyrWMS) wmc.layerList.get(i);
168
                                        dstView.getMapContext().getLayers().addLayer(layer);                
169
                                }
170
                        }
171
                } catch (UnsupportedVersionException e) {
172
                        JOptionPane.showMessageDialog(
173
                                        (Component) PluginServices.getMainFrame(),
174
                                        e.getMessage(),
175
                                        PluginServices.getText(this, "unsupported_version"), 
176
                                        JOptionPane.ERROR_MESSAGE);
177
                        e.printStackTrace();
178
                }
179
                
180
        }
181

    
182
        public boolean isEnabled() {
183
                return true;
184
        }
185

    
186
        public boolean isVisible() {
187
                return true;
188
        }
189

    
190
}