Statistics
| Revision:

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

History | View | Annotate | Download (10.5 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.dialog;
24

    
25
import java.awt.BorderLayout;
26
import java.awt.event.ActionEvent;
27
import java.awt.event.ActionListener;
28
import java.util.HashMap;
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.app.gui.wizards.WizardListener;
37
import org.gvsig.app.gui.wizards.WizardListenerSupport;
38
import org.gvsig.fmap.mapcontext.MapContext;
39
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
40
import org.gvsig.fmap.mapcontext.layers.FLayer;
41
import org.gvsig.fmap.mapcontext.layers.FLayers;
42
import org.gvsig.fmap.mapcontrol.MapControl;
43
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel;
44
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelEvent;
45
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelListener;
46
import org.gvsig.raster.tools.app.basic.RasterToolsUtil;
47
import org.gvsig.raster.util.CancelTaskImpl;
48
import org.gvsig.raster.wmts.app.wmtsclient.gui.panel.WMTSParamsPanel;
49
import org.gvsig.raster.wmts.app.wmtsclient.layer.FLyrWMTS;
50
import org.gvsig.raster.wmts.io.WMTSServerExplorer;
51
import org.gvsig.remoteclient.wmts.exception.WMTSException;
52

    
53

    
54
/**
55
 * The TOC WMTS properties panel container.
56
 *
57
 * @author Nacho Brodin (nachobrodin@gmail.com)
58
 */
59
public class WMTSPropsDialog extends JPanel implements IWindow, ButtonsPanelListener {
60
        private static final long         serialVersionUID      = 1L;
61
        private FLyrWMTS                  fLayer                = null;
62
        private WindowInfo                m_ViewInfo            = null;
63
        private ButtonsPanel              buttonsPanel          = null;          
64
        private WMTSParamsPanel           wmtsParamsTabbedPane  = null;
65
    private WMTSServerExplorer        explorer              = null;
66

    
67
    public WMTSPropsDialog() {
68
            initialize(null);
69
    }
70
    
71
    public WMTSPropsDialog(FLayer layer) {
72
                super();
73
                if(layer instanceof FLyrWMTS) {
74
                        fLayer = (FLyrWMTS)layer;
75
                        initialize(fLayer);
76
                }
77
        }
78

    
79
        private void initialize(FLyrWMTS layer) {
80
                HashMap<String, Object> hash = layer != null ? layer.getProperties() : null;
81
                wmtsParamsTabbedPane = getParamsPanel(hash);
82
        wmtsParamsTabbedPane.addWizardListener(new WizardListener(){
83
                        @SuppressWarnings("deprecation")
84
                        public void wizardStateChanged(boolean finishable) {
85
                                getButtonsPanel().getButton(ButtonsPanel.BUTTON_ACCEPT).setEnabled(finishable);
86
                                getButtonsPanel().getButton(ButtonsPanel.BUTTON_APPLY).setEnabled(finishable);
87
                        }
88

    
89
                        public void error(Exception e) {
90
                        }
91

    
92
        });
93
                setLayout(new BorderLayout());
94
        this.add(wmtsParamsTabbedPane, BorderLayout.CENTER);
95
        this.add(getButtonsPanel(), BorderLayout.SOUTH);
96
        }
97
        
98
        /**
99
         * Gets the buttons panel
100
         * @return
101
         */
102
        public ButtonsPanel getButtonsPanel() {
103
                if(buttonsPanel == null) {
104
                        buttonsPanel = new ButtonsPanel();
105
                        buttonsPanel.addApply();
106
                        buttonsPanel.addAccept();
107
                        buttonsPanel.addClose();
108
                        buttonsPanel.addButtonPressedListener(this);
109
                }
110
                return buttonsPanel;
111
        }
112

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

    
148
                try {
149
                        if(explorer == null) {
150
                                explorer = ((FLyrWMTS)fLayer).getExplorer();
151

    
152
                                try {
153
                                        explorer.connect(new CancelTaskImpl());
154
                                } catch (WMTSException e) {
155
                                        RasterToolsUtil.messageBoxError("The connection cannot be established", this, e);
156
                                        return null;
157
                                } 
158
                        }
159
                } catch (Exception e) {
160
                        explorer = null;
161
                        JOptionPane.showMessageDialog(null, "error_comunicacion_servidor", "Error", JOptionPane.ERROR_MESSAGE);
162
                }
163
                try {
164

    
165
                        // name
166
                        WMTSParamsPanel toc = new WMTSParamsPanel();
167
                        toc.setLayerName((String)info.get("name"));
168
                        toc.setWizardData(explorer);
169

    
170
                        toc.setVisible(true);
171
                        toc.setListenerSupport(new WizardListenerSupport());
172

    
173
                        String selectedLayer = (String) info.get("selectedLayer");
174
                        toc.setSelectedLayer(selectedLayer);
175

    
176
                        // srs
177
                        String srs = (String) info.get("srs");
178
                        int index = toc.getSRSIndex( srs );
179
                        if (index != -1) {
180
                                toc.getLstSRSs().setSelectedIndex(index);
181
                        }
182

    
183
                        // image format
184
                        String imageFormat = (String) info.get("imageformat");
185
                        index = toc.getImageFormatIndex(imageFormat);
186
                        if (index != -1) {
187
                                toc.getImageFormats().setSelectedIndex(index);
188
                        }
189

    
190
                        // info format
191
                        String infoFormat = (String) info.get("infoformat");
192
                        index = toc.getInfoFormatIndex(infoFormat);
193
                        if (index != -1) {
194
                                toc.getInfoFormats().setSelectedIndex(index);
195
                        }
196

    
197
                        toc.refreshInfo();
198
                        toc.refreshData();
199
                        return toc;
200
                } catch (Exception e) {
201
                        JOptionPane.showMessageDialog(null, "error_comunicacion_servidor", "Error", JOptionPane.ERROR_MESSAGE);
202
                }
203

    
204
                return null;
205
        }
206

    
207
    @SuppressWarnings("unused")
208
        private class CommandListener implements ActionListener {
209

    
210
        /**
211
         * Creates a new ComandosListener object.
212
         *
213
         * @param lg DOCUMENT ME!
214
         */
215
        public CommandListener(WMTSPropsDialog tp) {
216
        }
217

    
218
                /* (non-Javadoc)
219
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
220
                 */
221
        public void actionPerformed(ActionEvent e) {
222
                if (e.getActionCommand() == "CANCEL") {
223
                        close();
224
                } else {
225
                                   MapControl mapCtrl = null;
226

    
227
                                IWindow[] w = PluginServices.getMDIManager().getAllWindows();
228
                                for (int i = 0; i < w.length; i++) {
229
                                        if (w[i] instanceof org.gvsig.app.project.documents.view.gui.AbstractViewPanel) {
230
                                                MapContext mapContext = ((org.gvsig.app.project.documents.view.gui.AbstractViewPanel)w[i]).getMapControl().getMapContext();
231
                                                if(mapContext == fLayer.getMapContext())
232
                                                        mapCtrl = ((org.gvsig.app.project.documents.view.gui.AbstractViewPanel)w[i]).getMapControl();
233
                                        }
234
                                }
235
                                
236
                        try {
237
                                FLayer[] layerList = wmtsParamsTabbedPane.getLayers();
238
                                removeDuplicate(layerList, mapCtrl);
239
                                for (int i = 0; i < layerList.length; i++) {
240
                                        mapCtrl.getMapContext().getLayers().addLayer(layerList[i]);
241
                                }
242
                                mapCtrl.getMapContext().invalidate();
243
                        } catch (LoadLayerException exc) {
244
                                exc.printStackTrace();
245
                        }
246
                }
247
                }
248
    }
249
    
250
    private void removeDuplicate(FLayer[] layerList, MapControl mapCtrl) {
251
            FLayers lyrs = mapCtrl.getMapContext().getLayers();
252
            for (int i = 0; i < lyrs.getLayersCount(); i++) {
253
                        FLayer lyr = lyrs.getLayer(i);
254
                        for (int j = 0; j < layerList.length; j++) {
255
                                if(lyr.getName().compareTo(layerList[j].getName()) == 0) {
256
                                        lyrs.removeLayer(lyr);
257
                                }
258
                        }
259
                }
260
    }
261
    
262
    
263
        public WindowInfo getWindowInfo() {
264
                if (m_ViewInfo == null) {
265
                        m_ViewInfo = new WindowInfo(WindowInfo.MODALDIALOG | WindowInfo.RESIZABLE);
266
                        m_ViewInfo.setTitle(PluginServices.getText(this,"fit_WMTS_layer"));
267
                m_ViewInfo.setWidth(500);
268
                m_ViewInfo.setHeight(500);
269
                }
270
                return m_ViewInfo;
271
        }
272

    
273
        public void close() {
274
                PluginServices.getMDIManager().closeWindow(this);
275
        }
276

    
277
        public Object getWindowProfile() {
278
                return WindowInfo.DIALOG_PROFILE;
279
        }
280

    
281
        public void actionButtonPressed(ButtonsPanelEvent e) {
282
                if(        e.getButton() == ButtonsPanel.BUTTON_APPLY || 
283
                        e.getButton() == ButtonsPanel.BUTTON_ACCEPT) {
284
                        MapControl mapCtrl = null;
285

    
286
                        IWindow[] w = PluginServices.getMDIManager().getAllWindows();
287
                        for (int i = 0; i < w.length; i++) {
288
                                if (w[i] instanceof org.gvsig.app.project.documents.view.gui.AbstractViewPanel) {
289
                                        MapContext mapContext = ((org.gvsig.app.project.documents.view.gui.AbstractViewPanel)w[i]).getMapControl().getMapContext();
290
                                        if(mapContext == fLayer.getMapContext())
291
                                                mapCtrl = ((org.gvsig.app.project.documents.view.gui.AbstractViewPanel)w[i]).getMapControl();
292
                                }
293
                        }
294
                        
295
                    try {
296
                            FLayer[] layerList = wmtsParamsTabbedPane.getLayers();
297
                            if(layerList.length > 0 && layerList[0] != null) {
298
                                    if(layerList[0] != fLayer) {
299
                                            mapCtrl.getMapContext().getLayers().removeLayer(fLayer);
300
                                            mapCtrl.getMapContext().getLayers().addLayer(layerList[0]);
301
                                    }
302
                            }
303
                            /*removeDuplicate(layerList, mapCtrl);
304
                            for (int i = 0; i < layerList.length; i++) {
305
                                    mapCtrl.getMapContext().getLayers().addLayer(layerList[i]);
306
                            }*/
307
                            mapCtrl.getMapContext().invalidate();
308
                    } catch (LoadLayerException exc) {
309
                            exc.printStackTrace();
310
                    }
311
                }
312
                
313
                if(        e.getButton() == ButtonsPanel.BUTTON_CLOSE ||
314
                        e.getButton() == ButtonsPanel.BUTTON_ACCEPT) {
315
                        close();
316
                }
317
        }
318

    
319
}  //  @jve:decl-index=0:visual-constraint="10,10"