Statistics
| Revision:

gvsig-osm / org.gvsig.raster.osm / trunk / org.gvsig.raster.osm / org.gvsig.raster.osm.app.osmclient / src / main / java / org / gvsig / raster / osm / app / osmclient / AddOSMLayerWizard.java @ 58

History | View | Annotate | Download (10.6 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.raster.osm.app.osmclient;
23

    
24
import java.awt.Component;
25
import java.awt.GridBagConstraints;
26
import java.awt.GridBagLayout;
27
import java.awt.Insets;
28
import java.awt.event.MouseEvent;
29
import java.awt.event.MouseListener;
30
import java.beans.PropertyVetoException;
31
import java.io.File;
32
import java.io.FileNotFoundException;
33
import java.io.IOException;
34

    
35
import javax.swing.JOptionPane;
36
import javax.swing.JPanel;
37

    
38
import org.cresques.cts.IProjection;
39
import org.gvsig.andami.Launcher;
40
import org.gvsig.andami.PluginServices;
41
import org.gvsig.andami.PluginsLocator;
42
import org.gvsig.andami.ui.mdiManager.IWindow;
43
import org.gvsig.app.ApplicationLocator;
44
import org.gvsig.app.ApplicationManager;
45
import org.gvsig.app.gui.WizardPanel;
46
import org.gvsig.app.gui.wizards.WizardListener;
47
import org.gvsig.app.gui.wizards.WizardListenerSupport;
48
import org.gvsig.app.project.ProjectManager;
49
import org.gvsig.app.project.documents.view.ViewDocument;
50
import org.gvsig.app.project.documents.view.ViewManager;
51
import org.gvsig.app.project.documents.view.gui.AbstractViewPanel;
52
import org.gvsig.app.project.documents.view.gui.IView;
53
import org.gvsig.fmap.crs.CRSFactory;
54
import org.gvsig.fmap.dal.DALLocator;
55
import org.gvsig.fmap.dal.DataParameters;
56
import org.gvsig.fmap.dal.DataStoreParameters;
57
import org.gvsig.fmap.dal.coverage.RasterLocator;
58
import org.gvsig.fmap.dal.coverage.store.parameter.TileDataParameters;
59
import org.gvsig.fmap.dal.coverage.util.FileUtils;
60
import org.gvsig.fmap.dal.exception.InitializeException;
61
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
62
import org.gvsig.fmap.dal.spi.DataManagerProviderServices;
63
import org.gvsig.fmap.mapcontext.MapContext;
64
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
65
import org.gvsig.fmap.mapcontrol.MapControl;
66
import org.gvsig.raster.osm.app.osmclient.layer.FLyrOSM;
67
import org.gvsig.raster.osm.io.OSMDataParameters;
68
import org.gvsig.raster.osm.io.OSMProvider;
69
import org.gvsig.raster.osm.swing.AddOSMLayerPanel;
70
import org.gvsig.raster.osm.swing.OSMSwingLocator;
71
import org.slf4j.Logger;
72
import org.slf4j.LoggerFactory;
73

    
74
/**
75
 * Wizard panel to add a new OSM raster layer
76
 * @author Nacho Brodin (nachobrodin@gmail.com)
77
 */
78
public class AddOSMLayerWizard extends WizardPanel implements MouseListener {
79
        private static final long           serialVersionUID      = 1L;
80
        private static final String         TAB_NAME              = "OSM";
81
        private static Logger               logger                = LoggerFactory.getLogger(AddOSMLayerWizard.class.getName());
82
        private AbstractViewPanel           view                  = null;
83
        private AddOSMLayerPanel            apiUI                 = null;
84
        private WizardListenerSupport       listenerSupport       = null;
85
        
86
        
87
        public AddOSMLayerWizard() {
88
                super();
89
                initialize();
90
        }
91

    
92
        /**
93
         * This method initializes this
94
         *
95
         * @return void
96
         */
97
        private void initialize() {
98
                setTabName(PluginServices.getText(this, TAB_NAME));
99
                setLayout(new GridBagLayout());
100
                GridBagConstraints gridBagConstraints = new GridBagConstraints();
101
                gridBagConstraints.fill = GridBagConstraints.BOTH;
102
                gridBagConstraints.weightx = 1.0;
103
                gridBagConstraints.weighty = 1.0;
104
                gridBagConstraints.insets = new Insets(6, 6, 6, 6);
105

    
106
                IWindow iw = PluginServices.getMDIManager().getActiveWindow();
107

    
108
                if (iw == null) {
109
                        return;
110
                }
111

    
112
                if (!(iw instanceof AbstractViewPanel)) {
113
                        return;
114
                }
115
                
116
                apiUI = OSMSwingLocator.getSwingManager().createAddOSMLayerPanel(new File(Launcher.getAppHomeDir()));
117
                
118
                //Copying osmservers.properties
119
                if(!new File(apiUI.getPropertiesFilePath()).exists()) {
120
                        File pluginFolder = PluginsLocator.getManager().getPluginsFolder();
121
                        File f = new File(pluginFolder.getAbsolutePath() + 
122
                                                        File.separator + 
123
                                                        "org.gvsig.raster.osm.app.osmclient" + 
124
                                                        File.separator + 
125
                                                        apiUI.getPropertiesFileName());
126
                        FileUtils utils = RasterLocator.getManager().getFileUtils();
127
                        try {
128
                                utils.copyFile(f.getAbsolutePath(), Launcher.getAppHomeDir() + File.separator);
129
                                apiUI = OSMSwingLocator.getSwingManager().createAddOSMLayerPanel(new File(Launcher.getAppHomeDir()));
130
                        } catch (FileNotFoundException e) {
131
                                logger.info("File not found copying osmservers.properties", e);
132
                        } catch (IOException e) {
133
                                logger.info("Error copying osmservers.properties", e);
134
                        }
135
                }
136
                
137
                apiUI.addMouseListener(this);
138
                
139
                view = (AbstractViewPanel) iw;
140
                setMapCtrl(view.getMapControl());
141
                
142
                JPanel mainPanel = (JPanel)apiUI.getComponent();
143
                add(mainPanel, gridBagConstraints);
144
        }
145
        
146
        private WizardListenerSupport getWizardListenerSupport() {
147
                if(listenerSupport == null)
148
                        listenerSupport = new WizardListenerSupport();
149
                return listenerSupport;
150
        }
151
        
152
        /**
153
         * This method checks for the options selected within the configuration
154
         * dialog are correct.
155
         *
156
         * @return true if you're done, false if not.
157
         */
158
        public boolean areSettingsValid() {
159
                return apiUI.areSettingsValid(); 
160
        }
161
        
162
        public void addWizardListener(WizardListener listener) {
163
                getWizardListenerSupport().addWizardListener(listener);
164
                getWizardListenerSupport().callStateChanged(false);
165
        }
166

    
167
        public void removeWizardListener(WizardListener listener) {
168
                getWizardListenerSupport().removeWizardListener(listener);
169
        }
170
        
171
        @Override
172
        public void close() {
173
                
174
        }
175

    
176
        @Override
177
        public void execute() {
178
                if(!apiUI.areSettingsValid()) {
179
                        messageBoxInfo("server_not_selected", PluginServices.getMDIManager().getActiveWindow(), null);
180
                        return;
181
                }
182
                        
183
                DataManagerProviderServices dataman = (DataManagerProviderServices) DALLocator.getDataManager();
184
                try {
185
                        OSMDataParameters parameters = (OSMDataParameters)dataman.createStoreParameters(OSMProvider.NAME);
186
                        parameters.setURI(apiUI.getSelectedURL());
187
                        parameters.setTileSuffix(apiUI.getSelectedFormat());
188
                        parameters.setOSMLayerName(apiUI.getSelectedName());
189
                        try {
190
                                parameters.setNumberOfLevels(new Integer(apiUI.getSelectedLevel()));
191
                        } catch (NumberFormatException e) {
192
                                
193
                        }
194
                        
195
                        TileDataParameters tileParams = (TileDataParameters) dataman.createStoreParameters("Tile Store");
196
                        tileParams.setDataParameters((DataParameters)parameters);
197
                        
198
                        //Creating a OSM layer
199
                        FLyrOSM layer = new FLyrOSM();
200
                        layer.deleteCache(false);  //Esto es parametrizable
201
                        try {
202
                                layer.setParameters(tileParams);
203
                                layer.setName(apiUI.getSelectedName());
204
                        } catch (InitializeException e) {
205
                                messageBoxInfo("error_initializing_layer", PluginServices.getMDIManager().getActiveWindow(), e);
206
                        }
207
                        
208
                        //Getting the MapContext
209
                        MapControl mapCtrl = null;
210
                        MapContext mapContext = null;
211
                        IWindow[] w = PluginServices.getMDIManager().getAllWindows();
212
                        for (int i = 0; i < w.length; i++) {
213
                                if (w[i] instanceof org.gvsig.app.project.documents.view.gui.AbstractViewPanel) {
214
                                        mapCtrl = ((org.gvsig.app.project.documents.view.gui.AbstractViewPanel)w[i]).getMapControl();
215
                                        mapContext = mapCtrl.getMapContext();
216
                                }
217
                        }
218
                        
219
                        if(mapContext == null) {
220
                                mapContext = createViewWindow();
221
                        }
222
                        
223
                        //Adding the layer
224
                        mapContext.beginAtomicEvent();
225
                        layer.setVisible(true);
226
                        mapContext.getLayers().addLayer(layer);
227
                        mapContext.callLegendChanged();
228
                        mapContext.endAtomicEvent();
229
                        
230
                } catch (InitializeException e) {
231
                        messageBoxInfo("error_creating_parameters", PluginServices.getMDIManager().getActiveWindow(), e);
232
                } catch (ProviderNotRegisteredException e) {
233
                        messageBoxInfo("error_creating_parameters", PluginServices.getMDIManager().getActiveWindow(), e);
234
                } catch (LoadLayerException e) {
235
                        messageBoxInfo("error_creating_the_view", PluginServices.getMDIManager().getActiveWindow(), e);
236
                }
237
        }
238
        
239
        private void messageBoxInfo(String msg, Object parentWindow, Exception exception) {
240
                LoggerFactory.getLogger(getClass()).debug(msg, exception);
241
                String string = PluginServices.getText(parentWindow, "accept");
242
                Object[] options = {string};
243
                JOptionPane.showOptionDialog((Component)/*PluginServices.getMainFrame()*/parentWindow,
244
                                        "<html>" + PluginServices.getText(parentWindow, msg).replaceAll("\n", "<br>") + "</html>",
245
                                        PluginServices.getText(parentWindow, "confirmacion"),
246
                                        JOptionPane.OK_OPTION,
247
                                        JOptionPane.INFORMATION_MESSAGE,
248
                                        null,
249
                                        options,
250
                                        string);
251
        }
252
        
253
        /**
254
     * Create the view in the project. Add the blocks layer
255
     * to the view, and register the tool for get info of the
256
     * blocks.
257
     */
258
    @SuppressWarnings("deprecation")
259
        private MapContext createViewWindow() throws LoadLayerException {
260
            ApplicationManager application = ApplicationLocator.getManager();
261

    
262
            ProjectManager projectManager = application.getProjectManager();
263

    
264
            // 1. Create a new view and set the name.
265
            ViewManager viewManager = (ViewManager) projectManager.getDocumentManagers(ViewManager.TYPENAME);
266
            ViewDocument view = (ViewDocument) viewManager.createDocument();
267
            view.setName("OpenStreetMap View");
268

    
269
            // 4. Add the view to the current project.
270
            projectManager.getCurrentProject().add(view);
271

    
272
            // 5. Force to show the view's window.
273
            IView viewWindow = (IView) viewManager.getMainWindow(view);
274

    
275
            application.getUIManager().addWindow(viewWindow, GridBagConstraints.CENTER);
276
            try {
277
                        application.getUIManager().setMaximum((IWindow) viewWindow, true);
278
                } catch (PropertyVetoException e) {
279
                }
280
            
281
                if(viewWindow != null) {
282
                        IProjection proj = CRSFactory.getCRS("3785");
283
                        if(proj != null)
284
                                viewWindow.getMapControl().setProjection(proj);
285
                        return viewWindow.getMapControl().getMapContext();
286
                }
287
                return null;
288
    }
289

    
290
        @Override
291
        public DataStoreParameters[] getParameters() {
292
                return null;
293
        }
294

    
295
        @Override
296
        public void initWizard() {
297
                
298
        }
299

    
300
        public void mouseClicked(MouseEvent e) {
301
                
302
        }
303

    
304
        public void mouseEntered(MouseEvent e) {
305
                
306
        }
307

    
308
        public void mouseExited(MouseEvent e) {
309
                
310
        }
311

    
312
        public void mousePressed(MouseEvent e) {
313
                getWizardListenerSupport().callStateChanged(true);
314
        }
315

    
316
        public void mouseReleased(MouseEvent e) {
317
                
318
        }
319
}