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 @ 40559
History | View | Annotate | Download (6.26 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
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 3
|
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 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.fmap.dal.feature.impl.featureset; |
25 |
|
26 |
import org.gvsig.fmap.dal.feature.Feature; |
27 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
28 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
29 |
import org.gvsig.fmap.dal.feature.FeatureStoreNotification; |
30 |
import org.gvsig.tools.dispose.DisposableIterator; |
31 |
import org.gvsig.tools.dynobject.DynObject; |
32 |
import org.gvsig.tools.dynobject.DynObjectSet; |
33 |
import org.gvsig.tools.exception.BaseException; |
34 |
import org.gvsig.tools.observer.Observable; |
35 |
import org.gvsig.tools.observer.Observer; |
36 |
import org.gvsig.tools.observer.impl.BaseWeakReferencingObservable; |
37 |
import org.gvsig.tools.visitor.VisitCanceledException; |
38 |
import org.gvsig.tools.visitor.Visitor; |
39 |
|
40 |
/**
|
41 |
* {@link DynObject} implementation to facade of a {@link FeatureSet} and allow
|
42 |
* to be used as a {@link DynObjectSet}.
|
43 |
*
|
44 |
* @author gvSIG Team
|
45 |
* @version $Id$
|
46 |
*/
|
47 |
public class DynObjectSetFeatureSetFacade extends BaseWeakReferencingObservable |
48 |
implements DynObjectSet, Observer { |
49 |
|
50 |
private final FeatureSet featureSet; |
51 |
|
52 |
private final FeatureStore store; |
53 |
|
54 |
private final DynObjectFeatureFacade featureFacade = |
55 |
new DynObjectFeatureFacade();
|
56 |
|
57 |
private final boolean fast; |
58 |
|
59 |
/**
|
60 |
* Creates a new facade over a given feature set, with fast dynobject
|
61 |
* iteration.
|
62 |
*
|
63 |
* @param featureSet
|
64 |
* to facade
|
65 |
*/
|
66 |
public DynObjectSetFeatureSetFacade(FeatureSet featureSet,
|
67 |
FeatureStore store) { |
68 |
this(featureSet, store, true); |
69 |
} |
70 |
|
71 |
/**
|
72 |
* Creates a new facade over a given feature set, with fast dynobject
|
73 |
* iteration.
|
74 |
*
|
75 |
* @param featureSet
|
76 |
* to facade
|
77 |
* @param fast
|
78 |
* if true try to reuse objects as much as possible to make the
|
79 |
* object iteration faster. If true, DynObjects got through the
|
80 |
* returned set must not be stored unless cloned.
|
81 |
*/
|
82 |
public DynObjectSetFeatureSetFacade(FeatureSet featureSet,
|
83 |
FeatureStore store, boolean fast) {
|
84 |
this.featureSet = featureSet;
|
85 |
this.store = store;
|
86 |
store.addObserver(this);
|
87 |
this.fast = fast;
|
88 |
} |
89 |
|
90 |
public void dispose() { |
91 |
store.deleteObserver(this);
|
92 |
} |
93 |
|
94 |
public void accept(final Visitor visitor, long firstValueIndex) |
95 |
throws BaseException {
|
96 |
featureSet.accept(new Visitor() {
|
97 |
|
98 |
public void visit(Object obj) throws VisitCanceledException, |
99 |
BaseException { |
100 |
DynObjectFeatureFacade feature; |
101 |
if (fast) {
|
102 |
feature = featureFacade; |
103 |
} else {
|
104 |
feature = new DynObjectFeatureFacade();
|
105 |
} |
106 |
feature.setFeatureCopy((Feature) obj); |
107 |
visitor.visit(feature); |
108 |
} |
109 |
}, firstValueIndex); |
110 |
} |
111 |
|
112 |
public void accept(final Visitor visitor) throws BaseException { |
113 |
featureSet.accept(new Visitor() {
|
114 |
|
115 |
public void visit(Object obj) throws VisitCanceledException, |
116 |
BaseException { |
117 |
featureFacade.setFeatureCopy((Feature) obj); |
118 |
visitor.visit(featureFacade); |
119 |
} |
120 |
}); |
121 |
} |
122 |
|
123 |
public long getSize() throws BaseException { |
124 |
return featureSet.getSize();
|
125 |
} |
126 |
|
127 |
public DisposableIterator iterator(long index) throws BaseException { |
128 |
if (fast) {
|
129 |
return new DynObjectIteratorFeatureIteratorFacade( |
130 |
featureSet.fastIterator(index), featureFacade); |
131 |
} else {
|
132 |
return new DynObjectIteratorFeatureIteratorFacade( |
133 |
featureSet.fastIterator(index)); |
134 |
} |
135 |
} |
136 |
|
137 |
public DisposableIterator iterator() throws BaseException { |
138 |
if (fast) {
|
139 |
return new DynObjectIteratorFeatureIteratorFacade( |
140 |
featureSet.fastIterator(), featureFacade); |
141 |
} else {
|
142 |
return new DynObjectIteratorFeatureIteratorFacade( |
143 |
featureSet.fastIterator()); |
144 |
} |
145 |
} |
146 |
|
147 |
public boolean isEmpty() throws BaseException { |
148 |
return featureSet.isEmpty();
|
149 |
} |
150 |
|
151 |
public boolean isDeleteEnabled() { |
152 |
return store.isEditing();
|
153 |
} |
154 |
|
155 |
public void delete(DynObject dynObject) throws BaseException { |
156 |
DynObjectFeatureFacade facade = (DynObjectFeatureFacade) dynObject; |
157 |
featureSet.delete(facade.getFeature()); |
158 |
} |
159 |
|
160 |
public boolean isUpdateEnabled() { |
161 |
return store.isEditing();
|
162 |
} |
163 |
|
164 |
public void update(DynObject dynObject) throws BaseException { |
165 |
DynObjectFeatureFacade facade = (DynObjectFeatureFacade) dynObject; |
166 |
featureSet.update(facade.getEditable()); |
167 |
} |
168 |
|
169 |
public void update(Observable observable, Object notification) { |
170 |
if (observable.equals(store)
|
171 |
&& notification instanceof FeatureStoreNotification) {
|
172 |
FeatureStoreNotification event = |
173 |
(FeatureStoreNotification) notification; |
174 |
if (event.getType() == FeatureStoreNotification.AFTER_STARTEDITING
|
175 |
|| event.getType() == FeatureStoreNotification.AFTER_FINISHEDITING |
176 |
|| event.getType() == FeatureStoreNotification.AFTER_CANCELEDITING) { |
177 |
setChanged(); |
178 |
notify(this, new Notification() { |
179 |
|
180 |
public String getType() { |
181 |
return Notification.EDITION_STATUS_CHANGE; |
182 |
} |
183 |
}); |
184 |
} |
185 |
} |
186 |
|
187 |
} |
188 |
} |