Revision 2203
org.gvsig.vectorediting/trunk/org.gvsig.vectorediting/org.gvsig.vectorediting.swing/org.gvsig.vectorediting.swing.impl/src/main/java/org/gvsig/vectorediting/swing/impl/DefaultEditingContext.java | ||
---|---|---|
28 | 28 |
import java.awt.Component; |
29 | 29 |
import java.lang.ref.WeakReference; |
30 | 30 |
import java.lang.reflect.InvocationTargetException; |
31 |
import java.util.AbstractList; |
|
31 | 32 |
import java.util.ArrayList; |
33 |
import java.util.List; |
|
32 | 34 |
import java.util.Set; |
33 | 35 |
import java.util.Stack; |
34 | 36 |
import java.util.prefs.PreferenceChangeEvent; |
... | ... | |
92 | 94 |
import org.apache.commons.lang3.StringUtils; |
93 | 95 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
94 | 96 |
import org.gvsig.expressionevaluator.MutableSymbolTable; |
97 |
import org.gvsig.expressionevaluator.SymbolTable; |
|
95 | 98 |
import org.gvsig.fmap.dal.DataTypes; |
96 | 99 |
|
97 | 100 |
import org.gvsig.fmap.dal.EditingNotification; |
... | ... | |
965 | 968 |
} |
966 | 969 |
return false; |
967 | 970 |
} |
971 |
|
|
972 |
private static EditingContextSymbolTable contextSymbolTable = new EditingContextSymbolTable(); |
|
968 | 973 |
|
969 | 974 |
private Point parsePoint(String response) throws ParsePointException { |
970 |
TemporaryStorageManager manager = TemporaryStorageLocator.getTemporaryStorageManager(); |
|
971 |
TemporaryStorageGroup storage = manager.create("Points",Point.class); |
|
972 |
|
|
973 |
MutableSymbolTable symbolTable = ExpressionUtils.createSymbolTable(); |
|
974 |
symbolTable.setVar("LAST_POINT_ADDED", storage.get("LAST_POINT_ADDED_IN_EDTING")); |
|
975 |
|
|
976 | 975 |
try { |
977 |
Object x = ExpressionUtils.evaluate(symbolTable, response);
|
|
976 |
Object x = ExpressionUtils.evaluate(contextSymbolTable, response);
|
|
978 | 977 |
if( x instanceof Point ) { |
979 |
storage.put("LAST_POINT_ADDED_IN_EDTING", x);
|
|
978 |
contextSymbolTable.addPoint((Point) x);
|
|
980 | 979 |
return (Point) x; |
981 | 980 |
} |
982 | 981 |
} catch(Exception ex) { |
... | ... | |
984 | 983 |
} |
985 | 984 |
String s = "ST_MakePoint("+response+")"; |
986 | 985 |
try { |
987 |
Object x = ExpressionUtils.evaluate(symbolTable, s);
|
|
986 |
Object x = ExpressionUtils.evaluate(contextSymbolTable, s);
|
|
988 | 987 |
if( x instanceof Point ) { |
989 |
storage.put("LAST_POINT_ADDED_IN_EDTING", x);
|
|
988 |
contextSymbolTable.addPoint((Point) x);
|
|
990 | 989 |
return (Point) x; |
991 | 990 |
} |
992 | 991 |
} catch(Exception ex) { |
... | ... | |
996 | 995 |
} |
997 | 996 |
|
998 | 997 |
private Double parseValue(String response) throws ParseValueException { |
999 |
TemporaryStorageManager manager = TemporaryStorageLocator.getTemporaryStorageManager(); |
|
1000 |
TemporaryStorageGroup storage = manager.create("Points",Point.class); |
|
1001 |
|
|
1002 |
MutableSymbolTable symbolTable = ExpressionUtils.createSymbolTable(); |
|
1003 |
symbolTable.setVar("LAST_POINT_ADDED", storage.get("LAST_POINT_ADDED_IN_EDTING")); |
|
1004 |
|
|
1005 | 998 |
try { |
1006 |
Object x = ExpressionUtils.evaluate(symbolTable, response);
|
|
999 |
Object x = ExpressionUtils.evaluate(contextSymbolTable, response);
|
|
1007 | 1000 |
if( x instanceof Double ) { |
1008 | 1001 |
return (Double) x; |
1009 | 1002 |
} |
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 | ||
---|---|---|
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 |
} |
Also available in: Unified diff