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 @ 120

History | View | Annotate | Download (11.2 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.raster.osm.app.osmclient.layer.FLyrOSM;
66
import org.gvsig.raster.osm.io.OSMDataParameters;
67
import org.gvsig.raster.osm.io.OSMProvider;
68
import org.gvsig.raster.osm.swing.AddOSMLayerPanel;
69
import org.gvsig.raster.osm.swing.OSMSwingLocator;
70
import org.slf4j.Logger;
71
import org.slf4j.LoggerFactory;
72

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

    
89
        /**
90
         * This method initializes this
91
         *
92
         * @return void
93
         */
94
        private void initialize() {
95
                setTabName(PluginServices.getText(this, TAB_NAME));
96
                setLayout(new GridBagLayout());
97
                GridBagConstraints gridBagConstraints = new GridBagConstraints();
98
                gridBagConstraints.fill = GridBagConstraints.BOTH;
99
                gridBagConstraints.weightx = 1.0;
100
                gridBagConstraints.weighty = 1.0;
101
                gridBagConstraints.insets = new Insets(6, 6, 6, 6);
102
                
103
                apiUI = OSMSwingLocator.getSwingManager().createAddOSMLayerPanel(new File(Launcher.getAppHomeDir()));
104
                
105
                //Copying osmservers.properties
106
                if(!new File(apiUI.getPropertiesFilePath()).exists()) {
107
                        File pluginFolder = PluginsLocator.getManager().getPluginsFolder();
108
                        File f = new File(pluginFolder.getAbsolutePath() + 
109
                                                        File.separator + 
110
                                                        "org.gvsig.raster.osm.app.osmclient" + 
111
                                                        File.separator + 
112
                                                        apiUI.getPropertiesFileName());
113
                        FileUtils utils = RasterLocator.getManager().getFileUtils();
114
                        try {
115
                                utils.copyFile(f.getAbsolutePath(), Launcher.getAppHomeDir() + File.separator);
116
                                apiUI = OSMSwingLocator.getSwingManager().createAddOSMLayerPanel(new File(Launcher.getAppHomeDir()));
117
                        } catch (FileNotFoundException e) {
118
                                logger.info("File not found copying osmservers.properties", e);
119
                        } catch (IOException e) {
120
                                logger.info("Error copying osmservers.properties", e);
121
                        }
122
                }
123
                
124
                apiUI.addMouseListener(this);
125
                
126
                JPanel mainPanel = (JPanel)apiUI.getComponent();
127
                add(mainPanel, gridBagConstraints);
128
        }
129
        
130
        private WizardListenerSupport getWizardListenerSupport() {
131
                if(listenerSupport == null)
132
                        listenerSupport = new WizardListenerSupport();
133
                return listenerSupport;
134
        }
135
        
136
        /**
137
         * This method checks for the options selected within the configuration
138
         * dialog are correct.
139
         *
140
         * @return true if you're done, false if not.
141
         */
142
        public boolean areSettingsValid() {
143
                return apiUI.areSettingsValid(); 
144
        }
145
        
146
        public void addWizardListener(WizardListener listener) {
147
                getWizardListenerSupport().addWizardListener(listener);
148
                getWizardListenerSupport().callStateChanged(false);
149
        }
150

    
151
        public void removeWizardListener(WizardListener listener) {
152
                getWizardListenerSupport().removeWizardListener(listener);
153
        }
154
        
155
        @Override
156
        public void close() {
157
                
158
        }
159

    
160
        @Override
161
        public void execute() {
162
                if(!apiUI.areSettingsValid()) {
163
                        messageBoxInfo("server_not_selected", PluginServices.getMDIManager().getActiveWindow(), null);
164
                        return;
165
                }
166
                        
167
                try {
168
                        TileDataParameters tileParams = createDataParameters();
169
                        
170
                        //Getting the MapContext
171
                        MapContext mapContext = getMapContext();
172
                        
173
                        if(mapContext == null) {
174
                                mapContext = createViewWindow();
175
                        }
176
                        
177
                        //Adding the layer
178
                        FLyrOSM layer = buildLayer(tileParams);
179
                        addLayerToMapContext(mapContext, layer);
180
                } catch (InitializeException e) {
181
                        messageBoxInfo("error_creating_parameters", PluginServices.getMDIManager().getActiveWindow(), e);
182
                } catch (ProviderNotRegisteredException e) {
183
                        messageBoxInfo("error_creating_parameters", PluginServices.getMDIManager().getActiveWindow(), e);
184
                } catch (LoadLayerException e) {
185
                        messageBoxInfo("error_creating_the_view", PluginServices.getMDIManager().getActiveWindow(), e);
186
                }
187
        }
188
        
189
        /**
190
         * Create the parameters to the provider
191
         * @return
192
         * @throws InitializeException
193
         * @throws ProviderNotRegisteredException
194
         */
195
        private TileDataParameters createDataParameters() throws InitializeException, ProviderNotRegisteredException {
196
                DataManagerProviderServices dataman = (DataManagerProviderServices) DALLocator.getDataManager();
197
                OSMDataParameters parameters = (OSMDataParameters)dataman.createStoreParameters(OSMProvider.NAME);
198
                parameters.setURI(apiUI.getSelectedURL());
199
                parameters.setTileSuffix(apiUI.getSelectedFormat());
200
                parameters.setOSMLayerName(apiUI.getSelectedName());
201
                try {
202
                        parameters.setNumberOfLevels(new Integer(apiUI.getSelectedLevel()));
203
                } catch (NumberFormatException e) {
204
                        
205
                }
206
                
207
                TileDataParameters tileParams = (TileDataParameters) dataman.createStoreParameters("Tile Store");
208
                tileParams.setDataParameters((DataParameters)parameters);
209
                return tileParams;
210
        }
211
        
212
        /**
213
         * Adds the layer to the MapContext selected
214
         * @param mapContext
215
         * @param layer
216
         */
217
        private void addLayerToMapContext(MapContext mapContext, FLyrOSM layer) {
218
                mapContext.beginAtomicEvent();
219
                layer.setVisible(true);
220
                mapContext.getLayers().addLayer(layer);
221
                mapContext.callLegendChanged();
222
                mapContext.endAtomicEvent();
223
        }
224
        
225
        /**
226
         * Builds the OSM layer
227
         * @param tileParams
228
         * @return
229
         */
230
        private FLyrOSM buildLayer(TileDataParameters tileParams) {
231
                FLyrOSM layer = new FLyrOSM();
232
                layer.deleteCache(false);  //Esto es parametrizable
233
                try {
234
                        layer.setParameters(tileParams);
235
                        layer.setName(apiUI.getSelectedName());
236
                } catch (InitializeException e) {
237
                        messageBoxInfo("error_initializing_layer", PluginServices.getMDIManager().getActiveWindow(), e);
238
                }
239
                return layer;
240
        }
241
        
242
        /**
243
         * Gets the active view or the first view opened if there is not 
244
         * any with the focus
245
         * @return
246
         */
247
        public MapContext getMapContext() {
248
                IWindow window = PluginServices.getMDIManager().getFocusWindow();
249
                if(window instanceof AbstractViewPanel) {
250
                        return ((AbstractViewPanel)window).getMapControl().getMapContext();
251
                }
252
                
253
                IWindow[] w = PluginServices.getMDIManager().getAllWindows();
254
                for (int i = 0; i < w.length; i++) {
255
                        if (w[i] instanceof org.gvsig.app.project.documents.view.gui.AbstractViewPanel) {
256
                                return ((AbstractViewPanel)w[i]).getMapControl().getMapContext();
257
                        }
258
                }
259
                return null;
260
        }
261
        
262
        private void messageBoxInfo(String msg, Object parentWindow, Exception exception) {
263
                LoggerFactory.getLogger(getClass()).debug(msg, exception);
264
                String string = PluginServices.getText(parentWindow, "accept");
265
                Object[] options = {string};
266
                JOptionPane.showOptionDialog((Component)/*PluginServices.getMainFrame()*/parentWindow,
267
                                        "<html>" + PluginServices.getText(parentWindow, msg).replaceAll("\n", "<br>") + "</html>",
268
                                        PluginServices.getText(parentWindow, "confirmacion"),
269
                                        JOptionPane.OK_OPTION,
270
                                        JOptionPane.INFORMATION_MESSAGE,
271
                                        null,
272
                                        options,
273
                                        string);
274
        }
275
        
276
        /**
277
     * Create the view in the project. Add the blocks layer
278
     * to the view, and register the tool for get info of the
279
     * blocks.
280
     */
281
    @SuppressWarnings("deprecation")
282
        private MapContext createViewWindow() throws LoadLayerException {
283
            ApplicationManager application = ApplicationLocator.getManager();
284

    
285
            ProjectManager projectManager = application.getProjectManager();
286

    
287
            // 1. Create a new view and set the name.
288
            ViewManager viewManager = (ViewManager) projectManager.getDocumentManagers(ViewManager.TYPENAME);
289
            ViewDocument view = (ViewDocument) viewManager.createDocument();
290
            view.setName("OpenStreetMap View");
291

    
292
            // 4. Add the view to the current project.
293
            projectManager.getCurrentProject().add(view);
294

    
295
            // 5. Force to show the view's window.
296
            IView viewWindow = (IView) viewManager.getMainWindow(view);
297

    
298
            application.getUIManager().addWindow(viewWindow, GridBagConstraints.CENTER);
299
            try {
300
                        application.getUIManager().setMaximum((IWindow) viewWindow, true);
301
                } catch (PropertyVetoException e) {
302
                }
303
            
304
                if(viewWindow != null) {
305
                        IProjection proj = CRSFactory.getCRS("3785");
306
                        if(proj != null)
307
                                viewWindow.getMapControl().setProjection(proj);
308
                        return viewWindow.getMapControl().getMapContext();
309
                }
310
                return null;
311
    }
312

    
313
        @Override
314
        public DataStoreParameters[] getParameters() {
315
                return null;
316
        }
317

    
318
        @Override
319
        public void initWizard() {
320
                
321
        }
322

    
323
        public void mouseClicked(MouseEvent e) {
324
                
325
        }
326

    
327
        public void mouseEntered(MouseEvent e) {
328
                
329
        }
330

    
331
        public void mouseExited(MouseEvent e) {
332
                
333
        }
334

    
335
        public void mousePressed(MouseEvent e) {
336
                getWizardListenerSupport().callStateChanged(true);
337
        }
338

    
339
        public void mouseReleased(MouseEvent e) {
340
                
341
        }
342
}