Revision 627

View differences:

org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dynobject/impl/DefaultDynField.java
47 47
	private DynField elementsType;
48 48
	private boolean validateElements;
49 49
	private boolean isReadOnly;
50
	private Class theClassOfItems=null;
50
	private Class theClassOfItems = null;
51 51

  
52 52
	public void check() throws ListBaseException {
53 53
		ListBaseException exceptions = null;
54
		
55
		if( name == null ) {
56
			exceptions = CheckDynFieldListException.add(exceptions, name, "name", name);
54

  
55
		if (name == null) {
56
			exceptions = CheckDynFieldListException.add(exceptions, name,
57
					"name", name);
57 58
		}
58
		if( exceptions!= null ) {
59
		if (exceptions != null) {
59 60
			throw exceptions;
60 61
		}
61 62
	}
62
	
63
	public static class CheckDynFieldListException extends ListBaseException {  
64
	
63

  
64
	public static class CheckDynFieldListException extends ListBaseException {
65

  
65 66
		public static class CheckDynFieldException extends DynObjectException {
66
			
67

  
67 68
			/**
68 69
			 * 
69 70
			 */
70 71
			private static final long serialVersionUID = 2486744641818117262L;
71 72

  
72 73
			public CheckDynFieldException(String attrname, Object attrvalue) {
73
				super(
74
						"Wrong value %(value) for attribute %(name).",
74
				super("Wrong value %(value) for attribute %(name).",
75 75
						"Wrong_value_XvalueX_for_attribute_XnameX",
76
						serialVersionUID
77
				);
76
						serialVersionUID);
78 77
			}
79 78
		}
80
		
79

  
81 80
		/**
82 81
		 * 
83 82
		 */
84 83
		private static final long serialVersionUID = 1L;
85 84

  
86 85
		public CheckDynFieldListException(String name) {
87
			super(
88
				"Inconsistent field %(name) definition.",
89
				"_Inconsistent_field_XnameX_definition",
90
				serialVersionUID
91
			);
92
			if( name == null ) {
86
			super("Inconsistent field %(name) definition.",
87
					"_Inconsistent_field_XnameX_definition", serialVersionUID);
88
			if (name == null) {
93 89
				name = "[unknow]";
94 90
			}
95
			setValue("name",name);
91
			setValue("name", name);
96 92
		}
97
		
98
		public static ListBaseException add(ListBaseException exceptions, String name, String attrname, Object attrvalue) {
99
			if( exceptions == null ) {
93

  
94
		public static ListBaseException add(ListBaseException exceptions,
95
				String name, String attrname, Object attrvalue) {
96
			if (exceptions == null) {
100 97
				exceptions = new CheckDynFieldListException(name);
101 98
			}
102
			exceptions.add( new CheckDynFieldException(attrname, attrvalue) );
99
			exceptions.add(new CheckDynFieldException(attrname, attrvalue));
103 100
			return exceptions;
104 101
		}
105 102
	}
106
	
103

  
107 104
	public DefaultDynField(String name) {
108 105
		this(name, // field name
109 106
				DataTypes.STRING, // data type
......
112 109
				false // mandatory
113 110
		);
114 111
	}
115
	
112

  
116 113
	private DefaultDynField(String name, int dataType) {
117 114
		this(name, // field name
118 115
				dataType, // data type
......
143 140
	public String toString() {
144 141
		StringBuffer buffer = new StringBuffer();
145 142

  
146
		buffer.append("DynField").append("[").append(this.hashCode()).append("]")
147
		        .append("( ")
148
		        .append("name='").append(this.name).append("', ")
149
				.append("description='").append(this.description).append("', ")
150
                .append("type='").append(this.dataType.getName()).append("', ")
151
                .append("subType='").append(this.subtype).append("', ")
152
                .append("mandatory='").append(this.isMandatory()).append("', ")
153
                .append("defaultValue='").append(this.getDefaultValue()).append("', ")
154
                .append("dataType=[").append(this.dataType).append("], ")
143
		buffer.append("DynField").append("[").append(this.hashCode())
144
				.append("]").append("( ").append("name='").append(this.name)
145
				.append("', ").append("description='").append(this.description)
146
				.append("', ").append("type='").append(this.dataType.getName())
147
				.append("', ").append("subType='").append(this.subtype)
148
				.append("', ").append("mandatory='").append(this.isMandatory())
149
				.append("', ").append("defaultValue='")
150
				.append(this.getDefaultValue()).append("', ")
151
				.append("dataType=[").append(this.dataType).append("], ")
155 152
				.append("minValue='").append(this.minValue).append("', ")
156 153
				.append("maxValue='").append(this.maxValue).append("', ")
157 154
				.append("persistent='").append(this.isPersistent())
......
174 171

  
175 172
	public DynField setType(int dataType) {
176 173
		DataTypesManager datamanager = ToolsLocator.getDataTypesManager();
177
		return setType( datamanager.get(dataType) );
174
		return setType(datamanager.get(dataType));
178 175
	}
179 176

  
180 177
	public DynField setType(DataType dataType) {
......
191 188
	public DataType getDataType() {
192 189
		return this.dataType;
193 190
	}
194
	
191

  
195 192
	public DynField setSubtype(String subtype) {
196 193
		this.subtype = subtype;
197 194
		if (subtype != null && this.dataType.getType() == DataTypes.LIST
198 195
				&& this.elementsType != null
199 196
				&& this.elementsType.getType() == DataTypes.DYNOBJECT) {
200
		    if( ToolsLocator.getDynObjectManager().get(subtype) == null ) {
201
                throw new IllegalArgumentException("DynClass '" + subtype + "' does not exist.");
202
            }
203
		    this.elementsType.setSubtype(subtype);
197
			if (ToolsLocator.getDynObjectManager().get(subtype) == null) {
198
				throw new IllegalArgumentException("DynClass '" + subtype
199
						+ "' does not exist.");
200
			}
201
			this.elementsType.setSubtype(subtype);
204 202
		} else if (subtype != null
205 203
				&& this.dataType.getType() == DataTypes.DYNOBJECT) {
206 204
			if (ToolsLocator.getDynObjectManager().get(subtype) == null) {
......
225 223
	}
226 224

  
227 225
	public DynField setAvailableValues(DynObjectValueItem[] availableValues) {
226
		// If type is a list then the available values should be at the elements
227
		// type, not here.
228
		if (this.getType() == DataTypes.LIST) {
229
			if (this.getElementsType() != null) {
230
				this.getElementsType().setAvailableValues(availableValues);
231
				return this;
232
			}
233
		}
228 234
		if (availableValues == null || availableValues.length == 0) {
229 235
			this.availableValues = null;
230 236
		} else {
......
234 240
	}
235 241

  
236 242
	public DynField setAvailableValues(List availableValues) {
237
		if( availableValues == null ) {
243
		// If type is a list then the available values should be at the elements
244
		// type, not here.
245
		if (this.getType() == DataTypes.LIST) {
246
			if (this.getElementsType() != null) {
247
				this.getElementsType().setAvailableValues(availableValues);
248
				return this;
249
			}
250
		}
251
		if (availableValues == null) {
238 252
			this.availableValues = null;
239
		} else if( availableValues.isEmpty() ) {
253
		} else if (availableValues.isEmpty()) {
240 254
			this.availableValues = null;
241 255
		} else {
242 256
			this.availableValues = (DynObjectValueItem[]) availableValues
......
309 323
	}
310 324

  
311 325
	public DynField setValidateElements(boolean validate) {
312
		if ( !this.dataType.isContainer()) {
326
		if (!this.dataType.isContainer()) {
313 327
			throw new DynFieldIsNotAContainerException(this.name);
314 328
		}
315 329
		this.validateElements = validate;
......
332 346
			throw new DynFieldIsNotAContainerException(this.name);
333 347
		}
334 348
		this.elementsType = new DefaultDynField(this.name + "-"
335
				+ this.dataType.getName()
336
				+ "-item",type);
349
				+ this.dataType.getName() + "-item", type);
337 350
		return this;
338 351
	}
339 352

  
340

  
341 353
	public DynField setElementsType(DynStruct type)
342 354
			throws DynFieldIsNotAContainerException {
343 355
		// Getter allows null values
344 356
		if (type != null) {
345
			this.setElementsType(DataTypes.DYNOBJECT).getElementsType().setSubtype(type.getFullName());
357
			this.setElementsType(DataTypes.DYNOBJECT).getElementsType()
358
					.setSubtype(type.getFullName());
346 359
		}
347 360
		return this;
348 361
	}
349 362

  
350
	public boolean isContainer(){
351
	    return this.dataType.isContainer();
363
	public boolean isContainer() {
364
		return this.dataType.isContainer();
352 365
	}
353 366

  
354 367
	public DynField getElementsType() {
......
425 438
			}
426 439
			break;
427 440
		case DataTypes.FILE:
428
		    if (!(value instanceof File)) {
429
		    	throw new DynFieldValidateException(value, this);
430
		    }
431
		    break;
441
			if (!(value instanceof File)) {
442
				throw new DynFieldValidateException(value, this);
443
			}
444
			break;
432 445
		case DataTypes.FOLDER:
433
		    if (!(value instanceof File)) {
434
		    	throw new DynFieldValidateException(value, this);
435
		    }
436
		    break;
446
			if (!(value instanceof File)) {
447
				throw new DynFieldValidateException(value, this);
448
			}
449
			break;
437 450
		case DataTypes.URI:
438
		    if (!(value instanceof URI)) {
439
		    	throw new DynFieldValidateException(value, this);
440
		    }
441
		    break;
451
			if (!(value instanceof URI)) {
452
				throw new DynFieldValidateException(value, this);
453
			}
454
			break;
442 455
		case DataTypes.URL:
443
		    if (!(value instanceof URL)) {
444
		    	throw new DynFieldValidateException(value, this);
445
		    }
446
		    break;
456
			if (!(value instanceof URL)) {
457
				throw new DynFieldValidateException(value, this);
458
			}
459
			break;
447 460
		case DataTypes.SET:
448 461
			if (!(value instanceof Set)) {
449 462
				throw new DynFieldValidateException(value, this);
......
456 469
			}
457 470
			DynClass dynClass = ToolsLocator.getDynObjectManager().get(
458 471
					this.getSubtype());
459
			if ( dynClass==null || !dynClass.isInstance((DynObject) value)) {
472
			if (dynClass == null || !dynClass.isInstance((DynObject) value)) {
460 473
				throw new DynFieldValidateException(value, this);
461 474
			}
462 475
			try {
......
471 484
			break;
472 485

  
473 486
		default:
474
			if( this.dataType.isObject() ) {
487
			if (this.dataType.isObject()) {
475 488
				if (this.theClass != null) {
476 489
					if (!this.theClass.isInstance(value)) {
477 490
						throw new DynFieldValidateException(value, this);
......
507 520
				throw new DynFieldValidateException(value, this);
508 521
			}
509 522
		}
510
//
511
//		This shouldn't be necessary since any assignment passes through the coerce function anyway
512
//
513
//		//if all the above is correct, then we should check that coercing is possible
514
//		try {
515
//			coerce(value);
516
//		} catch (CoercionException e) {
517
//			throw new DynFieldValidateException(value, this);
518
//		}
519
		
523
		//
524
		// This shouldn't be necessary since any assignment passes through the
525
		// coerce function anyway
526
		//
527
		// //if all the above is correct, then we should check that coercing is
528
		// possible
529
		// try {
530
		// coerce(value);
531
		// } catch (CoercionException e) {
532
		// throw new DynFieldValidateException(value, this);
533
		// }
534

  
520 535
	}
521 536

  
522 537
	public Object coerce(Object value) throws CoercionException {
......
526 541
		return this.dataType.coerce(value);
527 542
	}
528 543

  
529

  
530 544
	public String getGroup() {
531 545
		return this.groupName;
532 546
	}
......
553 567
		this.hidden = hidden;
554 568
		return this;
555 569
	}
556
	
570

  
557 571
	public boolean isReadOnly() {
558
	    return this.isReadOnly;
572
		return this.isReadOnly;
559 573
	}
560 574

  
561
	
562 575
	public DynField setReadOnly(boolean isReadOnly) {
563
	    this.isReadOnly = isReadOnly;
564
	    return this;
576
		this.isReadOnly = isReadOnly;
577
		return this;
565 578
	}
566 579

  
567 580
	public Class getClassOfItems() {
......
579 592
		if (theClass == null) {
580 593
			return this;
581 594
		}
582
		if (! this.dataType.isContainer() ) {
595
		if (!this.dataType.isContainer()) {
583 596
			throw new DynFieldIsNotAContainerException(this.name);
584 597
		}
585 598
		this.theClassOfItems = theClass;
586 599
		return this;
587 600
	}
588 601

  
589

  
590 602
}

Also available in: Unified diff