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

History | View | Annotate | Download (30.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.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
                    if (layerStore != null && layerStore.getParameters().isTheSameStore(store.getParameters())) { //StringUtils.equals(layerStore.getFullName(),store.getFullName() )) {
399
                        try {
400
                            layerStore.refresh();
401
                        } catch (Exception ex) {
402
                            LOGGER.warn("Can't refresh store from layer '" + layer.getName() + "'.", ex);
403
                        }
404
                        view.getMapContext().invalidate();
405
                        break;
406
                    }
407
                }
408
            }
409
        }
410

    
411
        List<Document> tables = project.getDocuments(TableManager.TYPENAME);
412
        for (Document doc : tables) {
413
            TableDocument table = (TableDocument)doc;
414
            FeatureStore featureStore = table.getStore();
415
//            if( StringUtils.equals(featureStore.getFullName(), storeFullName)){
416
            if( featureStore.getParameters().isTheSameStore(store.getParameters())){
417
                try {
418
                    featureStore.refresh();
419
                } catch (DataException e) {
420
                    LOGGER.warn("Error refreshing table", e);
421
                }
422
            }
423
        }
424
    }
425

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

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

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

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

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

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

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

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

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

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

    
608
                    JDBCServerExplorer wsexplorer = (JDBCServerExplorer) dataManager.openServerExplorer(wsparameters.getProviderName(), wsparameters);
609
                    OnlineWorkingcopy workspace = manager.openWorkingcopy(wsexplorer);
610

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

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

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

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

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

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

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

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