Statistics
| Revision:

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

History | View | Annotate | Download (13.9 KB)

1 4503 jaume
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2 3746 jaume
*
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 4503 jaume
*   Av. Blasco Ib??ez, 50
24 3746 jaume
*   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 5334 jaume
import java.awt.Component;
44 4222 jaume
import java.awt.Dimension;
45 3746 jaume
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 6115 jaume
import org.gvsig.gui.beans.swing.JButton;
52 3746 jaume
import javax.swing.JDialog;
53
import javax.swing.JOptionPane;
54
import javax.swing.JPanel;
55 4962 ldiaz
import javax.swing.tree.TreePath;
56 3746 jaume
57
import com.iver.andami.PluginServices;
58 6877 cesar
import com.iver.andami.ui.mdiManager.IWindow;
59 3746 jaume
import com.iver.andami.ui.mdiManager.ViewInfo;
60
import com.iver.cit.gvsig.fmap.DriverException;
61
import com.iver.cit.gvsig.fmap.MapControl;
62 5334 jaume
import com.iver.cit.gvsig.fmap.exceptions.CannotReprojectException;
63 3746 jaume
import com.iver.cit.gvsig.fmap.layers.FLayer;
64 4970 ldiaz
import com.iver.cit.gvsig.fmap.layers.FLayers;
65 3746 jaume
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 4962 ldiaz
import com.iver.cit.gvsig.gui.wizards.LayerTreeModel;
69 3746 jaume
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 4881 jaume
 * The TOC WMS properties panel container.
75 6115 jaume
 *
76 3746 jaume
 * @author jaume - jaume.dominguez@iver.es
77
 *
78
 */
79 6877 cesar
public class WMSPropsDialog extends JPanel implements IWindow {
80 3746 jaume
        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 4969 ldiaz
        boolean applied = false;;
85 3746 jaume
        private ViewInfo m_ViewInfo = null;
86 6115 jaume
87 3746 jaume
        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 6115 jaume
98
99 3746 jaume
        private void initialize(FLayer layer) {
100
                setLayout(null);
101 6115 jaume
102 3746 jaume
        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 6115 jaume
113 5518 ldiaz
        })  ;
114
        wmsParamsTabbedPane.disableDisagregatedLayers();
115 3746 jaume
        this.add(wmsParamsTabbedPane);
116
        this.add(getButtonsPanel(), null);
117 6115 jaume
118 3746 jaume
        }
119
120 6115 jaume
121 3746 jaume
        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 6115 jaume
         *
132
         *
133 3746 jaume
         * 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 6115 jaume
         *
137
         *
138 4222 jaume
         * 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 3746 jaume
         * al servidor, recuperando los datos necesarios, rellenando el contenido y
142
         * dejando seleccionados los valores que estaban seleccionados cuando se
143 4222 jaume
         * guard? el proyecto.
144 6115 jaume
         *
145
         *
146 4222 jaume
         * 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 6115 jaume
         *
150
         *
151 5470 ldiaz
         * @return WMSParamsPanel
152 3746 jaume
         */
153
        public WMSParamsPanel getParamsPanel(HashMap info) {
154
            if (info!=null){
155
                try {
156 6115 jaume
157 4222 jaume
                        // host
158 3746 jaume
                URL host = (URL) info.get("host");
159
                WMSWizardData dataSource = new WMSWizardData();
160 4503 jaume
                dataSource.setHost(host, false);
161 6115 jaume
162 4222 jaume
                // name
163 6115 jaume
                    WMSParamsPanel toc = new WMSParamsPanel();
164
                    toc.setLayerName((String)info.get("name"));
165 3746 jaume
                    toc.setWizardData(dataSource);
166 6115 jaume
167 3746 jaume
                    toc.setVisible(true);
168
                    toc.setListenerSupport(new WizardListenerSupport());
169 6115 jaume
170 3746 jaume
                WMSLayerNode[] selectedLayers = (WMSLayerNode[]) ((Vector) info.get("selectedLayers")).toArray(new WMSLayerNode[0]);
171 4969 ldiaz
                //boolean any = false;
172 4222 jaume
                // selected layers
173 5518 ldiaz
                //((JDnDListModel) toc.getLstSelectedLayers().getModel()).clear();
174 3746 jaume
                    for (int i = 0; i < selectedLayers.length; i++) {
175 5518 ldiaz
                            toc.getJustTreeLayer().clearSelection();
176 6115 jaume
                            //  Se a?ade a la lista de layers seleccionados
177 4962 ldiaz
                            TreePath path = new TreePath(((LayerTreeModel)toc.getJustTreeLayer()
178
                                                                    .getModel()).getNodeByName(selectedLayers[i].getName()));
179
                            toc.getJustTreeLayer().addSelectionPath(path);
180
                            toc.addLayer();
181 4969 ldiaz
                            //This block is deleted since this is implemented in the addLayer method.
182 6115 jaume
//                    JDnDListModel modelo = (JDnDListModel) toc.getLstSelectedLayers().getModel();
183 4962 ldiaz
//                    if (modelo.addElement(0, selectedLayers[i])) {
184
//                        any = true;
185
//                    }
186 3746 jaume
                }
187 4962 ldiaz
//                if (any) {
188
//                    toc.refreshData();
189
//                }
190 6115 jaume
191 4222 jaume
                // srs
192 3746 jaume
                String srs = (String) info.get("srs");
193 4222 jaume
                int index = toc.getSRSIndex( srs );
194 3746 jaume
                    if (index != -1)
195
                        toc.getLstSRSs().setSelectedIndex(index);
196 6115 jaume
197 4222 jaume
                // format
198
                    String format = (String) info.get("format");
199 3746 jaume
                    index = toc.getFormatIndex(format);
200
                    if (index != -1)
201
                        toc.getLstFormats().setSelectedIndex(index);
202 6115 jaume
203 4222 jaume
                    // transparency
204 3746 jaume
                toc.setTransparent(((Boolean) info.get("wmsTransparency")).booleanValue());
205 6115 jaume
206 4222 jaume
                // styles
207 3746 jaume
                Vector v = (Vector) info.get("styles");
208
                if (v != null)
209
                        toc.setStyleSelections(v);
210 6115 jaume
211 4222 jaume
                // dimensions
212 3746 jaume
                v = (Vector) info.get("dimensions");
213
                if (v != null) toc.setDimensions(v);
214 6115 jaume
215 4222 jaume
                // fixed sizes
216
                Dimension sz = (Dimension) info.get("fixedSize");
217
                if (sz != null) toc.setFixedSize(sz);
218 3806 jaume
                toc.refreshInfo();
219 4222 jaume
                applied = false;
220 3746 jaume
                    return toc;
221
                } catch (DriverException ioe){
222
                    JOptionPane.showMessageDialog(null, "error_comunicacion_servidor", "Error", JOptionPane.ERROR_MESSAGE);
223 5470 ldiaz
                } catch (Exception e) {
224
                    e.printStackTrace();
225
                }
226 3746 jaume
            }
227 6115 jaume
            return null;
228 3746 jaume
        }
229
230 6115 jaume
231 3746 jaume
        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 6115 jaume
                        buttonsPanel.setLayout(null);
237 3746 jaume
                        buttonsPanel.setName("buttonPanel");
238 6115 jaume
239 3746 jaume
                buttonsPanel.add(getBtnOk(), null);
240
                buttonsPanel.add(getBtnApply(), null);
241
                buttonsPanel.add(getBtnCancel(), null);
242
                }
243
                return buttonsPanel;
244
        }
245 6115 jaume
246 3746 jaume
        public JButton getBtnOk() {
247
                if (btnOk == null) {
248 4222 jaume
                btnOk = new JButton("ok");
249
                btnOk.setText(PluginServices.getText(this,"ok"));
250 3746 jaume
                btnOk.setActionCommand("OK");
251
                btnOk.addActionListener(m_actionListener);
252
                btnOk.setBounds(367, 9, 90, 25);
253
                }
254
                return btnOk;
255
        }
256 6115 jaume
257 3746 jaume
        public JButton getBtnApply() {
258
                if (btnApply == null) {
259 3850 jaume
                btnApply = new JButton("apply");
260 4222 jaume
                btnApply.setText(PluginServices.getText(this,"apply"));
261 3746 jaume
                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 6115 jaume
269 3746 jaume
        public JButton getBtnCancel() {
270
                if (btnCancel == null) {
271 4222 jaume
                btnCancel = new JButton("cancel");
272
                btnCancel.setText(PluginServices.getText(this,"cancel"));
273 3746 jaume
                btnCancel.setActionCommand("CANCEL");
274
                btnCancel.addActionListener(m_actionListener);
275
                btnCancel.setBounds(137, 9, 90, 25);
276
                }
277
                return btnCancel;
278
        }
279 6115 jaume
280 3746 jaume
    private class CommandListener implements ActionListener {
281 6115 jaume
282 3746 jaume
        /**
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 5334 jaume
                    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 6115 jaume
                                }
305 5334 jaume
                ((FLyrWMS) fLayer).setFormat(wmsParamsTabbedPane.getFormat());
306 6115 jaume
307 5518 ldiaz
                if (!wmsParamsTabbedPane.getDisagregatedLayers())
308
                        ((FLyrWMS) fLayer).setLayerQuery(wmsParamsTabbedPane.getLayersQuery());
309 5334 jaume
                ((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 6115 jaume
317 4970 ldiaz
                    //fLayer = wmsParamsTabbedPane.getLayer();
318
                    FLayers parent = fLayer.getParentLayer();
319 6115 jaume
320 3746 jaume
                if (e.getActionCommand() == "APPLY"){
321
                    com.iver.cit.gvsig.gui.View vista = (com.iver.cit.gvsig.gui.View) PluginServices.getMDIManager().getActiveView();
322
                    MapControl mapCtrl = vista.getMapControl();
323 6115 jaume
324 4970 ldiaz
                    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 4969 ldiaz
                    //mapCtrl.getMapContext().getLayers().addLayer( fLayer );
334 3746 jaume
                    mapCtrl.getMapContext().invalidate();
335
                    applied = true;
336 6115 jaume
                    getBtnApply().setEnabled(!applied);
337
                }
338 3746 jaume
                if (e.getActionCommand() == "OK") {
339
                    if (!applied) {
340 6115 jaume
341 3746 jaume
                        com.iver.cit.gvsig.gui.View vista = (com.iver.cit.gvsig.gui.View) PluginServices.getMDIManager().getActiveView();
342
                        MapControl mapCtrl = vista.getMapControl();
343 6115 jaume
344 4970 ldiaz
                        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 3746 jaume
                    }
354
                    close();
355
                }
356
            }
357
                }
358
    }
359 4970 ldiaz
    /**
360
     * Merge two FLayers in one
361
     * @param group1
362
     * @param group2
363
     * @return
364
     */
365
    private FLayers mergeFLayers(FLayers group1, FLayers group2)
366 6115 jaume
    {
367 4970 ldiaz
            //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 6115 jaume
377 4970 ldiaz
            return group1;
378
    }
379 6115 jaume
380
381
382 3746 jaume
        public ViewInfo getViewInfo() {
383
                if (m_ViewInfo==null){
384
                        m_ViewInfo=new ViewInfo(ViewInfo.MODALDIALOG);
385 4222 jaume
                        m_ViewInfo.setTitle(PluginServices.getText(this,"fit_WMS_layer"));
386 3746 jaume
                m_ViewInfo.setWidth(wmsParamsTabbedPane.getWidth()+10);
387
                m_ViewInfo.setHeight(wmsParamsTabbedPane.getHeight()+40);
388
389
                }
390
                return m_ViewInfo;
391
        }
392 6115 jaume
393 3746 jaume
        public void close() {
394 6115 jaume
                PluginServices.getMDIManager().closeView(this);
395 3746 jaume
        }
396
397
}  //  @jve:decl-index=0:visual-constraint="10,10"