Revision 1031 org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dynobject/impl/DefaultDynField.java

View differences:

DefaultDynField.java
27 27
import java.net.URI;
28 28
import java.net.URL;
29 29
import java.util.Date;
30
import java.util.HashMap;
30 31
import java.util.Iterator;
31 32
import java.util.List;
32 33
import java.util.Map;
......
39 40
import org.gvsig.tools.dataTypes.DataTypesManager;
40 41
import org.gvsig.tools.dynobject.DynClass;
41 42
import org.gvsig.tools.dynobject.DynField;
42
import org.gvsig.tools.dynobject.DynField_LabelAttribute;
43
import org.gvsig.tools.dynobject.DynField_v2;
43 44
import org.gvsig.tools.dynobject.DynObject;
44 45
import org.gvsig.tools.dynobject.DynObjectException;
46
import org.gvsig.tools.dynobject.DynObjectManager;
45 47
import org.gvsig.tools.dynobject.DynObjectValueItem;
46 48
import org.gvsig.tools.dynobject.DynStruct;
47 49
import org.gvsig.tools.dynobject.exception.DynFieldIsNotAContainerException;
......
50 52
import org.gvsig.tools.dynobject.exception.DynObjectValidateException;
51 53
import org.gvsig.tools.exception.ListBaseException;
52 54

  
53
public class DefaultDynField implements DynField, DynField_LabelAttribute {
55
public class DefaultDynField implements DynField_v2 {
54 56
	private String name;
55 57
	private String description;
56 58

  
......
68 70
	private boolean mandatory;
69 71
	private boolean persistent;
70 72
	private Class theClass;
71
	private DynField elementsType;
73
	//private DynField elementsType;
74
	private int elementsType;
75
	
72 76
	private boolean validateElements;
73 77
	private boolean isReadOnly;
74 78
	private Class theClassOfItems = null;
75 79
	private String label = null;
76

  
80
	private DynStruct structWhenTypeIsDynObject = null;
81
	private Map tags = null;
77 82
	public void check() throws ListBaseException {
78 83
		ListBaseException exceptions = null;
79 84

  
......
81 86
			exceptions = CheckDynFieldListException.add(exceptions, name,
82 87
					"name", name);
83 88
		}
89
		if( this.elementsType == DataTypes.DYNOBJECT ) {
90
			if( !this.subtype.equalsIgnoreCase("DynObject") ) {
91
				DynObjectManager mgr = ToolsLocator.getDynObjectManager();
92
				this.structWhenTypeIsDynObject = (DynStruct) mgr.createDynClassName(this.subtype);
93
				this.subtype = "DynObject";
94
			}
95
		}
84 96
		if (exceptions != null) {
85 97
			throw exceptions;
86 98
		}
......
225 237

  
226 238
	public DynField setSubtype(String subtype) {
227 239
		this.subtype = subtype;
240
		/*
228 241
		if (subtype != null && this.dataType.getType() == DataTypes.LIST
229
				&& this.elementsType != null
230
				&& this.elementsType.getType() == DataTypes.DYNOBJECT) {
242
				&& this.structWhenTypeIsDynObject != null ) {
231 243
			if (ToolsLocator.getDynObjectManager().get(subtype) == null) {
232 244
				throw new IllegalArgumentException("DynClass '" + subtype
233 245
						+ "' does not exist.");
234 246
			}
235
			this.elementsType.setSubtype(subtype);
236 247
		} else if (subtype != null
237 248
				&& this.dataType.getType() == DataTypes.DYNOBJECT) {
238 249
			if (ToolsLocator.getDynObjectManager().get(subtype) == null) {
......
240 251
						+ "' does not exist.");
241 252
			}
242 253
		}
254
		*/
243 255
		return this;
244 256
	}
245 257

  
......
256 268
		return defaultValue;
257 269
	}
258 270

  
271
	public DynField setStructWhenTypeIsDynObject(DynStruct dynStruct) {
272
		if( dynStruct==null ) {
273
			this.structWhenTypeIsDynObject = null;
274
			this.theClassOfItems = null;
275
			this.subtype = null;
276
			this.elementsType = DataTypes.UNKNOWN; 
277
		} else {
278
			this.structWhenTypeIsDynObject = dynStruct;
279
			this.theClassOfItems = DynObject.class;
280
			this.subtype = "DynObject";
281
			this.elementsType = DataTypes.DYNOBJECT; 
282
		}
283
		return this;
284
	}
285
	
286
	public DynStruct getStructWhenTypeIsDynObject() {
287
		return this.structWhenTypeIsDynObject;
288
	}
289
	
259 290
	public DynField setAvailableValues(DynObjectValueItem[] availableValues) {
260
		// If type is a list then the available values should be at the elements
261
		// type, not here.
262
		if (this.getType() == DataTypes.LIST) {
263
			if (this.getElementsType() != null) {
264
				this.getElementsType().setAvailableValues(availableValues);
265
				return this;
266
			}
267
		}
268 291
		if (availableValues == null || availableValues.length == 0) {
269 292
			this.availableValues = null;
270 293
		} else {
......
274 297
	}
275 298

  
276 299
	public DynField setAvailableValues(List availableValues) {
277
		// If type is a list then the available values should be at the elements
278
		// type, not here.
279
		if (this.getType() == DataTypes.LIST) {
280
			if (this.getElementsType() != null) {
281
				this.getElementsType().setAvailableValues(availableValues);
282
				return this;
283
			}
284
		}
285 300
		if (availableValues == null) {
286 301
			this.availableValues = null;
287 302
		} else if (availableValues.isEmpty()) {
......
386 401
		return this;
387 402
	}
388 403

  
389
	public DynField setElementsType(int type)
390
			throws DynFieldIsNotAContainerException {
391
		if (!isContainer()) {
392
			throw new DynFieldIsNotAContainerException(this.name);
393
		}
394
		this.elementsType = new DefaultDynField(this.name + "-"
395
				+ this.dataType.getName() + "-item", type);
396
		return this;
397
	}
398 404

  
399
	public DynField setElementsType(DynStruct type)
400
			throws DynFieldIsNotAContainerException {
401
		// Getter allows null values
402
		if (type != null) {
403
			this.setElementsType(DataTypes.DYNOBJECT).getElementsType()
404
					.setSubtype(type.getFullName());
405
		}
406
		return this;
407
	}
408 405

  
406
	
409 407
	public boolean isContainer() {
410 408
		return this.dataType.isContainer();
411 409
	}
412 410

  
413
	public DynField getElementsType() {
414
		return this.elementsType;
415
	}
416

  
417 411
	public void validate(Object value) throws DynFieldValidateException {
418 412
		Comparable v;
419 413
		if (value == null) {
......
497 491
			if (!(value instanceof List)) {
498 492
				throw new DynFieldValidateException(value, this);
499 493
			}
494
			/*
500 495
			if (this.validateElements && this.elementsType != null) {
501 496
				Iterator it = ((List) value).iterator();
502 497
				while (it.hasNext()) {
503 498
					this.elementsType.validate(it.next());
504 499
				}
505 500
			}
501
			*/
506 502
			break;
507 503

  
508 504
		case DataTypes.MAP:
......
679 675
		this.theClassOfItems = theClass;
680 676
		return this;
681 677
	}
678
	
679
	public Object getTag(String name) {
680
		if(this.tags == null){
681
			return null;
682
		}
683
		return this.tags.get(name);
684
	}
685
	
686
	public Object getTag(String name, int type) throws CoercionException {
687
		if(this.tags == null){
688
			return null;
689
		}
690
		DataTypesManager dataman = ToolsLocator.getDataTypesManager();
691
		return dataman.coerce(type, this.tags.get(name));
692
	}
693
	
694
	public DynField setTag(String name, Object value) {
695
		if(this.tags == null){
696
			this.tags  = new HashMap();
697
		}
698
		this.tags.put(name, value);
699
		return this;
700
	}
701
 
702
	public boolean hasTag(String name) {
703
		if(this.tags == null){
704
			return false;
705
		}		
706
		return this.tags.containsKey(name);
707
	}
708
	
709
	public int getTagInt(String name) throws CoercionException {
710
		Object x = this.getTag(name,DataTypes.INT);
711
		if( x==null ) {
712
			return 0;
713
		}
714
		return ((Integer)x).intValue();
715
	}
716
	
717
	public Iterator getTagKeys() {
718
		if(this.tags == null){
719
			return null;
720
		}
721
		return this.tags.keySet().iterator();
722
	}
723
	
724
	public DynField setElementsType(int type) {
725
		this.elementsType = type;
726
		return this;
727
	}
682 728

  
729
	public DynField setElementsType(DynStruct type)
730
			throws DynFieldIsNotAContainerException {
731
		this.setStructWhenTypeIsDynObject(type);
732
		return this;
733
	}
734

  
735
	public DynField getElementsType() {
736
		return null;
737
	}
738
	
683 739
}

Also available in: Unified diff