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 / dialog / WMTSPropsDialog.java @ 2123

History | View | Annotate | Download (9.6 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.fmap.mapcontext.MapContext;
38
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
39
import org.gvsig.fmap.mapcontext.layers.FLayer;
40
import org.gvsig.fmap.mapcontext.layers.FLayers;
41
import org.gvsig.fmap.mapcontrol.MapControl;
42
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel;
43
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelEvent;
44
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelListener;
45
import org.gvsig.raster.swing.RasterSwingLibrary;
46
import org.gvsig.raster.util.CancelTaskImpl;
47
import org.gvsig.raster.wmts.app.wmtsclient.gui.panel.WMTSGUIInterface;
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.raster.wmts.ogc.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 WMTSGUIInterface          wmtsGUIInterface      = 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
                wmtsGUIInterface = getParamsPanel(hash);
82
                wmtsGUIInterface.addWizardListener(new WizardListener(){
83
                        public void wizardStateChanged(boolean finishable) {
84
                                getButtonsPanel().getButton(ButtonsPanel.BUTTON_ACCEPT).setEnabled(finishable);
85
                                getButtonsPanel().getButton(ButtonsPanel.BUTTON_APPLY).setEnabled(finishable);
86
                        }
87

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

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

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

    
146
                try {
147
                        if(explorer == null) {
148
                                explorer = ((FLyrWMTS)fLayer).getExplorer();
149

    
150
                                try {
151
                                        explorer.connect(new CancelTaskImpl());
152
                                } catch (WMTSException e) {
153
                                        RasterSwingLibrary.messageBoxError("The connection cannot be established", this, e);
154
                                        return null;
155
                                } 
156
                        }
157
                } catch (Exception e) {
158
                        explorer = null;
159
                        JOptionPane.showMessageDialog(null, "error_comunicacion_servidor", "Error", JOptionPane.ERROR_MESSAGE);
160
                }
161
                
162
                try {
163
                        WMTSGUIInterface toc = new WMTSParamsPanel();
164
                        ((WMTSParamsPanel)toc).setWizardData(explorer);
165
                        toc.updateSelectedInfo(info);
166
                        return toc;
167
                } catch (Exception e) {
168
                        JOptionPane.showMessageDialog(null, "error_comunicacion_servidor", "Error", JOptionPane.ERROR_MESSAGE);
169
                }
170

    
171
                return null;
172
        }
173

    
174
    @SuppressWarnings("unused")
175
        private class CommandListener implements ActionListener {
176

    
177
        /**
178
         * Creates a new ComandosListener object.
179
         *
180
         * @param lg DOCUMENT ME!
181
         */
182
        public CommandListener(WMTSPropsDialog tp) {
183
        }
184

    
185
                /* (non-Javadoc)
186
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
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 void actionButtonPressed(ButtonsPanelEvent e) {
249
                if(        e.getButton() == ButtonsPanel.BUTTON_APPLY || 
250
                        e.getButton() == ButtonsPanel.BUTTON_ACCEPT) {
251
                        MapControl mapCtrl = null;
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
                                        MapContext mapContext = ((org.gvsig.app.project.documents.view.gui.AbstractViewPanel)w[i]).getMapControl().getMapContext();
257
                                        if(mapContext == fLayer.getMapContext())
258
                                                mapCtrl = ((org.gvsig.app.project.documents.view.gui.AbstractViewPanel)w[i]).getMapControl();
259
                                }
260
                        }
261
                        
262
                    try {
263
                            FLayer[] layerList = wmtsGUIInterface.getLayers();
264
                            if(layerList.length > 0 && layerList[0] != null) {
265
                                    if(layerList[0] instanceof FLyrWMTS) {
266
                                            mapCtrl.getMapContext().getLayers().removeLayer(fLayer);
267
                                            mapCtrl.getMapContext().getLayers().addLayer(layerList[0]);
268
                                            fLayer = (FLyrWMTS)layerList[0];
269
                                    }
270
                            }
271
                            //removeDuplicate(layerList, mapCtrl);
272
                            mapCtrl.getMapContext().invalidate();
273
                    } catch (LoadLayerException exc) {
274
                            exc.printStackTrace();
275
                    }
276
                }
277
                
278
                if(        e.getButton() == ButtonsPanel.BUTTON_CLOSE ||
279
                        e.getButton() == ButtonsPanel.BUTTON_ACCEPT) {
280
                        close();
281
                }
282
        }
283

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