Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / project / symboltables / ProjectSymbolTable.java @ 46333

History | View | Annotate | Download (29.7 KB)

1
package org.gvsig.app.project.symboltables;
2

    
3
import java.io.File;
4
import java.io.FileInputStream;
5
import java.util.HashMap;
6
import java.util.Iterator;
7
import java.util.Map;
8
import java.util.Properties;
9
import org.apache.commons.io.IOUtils;
10
import org.apache.commons.lang3.Range;
11
import org.apache.commons.lang3.StringUtils;
12
import org.cresques.cts.IProjection;
13
import org.gvsig.andami.PluginsLocator;
14
import org.gvsig.andami.PluginsManager;
15
import org.gvsig.app.ApplicationLocator;
16
import org.gvsig.app.ApplicationManager;
17
import org.gvsig.app.project.Project;
18
import org.gvsig.app.project.ProjectManager;
19
import org.gvsig.app.project.documents.Document;
20
import org.gvsig.app.project.documents.view.ViewDocument;
21
import org.gvsig.app.project.documents.view.ViewManager;
22
import org.gvsig.expressionevaluator.Expression;
23
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
24
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager;
25
import org.gvsig.expressionevaluator.ExpressionRuntimeException;
26
import org.gvsig.expressionevaluator.Interpreter;
27
import org.gvsig.expressionevaluator.spi.AbstractFunction;
28
import org.gvsig.expressionevaluator.spi.AbstractGeometryFunction;
29
import org.gvsig.expressionevaluator.spi.AbstractSymbolTable;
30
import org.gvsig.fmap.crs.CRSFactory;
31
import org.gvsig.fmap.dal.DALLocator;
32
import org.gvsig.fmap.dal.DataManager;
33
import org.gvsig.fmap.dal.HasDataStore;
34
import org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable;
35
import org.gvsig.fmap.dal.feature.Feature;
36
import org.gvsig.fmap.dal.feature.FeatureSet;
37
import org.gvsig.fmap.dal.feature.FeatureStore;
38
import org.gvsig.fmap.geom.Geometry;
39
import org.gvsig.fmap.geom.primitive.Point;
40
import org.gvsig.fmap.mapcontext.MapContext;
41
import org.gvsig.fmap.mapcontext.layers.FLayer;
42
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
43
import org.gvsig.fmap.mapcontrol.AreaAndPerimeterCalculator;
44
import org.gvsig.temporarystorage.TemporaryStorageGroup;
45
import org.gvsig.temporarystorage.TemporaryStorageLocator;
46
import org.gvsig.temporarystorage.TemporaryStorageManager;
47

    
48
/**
49
 *
50
 * @author jjdelcerro
51
 */
52
@SuppressWarnings("UseSpecificCatch")
53
public class ProjectSymbolTable extends AbstractSymbolTable {
54
    public static final String PROJECT_GROUP = "Project";        
55

    
56
    public static final String AREA_NAME = "AREA";        
57
    public static final String PERIMETER_NAME = "PERIMETER";
58
    public static final String TABLE_NAME = "TABLE";
59
//    public static final String FETCH_FIRST_NAME = "FETCH_FIRST";
60
    public static final String FETCH_FIRST_SELECTED_NAME = "FETCH_FIRST_SELECTED";
61
//    public static final String FETCH_NAME = "FETCH";
62
    public static final String APPLICATION_NAME = "APPLICATION";
63
        
64
    static final String NAME = "Project";
65
    
66
    private static final String TABLE_DOCUMENT_TYPENAME = "project.document.table";
67
    
68
    private final ProjectValue currentProject = new ProjectValue();
69
    private final CurrentViewValue currentView = new CurrentViewValue();
70
    private final CurrentViewEnvelopeValue currentViewEnvelope = new CurrentViewEnvelopeValue();
71
    private final PropertiesValue propertiesFiles = new PropertiesValue();
72
    private Map<String,Object> appvars;
73
    
74
    private abstract class CachedValue<T> {
75

    
76
        T value = null;
77
        long lastAccess = 0;
78

    
79
        protected abstract void reload();
80

    
81
        public boolean isExpired() {
82
            long now = System.currentTimeMillis();
83
            return now - lastAccess > 3000;
84
        }
85

    
86
        public T get() {
87
            if (isExpired()) {
88
                reload();
89
            }
90
            lastAccess = System.currentTimeMillis();
91
            return value;
92
        }
93
    }
94

    
95
    private class ProjectValue extends CachedValue<Project> {
96

    
97
        @Override
98
        protected void reload() {
99
            value = ProjectManager.getInstance().getCurrentProject();
100
        }
101

    
102
    }
103

    
104
    private class CurrentViewValue extends CachedValue<ViewDocument> {
105

    
106
        @Override
107
        protected void reload() {
108
            ApplicationManager application = ApplicationLocator.getManager();
109
            ViewDocument viewdoc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME);
110
            value = viewdoc;
111
        }
112

    
113
    }
114

    
115
    private class CurrentViewEnvelopeValue extends CachedValue<Geometry> {
116

    
117
        @Override
118
        protected void reload() {
119
            ApplicationManager application = ApplicationLocator.getManager();
120
            ViewDocument viewdoc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME);
121
            if( viewdoc == null ) {
122
                value = null;
123
                return;
124
            }
125
            value = viewdoc.getMapContext().getViewPort().getEnvelope().getGeometry();
126
        }
127

    
128
    }
129

    
130
    private class PropertiesValue extends CachedValue<Map<File, Properties>> {
131

    
132
        @Override
133
        protected void reload() {
134
            value = new HashMap<>();
135
        }
136
    }
137

    
138
    private class CurrentProjectFunction extends AbstractFunction {
139

    
140
        public CurrentProjectFunction() {
141
            super(
142
                    PROJECT_GROUP,
143
                    "project",
144
                    Range.is(0),
145
                    "Access to the current project loaded in gvSIG desktop.\n",
146
                    "project()",
147
                    null,
148
                    "Project"
149
            );
150
        }
151

    
152
        @Override
153
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
154
            return currentProject.get();
155
        }
156

    
157
    }
158

    
159
    private class ApplicationFunction extends AbstractFunction {
160

    
161
        public ApplicationFunction() {
162
            super(
163
                    PROJECT_GROUP,
164
                    APPLICATION_NAME,
165
                    Range.is(0),
166
                    "Access to the Application object.\n",
167
                    APPLICATION_NAME+"()",
168
                    null,
169
                    "ApplicationManager"
170
            );
171
        }
172

    
173
        @Override
174
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
175
            return ApplicationLocator.getApplicationManager();
176
        }
177

    
178
    }
179

    
180
    private class TabbleFunction extends AbstractFunction {
181

    
182
        public TabbleFunction() {
183
            super(PROJECT_GROUP,
184
                    TABLE_NAME,
185
                    Range.is(2), // Range.between(1, 2),
186
                    "Return the data store associated with a layer.", // or a table.\n" +
187
//                       "If receive a single parameter, it will correspond to " + 
188
//                       "the name of a table-type document, and it will return " + 
189
//                       "your data store. If you receive two parameters, you expect " +
190
//                       "to be the name of a view and a layer of it, returning the " +
191
//                       "data store of the layer.",
192
                    TABLE_NAME+"({{view}}, layer)",
193
                    new String[]{
194
                        "view - String value with the name of a view, if view is null use current View.",
195
                        "layer - String value with the name of a layer in the indicated view"
196
//                        "table - String value with the name of document table"
197
                    },
198
                    "DataStore"
199
            );
200
        }
201

    
202
        @Override
203
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
204
            Project project = currentProject.get();
205
            switch (args.length) {
206
                case 1:
207
                    String tableName = getStr(args, 0);
208
                    Document document = project.getDocument(tableName, TABLE_DOCUMENT_TYPENAME);
209
                    if (document == null) {
210
                        throw new ExpressionRuntimeException("Problems calling '"+TABLE_NAME+"' function, can't locate table document '"+tableName+"'.");
211
//                        return null;
212
                    }
213
                    if( document instanceof HasDataStore ) {
214
                        return ((HasDataStore) document).getDataStore();
215
                    }
216
                    throw new ExpressionRuntimeException("Problems calling '"+TABLE_NAME+"' function, can't get store from table document '"+tableName+"'.");
217
//                    return null;//                    return null;
218

    
219
                case 2:
220
                    ViewDocument view;
221
                    String viewName = getStr(args, 0);
222
                    if( StringUtils.isBlank(viewName) ) {
223
                        view = (ViewDocument) project.getActiveDocument(ViewManager.TYPENAME);
224
                        if (view == null) {
225
                            throw new ExpressionRuntimeException("Problems calling '"+TABLE_NAME+"' function, can't locate active View.");
226
//                            return null;
227
                        }
228
                    } else {
229
                        view = (ViewDocument) project.getDocument(viewName, ViewManager.TYPENAME);
230
                        if (view == null) {
231
                            throw new ExpressionRuntimeException("Problems calling '"+TABLE_NAME+"' function, can't locate View '"+viewName+"'.");
232
//                            return null;
233
                        }
234
                    }
235
                    String layerName = getStr(args, 1);
236
                    FLayer layer = view.getLayer(layerName);
237
                    if( layer == null ) {
238
                        throw new ExpressionRuntimeException("Problems calling '"+TABLE_NAME+"' function, can't locate layer '"+layerName+"' in View '"+viewName+"'.");
239
                    }
240
                    if( layer instanceof HasDataStore ) {
241
                        return ((HasDataStore)layer).getDataStore();
242
                    }
243
                    throw new ExpressionRuntimeException("Problems calling '"+TABLE_NAME+"' function, can't get the store from layer '"+layerName+"' in View '"+viewName+"'.");
244
//                    return null;
245
            }
246
            return null;
247
        }
248
        
249
    }
250
    
251
//    private class FetchFunction extends AbstractFunction {
252
//
253
//        public FetchFunction() {
254
//            super(
255
//                    PROJECT_GROUP,
256
//                    FETCH_NAME,
257
//                    Range.between(2, 4),
258
//                    "Access to the features of a table and retuen a list with the values.",
259
//                    FETCH_NAME+"({{value}}, store, where, order)",
260
//                    new String[]{
261
//                        "value - value to retrieve from the store, usually an expression in the columns of this are involved.",
262
//                        "store - data store from which values will be collected",
263
//                        "where - Optional. String value with a filter expression",
264
//                        "order - Optional. String value with the order. must be a string with the names of separate fields with commas"
265
//                    },
266
//                    "Object"
267
//            );
268
//        }
269
//
270
//        @Override
271
//        public Object call(Interpreter interpreter, Object[] args) throws Exception {
272
//            Object value;
273
//            String where = null;
274
//            String order = null;
275
//            String expression_s = getStr(args, 0);
276
//            FeatureStore store = (FeatureStore) getObject(args, 1);
277
//            switch (args.length) {
278
//                case 3:
279
//                    where = getStr(args, 2);
280
//                    break;
281
//                case 4:
282
//                    where = getStr(args, 2);
283
//                    order = getStr(args, 3);
284
//                    break;
285
//            }
286
//            try {
287
//                List<Feature> features;
288
//                if (where == null && order == null) {
289
//                    features = store.getFeatures();
290
//                } else {
291
//                    FeatureQuery query = store.createFeatureQuery();
292
//                    if (where != null) {
293
//                        query.addFilter(where);
294
//                    }
295
//                    if (order != null) {
296
//                        query.getOrder().add(order);
297
//                    }
298
//                    query.retrievesAllAttributes();
299
//                    features = store.getFeatures(query);
300
//                }
301
//                UnmodifiableBasicList<Object> list = new ListOfFeaturesWrapper(
302
//                        features,
303
//                        interpreter.getSymbolTable(), 
304
//                        expression_s
305
//                );
306
//                return list;
307
//            } catch (Exception ex) {
308
//                throw new ExpressionRuntimeException("Problems calling '"+FETCH_NAME+"' function", ex);
309
//            }
310
//        }
311
//    }
312
//
313
//    private class FetchFirstFunction extends AbstractFunction {
314
//
315
//        public FetchFirstFunction() {
316
//            super(
317
//                    PROJECT_GROUP,
318
//                    FETCH_FIRST_NAME,
319
//                    Range.between(2, 4),
320
//                    "Access to the first feature of the layer, and "
321
//                    + "return the value of the attribute.",
322
//                    FETCH_FIRST_NAME+"({{value}}, store, where, order)",
323
//                    new String[]{
324
//                        "value - value to retrieve from the store, usually an expression in the columns of this are involved.",
325
//                        "store - data store from which values will be collected",
326
//                        "where - Optional. String value with a filter expression",
327
//                        "order - Optional. String value with the order. must be a string with the names of separate fields with commas"
328
//                    },
329
//                    "Object"
330
//            );
331
//        }
332
//
333
//        @Override
334
//        public Object call(Interpreter interpreter, Object[] args) throws Exception {
335
//            Object value;
336
//            String where = null;
337
//            String order = null;
338
//            String expression_s = getStr(args, 0);
339
//            FeatureStore store = (FeatureStore) getObject(args, 1);
340
//            if( store == null ) {
341
//                throw new ExpressionRuntimeException("Problems calling '"+FETCH_FIRST_NAME+"' function, the store is null.");
342
//            }
343
//            switch (args.length) {
344
//                case 3:
345
//                    where = getStr(args, 2);
346
//                    break;
347
//                case 4:
348
//                    where = getStr(args, 2);
349
//                    order = getStr(args, 3);
350
//                    break;
351
//            }
352
//            try {
353
//                FeatureSet set;
354
//                if (where == null && order == null) {
355
//                    set = store.getFeatureSet();
356
//                } else {
357
//                    FeatureQuery query = store.createFeatureQuery();
358
//                    if (where != null) {
359
//                        query.addFilter(where);
360
//                    }
361
//                    if (order != null) {
362
//                        query.getOrder().add(order);
363
//                    }
364
//                    query.retrievesAllAttributes();
365
//                    set = store.getFeatureSet(query);
366
//                }
367
//                Feature feature = set.first();
368
//                if (feature == null) {
369
//                    return null;
370
//                }
371
//                DataManager dataManager = DALLocator.getDataManager();
372
//                FeatureSymbolTable symbolTable = dataManager.createFeatureSymbolTable();
373
//                symbolTable.setFeature(feature);
374
//
375
//                ExpressionEvaluatorManager expManager = ExpressionEvaluatorLocator.getManager();
376
//                Expression expression = expManager.createExpression();
377
//                expression.setPhrase(expression_s);
378
//                value = expression.execute(symbolTable.createParent());
379
//                
380
//                return value;
381
//            } catch (Exception ex) {
382
//                throw new ExpressionRuntimeException("Problems calling '"+FETCH_FIRST_NAME+"' function", ex);
383
//            }
384
//        }
385
//    }
386

    
387
    private class FetchFirstSelectedFunction extends AbstractFunction {
388

    
389
        public FetchFirstSelectedFunction() {
390
            super(
391
                    PROJECT_GROUP,
392
                    FETCH_FIRST_SELECTED_NAME,
393
                    Range.is(2),
394
                    "Access to the first feature of the selection.",
395
                    FETCH_FIRST_SELECTED_NAME+"({{value}}, store)",
396
                    new String[]{
397
                        "value - value to retrieve from the store, usually an expression in the columns of this are involved.",
398
                        "store - data store from which values will be collected"
399
                    },
400
                    "Object"
401
            );
402
        }
403

    
404
        @Override
405
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
406
            String expression_s = getStr(args, 0);
407
            FeatureStore store = (FeatureStore) getObject(args, 1);
408
            try {
409
                FeatureSet set = store.getFeatureSelection();
410
                Feature feature = set.first();
411
                if (feature == null) {
412
                    return null;
413
                }
414
                DataManager dataManager = DALLocator.getDataManager();
415
                FeatureSymbolTable symbolTable = dataManager.createFeatureSymbolTable();
416
                symbolTable.setFeature(feature);
417

    
418
                ExpressionEvaluatorManager expManager = ExpressionEvaluatorLocator.getManager();
419
                Expression expression = expManager.createExpression();
420
                expression.setPhrase(expression_s);
421
                Object value = expression.execute(symbolTable.createParent());
422
                
423
                return value;
424
            } catch (Exception ex) {
425
                throw new ExpressionRuntimeException("Problems calling '"+FETCH_FIRST_SELECTED_NAME+"' function", ex);
426
            }
427
        }
428
    }
429

    
430
    private class ViewFunction extends AbstractFunction {
431

    
432
        public ViewFunction() {
433
            super(
434
                    PROJECT_GROUP,
435
                    "view",
436
                    Range.is(1),
437
                    "Access to the indicated view",
438
                    "view({{viewName}})",
439
                    new String[]{
440
                        "view - String value with the name of a view"
441
                    },
442
                    "DocumentView"
443
            );
444
        }
445

    
446
        @Override
447
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
448
            String viewName = getStr(args, 0);
449
            Project project = currentProject.get();
450
            ViewDocument view = (ViewDocument) project.getDocument(viewName, ViewManager.TYPENAME);
451
            return view;
452
        }
453

    
454
    }
455

    
456
    private class ViewBBoxFunction extends AbstractFunction {
457

    
458
        public ViewBBoxFunction() {
459
            super(
460
                    PROJECT_GROUP,
461
                    "viewbbox",
462
                    Range.is(0),
463
                    "Return the BBox of the active view.",
464
                    "viewbbox()",
465
                    null,
466
                    "Geometry"
467
            );
468
        }
469

    
470
        @Override
471
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
472
            return currentViewEnvelope.get();
473
        }
474

    
475
    }
476

    
477

    
478
    private class SavedPointFunction extends AbstractFunction {
479

    
480
        public SavedPointFunction() {
481
            super(
482
                    PROJECT_GROUP,
483
                    "savedpoint",
484
                    Range.is(1),
485
                    "Return the value of the saved point with the name indicated.\n"
486
                            + "If the named point do not exists return null.",
487
                    "savedpoint({{name}})",
488
                    new String[]{
489
                        "name - String value with the name of the point"
490
                    },
491
                    "Geometry"
492
            );
493
        }
494

    
495
        @Override
496
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
497
            TemporaryStorageManager manager = TemporaryStorageLocator.getTemporaryStorageManager();
498
            TemporaryStorageGroup storage = manager.create("Points",Point.class);
499
            Geometry value = (Geometry) storage.get(getStr(args, 0));
500
            return value;
501
        }
502
                
503
                }
504

    
505
    private  class AreaFunction extends AbstractGeometryFunction {
506
        
507
        public AreaFunction() {
508
            super(
509
                    PROJECT_GROUP,
510
                    AREA_NAME,
511
                    Range.between(2,3),
512
                    "Calculate the area of the geometry in the indicated units",
513
                    AREA_NAME+"({{geometry}}, 'm?')",
514
                    new String[]{
515
                        "geometry - Geometry",
516
                        "Projection - Optional. Projection of the geometry or a string with the projection name",
517
                        "units - String value with the name the units to use"
518
                    },
519
                    "Double"
520
            );
521
        }
522

    
523
        @Override
524
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
525
            int units = 0;
526
            IProjection proj = null;
527
            Geometry geom = getGeom(args, 0);
528
            switch( args.length ) {
529
                case 2:
530
                    units = getUnitsArea(args, 1);
531
                    break;
532
                case 3:
533
                    proj = getProjection(args, 1);
534
                    units = getUnitsArea(args, 2);
535
                    break;
536
            }
537
            if (proj == null) {
538
                proj = geom.getProjection();
539
            }
540
            AreaAndPerimeterCalculator calculator = new AreaAndPerimeterCalculator();
541
            double area = calculator.area(geom, proj, units);
542
            return area;
543
        }
544
    }
545

    
546
    private class PerimeterFunction extends AbstractGeometryFunction {
547

    
548
        public PerimeterFunction() {
549
            super(
550
                    PROJECT_GROUP,
551
                    PERIMETER_NAME,
552
                    Range.between(2,3),
553
                    "Calculate the perimeter of the geometry in the indicated units",
554
                    PERIMETER_NAME+"({{geometry}}, 'm')",
555
                    new String[]{
556
                        "geometry - Geometry",
557
                        "Projection - Optional. Projection of the geometry or a string with the projection name",
558
                        "units - String value with the name the units to use"
559
                    },
560
                    "Double"
561
            );
562
        }
563

    
564
        @Override
565
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
566
            int units = 0;
567
            IProjection proj = null;
568
            Geometry geom = getGeom(args, 0);
569
            switch( args.length ) {
570
                case 2:
571
                    units = getUnitsDistance(args, 1);
572
                    break;
573
                case 3:
574
                    proj = getProjection(args, 1);
575
                    units = getUnitsDistance(args, 2);
576
                    break;
577
            }
578
            if (proj == null) {
579
                proj = geom.getProjection();
580
            }
581
            AreaAndPerimeterCalculator calculator = new AreaAndPerimeterCalculator();
582
            double perimeter = calculator.perimeter(geom, proj, units);
583
            return perimeter;
584
        }
585
    }
586

    
587
    private class PropertyFunction extends AbstractFunction {
588

    
589
        public PropertyFunction() {
590
            super(
591
                    PROJECT_GROUP,
592
                    "property",
593
                    Range.between(2, 3),
594
                    "Access to a property value in a properties file. If the"
595
                    + "indicated filename is not absolute, access it relative"
596
                    + "to the project.",
597
                    "property({{filename}}, name, defaultValue)",
598
                    new String[]{
599
                        "filename - String value with the name of the properties file",
600
                        "name - String value with the name of the property to retrieve from the file",
601
                        "defaultValue - Optional. Default value if can't access the file or the property",},
602
                    "String"
603
            );
604
        }
605

    
606
        @Override
607
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
608
            String filename = getStr(args, 0);
609
            String name = getStr(args, 1);
610
            String defaultValue = null;
611
            if (args.length == 3) {
612
                defaultValue = getStr(args, 2);
613
            }
614

    
615
            File file = new File(filename);
616
            if (!file.isAbsolute()) {
617
                Project project = currentProject.get();
618
                if (project.getFile() == null) {
619
                    return defaultValue;
620
                }
621
                file = new File(project.getFile().getParent(), filename);
622
            }
623
            Map<File, Properties> x = propertiesFiles.get();
624
            Properties properties = x.get(file);
625
            if (properties == null) {
626
                properties = new Properties();
627
                FileInputStream in = null;
628
                try {
629
                    in = new FileInputStream(file);
630
                    properties.load(in);
631
                } catch (Exception ex) {
632
                    return defaultValue;
633
                } finally {
634
                    IOUtils.closeQuietly(in);
635
                }
636
                x.put(file, properties);
637
            }
638
            String value = properties.getProperty(name, defaultValue);
639
            return value;
640
        }
641

    
642
    }
643

    
644
    @SuppressWarnings("OverridableMethodCallInConstructor")
645
    public ProjectSymbolTable() {
646
        super(NAME);
647
        this.addFunction(new CurrentProjectFunction());
648
        this.addFunction(new TabbleFunction());
649
//        this.addFunction(new FetchFunction());
650
//        this.addFunction(new FetchFirstFunction());
651
        this.addFunction(new FetchFirstSelectedFunction());
652
        this.addFunction(new ViewFunction());
653
        this.addFunction(new ViewBBoxFunction());
654
        this.addFunction(new PropertyFunction());
655
        this.addFunction(new AreaFunction());
656
        this.addFunction(new PerimeterFunction());
657
        this.addFunction(new SavedPointFunction());
658
        this.addFunction(new ApplicationFunction());
659
    }
660

    
661
    private MapContext getMapContext(Feature feature) {
662
        FeatureStore store = feature.getStore();
663
        Project project = ProjectManager.getInstance().getCurrentProject();
664
        project.getDocuments(ViewManager.TYPENAME);
665
        for (Document document : project.getDocuments(ViewManager.TYPENAME)) {
666
            ViewDocument view = (ViewDocument) document;
667
            MapContext mapContext = view.getMapContext();
668
            FLayer layer = getLayer(mapContext, store);
669
            if (layer != null) {
670
                return mapContext;
671
            }
672
        }
673
        return null;
674
    }
675

    
676
    private FLayer getLayer(MapContext mapContext, FeatureStore store) {
677
        Iterator<FLayer> it = mapContext.deepiterator();
678
        while (it.hasNext()) {
679
            FLayer layer = it.next();
680
            if (layer instanceof FLyrVect) {
681
                FeatureStore layer_store = ((FLyrVect) layer).getFeatureStore();
682
                if (layer_store == store) {
683
                    return layer;
684
                }
685
            }
686
        }
687
        return null;
688
    }
689

    
690
    private IProjection getProjection(Object[] args, int i) {
691
        Object value = args[i];
692
        if (value == null) {
693
            return null;
694
        }
695
        if (value instanceof IProjection) {
696
            return (IProjection) value;
697
        }
698
        String code = value.toString();
699
        return CRSFactory.getCRS(code);
700
    }
701

    
702
    private int getUnitsArea(Object[] args, int i) {
703
        Object value = args[i];
704
        if (value == null) {
705
            throw new IllegalArgumentException("Illegal unit value 'null'");
706
        }
707
        if (value instanceof Number) {
708
            return ((Number) value).intValue();
709
        }
710
        String name = value.toString();
711
        String[] names = MapContext.getAreaAbbr();
712
        for (int j = 0; j < names.length; j++) {
713
            if (name.equalsIgnoreCase(names[j])) {
714
                return j;
715
            }
716
        }
717
        names = MapContext.getAreaNames();
718
        for (int j = 0; j < names.length; j++) {
719
            if (name.equalsIgnoreCase(names[j])) {
720
                return j;
721
            }
722
        }
723
        throw new IllegalArgumentException("Illegal unit name '" + name + "'");
724
    }
725

    
726
    private int getUnitsDistance(Object[] args, int i) {
727
        Object value = args[i];
728
        if (value == null) {
729
            throw new IllegalArgumentException("Illegal unit value 'null'");
730
        }
731
        if (value instanceof Number) {
732
            return ((Number) value).intValue();
733
        }
734
        String name = value.toString();
735
        String[] names = MapContext.getDistanceAbbr();
736
        for (int j = 0; j < names.length; j++) {
737
            if (name.equalsIgnoreCase(names[j])) {
738
                return j;
739
            }
740
        }
741
        names = MapContext.getDistanceNames();
742
        for (int j = 0; j < names.length; j++) {
743
            if (name.equalsIgnoreCase(names[j])) {
744
                return j;
745
            }
746
        }
747
        throw new IllegalArgumentException("Illegal unit name '" + name + "'");
748
    }
749

    
750
    @Override
751
    protected Map<String, Object> getVars() {
752
        if( this.appvars == null ) {
753
            PluginsManager manager = PluginsLocator.getPluginsManager();
754
            this.appvars = new HashMap<>();
755
            this.appvars.put("$GVSIGFOLDER",manager.getApplicationFolder().getAbsolutePath());
756
            this.appvars.put("$GVSIGHOMEFOLDER",manager.getApplicationHomeFolder().getAbsolutePath());
757
            this.appvars.put("$GVSIGVERSION",manager.getApplicationVersion());
758
            this.appvars.put("$GVSIGARGS",manager.getArguments());
759
            this.appvars.put("$GVSIGPACKAGE",manager.getPackageInfo());
760
        }
761
        return this.appvars;
762
    }
763

    
764
    @Override
765
    public void setVar(String name, Object value) {
766
        // Do noting ??
767
    }
768

    
769
    @Override
770
    public void removeVar(String name) {
771
        // Do noting ??
772
    }
773

    
774
}