Statistics
| Revision:

gvsig-projects-pool / org.gvsig.online / trunk / org.gvsig.online / org.gvsig.online.app / org.gvsig.online.app.mainplugin / src / main / java / org / gvsig / online / app / mainplugin / OnlineSwingServicesImpl.java @ 9459

History | View | Annotate | Download (30.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.online.app.mainplugin;
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.online.lib.api.OnlineLocator;
64
import org.gvsig.online.lib.api.OnlineManager;
65
import org.gvsig.online.lib.api.OnlineRuntimeException;
66
import org.gvsig.online.swing.api.OnlineSwingServices;
67
import org.gvsig.tools.ToolsLocator;
68
import org.gvsig.tools.dispose.Disposable;
69
import org.gvsig.tools.dispose.DisposeUtils;
70
import org.gvsig.tools.i18n.I18nManager;
71
import org.gvsig.tools.util.LabeledValue;
72
import org.gvsig.tools.util.LabeledValueImpl;
73
import org.slf4j.LoggerFactory;
74
import org.gvsig.online.lib.api.workingcopy.OnlineWorkingcopy;
75

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
499
//    @Override
500
//    public void addLayerToActiveView(FeatureStore store, String name) {
501
//        if(store != null && StringUtils.isNotBlank(name)){
502
//            IView view = getActiveView();
503
//            if(view != null){
504
//                ViewDocument viewDocument = view.getViewDocument();
505
//                this.addLayerToView(store, viewDocument, null, name, false);
506
//            }
507
//        }
508
//    }
509
//
510
    @Override
511
    public boolean isThereAnyActiveView() {
512
        return this.getActiveView() != null;
513
    }
514

    
515
    @Override
516
    public void addTableToProject(OnlineWorkingcopy ws, FeatureStore store, String tableName) {
517
        ApplicationManager appManager = ApplicationLocator.getApplicationManager();
518
        
519
        DocumentManager tableManager = appManager.getProjectManager().getDocumentManager(
520
                TableManager.TYPENAME
521
        );
522

    
523
        Project project = appManager.getCurrentProject();
524
        TableDocument tableDoc = (TableDocument) tableManager.createDocument();
525
        
526
        tableDoc.setName(tableName);
527
        tableDoc.setStore(store);
528
        project.addDocument(tableDoc);
529
    }
530

    
531
    @Override
532
    public Icon getFeatureStoresTreeModelIcon(TreeModel model, Object obj) {
533
        if( model instanceof FeatureStoresTreeModel ) {
534
            return ((FeatureStoresTreeModel)model).getIcon(obj);
535
        }
536
        return null;
537
    }
538
    
539
    @Override
540
    public void connectedToModel(OnlineWorkingcopy workspace, String model) {
541
        String previousProjectModelInfo = null;
542
        try {
543
            ApplicationManager application = ApplicationLocator.getApplicationManager();
544
            Project project = application.getCurrentProject();
545

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

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

    
593
            OnlineManager manager = OnlineLocator.getOnlineManager();
594
            DataManager dataManager = DALLocator.getDataManager();
595
            JsonArray array = Json.createArray(projectModelInfo);
596
            for (JsonValue value : array) {
597
                String modelName = "unkbown";
598
                String url = "unknown";
599
                try {
600
                    JsonObject item = (JsonObject) value;
601
                    modelName = item.getString("model", null);
602
                    JDBCServerExplorerParameters wsparameters = (JDBCServerExplorerParameters) Json.toObject(item.get("parameters"));
603
                    url = wsparameters.getUrl();
604

    
605
                    JDBCServerExplorer wsexplorer = (JDBCServerExplorer) dataManager.openServerExplorer(wsparameters.getProviderName(), wsparameters);
606
                    OnlineWorkingcopy workspace = manager.openWorkingcopy(wsexplorer);
607

    
608
                    workspace.registerDataModelRepository(modelName);
609
                    LOGGER.info("Conected to online model '"+modelName+"' (workspace="+url+").");
610
                } catch (Exception ex) {
611
                    LOGGER.warn("Can't register data model '" + modelName + "' of workspace '" + url + "'.", ex);
612
                    // TODO: ¿ avisar al usuario o basta con lo que saca en la linea de mensajes del MDI ?
613
                }
614
            }
615
        } catch(Exception ex) {
616
            LOGGER.warn("Can't add information of online model to project properties ("+projectModelInfo+").", ex);
617
        }
618
    }
619

    
620
    @Override
621
    public Geometry getExtentOfCurrentDocument() {
622
        ApplicationManager application = ApplicationLocator.getApplicationManager();
623
        Project project = application.getCurrentProject();
624
        
625
        ViewDocument view = (ViewDocument) project.getActiveDocument(ViewDocument.class);
626
        if( view == null ) {
627
            return null;
628
        }
629
        return view.getMapContext().getViewPort().getEnvelope().getBox2D();
630
    }
631

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

    
666
    @Override
667
    public LabeledValue<MapControl> getActiveMapControl() {
668
        try {
669
            ApplicationManager application = ApplicationLocator.getApplicationManager();
670
            Project project = application.getCurrentProject();
671

    
672
            ViewDocument viewDoc = (ViewDocument) project.getActiveDocument(ViewDocument.class);
673
            if( viewDoc == null ) {
674
                return null;
675
            }
676
            IView view = (IView) viewDoc.getMainComponent();
677
            return new LabeledValueImpl<>(viewDoc.getName(), view.getMapControl());
678
        } catch(Throwable t) {
679
            return null;
680
        }
681
    }
682

    
683
    public List<LabeledValue<MapControl>> getMapControls() {
684
        ApplicationManager application = ApplicationLocator.getManager();
685
        ProjectManager projectManager = application.getProjectManager();
686
        Project project = projectManager.getCurrentProject();
687

    
688
        List<LabeledValue<MapControl>> mapControls = new ArrayList<>();
689
        List<Document> views = project.getDocuments(ViewManager.TYPENAME);        
690
        for (Document doc : views) {
691
            ViewDocument viewDoc = (ViewDocument)doc;
692
            IView view = (IView) viewDoc.getMainComponent();
693
            mapControls.add(new LabeledValueImpl<>(viewDoc.getName(), view.getMapControl()));
694
        }
695
        return mapControls;
696
    }
697
    
698
    private static class ListDisposable<T> extends ArrayList<T> implements Disposable {
699

    
700
        @Override
701
        public void dispose() {
702
            for (T element : this) {
703
                DisposeUtils.dispose(element);
704
            }
705
            this.clear();
706
        }
707
        
708
    }
709
    
710
    public List<FeatureStore> getFeatureStores() {
711
        ListDisposable<FeatureStore> stores = new ListDisposable<>();
712
        
713
        ApplicationManager appManager = ApplicationLocator.getApplicationManager();
714
        Project project = appManager.getCurrentProject();
715
        List<Document> docs = project.getDocuments();
716
        for (Document document : docs) {
717
            if( document instanceof TableDocument ) {
718
                TableDocument table = (TableDocument) document;
719
                FeatureStore store = table.getFeatureStore();
720
                if( store!=null ) {
721
                    DisposeUtils.bind(store);
722
                    stores.add(store);
723
                }
724
            } else if( document instanceof ViewDocument ) {
725
                ViewDocument view = (ViewDocument) document;
726
                Iterator<FLayer> it = view.getMapContext().deepiterator();
727
                while (it.hasNext()) {
728
                    FLayer layer = it.next();
729
                    if( layer instanceof VectorLayer ) {
730
                        FeatureStore store = ((VectorLayer) layer).getFeatureStore();
731
                        if( store!=null ) {
732
                            DisposeUtils.bind(store);
733
                            stores.add(store);
734
                        }
735
                    }
736
                }
737
            }
738
        }  
739
        return stores;
740
    }
741
}