Statistics
| Revision:

gvsig-raster / org.gvsig.raster.wmts / trunk / org.gvsig.raster.wmts / org.gvsig.raster.wmts.app.wmtsclient / src / main / java / org / gvsig / raster / wmts / app / wmtsclient / gui / wizard / WMTSPropsDialog.java @ 2614

History | View | Annotate | Download (10.8 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
 
23
package org.gvsig.raster.wmts.app.wmtsclient.gui.wizard;
24

    
25
import java.awt.BorderLayout;
26
import java.util.ArrayList;
27
import java.util.HashMap;
28
import java.util.List;
29

    
30
import javax.swing.JOptionPane;
31
import javax.swing.JPanel;
32

    
33
import org.gvsig.andami.PluginServices;
34
import org.gvsig.andami.ui.mdiManager.IWindow;
35
import org.gvsig.andami.ui.mdiManager.WindowInfo;
36
import org.gvsig.fmap.dal.DataParameters;
37
import org.gvsig.fmap.dal.coverage.exception.ConnectException;
38
import org.gvsig.fmap.dal.coverage.store.parameter.RasterDataParameters;
39
import org.gvsig.fmap.dal.coverage.store.parameter.TileDataParameters;
40
import org.gvsig.fmap.dal.exception.InitializeException;
41
import org.gvsig.fmap.mapcontext.MapContext;
42
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
43
import org.gvsig.fmap.mapcontext.layers.FLayer;
44
import org.gvsig.fmap.mapcontrol.MapControl;
45
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel;
46
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelEvent;
47
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelListener;
48
import org.gvsig.raster.swing.RasterSwingLibrary;
49
import org.gvsig.raster.util.CancelTaskImpl;
50
import org.gvsig.raster.wmts.app.wmtsclient.layer.FLyrWMTS;
51
import org.gvsig.raster.wmts.io.WMTSDataParameters;
52
import org.gvsig.raster.wmts.io.WMTSServerExplorer;
53
import org.gvsig.raster.wmts.swing.WMTSSwingLocator;
54
import org.gvsig.raster.wmts.swing.WMTSWizardListener;
55
import org.gvsig.raster.wmts.swing.WmtsGUI;
56

    
57

    
58
/**
59
 * The TOC WMTS properties panel container.
60
 *
61
 * @author Nacho Brodin (nachobrodin@gmail.com)
62
 */
63
public class WMTSPropsDialog extends JPanel implements IWindow, ButtonsPanelListener {
64
        private static final long         serialVersionUID      = 1L;
65
        private FLyrWMTS                  fLayer                = null;
66
        private WindowInfo                m_ViewInfo            = null;
67
        private ButtonsPanel              buttonsPanel          = null;   
68
        private WmtsGUI                   wmtsGUIInterface      = null;
69
    private WMTSServerExplorer        explorer              = null;
70
    
71
    public class WizardListenerImpl implements WMTSWizardListener {
72
                public void correctlyConfiguredEvent(boolean finishable) {
73
                        getButtonsPanel().getButton(ButtonsPanel.BUTTON_ACCEPT).setEnabled(finishable);
74
                        getButtonsPanel().getButton(ButtonsPanel.BUTTON_APPLY).setEnabled(finishable);
75
                }
76

    
77
                public void addLayerEvent() {
78
                }
79

    
80
                public void removeLayerEvent() {
81
                }
82
        }
83

    
84

    
85
    public WMTSPropsDialog() {
86
            initialize(null);
87
    }
88
    
89
    public WMTSPropsDialog(FLayer layer) {
90
                super();
91
                if(layer instanceof FLyrWMTS) {
92
                        fLayer = (FLyrWMTS)layer;
93
                        initialize(fLayer);
94
                }
95
        }
96

    
97
        private void initialize(FLyrWMTS layer) {
98
                HashMap<String, Object> hash = layer != null ? layer.getProperties() : null;
99
        wmtsGUIInterface = getParamsPanel(hash);
100
                setLayout(new BorderLayout());
101
        this.add(wmtsGUIInterface, BorderLayout.CENTER);
102
        this.add(getButtonsPanel(), BorderLayout.SOUTH);
103
        }
104
        
105
        /**
106
         * Gets the buttons panel
107
         * @return
108
         */
109
        public ButtonsPanel getButtonsPanel() {
110
                if(buttonsPanel == null) {
111
                        buttonsPanel = new ButtonsPanel();
112
                        buttonsPanel.addApply();
113
                        buttonsPanel.addAccept();
114
                        buttonsPanel.addClose();
115
                        buttonsPanel.addButtonPressedListener(this);
116
                }
117
                return buttonsPanel;
118
        }
119

    
120
        /**
121
         * With getParamsPanel we have access to the map config TabbedPane.
122
         * If this panel doesn't exist yet (which can occur when an existing project is
123
         * recovered) it is been automatically constructed by connecting to the server,
124
         * reloading the necessary data, filling up the content and setting the selected
125
         * values that were selected when the projet was saved.
126
         *
127
         *
128
         * Since a connection to the server is needed when rebuiliding the panel, this
129
         * causes a delay for the panel's showing up or a nullPointer error if there is
130
         * no path to the server.
131
         *
132
         *
133
         * Con getParamsPanel tenemos acceso a juego de pesta?as de configuraci?n
134
         * del mapa. Si este panel todav?a no existe (como puede ser cuando
135
         * recuperamos un proyecto guardado) ?ste se crea autom?ticamente conectando
136
         * al servidor, recuperando los datos necesarios, rellenando el contenido y
137
         * dejando seleccionados los valores que estaban seleccionados cuando se
138
         * guarda el proyecto.
139
         *
140
         *
141
         * Como para reconstruirse requiere una conexi?n con el servidor esto causa
142
         * un retardo en la aparici?n en el toc o un error de nullPointer si no
143
         * hay conexi?n hasta el servidor.
144
         *
145
         *
146
         * @return WMSParamsPanel
147
         */
148
        public WmtsGUI getParamsPanel(HashMap<String, Object> info) {
149
                if (info == null) {
150
                        WmtsGUI toc = WMTSSwingLocator.getSwingManager().createMainWizard(new WizardListenerImpl());
151
                        return toc;
152
                }
153

    
154
                try {
155
                        if(explorer == null) {
156
                                explorer = ((FLyrWMTS)fLayer).getExplorer();
157

    
158
                                try {
159
                                        explorer.connect(new CancelTaskImpl());
160
                                } catch (ConnectException e) {
161
                                        RasterSwingLibrary.messageBoxError("The connection cannot be established", this, e);
162
                                        return null;
163
                                } 
164
                        }
165
                } catch (Exception e) {
166
                        explorer = null;
167
                        JOptionPane.showMessageDialog(null, "error_comunicacion_servidor", "Error", JOptionPane.ERROR_MESSAGE);
168
                }
169
                
170
                try {
171
                        WmtsGUI toc = WMTSSwingLocator.getSwingManager().createMainWizard(new WizardListenerImpl());
172
                        toc.setWizardData(explorer);
173
                        toc.updateSelectedInfo(info);
174
                        return toc;
175
                } catch (Exception e) {
176
                        JOptionPane.showMessageDialog(null, "error_comunicacion_servidor", "Error", JOptionPane.ERROR_MESSAGE);
177
                }
178

    
179
                return null;
180
        }
181

    
182
//    @SuppressWarnings("unused")
183
//        private class CommandListener implements ActionListener {
184
//
185
//        public CommandListener(WMTSPropsDialog tp) {
186
//        }
187
//
188
//        public void actionPerformed(ActionEvent e) {
189
//                if (e.getActionCommand() == "CANCEL") {
190
//                        close();
191
//                } else {
192
//                                   MapControl mapCtrl = null;
193
//
194
//                                IWindow[] w = PluginServices.getMDIManager().getAllWindows();
195
//                                for (int i = 0; i < w.length; i++) {
196
//                                        if (w[i] instanceof org.gvsig.app.project.documents.view.gui.AbstractViewPanel) {
197
//                                                MapContext mapContext = ((org.gvsig.app.project.documents.view.gui.AbstractViewPanel)w[i]).getMapControl().getMapContext();
198
//                                                if(mapContext == fLayer.getMapContext())
199
//                                                        mapCtrl = ((org.gvsig.app.project.documents.view.gui.AbstractViewPanel)w[i]).getMapControl();
200
//                                        }
201
//                                }
202
//                                
203
//                        try {
204
//                                FLayer[] layerList = wmtsGUIInterface.getLayers();
205
//                                removeDuplicate(layerList, mapCtrl);
206
//                                for (int i = 0; i < layerList.length; i++) {
207
//                                        mapCtrl.getMapContext().getLayers().addLayer(layerList[i]);
208
//                                }
209
//                                mapCtrl.getMapContext().invalidate();
210
//                        } catch (LoadLayerException exc) {
211
//                                exc.printStackTrace();
212
//                        }
213
//                }
214
//                }
215
//    }
216
    
217
//    private void removeDuplicate(FLayer[] layerList, MapControl mapCtrl) {
218
//            FLayers lyrs = mapCtrl.getMapContext().getLayers();
219
//            for (int i = 0; i < lyrs.getLayersCount(); i++) {
220
//                        FLayer lyr = lyrs.getLayer(i);
221
//                        for (int j = 0; j < layerList.length; j++) {
222
//                                if(lyr.getName().compareTo(layerList[j].getName()) == 0) {
223
//                                        lyrs.removeLayer(lyr);
224
//                                }
225
//                        }
226
//                }
227
//    }
228
    
229
    
230
        public WindowInfo getWindowInfo() {
231
                if (m_ViewInfo == null) {
232
                        m_ViewInfo = new WindowInfo(WindowInfo.MODALDIALOG | WindowInfo.RESIZABLE);
233
                        m_ViewInfo.setTitle(PluginServices.getText(this,"fit_WMTS_layer"));
234
                m_ViewInfo.setWidth(500);
235
                m_ViewInfo.setHeight(500);
236
                }
237
                return m_ViewInfo;
238
        }
239

    
240
        public void close() {
241
                PluginServices.getMDIManager().closeWindow(this);
242
        }
243

    
244
        public Object getWindowProfile() {
245
                return WindowInfo.DIALOG_PROFILE;
246
        }
247
        
248
        public List<FLyrWMTS> getLayerList(RasterDataParameters[] paramsList) throws LoadLayerException {
249
                List<FLyrWMTS> layerList = new ArrayList<FLyrWMTS>();
250
                for (int i = 0; i < paramsList.length; i++) {
251
                        FLyrWMTS layer = new FLyrWMTS();
252
                        layer.deleteCache(wmtsGUIInterface.getDeleteCache());
253
                        try {
254
                                layer.setParameters(paramsList[i]);
255
                                String title = null;
256
                                if(paramsList[i] instanceof WMTSDataParameters) {
257
                                        title = ((WMTSDataParameters) paramsList[i]).getLayer().getTitle();
258
                                } else if(paramsList[i] instanceof TileDataParameters) {
259
                                        DataParameters p = ((TileDataParameters) paramsList[i]).getDataParameters();
260
                                        if(p instanceof WMTSDataParameters)
261
                                                title = ((WMTSDataParameters) p).getLayer().getTitle();
262
                                }
263
                                
264
                                layer.setName(title);
265
                                layer.setExplorer(explorer);
266
                                layerList.add(layer);
267
                        } catch (InitializeException e) {
268
                                throw new LoadLayerException("Error initializating layer", e);
269
                        }
270
                }
271
                return layerList;
272
        }
273

    
274
        public void actionButtonPressed(ButtonsPanelEvent e) {
275
                if(        e.getButton() == ButtonsPanel.BUTTON_APPLY || 
276
                        e.getButton() == ButtonsPanel.BUTTON_ACCEPT) {
277
                        MapControl mapCtrl = null;
278

    
279
                        IWindow[] w = PluginServices.getMDIManager().getAllWindows();
280
                        for (int i = 0; i < w.length; i++) {
281
                                if (w[i] instanceof org.gvsig.app.project.documents.view.gui.AbstractViewPanel) {
282
                                        MapContext mapContext = ((org.gvsig.app.project.documents.view.gui.AbstractViewPanel)w[i]).getMapControl().getMapContext();
283
                                        if(mapContext == fLayer.getMapContext())
284
                                                mapCtrl = ((org.gvsig.app.project.documents.view.gui.AbstractViewPanel)w[i]).getMapControl();
285
                                }
286
                        }
287
                        
288
                    try {
289
                            List<FLyrWMTS> layerList = getLayerList(wmtsGUIInterface.getDataParameters());
290
                            if(layerList.size() > 0 && layerList.get(0) != null) {
291
                                    for (int i = 0; i < layerList.size(); i++) {
292
                                            if(layerList.get(i) instanceof FLyrWMTS) {
293
                                                    mapCtrl.getMapContext().getLayers().removeLayer(fLayer);
294
                                                    mapCtrl.getMapContext().getLayers().addLayer(layerList.get(i));
295
                                                    fLayer = (FLyrWMTS)layerList.get(i);
296
                                            }
297
                                    }
298
                            }
299
                            //removeDuplicate(layerList, mapCtrl);
300
                            mapCtrl.getMapContext().invalidate();
301
                    } catch (LoadLayerException exc) {
302
                            exc.printStackTrace();
303
                    }
304
                }
305
                
306
                if(        e.getButton() == ButtonsPanel.BUTTON_CLOSE ||
307
                        e.getButton() == ButtonsPanel.BUTTON_ACCEPT) {
308
                        close();
309
                }
310
        }
311

    
312
}