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