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