Statistics
| Revision:

gvsig-projects-pool / org.gvsig.vcsgis / trunk / org.gvsig.vcsgis / org.gvsig.vcsgis.app / org.gvsig.vcsgis.app.mainplugin / src / main / java / org / gvsig / vcsgis / app / VCSGisSwingServicesImpl.java @ 3452

History | View | Annotate | Download (16.9 KB)

1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.vcsgis.app;
7

    
8
import java.io.File;
9
import java.util.ArrayList;
10
import java.util.HashMap;
11
import java.util.Iterator;
12
import java.util.List;
13
import java.util.Map;
14
import javax.swing.ComboBoxModel;
15
import javax.swing.DefaultComboBoxModel;
16
import javax.swing.DefaultListModel;
17
import javax.swing.ListModel;
18
import javax.swing.tree.TreeModel;
19
import org.apache.commons.lang.StringUtils;
20
import org.gvsig.andami.PluginsLocator;
21
import org.gvsig.app.ApplicationLocator;
22
import org.gvsig.app.ApplicationManager;
23
import org.gvsig.app.project.Project;
24
import org.gvsig.app.project.ProjectManager;
25
import org.gvsig.app.project.documents.Document;
26
import org.gvsig.app.project.documents.DocumentManager;
27
import org.gvsig.app.project.documents.table.TableDocument;
28
import org.gvsig.app.project.documents.table.TableManager;
29
import org.gvsig.app.project.documents.view.ViewDocument;
30
import org.gvsig.app.project.documents.view.ViewManager;
31
import org.gvsig.app.project.documents.view.gui.IView;
32
import org.gvsig.fmap.dal.exception.DataException;
33
import org.gvsig.fmap.dal.feature.FeatureStore;
34
import org.gvsig.fmap.geom.Geometry;
35
import org.gvsig.fmap.geom.aggregate.MultiCurve;
36
import org.gvsig.fmap.geom.aggregate.MultiPoint;
37
import org.gvsig.fmap.geom.aggregate.MultiSurface;
38
import org.gvsig.fmap.geom.primitive.Curve;
39
import org.gvsig.fmap.geom.primitive.Point;
40
import org.gvsig.fmap.geom.primitive.Surface;
41
import org.gvsig.fmap.mapcontext.MapContext;
42
import org.gvsig.fmap.mapcontext.MapContextLocator;
43
import org.gvsig.fmap.mapcontext.layers.FLayer;
44
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
45
import org.gvsig.fmap.mapcontext.layers.vectorial.GraphicLayer;
46
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
47
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol_v2;
48
import org.gvsig.tools.ToolsLocator;
49
import org.gvsig.tools.i18n.I18nManager;
50
import org.gvsig.tools.util.LabeledValue;
51
import org.gvsig.tools.util.LabeledValueImpl;
52
import org.gvsig.vcsgis.lib.VCSGisLocator;
53
import org.gvsig.vcsgis.lib.VCSGisManager;
54
import org.gvsig.vcsgis.lib.VCSGisRuntimeException;
55
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspace;
56
import org.gvsig.vcsgis.swing.VCSGisSwingServices;
57
import org.slf4j.LoggerFactory;
58

    
59
/**
60
 *
61
 * @author gvSIG Team
62
 */
63
@SuppressWarnings("UseSpecificCatch")
64
public class VCSGisSwingServicesImpl implements VCSGisSwingServices {
65
    
66
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(VCSGisSwingServicesImpl.class);
67

    
68
    private Map<Integer,ISymbol> highlightedPolygonSymbols;
69
    private Map<Integer,ISymbol> highlightedLineSymbols;
70
    private Map<Integer,ISymbol> highlightedPointSymbols;
71

    
72

    
73
    @Override
74
    public TreeModel getFeatureStoresTreeModel() {
75
        return new FeatureStoresTreeModel();
76
    }
77

    
78
    @Override
79
    public ListModel getFeatureStoresListModel() {
80
        DefaultListModel<FeatureStore> model = new DefaultListModel();
81
        
82
        ApplicationManager appManager = ApplicationLocator.getApplicationManager();
83
        Project project = appManager.getCurrentProject();
84
        List<Document> tables = project.getDocuments(TableManager.TYPENAME);
85
        for (Document document : tables) {
86
            TableDocument table = (TableDocument) document;
87
            model.addElement(table.getFeatureStore());
88
        }
89
        
90
        return model;
91
    }
92

    
93
    @Override
94
    public void addTableToProject(VCSGisWorkspace ws, FeatureStore store) {
95
        ApplicationManager appManager = ApplicationLocator.getApplicationManager();
96
        
97
        DocumentManager tableManager = appManager.getProjectManager().getDocumentManager(
98
                TableManager.TYPENAME
99
        );
100

    
101
        Project project = appManager.getCurrentProject();
102
        TableDocument tableDoc = (TableDocument) tableManager.createDocument();
103
        
104
        tableDoc.setName(store.getName());
105
        tableDoc.setStore(store);
106
        project.addDocument(tableDoc);
107
    }
108

    
109
    @Override
110
    public ComboBoxModel getViewDocumentsComboBoxModel() {
111
        I18nManager i18n = ToolsLocator.getI18nManager();
112
        DefaultComboBoxModel<LabeledValue> model = new DefaultComboBoxModel();
113
        model.addElement(new LabeledValueImpl(i18n.getTranslation("_Select_a_view"), null));
114
        ApplicationManager appManager = ApplicationLocator.getApplicationManager();
115
        Project project = appManager.getCurrentProject();
116
        List<Document> views = project.getDocuments(ViewManager.TYPENAME);
117
        for (Document document : views) {
118
            ViewDocument view = (ViewDocument) document;
119
            model.addElement(new LabeledValueImpl(view.getName(), view));
120
        }
121
        return model;
122
    }
123

    
124
    @Override
125
    public void addLayerToView(FeatureStore store, LabeledValue labeledView) {
126
        addLayerToView(store, labeledView, null);
127
    }
128

    
129
    @Override
130
    public void addLayerToView(FeatureStore store, LabeledValue labeledView, String name) {
131
        if (labeledView.getValue() == null){
132
            return;
133
        }
134
        ViewDocument view = (ViewDocument) labeledView.getValue();
135
        addLayerToView(store, view, name);
136
    }
137
        
138

    
139
    private void addLayerToView(FeatureStore store, ViewDocument view, String name) {
140
        try {
141
            String layerName = name;
142
            if(StringUtils.isBlank(layerName)){
143
                layerName = store.getName();
144
            }
145
            FLayer layer = MapContextLocator.getMapContextManager().createLayer(layerName, store);
146
            view.getMapContext().getLayers().add(layer);
147
        } catch (Exception ex) {
148
            VCSGisManager manager = VCSGisLocator.getManager();
149
            throw new VCSGisRuntimeException(
150
                    VCSGisManager.ERR_CANT_ADD_LAYER, 
151
                    manager.getErrorMessage(VCSGisManager.ERR_CANT_ADD_LAYER),
152
                    ex);
153
        }
154
    }
155
    
156
    @Override
157
    public void highlight(int mode, Geometry geom) {
158
        highlight(mode, geom, null);
159
    }
160

    
161
    @Override
162
    public void highlight(int mode, Geometry geom, FeatureStore store) {
163
        if (this.highlightedPointSymbols == null) {
164
            this.highlightedPointSymbols = new HashMap<>();
165
            this.highlightedLineSymbols = new HashMap<>();
166
            this.highlightedPolygonSymbols = new HashMap<>();
167
            try {
168
                File pluginfolder = PluginsLocator.getManager().getPlugin(this).getPluginDirectory();
169
                File folder = new File(pluginfolder, "symbols");
170
                ISymbol[] symbols = MapContextLocator.getSymbolManager().loadSymbols(folder);
171
                for (ISymbol symbol : symbols) {
172
                    if (symbol instanceof ISymbol_v2) {
173
                        String symbolid = ((ISymbol_v2) symbol).getID();
174
                        switch(symbolid) {
175
                            case "vcsgis-repository-polygon":
176
                                this.highlightedPolygonSymbols.put(HIGHLIGHT_REPOSITORY, symbol);
177
                                break;
178
                            case "vcsgis-repository-line":
179
                                this.highlightedLineSymbols.put(HIGHLIGHT_REPOSITORY, symbol);
180
                                break;
181
                            case "vcsgis-repository-point":
182
                                this.highlightedPointSymbols.put(HIGHLIGHT_REPOSITORY, symbol);
183
                                break;
184
 
185
                            case "vcsgis-workspace-polygon":
186
                                this.highlightedPolygonSymbols.put(HIGHLIGHT_WORKSPACE, symbol);
187
                                break;
188
                            case "vcsgis-workspace-line":
189
                                this.highlightedLineSymbols.put(HIGHLIGHT_WORKSPACE, symbol);
190
                                break;
191
                            case "vcsgis-workspace-point":
192
                                this.highlightedPointSymbols.put(HIGHLIGHT_WORKSPACE, symbol);
193
                                break;
194
                            
195
                            case "vcsgis-workspace-previous-polygon":
196
                                this.highlightedPolygonSymbols.put(HIGHLIGHT_WORKSPACE_PREVIOUS, symbol);
197
                                break;
198
                            case "vcsgis-workspace-previous-line":
199
                                this.highlightedLineSymbols.put(HIGHLIGHT_WORKSPACE_PREVIOUS, symbol);
200
                                break;
201
                            case "vcsgis-workspace-previous-point":
202
                                this.highlightedPointSymbols.put(HIGHLIGHT_WORKSPACE_PREVIOUS, symbol);
203
                                break;
204
                        }
205
                    }
206
                }
207
            } catch (Exception ex) {
208
            }
209
        }
210
        List<ViewDocument> viewList = null;
211
        if(store == null){
212
            ApplicationManager application = ApplicationLocator.getManager();
213
            ViewDocument viewdoc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME);
214
            viewList = new ArrayList<>();
215
            viewList.add(viewdoc);
216
        } else {
217
            viewList = getViewDocumentsHavingAStore(store);
218
        }
219
        for (ViewDocument viewDoc : viewList) {
220
            MapContext mapContext = viewDoc.getMapContext();
221
            GraphicLayer gl = mapContext.getGraphicsLayer();
222
            if (geom != null) {
223
                ISymbol symbol = null;
224
                if (geom instanceof Point || geom instanceof MultiPoint) {
225
                    symbol = this.highlightedPointSymbols.get(mode);
226
                } else if (geom instanceof Curve || geom instanceof MultiCurve) {
227
                    symbol = this.highlightedLineSymbols.get(mode);
228
                } else if (geom instanceof Surface || geom instanceof MultiSurface) {
229
                    symbol = this.highlightedPolygonSymbols.get(mode);
230
                }
231
                if (symbol != null) {
232
                    int symbolid = gl.getSymbolId(symbol);
233
                    if (symbolid < 0) {
234
                        gl.addSymbol(symbol);
235
                        symbolid = gl.getSymbolId(symbol);
236
                    }
237
                    gl.addGraphic("vcsgis-highlighted", geom, symbolid);
238
                }
239
            }
240
            mapContext.invalidate();
241
        }
242
    }
243

    
244
    @Override
245
    public void centerActiveViewToGeometry(Geometry geometry) {
246
        if(geometry != null){
247
            IView view = getActiveView();
248
            if(view != null){
249
                ViewDocument viewDocument = view.getViewDocument();
250
                viewDocument.center(geometry.getEnvelope());
251
            }
252
        }
253
    }
254

    
255
    @Override
256
    public void centerViewsHavingAStoreToGeometry(FeatureStore store, Geometry geometry) {
257
        if(geometry != null){
258
            List<ViewDocument> views = getViewDocumentsHavingAStore(store);
259
            for (ViewDocument view : views) {
260
                if(view != null){
261
                    view.center(geometry.getEnvelope());
262
                }
263
            }
264
        }
265
    }
266

    
267
    @Override
268
    public void zoomActiveViewToGeometry(Geometry geometry) {
269
        if(geometry != null){
270
            IView view = getActiveView();
271
            if(view != null){
272
                ViewDocument viewDocument = view.getViewDocument();
273
                viewDocument.getMapContext().getViewPort().setEnvelope(geometry.getEnvelope());
274
            }
275
        }
276
    }
277

    
278
    @Override
279
    public void zoomViewsHavingAStoreToGeometry(FeatureStore store, Geometry geometry) {
280
        if(geometry != null){
281
            List<ViewDocument> views = getViewDocumentsHavingAStore(store);
282
            for (ViewDocument view : views) {
283
                if(view != null){
284
                    view.getMapContext().getViewPort().setEnvelope(geometry.getEnvelope());
285
                }
286
            }
287
        }
288
    }
289

    
290
    private IView getActiveView() {
291
        ApplicationManager application = ApplicationLocator.getManager();
292
        IView view = (IView) application.getActiveComponent(ViewDocument.class);
293
        return view;
294
    }
295

    
296
    @Override
297
    public void cleanHighligthed() {
298
        ApplicationManager application = ApplicationLocator.getManager();
299
        ProjectManager projectManager = application.getProjectManager();
300
        Project project = projectManager.getCurrentProject();
301
        List<Document> views = project.getDocuments(ViewManager.TYPENAME);
302
        for (Document doc : views) {
303
            ViewDocument viewdoc = (ViewDocument)doc;
304
            MapContext mapContext = viewdoc.getMapContext();
305
            GraphicLayer gl = mapContext.getGraphicsLayer();
306
            //FIXME: Refrescar el mapContext solo cuando se ha borrado el graphics  
307
            if(gl.removeGraphics("vcsgis-highlighted")) {
308
                mapContext.invalidate();
309
            }
310
        }
311
    }
312
    
313
    @Override
314
    public void cleanActiveViewHighligthed() {
315
        ApplicationManager application = ApplicationLocator.getManager();
316
        ViewDocument viewdoc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME);
317
        if(viewdoc != null){
318
            MapContext mapContext = viewdoc.getMapContext();
319
            GraphicLayer gl = mapContext.getGraphicsLayer();
320
            gl.removeGraphics("vcsgis-highlighted");
321
            mapContext.invalidate();
322
        }
323
    }
324
    
325
    @Override
326
    public void refreshDocument(FeatureStore store){
327
        if(store == null){
328
            return;
329
        }
330
        String storeFullName = store.getFullName();
331
        ApplicationManager application = ApplicationLocator.getManager();
332
        ProjectManager projectManager = application.getProjectManager();
333
        Project project = projectManager.getCurrentProject();
334
        List<Document> views = project.getDocuments(ViewManager.TYPENAME);
335
        for (Document doc : views) {
336
            ViewDocument view = (ViewDocument)doc;
337
            for (Iterator<FLayer> it = view.deepiterator(); it.hasNext();) {
338
                FLayer layer = it.next();
339
                if(layer instanceof FLyrVect){
340
                    if( StringUtils.equals(((FLyrVect) layer).getFeatureStore().getFullName(), storeFullName)){
341
                        view.getMapContext().invalidate();
342
                        break;
343
                    }
344
                }
345
            }
346
        }
347
        List<Document> tables = project.getDocuments(TableManager.TYPENAME);
348
        for (Document doc : tables) {
349
            TableDocument table = (TableDocument)doc;
350
            FeatureStore featureStore = table.getStore();
351
            if( StringUtils.equals(featureStore.getFullName(), storeFullName)){
352
                try {
353
                    featureStore.refresh();
354
                } catch (DataException e) {
355
                    LOGGER.warn("Error refreshing table", e);
356
                }
357
            }
358
        }
359
    }
360

    
361
    
362
    public List<ViewDocument> getViewDocumentsHavingAStore(FeatureStore store){
363
        if(store == null){
364
            return null;
365
        }
366
        List<ViewDocument> viewList = new ArrayList<>();
367
        
368
        String storeFullName = store.getFullName();
369
        ApplicationManager application = ApplicationLocator.getManager();
370
        ProjectManager projectManager = application.getProjectManager();
371
        Project project = projectManager.getCurrentProject();
372
        List<Document> views = project.getDocuments(ViewManager.TYPENAME);
373
        for (Document doc : views) {
374
            ViewDocument view = (ViewDocument) doc;
375
            for (Iterator<FLayer> it = view.deepiterator(); it.hasNext();) {
376
                FLayer layer = it.next();
377
                if (layer instanceof FLyrVect) {
378
                    if (StringUtils.equals(
379
                            ((FLyrVect) layer).getFeatureStore().getFullName(),
380
                            store.getFullName()
381
                    )) {
382
                        viewList.add(view);
383
                    }
384
                    break;
385
                }
386
            }
387
        }
388
        return viewList;
389
    }
390

    
391
    @Override
392
    public void addLayerToActiveView(FeatureStore store, String name) {
393
        if(store != null && StringUtils.isNotBlank(name)){
394
            IView view = getActiveView();
395
            if(view != null){
396
                ViewDocument viewDocument = view.getViewDocument();
397
                this.addLayerToView(store, viewDocument, name);
398
            }
399
        }
400
    }
401

    
402
    @Override
403
    public boolean isThereAnyActiveView() {
404
        return this.getActiveView() != null;
405
    }
406

    
407
    @Override
408
    public void addTableToProject(VCSGisWorkspace ws, FeatureStore store, String tableName) {
409
        ApplicationManager appManager = ApplicationLocator.getApplicationManager();
410
        
411
        DocumentManager tableManager = appManager.getProjectManager().getDocumentManager(
412
                TableManager.TYPENAME
413
        );
414

    
415
        Project project = appManager.getCurrentProject();
416
        TableDocument tableDoc = (TableDocument) tableManager.createDocument();
417
        
418
        tableDoc.setName(tableName);
419
        tableDoc.setStore(store);
420
        project.addDocument(tableDoc);
421
    }
422

    
423
}