Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extWFS2 / src / com / iver / cit / gvsig / gui / dialogs / WFSPropsDialog.java @ 9387

History | View | Annotate | Download (16.1 KB)

1
package com.iver.cit.gvsig.gui.dialogs;
2

    
3
import java.awt.Component;
4
import java.awt.event.ActionEvent;
5
import java.awt.event.ActionListener;
6
import java.net.MalformedURLException;
7
import java.net.URL;
8
import java.util.HashMap;
9

    
10
import javax.swing.JButton;
11
import javax.swing.JOptionPane;
12
import javax.swing.JPanel;
13

    
14
import org.gvsig.remoteClient.wfs.WFSStatus;
15

    
16
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
17
import com.iver.andami.PluginServices;
18
import com.iver.andami.ui.mdiManager.IWindow;
19
import com.iver.andami.ui.mdiManager.WindowInfo;
20
import com.iver.cit.gvsig.fmap.DriverException;
21
import com.iver.cit.gvsig.fmap.MapControl;
22
import com.iver.cit.gvsig.fmap.drivers.wfs.FMapWFSDriver;
23
import com.iver.cit.gvsig.fmap.layers.FLayer;
24
import com.iver.cit.gvsig.fmap.layers.FLayers;
25
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
26
import com.iver.cit.gvsig.fmap.layers.FLyrWFS;
27
import com.iver.cit.gvsig.fmap.layers.FLyrWFSFactory;
28
import com.iver.cit.gvsig.fmap.layers.WFSLayerNode;
29
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
30
import com.iver.cit.gvsig.gui.layout.Layout;
31
import com.iver.cit.gvsig.gui.panels.WFSParamsPanel;
32
import com.iver.cit.gvsig.gui.wizards.WFSWizardData;
33
import com.iver.cit.gvsig.gui.wizards.WizardListener;
34
import com.iver.cit.gvsig.gui.wizards.WizardListenerSupport;
35
import com.iver.cit.gvsig.project.documents.view.gui.View;
36

    
37
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
38
 *
39
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
40
 *
41
 * This program is free software; you can redistribute it and/or
42
 * modify it under the terms of the GNU General Public License
43
 * as published by the Free Software Foundation; either version 2
44
 * of the License, or (at your option) any later version.
45
 *
46
 * This program is distributed in the hope that it will be useful,
47
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
48
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
49
 * GNU General Public License for more details.
50
 *
51
 * You should have received a copy of the GNU General Public License
52
 * along with this program; if not, write to the Free Software
53
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
54
 *
55
 * For more information, contact:
56
 *
57
 *  Generalitat Valenciana
58
 *   Conselleria d'Infraestructures i Transport
59
 *   Av. Blasco Ib??ez, 50
60
 *   46010 VALENCIA
61
 *   SPAIN
62
 *
63
 *      +34 963862235
64
 *   gvsig@gva.es
65
 *      www.gvsig.gva.es
66
 *
67
 *    or
68
 *
69
 *   IVER T.I. S.A
70
 *   Salamanca 50
71
 *   46005 Valencia
72
 *   Spain
73
 *
74
 *   +34 963163400
75
 *   dac@iver.es
76
 */
77
/* CVS MESSAGES:
78
 *
79
 * $Id: WFSPropsDialog.java 9387 2006-12-20 14:21:22Z ppiqueras $
80
 * $Log$
81
 * Revision 1.20  2006-12-20 14:21:22  ppiqueras
82
 * Corregido bug que no actualice vista si el filtro no es correcto
83
 *
84
 * Revision 1.19  2006/12/19 08:27:29  jorpiell
85
 * Problem decreasing the selected attributes number solved
86
 *
87
 * Revision 1.18  2006/12/18 08:48:38  jorpiell
88
 * The props panel uses the same layer driver insteaf of create a nes driver each time
89
 *
90
 * Revision 1.17  2006/12/15 13:57:08  ppiqueras
91
 * Permite que se almacenen y/o recojan todos los campos y valores conocidos de la capa actual.
92
 *
93
 * Adem?s, control frentre a consultas de filtro err?neas, (esto a medias a?n).
94
 *
95
 * Revision 1.16  2006/12/13 14:00:24  ppiqueras
96
 * A?adido un comentario a una l?nea de instrucciones
97
 *
98
 * Revision 1.15  2006/12/04 08:59:47  ppiqueras
99
 * Algunos bugs corregidos. A cambio hay 2 bugs relacionados que todav?a no han sido corregidos (ver PHPCollab) (los tiene asignados Jorge).
100
 *
101
 * Revision 1.14  2006/11/28 08:05:31  jorpiell
102
 * Se refrescan los mapas despu?s de aplicar un filtro
103
 *
104
 * Revision 1.13  2006/11/16 16:57:49  jorpiell
105
 * Se copia la laeyenda de la capa anterior antes de eliminarla
106
 *
107
 * Revision 1.12  2006/11/14 13:45:49  ppiqueras
108
 * A?adida peque?a funcionalidad:
109
 * Cuando se pulsa el bot?n "Aplicar", (al seleccionar un nuevo filtro), si el ?rbol de campos posee alguno seleccionado, actualiza los valores (con los nuevos), asociados a ?ste campo seleccionado.
110
 *
111
 * Revision 1.11  2006/10/31 09:40:49  jorpiell
112
 * Se ha creado una factoria para crear la capa. De ese modo no se repite c?digo desde le panel de propiedades y desde el panel de la capa
113
 *
114
 * Revision 1.9  2006/10/23 08:47:12  jorpiell
115
 * Refactorizado un m?todo
116
 *
117
 * Revision 1.8  2006/10/23 08:29:06  ppiqueras
118
 * Algunos cambios
119
 *
120
 * Revision 1.6  2006/10/02 09:09:45  jorpiell
121
 * Cambios del 10 copiados al head
122
 *
123
 * Revision 1.4.2.3  2006/09/27 11:12:15  jorpiell
124
 * Hay que comprobar que se han devuelto un n?mero de features menor que el n?mero m?ximo permitido
125
 *
126
 * Revision 1.4.2.2  2006/09/27 09:15:01  jorpiell
127
 * Ya no se hace otra petici?n al pulsar sobre el bot?n aceptar.
128
 *
129
 * Revision 1.4.2.1  2006/09/19 12:28:11  jorpiell
130
 * Ya no se depende de geotools
131
 *
132
 *
133
 * Revision 1.4  2006/08/30 07:42:29  jorpiell
134
 * Se le asigna a la capa creada una proyecci?n concreta. Si esto no se hace, al exportar a postigis se produce un error.
135
 *
136
 * Revision 1.3  2006/08/29 07:56:12  cesar
137
 * Rename the *View* family of classes to *Window* (ie: SingletonView to SingletonWindow, ViewInfo to WindowInfo, etc)
138
 *
139
 * Revision 1.2  2006/08/29 07:13:40  cesar
140
 * Rename class com.iver.andami.ui.mdiManager.View to com.iver.andami.ui.mdiManager.IWindow
141
 *
142
 * Revision 1.1  2006/06/21 12:35:45  jorpiell
143
 * Se ha a?adido la ventana de propiedades. Esto implica a?adir listeners por todos los paneles. Adem?s no se muestra la geomatr?a en la lista de atributos y se muestran ?nicamnete los que se van a descargar
144
 *
145
 *
146
 */
147
/**
148
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
149
 * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
150
 */
151
public class WFSPropsDialog extends JPanel implements IWindow{
152
        private WFSParamsPanel wfsParamsTabbedPane = null;
153
        private WindowInfo m_ViewInfo = null;
154
        private FLayer fLayer = null;
155
        private JButton btnApply = null;
156
        private JButton btnOk = null;
157
        private JButton btnCancel = null;
158
        private CommandListener m_actionListener = null;
159
        private JPanel buttonsPanel = null;
160
        boolean applied = false;
161
        
162

    
163
    public WFSPropsDialog(FLayer layer) {
164
                super();
165
                initialize(layer);
166
        }
167
    
168
    private void initialize(FLayer layer) {
169
                setLayout(null);                
170
        setFLayer(layer);
171
        wfsParamsTabbedPane = getParamsPanel(((FLyrWFS) layer).getProperties());
172
        wfsParamsTabbedPane.addWizardListener(new WizardListener(){
173
                        public void wizardStateChanged(boolean finishable) {
174
                                getBtnOk().setEnabled(finishable);
175
                                getBtnApply().setEnabled(finishable);
176
                        }
177

    
178
                        public void error(Exception e) {
179
                        }
180
                
181
        })  ;
182
        //wfsParamsTabbedPane.disableDisagregatedLayers();
183
        this.add(wfsParamsTabbedPane);
184
        this.add(getButtonsPanel(), null);
185
    }
186
    
187
    public JPanel getButtonsPanel() {
188
                if (buttonsPanel == null) {
189
                        m_actionListener = new CommandListener(this);
190
                        buttonsPanel = new JPanel();
191
                buttonsPanel.setBounds(5, wfsParamsTabbedPane.getHeight(), 471, 40);
192
                        buttonsPanel.setLayout(null);     
193
                        buttonsPanel.setName("buttonPanel");
194
                        
195
                buttonsPanel.add(getBtnOk(), null);
196
                buttonsPanel.add(getBtnApply(), null);
197
                buttonsPanel.add(getBtnCancel(), null);
198
                }
199
                return buttonsPanel;
200
        }
201
        
202
    
203
    /**
204
         * With getParamsPanel we have access to the map config TabbedPane.
205
         * If this panel doesn't exist yet (which can occur when an existing project is
206
         * recovered) it is been automatically constructed by connecting to the server,
207
         * reloading the necessary data, filling up the content and setting the selected
208
         * values that were selected when the projet was saved.
209
         * 
210
         * 
211
         * Since a connection to the server is needed when rebuiliding the panel, this
212
         * causes a delay for the panel's showing up or a nullPointer error if there is
213
         * no path to the server.
214
         * 
215
         * 
216
         * Con getParamsPanel tenemos acceso a juego de pesta?as de configuraci?n
217
         * del mapa. Si este panel todav?a no existe (como puede ser cuando
218
         * recuperamos un proyecto guardado) ?ste se crea autom?ticamente conectando
219
         * al servidor, recuperando los datos necesarios, rellenando el contenido y
220
         * dejando seleccionados los valores que estaban seleccionados cuando se
221
         * guard? el proyecto.
222
         * 
223
         * 
224
         * Como para reconstruirse requiere una conexi?n con el servidor esto causa
225
         * un retardo en la aparici?n en el toc o un error de nullPointer si no
226
         * hay conexi?n hasta el servidor.
227
         * 
228
         * 
229
         * @return WMSParamsPanel
230
         */
231
        public WFSParamsPanel getParamsPanel(HashMap info) {
232
            if (info!=null){
233
                    URL host = (URL) info.get("host");
234
                    WFSWizardData dataSource = new WFSWizardData();
235
                    //dataSource.setHost(host, false);
236
                    dataSource.setDriver(((FLyrWFS)fLayer).getWfsDriver());
237
                    WFSParamsPanel toc = new WFSParamsPanel();
238
                    toc.setWizardData(dataSource);
239
                    toc.setListenerSupport(new WizardListenerSupport());
240
                    toc.setSelectedFeature((WFSLayerNode)info.get("wfsLayerNode"));
241
                    toc.setLayerName(fLayer.getName());
242
                    toc.refreshInfo((WFSLayerNode)info.get("wfsLayerNode"));
243
                    toc.setStatus((WFSStatus)info.get("status"));
244
                    toc.setVisible(true);
245
                    applied = false;
246
                    return toc;                    
247
            }
248
            return null;        
249
        }
250
        
251
        
252
        public WindowInfo getWindowInfo() {
253
                if (m_ViewInfo == null){
254
                        m_ViewInfo=new WindowInfo(WindowInfo.MODALDIALOG);
255
                        m_ViewInfo.setTitle(PluginServices.getText(this,"fit_WFS_layer"));
256
                        m_ViewInfo.setWidth(wfsParamsTabbedPane.getWidth()+ 10);
257
                        m_ViewInfo.setHeight(wfsParamsTabbedPane.getHeight() + 40);
258
                }
259
                return m_ViewInfo;
260
        }
261

    
262
        /**
263
         * @return Returns the fLayer.
264
         */
265
        public FLayer getFLayer() {
266
                return fLayer;
267
        }
268

    
269
        /**
270
         * @param layer The fLayer to set.
271
         */
272
        public void setFLayer(FLayer layer) {
273
                fLayer = layer;
274
        }
275

    
276
        public JButton getBtnOk() {
277
                if (btnOk == null) {
278
                btnOk = new JButton("ok");
279
                btnOk.setText(PluginServices.getText(this,"ok"));
280
                btnOk.setActionCommand("OK");
281
                btnOk.addActionListener(m_actionListener);
282
                btnOk.setBounds(367, 9, 90, 25);
283
                }
284
                return btnOk;
285
        }
286
        
287
        public JButton getBtnApply() {
288
                if (btnApply == null) {
289
                btnApply = new JButton("apply");
290
                btnApply.setText(PluginServices.getText(this,"apply"));
291
                btnApply.setEnabled(false);
292
                btnApply.setActionCommand("APPLY");
293
                btnApply.addActionListener(m_actionListener);
294
                btnApply.setBounds(267, 9, 90, 25);
295
                }
296
                return btnApply;
297
        }
298
        
299
        public JButton getBtnCancel() {
300
                if (btnCancel == null) {
301
                btnCancel = new JButton("cancel");
302
                btnCancel.setText(PluginServices.getText(this,"cancel"));
303
                btnCancel.setActionCommand("CANCEL");
304
                btnCancel.addActionListener(m_actionListener);
305
                btnCancel.setBounds(137, 9, 90, 25);
306
                }
307
                return btnCancel;
308
        }
309
        
310
        public void close() {
311
                PluginServices.getMDIManager().closeWindow(this);                
312
        }
313
        
314
    /**
315
     * Merge two FLayers in one
316
     * @param group1
317
     * @param group2
318
     * @return
319
     */
320
    private FLayers mergeFLayers(FLayers group1, FLayers group2)
321
    {            
322
            FLayer layer;
323
            for(int i = 0; i < group2.getLayersCount(); i++)
324
            {
325
                    layer = group2.getLayer( i );
326
                    if(group1.getLayer( layer.getName()) == null ){
327
                            group1.addLayer( layer );
328
                    }
329
            }
330
            
331
            return group1;
332
    }
333
        
334
         private class CommandListener implements ActionListener {
335
                 public CommandListener(WFSPropsDialog tp) {
336
                         //m_tp = tp;
337
                 }
338
                 
339
                 /* (non-Javadoc)
340
                  * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
341
                  */
342
                 public void actionPerformed(ActionEvent e) {
343
                         if ((e.getActionCommand() == "CANCEL") || (!getBtnApply().isEnabled())){
344
                                 close();
345
                         }else{
346
                                 String layerName = null;
347
                                 
348
                                 // Don't update if the filter is incorrect
349
                                 if (wfsParamsTabbedPane.getFilterQuery() != null) {
350
                                         layerName = fLayer.getName();
351
                                          View activeView = (View) PluginServices.getMDIManager().getActiveWindow();
352
                                          VectorialLegend legend = (VectorialLegend)((FLyrVect)fLayer).getLegend();
353
                                          FMapWFSDriver driver = wfsParamsTabbedPane.getDriver();                                                
354
                                          try {
355
                                                  fLayer = wfsParamsTabbedPane.getLayer();
356
                                                  URL host = new URL(wfsParamsTabbedPane.getData().getHost());
357
                                                  String onlineResource = wfsParamsTabbedPane.getData().getOnlineResource();
358
                                                  fLayer = new FLyrWFSFactory().getFLyrWFS(fLayer,host,onlineResource,driver,true);
359
                                                  if (fLayer != null){
360
                                                          ((FLyrWFS)fLayer).setLegend(legend);
361
                                                        
362
                                                          // Update all fields and values known about the current layer loaded (and selected)
363
                                                          ((FLyrWFS)fLayer).setAllFieldsAndValuesKnown(wfsParamsTabbedPane.getAllFieldsAndValuesKnownOfCurrentLayer());
364
                                                  }
365
                                         } catch (MalformedURLException e1) {
366
                                                 e1.printStackTrace();
367
                                         } catch (FieldNotFoundException e1) {
368
                                                 e1.printStackTrace();
369
                                         } catch (DriverException e1) {
370
                                                 e1.printStackTrace();
371
                                         }
372
                                 }
373
                                 
374
                                 if (e.getActionCommand() == "APPLY"){
375
                                        // We can't 'apply' if there is a filter query and it's incorrect
376
                                        if (wfsParamsTabbedPane.getFilterQuery() != null) {                                        
377
                                                 View vista = (View) PluginServices.getMDIManager().getActiveWindow();
378
                                                 MapControl mapCtrl = vista.getMapControl();
379
                                                 mapCtrl.getMapContext().getLayers().replaceLayer(layerName, fLayer);
380
                                                 mapCtrl.getMapContext().invalidate();
381
                                                 applied = true;
382
                                                 getBtnApply().setEnabled(!applied);
383
                                                 refreshLayouts();
384
                                                 // If we load another layer, or the same but we've selected others attributes -> notify it to he WFSFilter panel
385
                                                 if (getWFSFilterPanelIsAsTabForWFSLayersLoad() || (wfsParamsTabbedPane.getAttributesSelectedOfSameLayerHasChanged())) {
386
                                                         setWFSFilterPanelIsAsTabForWFSLayersLoad(false);
387
                                                         wfsParamsTabbedPane.resetAttributesSelectedOfSameLayerHasChanged(); // reset that attribute
388
                                                  }
389
                                                         
390
                                                 // Update values associated to the current selected field
391
                                                 wfsParamsTabbedPane.updateWFSFilterFieldValues();
392
                                         }
393
                                 }
394
                                
395
                                 if (e.getActionCommand() == "OK") {
396
                                        // We can't 'apply' if there is a filter query and it's incorrect
397
                                        if (wfsParamsTabbedPane.getFilterQuery() != null) {        
398
                                                 if (!applied) {
399
                                                         if (getBtnApply().isEnabled()){
400
                                                                 View vista = (View) PluginServices.getMDIManager().getActiveWindow();
401
                                                                 MapControl mapCtrl = vista.getMapControl();
402
                                                                 mapCtrl.getMapContext().getLayers().replaceLayer(layerName, fLayer);
403
                                                                 mapCtrl.getMapContext().invalidate();
404
                                                                 refreshLayouts();        
405
                                                         }
406
                                                 }        
407
                                                 close();
408
                                                 
409
                                                 // Update all fields and values known about the current layer loaded (and selected)
410
                                                 ((FLyrWFS)fLayer).setAllFieldsAndValuesKnown(wfsParamsTabbedPane.getAllFieldsAndValuesKnownOfCurrentLayer());
411
                                        }
412
                                 }
413
                         }
414
                 }
415

    
416
                 private boolean loadLayer(FLayer flayer){
417
                         try {
418
                                 ((FLyrWFS)fLayer).load();
419
                                
420
                                 if (((FLyrWFS)fLayer).getNumfeatures() == ((FLyrWFS)fLayer).getWfsDriver().getRowCount())  {
421
                                         JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),
422
                                                         PluginServices.getText(this,"maxFeatures_aviso"));        
423
                                 }
424
                         } catch (Exception e1) {
425
                                 e1.printStackTrace();
426
                                 JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),
427
                                                 PluginServices.getText(this,"cantLoad"));
428
                                 return false;
429
                         }
430
                         return true;
431
                 }                         
432
                 
433
         }
434
         
435
        /**
436
         * @see WFSFilterPanel#setWFSFilterPanelIsAsTabForWFSLayersLoad(boolean)
437
         */
438
        public void setWFSFilterPanelIsAsTabForWFSLayersLoad(boolean b) {
439
                this.wfsParamsTabbedPane.setWFSFilterPanelIsAsTabForWFSLayersLoad(b);
440
        }
441
        
442
        /**
443
         * @see WFSFilterPanel#getWFSFilterPanelIsAsTabForWFSLayersLoad()
444
         */
445
        public boolean getWFSFilterPanelIsAsTabForWFSLayersLoad() {
446
                return this.wfsParamsTabbedPane.getWFSFilterPanelIsAsTabForWFSLayersLoad();
447
        }
448
        
449
        /**
450
         * Sets all fielsd and values known of current layer to WFSFilterPanel
451
         */
452
        public void updateAllFieldsAndValuesKnownOfCurrentLayerToWFSFilterPanel() {
453
                this.wfsParamsTabbedPane.setAllFieldsAndValuesKnownOfCurrentLayer(((FLyrWFS)this.getFLayer()).getAllFieldsAndValuesKnown());
454
        }
455
        
456
        /**
457
         * Refresh the layouts
458
         *
459
         */
460
        private void refreshLayouts(){        
461
                IWindow[] windows = PluginServices.getMDIManager().getAllWindows();
462
                for (int i=0 ; i<windows.length ; i++){
463
                        if (windows[i] instanceof Layout){
464
                                ((Layout)windows[i]).fullRefresh();
465
                        }
466
                }
467
        }
468
}
469