svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.symbology / org.gvsig.symbology.lib / org.gvsig.symbology.lib.impl / src / main / java / org / gvsig / symbology / fmap / mapcontext / rendering / dynamiclegend / DefaultDynamicVectorLegend.java @ 44455
History | View | Annotate | Download (9.15 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.symbology.fmap.mapcontext.rendering.dynamiclegend; |
7 |
|
8 |
import java.util.ArrayList; |
9 |
import java.util.Arrays; |
10 |
import java.util.List; |
11 |
import java.util.logging.Level; |
12 |
import org.gvsig.expressionevaluator.Expression; |
13 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
14 |
import org.gvsig.fmap.dal.exception.DataException; |
15 |
import org.gvsig.fmap.dal.feature.Feature; |
16 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
17 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
18 |
import org.gvsig.fmap.geom.Geometry; |
19 |
import org.gvsig.fmap.geom.GeometryLocator; |
20 |
import org.gvsig.fmap.geom.primitive.Point; |
21 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
22 |
import org.gvsig.fmap.mapcontext.MapContextManager; |
23 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
24 |
import org.gvsig.symbology.SymbologyLocator; |
25 |
import org.gvsig.symbology.SymbologyManager; |
26 |
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.impl.AbstractVectorialLegend; |
27 |
import static org.gvsig.symbology.fmap.mapcontext.rendering.legend.impl.AbstractVectorialLegend.VECTORIAL_LEGEND_PERSISTENCE_DEFINITION_NAME; |
28 |
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.impl.VectorialUniqueValueLegend; |
29 |
import org.gvsig.tools.ToolsLocator; |
30 |
import org.gvsig.tools.dynobject.DynStruct; |
31 |
import org.gvsig.tools.persistence.PersistenceManager; |
32 |
import org.gvsig.tools.persistence.PersistentState; |
33 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
34 |
import org.gvsig.tools.util.Callable; |
35 |
import org.slf4j.Logger; |
36 |
import org.slf4j.LoggerFactory; |
37 |
|
38 |
/**
|
39 |
*
|
40 |
* @author osc
|
41 |
*/
|
42 |
public class DefaultDynamicVectorLegend extends AbstractVectorialLegend implements DynamicVectorLegend { |
43 |
|
44 |
final static private Logger LOG = LoggerFactory.getLogger(VectorialUniqueValueLegend.class); |
45 |
|
46 |
public static final String DYNAMIC_VECTOR_PERSISTENCE_DEFINITION_NAME |
47 |
= "SimgleSymbolLegend";
|
48 |
public static final String LEGEND_NAME = "DynamicSymbol"; |
49 |
private ISymbol defaultSymbol;
|
50 |
private int shapeType = Geometry.TYPES.SURFACE; // Por defecto, tipo pol?gono |
51 |
|
52 |
public static Expression expOutlineColor = null; |
53 |
public static Expression expFillColor = null; |
54 |
public static Expression expSize = null; |
55 |
public static Expression expRotation = null; |
56 |
public static String[] requiredAttributes = null; |
57 |
|
58 |
// public static final String FIELD_OUTLINECOLOR = "COUTLINE";
|
59 |
// public static final String FIELD_FILLCOLOR = "CFILL";
|
60 |
// public static final String FIELD_SIZE = "CSIZE";
|
61 |
// public static final String FIELD_ROTATION = "CROTATION";
|
62 |
|
63 |
/**
|
64 |
* Constructor method, needed by persistence.
|
65 |
*/
|
66 |
public DefaultDynamicVectorLegend() {
|
67 |
|
68 |
} |
69 |
|
70 |
/**
|
71 |
* Convenience fast constructor.
|
72 |
*
|
73 |
* @param style S?mbolo.
|
74 |
*/
|
75 |
public DefaultDynamicVectorLegend(ISymbol style) {
|
76 |
|
77 |
setDefaultSymbol(style); |
78 |
} |
79 |
|
80 |
@Override
|
81 |
public void setDefaultSymbol(ISymbol s) { |
82 |
if (s == null) { |
83 |
throw new NullPointerException("Default symbol cannot be null"); |
84 |
} |
85 |
ISymbol old = defaultSymbol; |
86 |
defaultSymbol = s; |
87 |
// fireDefaultSymbolChangedEvent(new SymbolLegendEvent(old, s));
|
88 |
} |
89 |
|
90 |
public ISymbol getSymbol(int recordIndex) { |
91 |
return defaultSymbol;
|
92 |
} |
93 |
|
94 |
@Override
|
95 |
public ISymbol getDefaultSymbol() {
|
96 |
if (defaultSymbol == null) { |
97 |
SymbologyManager symbolManager = SymbologyLocator.getSymbologyManager(); |
98 |
defaultSymbol = symbolManager.createDynamicSymbol(); |
99 |
} |
100 |
return defaultSymbol;
|
101 |
} |
102 |
|
103 |
@Override
|
104 |
public int getShapeType() { |
105 |
return shapeType;
|
106 |
} |
107 |
|
108 |
@Override
|
109 |
public void setShapeType(int shapeType) { |
110 |
if (this.shapeType != shapeType) { |
111 |
if (defaultSymbol == null || defaultSymbol.getSymbolType() != shapeType) { |
112 |
defaultSymbol = getSymbolManager().createSymbol(shapeType); |
113 |
} |
114 |
this.shapeType = shapeType;
|
115 |
} |
116 |
} |
117 |
|
118 |
private Expression getExpressionFromString(String value) { |
119 |
Expression expression = ExpressionEvaluatorLocator.getManager().createExpression();
|
120 |
expression.setPhrase(value); |
121 |
return expression;
|
122 |
} |
123 |
|
124 |
public void setOutlineColor(Expression expression) { |
125 |
expOutlineColor = expression; |
126 |
} |
127 |
|
128 |
public void setFillColor(Expression expression) { |
129 |
expFillColor = expression; |
130 |
} |
131 |
|
132 |
public void setSize(Expression expression) { |
133 |
expSize = expression; |
134 |
} |
135 |
|
136 |
public void setRotation(Expression expression) { |
137 |
expRotation = expression; |
138 |
} |
139 |
|
140 |
@Override
|
141 |
public ISymbol getSymbolByFeature(Feature feat) {
|
142 |
DynamicSymbol symbol = (DynamicSymbol) getDefaultSymbol(); |
143 |
|
144 |
symbol.setFillColor(expFillColor); |
145 |
|
146 |
symbol.setOutlineColor(expOutlineColor); |
147 |
|
148 |
symbol.setSize(expSize); |
149 |
|
150 |
symbol.setRotation(expRotation); |
151 |
|
152 |
//OFFSET TODO
|
153 |
Point pointOffset = null; |
154 |
String value = ""; |
155 |
try {
|
156 |
pointOffset = GeometryLocator.getGeometryManager().createPoint(0, 0, Geometry.SUBTYPES.GEOM2D); |
157 |
value = pointOffset.convertToWKT(); |
158 |
} catch (Exception ex) { |
159 |
java.util.logging.Logger.getLogger(DefaultDynamicSymbol.class.getName()).log(Level.SEVERE, null, ex); |
160 |
} |
161 |
|
162 |
Expression offset = getExpressionFromString(value);
|
163 |
symbol.setOffset(offset); |
164 |
|
165 |
try {
|
166 |
String[] strList = this.getRequiredFeatureAttributeNames((FeatureStore) feat.getStore()); |
167 |
List<String> reqAttr = new ArrayList<>(); |
168 |
reqAttr.addAll(Arrays.asList(strList));
|
169 |
symbol.setRequiredFeatureAttributesNames(reqAttr); |
170 |
} catch (DataException ex) {
|
171 |
java.util.logging.Logger.getLogger(DefaultDynamicVectorLegend.class.getName()).log(Level.SEVERE, null, ex); |
172 |
} |
173 |
|
174 |
symbol.setFeature(feat); |
175 |
return symbol;
|
176 |
} |
177 |
|
178 |
@Override
|
179 |
public void useDefaultSymbol(boolean b) { |
180 |
LOG.warn("TODO: DefaultDynamicVectorLegend.useDefaultSymbol");
|
181 |
} |
182 |
|
183 |
public String[] getUsedFields() { |
184 |
return new String[0]; |
185 |
} |
186 |
|
187 |
@Override
|
188 |
public boolean isUseDefaultSymbol() { |
189 |
return true; |
190 |
|
191 |
} |
192 |
|
193 |
public String getClassName() { |
194 |
return getClass().getName();
|
195 |
} |
196 |
|
197 |
@Override
|
198 |
public boolean isSuitableForShapeType(int shapeType) { |
199 |
return getShapeType() == shapeType;
|
200 |
} |
201 |
|
202 |
public void setFeatureStore(FeatureStore fs) throws DataException { |
203 |
LOG.warn("TODO: DefaultDynamicVectorLegend.useDefaultSymbol");
|
204 |
} |
205 |
|
206 |
@Override
|
207 |
protected String[] getRequiredFeatureAttributeNames( |
208 |
//TODO: Optimizar por obtener solo los campos que usan las expresiones
|
209 |
FeatureStore featureStore) throws DataException {
|
210 |
if (requiredAttributes == null) { |
211 |
int arraySize = featureStore.getDefaultFeatureType().size();
|
212 |
requiredAttributes = new String[arraySize]; |
213 |
for (int i = 0; i < featureStore.getDefaultFeatureType().size(); i++) { |
214 |
FeatureAttributeDescriptor att = featureStore.getDefaultFeatureType().get(i); |
215 |
requiredAttributes[i] = att.getName(); |
216 |
} |
217 |
} |
218 |
return requiredAttributes;
|
219 |
} |
220 |
|
221 |
@Override
|
222 |
public Object clone() throws CloneNotSupportedException { |
223 |
DynamicVectorLegend clone = (DynamicVectorLegend) super.clone();
|
224 |
|
225 |
// // Clone default symbol
|
226 |
// if (defaultSymbol != null) {
|
227 |
// clone.defaultSymbol = (ISymbol) defaultSymbol.clone();
|
228 |
// }
|
229 |
return clone;
|
230 |
} |
231 |
|
232 |
@Override
|
233 |
public void loadFromState(PersistentState state) |
234 |
throws PersistenceException {
|
235 |
super.loadFromState(state);
|
236 |
} |
237 |
|
238 |
@Override
|
239 |
public void saveToState(PersistentState state) throws PersistenceException { |
240 |
super.saveToState(state);
|
241 |
} |
242 |
|
243 |
public static class RegisterPersistence implements Callable { |
244 |
|
245 |
@Override
|
246 |
public Object call() throws Exception { |
247 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
248 |
if (manager.getDefinition(DYNAMIC_VECTOR_PERSISTENCE_DEFINITION_NAME) == null) { |
249 |
DynStruct definition = manager.addDefinition( |
250 |
DynamicVectorLegend.class, |
251 |
DYNAMIC_VECTOR_PERSISTENCE_DEFINITION_NAME, |
252 |
DYNAMIC_VECTOR_PERSISTENCE_DEFINITION_NAME + " Persistence definition",
|
253 |
null,
|
254 |
null
|
255 |
); |
256 |
// Extend the Vectorial Legend base definition
|
257 |
definition.extend(manager.getDefinition(VECTORIAL_LEGEND_PERSISTENCE_DEFINITION_NAME)); |
258 |
} |
259 |
return Boolean.TRUE; |
260 |
} |
261 |
|
262 |
} |
263 |
|
264 |
public static class RegisterLegend implements Callable { |
265 |
|
266 |
@Override
|
267 |
public Object call() throws Exception { |
268 |
MapContextManager manager = MapContextLocator.getMapContextManager(); |
269 |
|
270 |
// manager.registerLegend(ISingleSymbolLegend.LEGEND_NAME,
|
271 |
manager.registerLegend(LEGEND_NAME, |
272 |
DynamicVectorLegend.class); |
273 |
|
274 |
return Boolean.TRUE; |
275 |
} |
276 |
|
277 |
} |
278 |
|
279 |
} |