Statistics
| Revision:

gvsig-webmap / org.gvsig.webmap / trunk / org.gvsig.webmap / org.gvsig.webmap.app / org.gvsig.webmap.app.mainplugin / src / main / java / org / gvsig / webmap / app / mainplugin / AddWebMapLayerWizard.java @ 58

History | View | Annotate | Download (8.5 KB)

1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2016 gvSIG Association
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., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.webmap.app.mainplugin;
24

    
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.util.HashMap;
31
import java.util.Iterator;
32
import java.util.List;
33
import java.util.Map;
34
import java.util.Map.Entry;
35
import java.util.Set;
36

    
37
import javax.swing.JLabel;
38
import javax.swing.JOptionPane;
39
import javax.swing.JTabbedPane;
40

    
41
import org.gvsig.andami.PluginServices;
42
import org.gvsig.app.gui.WizardPanel;
43
import org.gvsig.app.gui.wizards.WizardListener;
44
import org.gvsig.app.gui.wizards.WizardListenerSupport;
45
import org.gvsig.fmap.crs.CRSFactory;
46
import org.gvsig.fmap.dal.DataStoreParameters;
47
import org.gvsig.fmap.dal.exception.InitializeException;
48
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
49
import org.gvsig.fmap.mapcontext.MapContext;
50
import org.gvsig.fmap.mapcontext.layers.FLayer;
51
import org.gvsig.fmap.mapcontrol.MapControl;
52
import org.gvsig.tools.ToolsLocator;
53
import org.gvsig.tools.i18n.I18nManager;
54
import org.gvsig.tools.swing.api.Component;
55
import org.gvsig.webmap.lib.api.WebMapLocator;
56
import org.gvsig.webmap.lib.api.WebMapService;
57
import org.gvsig.webmap.lib.api.WebMapServiceFactory;
58
import org.gvsig.webmap.lib.api.exceptions.WebMapInvalidParamsException;
59

    
60
import org.cresques.cts.IProjection;
61
import org.slf4j.Logger;
62
import org.slf4j.LoggerFactory;
63

    
64
/**
65
 * Wizard panel to add a new WebMap layer
66
 * @author dmartinez (dmartinez@disid.com)
67
 */
68
public class AddWebMapLayerWizard extends WizardPanel implements MouseListener {
69
        private static final long           serialVersionUID      = 1L;
70
        private static final String         TAB_NAME              = "WebMaps";
71
        private static Logger               logger                = LoggerFactory.getLogger(AddWebMapLayerWizard.class.getName());
72
        private JTabbedPane                 apiUI                 = null;
73
        private WizardListenerSupport       listenerSupport       = null;
74
        private Map<String,WebMapService>   services              = null;
75
        final I18nManager i18nManager = ToolsLocator.getI18nManager();
76
        /**
77
         * This method creates this class
78
         */
79
        public AddWebMapLayerWizard() {
80
                super();
81
        }
82

    
83
        /**
84
         * This method initializes this
85
         */
86
        private void initialize() {
87
            services=new HashMap<String,WebMapService>();
88
                setTabName(PluginServices.getText(this, TAB_NAME));
89
                setLayout(new GridBagLayout());
90
                GridBagConstraints gridBagConstraints = new GridBagConstraints();
91
                gridBagConstraints.fill = GridBagConstraints.BOTH;
92
                gridBagConstraints.weightx = 1.0;
93
                gridBagConstraints.weighty = 1.0;
94
                gridBagConstraints.insets = new Insets(6, 6, 6, 6);
95

    
96
                apiUI = new JTabbedPane();
97
                List<WebMapServiceFactory> serviceFactories = WebMapLocator.getManager().getServices();
98
                if (serviceFactories.isEmpty()){
99
                    apiUI.add(new JLabel(i18nManager.getTranslation("_not_installed_webmap_services_found")));
100
                }else{
101
                      for (WebMapServiceFactory serviceFactory:serviceFactories){
102
                            WebMapService webMapService= serviceFactory.create();
103
                            webMapService.setMapCtrl(getMapCtrl());
104
                            services.put(webMapService.getLabel(), webMapService);
105
                            Component parametersPanel = webMapService.getParametersPanel();
106
                    apiUI.add(webMapService.getLabel(), parametersPanel.asJComponent());
107
                        }
108
                }
109
                apiUI.addMouseListener(this);
110
                add(apiUI, gridBagConstraints);
111
        }
112

    
113
   private WebMapService getSelectedWebMapService(){
114
       int index = apiUI.getSelectedIndex();
115
       String label = apiUI.getTitleAt(index);
116
       return services.get(label);
117
   }
118

    
119
        private WizardListenerSupport getWizardListenerSupport() {
120
                if(listenerSupport == null)
121
                        listenerSupport = new WizardListenerSupport();
122
                return listenerSupport;
123
        }
124

    
125
        /**
126
         * This method checks for the options selected within the configuration
127
         * dialog are correct.
128
         *
129
         * @return true if you're done, false if not.
130
         */
131
        public boolean areSettingsValid() {
132
            WebMapService selectedWebMapService = getSelectedWebMapService();
133
            if (selectedWebMapService!=null){
134
                try {
135
                return selectedWebMapService.isValid();
136
            } catch (WebMapInvalidParamsException e) {
137
                logger.error("Parameters no valid", e.getLocalizedMessage());
138
                JOptionPane.showMessageDialog(
139
                    null,
140
                    e.getLocalizedMessage(),
141
                    i18nManager.getTranslation("error"), JOptionPane.ERROR_MESSAGE);
142
                return false;
143
            }
144
            }
145
                return false;
146
        }
147

    
148
        public void addWizardListener(WizardListener listener) {
149
                getWizardListenerSupport().addWizardListener(listener);
150
                getWizardListenerSupport().callStateChanged(false);
151
        }
152

    
153
        public void removeWizardListener(WizardListener listener) {
154
                getWizardListenerSupport().removeWizardListener(listener);
155
        }
156

    
157
        @Override
158
        public void close() {
159

    
160
        }
161

    
162
        @Override
163
        public void execute() {
164
            if (services.isEmpty()){
165
                close();
166
                return;
167
            }
168
                if(!areSettingsValid()) {
169
                        messageBoxInfo("error_creating_parameters", PluginServices.getMDIManager().getActiveWindow(), null);
170
                        return;
171
                }
172
                try {
173
                    WebMapService webMapService= getSelectedWebMapService();
174
                    DataStoreParameters dataStoreParams=webMapService.getParameters();
175
            MapContext mapContext = getMapContext();
176
            FLayer layer = webMapService.createLayer(dataStoreParams);
177
                    addLayerToMapContext(mapContext, layer);
178
                } catch (InitializeException e) {
179
                        messageBoxInfo("error_creating_parameters", PluginServices.getMDIManager().getActiveWindow(), e);
180
                } catch (ProviderNotRegisteredException e) {
181
                        messageBoxInfo("error_creating_parameters", PluginServices.getMDIManager().getActiveWindow(), e);
182
                }
183
        }
184

    
185
           /**
186
     * Adds the layer to the MapContext selected
187
     * @param mapContext
188
     * @param layer
189
     */
190
    private void addLayerToMapContext(MapContext mapContext, FLayer layer) {
191
        mapContext.beginAtomicEvent();
192
        layer.setVisible(true);
193
        mapContext.getLayers().addLayer(layer);
194
        mapContext.callLegendChanged();
195
        mapContext.endAtomicEvent();
196
    }
197

    
198
        private void messageBoxInfo(String msg, Object parentWindow, Exception exception) {
199
                LoggerFactory.getLogger(getClass()).debug(msg, exception);
200
                String string = PluginServices.getText(parentWindow, "accept");
201
                Object[] options = {string};
202
                JOptionPane.showOptionDialog((java.awt.Component)/*PluginServices.getMainFrame()*/parentWindow,
203
                                        "<html>" + PluginServices.getText(parentWindow, msg).replaceAll("\n", "<br>") + "</html>",
204
                                        PluginServices.getText(parentWindow, "confirmacion"),
205
                                        JOptionPane.OK_OPTION,
206
                                        JOptionPane.INFORMATION_MESSAGE,
207
                                        null,
208
                                        options,
209
                                        string);
210
        }
211

    
212
        @Override
213
        public DataStoreParameters[] getParameters() {
214
            WebMapService serviceParamPanel= getSelectedWebMapService();
215
       try {
216
           DataStoreParameters dataStoreParams=serviceParamPanel.getParameters();
217
           return new DataStoreParameters[]{dataStoreParams};
218
        } catch (InitializeException e) {
219
            messageBoxInfo("error_creating_parameters", PluginServices.getMDIManager().getActiveWindow(), e);
220
        } catch (ProviderNotRegisteredException e) {
221
            messageBoxInfo("error_creating_parameters", PluginServices.getMDIManager().getActiveWindow(), e);
222
        }
223
                return null;
224
        }
225

    
226
        @Override
227
        public void initWizard() {
228
            initialize();
229
        }
230

    
231
        public void mouseClicked(MouseEvent e) {
232

    
233
        }
234

    
235
        public void mouseEntered(MouseEvent e) {
236

    
237
        }
238

    
239
        public void mouseExited(MouseEvent e) {
240

    
241
        }
242

    
243
        public void mousePressed(MouseEvent e) {
244
                getWizardListenerSupport().callStateChanged(true);
245
        }
246

    
247
        public void mouseReleased(MouseEvent e) {
248

    
249
        }
250
}