Statistics
| Revision:

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

History | View | Annotate | Download (7.22 KB)

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

    
3
import java.awt.event.ActionEvent;
4
import java.awt.event.ActionListener;
5
import java.net.URL;
6
import java.util.ArrayList;
7

    
8
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
9
import com.iver.andami.PluginServices;
10
import com.iver.andami.messages.NotificationManager;
11
import com.iver.andami.ui.mdiManager.IWindow;
12
import com.iver.cit.gvsig.ProjectExtension;
13
import com.iver.cit.gvsig.fmap.MapControl;
14
import com.iver.cit.gvsig.fmap.drivers.wfs.FMapWFSDriver;
15
import com.iver.cit.gvsig.fmap.layers.FLayer;
16
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
17
import com.iver.cit.gvsig.fmap.layers.FLyrWFS;
18
import com.iver.cit.gvsig.fmap.layers.FLyrWFSFactory;
19
import com.iver.cit.gvsig.gui.panels.WFSAreaPanel;
20
import com.iver.cit.gvsig.gui.panels.WFSFilterPanel;
21
import com.iver.cit.gvsig.project.documents.ProjectDocument;
22
import com.iver.cit.gvsig.project.documents.table.ProjectTable;
23
import com.iver.cit.gvsig.project.documents.table.ProjectTableFactory;
24
import com.iver.cit.gvsig.project.documents.view.gui.BaseView;
25
import com.iver.cit.gvsig.project.documents.view.gui.View;
26

    
27
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
28
 *
29
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
30
 *
31
 * This program is free software; you can redistribute it and/or
32
 * modify it under the terms of the GNU General Public License
33
 * as published by the Free Software Foundation; either version 2
34
 * of the License, or (at your option) any later version.
35
 *
36
 * This program is distributed in the hope that it will be useful,
37
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
39
 * GNU General Public License for more details.
40
 *
41
 * You should have received a copy of the GNU General Public License
42
 * along with this program; if not, write to the Free Software
43
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
44
 *
45
 * For more information, contact:
46
 *
47
 *  Generalitat Valenciana
48
 *   Conselleria d'Infraestructures i Transport
49
 *   Av. Blasco Ib??ez, 50
50
 *   46010 VALENCIA
51
 *   SPAIN
52
 *
53
 *      +34 963862235
54
 *   gvsig@gva.es
55
 *      www.gvsig.gva.es
56
 *
57
 *    or
58
 *
59
 *   IVER T.I. S.A
60
 *   Salamanca 50
61
 *   46005 Valencia
62
 *   Spain
63
 *
64
 *   +34 963163400
65
 *   dac@iver.es
66
 */
67
/* CVS MESSAGES:
68
 *
69
 * $Id$
70
 * $Log$
71
 *
72
 */
73

    
74
/**
75
 * <p>Listener used when user presses any of the buttons of the WFS properties dialog.</p>
76
 * 
77
 * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
78
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
79
 */
80
public class WFSPropertiesDialogListener implements ActionListener {
81
        public static final String CANCEL_BUTTON_ACTION_COMMAND = "CANCEL";
82
        public static final String APPLY_BUTTON_ACTION_COMMAND = "APPLY";
83
        public static final String ACCEPT_BUTTON_ACTION_COMMAND = "ACCEPT";
84

    
85
        private WFSPropertiesDialog dialog = null;
86

    
87
        /**
88
         * Creates a new WFS properties dialog listener.
89
         * 
90
         * @param dialog reference to the WFS properties dialog
91
         */
92
        public WFSPropertiesDialogListener(WFSPropertiesDialog dialog) {
93
                this.dialog = dialog;
94
        }
95

    
96
        /* (non-Javadoc)
97
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
98
         */
99
        public void actionPerformed(ActionEvent e) {
100
                if (e.getActionCommand() == CANCEL_BUTTON_ACTION_COMMAND) {
101
                        dialog.close();
102
                        return;
103
                }
104

    
105
                // Accelerate the response: if there are not changes to apply it closes the dialog
106
                if ((e.getActionCommand() == ACCEPT_BUTTON_ACTION_COMMAND) && (! dialog.isEnabledApplyButton())) {
107
                        dialog.close();
108
                        return;
109
                }
110

    
111
                //Accept or apply
112
                try {
113
                        // We can't 'apply' if there is a filter query and it's incorrect
114
                        if (dialog.getFilterQuery() == null)
115
                                return;
116

    
117
                        // Gets a reference to the old layer
118
                        FLayer oldLayer = (FLayer) dialog.getReference();
119
                        String oldLayerName = oldLayer.getName();
120

    
121
                        // Gets the layer with the current information:
122
                        FLyrWFS newLayer = dialog.getFLayer(); 
123

    
124
                        // Gets the driver
125
                        FMapWFSDriver driver = newLayer.getWfsDriver(); //dialog.getDriver();                
126

    
127
                        // Sets the online resource
128
                        URL host = new URL(dialog.getWizardData().getHost());
129
                        String onlineResource = dialog.getWizardData().getOnlineResource();
130

    
131
                        // Tries to get the new layer
132
                        newLayer = new FLyrWFSFactory().getFLyrWFS(newLayer, host, onlineResource, driver, true, false);
133

    
134
                    // If can load the layer, update all data
135
                        if (newLayer != null) {
136
                                // Removes the last project table if exists
137
                                removeProjectTable((FLyrVect) oldLayer);
138

    
139
                                // Sets the new layer
140
                                dialog.updateReference(newLayer);
141
                                
142
                                // Replace the old layer entry at the TOC with the new one
143
                                BaseView activeView = (BaseView) PluginServices.getMDIManager().getActiveWindow();
144
                                MapControl mapCtrl = activeView.getMapControl();
145
                                mapCtrl.getMapContext().getLayers().replaceLayer(oldLayerName, newLayer);
146
                                mapCtrl.getMapContext().zoomToExtent(newLayer.getFullExtent());
147

    
148
                                // Refresh the active window where the new layer is located
149
                                refreshActiveWindow();
150

    
151
                                // Update data
152
                                if (e.getActionCommand() == APPLY_BUTTON_ACTION_COMMAND) {
153
                                        updateFilterAndAreaPanelsData();
154

    
155
                                        // Disable the apply button
156
                                        dialog.setEnabledApplyButton(false);
157
                                        return;
158
                                }
159
                        }
160

    
161
                        // Accept button clicked
162
                        if (e.getActionCommand() == ACCEPT_BUTTON_ACTION_COMMAND) {
163
                                dialog.close();
164
                                return;
165
                        }
166

    
167
                        return;
168
                } catch (Exception e1) {
169
                        NotificationManager.addError(e1);
170
                }                 
171
        }
172

    
173
        /**
174
         * Removes the associated project table of the <code>flayer</code>.
175
         * 
176
         * @param flayer a vector layer
177
         */
178
        private void removeProjectTable(FLyrVect flayer) {
179
                // Remove it
180
                ProjectExtension ext = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class);
181
                ArrayList<ProjectDocument> tables = ext.getProject().getDocumentsByType(ProjectTableFactory.registerName);
182

    
183
                for (int i=0 ; i < tables.size() ; i++){
184
                        ProjectTable projectTable = (ProjectTable)tables.get(i);
185

    
186
                        try {
187
                                if (flayer.getRecordset().equals(projectTable.getAssociatedTable().getRecordset())){
188
                                        ext.getProject().delDocument(projectTable);
189
                                }
190
                        } catch (ReadDriverException e) {
191
                                NotificationManager.addError(e);
192
                        }
193
                }
194
        }
195

    
196
        /**
197
         * @see WFSFilterPanel#setWFSFilterPanelIsAsTabForWFSLayersLoad(boolean)
198
         */
199
        private void setWFSFilterPanelIsAsTabForWFSLayersLoad(boolean b) {
200
                dialog.setWFSFilterPanelIsAsTabForWFSLayersLoad(b);
201
        }
202

    
203
        /**
204
         * <p>Refresh the active window.</p>
205
         */
206
        private void refreshActiveWindow() {
207
                IWindow window = PluginServices.getMDIManager().getActiveWindow();
208

    
209
                if (window instanceof View)
210
                        ((View)window).invalidate();
211
        }
212

    
213
        /**
214
         * Updates the information of the panels {@linkplain WFSFilterPanel WFSFilterPanel} and {@linkplain WFSAreaPanel WFSAreaPanel}.
215
         */
216
        private void updateFilterAndAreaPanelsData() {
217
                // Restores the private attribute of the area panel: hasUserDefinedAnArea
218
                dialog.setUserHasntDefinedAnArea();
219

    
220
                // If we load another layer, or the same but we've selected others fields -> notify it to the WFSFilter panel
221
                if (dialog.getFieldsSelectedOfSameLayerHasChanged()) {
222
                        dialog.resetFieldsSelectedOfSameLayerHasChanged(); // reset that field
223
                }
224

    
225
                // Update the data of the Filter and Area panels
226
                setWFSFilterPanelIsAsTabForWFSLayersLoad(false);
227
                dialog.updateWFSFilterFieldValues();
228
                dialog.updateWFSArea();
229
        }
230
}