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