Statistics
| Revision:

root / trunk / extensions / extWFS2 / src / com / iver / cit / gvsig / gui / dialogs / WFSPropsDialog.java @ 9392

History | View | Annotate | Download (16 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.panels.WFSParamsPanel;
31
import com.iver.cit.gvsig.gui.wizards.WFSWizardData;
32
import com.iver.cit.gvsig.gui.wizards.WizardListener;
33
import com.iver.cit.gvsig.gui.wizards.WizardListenerSupport;
34
import com.iver.cit.gvsig.project.documents.layout.gui.Layout;
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 9392 2006-12-20 14:50:10Z caballero $
80
 * $Log$
81
 * Revision 1.21  2006-12-20 14:50:10  caballero
82
 * Remodelado Layout
83
 *
84
 * Revision 1.20  2006/12/20 14:21:22  ppiqueras
85
 * Corregido bug que no actualice vista si el filtro no es correcto
86
 *
87
 * Revision 1.19  2006/12/19 08:27:29  jorpiell
88
 * Problem decreasing the selected attributes number solved
89
 *
90
 * Revision 1.18  2006/12/18 08:48:38  jorpiell
91
 * The props panel uses the same layer driver insteaf of create a nes driver each time
92
 *
93
 * Revision 1.17  2006/12/15 13:57:08  ppiqueras
94
 * Permite que se almacenen y/o recojan todos los campos y valores conocidos de la capa actual.
95
 *
96
 * Adem?s, control frentre a consultas de filtro err?neas, (esto a medias a?n).
97
 *
98
 * Revision 1.16  2006/12/13 14:00:24  ppiqueras
99
 * A?adido un comentario a una l?nea de instrucciones
100
 *
101
 * Revision 1.15  2006/12/04 08:59:47  ppiqueras
102
 * Algunos bugs corregidos. A cambio hay 2 bugs relacionados que todav?a no han sido corregidos (ver PHPCollab) (los tiene asignados Jorge).
103
 *
104
 * Revision 1.14  2006/11/28 08:05:31  jorpiell
105
 * Se refrescan los mapas despu?s de aplicar un filtro
106
 *
107
 * Revision 1.13  2006/11/16 16:57:49  jorpiell
108
 * Se copia la laeyenda de la capa anterior antes de eliminarla
109
 *
110
 * Revision 1.12  2006/11/14 13:45:49  ppiqueras
111
 * A?adida peque?a funcionalidad:
112
 * 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.
113
 *
114
 * Revision 1.11  2006/10/31 09:40:49  jorpiell
115
 * 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
116
 *
117
 * Revision 1.9  2006/10/23 08:47:12  jorpiell
118
 * Refactorizado un m?todo
119
 *
120
 * Revision 1.8  2006/10/23 08:29:06  ppiqueras
121
 * Algunos cambios
122
 *
123
 * Revision 1.6  2006/10/02 09:09:45  jorpiell
124
 * Cambios del 10 copiados al head
125
 *
126
 * Revision 1.4.2.3  2006/09/27 11:12:15  jorpiell
127
 * Hay que comprobar que se han devuelto un n?mero de features menor que el n?mero m?ximo permitido
128
 *
129
 * Revision 1.4.2.2  2006/09/27 09:15:01  jorpiell
130
 * Ya no se hace otra petici?n al pulsar sobre el bot?n aceptar.
131
 *
132
 * Revision 1.4.2.1  2006/09/19 12:28:11  jorpiell
133
 * Ya no se depende de geotools
134
 *
135
 *
136
 * Revision 1.4  2006/08/30 07:42:29  jorpiell
137
 * Se le asigna a la capa creada una proyecci?n concreta. Si esto no se hace, al exportar a postigis se produce un error.
138
 *
139
 * Revision 1.3  2006/08/29 07:56:12  cesar
140
 * Rename the *View* family of classes to *Window* (ie: SingletonView to SingletonWindow, ViewInfo to WindowInfo, etc)
141
 *
142
 * Revision 1.2  2006/08/29 07:13:40  cesar
143
 * Rename class com.iver.andami.ui.mdiManager.View to com.iver.andami.ui.mdiManager.IWindow
144
 *
145
 * Revision 1.1  2006/06/21 12:35:45  jorpiell
146
 * 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
147
 *
148
 *
149
 */
150
/**
151
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
152
 * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
153
 */
154
public class WFSPropsDialog extends JPanel implements IWindow{
155
        private WFSParamsPanel wfsParamsTabbedPane = null;
156
        private WindowInfo m_ViewInfo = null;
157
        private FLayer fLayer = null;
158
        private JButton btnApply = null;
159
        private JButton btnOk = null;
160
        private JButton btnCancel = null;
161
        private CommandListener m_actionListener = null;
162
        private JPanel buttonsPanel = null;
163
        boolean applied = false;
164

    
165

    
166
    public WFSPropsDialog(FLayer layer) {
167
                super();
168
                initialize(layer);
169
        }
170

    
171
    private void initialize(FLayer layer) {
172
                setLayout(null);
173
        setFLayer(layer);
174
        wfsParamsTabbedPane = getParamsPanel(((FLyrWFS) layer).getProperties());
175
        wfsParamsTabbedPane.addWizardListener(new WizardListener(){
176
                        public void wizardStateChanged(boolean finishable) {
177
                                getBtnOk().setEnabled(finishable);
178
                                getBtnApply().setEnabled(finishable);
179
                        }
180

    
181
                        public void error(Exception e) {
182
                        }
183

    
184
        })  ;
185
        //wfsParamsTabbedPane.disableDisagregatedLayers();
186
        this.add(wfsParamsTabbedPane);
187
        this.add(getButtonsPanel(), null);
188
    }
189

    
190
    public JPanel getButtonsPanel() {
191
                if (buttonsPanel == null) {
192
                        m_actionListener = new CommandListener(this);
193
                        buttonsPanel = new JPanel();
194
                buttonsPanel.setBounds(5, wfsParamsTabbedPane.getHeight(), 471, 40);
195
                        buttonsPanel.setLayout(null);
196
                        buttonsPanel.setName("buttonPanel");
197

    
198
                buttonsPanel.add(getBtnOk(), null);
199
                buttonsPanel.add(getBtnApply(), null);
200
                buttonsPanel.add(getBtnCancel(), null);
201
                }
202
                return buttonsPanel;
203
        }
204

    
205

    
206
    /**
207
         * With getParamsPanel we have access to the map config TabbedPane.
208
         * If this panel doesn't exist yet (which can occur when an existing project is
209
         * recovered) it is been automatically constructed by connecting to the server,
210
         * reloading the necessary data, filling up the content and setting the selected
211
         * values that were selected when the projet was saved.
212
         *
213
         *
214
         * Since a connection to the server is needed when rebuiliding the panel, this
215
         * causes a delay for the panel's showing up or a nullPointer error if there is
216
         * no path to the server.
217
         *
218
         *
219
         * Con getParamsPanel tenemos acceso a juego de pesta?as de configuraci?n
220
         * del mapa. Si este panel todav?a no existe (como puede ser cuando
221
         * recuperamos un proyecto guardado) ?ste se crea autom?ticamente conectando
222
         * al servidor, recuperando los datos necesarios, rellenando el contenido y
223
         * dejando seleccionados los valores que estaban seleccionados cuando se
224
         * guard? el proyecto.
225
         *
226
         *
227
         * Como para reconstruirse requiere una conexi?n con el servidor esto causa
228
         * un retardo en la aparici?n en el toc o un error de nullPointer si no
229
         * hay conexi?n hasta el servidor.
230
         *
231
         *
232
         * @return WMSParamsPanel
233
         */
234
        public WFSParamsPanel getParamsPanel(HashMap info) {
235
            if (info!=null){
236
                    URL host = (URL) info.get("host");
237
                    WFSWizardData dataSource = new WFSWizardData();
238
                    //dataSource.setHost(host, false);
239
                    dataSource.setDriver(((FLyrWFS)fLayer).getWfsDriver());
240
                    WFSParamsPanel toc = new WFSParamsPanel();
241
                    toc.setWizardData(dataSource);
242
                    toc.setListenerSupport(new WizardListenerSupport());
243
                    toc.setSelectedFeature((WFSLayerNode)info.get("wfsLayerNode"));
244
                    toc.setLayerName(fLayer.getName());
245
                    toc.refreshInfo((WFSLayerNode)info.get("wfsLayerNode"));
246
                    toc.setStatus((WFSStatus)info.get("status"));
247
                    toc.setVisible(true);
248
                    applied = false;
249
                    return toc;
250
            }
251
            return null;
252
        }
253

    
254

    
255
        public WindowInfo getWindowInfo() {
256
                if (m_ViewInfo == null){
257
                        m_ViewInfo=new WindowInfo(WindowInfo.MODALDIALOG);
258
                        m_ViewInfo.setTitle(PluginServices.getText(this,"fit_WFS_layer"));
259
                        m_ViewInfo.setWidth(wfsParamsTabbedPane.getWidth()+ 10);
260
                        m_ViewInfo.setHeight(wfsParamsTabbedPane.getHeight() + 40);
261
                }
262
                return m_ViewInfo;
263
        }
264

    
265
        /**
266
         * @return Returns the fLayer.
267
         */
268
        public FLayer getFLayer() {
269
                return fLayer;
270
        }
271

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

    
279
        public JButton getBtnOk() {
280
                if (btnOk == null) {
281
                btnOk = new JButton("ok");
282
                btnOk.setText(PluginServices.getText(this,"ok"));
283
                btnOk.setActionCommand("OK");
284
                btnOk.addActionListener(m_actionListener);
285
                btnOk.setBounds(367, 9, 90, 25);
286
                }
287
                return btnOk;
288
        }
289

    
290
        public JButton getBtnApply() {
291
                if (btnApply == null) {
292
                btnApply = new JButton("apply");
293
                btnApply.setText(PluginServices.getText(this,"apply"));
294
                btnApply.setEnabled(false);
295
                btnApply.setActionCommand("APPLY");
296
                btnApply.addActionListener(m_actionListener);
297
                btnApply.setBounds(267, 9, 90, 25);
298
                }
299
                return btnApply;
300
        }
301

    
302
        public JButton getBtnCancel() {
303
                if (btnCancel == null) {
304
                btnCancel = new JButton("cancel");
305
                btnCancel.setText(PluginServices.getText(this,"cancel"));
306
                btnCancel.setActionCommand("CANCEL");
307
                btnCancel.addActionListener(m_actionListener);
308
                btnCancel.setBounds(137, 9, 90, 25);
309
                }
310
                return btnCancel;
311
        }
312

    
313
        public void close() {
314
                PluginServices.getMDIManager().closeWindow(this);
315
        }
316

    
317
    /**
318
     * Merge two FLayers in one
319
     * @param group1
320
     * @param group2
321
     * @return
322
     */
323
    private FLayers mergeFLayers(FLayers group1, FLayers group2)
324
    {
325
            FLayer layer;
326
            for(int i = 0; i < group2.getLayersCount(); i++)
327
            {
328
                    layer = group2.getLayer( i );
329
                    if(group1.getLayer( layer.getName()) == null ){
330
                            group1.addLayer( layer );
331
                    }
332
            }
333

    
334
            return group1;
335
    }
336

    
337
         private class CommandListener implements ActionListener {
338
                 public CommandListener(WFSPropsDialog tp) {
339
                         //m_tp = tp;
340
                 }
341

    
342
                 /* (non-Javadoc)
343
                  * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
344
                  */
345
                 public void actionPerformed(ActionEvent e) {
346
                         if ((e.getActionCommand() == "CANCEL") || (!getBtnApply().isEnabled())){
347
                                 close();
348
                         }else{
349
                                 String layerName = null;
350

    
351
                                 // Don't update if the filter is incorrect
352
                                 if (wfsParamsTabbedPane.getFilterQuery() != null) {
353
                                         layerName = fLayer.getName();
354
                                          View activeView = (View) PluginServices.getMDIManager().getActiveWindow();
355
                                          VectorialLegend legend = (VectorialLegend)((FLyrVect)fLayer).getLegend();
356
                                          FMapWFSDriver driver = wfsParamsTabbedPane.getDriver();
357
                                          try {
358
                                                  fLayer = wfsParamsTabbedPane.getLayer();
359
                                                  URL host = new URL(wfsParamsTabbedPane.getData().getHost());
360
                                                  String onlineResource = wfsParamsTabbedPane.getData().getOnlineResource();
361
                                                  fLayer = new FLyrWFSFactory().getFLyrWFS(fLayer,host,onlineResource,driver,true);
362
                                                  if (fLayer != null){
363
                                                          ((FLyrWFS)fLayer).setLegend(legend);
364

    
365
                                                          // Update all fields and values known about the current layer loaded (and selected)
366
                                                          ((FLyrWFS)fLayer).setAllFieldsAndValuesKnown(wfsParamsTabbedPane.getAllFieldsAndValuesKnownOfCurrentLayer());
367
                                                  }
368
                                         } catch (MalformedURLException e1) {
369
                                                 e1.printStackTrace();
370
                                         } catch (FieldNotFoundException e1) {
371
                                                 e1.printStackTrace();
372
                                         } catch (DriverException e1) {
373
                                                 e1.printStackTrace();
374
                                         }
375
                                 }
376

    
377
                                 if (e.getActionCommand() == "APPLY"){
378
                                        // We can't 'apply' if there is a filter query and it's incorrect
379
                                        if (wfsParamsTabbedPane.getFilterQuery() != null) {
380
                                                 View vista = (View) PluginServices.getMDIManager().getActiveWindow();
381
                                                 MapControl mapCtrl = vista.getMapControl();
382
                                                 mapCtrl.getMapContext().getLayers().replaceLayer(layerName, fLayer);
383
                                                 mapCtrl.getMapContext().invalidate();
384
                                                 applied = true;
385
                                                 getBtnApply().setEnabled(!applied);
386
                                                 refreshLayouts();
387
                                                 // If we load another layer, or the same but we've selected others attributes -> notify it to he WFSFilter panel
388
                                                 if (getWFSFilterPanelIsAsTabForWFSLayersLoad() || (wfsParamsTabbedPane.getAttributesSelectedOfSameLayerHasChanged())) {
389
                                                         setWFSFilterPanelIsAsTabForWFSLayersLoad(false);
390
                                                         wfsParamsTabbedPane.resetAttributesSelectedOfSameLayerHasChanged(); // reset that attribute
391
                                                  }
392

    
393
                                                 // Update values associated to the current selected field
394
                                                 wfsParamsTabbedPane.updateWFSFilterFieldValues();
395
                                         }
396
                                 }
397

    
398
                                 if (e.getActionCommand() == "OK") {
399
                                        // We can't 'apply' if there is a filter query and it's incorrect
400
                                        if (wfsParamsTabbedPane.getFilterQuery() != null) {
401
                                                 if (!applied) {
402
                                                         if (getBtnApply().isEnabled()){
403
                                                                 View vista = (View) PluginServices.getMDIManager().getActiveWindow();
404
                                                                 MapControl mapCtrl = vista.getMapControl();
405
                                                                 mapCtrl.getMapContext().getLayers().replaceLayer(layerName, fLayer);
406
                                                                 mapCtrl.getMapContext().invalidate();
407
                                                                 refreshLayouts();
408
                                                         }
409
                                                 }
410
                                                 close();
411

    
412
                                                 // Update all fields and values known about the current layer loaded (and selected)
413
                                                 ((FLyrWFS)fLayer).setAllFieldsAndValuesKnown(wfsParamsTabbedPane.getAllFieldsAndValuesKnownOfCurrentLayer());
414
                                        }
415
                                 }
416
                         }
417
                 }
418

    
419
                 private boolean loadLayer(FLayer flayer){
420
                         try {
421
                                 ((FLyrWFS)fLayer).load();
422

    
423
                                 if (((FLyrWFS)fLayer).getNumfeatures() == ((FLyrWFS)fLayer).getWfsDriver().getRowCount())  {
424
                                         JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),
425
                                                         PluginServices.getText(this,"maxFeatures_aviso"));
426
                                 }
427
                         } catch (Exception e1) {
428
                                 e1.printStackTrace();
429
                                 JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),
430
                                                 PluginServices.getText(this,"cantLoad"));
431
                                 return false;
432
                         }
433
                         return true;
434
                 }
435

    
436
         }
437

    
438
        /**
439
         * @see WFSFilterPanel#setWFSFilterPanelIsAsTabForWFSLayersLoad(boolean)
440
         */
441
        public void setWFSFilterPanelIsAsTabForWFSLayersLoad(boolean b) {
442
                this.wfsParamsTabbedPane.setWFSFilterPanelIsAsTabForWFSLayersLoad(b);
443
        }
444

    
445
        /**
446
         * @see WFSFilterPanel#getWFSFilterPanelIsAsTabForWFSLayersLoad()
447
         */
448
        public boolean getWFSFilterPanelIsAsTabForWFSLayersLoad() {
449
                return this.wfsParamsTabbedPane.getWFSFilterPanelIsAsTabForWFSLayersLoad();
450
        }
451

    
452
        /**
453
         * Sets all fielsd and values known of current layer to WFSFilterPanel
454
         */
455
        public void updateAllFieldsAndValuesKnownOfCurrentLayerToWFSFilterPanel() {
456
                this.wfsParamsTabbedPane.setAllFieldsAndValuesKnownOfCurrentLayer(((FLyrWFS)this.getFLayer()).getAllFieldsAndValuesKnown());
457
        }
458

    
459
        /**
460
         * Refresh the layouts
461
         *
462
         */
463
        private void refreshLayouts(){
464
                IWindow[] windows = PluginServices.getMDIManager().getAllWindows();
465
                for (int i=0 ; i<windows.length ; i++){
466
                        if (windows[i] instanceof Layout){
467
                                ((Layout)windows[i]).getLayoutControl().fullRefresh();
468
                        }
469
                }
470
        }
471
}
472