Statistics
| Revision:

gvsig-raster / org.gvsig.raster.wms / branches / org.gvsig.raster.wms_dataaccess_refactoring / org.gvsig.raster.wms.app.wmsclient / src / main / java / org / gvsig / raster / wms / app / wmsclient / gui / dialog / WMSPropsDialog.java @ 2317

History | View | Annotate | Download (16.2 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.wms.app.wmsclient.gui.dialog;
24

    
25
import java.awt.Dimension;
26
import java.awt.event.ActionEvent;
27
import java.awt.event.ActionListener;
28
import java.util.HashMap;
29
import java.util.Vector;
30

    
31
import javax.swing.JButton;
32
import javax.swing.JDialog;
33
import javax.swing.JOptionPane;
34
import javax.swing.JPanel;
35
import javax.swing.tree.TreePath;
36

    
37
import org.gvsig.andami.PluginServices;
38
import org.gvsig.andami.ui.mdiManager.IWindow;
39
import org.gvsig.andami.ui.mdiManager.WindowInfo;
40
import org.gvsig.app.gui.wizards.WizardListener;
41
import org.gvsig.app.gui.wizards.WizardListenerSupport;
42
import org.gvsig.fmap.dal.coverage.exception.ConnectException;
43
import org.gvsig.fmap.dal.exception.InitializeException;
44
import org.gvsig.fmap.mapcontext.MapContext;
45
import org.gvsig.fmap.mapcontext.exceptions.ConnectionErrorLayerException;
46
import org.gvsig.fmap.mapcontext.exceptions.DriverLayerException;
47
import org.gvsig.fmap.mapcontext.exceptions.LegendLayerException;
48
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
49
import org.gvsig.fmap.mapcontext.exceptions.NameLayerException;
50
import org.gvsig.fmap.mapcontext.exceptions.ProjectionLayerException;
51
import org.gvsig.fmap.mapcontext.exceptions.TypeLayerException;
52
import org.gvsig.fmap.mapcontext.exceptions.URLLayerException;
53
import org.gvsig.fmap.mapcontext.exceptions.UnsupportedVersionLayerException;
54
import org.gvsig.fmap.mapcontext.layers.FLayer;
55
import org.gvsig.fmap.mapcontext.layers.FLayers;
56
import org.gvsig.fmap.mapcontrol.MapControl;
57
import org.gvsig.raster.fmap.layers.FLyrRaster;
58
import org.gvsig.raster.swing.RasterSwingLibrary;
59
import org.gvsig.raster.util.CancelTaskImpl;
60
import org.gvsig.raster.wms.app.wmsclient.gui.panel.WMSParamsPanel;
61
import org.gvsig.raster.wms.app.wmsclient.gui.wizard.LayerTreeModel;
62
import org.gvsig.raster.wms.app.wmsclient.layer.FLyrWMS;
63
import org.gvsig.raster.wms.io.WMSLayerNode;
64
import org.gvsig.raster.wms.io.WMSServerExplorer;
65
import org.slf4j.Logger;
66
import org.slf4j.LoggerFactory;
67

    
68

    
69
/**
70
 * The TOC WMS properties panel container.
71
 *
72
 * @author jaume - 
73
 *
74
 */
75
public class WMSPropsDialog extends JPanel implements IWindow {
76

    
77
        private static final long         serialVersionUID    = 1L;
78
        private Logger                   logger              = LoggerFactory.getLogger(WMSPropsDialog.class);
79
        JDialog                           dlg                 = null;  
80
        private JPanel                    buttonsPanel        = null;
81
        private FLyrRaster                fLayer              = null;
82
        boolean                           applied             = false;;
83
        private WindowInfo                m_ViewInfo          = null;
84

    
85
        private JButton                   btnApply            = null;
86
        private JButton                   btnOk               = null;
87
        private JButton                   btnCancel           = null;
88
        private WMSParamsPanel            wmsParamsTabbedPane;
89
    private CommandListener           m_actionListener;
90
    private WMSServerExplorer         explorer            = null;
91

    
92
    public WMSPropsDialog(FLayer layer) {
93
                super();
94
                if(layer instanceof FLyrRaster)
95
                        initialize((FLyrRaster)layer);
96
        }
97

    
98

    
99
        private void initialize(FLyrRaster layer) {
100
                setLayout(null);
101

    
102
        setFLayer(layer);
103
        wmsParamsTabbedPane = getParamsPanel(((FLyrWMS) layer).getProperties());
104
        wmsParamsTabbedPane.addWizardListener(new WizardListener(){
105
                        public void wizardStateChanged(boolean finishable) {
106
                                getBtnOk().setEnabled(finishable);
107
                                getBtnApply().setEnabled(finishable);
108
                        }
109

    
110
                        public void error(Exception e) {
111
                        }
112

    
113
        })  ;
114
        wmsParamsTabbedPane.disableDisagregatedLayers();
115
        this.add(wmsParamsTabbedPane);
116
        this.add(getButtonsPanel(), null);
117

    
118
        }
119

    
120

    
121
        public void setFLayer(FLyrRaster f) {
122
                fLayer = f;
123
        }
124

    
125
        /**
126
         * With getParamsPanel we have access to the map config TabbedPane.
127
         * If this panel doesn't exist yet (which can occur when an existing project is
128
         * recovered) it is been automatically constructed by connecting to the server,
129
         * reloading the necessary data, filling up the content and setting the selected
130
         * values that were selected when the projet was saved.
131
         *
132
         *
133
         * Since a connection to the server is needed when rebuiliding the panel, this
134
         * causes a delay for the panel's showing up or a nullPointer error if there is
135
         * no path to the server.
136
         *
137
         *
138
         * Con getParamsPanel tenemos acceso a juego de pesta?as de configuraci?n
139
         * del mapa. Si este panel todav?a no existe (como puede ser cuando
140
         * recuperamos un proyecto guardado) ?ste se crea autom?ticamente conectando
141
         * al servidor, recuperando los datos necesarios, rellenando el contenido y
142
         * dejando seleccionados los valores que estaban seleccionados cuando se
143
         * guarda el proyecto.
144
         *
145
         *
146
         * Como para reconstruirse requiere una conexi?n con el servidor esto causa
147
         * un retardo en la aparici?n en el toc o un error de nullPointer si no
148
         * hay conexi?n hasta el servidor.
149
         *
150
         *
151
         * @return WMSParamsPanel
152
         */
153
        @SuppressWarnings({ "unchecked" })
154
        public WMSParamsPanel getParamsPanel(HashMap info) {
155
            if (info != null) {
156
                    try {
157
                            if(explorer == null) {
158
                                        explorer = ((FLyrWMS)fLayer).getExplorer();
159
                                        
160
                                        try {
161
                                            explorer.connect(new CancelTaskImpl(), true);
162
                                        } catch (ConnectException e) {
163
                                                RasterSwingLibrary.messageBoxError("The connection cannot be established", this, e);
164
                                                return null;
165
                                        } 
166
                            }
167
                    } catch (Exception e) {
168
                            explorer = null;
169
                            JOptionPane.showMessageDialog(null, "error_comunicacion_servidor", "Error", JOptionPane.ERROR_MESSAGE);
170
                    }
171
                    try {
172
                        
173
                // name
174
                    WMSParamsPanel toc = new WMSParamsPanel();
175
                    toc.setLayerName((String)info.get("name"));
176
                    toc.setWizardData(explorer);
177

    
178
                    toc.setVisible(true);
179
                    toc.setListenerSupport(new WizardListenerSupport());
180

    
181
                    WMSLayerNode[] selectedLayers = (WMSLayerNode[]) ((Vector) info.get("selectedLayers")).toArray(new WMSLayerNode[0]);
182
                    for (int i = 0; i < selectedLayers.length; i++) {
183
                            toc.getJustTreeLayer().clearSelection();
184
                            //  Se a?ade a la lista de layers seleccionados
185
                            TreePath path = new TreePath(((LayerTreeModel)toc.getJustTreeLayer()
186
                                                                    .getModel()).getNodeByName(selectedLayers[i].getName()));
187
                            toc.getJustTreeLayer().addSelectionPath(path);
188
                            toc.addLayer();
189
                }
190

    
191
                // srs
192
                String srs = (String) info.get("srs");
193
                int index = toc.getSRSIndex( srs );
194
                    if (index != -1) {
195
                                        toc.getLstSRSs().setSelectedIndex(index);
196
                                }
197

    
198
                // format
199
                    String format = (String) info.get("format");
200
                    index = toc.getFormatIndex(format);
201
                    if (index != -1) {
202
                                        toc.getLstFormats().setSelectedIndex(index);
203
                                }
204
                    
205
                    //info format
206
                    String infoFormat = (String) info.get("infoformat");
207
                    index = toc.getFormatIndex(infoFormat);
208
                    if (index != -1) {
209
                                        toc.getLstInfoFormats().setSelectedIndex(index);
210
                                }
211

    
212
                    // transparency
213
                toc.setTransparent(((Boolean) info.get("wmsTransparency")).booleanValue());
214

    
215
                // styles
216
                Vector v = (Vector) info.get("styles");
217
                if (v != null && v.size() > 0) {
218
                                        toc.setStyleSelections(v);
219
                                }
220

    
221
                // dimensions
222
                v = (Vector) info.get("dimensions");
223
                if (v != null) {
224
                                        toc.setDimensions(v);
225
                                }
226

    
227
                // fixed sizes
228
                Dimension sz = (Dimension) info.get("fixedSize");
229
                if (sz != null) {
230
                                        toc.setFixedSize(sz);
231
                                }
232
                toc.refreshInfo();
233
                applied = false;
234
                    return toc;
235
                } catch (Exception e) {
236
                        JOptionPane.showMessageDialog(null, "error_comunicacion_servidor", "Error", JOptionPane.ERROR_MESSAGE);
237
                }
238
            }
239
            return null;
240
        }
241

    
242

    
243
        public JPanel getButtonsPanel() {
244
                if (buttonsPanel == null) {
245
                        m_actionListener = new CommandListener(this);
246
                        buttonsPanel = new JPanel();
247
                buttonsPanel.setBounds(5, wmsParamsTabbedPane.getHeight(), 471, 40);
248
                        buttonsPanel.setLayout(null);
249
                        buttonsPanel.setName("buttonPanel");
250

    
251
                buttonsPanel.add(getBtnOk(), null);
252
                buttonsPanel.add(getBtnApply(), null);
253
                buttonsPanel.add(getBtnCancel(), null);
254
                }
255
                return buttonsPanel;
256
        }
257

    
258
        public JButton getBtnOk() {
259
                if (btnOk == null) {
260
                btnOk = new JButton("ok");
261
                btnOk.setText(PluginServices.getText(this,"ok"));
262
                btnOk.setActionCommand("OK");
263
                btnOk.addActionListener(m_actionListener);
264
                btnOk.setBounds(367, 9, 90, 25);
265
                }
266
                return btnOk;
267
        }
268

    
269
        public JButton getBtnApply() {
270
                if (btnApply == null) {
271
                btnApply = new JButton("apply");
272
                btnApply.setText(PluginServices.getText(this,"apply"));
273
                btnApply.setEnabled(false);
274
                btnApply.setActionCommand("APPLY");
275
                btnApply.addActionListener(m_actionListener);
276
                btnApply.setBounds(267, 9, 90, 25);
277
                }
278
                return btnApply;
279
        }
280

    
281
        public JButton getBtnCancel() {
282
                if (btnCancel == null) {
283
                btnCancel = new JButton("cancel");
284
                btnCancel.setText(PluginServices.getText(this,"cancel"));
285
                btnCancel.setActionCommand("CANCEL");
286
                btnCancel.addActionListener(m_actionListener);
287
                btnCancel.setBounds(137, 9, 90, 25);
288
                }
289
                return btnCancel;
290
        }
291

    
292
    private class CommandListener implements ActionListener {
293

    
294
        /**
295
         * Creates a new ComandosListener object.
296
         *
297
         * @param lg DOCUMENT ME!
298
         */
299
        public CommandListener(WMSPropsDialog tp) {
300
            //m_tp = tp;
301
        }
302

    
303
                @SuppressWarnings({ "unchecked" })
304
                public void actionPerformed(ActionEvent e) {
305
            if (e.getActionCommand() == "CANCEL") {
306
                                close();
307
                        } else {
308
                    /*try {
309
                                        ((FLyrWMS) fLayer).setFullExtent(wmsParamsTabbedPane.getLayersRectangle());
310
                                } catch (ProjectionLayerException ex) {
311
                                        ex.printStackTrace();
312
                                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), ex.getMessage());
313
                                return;
314
                                }*/
315
                                ((FLyrWMS) fLayer).getParameters().setFormat(wmsParamsTabbedPane.getFormat());
316
                                ((FLyrWMS) fLayer).getParameters().setInfoFormat(wmsParamsTabbedPane.getInfoFormat());
317
                try {
318
                if (!wmsParamsTabbedPane.getDisagregatedLayers()) {
319
                                        ((FLyrWMS) fLayer).getParameters().setLayerQuery(wmsParamsTabbedPane.getLayersQuery());
320
                                }
321
                ((FLyrWMS) fLayer).getParameters().setInfoLayerQuery(wmsParamsTabbedPane.getQueryableLayerQuery());
322
                ((FLyrWMS) fLayer).getParameters().setSRS(wmsParamsTabbedPane.getSRS());
323
                ((FLyrWMS) fLayer).getParameters().setName(wmsParamsTabbedPane.getLayerName());
324
                ((FLyrWMS) fLayer).getParameters().setWmsTransparency(wmsParamsTabbedPane.getTransparency());
325
                ((FLyrWMS) fLayer).getParameters().setStyles(wmsParamsTabbedPane.getStyles());
326
                ((FLyrWMS) fLayer).getParameters().setDimensions(wmsParamsTabbedPane.getDimensions());
327
                ((FLyrWMS) fLayer).getParameters().setFixedSize(wmsParamsTabbedPane.getFixedSize());
328

    
329
                    //fLayer = wmsParamsTabbedPane.getLayer();
330
                    FLayers parent = ((FLayer)fLayer).getParentLayer();
331

    
332
                if (e.getActionCommand() == "APPLY"){
333
                        MapControl mapCtrl = null;
334
                        
335
                        IWindow[] w = PluginServices.getMDIManager().getAllWindows();
336
                        for (int i = 0; i < w.length; i++) {
337
                                                if (w[i] instanceof org.gvsig.app.project.documents.view.gui.AbstractViewPanel) {
338
                                                        MapContext mapContext = ((org.gvsig.app.project.documents.view.gui.AbstractViewPanel)w[i]).getMapControl().getMapContext();
339
                                                        if(mapContext == fLayer.getMapContext())
340
                                                                mapCtrl = ((org.gvsig.app.project.documents.view.gui.AbstractViewPanel)w[i]).getMapControl();
341
                                                }
342
                                        } 
343

    
344
                    if (parent != null) {
345
                            if ( wmsParamsTabbedPane.getLayer() instanceof FLayers){
346
                                                        mapCtrl.getMapContext().getLayers().
347
                                                        replaceLayer(parent.getName(),mergeFLayers(parent,(FLayers)wmsParamsTabbedPane.getLayer()));
348
                            }
349
                    }
350
                    else {
351
                            mapCtrl.getMapContext().getLayers().replaceLayer(fLayer.getName(), wmsParamsTabbedPane.getLayer());
352
                    }
353
                    //mapCtrl.getMapContext().getLayers().addLayer( fLayer );
354
                    mapCtrl.getMapContext().invalidate();
355
                    applied = true;
356
                    getBtnApply().setEnabled(!applied);
357
                }
358
                if (e.getActionCommand() == "OK") {
359
                    if (!applied) {
360

    
361
                        org.gvsig.app.project.documents.view.gui.AbstractViewPanel vista = (org.gvsig.app.project.documents.view.gui.AbstractViewPanel) PluginServices.getMDIManager().getActiveWindow();
362
                        MapControl mapCtrl = vista.getMapControl();
363

    
364
                        if (parent != null){
365
                                if ( wmsParamsTabbedPane.getLayer() instanceof FLayers){
366
                                        mapCtrl.getMapContext().getLayers().
367
                                        replaceLayer(parent.getName(),mergeFLayers(parent,(FLayers)wmsParamsTabbedPane.getLayer()));
368
                                }
369
                        }
370
                        else {
371
                                mapCtrl.getMapContext().getLayers().replaceLayer(fLayer.getName(), wmsParamsTabbedPane.getLayer());
372
                        }                        mapCtrl.getMapContext().invalidate();
373
                    }
374
                    close();
375
                }
376
                } catch (ConnectionErrorLayerException e1) {
377
                        logger.info("", e1);
378
                            } catch (DriverLayerException e1) {
379
                                    logger.info("", e1);
380
                            } catch (LegendLayerException e1) {
381
                                    logger.info("", e1);
382
                            } catch (NameLayerException e1) {
383
                                    logger.info("", e1);
384
                            } catch (ProjectionLayerException e1) {
385
                                    logger.info("", e1);
386
                            } catch (TypeLayerException e1) {
387
                                    logger.info("", e1);
388
                            } catch (UnsupportedVersionLayerException e1) {
389
                                    logger.info("", e1);
390
                            } catch (URLLayerException e1) {
391
                                    logger.info("", e1);
392
                            } catch (LoadLayerException e1) {
393
                                    logger.info("", e1);
394
                            } catch (InitializeException e1) {
395
                                        logger.info("", e1);
396
                                }
397
            }
398
                }
399
    }
400
    /**
401
     * Merge two FLayers in one
402
     * @param group1
403
     * @param group2
404
     * @return
405
     */
406
    private FLayers mergeFLayers(FLayers group1, FLayers group2)
407
    {
408
            //FLayers agrupation = new FLayers(group1.getFMap(), group1.getParentLayer());
409
            FLayer layer;
410
            for(int i = 0; i < group2.getLayersCount(); i++)
411
            {
412
                    layer = group2.getLayer( i );
413
                    if(group1.getLayer( layer.getName()) == null ){
414
                            group1.addLayer( layer );
415
                    }
416
            }
417

    
418
            return group1;
419
    }
420

    
421

    
422

    
423
        public WindowInfo getWindowInfo() {
424
                if (m_ViewInfo==null){
425
                        m_ViewInfo=new WindowInfo(WindowInfo.MODALDIALOG);
426
                        m_ViewInfo.setTitle(PluginServices.getText(this,"fit_WMS_layer"));
427
                m_ViewInfo.setWidth(wmsParamsTabbedPane.getWidth()+10);
428
                m_ViewInfo.setHeight(wmsParamsTabbedPane.getHeight()+40);
429

    
430
                }
431
                return m_ViewInfo;
432
        }
433

    
434
        public void close() {
435
                PluginServices.getMDIManager().closeWindow(this);
436
        }
437

    
438

    
439
        public Object getWindowProfile() {
440
                return WindowInfo.DIALOG_PROFILE;
441
        }
442

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