Revision 106 org.gvsig.legend.quantitybycategory.app.mainplugin/trunk/org.gvsig.legend.quantitybycategory.app.mainplugin/src/main/java/org/gvsig/symbology/fmap/rendering/QuantityByCategoryLegend.java
QuantityByCategoryLegend.java | ||
---|---|---|
1 | 1 |
package org.gvsig.symbology.fmap.rendering; |
2 | 2 |
|
3 | 3 |
import java.util.ArrayList; |
4 |
import java.util.List; |
|
4 | 5 |
|
5 | 6 |
import org.slf4j.Logger; |
6 | 7 |
import org.slf4j.LoggerFactory; |
... | ... | |
36 | 37 |
* @author jaume dominguez faus - jaume.dominguez@iver.es |
37 | 38 |
*/ |
38 | 39 |
public class QuantityByCategoryLegend extends AbstractClassifiedVectorLegend { |
39 |
|
|
40 |
|
|
40 | 41 |
private static final Logger logger = |
41 | 42 |
LoggerFactory.getLogger(QuantityByCategoryLegend.class); |
42 |
|
|
43 |
|
|
43 | 44 |
public static final String |
44 | 45 |
QUANTITY_BY_CATEGORY_LEGEND_PERSISTENCE_DEFINITION_NAME = |
45 | 46 |
"QUANTITY_BY_CATEGORY_LEGEND_PERSISTENCE_DEFINITION_NAME"; |
46 |
|
|
47 |
|
|
47 | 48 |
public static final String |
48 | 49 |
QUANTITY_BY_CATEGORY_LEGEND_NAME = "QUANTITY_BY_CATEGORY_LEGEND_NAME"; |
49 |
|
|
50 |
|
|
50 | 51 |
private GraduatedSymbolsLegend graduatedSymbol; |
51 | 52 |
private VectorialIntervalLegend colorRamp; |
52 |
|
|
53 |
|
|
53 | 54 |
private ISymbol defaultSymbol = null; |
54 | 55 |
private int shapeType = Geometry.TYPES.SURFACE; |
55 |
|
|
56 |
|
|
56 | 57 |
private boolean isUseDefaultSymbol; |
57 | 58 |
// private DataSource ds; |
58 | 59 |
|
... | ... | |
95 | 96 |
for (int i = 0; i < colorRamp.getClassifyingFieldTypes().length; i++) { |
96 | 97 |
l.add(colorRamp.getClassifyingFieldTypes()[i]); |
97 | 98 |
} |
98 |
|
|
99 |
|
|
99 | 100 |
int len = l.size(); |
100 | 101 |
int[] resp = new int[len]; |
101 | 102 |
for (int i=0; i<len; i++) { |
... | ... | |
157 | 158 |
public String[] getDescriptions() { |
158 | 159 |
String[] desc1 = colorRamp.getDescriptions(); |
159 | 160 |
String[] desc2 = graduatedSymbol.getDescriptions(); |
161 |
Object[] objects1 = colorRamp.getValues(); |
|
162 |
Object[] objects2 = graduatedSymbol.getValues(); |
|
160 | 163 |
|
161 |
String[] descriptions = new String[desc1.length + desc2.length]; |
|
164 |
List descriptionsList = new ArrayList<String>(); |
|
165 |
for (int i = 0; i < objects1.length; i++) { |
|
166 |
//Pretend?a saltar el valor por defecto pero no es necesario si en getValues se pone como clave null |
|
167 |
// if(objects1[i] instanceof IInterval){ |
|
168 |
descriptionsList.add(desc1[i]); |
|
169 |
// } |
|
170 |
} |
|
171 |
for (int i = 0; i < objects2.length; i++) { |
|
172 |
//Pretend?a saltar el valor por defecto pero no es necesario si en getValues se pone como clave null |
|
173 |
// if(objects2[i] instanceof IInterval){ |
|
174 |
descriptionsList.add(desc2[i]); |
|
175 |
// } |
|
176 |
} |
|
162 | 177 |
|
163 |
if(desc1.length == 0) |
|
164 |
return desc2; |
|
165 |
else { |
|
166 |
for (int i = 0; i < descriptions.length; i++) { |
|
167 |
descriptions[i] = (i <desc1.length) ? desc1[i] : desc2[i - desc1.length]; |
|
168 |
} |
|
169 |
return descriptions; |
|
170 |
} |
|
178 |
String[] descriptions = new String[descriptionsList.size()]; |
|
179 |
descriptions = (String[]) descriptionsList.toArray(descriptions); |
|
180 |
return descriptions; |
|
181 |
|
|
171 | 182 |
} |
172 | 183 |
|
173 | 184 |
public ISymbol[] getSymbols() { |
174 | 185 |
ISymbol[] symbols1 = colorRamp.getSymbols(); |
175 | 186 |
ISymbol[] symbols2 = graduatedSymbol.getSymbols(); |
187 |
Object[] objects1 = colorRamp.getValues(); |
|
188 |
Object[] objects2 = graduatedSymbol.getValues(); |
|
176 | 189 |
|
177 |
ISymbol[] symbols = new ISymbol[symbols1.length + symbols2.length]; |
|
178 | 190 |
|
179 |
if(symbols1.length == 0) |
|
180 |
return symbols2; |
|
181 |
else { |
|
182 |
for (int i = 0; i < symbols.length; i++) { |
|
183 |
symbols[i] = (i < symbols1.length) ? symbols1[i] : symbols2[i - symbols1.length]; |
|
184 |
} |
|
185 |
} |
|
186 |
return symbols; |
|
191 |
List symbolsList = new ArrayList<ISymbol>(); |
|
192 |
for (int i = 0; i < objects1.length; i++) { |
|
193 |
//Pretend?a saltar el valor por defecto pero no es necesario si en getValues se pone como clave null |
|
194 |
// if(objects1[i] instanceof IInterval){ |
|
195 |
symbolsList.add(symbols1[i]); |
|
196 |
// } |
|
197 |
} |
|
198 |
for (int i = 0; i < objects2.length; i++) { |
|
199 |
//Pretend?a saltar el valor por defecto pero no es necesario si en getValues se pone como clave null |
|
200 |
// if(objects2[i] instanceof IInterval){ |
|
201 |
symbolsList.add(symbols2[i]); |
|
202 |
// } |
|
203 |
} |
|
204 |
|
|
205 |
ISymbol[] symbols = new ISymbol[symbolsList.size()]; |
|
206 |
symbols = (ISymbol[]) symbolsList.toArray(symbols); |
|
207 |
return symbols; |
|
187 | 208 |
} |
188 | 209 |
|
189 | 210 |
public Object[] getValues() { |
190 | 211 |
Object[] objects1 = colorRamp.getValues(); |
191 | 212 |
Object[] objects2 = graduatedSymbol.getValues(); |
192 | 213 |
|
193 |
Object[] objects = new IInterval[objects1.length + objects2.length]; |
|
214 |
List objectsList = new ArrayList<IInterval>(); |
|
215 |
for (int i = 0; i < objects1.length; i++) { |
|
216 |
if(objects1[i] instanceof IInterval){ |
|
217 |
objectsList.add(objects1[i]); |
|
218 |
} else { |
|
219 |
objectsList.add(null); //Si no es un IInterval es el valor por defecto |
|
220 |
} |
|
221 |
} |
|
222 |
for (int i = 0; i < objects2.length; i++) { |
|
223 |
if(objects2[i] instanceof IInterval){ |
|
224 |
objectsList.add(objects2[i]); |
|
225 |
} else { |
|
226 |
objectsList.add(null); //Si no es un IInterval es el valor por defecto |
|
227 |
} |
|
228 |
} |
|
229 |
Object[] objects = new IInterval[objectsList.size()]; |
|
230 |
objects = objectsList.toArray(objects); |
|
231 |
return objects; |
|
194 | 232 |
|
195 |
if(objects1.length == 0) |
|
196 |
return objects2; |
|
197 |
|
|
198 |
|
|
199 |
else { |
|
200 |
for (int i = 0; i < objects.length; i++) { |
|
201 |
objects[i] = (i < objects1.length) ? objects1[i] : objects2[i - objects1.length]; |
|
202 |
} |
|
203 |
return objects; |
|
204 |
} |
|
205 |
|
|
206 | 233 |
} |
207 | 234 |
|
208 | 235 |
|
209 | 236 |
public Object clone() throws CloneNotSupportedException { |
210 |
|
|
237 |
|
|
211 | 238 |
QuantityByCategoryLegend resp = |
212 | 239 |
(QuantityByCategoryLegend) super.clone(); |
213 |
|
|
240 |
|
|
214 | 241 |
resp.setShapeType(this.getShapeType()); |
215 |
|
|
242 |
|
|
216 | 243 |
resp.useDefaultSymbol(this.isUseDefaultSymbol()); |
217 | 244 |
ISymbol defsym = this.getDefaultSymbol(); |
218 | 245 |
defsym = (ISymbol) defsym.clone(); |
219 | 246 |
resp.setDefaultSymbol(defsym); |
220 |
|
|
247 |
|
|
221 | 248 |
GraduatedSymbolsLegend gleg = this.getGraduatedSymbolsLegend(); |
222 | 249 |
gleg = (GraduatedSymbolsLegend) gleg.cloneLegend(); |
223 | 250 |
resp.setGraduateSymbolLegend(gleg); |
224 |
|
|
251 |
|
|
225 | 252 |
VectorialIntervalLegend interleg = this.getColorRampLegend(); |
226 | 253 |
interleg = (VectorialIntervalLegend) interleg.cloneLegend(); |
227 | 254 |
resp.setColorRampLegend(interleg); |
... | ... | |
257 | 284 |
|
258 | 285 |
/* |
259 | 286 |
public ISymbol getSymbol(int i) { |
260 |
|
|
287 |
|
|
261 | 288 |
IMarkerSymbol sym1 = (IMarkerSymbol) graduatedSymbol.getSymbol(i); |
262 | 289 |
ISymbol sym2 = colorRamp.getSymbol(i); |
263 |
|
|
290 |
|
|
264 | 291 |
IMultiLayerSymbol multiSym = null; |
265 | 292 |
int shpt = this.getShapeType(); |
266 |
|
|
293 |
|
|
267 | 294 |
if (isPolygonal(shpt)) { |
268 | 295 |
// symbol from the GraduatedSymbolLegend is a marker, but |
269 | 296 |
// what we need is a fill symbol. Will use a MarkerFillSymbol |
... | ... | |
280 | 307 |
|
281 | 308 |
multiSym = MapContextLocator.getSymbolManager(). |
282 | 309 |
createMultiLayerSymbol(Geometry.TYPES.SURFACE); |
283 |
|
|
310 |
|
|
284 | 311 |
multiSym.addLayer(sym2); |
285 | 312 |
multiSym.addLayer(aux); |
286 | 313 |
break; |
287 |
|
|
314 |
|
|
288 | 315 |
} else { |
289 | 316 |
logger.info("Error: unexpected shape type (should be surface): " + shpt); |
290 | 317 |
} |
291 |
|
|
318 |
|
|
292 | 319 |
return multiSym; |
293 | 320 |
} |
294 | 321 |
*/ |
295 | 322 |
|
296 | 323 |
public ISymbol getSymbolByFeature(Feature feat) throws MapContextException { |
297 |
|
|
324 |
|
|
298 | 325 |
ISymbol sym1 = null, sym2 = null; |
299 | 326 |
sym1 = graduatedSymbol.getSymbolByFeature(feat); |
300 | 327 |
// Ensure fill symbol |
301 | 328 |
sym1 = GraduatedSymbolsLegend.toFillSymbol(sym1); |
302 |
|
|
329 |
|
|
303 | 330 |
sym2 = colorRamp.getSymbolByFeature(feat); |
304 | 331 |
|
305 | 332 |
IMultiLayerSymbol multiSym = null; |
306 | 333 |
int shpt = this.getShapeType(); |
307 |
|
|
334 |
|
|
308 | 335 |
if (isPolygonal(shpt)) { |
309 |
|
|
336 |
|
|
310 | 337 |
multiSym = MapContextLocator.getSymbolManager(). |
311 | 338 |
createMultiLayerSymbol(Geometry.TYPES.SURFACE); |
312 |
|
|
339 |
|
|
313 | 340 |
if (sym2 != null) multiSym.addLayer(sym2); |
314 | 341 |
if (sym1 != null) multiSym.addLayer(sym1); |
315 | 342 |
} else { |
316 | 343 |
logger.info("Error: unexpected shape type (should be surface): " + shpt); |
317 | 344 |
} |
318 |
|
|
345 |
|
|
319 | 346 |
return multiSym; |
320 | 347 |
} |
321 | 348 |
|
322 | 349 |
public void setShapeType(int shpt) { |
323 |
|
|
350 |
|
|
324 | 351 |
this.shapeType = shpt; |
325 | 352 |
ISymbol sym = null; |
326 |
|
|
353 |
|
|
327 | 354 |
graduatedSymbol.setShapeType(Geometry.TYPES.POINT); |
328 | 355 |
sym = MapContextLocator.getSymbolManager().createSymbol( |
329 | 356 |
Geometry.TYPES.POINT); |
330 | 357 |
graduatedSymbol.setDefaultSymbol(sym); |
331 |
|
|
358 |
|
|
332 | 359 |
colorRamp.setShapeType(shpt); |
333 | 360 |
sym = MapContextLocator.getSymbolManager().createSymbol(shpt); |
334 | 361 |
colorRamp.setDefaultSymbol(sym); |
... | ... | |
405 | 432 |
this.defaultSymbol = s; |
406 | 433 |
} |
407 | 434 |
|
408 |
|
|
435 |
|
|
409 | 436 |
// ============================= |
410 |
|
|
437 |
|
|
411 | 438 |
public static class RegisterPersistence implements Callable { |
412 | 439 |
|
413 | 440 |
public Object call() throws Exception { |
414 |
|
|
441 |
|
|
415 | 442 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
416 | 443 |
if (manager.getDefinition( |
417 | 444 |
QUANTITY_BY_CATEGORY_LEGEND_PERSISTENCE_DEFINITION_NAME) == null) { |
... | ... | |
420 | 447 |
QUANTITY_BY_CATEGORY_LEGEND_PERSISTENCE_DEFINITION_NAME, |
421 | 448 |
QUANTITY_BY_CATEGORY_LEGEND_PERSISTENCE_DEFINITION_NAME |
422 | 449 |
+ " Persistence definition", null, null); |
423 |
|
|
450 |
|
|
424 | 451 |
definition.extend(manager.getDefinition( |
425 | 452 |
AbstractClassifiedVectorLegend |
426 | 453 |
.CLASSIFIED_VECTOR_LEGEND_PERSISTENCE_DEFINITION_NAME)); |
427 |
|
|
454 |
|
|
428 | 455 |
definition.addDynFieldBoolean("useDefaultSymbol") |
429 | 456 |
.setMandatory(true); |
430 |
|
|
457 |
|
|
431 | 458 |
definition.addDynFieldObject("graduatedLegend") |
432 | 459 |
.setClassOfValue(ILegend.class).setMandatory(true); |
433 | 460 |
definition.addDynFieldObject("rampLegend") |
... | ... | |
452 | 479 |
} |
453 | 480 |
|
454 | 481 |
} |
455 |
|
|
482 |
|
|
456 | 483 |
public void saveToState(PersistentState state) throws PersistenceException { |
457 | 484 |
|
458 | 485 |
super.saveToState(state); |
... | ... | |
463 | 490 |
|
464 | 491 |
public void loadFromState(PersistentState state) |
465 | 492 |
throws PersistenceException { |
466 |
|
|
493 |
|
|
467 | 494 |
super.loadFromState(state); |
468 |
|
|
495 |
|
|
469 | 496 |
Boolean b = state.getBoolean("useDefaultSymbol"); |
470 | 497 |
this.useDefaultSymbol(b); |
471 |
|
|
498 |
|
|
472 | 499 |
ILegend leg = (ILegend) state.get("graduatedLegend"); |
473 | 500 |
if (leg instanceof GraduatedSymbolsLegend) { |
474 | 501 |
this.setGraduateSymbolLegend((GraduatedSymbolsLegend) leg); |
Also available in: Unified diff