Statistics
| Revision:

gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.swing / org.gvsig.vectorediting.swing.impl / src / main / java / org / gvsig / vectorediting / swing / impl / EditingContextSymbolTable.java @ 2203

History | View | Annotate | Download (5.69 KB)

1
/*
2
 * gvSIG. Desktop Geographic Information System.
3
 * 
4
 * Copyright (C) 2007-2020 gvSIG Association.
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License 
17
 * along with this program. If not, see <https://www.gnu.org/licenses/>. 
18
 * 
19
 * For any additional information, do not hesitate to contact us
20
 * at info AT gvsig.com, or visit our website www.gvsig.com.
21
 */
22

    
23
package org.gvsig.vectorediting.swing.impl;
24

    
25
import java.util.ArrayList;
26
import java.util.Collection;
27
import java.util.Collections;
28
import java.util.List;
29
import org.apache.commons.lang3.StringUtils;
30
import org.gvsig.expressionevaluator.spi.AbstractSymbolTable;
31
import org.gvsig.fmap.geom.Geometry;
32
import org.gvsig.fmap.geom.primitive.Point;
33
import org.gvsig.temporarystorage.TemporaryStorageGroup;
34
import org.gvsig.temporarystorage.TemporaryStorageLocator;
35
import org.gvsig.temporarystorage.TemporaryStorageManager;
36

    
37
/**
38
 *
39
 * @author gvSIG Team
40
 */
41
public class EditingContextSymbolTable extends AbstractSymbolTable {
42

    
43
    private static class ListWithMaximumSize<T> extends ArrayList<T> {
44
        
45
        private final int maximumSize;
46
        
47
        public ListWithMaximumSize(int maximumSize) {
48
            this.maximumSize = maximumSize;
49
        }
50

    
51
        @Override
52
        public boolean add(T e) {
53
            if( this.size()>this.maximumSize ) {
54
                this.remove(0);
55
            }
56
            return super.add(e);
57
        }
58

    
59
        @Override
60
        public void add(int index, T element) {
61
            if( this.size()>this.maximumSize ) {
62
                this.remove(0);
63
            }
64
            super.add(index, element);
65
        }
66
    }
67

    
68
    private final List<Point> lastPoints;
69
    private final TemporaryStorageGroup globalPointsStorage;
70
    
71
    public EditingContextSymbolTable() {
72
        this.lastPoints = new ListWithMaximumSize(100);
73
        TemporaryStorageManager manager = TemporaryStorageLocator.getTemporaryStorageManager();
74
        this.globalPointsStorage = manager.create("Points",Point.class);
75
    }
76
    
77
    public void addPoint(Point point) {
78
        this.lastPoints.add(point);
79
    }
80

    
81
    @Override
82
    public boolean exists(String name) {
83
        if( globalPointsStorage.contains(name) ) {
84
            return true;
85
        }
86
        if( globalPointsStorage.contains(name+"$x") ) {
87
            return true;
88
        }
89
        if( globalPointsStorage.contains(name+"$y") ) {
90
            return true;
91
        }
92
        if( this.lastPoints.isEmpty() || StringUtils.isBlank(name) ) {
93
            return false;
94
        }
95
        name = name.toLowerCase();
96
        int index = -1;
97
        if( name.length()==2 ) {
98
            index = this.lastPoints.size()-1;
99
        } else if( name.length()<3 ) {
100
            return false;
101
        } else {
102
            try {
103
                index = Integer.parseUnsignedInt(name.substring(2));
104
            } catch(NumberFormatException ex) {
105
                return false;
106
            }
107
        }
108
        if( index < 0 || index >= this.lastPoints.size() ) {
109
            return false;
110
        }
111
        if( name.startsWith("$p") || name.startsWith("$x") || name.startsWith("$y") ) {
112
            return true;
113
        }
114
        return false;
115
    }
116

    
117
    @Override
118
    public Object value(String name) {
119
        if( globalPointsStorage.contains(name) ) {
120
            return globalPointsStorage.get(name);
121
        }
122
        if( globalPointsStorage.contains(name+"$x") ) {
123
            Point point = (Point) globalPointsStorage.get(name+"$x");
124
            return point.getX();
125
        }
126
        if( globalPointsStorage.contains(name+"$y") ) {
127
            Point point = (Point) globalPointsStorage.get(name+"$y");
128
            return point.getY();
129
        }
130
        if( this.lastPoints.isEmpty() || StringUtils.isBlank(name) ) {
131
            return null;
132
        }
133
        name = name.toLowerCase();
134
        int index = -1;
135
        if( name.length()==2 ) {
136
            index = 0;
137
        } else if( name.length()<3 ) {
138
            return null;
139
        } else {
140
            try {
141
                index = Integer.parseUnsignedInt(name.substring(2));
142
            } catch(NumberFormatException ex) {
143
                return null;
144
            }
145
        }
146
        if( index < 0 || index >= this.lastPoints.size() ) {
147
            return false;
148
        }
149
        // El index 0 es el ultimo en la lista
150
        index =  (this.lastPoints.size() - index) - 1;
151
        
152
        if( name.startsWith("$p") ) {
153
            Point point = this.lastPoints.get(index);
154
            return point;
155
        }
156
        if( name.startsWith("$x") ) {
157
            Point point = this.lastPoints.get(index);
158
            return point.getX();
159
        }
160
        if( name.startsWith("$y") ) {
161
            Point point = this.lastPoints.get(index);
162
            return point.getY();
163
        }
164
        return null;
165
    }
166

    
167
    @Override
168
    public Collection<String> localvariables() {
169
        if( this.lastPoints.isEmpty() ) {
170
            return Collections.EMPTY_LIST;
171
        }
172
        List<String> names = new ArrayList<>();
173
        names.add("$p");
174
        names.add("$x");
175
        names.add("$y");
176
        for (int i = 0; i < this.lastPoints.size(); i++) {
177
            names.add("$p"+i);
178
            names.add("$x"+i);
179
            names.add("$y"+i);
180
        }
181
        return names;
182
    }
183
    
184
    
185
}