svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / main / java / org / gvsig / fmap / dal / feature / impl / featureset / DynObjectSetFeatureSetFacade.java @ 40435
History | View | Annotate | Download (6.21 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
package org.gvsig.fmap.dal.feature.impl.featureset; |
23 |
|
24 |
import org.gvsig.fmap.dal.feature.Feature; |
25 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
26 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
27 |
import org.gvsig.fmap.dal.feature.FeatureStoreNotification; |
28 |
import org.gvsig.tools.dispose.DisposableIterator; |
29 |
import org.gvsig.tools.dynobject.DynObject; |
30 |
import org.gvsig.tools.dynobject.DynObjectSet; |
31 |
import org.gvsig.tools.exception.BaseException; |
32 |
import org.gvsig.tools.observer.Observable; |
33 |
import org.gvsig.tools.observer.Observer; |
34 |
import org.gvsig.tools.observer.impl.BaseWeakReferencingObservable; |
35 |
import org.gvsig.tools.visitor.VisitCanceledException; |
36 |
import org.gvsig.tools.visitor.Visitor; |
37 |
|
38 |
/**
|
39 |
* {@link DynObject} implementation to facade of a {@link FeatureSet} and allow
|
40 |
* to be used as a {@link DynObjectSet}.
|
41 |
*
|
42 |
* @author gvSIG Team
|
43 |
* @version $Id$
|
44 |
*/
|
45 |
public class DynObjectSetFeatureSetFacade extends BaseWeakReferencingObservable |
46 |
implements DynObjectSet, Observer { |
47 |
|
48 |
private final FeatureSet featureSet; |
49 |
|
50 |
private final FeatureStore store; |
51 |
|
52 |
private final DynObjectFeatureFacade featureFacade = |
53 |
new DynObjectFeatureFacade();
|
54 |
|
55 |
private final boolean fast; |
56 |
|
57 |
/**
|
58 |
* Creates a new facade over a given feature set, with fast dynobject
|
59 |
* iteration.
|
60 |
*
|
61 |
* @param featureSet
|
62 |
* to facade
|
63 |
*/
|
64 |
public DynObjectSetFeatureSetFacade(FeatureSet featureSet,
|
65 |
FeatureStore store) { |
66 |
this(featureSet, store, true); |
67 |
} |
68 |
|
69 |
/**
|
70 |
* Creates a new facade over a given feature set, with fast dynobject
|
71 |
* iteration.
|
72 |
*
|
73 |
* @param featureSet
|
74 |
* to facade
|
75 |
* @param fast
|
76 |
* if true try to reuse objects as much as possible to make the
|
77 |
* object iteration faster. If true, DynObjects got through the
|
78 |
* returned set must not be stored unless cloned.
|
79 |
*/
|
80 |
public DynObjectSetFeatureSetFacade(FeatureSet featureSet,
|
81 |
FeatureStore store, boolean fast) {
|
82 |
this.featureSet = featureSet;
|
83 |
this.store = store;
|
84 |
store.addObserver(this);
|
85 |
this.fast = fast;
|
86 |
} |
87 |
|
88 |
public void dispose() { |
89 |
store.deleteObserver(this);
|
90 |
} |
91 |
|
92 |
public void accept(final Visitor visitor, long firstValueIndex) |
93 |
throws BaseException {
|
94 |
featureSet.accept(new Visitor() {
|
95 |
|
96 |
public void visit(Object obj) throws VisitCanceledException, |
97 |
BaseException { |
98 |
DynObjectFeatureFacade feature; |
99 |
if (fast) {
|
100 |
feature = featureFacade; |
101 |
} else {
|
102 |
feature = new DynObjectFeatureFacade();
|
103 |
} |
104 |
feature.setFeatureCopy((Feature) obj); |
105 |
visitor.visit(feature); |
106 |
} |
107 |
}, firstValueIndex); |
108 |
} |
109 |
|
110 |
public void accept(final Visitor visitor) throws BaseException { |
111 |
featureSet.accept(new Visitor() {
|
112 |
|
113 |
public void visit(Object obj) throws VisitCanceledException, |
114 |
BaseException { |
115 |
featureFacade.setFeatureCopy((Feature) obj); |
116 |
visitor.visit(featureFacade); |
117 |
} |
118 |
}); |
119 |
} |
120 |
|
121 |
public long getSize() throws BaseException { |
122 |
return featureSet.getSize();
|
123 |
} |
124 |
|
125 |
public DisposableIterator iterator(long index) throws BaseException { |
126 |
if (fast) {
|
127 |
return new DynObjectIteratorFeatureIteratorFacade( |
128 |
featureSet.fastIterator(index), featureFacade); |
129 |
} else {
|
130 |
return new DynObjectIteratorFeatureIteratorFacade( |
131 |
featureSet.fastIterator(index)); |
132 |
} |
133 |
} |
134 |
|
135 |
public DisposableIterator iterator() throws BaseException { |
136 |
if (fast) {
|
137 |
return new DynObjectIteratorFeatureIteratorFacade( |
138 |
featureSet.fastIterator(), featureFacade); |
139 |
} else {
|
140 |
return new DynObjectIteratorFeatureIteratorFacade( |
141 |
featureSet.fastIterator()); |
142 |
} |
143 |
} |
144 |
|
145 |
public boolean isEmpty() throws BaseException { |
146 |
return featureSet.isEmpty();
|
147 |
} |
148 |
|
149 |
public boolean isDeleteEnabled() { |
150 |
return store.isEditing();
|
151 |
} |
152 |
|
153 |
public void delete(DynObject dynObject) throws BaseException { |
154 |
DynObjectFeatureFacade facade = (DynObjectFeatureFacade) dynObject; |
155 |
featureSet.delete(facade.getFeature()); |
156 |
} |
157 |
|
158 |
public boolean isUpdateEnabled() { |
159 |
return store.isEditing();
|
160 |
} |
161 |
|
162 |
public void update(DynObject dynObject) throws BaseException { |
163 |
DynObjectFeatureFacade facade = (DynObjectFeatureFacade) dynObject; |
164 |
featureSet.update(facade.getEditable()); |
165 |
} |
166 |
|
167 |
public void update(Observable observable, Object notification) { |
168 |
if (observable.equals(store)
|
169 |
&& notification instanceof FeatureStoreNotification) {
|
170 |
FeatureStoreNotification event = |
171 |
(FeatureStoreNotification) notification; |
172 |
if (event.getType() == FeatureStoreNotification.AFTER_STARTEDITING
|
173 |
|| event.getType() == FeatureStoreNotification.AFTER_FINISHEDITING |
174 |
|| event.getType() == FeatureStoreNotification.AFTER_CANCELEDITING) { |
175 |
setChanged(); |
176 |
notify(this, new Notification() { |
177 |
|
178 |
public String getType() { |
179 |
return Notification.EDITION_STATUS_CHANGE; |
180 |
} |
181 |
}); |
182 |
} |
183 |
} |
184 |
|
185 |
} |
186 |
} |