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

History | View | Annotate | Download (29.6 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.json.JsonArray;
17
import javax.json.JsonObject;
18
import javax.json.JsonValue;
19
import javax.swing.ComboBoxModel;
20
import javax.swing.DefaultComboBoxModel;
21
import javax.swing.DefaultListModel;
22
import javax.swing.Icon;
23
import javax.swing.ListModel;
24
import javax.swing.tree.TreeModel;
25
import org.apache.commons.lang3.StringUtils;
26
import org.gvsig.andami.PluginsLocator;
27
import org.gvsig.app.ApplicationLocator;
28
import org.gvsig.app.ApplicationManager;
29
import org.gvsig.app.project.Project;
30
import org.gvsig.app.project.ProjectManager;
31
import org.gvsig.app.project.documents.Document;
32
import org.gvsig.app.project.documents.DocumentManager;
33
import org.gvsig.app.project.documents.table.TableDocument;
34
import org.gvsig.app.project.documents.table.TableManager;
35
import org.gvsig.app.project.documents.view.ViewDocument;
36
import org.gvsig.app.project.documents.view.ViewManager;
37
import org.gvsig.app.project.documents.view.gui.IView;
38
import org.gvsig.fmap.dal.DALLocator;
39
import org.gvsig.fmap.dal.DataManager;
40
import org.gvsig.fmap.dal.exception.DataException;
41
import org.gvsig.fmap.dal.feature.FeatureStore;
42
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer;
43
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters;
44
import org.gvsig.fmap.geom.Geometry;
45
import org.gvsig.fmap.geom.aggregate.MultiCurve;
46
import org.gvsig.fmap.geom.aggregate.MultiPoint;
47
import org.gvsig.fmap.geom.aggregate.MultiSurface;
48
import org.gvsig.fmap.geom.primitive.Curve;
49
import org.gvsig.fmap.geom.primitive.Point;
50
import org.gvsig.fmap.geom.primitive.Surface;
51
import org.gvsig.fmap.mapcontext.MapContext;
52
import org.gvsig.fmap.mapcontext.MapContextLocator;
53
import org.gvsig.fmap.mapcontext.layers.FLayer;
54
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
55
import org.gvsig.fmap.mapcontext.layers.vectorial.GraphicLayer;
56
import org.gvsig.fmap.mapcontext.layers.vectorial.VectorLayer;
57
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
58
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol_v2;
59
import org.gvsig.fmap.mapcontrol.MapControl;
60
import org.gvsig.json.Json;
61
import org.gvsig.json.JsonArrayBuilder;
62
import org.gvsig.json.JsonObjectBuilder;
63
import org.gvsig.tools.ToolsLocator;
64
import org.gvsig.tools.dispose.Disposable;
65
import org.gvsig.tools.dispose.DisposeUtils;
66
import org.gvsig.tools.i18n.I18nManager;
67
import org.gvsig.tools.util.LabeledValue;
68
import org.gvsig.tools.util.LabeledValueImpl;
69
import org.gvsig.vcsgis.app.addlayer.LayersWithGroup;
70
import org.gvsig.vcsgis.lib.VCSGisLocator;
71
import org.gvsig.vcsgis.lib.VCSGisManager;
72
import org.gvsig.vcsgis.lib.VCSGisRuntimeException;
73
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspace;
74
import org.gvsig.vcsgis.swing.VCSGisSwingServices;
75
import org.slf4j.LoggerFactory;
76

    
77
/**
78
 *
79
 * @author gvSIG Team
80
 */
81
@SuppressWarnings("UseSpecificCatch")
82
public class VCSGisSwingServicesImpl implements VCSGisSwingServices {
83
    
84
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(VCSGisSwingServicesImpl.class);
85

    
86
    private static final String VCSGISMODELS_PROJECT_PROPERTY = "VCSGisModels";
87

    
88
    private Map<Integer,ISymbol> highlightedPolygonSymbols;
89
    private Map<Integer,ISymbol> highlightedLineSymbols;
90
    private Map<Integer,ISymbol> highlightedPointSymbols;
91

    
92
    @Override
93
    public TreeModel getFeatureStoresTreeModel() {
94
        return new FeatureStoresTreeModel();
95
    }
96

    
97
    @Override
98
    public ListModel getFeatureStoresListModel() {
99
        DefaultListModel<FeatureStore> model = new DefaultListModel();
100
        
101
        ApplicationManager appManager = ApplicationLocator.getApplicationManager();
102
        Project project = appManager.getCurrentProject();
103
        List<Document> tables = project.getDocuments(TableManager.TYPENAME);
104
        for (Document document : tables) {
105
            TableDocument table = (TableDocument) document;
106
            FeatureStore tableStore = table.getFeatureStore();
107
            if( tableStore!=null ) {
108
                model.addElement(tableStore);
109
            }
110
        }        
111
        return model;
112
    }
113

    
114
    @Override
115
    public void addTableToProject(VCSGisWorkspace ws, FeatureStore store) {
116
        ApplicationManager appManager = ApplicationLocator.getApplicationManager();
117
        
118
        DocumentManager tableManager = appManager.getProjectManager().getDocumentManager(
119
                TableManager.TYPENAME
120
        );
121

    
122
        Project project = appManager.getCurrentProject();
123
        TableDocument tableDoc = (TableDocument) tableManager.createDocument();
124
        
125
        tableDoc.setName(store.getName());
126
        tableDoc.setStore(store);
127
        project.addDocument(tableDoc);
128
    }
129

    
130
    @Override
131
    public ComboBoxModel getViewDocumentsComboBoxModel() {
132
        I18nManager i18n = ToolsLocator.getI18nManager();
133
        DefaultComboBoxModel<LabeledValue> model = new DefaultComboBoxModel();
134
        model.addElement(new LabeledValueImpl(i18n.getTranslation("_Select_a_view"), null));
135
        ApplicationManager appManager = ApplicationLocator.getApplicationManager();
136
        Project project = appManager.getCurrentProject();
137
        List<Document> views = project.getDocuments(ViewManager.TYPENAME);
138
        for (Document document : views) {
139
            ViewDocument view = (ViewDocument) document;
140
            model.addElement(new LabeledValueImpl(view.getName(), view));
141
        }
142
        return model;
143
    }
144

    
145
//    @Override
146
//    public void addLayerToView(FeatureStore store, LabeledValue labeledView) {
147
//        addLayerToView(store, labeledView, null, null, false);
148
//    }
149
//
150
    @Override
151
    public void addLayerToView(FeatureStore store, LabeledValue labeledView, String groupName, String name, boolean replaceLayerIfExists) {
152
        ViewDocument view;
153
        if( labeledView == null ) {
154
            view = null;
155
        } else  if (labeledView.getValue() == null){
156
            view = null;
157
        } else {
158
            view = (ViewDocument) labeledView.getValue();
159
        }
160
        addLayerToView(store, view, groupName, name, replaceLayerIfExists);
161
    }
162
        
163

    
164
    private void addLayerToView(FeatureStore store, ViewDocument view, String groupName, String name, boolean replaceLayerIfExists) {
165
        try {
166
            if( store == null ) {
167
                return;
168
            }
169
            if( view==null ) {               
170
                IView viewWin = getActiveView();
171
                if(viewWin == null){
172
                    return;
173
                }
174
                view = viewWin.getViewDocument();
175
            }
176
            String layerName = name;
177
            if(StringUtils.isBlank(layerName)){
178
                layerName = store.getName();
179
            }
180
            FLayer layer = MapContextLocator.getMapContextManager().createLayer(layerName, store);
181
            LayersWithGroup helper = new LayersWithGroup(view.getMapContext());
182
            helper.add(groupName, name, layer);
183
            helper.updateMapContext(true, replaceLayerIfExists);
184
        } catch (Exception ex) {
185
            VCSGisManager manager = VCSGisLocator.getManager();
186
            throw new VCSGisRuntimeException(
187
                    VCSGisManager.ERR_CANT_ADD_LAYER, 
188
                    manager.getErrorMessage(VCSGisManager.ERR_CANT_ADD_LAYER),
189
                    ex);
190
        }
191
    }
192
    
193
    @Override
194
    public void highlight(int mode, Geometry geom) {
195
        highlight(mode, geom, null);
196
    }
197

    
198
    @Override
199
    public void highlight(int mode, Geometry geom, FeatureStore store) {
200
        if (this.highlightedPointSymbols == null) {
201
            this.highlightedPointSymbols = new HashMap<>();
202
            this.highlightedLineSymbols = new HashMap<>();
203
            this.highlightedPolygonSymbols = new HashMap<>();
204
            try {
205
                File pluginfolder = PluginsLocator.getManager().getPlugin(this).getPluginDirectory();
206
                File folder = new File(pluginfolder, "symbols");
207
                ISymbol[] symbols = MapContextLocator.getSymbolManager().loadSymbols(folder);
208
                for (ISymbol symbol : symbols) {
209
                    if (symbol instanceof ISymbol_v2) {
210
                        String symbolid = ((ISymbol_v2) symbol).getID();
211
                        switch(symbolid) {
212
                            case "vcsgis-repository-polygon":
213
                                this.highlightedPolygonSymbols.put(HIGHLIGHT_REPOSITORY, symbol);
214
                                break;
215
                            case "vcsgis-repository-line":
216
                                this.highlightedLineSymbols.put(HIGHLIGHT_REPOSITORY, symbol);
217
                                break;
218
                            case "vcsgis-repository-point":
219
                                this.highlightedPointSymbols.put(HIGHLIGHT_REPOSITORY, symbol);
220
                                break;
221
 
222
                            case "vcsgis-workspace-polygon":
223
                                this.highlightedPolygonSymbols.put(HIGHLIGHT_WORKSPACE, symbol);
224
                                break;
225
                            case "vcsgis-workspace-line":
226
                                this.highlightedLineSymbols.put(HIGHLIGHT_WORKSPACE, symbol);
227
                                break;
228
                            case "vcsgis-workspace-point":
229
                                this.highlightedPointSymbols.put(HIGHLIGHT_WORKSPACE, symbol);
230
                                break;
231
                            
232
                            case "vcsgis-workspace-previous-polygon":
233
                                this.highlightedPolygonSymbols.put(HIGHLIGHT_WORKSPACE_PREVIOUS, symbol);
234
                                break;
235
                            case "vcsgis-workspace-previous-line":
236
                                this.highlightedLineSymbols.put(HIGHLIGHT_WORKSPACE_PREVIOUS, symbol);
237
                                break;
238
                            case "vcsgis-workspace-previous-point":
239
                                this.highlightedPointSymbols.put(HIGHLIGHT_WORKSPACE_PREVIOUS, symbol);
240
                                break;
241
                        }
242
                    }
243
                }
244
            } catch (Exception ex) {
245
            }
246
        }
247
        List<ViewDocument> viewList;
248
        if(store == null){
249
            ApplicationManager application = ApplicationLocator.getManager();
250
            ViewDocument viewdoc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME);
251
            viewList = new ArrayList<>();
252
            viewList.add(viewdoc);
253
        } else {
254
            viewList = getViewDocumentsHavingAStore(store);
255
        }
256
        for (ViewDocument viewDoc : viewList) {
257
            MapContext mapContext = viewDoc.getMapContext();
258
            GraphicLayer gl = mapContext.getGraphicsLayer();
259
            if (geom != null) {
260
                ISymbol symbol = null;
261
                if (geom instanceof Point || geom instanceof MultiPoint) {
262
                    symbol = this.highlightedPointSymbols.get(mode);
263
                } else if (geom instanceof Curve || geom instanceof MultiCurve) {
264
                    symbol = this.highlightedLineSymbols.get(mode);
265
                } else if (geom instanceof Surface || geom instanceof MultiSurface) {
266
                    symbol = this.highlightedPolygonSymbols.get(mode);
267
                }
268
                if (symbol != null) {
269
                    int symbolid = gl.getSymbolId(symbol);
270
                    if (symbolid < 0) {
271
                        gl.addSymbol(symbol);
272
                        symbolid = gl.getSymbolId(symbol);
273
                    }
274
                    gl.addGraphic("vcsgis-highlighted", geom, symbolid);
275
                }
276
            }
277
            mapContext.invalidate();
278
        }
279
    }
280

    
281
    @Override
282
    public void centerActiveViewToGeometry(Geometry geometry) {
283
        if(geometry != null){
284
            IView view = getActiveView();
285
            if(view != null){
286
                ViewDocument viewDocument = view.getViewDocument();
287
                viewDocument.center(geometry.getEnvelope());
288
            }
289
        }
290
    }
291

    
292
    @Override
293
    public void centerViewsHavingAStoreToGeometry(FeatureStore store, Geometry geometry) {
294
        if(geometry != null){
295
            List<ViewDocument> views = getViewDocumentsHavingAStore(store);
296
            for (ViewDocument view : views) {
297
                if(view != null){
298
                    view.center(geometry.getEnvelope());
299
                }
300
            }
301
        }
302
    }
303

    
304
    @Override
305
    public void zoomActiveViewToGeometry(Geometry geometry) {
306
        if(geometry != null){
307
            IView view = getActiveView();
308
            if(view != null){
309
                ViewDocument viewDocument = view.getViewDocument();
310
                viewDocument.getMapContext().getViewPort().setEnvelope(geometry.getEnvelope());
311
            }
312
        }
313
    }
314

    
315
    @Override
316
    public void zoomViewsHavingAStoreToGeometry(FeatureStore store, Geometry geometry) {
317
        if(geometry != null){
318
            List<ViewDocument> views = getViewDocumentsHavingAStore(store);
319
            for (ViewDocument view : views) {
320
                if(view != null){
321
                    view.getMapContext().getViewPort().setEnvelope(geometry.getEnvelope());
322
                }
323
            }
324
        }
325
    }
326

    
327
    private IView getActiveView() {
328
        ApplicationManager application = ApplicationLocator.getManager();
329
        IView view = (IView) application.getActiveComponent(ViewDocument.class);
330
        return view;
331
    }
332

    
333
    @Override
334
    public void cleanHighligthed() {
335
        ApplicationManager application = ApplicationLocator.getManager();
336
        ProjectManager projectManager = application.getProjectManager();
337
        Project project = projectManager.getCurrentProject();
338
        List<Document> views = project.getDocuments(ViewManager.TYPENAME);
339
        for (Document doc : views) {
340
            ViewDocument viewdoc = (ViewDocument)doc;
341
            MapContext mapContext = viewdoc.getMapContext();
342
            GraphicLayer gl = mapContext.getGraphicsLayer();
343
            //FIXME: Refrescar el mapContext solo cuando se ha borrado el graphics  
344
            if(gl.removeGraphics("vcsgis-highlighted")) {
345
                mapContext.invalidate();
346
            }
347
        }
348
    }
349
    
350
    @Override
351
    public void cleanActiveViewHighligthed() {
352
        ApplicationManager application = ApplicationLocator.getManager();
353
        ViewDocument viewdoc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME);
354
        if(viewdoc != null){
355
            MapContext mapContext = viewdoc.getMapContext();
356
            GraphicLayer gl = mapContext.getGraphicsLayer();
357
            gl.removeGraphics("vcsgis-highlighted");
358
            mapContext.invalidate();
359
        }
360
    }
361

    
362
    @Override
363
    public void refreshDocuments() {
364
        ApplicationManager application = ApplicationLocator.getManager();
365
        ProjectManager projectManager = application.getProjectManager();
366
        Project project = projectManager.getCurrentProject();
367
        
368
        List<Document> views = project.getDocuments(ViewManager.TYPENAME);
369
        for (Document doc : views) {
370
            doc.refresh();
371
        }
372
        List<Document> tables = project.getDocuments(TableManager.TYPENAME);
373
        for (Document doc : tables) {
374
            doc.refresh();
375
        }
376
    }
377
    
378
    @Override
379
    public void refreshDocument(FeatureStore store){
380
        if(store == null){
381
            return;
382
        }
383
        ApplicationManager application = ApplicationLocator.getManager();
384
        ProjectManager projectManager = application.getProjectManager();
385
        Project project = projectManager.getCurrentProject();
386
        
387
        String storeFullName = store.getFullName();
388
        List<Document> views = project.getDocuments(ViewManager.TYPENAME);
389
        for (Document doc : views) {
390
            ViewDocument view = (ViewDocument)doc;
391
            for (Iterator<FLayer> it = view.deepiterator(); it.hasNext();) {
392
                FLayer layer = it.next();
393
                if(layer instanceof FLyrVect){
394
                    FeatureStore layerStore = ((FLyrVect) layer).getFeatureStore();
395
                    if( layerStore!=null && StringUtils.equals(layerStore.getFullName(), storeFullName)){
396
                        view.getMapContext().invalidate();
397
                        break;
398
                    }
399
                }
400
            }
401
        }
402
        List<Document> tables = project.getDocuments(TableManager.TYPENAME);
403
        for (Document doc : tables) {
404
            TableDocument table = (TableDocument)doc;
405
            FeatureStore featureStore = table.getStore();
406
            if( StringUtils.equals(featureStore.getFullName(), storeFullName)){
407
                try {
408
                    featureStore.refresh();
409
                } catch (DataException e) {
410
                    LOGGER.warn("Error refreshing table", e);
411
                }
412
            }
413
        }
414
    }
415

    
416
    @Override
417
    public void refreshDocument(Set<FeatureStore> stores){
418
        if(stores == null || stores.isEmpty()){
419
            return;
420
        }
421
        
422
        ApplicationManager application = ApplicationLocator.getManager();
423
        ProjectManager projectManager = application.getProjectManager();
424
        Project project = projectManager.getCurrentProject();
425
        
426
        Set<ViewDocument> viewsToRefresh = new HashSet<>();
427
        for (FeatureStore store : stores) {
428
            String storeFullName = store.getFullName();
429
            List<Document> views = project.getDocuments(ViewManager.TYPENAME);
430
            for (Document doc : views) {
431
                ViewDocument view = (ViewDocument)doc;
432
                for (Iterator<FLayer> it = view.deepiterator(); it.hasNext();) {
433
                    FLayer layer = it.next();
434
                    if(layer instanceof FLyrVect){
435
                        FeatureStore layerStore = ((FLyrVect) layer).getFeatureStore();
436
                        if( layerStore!=null && StringUtils.equals(layerStore.getFullName(), storeFullName)){
437
                            viewsToRefresh.add(view);
438
                            break;
439
                        }
440
                    }
441
                }
442
            }
443

    
444
            List<Document> tables = project.getDocuments(TableManager.TYPENAME);
445
            for (Document doc : tables) {
446
                TableDocument table = (TableDocument)doc;
447
                FeatureStore featureStore = table.getStore();
448
                if( StringUtils.equals(featureStore.getFullName(), storeFullName)){
449
                    try {
450
                        featureStore.refresh();
451
                    } catch (DataException e) {
452
                        LOGGER.warn("Error refreshing table", e);
453
                    }
454
                }
455
            }
456
        }
457
        
458
        for (ViewDocument viewDocument : viewsToRefresh) {
459
            viewDocument.getMapContext().invalidate();
460
        }
461
        
462
    }
463

    
464
    
465
    public List<ViewDocument> getViewDocumentsHavingAStore(FeatureStore store){
466
        if(store == null){
467
            return null;
468
        }
469
        List<ViewDocument> viewList = new ArrayList<>();
470
        
471
        String storeFullName = store.getFullName();
472
        ApplicationManager application = ApplicationLocator.getManager();
473
        ProjectManager projectManager = application.getProjectManager();
474
        Project project = projectManager.getCurrentProject();
475
        List<Document> views = project.getDocuments(ViewManager.TYPENAME);
476
        for (Document doc : views) {
477
            ViewDocument view = (ViewDocument) doc;
478
            for (Iterator<FLayer> it = view.deepiterator(); it.hasNext();) {
479
                FLayer layer = it.next();
480
                if (layer instanceof FLyrVect) {
481
                    FeatureStore layerStore = ((FLyrVect) layer).getFeatureStore();
482
                    if ( layerStore!=null && StringUtils.equals(layerStore.getFullName(),store.getFullName() )) {
483
                        viewList.add(view);
484
                        break;
485
                    }
486
                }
487
            }
488
        }
489
        return viewList;
490
    }
491

    
492
//    @Override
493
//    public void addLayerToActiveView(FeatureStore store, String name) {
494
//        if(store != null && StringUtils.isNotBlank(name)){
495
//            IView view = getActiveView();
496
//            if(view != null){
497
//                ViewDocument viewDocument = view.getViewDocument();
498
//                this.addLayerToView(store, viewDocument, null, name, false);
499
//            }
500
//        }
501
//    }
502
//
503
    @Override
504
    public boolean isThereAnyActiveView() {
505
        return this.getActiveView() != null;
506
    }
507

    
508
    @Override
509
    public void addTableToProject(VCSGisWorkspace ws, FeatureStore store, String tableName) {
510
        ApplicationManager appManager = ApplicationLocator.getApplicationManager();
511
        
512
        DocumentManager tableManager = appManager.getProjectManager().getDocumentManager(
513
                TableManager.TYPENAME
514
        );
515

    
516
        Project project = appManager.getCurrentProject();
517
        TableDocument tableDoc = (TableDocument) tableManager.createDocument();
518
        
519
        tableDoc.setName(tableName);
520
        tableDoc.setStore(store);
521
        project.addDocument(tableDoc);
522
    }
523

    
524
    @Override
525
    public Icon getFeatureStoresTreeModelIcon(TreeModel model, Object obj) {
526
        if( model instanceof FeatureStoresTreeModel ) {
527
            return ((FeatureStoresTreeModel)model).getIcon(obj);
528
        }
529
        return null;
530
    }
531
    
532
    @Override
533
    public void connectedToModel(VCSGisWorkspace workspace, String model) {
534
        String previousProjectModelInfo = null;
535
        try {
536
            ApplicationManager application = ApplicationLocator.getApplicationManager();
537
            Project project = application.getCurrentProject();
538

    
539
            Map<String,JsonObject> models = new HashMap<>();
540
            previousProjectModelInfo = (String) project.getProperty(VCSGISMODELS_PROJECT_PROPERTY);
541
            if( previousProjectModelInfo != null ) {
542
                JsonArray array = Json.createArray(previousProjectModelInfo);
543
                for (JsonValue value : array) {
544
                    JsonObject item = (JsonObject) value;
545
                    String modelName = item.getString("model", null);
546
                    JDBCServerExplorerParameters wsparameters = (JDBCServerExplorerParameters) Json.toObject(item.getJsonObject("parameters"));
547
                    String url = wsparameters.getUrl();
548
                    String key = modelName; //+ "("+ url + ")";
549
                    models.put(key, item);
550
                }
551
            }
552
            JDBCServerExplorerParameters parameters = workspace.getExplorerParameters();
553
            String url = parameters.getUrl();
554
            String key = model;// + "("+ url + ")";
555
            JsonObjectBuilder item = Json.createObjectBuilder();
556
            item.add("model", model);
557
            item.add("parameters", parameters);
558
            models.put(key, item.build());
559
            JsonArrayBuilder array = Json.createArrayBuilder();
560
            for (JsonObject n : models.values()) {
561
                array.add(n);
562
            }
563
            String s = array.build().toString();
564
            project.setProperty(VCSGISMODELS_PROJECT_PROPERTY, s);
565
            LOGGER.info("Added information of vcsgis model to project properties.");
566
            LOGGER.debug(s);
567
        } catch(Exception ex) {
568
            LOGGER.warn("Can't add information of vcsgis model to project properties. Previous model info='"+previousProjectModelInfo+"'", ex);
569
        }
570
    }
571

    
572
    public void projectLoaded(Project project) {
573
        String projectModelInfo = null;
574
        try {
575
            projectModelInfo = (String) project.getProperty(VCSGISMODELS_PROJECT_PROPERTY);
576
            if (projectModelInfo == null) {
577
                LOGGER.info("Project loaded without information of vcsgis models.");
578
                return;
579
            }
580
            // TODO:    El projecto se guardo cuando estaba conectado a uno o mas
581
            //          modelos de datos... 
582
            //          ¿ Preguntamos al usuario si nos reconectamos automaticamente
583
            //          o nos reconectamos sin decirle nada ?
584
            //          Si preguntamos que recuerde la respuesta.
585

    
586
            VCSGisManager manager = VCSGisLocator.getVCSGisManager();
587
            DataManager dataManager = DALLocator.getDataManager();
588
            JsonArray array = Json.createArray(projectModelInfo);
589
            for (JsonValue value : array) {
590
                String modelName = "unkbown";
591
                String url = "unknown";
592
                try {
593
                    JsonObject item = (JsonObject) value;
594
                    modelName = item.getString("model", null);
595
                    JDBCServerExplorerParameters wsparameters = (JDBCServerExplorerParameters) Json.toObject(item.get("parameters"));
596
                    url = wsparameters.getUrl();
597

    
598
                    JDBCServerExplorer wsexplorer = (JDBCServerExplorer) dataManager.openServerExplorer(wsparameters.getProviderName(), wsparameters);
599
                    VCSGisWorkspace workspace = manager.openWorkspace(wsexplorer);
600

    
601
                    workspace.registerDataModelRepository(modelName);
602
                    LOGGER.info("Conected to vcsgis model '"+modelName+"' (workspace="+url+").");
603
                } catch (Exception ex) {
604
                    LOGGER.warn("Can't register data model '" + modelName + "' of workspace '" + url + "'.", ex);
605
                    // TODO: ¿ avisar al usuario o basta con lo que saca en la linea de mensajes del MDI ?
606
                }
607
            }
608
        } catch(Exception ex) {
609
            LOGGER.warn("Can't add information of vcsgis model to project properties ("+projectModelInfo+").", ex);
610
        }
611
    }
612

    
613
    @Override
614
    public Geometry getExtentOfCurrentDocument() {
615
        ApplicationManager application = ApplicationLocator.getApplicationManager();
616
        Project project = application.getCurrentProject();
617
        
618
        ViewDocument view = (ViewDocument) project.getActiveDocument(ViewDocument.class);
619
        if( view == null ) {
620
            return null;
621
        }
622
        return view.getMapContext().getViewPort().getEnvelope().getBox2D();
623
    }
624

    
625
    @Override
626
    public boolean isInSomeDocument(FeatureStore store) {
627
        if(store == null){
628
            return false;
629
        }
630
        ApplicationManager application = ApplicationLocator.getManager();
631
        ProjectManager projectManager = application.getProjectManager();
632
        Project project = projectManager.getCurrentProject();
633
        
634
        String storeFullName = store.getFullName();
635
        List<Document> views = project.getDocuments(ViewManager.TYPENAME);
636
        for (Document doc : views) {
637
            ViewDocument view = (ViewDocument)doc;
638
            for (Iterator<FLayer> it = view.deepiterator(); it.hasNext();) {
639
                FLayer layer = it.next();
640
                if(layer instanceof FLyrVect){
641
                    FeatureStore layerStore = ((FLyrVect) layer).getFeatureStore();
642
                    if( layerStore!=null && StringUtils.equals(layerStore.getFullName(), storeFullName)){
643
                        return true;
644
                    }
645
                }
646
            }
647
        }
648
        List<Document> tables = project.getDocuments(TableManager.TYPENAME);
649
        for (Document doc : tables) {
650
            TableDocument table = (TableDocument)doc;
651
            FeatureStore featureStore = table.getStore();
652
            if( StringUtils.equals(featureStore.getFullName(), storeFullName)){
653
                return true;
654
            }
655
        }
656
        return false;
657
    }
658

    
659
    @Override
660
    public MapControl getActiveMapControl() {
661
        try {
662
            ApplicationManager application = ApplicationLocator.getApplicationManager();
663
            Project project = application.getCurrentProject();
664

    
665
            ViewDocument viewDoc = (ViewDocument) project.getActiveDocument(ViewDocument.class);
666
            if( viewDoc == null ) {
667
                return null;
668
            }
669
            IView view = (IView) viewDoc.getMainComponent();
670
            return view.getMapControl();
671
        } catch(Throwable t) {
672
            return null;
673
        }
674
    }
675

    
676
    private static class ListDisposable<T> extends ArrayList<T> implements Disposable {
677

    
678
        @Override
679
        public void dispose() {
680
            for (T element : this) {
681
                DisposeUtils.dispose(element);
682
            }
683
            this.clear();
684
        }
685
        
686
    }
687
    
688
    public List<FeatureStore> getFeatureStores() {
689
        ListDisposable<FeatureStore> stores = new ListDisposable<>();
690
        
691
        ApplicationManager appManager = ApplicationLocator.getApplicationManager();
692
        Project project = appManager.getCurrentProject();
693
        List<Document> docs = project.getDocuments();
694
        for (Document document : docs) {
695
            if( document instanceof TableDocument ) {
696
                TableDocument table = (TableDocument) document;
697
                FeatureStore store = table.getFeatureStore();
698
                if( store!=null ) {
699
                    DisposeUtils.bind(store);
700
                    stores.add(store);
701
                }
702
            } else if( document instanceof ViewDocument ) {
703
                ViewDocument view = (ViewDocument) document;
704
                Iterator<FLayer> it = view.getMapContext().deepiterator();
705
                while (it.hasNext()) {
706
                    FLayer layer = it.next();
707
                    if( layer instanceof VectorLayer ) {
708
                        FeatureStore store = ((VectorLayer) layer).getFeatureStore();
709
                        if( store!=null ) {
710
                            DisposeUtils.bind(store);
711
                            stores.add(store);
712
                        }
713
                    }
714
                }
715
            }
716
        }  
717
        return stores;
718
    }
719
}