Revision 20599 trunk/libraries/libDataSource/src/org/gvsig/data/vectorial/Feature.java

View differences:

Feature.java
1 1
package org.gvsig.data.vectorial;
2 2

  
3
import java.util.ArrayList;
3
import java.text.ParseException;
4 4
import java.util.Date;
5 5
import java.util.Iterator;
6 6

  
7 7
import org.gvsig.data.exception.DataException;
8
import org.gvsig.data.exception.InitializeException;
9
import org.gvsig.data.exception.ReadException;
8 10

  
9
public abstract class Feature extends ArrayList implements IFeature{
11
public abstract class Feature implements IFeature{
10 12
	protected IFeatureType featureType;
11 13
	protected Object defaultGeometry;
12 14
	protected Object object;
13 15
	private boolean editing = false;
14 16
	private boolean loading = false;
15 17
	private IFeature newFeature;
18
	protected Object[] values;
16 19

  
17 20
	protected abstract IFeature cloneFeature() throws DataException;
18 21

  
19
	public Feature(IFeatureType featureType){
20
		super(featureType.getCount());
22
	public Feature(IFeatureType featureType) throws ReadException{
23
		this.init(featureType, false);
24
	}
25

  
26
	public Feature(IFeatureType featureType,boolean loadDefaultValues) throws ReadException{
27
		this.init(featureType, loadDefaultValues);
28
	}
29

  
30
	protected void init(IFeatureType featureType,boolean loadDefaultValues)throws InitializeException{
31
		this.values = new Object[featureType.getCount()];
21 32
		this.featureType=featureType;
22
		for (int i = 0; i < featureType.getCount()+1; i++) {
23
			add(null);
33
		if (loadDefaultValues){
34
			try {
35
				this.loadDefaultValues();
36
			} catch (Exception e) {
37
				throw new InitializeException("Feature",e);
38
			}
24 39
		}
40

  
25 41
	}
42

  
43
	public int size() {
44
		return this.getType().size();
45
	}
46

  
26 47
	public IFeatureType getType() {
27 48
		return featureType;
28 49
	}
......
85 106
		return value;
86 107
	}
87 108
	public Object get(int index) {
88
		if (this.isEditing())
109
		if (this.isEditing() && this.newFeature != null)
89 110
			this.newFeature.get(index);
90
		return super.get(index);
111
		return this.values[index];
91 112
	}
92 113

  
93 114
	public IFeature getFeature(int index) {
......
146 167

  
147 168
	public void set(String name, Object value) throws IsNotFeatureSettingException {
148 169
		int i=featureType.getFieldIndex(name);
149
		this.setObject(i, value);
170
		this.set(i, value);
150 171
	}
151 172

  
152 173
	public void set(String name, int value) throws IsNotFeatureSettingException {
153 174
		int i=featureType.getFieldIndex(name);
154
		this.setObject(i, new Integer(value));
175
		this.set(i, new Integer(value));
155 176
	}
156 177

  
157 178
	public void set(String name, boolean value) throws IsNotFeatureSettingException {
158 179
		int i=featureType.getFieldIndex(name);
159
		this.setObject(i,new Boolean(value));
180
		this.set(i,new Boolean(value));
160 181
	}
161 182

  
162 183
	public void set(String name, long value) throws IsNotFeatureSettingException {
163 184
		int i=featureType.getFieldIndex(name);
164
		this.setObject(i,new Long(value));
185
		this.set(i,new Long(value));
165 186
	}
166 187

  
167 188
	public void set(String name, float value) throws IsNotFeatureSettingException {
168 189
		int i=featureType.getFieldIndex(name);
169
		this.setObject(i,new Float(value));
190
		this.set(i,new Float(value));
170 191
	}
171 192

  
172 193
	public void set(String name, double value) throws IsNotFeatureSettingException {
173 194
		int i=featureType.getFieldIndex(name);
174
		this.setObject(i,new Double(value));
195
		this.set(i,new Double(value));
175 196
	}
176 197

  
177
	public void setObject(int index, Object value) throws IsNotFeatureSettingException {
198
	public void set(int index, Object value) throws IsNotFeatureSettingException {
178 199
		canSetValue(index);
179
		if (newFeature!=null){
200
		if ( this.isEditing() && newFeature!=null){
180 201
			((Feature)newFeature).loading();
181
			newFeature.setObject(index,value);
202
			newFeature.set(index,value);
182 203
			((Feature)newFeature).stopLoading();
183 204
		}else
184
			super.set(index,value);
205
			this.values[index] =value;
185 206

  
186 207
	}
187 208

  
188 209
	public void set(int index, int value) throws IsNotFeatureSettingException {
189
		this.setObject(index,new Integer(value));
210
		this.set(index,new Integer(value));
190 211
	}
191 212

  
192 213
	public void set(int index, boolean value) throws IsNotFeatureSettingException {
193
		this.setObject(index,new Boolean(value));
214
		this.set(index,new Boolean(value));
194 215
	}
195 216

  
196 217
	public void set(int index, long value) throws IsNotFeatureSettingException {
197
		this.setObject(index,new Long(value));
218
		this.set(index,new Long(value));
198 219
	}
199 220

  
200 221
	public void set(int index, float value) throws IsNotFeatureSettingException {
201
		this.setObject(index,new Float(value));
222
		this.set(index,new Float(value));
202 223
	}
203 224

  
204 225
	public void set(int index, double value) throws IsNotFeatureSettingException {
205
		this.setObject(index,new Double(value));
226
		this.set(index,new Double(value));
206 227
	}
207 228

  
208
	public Object set(int index, Object value) {
209
		//FIXME!! Esto tendr?a que tener igual que los otros 'set'
210

  
211
		try {
212
			this.setObject(index,value);
213
		} catch (IsNotFeatureSettingException e) {
214
			new RuntimeException(e);
215
		}
216
		return null;
217
	}
218

  
219 229
	public void validateEnd(IFeatureStore featureStore) {
220 230
		getType().validateFeatureEnd(this,featureStore);
221 231

  
......
230 240
	}
231 241
	public IFeature getFeature(String name) {
232 242
		int i=featureType.getFieldIndex(name);
233
		return (IFeature)super.get(i);
243
		return (IFeature)this.values[i];
234 244
	}
235 245
	public Object getGeometry(int index) {
236 246
		return this.get(index);
......
253 263
		return value.byteValue();
254 264
	}
255 265
	public void set(int index, byte value) throws IsNotFeatureSettingException {
256
		this.setObject(index,new Byte(value));
266
		this.set(index,new Byte(value));
257 267
	}
258 268
	public void set(int index, String value) throws IsNotFeatureSettingException {
259
		this.setObject(index,value);
269
		this.set(index,(Object)value);
260 270
	}
261 271
	public void set(String name, byte value) throws IsNotFeatureSettingException {
262 272
		int i=featureType.getFieldIndex(name);
263
		this.setObject(i,new Byte(value));
273
		this.set(i,new Byte(value));
264 274
	}
265 275
	public void setGeometry(String name,Object geometry) throws IsNotFeatureSettingException {
266 276
		int i=featureType.getFieldIndex(name);
......
270 280
			newFeature.setGeometry(name,geometry);
271 281
			((Feature)newFeature).stopLoading();
272 282
		}else{
273
			super.set(i,geometry);
283
			this.values[i]=geometry;
274 284
			if (featureType.getDefaultGeometry().equals(name)){
275 285
				defaultGeometry=geometry;
276 286
			}
......
283 293
			newFeature.setGeometry(index,geometry);
284 294
			((Feature)newFeature).stopLoading();
285 295
		}else{
286
			super.set(index,geometry);
296
			this.values[index] =geometry;
287 297
			Iterator iterator=featureType.iterator();
288 298
			while (iterator.hasNext()) {
289 299
				IFeatureAttributeDescriptor fad = (IFeatureAttributeDescriptor) iterator.next();
......
316 326
	protected boolean isEditing(){
317 327
		return editing;
318 328
	}
319
	IFeature getNewFeature() {
329
	protected IFeature getNewFeature() {
320 330
		return newFeature;
321 331
	}
322 332
	protected void loading(){
......
335 345
			IFeatureAttributeDescriptor desc = (IFeatureAttributeDescriptor)iter.next();
336 346
			if (desc.isEvaluated()){
337 347
				Object value = desc.getEvaluator().evalue(store, this, desc);
338
				super.set(desc.ordinal(),value);
348
				this.values[desc.ordinal()] = value;
339 349
				if (desc.getDataType().equals(IFeatureAttributeDescriptor.TYPE_GEOMETRY)){
340 350
					if (featureType.getDefaultGeometry().equals(desc.getName())){
341
						defaultGeometry=value;
351
						this.defaultGeometry=value;
342 352
					}
343 353
				}
344 354
			}
345 355
		}
346 356
	}
347 357

  
358
	protected void loadDefaultValues() throws IsNotFeatureSettingException, NumberFormatException, ParseException{
359
		IFeatureType type = this.getType();
360
		Iterator iterator=type.iterator();
361

  
362
		this.loading();
363
		while(iterator.hasNext()){
364
			IFeatureAttributeDescriptor featureAttribute=(IFeatureAttributeDescriptor)iterator.next();
365
			int i=((DefaultAttributeDescriptor)featureAttribute).originalPosition();
366
			String attributeType=featureAttribute.getDataType();
367
			Object defaultValue=featureAttribute.getDefaultValue();
368
			if (attributeType.equals(IFeatureAttributeDescriptor.TYPE_BYTE)){
369
				if (defaultValue instanceof String){
370
					set(i,Byte.parseByte((String)defaultValue));
371
				}else{
372
					set(i,(Byte)defaultValue);
373
				}
374
			}else if (attributeType.equals(IFeatureAttributeDescriptor.TYPE_DATE)){
375
				if (defaultValue instanceof String){
376
					set(i,this.getType().getDateFormat().parse((String)defaultValue));
377
				} else{
378

  
379
				}
380
			}else if (attributeType.equals(IFeatureAttributeDescriptor.TYPE_DOUBLE)){
381
				if (defaultValue instanceof String){
382
					set(i,Double.parseDouble((String)defaultValue));
383
				}else{
384
					set(i,(Double)defaultValue);
385
				}
386
			}else if (attributeType.equals(IFeatureAttributeDescriptor.TYPE_FLOAT)){
387
				if (defaultValue instanceof String){
388
					set(i,Float.parseFloat((String)defaultValue));
389
				}else{
390
					set(i,(Float)defaultValue);
391
				}
392
			}else if (attributeType.equals(IFeatureAttributeDescriptor.TYPE_GEOMETRY)){
393
				setGeometry(i,defaultValue);
394
			}else if (attributeType.equals(IFeatureAttributeDescriptor.TYPE_INT)){
395
				if (defaultValue instanceof String){
396
					set(i,Integer.parseInt((String)defaultValue));
397
				}else{
398
					set(i,(Integer)defaultValue);
399
				}
400
			}else if (attributeType.equals(IFeatureAttributeDescriptor.TYPE_LONG)){
401
				if (defaultValue instanceof String){
402
					set(i,Long.parseLong((String)defaultValue));
403
				}else{
404
					set(i,(Long)defaultValue);
405
				}
406
			}else if (attributeType.equals(IFeatureAttributeDescriptor.TYPE_OBJECT)){
407
				set(i,defaultValue);
408
			}else if (attributeType.equals(IFeatureAttributeDescriptor.TYPE_STRING)){
409
				set(i,(String)defaultValue);
410
			}
411
		}
412
		this.stopLoading();
413

  
414
	}
415

  
348 416
}

Also available in: Unified diff