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

History | View | Annotate | Download (8.78 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

    
107
                    apiUI.add(webMapService.getLabel(), parametersPanel.asJComponent());
108
                        }
109
                }
110
                apiUI.addMouseListener(this);
111
                add(apiUI, gridBagConstraints);
112
        }
113

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

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

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

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

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

    
158
        @Override
159
        public void close() {
160

    
161
        }
162

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

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

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

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

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

    
232
        public void mouseClicked(MouseEvent e) {
233
            getWizardListenerSupport().callStateChanged(areSettingsValid());
234
        }
235

    
236
        public void mouseEntered(MouseEvent e) {
237
            getWizardListenerSupport().callStateChanged(areSettingsValid());
238
        }
239

    
240
        public void mouseExited(MouseEvent e) {
241
            getWizardListenerSupport().callStateChanged(areSettingsValid());
242
        }
243

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

    
248
        public void mouseReleased(MouseEvent e) {
249
            getWizardListenerSupport().callStateChanged(areSettingsValid());
250
        }
251
}