Statistics
| Revision:

root / trunk / extensions / extWMS / src / com / iver / cit / gvsig / gui / dialogs / WMSPropsDialog.java @ 7304

History | View | Annotate | Download (14 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
*
3
* Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
*
19
* For more information, contact:
20
*
21
*  Generalitat Valenciana
22
*   Conselleria d'Infraestructures i Transport
23
*   Av. Blasco Ib??ez, 50
24
*   46010 VALENCIA
25
*   SPAIN
26
*
27
*      +34 963862235
28
*   gvsig@gva.es
29
*      www.gvsig.gva.es
30
*
31
*    or
32
*
33
*   IVER T.I. S.A
34
*   Salamanca 50
35
*   46005 Valencia
36
*   Spain
37
*
38
*   +34 963163400
39
*   dac@iver.es
40
*/
41
package com.iver.cit.gvsig.gui.dialogs;
42

    
43
import java.awt.Component;
44
import java.awt.Dimension;
45
import java.awt.event.ActionEvent;
46
import java.awt.event.ActionListener;
47
import java.net.URL;
48
import java.util.HashMap;
49
import java.util.Vector;
50

    
51
import org.gvsig.gui.beans.swing.JButton;
52
import javax.swing.JDialog;
53
import javax.swing.JOptionPane;
54
import javax.swing.JPanel;
55
import javax.swing.tree.TreePath;
56

    
57
import com.iver.andami.PluginServices;
58
import com.iver.andami.ui.mdiManager.IWindow;
59
import com.iver.andami.ui.mdiManager.WindowInfo;
60
import com.iver.cit.gvsig.fmap.DriverException;
61
import com.iver.cit.gvsig.fmap.MapControl;
62
import com.iver.cit.gvsig.fmap.exceptions.CannotReprojectException;
63
import com.iver.cit.gvsig.fmap.layers.FLayer;
64
import com.iver.cit.gvsig.fmap.layers.FLayers;
65
import com.iver.cit.gvsig.fmap.layers.FLyrWMS;
66
import com.iver.cit.gvsig.fmap.layers.WMSLayerNode;
67
import com.iver.cit.gvsig.gui.panels.WMSParamsPanel;
68
import com.iver.cit.gvsig.gui.wizards.LayerTreeModel;
69
import com.iver.cit.gvsig.gui.wizards.WMSWizardData;
70
import com.iver.cit.gvsig.gui.wizards.WizardListener;
71
import com.iver.cit.gvsig.gui.wizards.WizardListenerSupport;
72

    
73
/**
74
 * The TOC WMS properties panel container.
75
 *
76
 * @author jaume - jaume.dominguez@iver.es
77
 *
78
 */
79
public class WMSPropsDialog extends JPanel implements IWindow {
80
        JDialog dlg = null;  //  @jve:decl-index=0:visual-constraint="10,20"
81
        private JPanel buttonsPanel = null;
82
        //private FLyrDefault fLayer = null;
83
        private FLayer fLayer = null;
84
        boolean applied = false;;
85
        private WindowInfo m_ViewInfo = null;
86

    
87
        private JButton btnApply = null;
88
        private JButton btnOk = null;
89
        private JButton btnCancel = null;
90
        private WMSParamsPanel wmsParamsTabbedPane;
91
    private CommandListener m_actionListener;
92

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

    
98

    
99
        private void initialize(FLayer 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(FLayer 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
         * guard? 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
        public WMSParamsPanel getParamsPanel(HashMap info) {
154
            if (info!=null){
155
                try {
156

    
157
                        // host
158
                URL host = (URL) info.get("host");
159
                WMSWizardData dataSource = new WMSWizardData();
160
                dataSource.setHost(host, false);
161

    
162
                // name
163
                    WMSParamsPanel toc = new WMSParamsPanel();
164
                    toc.setLayerName((String)info.get("name"));
165
                    toc.setWizardData(dataSource);
166

    
167
                    toc.setVisible(true);
168
                    toc.setListenerSupport(new WizardListenerSupport());
169

    
170
                WMSLayerNode[] selectedLayers = (WMSLayerNode[]) ((Vector) info.get("selectedLayers")).toArray(new WMSLayerNode[0]);
171
                //boolean any = false;
172
                // selected layers
173
                //((JDnDListModel) toc.getLstSelectedLayers().getModel()).clear();
174
                    for (int i = 0; i < selectedLayers.length; i++) {
175
                            toc.getJustTreeLayer().clearSelection();
176
                            //  Se a?ade a la lista de layers seleccionados
177
                            TreePath path = new TreePath(((LayerTreeModel)toc.getJustTreeLayer()
178
                                                                    .getModel()).getNodeByName(selectedLayers[i].getName()));
179
                            toc.getJustTreeLayer().addSelectionPath(path);
180
                            toc.addLayer();
181
                            //This block is deleted since this is implemented in the addLayer method.
182
//                    JDnDListModel modelo = (JDnDListModel) toc.getLstSelectedLayers().getModel();
183
//                    if (modelo.addElement(0, selectedLayers[i])) {
184
//                        any = true;
185
//                    }
186
                }
187
//                if (any) {
188
//                    toc.refreshData();
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
                // format
198
                    String format = (String) info.get("format");
199
                    index = toc.getFormatIndex(format);
200
                    if (index != -1)
201
                        toc.getLstFormats().setSelectedIndex(index);
202

    
203
                    // transparency
204
                toc.setTransparent(((Boolean) info.get("wmsTransparency")).booleanValue());
205

    
206
                // styles
207
                Vector v = (Vector) info.get("styles");
208
                if (v != null)
209
                        toc.setStyleSelections(v);
210

    
211
                // dimensions
212
                v = (Vector) info.get("dimensions");
213
                if (v != null) toc.setDimensions(v);
214

    
215
                // fixed sizes
216
                Dimension sz = (Dimension) info.get("fixedSize");
217
                if (sz != null) toc.setFixedSize(sz);
218
                toc.refreshInfo();
219
                applied = false;
220
                    return toc;
221
                } catch (DriverException ioe){
222
                    JOptionPane.showMessageDialog(null, "error_comunicacion_servidor", "Error", JOptionPane.ERROR_MESSAGE);
223
                } catch (Exception e) {
224
                    e.printStackTrace();
225
                }
226
            }
227
            return null;
228
        }
229

    
230

    
231
        public JPanel getButtonsPanel() {
232
                if (buttonsPanel == null) {
233
                        m_actionListener = new CommandListener(this);
234
                        buttonsPanel = new JPanel();
235
                buttonsPanel.setBounds(5, wmsParamsTabbedPane.getHeight(), 471, 40);
236
                        buttonsPanel.setLayout(null);
237
                        buttonsPanel.setName("buttonPanel");
238

    
239
                buttonsPanel.add(getBtnOk(), null);
240
                buttonsPanel.add(getBtnApply(), null);
241
                buttonsPanel.add(getBtnCancel(), null);
242
                }
243
                return buttonsPanel;
244
        }
245

    
246
        public JButton getBtnOk() {
247
                if (btnOk == null) {
248
                btnOk = new JButton("ok");
249
                btnOk.setText(PluginServices.getText(this,"ok"));
250
                btnOk.setActionCommand("OK");
251
                btnOk.addActionListener(m_actionListener);
252
                btnOk.setBounds(367, 9, 90, 25);
253
                }
254
                return btnOk;
255
        }
256

    
257
        public JButton getBtnApply() {
258
                if (btnApply == null) {
259
                btnApply = new JButton("apply");
260
                btnApply.setText(PluginServices.getText(this,"apply"));
261
                btnApply.setEnabled(false);
262
                btnApply.setActionCommand("APPLY");
263
                btnApply.addActionListener(m_actionListener);
264
                btnApply.setBounds(267, 9, 90, 25);
265
                }
266
                return btnApply;
267
        }
268

    
269
        public JButton getBtnCancel() {
270
                if (btnCancel == null) {
271
                btnCancel = new JButton("cancel");
272
                btnCancel.setText(PluginServices.getText(this,"cancel"));
273
                btnCancel.setActionCommand("CANCEL");
274
                btnCancel.addActionListener(m_actionListener);
275
                btnCancel.setBounds(137, 9, 90, 25);
276
                }
277
                return btnCancel;
278
        }
279

    
280
    private class CommandListener implements ActionListener {
281

    
282
        /**
283
         * Creates a new ComandosListener object.
284
         *
285
         * @param lg DOCUMENT ME!
286
         */
287
        public CommandListener(WMSPropsDialog tp) {
288
            //m_tp = tp;
289
        }
290

    
291
                /* (non-Javadoc)
292
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
293
                 */
294
                public void actionPerformed(ActionEvent e) {
295
            if (e.getActionCommand() == "CANCEL")
296
                close();
297
            else{
298
                    try {
299
                                        ((FLyrWMS) fLayer).setFullExtent(wmsParamsTabbedPane.getLayersRectangle());
300
                                } catch (CannotReprojectException ex) {
301
                                        ex.printStackTrace();
302
                                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), ex.getMessage());
303
                                return;
304
                                }
305
                ((FLyrWMS) fLayer).setFormat(wmsParamsTabbedPane.getFormat());
306

    
307
                if (!wmsParamsTabbedPane.getDisagregatedLayers())
308
                        ((FLyrWMS) fLayer).setLayerQuery(wmsParamsTabbedPane.getLayersQuery());
309
                ((FLyrWMS) fLayer).setInfoLayerQuery(wmsParamsTabbedPane.getQueryableLayerQuery());
310
                ((FLyrWMS) fLayer).setSRS(wmsParamsTabbedPane.getSRS());
311
                ((FLyrWMS) fLayer).setName(wmsParamsTabbedPane.getLayerName());
312
                ((FLyrWMS) fLayer).setWmsTransparency(wmsParamsTabbedPane.getTransparency());
313
                ((FLyrWMS) fLayer).setStyles(wmsParamsTabbedPane.getStyles());
314
                ((FLyrWMS) fLayer).setDimensions(wmsParamsTabbedPane.getDimensions());
315
                ((FLyrWMS) fLayer).setFixedSize(wmsParamsTabbedPane.getFixedSize());
316

    
317
                    //fLayer = wmsParamsTabbedPane.getLayer();
318
                    FLayers parent = fLayer.getParentLayer();
319

    
320
                if (e.getActionCommand() == "APPLY"){
321
                    com.iver.cit.gvsig.project.documents.view.gui.View vista = (com.iver.cit.gvsig.project.documents.view.gui.View) PluginServices.getMDIManager().getActiveWindow();
322
                    MapControl mapCtrl = vista.getMapControl();
323

    
324
                    if (parent != null){
325
                            if ( wmsParamsTabbedPane.getLayer() instanceof FLayers){
326
                                    mapCtrl.getMapContext().getLayers().
327
                                    replaceLayer(parent.getName(),mergeFLayers(parent,(FLayers)wmsParamsTabbedPane.getLayer()));
328
                            }
329
                    }
330
                    else {
331
                            mapCtrl.getMapContext().getLayers().replaceLayer(fLayer.getName(), wmsParamsTabbedPane.getLayer());
332
                    }
333
                    //mapCtrl.getMapContext().getLayers().addLayer( fLayer );
334
                    mapCtrl.getMapContext().invalidate();
335
                    applied = true;
336
                    getBtnApply().setEnabled(!applied);
337
                }
338
                if (e.getActionCommand() == "OK") {
339
                    if (!applied) {
340

    
341
                        com.iver.cit.gvsig.project.documents.view.gui.View vista = (com.iver.cit.gvsig.project.documents.view.gui.View) PluginServices.getMDIManager().getActiveWindow();
342
                        MapControl mapCtrl = vista.getMapControl();
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
                        }                        mapCtrl.getMapContext().invalidate();
353
                    }
354
                    close();
355
                }
356
            }
357
                }
358
    }
359
    /**
360
     * Merge two FLayers in one
361
     * @param group1
362
     * @param group2
363
     * @return
364
     */
365
    private FLayers mergeFLayers(FLayers group1, FLayers group2)
366
    {
367
            //FLayers agrupation = new FLayers(group1.getFMap(), group1.getParentLayer());
368
            FLayer layer;
369
            for(int i = 0; i < group2.getLayersCount(); i++)
370
            {
371
                    layer = group2.getLayer( i );
372
                    if(group1.getLayer( layer.getName()) == null ){
373
                            group1.addLayer( layer );
374
                    }
375
            }
376

    
377
            return group1;
378
    }
379

    
380

    
381

    
382
        public WindowInfo getWindowInfo() {
383
                if (m_ViewInfo==null){
384
                        m_ViewInfo=new WindowInfo(WindowInfo.MODALDIALOG);
385
                        m_ViewInfo.setTitle(PluginServices.getText(this,"fit_WMS_layer"));
386
                m_ViewInfo.setWidth(wmsParamsTabbedPane.getWidth()+10);
387
                m_ViewInfo.setHeight(wmsParamsTabbedPane.getHeight()+40);
388

    
389
                }
390
                return m_ViewInfo;
391
        }
392

    
393
        public void close() {
394
                PluginServices.getMDIManager().closeWindow(this);
395
        }
396

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