root / trunk / libraries / libDataSource / src / org / gvsig / data / vectorial / AbstractFeatureStore.java @ 20469
History | View | Annotate | Download (12.4 KB)
1 |
package org.gvsig.data.vectorial; |
---|---|
2 |
|
3 |
import java.lang.ref.Reference; |
4 |
import java.util.Iterator; |
5 |
|
6 |
import org.gvsig.data.ComplexObservable; |
7 |
import org.gvsig.data.IDataCollection; |
8 |
import org.gvsig.data.IDataStoreParameters; |
9 |
import org.gvsig.data.IObservable; |
10 |
import org.gvsig.data.IObserver; |
11 |
import org.gvsig.data.commands.ICommand; |
12 |
import org.gvsig.data.commands.ICommandsRecord; |
13 |
import org.gvsig.data.commands.implementation.FeatureCommandsRecord; |
14 |
import org.gvsig.data.exception.InitializeException; |
15 |
import org.gvsig.data.exception.OpenException; |
16 |
import org.gvsig.data.exception.ReadException; |
17 |
import org.gvsig.data.exception.WriteException; |
18 |
import org.gvsig.data.vectorial.expansionadapter.IExpansionAdapter; |
19 |
import org.gvsig.data.vectorial.expansionadapter.MemoryExpansionAdapter; |
20 |
|
21 |
|
22 |
public abstract class AbstractFeatureStore implements IFeatureStore { |
23 |
|
24 |
protected IDataStoreParameters parameters;
|
25 |
protected IDataCollection selection;
|
26 |
protected ICommandsRecord commands;
|
27 |
protected boolean alterMode = false; |
28 |
protected ComplexObservable observable=new ComplexObservable(); |
29 |
protected IDataCollection locked;
|
30 |
|
31 |
protected FeatureManager featureManager;
|
32 |
protected SpatialManager spatialManager;
|
33 |
protected AttributeManager attributeManager;
|
34 |
|
35 |
|
36 |
public void init(IDataStoreParameters parameters) throws InitializeException { |
37 |
this.parameters = parameters;
|
38 |
|
39 |
} |
40 |
|
41 |
protected void initSpatialManager(){ |
42 |
spatialManager=new SpatialManager();
|
43 |
} |
44 |
|
45 |
protected void initAttributeManager() { |
46 |
IExpansionAdapter expansionAttributeAdapter=new MemoryExpansionAdapter();
|
47 |
attributeManager=new AttributeManager(expansionAttributeAdapter,getDefaultFeatureType());
|
48 |
|
49 |
} |
50 |
|
51 |
protected void initExpansionManager() { |
52 |
IExpansionAdapter expansionFeatureAdapter=new MemoryExpansionAdapter();
|
53 |
featureManager=new FeatureManager(expansionFeatureAdapter);
|
54 |
} |
55 |
|
56 |
public IDataCollection getDataCollection() throws ReadException { |
57 |
return getDataCollection(null, null, null); |
58 |
} |
59 |
|
60 |
public void getDataCollection(IObserver observer) { |
61 |
getDataCollection(null, null, null,observer); |
62 |
} |
63 |
|
64 |
public void setSelection(IDataCollection selection) { |
65 |
this.observable.notifyObservers(
|
66 |
new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_SELECTIONCHANGE) |
67 |
); |
68 |
this.selection = selection;
|
69 |
this.observable.notifyObservers(
|
70 |
new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_SELECTIONCHANGE) |
71 |
); |
72 |
|
73 |
} |
74 |
|
75 |
public IDataCollection createSelection() {
|
76 |
|
77 |
MemoryFeatureCollection selection = new MemoryFeatureCollection();
|
78 |
return selection;
|
79 |
} |
80 |
|
81 |
public IDataCollection getSelection() {
|
82 |
if( selection == null ){ |
83 |
selection = createSelection(); |
84 |
} |
85 |
return selection;
|
86 |
} |
87 |
|
88 |
public IDataCollection createLocked() {
|
89 |
MemoryFeatureCollection locked = new MemoryFeatureCollection();
|
90 |
return locked;
|
91 |
} |
92 |
|
93 |
public IDataCollection getLocked() {
|
94 |
if( locked == null ){ |
95 |
locked = createLocked(); |
96 |
} |
97 |
return locked;
|
98 |
} |
99 |
|
100 |
public void setLocked(IDataCollection locked) { |
101 |
this.observable.notifyObservers(
|
102 |
new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_LOCKEDCHANGE) |
103 |
); |
104 |
this.locked = locked;
|
105 |
this.observable.notifyObservers(
|
106 |
new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_LOCKEDCHANGE) |
107 |
); |
108 |
|
109 |
} |
110 |
public boolean isLocked(IFeatureID id) { |
111 |
return locked.contains(id);
|
112 |
} |
113 |
|
114 |
public boolean lock(IFeatureID id) { |
115 |
this.observable.notifyObservers(
|
116 |
new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_LOCKEDCHANGE) |
117 |
); |
118 |
locked.add(id); |
119 |
this.observable.notifyObservers(
|
120 |
new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_LOCKEDCHANGE) |
121 |
); |
122 |
return true; |
123 |
} |
124 |
|
125 |
|
126 |
public void startEditing() throws ReadException { |
127 |
if (!this.isEditable()) { |
128 |
throw new ReadException("AbstractFeatureStore", |
129 |
new UnsupportedOperationException("Not editable")); |
130 |
} |
131 |
this.observable.notifyObservers(
|
132 |
new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_STARTEDITING) |
133 |
); |
134 |
// commands.clear();
|
135 |
initExpansionManager(); |
136 |
initSpatialManager(); |
137 |
initAttributeManager(); |
138 |
commands = new FeatureCommandsRecord(featureManager, spatialManager, attributeManager);
|
139 |
|
140 |
|
141 |
alterMode = true;
|
142 |
this.observable.notifyObservers(
|
143 |
new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_STARTEDITING) |
144 |
); |
145 |
|
146 |
} |
147 |
|
148 |
|
149 |
public void delete(IFeatureAttributeDescriptor attributeDescriptor) { |
150 |
if( !alterMode ) {
|
151 |
throw new RuntimeException("alterMode is false"); |
152 |
} |
153 |
this.observable.notifyObservers(
|
154 |
new AttributeStoreNotification(this,AttributeStoreNotification.BEFORE_DELETE_ATTRIBUTE,attributeDescriptor) |
155 |
); |
156 |
commands.delete(attributeDescriptor); |
157 |
this.observable.notifyObservers(
|
158 |
new AttributeStoreNotification(this,AttributeStoreNotification.AFTER_DELETE_ATTRIBUTE,attributeDescriptor) |
159 |
); |
160 |
|
161 |
|
162 |
} |
163 |
|
164 |
public void insert(IFeatureAttributeDescriptor attributeDescriptor) { |
165 |
if( !alterMode ) {
|
166 |
throw new RuntimeException("alterMode is false"); |
167 |
} |
168 |
// attributeDescriptor.validateModification(this);
|
169 |
this.observable.notifyObservers(
|
170 |
new AttributeStoreNotification(this,AttributeStoreNotification.BEFORE_INSERT_ATTRIBUTE,attributeDescriptor) |
171 |
); |
172 |
commands.insert(attributeDescriptor); |
173 |
this.observable.notifyObservers(
|
174 |
new AttributeStoreNotification(this,FeatureStoreNotification.AFTER_INSERT,attributeDescriptor) |
175 |
); |
176 |
|
177 |
} |
178 |
|
179 |
public void update(IFeatureAttributeDescriptor attributeDescriptor) { |
180 |
if( !alterMode ) {
|
181 |
throw new RuntimeException("alterMode is false"); |
182 |
} |
183 |
// feature.validateModification(this);
|
184 |
this.observable.notifyObservers(
|
185 |
new AttributeStoreNotification(this,AttributeStoreNotification.BEFORE_UPDATE_ATTRIBUTE,attributeDescriptor) |
186 |
); |
187 |
commands.update(attributeDescriptor,((DefaultAttributeDescriptor)attributeDescriptor).getOldAttributeDescriptor()); |
188 |
((DefaultAttributeDescriptor)attributeDescriptor).stopEditing(); |
189 |
this.observable.notifyObservers(
|
190 |
new AttributeStoreNotification(this,AttributeStoreNotification.AFTER_UPDATE_ATTRIBUTE,attributeDescriptor) |
191 |
); |
192 |
} |
193 |
|
194 |
|
195 |
public void delete(IFeature feature) { |
196 |
if( !alterMode ) {
|
197 |
throw new RuntimeException("alterMode is false"); |
198 |
} |
199 |
this.observable.notifyObservers(
|
200 |
new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_DELETE,feature) |
201 |
); |
202 |
commands.delete(feature); |
203 |
this.observable.notifyObservers(
|
204 |
new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_DELETE,feature) |
205 |
); |
206 |
|
207 |
|
208 |
} |
209 |
|
210 |
public void insert(IFeature feature) { |
211 |
if( !alterMode ) {
|
212 |
throw new RuntimeException("alterMode is false"); |
213 |
} |
214 |
feature.validateModification(this);
|
215 |
this.observable.notifyObservers(
|
216 |
new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_INSERT,feature) |
217 |
); |
218 |
commands.insert(feature); |
219 |
this.observable.notifyObservers(
|
220 |
new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_INSERT,feature) |
221 |
); |
222 |
|
223 |
} |
224 |
|
225 |
public void update(IFeature feature) { |
226 |
if( !alterMode ) {
|
227 |
throw new RuntimeException("alterMode is false"); |
228 |
} |
229 |
feature.validateModification(this);
|
230 |
this.observable.notifyObservers(
|
231 |
new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_UPDATE,feature) |
232 |
); |
233 |
commands.update(feature,((Feature)feature).getOldFeature()); |
234 |
((Feature)feature).stopEditing(); |
235 |
this.observable.notifyObservers(
|
236 |
new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_UPDATE,feature) |
237 |
); |
238 |
} |
239 |
|
240 |
public void redo() { |
241 |
if( !alterMode ) {
|
242 |
throw new RuntimeException("alterMode is false"); |
243 |
} |
244 |
ICommand redo = commands.getNextRedoCommand(); |
245 |
this.observable.notifyObservers(
|
246 |
new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_REDO,redo) |
247 |
); |
248 |
|
249 |
commands.redo(); |
250 |
|
251 |
this.observable.notifyObservers(
|
252 |
new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_REDO,redo) |
253 |
); |
254 |
|
255 |
} |
256 |
|
257 |
public void undo() { |
258 |
if( !alterMode ) {
|
259 |
throw new RuntimeException("alterMode is false"); |
260 |
} |
261 |
ICommand undo = commands.getNextUndoCommand(); |
262 |
this.observable.notifyObservers(
|
263 |
new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_UNDO,undo) |
264 |
); |
265 |
|
266 |
commands.undo(); |
267 |
|
268 |
this.observable.notifyObservers(
|
269 |
new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_UNDO,undo) |
270 |
); |
271 |
|
272 |
} |
273 |
|
274 |
public ICommandsRecord getCommandsRecord() {
|
275 |
if( !alterMode ) {
|
276 |
throw new RuntimeException("alterMode is false"); |
277 |
} |
278 |
return commands;
|
279 |
} |
280 |
|
281 |
public void cancelEditing() { |
282 |
if( !alterMode ) {
|
283 |
throw new RuntimeException("alterMode is false"); |
284 |
} |
285 |
this.observable.notifyObservers(
|
286 |
new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_CANCELEDITING) |
287 |
); |
288 |
|
289 |
commands.clear(); |
290 |
alterMode = false;
|
291 |
|
292 |
this.observable.notifyObservers(
|
293 |
new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_CANCELEDITING) |
294 |
); |
295 |
|
296 |
} |
297 |
|
298 |
public void finishEditing() throws WriteException, ReadException{ |
299 |
if( !alterMode ) {
|
300 |
//FIXME: OJO arreglar esta excepci?n!!!
|
301 |
throw new RuntimeException("alterMode is false"); |
302 |
} |
303 |
|
304 |
this.observable.notifyObservers(
|
305 |
new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_FINISHEDITING) |
306 |
); |
307 |
|
308 |
this.validateEndEditing();
|
309 |
|
310 |
doFinishEdition(); |
311 |
|
312 |
|
313 |
commands.clear(); |
314 |
featureManager=null;
|
315 |
spatialManager=null;
|
316 |
attributeManager=null;
|
317 |
commands = null;
|
318 |
|
319 |
alterMode = false;
|
320 |
|
321 |
this.observable.notifyObservers(
|
322 |
new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_FINISHEDITING) |
323 |
); |
324 |
|
325 |
} |
326 |
|
327 |
protected abstract void doFinishEdition() throws WriteException, ReadException ; |
328 |
|
329 |
|
330 |
public Iterator getChilds() { |
331 |
return null; |
332 |
} |
333 |
|
334 |
public void beginComplexNotification() { |
335 |
this.observable.beginComplexNotification();
|
336 |
|
337 |
} |
338 |
|
339 |
public void endComplexNotification() { |
340 |
this.observable.endComplexNotification();
|
341 |
|
342 |
} |
343 |
|
344 |
public void addObserver(IObserver o) { |
345 |
this.observable.addObserver(o);
|
346 |
|
347 |
} |
348 |
|
349 |
public void addObserver(Reference ref) { |
350 |
this.observable.addObserver(ref);
|
351 |
} |
352 |
|
353 |
public void deleteObserver(IObserver o) { |
354 |
this.observable.deleteObserver(o);
|
355 |
} |
356 |
|
357 |
public void deleteObserver(Reference ref) { |
358 |
this.observable.deleteObserver(ref);
|
359 |
} |
360 |
|
361 |
public void deleteObservers() { |
362 |
this.observable.deleteObservers();
|
363 |
|
364 |
} |
365 |
|
366 |
public boolean isEditing() { |
367 |
return alterMode;
|
368 |
} |
369 |
|
370 |
protected void validateEndEditing() throws ReadException{ |
371 |
IFeatureCollection collection = (IFeatureCollection)this.getDataCollection();
|
372 |
Iterator iter = collection.iterator();
|
373 |
while (iter.hasNext()){
|
374 |
((IFeature)iter.next()).validateEnd(this);
|
375 |
} |
376 |
|
377 |
} |
378 |
|
379 |
public void disableNotifications() { |
380 |
this.observable.diableNotifications();
|
381 |
|
382 |
} |
383 |
|
384 |
public void enableNotifications() { |
385 |
this.observable.enableNotifications();
|
386 |
} |
387 |
public void getDataCollection(IFeatureType type, String filter, String order,IObserver observer) { |
388 |
LoadInBackGround task = new LoadInBackGround(this,type,filter,order,observer); |
389 |
Thread thread = new Thread(task); |
390 |
thread.run(); |
391 |
} |
392 |
private class LoadInBackGround implements Runnable{ |
393 |
|
394 |
private IFeatureStore store;
|
395 |
private IFeatureType type;
|
396 |
private String filter; |
397 |
private String order; |
398 |
|
399 |
private IObserver observer;
|
400 |
|
401 |
public IDataCollection collection = null; |
402 |
public LoadInBackGround(IFeatureStore store,IFeatureType type, String filter, String order,IObserver observer){ |
403 |
this.store = store;
|
404 |
this.type = type;
|
405 |
this.filter = filter;
|
406 |
this.order = order;
|
407 |
this.observer = observer;
|
408 |
|
409 |
} |
410 |
|
411 |
public void run() { |
412 |
try{
|
413 |
collection = getDataCollection(type, filter, order); |
414 |
} catch (Exception e) { |
415 |
this.observer.update(this.store, new FeatureStoreNotification( |
416 |
this.store,
|
417 |
IFeatureStoreNotification.LOAD_FINISHED, |
418 |
e) |
419 |
); |
420 |
} |
421 |
this.observer.update(
|
422 |
this.store, new FeatureStoreNotification( |
423 |
this.store,
|
424 |
IFeatureStoreNotification.LOAD_FINISHED, |
425 |
collection) |
426 |
); |
427 |
|
428 |
} |
429 |
|
430 |
|
431 |
} |
432 |
public IFeature createFeature(IFeatureType type) throws IsNotFeatureSettingException { |
433 |
IFeature feature=new CreatedFeature(type,true); |
434 |
return feature;
|
435 |
} |
436 |
|
437 |
public IFeature createDefaultFeature(boolean defaultValues){ |
438 |
IFeature feature=new CreatedFeature(getDefaultFeatureType(),defaultValues);
|
439 |
return feature;
|
440 |
} |
441 |
|
442 |
public IDataStoreParameters getParameters() {
|
443 |
return parameters;
|
444 |
} |
445 |
|
446 |
} |