Revision 21144 branches/v2_0_0_prep/libraries/libFMap/src/org/gvsig/fmap/mapcontext/rendering/legend/VectorialUniqueValueLegend.java

View differences:

VectorialUniqueValueLegend.java
50 50
import org.apache.log4j.Logger;
51 51
import org.gvsig.data.ReadException;
52 52
import org.gvsig.data.vectorial.Feature;
53
import org.gvsig.data.vectorial.FeatureStore;
54
import org.gvsig.data.vectorial.FeatureType;
53 55
import org.gvsig.fmap.mapcontext.layers.XMLException;
54 56
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
55 57
import org.gvsig.fmap.mapcontext.rendering.legend.events.LegendClearEvent;
......
68 70
public class VectorialUniqueValueLegend extends AbstractClassifiedVectorLegend implements IVectorialUniqueValueLegend {
69 71
	private static final Logger log = Logger.getLogger(VectorialUniqueValueLegend.class);
70 72
	protected int fieldId;
71
	protected DataSource dataSource;
72

  
73
//	protected DataSource dataSource;
74
	protected FeatureStore featureStore;
73 75
	private TreeMap<Object, ISymbol> symbols = new TreeMap<Object, ISymbol>(
74 76
			new Comparator<Object>() {
75 77
		public int compare(Object o1, Object o2) {
......
146 148
     * @param id
147 149
     * @param symbol
148 150
     */
149
    public void setValueSymbol(int id, ISymbol symbol) {
150
    	ISymbol old = symbols.put(keys.get(id), symbol);
151
        fireClassifiedSymbolChangeEvent(new SymbolLegendEvent(old, symbol));
152
    }
151
//    public void setValueSymbol(int id, ISymbol symbol) {
152
//    	ISymbol old = symbols.put(keys.get(id), symbol);
153
//        fireClassifiedSymbolChangeEvent(new SymbolLegendEvent(old, symbol));
154
//    }
153 155

  
154 156
    public Object[] getValues() {
155 157
        return symbols.keySet().toArray(new Object[0]);
......
200 202
        return symbols.values().toArray(new ISymbol[0]);
201 203
    }
202 204

  
203
     @Override
204
     public void setClassifyingFieldNames(String[] fNames) {
205
    public void setClassifyingFieldNames(String[] fNames) {
205 206
    	 super.setClassifyingFieldNames(fNames);
206 207
    	 try {
207
    		 fieldId = dataSource.getFieldIndexByName(getClassifyingFieldNames()[0]);
208
    		 fieldId = ((FeatureType)featureStore.getFeatureTypes().get(0)).getFieldIndex(getClassifyingFieldNames()[0]);
208 209
    	 } catch (NullPointerException e) {
209 210
    		 log.warn("data source not set");
210
    	 } catch (ReadException e) {
211
    		 log.warn("failed setting field id");
212 211
    	 }
213 212
     }
214 213
    /*
215 214
     * @see com.iver.cit.gvsig.fmap.rendering.IVectorialLegend#getSymbol(int)
216 215
     */
217
    public ISymbol getSymbol(int recordIndex) throws ReadException {
218
		Object val = dataSource.getFieldValue(recordIndex, fieldId);
219
		ISymbol theSymbol = getSymbolByValue(val);
216
//    public ISymbol getSymbol(int recordIndex) throws ReadException {
217
//		Object val = dataSource.getFieldValue(recordIndex, fieldId);
218
//		ISymbol theSymbol = getSymbolByValue(val);
219
//
220
//		return theSymbol;
221
//	}
220 222

  
221
		return theSymbol;
222
	}
223

  
224 223
    /**
225 224
	 * Devuelve un s?mbolo a partir de una IFeature. OJO!! Cuando usamos un
226 225
	 * feature iterator de base de datos el ?nico campo que vendr? rellenado es
......
232 231
	 * @return S?mbolo.
233 232
	 */
234 233
    public ISymbol getSymbolByFeature(Feature feat) {
235
        Object val = feat.getAttribute(FLyrVect.forTestOnlyVariableUseIterators_REMOVE_THIS_FIELD
236
        		? 0 :fieldId);
234
        Object val = feat.get(fieldId);
237 235
        ISymbol theSymbol = getSymbolByValue(val);
238 236

  
239 237
        if (theSymbol != null) {
......
296 294
            	}else {
297 295
            		sv[i] = (values[i]).toString();
298 296
            	}
299
            	stk[i]= keys.get(i).getSQLType();
300
            	stv[i]= (values[i]).getSQLType();
297
            	stk[i]=getSQLType(keys.get(i));
298
            	stv[i]=getSQLType(values[i]);
299
//            	stk[i]= keys.get(i).getSQLType();
300
//            	stv[i]= (values[i]).getSQLType();
301 301
                xml.addChild(fsymbols[i].getXMLEntity());
302 302

  
303 303
                ///System.out.println("get-----------"+sk[i]+"--"+fsymbols[i].getDescription()+"---"+fsymbols[i].getColor());
......
317 317
        return xml;
318 318
    }
319 319

  
320
    public void setXMLEntity03(XMLEntity xml) {
321
        clear();
322
        setClassifyingFieldNames(new String[] {xml.getStringProperty("fieldName")});
320
    private int getSQLType(Object object) {
321
		if (object instanceof Integer){
322
			return Types.INTEGER;
323
		}else if (object instanceof Long){
324
			return Types.BIGINT;
325
		}else if (object instanceof Float){
326
			return Types.FLOAT;
327
		}else if (object instanceof Double){
328
			return Types.DOUBLE;
329
		}
330
    	return Types.LONGVARCHAR;
331
	}
323 332

  
324
        int useDefaultSymbol = xml.getIntProperty("useDefaultSymbol");
325

  
326
        if (useDefaultSymbol == 1) {
327
            setDefaultSymbol(FSymbol.createFromXML03(xml.getChild(0)));
328
        } else {
329
            setDefaultSymbol(null);
330
        }
331

  
332
        int numKeys = xml.getIntProperty("numKeys");
333

  
334
        if (numKeys > 0) {
335
            String className = xml.getStringProperty("tipoValueKeys");
336
            String[] sk = xml.getStringArrayProperty("keys");
337
            String[] sv = xml.getStringArrayProperty("values");
338
            Object auxValue;
339
            Object auxValue2;
340

  
341
            for (int i = 0; i < numKeys; i++) {
342
                try {
343
                    auxValue = ValueFactory.createValue(sk[i], className);
344
                    auxValue2 = ValueFactory.createValue(sv[i], className);
345

  
346
                    ISymbol sym = FSymbol.createFromXML03(xml.getChild(i +
347
                                useDefaultSymbol));
348

  
349
                    symbols.put(auxValue2, sym);
350
                    keys.add(auxValue);
351

  
352
                } catch (SemanticException e) {
353
                	log.error("Exception", e);
354
                    e.printStackTrace();
355
                }
356
            }
357
        }
358
    }
359

  
360
    public void setXMLEntity(XMLEntity xml) {
333
	public void setXMLEntity(XMLEntity xml) {
361 334
        clear();
362 335
        if (xml.contains("fieldName"))
363 336
        	setClassifyingFieldNames(new String[] {xml.getStringProperty("fieldName")});
......
392 365
							.equals("com.hardcode.gdbms.engine.values.NullUniqueValue")
393 366
							|| isDefault) {
394 367
						auxValue = new NullUniqueValue();
395
						auxValue2 = ValueFactory.createNullValue();
368
						auxValue2 = new NullValue();
396 369
					} else {
397 370
						auxValue = getValue(sk[i], stk[i]); // ValueFactory.createValue(sk[i],
398 371
															// className);
......
415 388
							.equals("com.hardcode.gdbms.engine.values.NullUniqueValue")
416 389
							|| isDefault) {
417 390
						auxValue = new NullUniqueValue();
418
						auxValue2 = ValueFactory.createNullValue();
391
						auxValue2 = new NullValue();
419 392
					} else {
420 393
						auxValue = getValue(sk[i]); // ValueFactory.createValue(sk[i],
421 394
													// className);
......
457 430
    private Object getValue(String s) {
458 431
        Object val = new NullUniqueValue();
459 432
        if (s.equals("Resto de Valores"))return val;
460
        try {
433
//        try {
461 434
            try {
462
                val = ValueFactory.createValueByType(s, Types.INTEGER);
435
                val = new Integer(s);//(s, Types.INTEGER);
463 436

  
464 437
                return val;
465 438
            } catch (NumberFormatException e) {
466 439
            }
467 440

  
468 441
            try {
469
                val = ValueFactory.createValueByType(s, Types.BIGINT);
442
                val = new Long(s);//ValueFactory.createValueByType(s, Types.BIGINT);
470 443

  
471 444
                return val;
472 445
            } catch (NumberFormatException e) {
473 446
            }
474 447

  
475 448
            try {
476
                val = ValueFactory.createValueByType(s, Types.FLOAT);
449
                val = new Float(s);//ValueFactory.createValueByType(s, Types.FLOAT);
477 450

  
478 451
                return val;
479 452
            } catch (NumberFormatException e) {
480 453
            }
481 454

  
482 455
            try {
483
                val = ValueFactory.createValueByType(s, Types.DOUBLE);
456
                val = new Double(s);//ValueFactory.createValueByType(s, Types.DOUBLE);
484 457

  
485 458
                return val;
486 459
            } catch (NumberFormatException e) {
487 460
            }
488 461

  
489
            val = ValueFactory.createValueByType(s, Types.LONGVARCHAR);
462
            val = s;//ValueFactory.createValueByType(s, Types.LONGVARCHAR);
490 463

  
491
        } catch (ParseException e) {
492
           log.warn("parse exception", e);
493
        }
464
//        } catch (ParseException e) {
465
//           log.warn("parse exception", e);
466
//        }
494 467

  
495 468
        return val;
496 469
    }
......
503 476
     * @return Value.
504 477
     */
505 478
    private Object getValue(String s,int type) {
506
        Object val = new NullUniqueValue();
507
        if (type==Types.OTHER)
508
        	return val;
509
        try {
510
        	val = ValueFactory.createValueByType(s, type);
511
        } catch (ParseException e) {
512
            e.printStackTrace();
513
        }
514
        return val;
479
    	Object val = new NullUniqueValue();
480
    	if (type==Types.OTHER)
481
    		return val;
482
    	switch (type) {
483
    	case Types.INTEGER:
484
    		val = new Integer(s);
485
    		break;
486
    	case Types.BIGINT:
487
    		val = new Long(s);
488
    		break;
489
    	case Types.FLOAT:
490
    		val = new Float(s);
491
    		break;
492
    	case Types.DOUBLE:
493
    		val = new Double(s);
494
    		break;
495
    	default:
496
    		val=s;
497
    	break;
498
    	}
499
    	return val;
515 500
    }
516 501

  
517 502
    public ILegend cloneLegend() throws XMLException {
......
522 507
    /* (non-Javadoc)
523 508
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setDataSource(com.hardcode.gdbms.engine.data.DataSource)
524 509
     */
525
    public void setDataSource(DataSource ds) throws FieldNotFoundException,
526
			ReadException {
527
		dataSource = ds;
528
		ds.start();
529
		fieldId = ds.getFieldIndexByName(getClassifyingFieldNames()[0]);
530
		ds.stop();
510
    public void setFeatureStore(FeatureStore fs) throws	ReadException {
511
		featureStore = fs;
512
		fieldId = ((FeatureType)fs.getFeatureTypes().get(0)).getFieldIndex(getClassifyingFieldNames()[0]);
531 513
	}
532 514

  
533 515
    /*

Also available in: Unified diff