Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extGeocoding / src / org / gvsig / geocoding / extension / ResultsGeocodingExtension.java @ 31946

History | View | Annotate | Download (7.88 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 * 
21
 */
22

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2008 Prodevelop S.L. main development
26
 */
27

    
28
package org.gvsig.geocoding.extension;
29

    
30
import java.awt.Component;
31

    
32
import javax.swing.JOptionPane;
33

    
34
import org.gvsig.andami.PluginServices;
35
import org.gvsig.andami.plugins.Extension;
36
import org.gvsig.andami.preferences.IPreference;
37
import org.gvsig.andami.preferences.IPreferenceExtension;
38
import org.gvsig.andami.ui.mdiManager.IWindow;
39
import org.gvsig.app.extension.ProjectExtension;
40
import org.gvsig.app.project.Project;
41
import org.gvsig.app.project.documents.table.TableDocument;
42
import org.gvsig.app.project.documents.table.TableManager;
43
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel;
44
import org.gvsig.app.project.documents.view.gui.DefaultViewPanel;
45
import org.gvsig.app.project.documents.view.gui.IView;
46
import org.gvsig.fmap.dal.feature.FeatureStore;
47
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureStore;
48
import org.gvsig.fmap.mapcontext.layers.FLayer;
49
import org.gvsig.fmap.mapcontext.layers.FLayers;
50
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
51
import org.gvsig.geocoding.GeocodingLibrary;
52
import org.gvsig.geocoding.gui.results.ResultsPanel;
53
import org.gvsig.geocoding.impl.DefaultGeocodingLibrary;
54
import org.gvsig.geocoding.utils.GeocodingExtTags;
55
import org.gvsig.tools.library.Library;
56
import org.slf4j.Logger;
57
import org.slf4j.LoggerFactory;
58

    
59
/**
60
 * Geocoding Extension. This extension geoposition in the map one postal
61
 * address.
62
 * 
63
 * @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
64
 * @author <a href="mailto:vsanjaime@prodevelop.es"> Vicente Sanjaime Calvet</a>
65
 */
66

    
67
public class ResultsGeocodingExtension extends Extension implements
68
                IPreferenceExtension {
69

    
70
        @SuppressWarnings("unused")
71
        private static final Logger log = LoggerFactory
72
                        .getLogger(ResultsGeocodingExtension.class);
73

    
74
        private GeocodingController control = null;
75

    
76
        /**
77
         * constructor
78
         */
79
        public ResultsGeocodingExtension() {
80
                control = GeocodingController.getInstance();
81
        }
82

    
83
        /**
84
         * execute
85
         */
86
        public void execute(String actionCommand) {
87

    
88
                if (actionCommand.equalsIgnoreCase(GeocodingExtTags.RESULTSGEOCODING)) {
89
                        // get control
90
                        control = GeocodingController.getInstance();
91
                        // show attributes selected geocoding layer
92
                        DefaultFeatureStore shpstore = (DefaultFeatureStore) showTableAttributes();
93
                        // registry panel like store observer
94
                        shpstore.addObserver(control.getGResPanel());
95
                        control.getGResPanel().setSelectedStore(shpstore);
96
                        // Get table all results relate with the layer
97
                        TableDocument table = loadTableAllResultsGeocodingLayer();
98
                        FeatureStore allstore = table.getStore();
99
                        control.getGResPanel().setAllStore(allstore);
100
                        // show panel
101
                        IWindow[] wins = PluginServices.getMDIManager().getAllWindows();
102
                        boolean exist = false;
103
                        for (int i = 0; i < wins.length; i++) {
104
                                if (wins[i] instanceof ResultsPanel) {
105
                                        ((ResultsPanel) wins[i]).setVisible(true);
106
                                        exist = true;
107
                                        break;
108
                                }
109
                        }
110
                        if (!exist) {
111
                                PluginServices.getMDIManager()
112
                                                .addWindow(control.getGResPanel());
113
                                control.getGResPanel().setVisible(true);
114
                        }
115

    
116
                        // registry panel like store observer
117
                        shpstore.addObserver(control.getGResPanel());
118
                }
119
        }
120

    
121
        /**
122
         * This method initializes some parameters of the extension
123
         */
124
        public void initialize() {
125

    
126
                Library lib = new DefaultGeocodingLibrary();
127
                lib.initialize();
128
        }
129

    
130
        /**
131
         * This method puts available the extension
132
         * 
133
         * @return true or false
134
         */
135
        public boolean isEnabled() {
136

    
137
                IWindow window = PluginServices.getMDIManager().getActiveWindow();
138
                // Visible when there are views in the window
139
                if (window instanceof IView) {
140
                        return true;
141
                }
142
                return false;
143
        }
144

    
145
        /**
146
         * This method puts visible the extension
147
         * 
148
         * @return true or false
149
         */
150
        public boolean isVisible() {
151
                FLyrVect lyr = getSelectedVectLayer();
152
                if (lyr != null) {
153
                        Object obj = lyr.getProperty(GeocodingExtTags.GEOCODINGPROPERTY);
154
                        if (obj instanceof TableDocument) {
155
                                return true;
156
                        }
157
                        return false;
158
                }
159
                return false;
160
        }
161

    
162
        /**
163
         * Get array of extension preferences pages
164
         * 
165
         * @return
166
         */
167
        public IPreference[] getPreferencesPages() {
168
                return null;
169
        }
170

    
171
        /**
172
         * 
173
         */
174
        private FeatureStore showTableAttributes() {
175

    
176
                FLyrVect lyr = getSelectedVectLayer();
177
                FeatureStore store = null;
178
                if (lyr != null) {
179
                        try {
180
                                store = lyr.getFeatureStore();
181
                                if (store != null) {
182
                                        Project project = ((ProjectExtension) PluginServices
183
                                                        .getExtension(ProjectExtension.class)).getProject();
184

    
185
                                        TableManager tmanager = new TableManager();
186

    
187
                                        TableDocument table = (TableDocument) tmanager
188
                                                        .createDocument();
189
                                        table.setName(lyr.getName());
190
                                        table.setStore(store);
191

    
192
                                        project.add(table);
193

    
194
                                        FeatureTableDocumentPanel t = new FeatureTableDocumentPanel(
195
                                                        table);
196
                                        t.getModel().setModified(true);
197

    
198
                                        if (t == null) {
199
                                                JOptionPane.showMessageDialog(
200
                                                                (Component) PluginServices.getMainFrame(),
201
                                                                PluginServices.getText(this,
202
                                                                                "error_opening_the_document"));
203
                                        }
204
                                        PluginServices.getMDIManager().addWindow(t);
205
                                        return store;
206
                                }
207
                        } catch (Exception e) {
208
                                log.error("", e);
209
                        }
210
                }
211
                return null;
212

    
213
        }
214

    
215
        private FLyrVect getSelectedVectLayer() {
216
                IWindow window = PluginServices.getMDIManager().getActiveWindow();
217
                if (window instanceof IView) {
218
                        DefaultViewPanel vi = (DefaultViewPanel) window;
219
                        FLayers lyrs = vi.getMapControl().getMapContext().getLayers();
220
                        if (lyrs != null && lyrs.getLayersCount() > 0) {
221
                                if (lyrs.getActives().length > 0) {
222
                                        FLayer lyr = lyrs.getActives()[0];
223
                                        if (lyr instanceof FLyrVect) {
224
                                                return (FLyrVect) lyr;
225
                                        }
226
                                }
227
                        }
228
                }
229
                return null;
230
        }
231

    
232
        private FLyrVect getSelectedGeocodingVectLayer() {
233
                IWindow[] window = PluginServices.getMDIManager().getOrderedWindows();
234
                for (int i = 0; i < window.length; i++) {
235
                        if (window[i] instanceof IView) {
236
                                DefaultViewPanel vi = (DefaultViewPanel) window[i];
237
                                FLayers lyrs = vi.getMapControl().getMapContext().getLayers();
238
                                if (lyrs != null && lyrs.getLayersCount() > 0) {
239
                                        if (lyrs.getActives().length > 0) {
240
                                                FLayer[] lyr = lyrs.getActives();
241
                                                for (int k = 0; k < lyr.length; k++) {
242
                                                        if (lyr[k] instanceof FLyrVect) {
243
                                                                FLyrVect vlyr = (FLyrVect) lyr[k];
244
                                                                Object obj = vlyr
245
                                                                                .getProperty(GeocodingExtTags.GEOCODINGPROPERTY);
246
                                                                if (obj != null) {
247
                                                                        return vlyr;
248
                                                                }
249
                                                        }
250
                                                }
251
                                        }
252
                                }
253
                        }
254
                }
255

    
256
                return null;
257
        }
258

    
259
        private TableDocument loadTableAllResultsGeocodingLayer() {
260
                FLyrVect lyr = getSelectedGeocodingVectLayer();
261
                TableDocument table = (TableDocument) lyr
262
                                .getProperty(GeocodingExtTags.GEOCODINGPROPERTY);
263
                return table;
264
        }
265

    
266
        /**
267
         * 
268
         */
269
        public void postInitialize() {
270
                Library api = new GeocodingLibrary();
271
                api.postInitialize();
272
        }
273

    
274
}