svn-gvsig-desktop / tags / v1_1_Build_1014 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / rendering / VectorialUniqueValueLegend.java @ 13593
History | View | Annotate | Download (26.5 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package com.iver.cit.gvsig.fmap.rendering; |
42 |
|
43 |
import java.sql.Types; |
44 |
import java.text.ParseException; |
45 |
import java.util.ArrayList; |
46 |
import java.util.Comparator; |
47 |
import java.util.TreeMap; |
48 |
|
49 |
import org.geotools.filter.ExpressionBuilder; |
50 |
import org.geotools.filter.Filter; |
51 |
import org.geotools.styling.FeatureTypeStyle; |
52 |
import org.geotools.styling.NamedLayer; |
53 |
import org.geotools.styling.Rule; |
54 |
import org.geotools.styling.SLDTransformer; |
55 |
import org.geotools.styling.Style; |
56 |
import org.geotools.styling.StyleBuilder; |
57 |
import org.geotools.styling.StyleFactory; |
58 |
import org.geotools.styling.StyledLayerDescriptor; |
59 |
import org.geotools.styling.Symbolizer; |
60 |
|
61 |
import com.hardcode.gdbms.engine.data.DataSource; |
62 |
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException; |
63 |
import com.hardcode.gdbms.engine.instruction.IncompatibleTypesException; |
64 |
import com.hardcode.gdbms.engine.instruction.SemanticException; |
65 |
import com.hardcode.gdbms.engine.values.BooleanValue; |
66 |
import com.hardcode.gdbms.engine.values.NullValue; |
67 |
import com.hardcode.gdbms.engine.values.StringValue; |
68 |
import com.hardcode.gdbms.engine.values.Value; |
69 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
70 |
import com.iver.cit.gvsig.fmap.DriverException; |
71 |
import com.iver.cit.gvsig.fmap.core.FShape; |
72 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
73 |
import com.iver.cit.gvsig.fmap.core.ISLDCompatible; |
74 |
import com.iver.cit.gvsig.fmap.core.ISymbol; |
75 |
import com.iver.cit.gvsig.fmap.core.SLDTags; |
76 |
import com.iver.cit.gvsig.fmap.core.SLDUtils; |
77 |
import com.iver.cit.gvsig.fmap.core.v02.FConstant; |
78 |
import com.iver.cit.gvsig.fmap.core.v02.FSymbol; |
79 |
import com.iver.cit.gvsig.fmap.layers.XMLException; |
80 |
import com.iver.utiles.XMLEntity; |
81 |
|
82 |
/**
|
83 |
* Leyenda vectorial por valores ?nicos.
|
84 |
*
|
85 |
* @author Vicente Caballero Navarro
|
86 |
*/
|
87 |
public class VectorialUniqueValueLegend implements UniqueValueLegend, |
88 |
VectorialLegend { |
89 |
private boolean sorter=true; |
90 |
private TreeMap symbols = new TreeMap(new Comparator() { |
91 |
public int compare(Object o1, Object o2) { |
92 |
if ((o1 != null) && (o2 != null)) { |
93 |
Value v2 = (Value) o2; |
94 |
Value v1 = (Value) o1; |
95 |
// TODO estas dos comprobaciones son por evitar un bug en el
|
96 |
// gdbms, cuando se solucione se puede eliminar.
|
97 |
if (v1 instanceof NullValue && v2 instanceof NullValue) { |
98 |
return 0; |
99 |
} |
100 |
|
101 |
if (v1 instanceof NullValue) { |
102 |
return -1; |
103 |
} |
104 |
|
105 |
if (v2 instanceof NullValue) { |
106 |
return 1; |
107 |
} |
108 |
if (sorter) {
|
109 |
|
110 |
BooleanValue boolVal; |
111 |
|
112 |
try {
|
113 |
boolVal = (BooleanValue) (v1.greater(v2)); |
114 |
|
115 |
if (boolVal.getValue()) {
|
116 |
return 1; |
117 |
} |
118 |
|
119 |
boolVal = (BooleanValue) (v1.less(v2)); |
120 |
|
121 |
if (boolVal.getValue()) {
|
122 |
return -1; |
123 |
} |
124 |
} catch (IncompatibleTypesException e) {
|
125 |
} |
126 |
|
127 |
try {
|
128 |
if (((BooleanValue) v1.equals(v2)).getValue()) {
|
129 |
return 0; |
130 |
} |
131 |
} catch (IncompatibleTypesException e) {
|
132 |
} |
133 |
|
134 |
if (v1 instanceof StringValue) { |
135 |
return -1; |
136 |
} |
137 |
|
138 |
if (v2 instanceof StringValue) { |
139 |
return 1; |
140 |
} |
141 |
|
142 |
} else {
|
143 |
int indexV1 = keys.indexOf(v1);
|
144 |
int indexV2 = keys.indexOf(v2);
|
145 |
if (indexV1 > indexV2) {
|
146 |
return -1; |
147 |
} else if (indexV1 < indexV2) { |
148 |
return 1; |
149 |
} |
150 |
return 0; |
151 |
} |
152 |
} |
153 |
return 0; |
154 |
} |
155 |
}); // Para poder ordenar
|
156 |
private ArrayList keys = new ArrayList(); // En lugar de un HashSet, para |
157 |
// tener acceso por ?ndice
|
158 |
private String fieldName; |
159 |
protected int fieldId = -1; |
160 |
private String labelFieldName; |
161 |
private String labelFieldHeight; |
162 |
private String labelFieldRotation; |
163 |
private ISymbol defaultSymbol;
|
164 |
private DataSource dataSource; |
165 |
private int shapeType; |
166 |
private String valueType = NullValue.class.getName(); |
167 |
private boolean useDefaultSymbol = false; |
168 |
// private boolean bWithHeightText = false;
|
169 |
|
170 |
/**
|
171 |
* Crea un nuevo VectorialUniqueValueLegend.
|
172 |
*/
|
173 |
public VectorialUniqueValueLegend() {
|
174 |
// defaultSymbol = LegendFactory.DEFAULT_POLYGON_SYMBOL;
|
175 |
} |
176 |
|
177 |
/**
|
178 |
* Crea un nuevo VectorialUniqueValueLegend.
|
179 |
*
|
180 |
* @param shapeType
|
181 |
* Tipo de shape.
|
182 |
*/
|
183 |
public VectorialUniqueValueLegend(int shapeType) { |
184 |
setShapeType(shapeType); |
185 |
} |
186 |
|
187 |
/**
|
188 |
* Inserta el tipo de shape.
|
189 |
*
|
190 |
* @param shapeType Tipo de shape.
|
191 |
*/
|
192 |
public void setShapeType(int shapeType) { |
193 |
if (this.shapeType != shapeType) { |
194 |
switch (shapeType) {
|
195 |
case FShape.POINT:
|
196 |
defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT);
|
197 |
|
198 |
break;
|
199 |
|
200 |
case FShape.LINE:
|
201 |
defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_LINE);
|
202 |
|
203 |
break;
|
204 |
|
205 |
case FShape.POLYGON:
|
206 |
defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_FILL);
|
207 |
|
208 |
break;
|
209 |
|
210 |
default:
|
211 |
defaultSymbol = new FSymbol(shapeType);
|
212 |
} |
213 |
|
214 |
this.shapeType = shapeType;
|
215 |
} |
216 |
} |
217 |
|
218 |
/**
|
219 |
* @see com.iver.cit.gvsig.fmap.rendering.UniqueValueLegend#setValueSymbolByID(int,
|
220 |
* ISymbol)
|
221 |
*/
|
222 |
public void setValueSymbolByID(int id, ISymbol symbol) { |
223 |
symbols.put(keys.get(id), symbol); |
224 |
} |
225 |
|
226 |
/**
|
227 |
* Devuelve un s?mbolo a partir del ID. Mira en el m_ArrayKeys el elemento
|
228 |
* ID, y con esa clave recupera el FSymbol por valor
|
229 |
*
|
230 |
* @param id ID.
|
231 |
* @param symbol DOCUMENT ME!
|
232 |
*/
|
233 |
|
234 |
/*
|
235 |
public FSymbol getSymbolByID(int ID) {
|
236 |
return (FSymbol) symbols.get(keys.get(ID));
|
237 |
}
|
238 |
*/
|
239 |
|
240 |
/**
|
241 |
* Se usa en la tabla que muestra una leyenda.
|
242 |
* @deprecated use setValueSymbolByID(int id, ISymbol symbol);
|
243 |
* @param id
|
244 |
* @param symbol
|
245 |
*/
|
246 |
public void setValueSymbol(int id, ISymbol symbol) { |
247 |
symbols.put(keys.get(id), symbol); |
248 |
} |
249 |
|
250 |
/**
|
251 |
* @see com.iver.cit.gvsig.fmap.rendering.UniqueValueLegend#getValues()
|
252 |
*/
|
253 |
public Object[] getValues() { |
254 |
return symbols.keySet().toArray(new Object[0]); |
255 |
} |
256 |
|
257 |
/**
|
258 |
* @see com.iver.cit.gvsig.fmap.rendering.UniqueValueLegend#addSymbol(java.lang.Object,
|
259 |
* ISymbol)
|
260 |
*/
|
261 |
public void addSymbol(Object key, ISymbol symbol) { |
262 |
Object resul;
|
263 |
resul = symbols.put(key, symbol); |
264 |
|
265 |
if (resul != null) { |
266 |
System.err.println("Error: la clave " + key + |
267 |
" ya exist?a. Resul = " + resul);
|
268 |
System.err.println("symbol nuevo:" + symbol.getDescription() + |
269 |
" Sviejo= " + ((ISymbol) resul).getDescription());
|
270 |
} else {
|
271 |
keys.add(key); |
272 |
if (!key.getClass().equals(NullValue.class)) {
|
273 |
valueType = key.getClass().getName(); |
274 |
} |
275 |
} |
276 |
} |
277 |
|
278 |
/**
|
279 |
* @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#clear()
|
280 |
*/
|
281 |
public void clear() { |
282 |
keys.clear(); |
283 |
symbols.clear(); |
284 |
} |
285 |
|
286 |
/**
|
287 |
* @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#getDescriptions()
|
288 |
*/
|
289 |
public String[] getDescriptions() { |
290 |
String[] descriptions = new String[symbols.size()]; |
291 |
ISymbol[] auxSym = getSymbols();
|
292 |
|
293 |
for (int i = 0; i < descriptions.length; i++) |
294 |
descriptions[i] = auxSym[i].getDescription(); |
295 |
|
296 |
return descriptions;
|
297 |
} |
298 |
|
299 |
/**
|
300 |
* @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#getSymbols()
|
301 |
*/
|
302 |
public ISymbol[] getSymbols() { |
303 |
return (ISymbol[]) symbols.values().toArray(new ISymbol[0]); |
304 |
} |
305 |
|
306 |
/**
|
307 |
* @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#getFieldName()
|
308 |
*/
|
309 |
public String getFieldName() { |
310 |
return fieldName;
|
311 |
} |
312 |
|
313 |
/**
|
314 |
* @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setDefaultSymbol(ISymbol)
|
315 |
*/
|
316 |
public void setDefaultSymbol(ISymbol s) { |
317 |
defaultSymbol = s; |
318 |
} |
319 |
|
320 |
/* (non-Javadoc)
|
321 |
* @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getLabelField()
|
322 |
*/
|
323 |
public String getLabelField() { |
324 |
return labelFieldName;
|
325 |
} |
326 |
|
327 |
/**
|
328 |
* @see com.iver.cit.gvsig.fmap.rendering.Legend#setLabelField(int)
|
329 |
*/
|
330 |
public void setLabelField(String fieldName) { |
331 |
labelFieldName = fieldName; |
332 |
} |
333 |
|
334 |
/**
|
335 |
* @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#setField()
|
336 |
*/
|
337 |
public void setFieldName(String str) { |
338 |
fieldName = str; |
339 |
} |
340 |
|
341 |
/**
|
342 |
* @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getSymbol(int)
|
343 |
*/
|
344 |
public ISymbol getSymbol(int recordIndex) throws DriverException { |
345 |
try {
|
346 |
Value val = dataSource.getFieldValue(recordIndex, fieldId); |
347 |
ISymbol theSymbol = getSymbolByValue(val); |
348 |
|
349 |
//if (theSymbol != null) {
|
350 |
return theSymbol;
|
351 |
|
352 |
//} else {
|
353 |
// return getDefaultSymbol();
|
354 |
//}
|
355 |
} catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
|
356 |
throw new DriverException(e); |
357 |
} |
358 |
} |
359 |
|
360 |
/**
|
361 |
* Devuelve un s?mbolo a partir de una IFeature.
|
362 |
* OJO!! Cuando usamos un feature iterator de base de datos
|
363 |
* el ?nico campo que vendr? rellenado es el de fieldID.
|
364 |
* Los dem?s vendr?n a nulos para ahorra tiempo de creaci?n.
|
365 |
* @param feat IFeature
|
366 |
*
|
367 |
* @return S?mbolo.
|
368 |
*/
|
369 |
public ISymbol getSymbolByFeature(IFeature feat) {
|
370 |
Value val = feat.getAttribute(fieldId); |
371 |
// Value val = feat.getAttribute(0);
|
372 |
ISymbol theSymbol = getSymbolByValue(val); |
373 |
|
374 |
//if (theSymbol != null) {
|
375 |
return theSymbol;
|
376 |
|
377 |
//} else {
|
378 |
// return getDefaultSymbol();
|
379 |
//}
|
380 |
} |
381 |
|
382 |
/**
|
383 |
* @see com.iver.cit.gvsig.fmap.rendering.Legend#getDefaultSymbol()
|
384 |
*/
|
385 |
public ISymbol getDefaultSymbol() {
|
386 |
NullUniqueValue nuv=new NullUniqueValue();
|
387 |
if (symbols.containsKey(nuv))
|
388 |
return (ISymbol)symbols.get(nuv);
|
389 |
return defaultSymbol;
|
390 |
} |
391 |
|
392 |
/**
|
393 |
* @deprecated
|
394 |
* Writes and SLD using GEOTOOLS objetcs.
|
395 |
* @see com.iver.cit.gvsig.fmap.rendering.Legend#getSLDString()
|
396 |
*/
|
397 |
public String getSLDString_() |
398 |
{ |
399 |
try{
|
400 |
StyledLayerDescriptor sld = new StyledLayerDescriptor();
|
401 |
StyleFactory styleFactory = StyleFactory.createStyleFactory(); |
402 |
StyleBuilder sb = new StyleBuilder();
|
403 |
Style style = sb.createStyle();
|
404 |
style.setName("default");
|
405 |
Filter filter = null; |
406 |
Rule rule = null;
|
407 |
|
408 |
FeatureTypeStyle featStyle = styleFactory.createFeatureTypeStyle(); |
409 |
featStyle.setFeatureTypeName(fieldName); |
410 |
|
411 |
ISymbol[] symbols = this.getSymbols(); |
412 |
Symbolizer[] theSymbolizers = new Symbolizer[symbols.length]; |
413 |
Object[] values = this.getValues(); |
414 |
String valueStr = null; |
415 |
String condition = null; |
416 |
|
417 |
for(int i = 0; i < symbols.length; i++ ) |
418 |
{ |
419 |
valueStr = values[i].toString(); |
420 |
//if(this.valueType == "")
|
421 |
condition = fieldName +"='"+valueStr+"'"; |
422 |
//else
|
423 |
// condition = fieldName +"="+values[i];
|
424 |
filter = (Filter)ExpressionBuilder.parse(condition);
|
425 |
theSymbolizers[0] = SLDUtils.toGeotoolsSymbol(symbols[i]);
|
426 |
rule = styleFactory.createRule(); |
427 |
rule.setName(valueStr); |
428 |
rule.setTitle(valueStr); |
429 |
rule.setFilter(filter); |
430 |
rule.setSymbolizers((Symbolizer[])theSymbolizers.clone());
|
431 |
featStyle.addRule(rule); |
432 |
} |
433 |
style.addFeatureTypeStyle(featStyle); |
434 |
SLDTransformer st = new SLDTransformer();
|
435 |
NamedLayer namedLayer = new NamedLayer();
|
436 |
namedLayer.setName("comunidades");
|
437 |
namedLayer.addStyle(style); |
438 |
sld.addStyledLayer(namedLayer); |
439 |
return st.transform(style);
|
440 |
|
441 |
}catch(Exception e) |
442 |
{ |
443 |
e.printStackTrace(); |
444 |
return null; |
445 |
} |
446 |
} |
447 |
/**
|
448 |
* creates the SLD String that defines this legend type.
|
449 |
*/
|
450 |
public String getSLDString(String name) |
451 |
{ |
452 |
try{
|
453 |
|
454 |
XmlBuilder xmlBuilder = new XmlBuilder();
|
455 |
xmlBuilder.writeHeader(); |
456 |
xmlBuilder.openTag(SLDTags.SLD_ROOT, SLDTags.VERSION_ATTR, SLDTags.VERSION_1_0_0); |
457 |
xmlBuilder.openTag(SLDTags.NAMEDLAYER); |
458 |
xmlBuilder.writeTag(SLDTags.NAME,name); |
459 |
xmlBuilder.openTag(SLDTags.USERSTYLE); |
460 |
xmlBuilder.openTag(SLDTags.FEATURETYPESTYLE); |
461 |
//xmlBuilder.writeTag(SLDTags.FEATURETYPENAME,"fieldName");
|
462 |
|
463 |
ISymbol[] symbols = this.getSymbols(); |
464 |
Object[] values = this.getValues(); |
465 |
String valueStr = null; |
466 |
|
467 |
for(int i = 0; i < symbols.length; i++ ) |
468 |
{ |
469 |
valueStr = values[i].toString(); |
470 |
xmlBuilder.openTag(SLDTags.RULE); |
471 |
xmlBuilder.writeTag(SLDTags.NAME, valueStr); |
472 |
xmlBuilder.openTag(SLDTags.FILTER); |
473 |
xmlBuilder.openTag(SLDTags.PROPERTYISEQUALTO); |
474 |
xmlBuilder.writeTag(SLDTags.PROPERTYNAME,fieldName); |
475 |
xmlBuilder.writeTag(SLDTags.LITERAL, valueStr); |
476 |
xmlBuilder.closeTag(); |
477 |
xmlBuilder.closeTag(); |
478 |
if (symbols[i] instanceof ISLDCompatible) |
479 |
{ |
480 |
ISLDCompatible symSLD = (ISLDCompatible) symbols[i]; |
481 |
xmlBuilder.writeRaw(symSLD.toSLD()); |
482 |
} |
483 |
else
|
484 |
throw new RuntimeException("Cannot convert Symbol " + i + " " + symbols[i].getDescription() + " to SLD"); |
485 |
|
486 |
xmlBuilder.closeTag(); |
487 |
} |
488 |
|
489 |
xmlBuilder.closeTag(); |
490 |
xmlBuilder.closeTag(); |
491 |
xmlBuilder.closeTag(); |
492 |
xmlBuilder.closeTag(); |
493 |
return xmlBuilder.getXML();
|
494 |
|
495 |
}catch(Exception e) |
496 |
{ |
497 |
e.printStackTrace(); |
498 |
return null; |
499 |
} |
500 |
} |
501 |
/**
|
502 |
* @see com.iver.cit.gvsig.fmap.rendering.Legend#getXMLEntity()
|
503 |
*/
|
504 |
public XMLEntity getXMLEntity() {
|
505 |
XMLEntity xml = new XMLEntity();
|
506 |
xml.putProperty("className", this.getClass().getName()); |
507 |
xml.putProperty("fieldName", fieldName);
|
508 |
xml.putProperty("labelfield", labelFieldName);
|
509 |
xml.putProperty("labelFieldHeight", labelFieldHeight);
|
510 |
xml.putProperty("labelFieldRotation", labelFieldRotation);
|
511 |
|
512 |
xml.putProperty("useDefaultSymbol", useDefaultSymbol);
|
513 |
xml.putProperty("sorter",sorter);
|
514 |
xml.addChild(getDefaultSymbol().getXMLEntity()); |
515 |
// xml.putProperty("isBWithHeightText", isBWithHeightText());
|
516 |
xml.putProperty("numKeys", keys.size());
|
517 |
|
518 |
if (keys.size() > 0) { |
519 |
xml.putProperty("tipoValueKeys", valueType);
|
520 |
|
521 |
String[] sk = new String[keys.size()]; |
522 |
String[] sv = new String[keys.size()]; |
523 |
int[] stk = new int[keys.size()]; |
524 |
int[] stv = new int[keys.size()]; |
525 |
ISymbol[] fsymbols = getSymbols();
|
526 |
Object[] values = getValues(); |
527 |
|
528 |
for (int i = 0; i < keys.size(); i++) { |
529 |
if (((Value) keys.get(i)).toString().equals("")) { |
530 |
sk[i] =" ";
|
531 |
}else {
|
532 |
sk[i] = ((Value) keys.get(i)).toString(); |
533 |
} |
534 |
if (((Value) values[i]).toString().equals("")) { |
535 |
sv[i] =" ";
|
536 |
}else {
|
537 |
sv[i] = ((Value) values[i]).toString(); |
538 |
} |
539 |
stk[i]= ((Value)keys.get(i)).getSQLType(); |
540 |
stv[i]= ((Value)values[i]).getSQLType(); |
541 |
xml.addChild(fsymbols[i].getXMLEntity()); |
542 |
|
543 |
///System.out.println("get-----------"+sk[i]+"--"+fsymbols[i].getDescription()+"---"+fsymbols[i].getColor());
|
544 |
} |
545 |
|
546 |
xml.putProperty("keys", sk);
|
547 |
xml.putProperty("values", sv);
|
548 |
xml.putProperty("typeKeys",stk);
|
549 |
xml.putProperty("typeValues",stv);
|
550 |
} |
551 |
|
552 |
return xml;
|
553 |
} |
554 |
|
555 |
/**
|
556 |
* Inserta el XMLEntity.
|
557 |
*
|
558 |
* @param xml XMLEntity.
|
559 |
*/
|
560 |
public void setXMLEntity03(XMLEntity xml) { |
561 |
clear(); |
562 |
setFieldName(xml.getStringProperty("fieldName"));
|
563 |
setLabelField(xml.getStringProperty("labelfield"));
|
564 |
|
565 |
int useDefaultSymbol = xml.getIntProperty("useDefaultSymbol"); |
566 |
|
567 |
if (useDefaultSymbol == 1) { |
568 |
setDefaultSymbol(FSymbol.createFromXML03(xml.getChild(0)));
|
569 |
} else {
|
570 |
setDefaultSymbol(null);
|
571 |
} |
572 |
|
573 |
int numKeys = xml.getIntProperty("numKeys"); |
574 |
|
575 |
if (numKeys > 0) { |
576 |
String className = xml.getStringProperty("tipoValueKeys"); |
577 |
String[] sk = xml.getStringArrayProperty("keys"); |
578 |
String[] sv = xml.getStringArrayProperty("values"); |
579 |
Value auxValue; |
580 |
Value auxValue2; |
581 |
|
582 |
for (int i = 0; i < numKeys; i++) { |
583 |
try {
|
584 |
auxValue = ValueFactory.createValue(sk[i], className); |
585 |
auxValue2 = ValueFactory.createValue(sv[i], className); |
586 |
|
587 |
ISymbol sym = FSymbol.createFromXML03(xml.getChild(i + |
588 |
useDefaultSymbol)); |
589 |
|
590 |
///addSymbol(auxValue, sym);
|
591 |
symbols.put(auxValue2, sym); |
592 |
keys.add(auxValue); |
593 |
|
594 |
///System.out.println("---set------"+auxValue.toString());
|
595 |
/// System.out.println("set-----------"+sk[i]+"--"+sym.getDescription()+"---"+sym.getColor());
|
596 |
} catch (SemanticException e) {
|
597 |
// TODO Auto-generated catch block
|
598 |
e.printStackTrace(); |
599 |
} |
600 |
} |
601 |
} |
602 |
} |
603 |
|
604 |
/**
|
605 |
* Inserta el XMLEntity.
|
606 |
*
|
607 |
* @param xml XMLEntity.
|
608 |
*/
|
609 |
public void setXMLEntity(XMLEntity xml) { |
610 |
clear(); |
611 |
setFieldName(xml.getStringProperty("fieldName"));
|
612 |
setLabelField(xml.getStringProperty("labelfield"));
|
613 |
|
614 |
if (xml.contains("labelFieldHeight")) { |
615 |
setLabelHeightField(xml.getStringProperty("labelFieldHeight"));
|
616 |
} |
617 |
|
618 |
if (xml.contains("labelFieldRotation")) { |
619 |
setLabelRotationField(xml.getStringProperty("labelFieldRotation"));
|
620 |
} |
621 |
|
622 |
useDefaultSymbol = xml.getBooleanProperty("useDefaultSymbol");
|
623 |
if (xml.contains("sorter")) |
624 |
sorter = xml.getBooleanProperty("sorter");
|
625 |
setDefaultSymbol(FSymbol.createFromXML(xml.getChild(0)));
|
626 |
|
627 |
// FJP: Esto no es necesario ya. Para comprobar si tenemos un campo de altura, miramos
|
628 |
// si getLabelHeightField devuelve null o no.
|
629 |
/* if (xml.contains("isBWithHeightText")) {
|
630 |
setBWithHeightText(xml.getBooleanProperty("isBWithHeightText"));
|
631 |
} */
|
632 |
|
633 |
//addSymbol(new NullUniqueValue(),getDefaultSymbol());
|
634 |
int numKeys = xml.getIntProperty("numKeys"); |
635 |
|
636 |
if (numKeys > 0) { |
637 |
String className = xml.getStringProperty("tipoValueKeys"); |
638 |
String[] sk = xml.getStringArrayProperty("keys"); |
639 |
String[] sv = xml.getStringArrayProperty("values"); |
640 |
Value auxValue = null;
|
641 |
Value auxValue2 = null;
|
642 |
int[] stk=null; |
643 |
if (xml.contains("typeKeys")) { |
644 |
stk = xml.getIntArrayProperty("typeKeys");
|
645 |
int[] stv = xml.getIntArrayProperty("typeValues"); |
646 |
for (int i = 0; i < numKeys; i++) { |
647 |
boolean isDefault = false; |
648 |
if (getValue(sk[i], stk[i]) == null) { |
649 |
isDefault = true;
|
650 |
} |
651 |
|
652 |
if (className
|
653 |
.equals("com.hardcode.gdbms.engine.values.NullUniqueValue")
|
654 |
|| isDefault) { |
655 |
auxValue = new NullUniqueValue();
|
656 |
auxValue2 = ValueFactory.createNullValue(); |
657 |
} else {
|
658 |
auxValue = getValue(sk[i], stk[i]); // ValueFactory.createValue(sk[i],
|
659 |
// className);
|
660 |
auxValue2 = getValue(sv[i], stv[i]); // ValueFactory.createValue(sv[i],
|
661 |
// className);
|
662 |
} |
663 |
ISymbol sym = FSymbol.createFromXML(xml.getChild(i + 1));
|
664 |
symbols.put(auxValue2, sym); |
665 |
keys.add(auxValue); |
666 |
} |
667 |
} else {
|
668 |
for (int i = 0; i < numKeys; i++) { |
669 |
boolean isDefault = false; |
670 |
if (getValue(sk[i]) == null) { |
671 |
isDefault = true;
|
672 |
} |
673 |
if (className
|
674 |
.equals("com.hardcode.gdbms.engine.values.NullUniqueValue")
|
675 |
|| isDefault) { |
676 |
auxValue = new NullUniqueValue();
|
677 |
auxValue2 = ValueFactory.createNullValue(); |
678 |
} else {
|
679 |
auxValue = getValue(sk[i]); // ValueFactory.createValue(sk[i],
|
680 |
// className);
|
681 |
auxValue2 = getValue(sv[i]); // ValueFactory.createValue(sv[i],
|
682 |
// className);
|
683 |
} |
684 |
ISymbol sym = FSymbol.createFromXML(xml.getChild(i + 1));
|
685 |
symbols.put(auxValue2, sym); |
686 |
keys.add(auxValue); |
687 |
} |
688 |
} |
689 |
} |
690 |
} |
691 |
|
692 |
/**
|
693 |
* Devuelve el valor a partir de su valor en un string.
|
694 |
*
|
695 |
* @param s
|
696 |
* String con el valor.
|
697 |
* @deprecated M?todo utilizado hasta la 1.0 alpha 855 Debes utilizar a partir de ahora getValue(String s,int type);
|
698 |
* @return Value.
|
699 |
*/
|
700 |
private Value getValue(String s) { |
701 |
Value val = new NullUniqueValue();
|
702 |
if (s.equals("Resto de Valores"))return val; |
703 |
try {
|
704 |
try {
|
705 |
val = ValueFactory.createValueByType(s, Types.INTEGER);
|
706 |
|
707 |
return val;
|
708 |
} catch (NumberFormatException e) { |
709 |
} |
710 |
|
711 |
try {
|
712 |
val = ValueFactory.createValueByType(s, Types.BIGINT);
|
713 |
|
714 |
return val;
|
715 |
} catch (NumberFormatException e) { |
716 |
} |
717 |
|
718 |
try {
|
719 |
val = ValueFactory.createValueByType(s, Types.FLOAT);
|
720 |
|
721 |
return val;
|
722 |
} catch (NumberFormatException e) { |
723 |
} |
724 |
|
725 |
try {
|
726 |
val = ValueFactory.createValueByType(s, Types.DOUBLE);
|
727 |
|
728 |
return val;
|
729 |
} catch (NumberFormatException e) { |
730 |
} |
731 |
|
732 |
val = ValueFactory.createValueByType(s, Types.LONGVARCHAR);
|
733 |
|
734 |
} catch (ParseException e) { |
735 |
e.printStackTrace(); |
736 |
} |
737 |
|
738 |
return val;
|
739 |
} |
740 |
/**
|
741 |
* Devuelve el valor a partir de su valor en un string.
|
742 |
*
|
743 |
* @param s String con el valor.
|
744 |
*
|
745 |
* @return Value.
|
746 |
*/
|
747 |
private Value getValue(String s,int type) { |
748 |
Value val = new NullUniqueValue();
|
749 |
if (type==Types.OTHER) |
750 |
return val;
|
751 |
try {
|
752 |
val = ValueFactory.createValueByType(s, type); |
753 |
} catch (ParseException e) { |
754 |
e.printStackTrace(); |
755 |
} |
756 |
return val;
|
757 |
} |
758 |
|
759 |
/**
|
760 |
* @see com.iver.cit.gvsig.fmap.rendering.Legend#cloneLegend()
|
761 |
*/
|
762 |
public Legend cloneLegend() throws XMLException { |
763 |
return LegendFactory.createFromXML(getXMLEntity());
|
764 |
} |
765 |
|
766 |
/* (non-Javadoc)
|
767 |
* @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setDataSource(com.hardcode.gdbms.engine.data.DataSource)
|
768 |
*/
|
769 |
public void setDataSource(DataSource ds) |
770 |
throws FieldNotFoundException, DriverException {
|
771 |
try {
|
772 |
dataSource = ds; |
773 |
ds.start(); |
774 |
fieldId = ds.getFieldIndexByName(fieldName); |
775 |
ds.stop(); |
776 |
} catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
|
777 |
throw new DriverException(e); |
778 |
} |
779 |
} |
780 |
|
781 |
/* (non-Javadoc)
|
782 |
* @see com.iver.cit.gvsig.fmap.rendering.UniqueValueLegend#getSymbolByValue(com.hardcode.gdbms.engine.values.Value)
|
783 |
*/
|
784 |
public ISymbol getSymbolByValue(Value key) {
|
785 |
boolean auxSorted=sorter;
|
786 |
sorter=true;
|
787 |
ISymbol symbol=(ISymbol) symbols.get(key); |
788 |
sorter=auxSorted; |
789 |
if (symbol!=null) { |
790 |
return symbol;
|
791 |
} else if (useDefaultSymbol) { |
792 |
return getDefaultSymbol();
|
793 |
} |
794 |
return null; |
795 |
|
796 |
} |
797 |
|
798 |
/* (non-Javadoc)
|
799 |
* @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getShapeType()
|
800 |
*/
|
801 |
public int getShapeType() { |
802 |
return shapeType;
|
803 |
} |
804 |
|
805 |
/* (non-Javadoc)
|
806 |
* @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getLabelHeightField()
|
807 |
*/
|
808 |
public String getLabelHeightField() { |
809 |
return labelFieldHeight;
|
810 |
} |
811 |
|
812 |
/**
|
813 |
* Inserta el alto de campo.
|
814 |
*
|
815 |
* @param str alto.
|
816 |
*/
|
817 |
public void setLabelHeightField(String str) { |
818 |
labelFieldHeight = str; |
819 |
} |
820 |
|
821 |
/* (non-Javadoc)
|
822 |
* @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getLabelRotationField()
|
823 |
*/
|
824 |
public String getLabelRotationField() { |
825 |
return labelFieldRotation;
|
826 |
} |
827 |
|
828 |
/**
|
829 |
* Inserta rotaci?n.
|
830 |
*
|
831 |
* @param str Rotaci?n.
|
832 |
*/
|
833 |
public void setLabelRotationField(String str) { |
834 |
labelFieldRotation = str; |
835 |
} |
836 |
|
837 |
/**
|
838 |
* Introduce si se tiene que representar el resto de valores o no.
|
839 |
*
|
840 |
* @param b True si se utiliza el resto de valores.
|
841 |
*/
|
842 |
public void useDefaultSymbol(boolean b) { |
843 |
useDefaultSymbol = b; |
844 |
} |
845 |
/**
|
846 |
* Devuelve si se utiliza o no el resto de valores para representarse.
|
847 |
*
|
848 |
* @return True si se utiliza el resto de valores.
|
849 |
*/
|
850 |
public boolean isUseDefaultSymbol() { |
851 |
return useDefaultSymbol;
|
852 |
} |
853 |
/**
|
854 |
* Devuelve true si el etiquetado de la capa se ha modificado.
|
855 |
*
|
856 |
* @return True si se ha modificado el etiquetado de la capa.
|
857 |
*/
|
858 |
/* public boolean isBWithHeightText() {
|
859 |
return bWithHeightText;
|
860 |
} */
|
861 |
|
862 |
/**
|
863 |
* Introduce si se ha modificado el etiquetado de la capa.
|
864 |
*
|
865 |
* @param withHeightText True si se ha modificado el etiquetado de la capa.
|
866 |
*/
|
867 |
/* public void setBWithHeightText(boolean withHeightText) {
|
868 |
bWithHeightText = withHeightText;
|
869 |
} */
|
870 |
|
871 |
/**
|
872 |
* Elimina el s?mbolo que tiene como clave el valor que se pasa como par?metro.
|
873 |
*
|
874 |
* @param key clave.
|
875 |
*/
|
876 |
public void delSymbol(Object key) { |
877 |
keys.remove(key); |
878 |
symbols.remove(key); |
879 |
} |
880 |
|
881 |
/* (non-Javadoc)
|
882 |
* @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getUsedFields()
|
883 |
*/
|
884 |
public String[] getUsedFields() { |
885 |
ArrayList usedFields = new ArrayList(); |
886 |
if (getFieldName() != null) |
887 |
usedFields.add(getFieldName()); |
888 |
if (getLabelField() != null) |
889 |
usedFields.add(getLabelField()); |
890 |
if (getLabelHeightField() != null) |
891 |
usedFields.add(getLabelHeightField()); |
892 |
if (getLabelRotationField() != null) |
893 |
usedFields.add(getLabelRotationField()); |
894 |
|
895 |
return (String[]) usedFields.toArray(new String[0]); |
896 |
|
897 |
} |
898 |
|
899 |
public void setSorter(boolean b) { |
900 |
sorter=b; |
901 |
} |
902 |
|
903 |
public boolean isSorter() { |
904 |
return sorter;
|
905 |
} |
906 |
} |