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 @ 3459

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

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

    
70
    private Map<Integer,ISymbol> highlightedPolygonSymbols;
71
    private Map<Integer,ISymbol> highlightedLineSymbols;
72
    private Map<Integer,ISymbol> highlightedPointSymbols;
73

    
74

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
391
            List<Document> tables = project.getDocuments(TableManager.TYPENAME);
392
            for (Document doc : tables) {
393
                TableDocument table = (TableDocument)doc;
394
                FeatureStore featureStore = table.getStore();
395
                if( StringUtils.equals(featureStore.getFullName(), storeFullName)){
396
                    try {
397
                        featureStore.refresh();
398
                    } catch (DataException e) {
399
                        LOGGER.warn("Error refreshing table", e);
400
                    }
401
                }
402
            }
403
        }
404
        
405
        for (ViewDocument viewDocument : viewsToRefresh) {
406
            viewDocument.getMapContext().invalidate();
407
        }
408
        
409
    }
410

    
411
    
412
    public List<ViewDocument> getViewDocumentsHavingAStore(FeatureStore store){
413
        if(store == null){
414
            return null;
415
        }
416
        List<ViewDocument> viewList = new ArrayList<>();
417
        
418
        String storeFullName = store.getFullName();
419
        ApplicationManager application = ApplicationLocator.getManager();
420
        ProjectManager projectManager = application.getProjectManager();
421
        Project project = projectManager.getCurrentProject();
422
        List<Document> views = project.getDocuments(ViewManager.TYPENAME);
423
        for (Document doc : views) {
424
            ViewDocument view = (ViewDocument) doc;
425
            for (Iterator<FLayer> it = view.deepiterator(); it.hasNext();) {
426
                FLayer layer = it.next();
427
                if (layer instanceof FLyrVect) {
428
                    if (StringUtils.equals(
429
                            ((FLyrVect) layer).getFeatureStore().getFullName(),
430
                            store.getFullName()
431
                    )) {
432
                        viewList.add(view);
433
                    }
434
                    break;
435
                }
436
            }
437
        }
438
        return viewList;
439
    }
440

    
441
    @Override
442
    public void addLayerToActiveView(FeatureStore store, String name) {
443
        if(store != null && StringUtils.isNotBlank(name)){
444
            IView view = getActiveView();
445
            if(view != null){
446
                ViewDocument viewDocument = view.getViewDocument();
447
                this.addLayerToView(store, viewDocument, name);
448
            }
449
        }
450
    }
451

    
452
    @Override
453
    public boolean isThereAnyActiveView() {
454
        return this.getActiveView() != null;
455
    }
456

    
457
    @Override
458
    public void addTableToProject(VCSGisWorkspace ws, FeatureStore store, String tableName) {
459
        ApplicationManager appManager = ApplicationLocator.getApplicationManager();
460
        
461
        DocumentManager tableManager = appManager.getProjectManager().getDocumentManager(
462
                TableManager.TYPENAME
463
        );
464

    
465
        Project project = appManager.getCurrentProject();
466
        TableDocument tableDoc = (TableDocument) tableManager.createDocument();
467
        
468
        tableDoc.setName(tableName);
469
        tableDoc.setStore(store);
470
        project.addDocument(tableDoc);
471
    }
472

    
473
}