svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / DefaultEditableFeatureType.java @ 27723
History | View | Annotate | Download (6.24 KB)
1 | 24496 | jmvivo | package org.gvsig.fmap.dal.feature.impl; |
---|---|---|---|
2 | 23754 | jjdelcerro | |
3 | import java.util.Iterator; |
||
4 | |||
5 | 24496 | jmvivo | import org.gvsig.fmap.dal.DataTypes; |
6 | 27723 | jmvivo | import org.gvsig.fmap.dal.exception.DataListException; |
7 | 24496 | jmvivo | import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
8 | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
||
9 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
10 | 24505 | jmvivo | import org.gvsig.fmap.dal.feature.exception.FeatureTypeIntegrityException; |
11 | 23754 | jjdelcerro | import org.gvsig.tools.evaluator.Evaluator; |
12 | |||
13 | public class DefaultEditableFeatureType extends DefaultFeatureType implements |
||
14 | EditableFeatureType { |
||
15 | |||
16 | /**
|
||
17 | *
|
||
18 | */
|
||
19 | private static final long serialVersionUID = -713976880396024389L; |
||
20 | |||
21 | private boolean hasStrongChanges; |
||
22 | private DefaultFeatureType source;
|
||
23 | |||
24 | public DefaultEditableFeatureType() {
|
||
25 | super();
|
||
26 | this.hasStrongChanges = false; |
||
27 | this.source = null; |
||
28 | } |
||
29 | |||
30 | 27262 | jmvivo | public DefaultEditableFeatureType(String id) { |
31 | super(id);
|
||
32 | this.hasStrongChanges = false; |
||
33 | this.source = null; |
||
34 | } |
||
35 | |||
36 | 23754 | jjdelcerro | protected DefaultEditableFeatureType(DefaultEditableFeatureType other) {
|
37 | super(other);
|
||
38 | 23772 | jjdelcerro | this.source = (DefaultFeatureType) other.getSource();
|
39 | 23754 | jjdelcerro | } |
40 | |||
41 | protected DefaultEditableFeatureType(DefaultFeatureType other) {
|
||
42 | super(other);
|
||
43 | this.source = other;
|
||
44 | } |
||
45 | |||
46 | 26057 | jmvivo | protected void intitalizeAddAttibute(DefaultFeatureAttributeDescriptor attr) { |
47 | super.add(new DefaultEditableFeatureAttributeDescriptor(attr)); |
||
48 | } |
||
49 | |||
50 | |||
51 | 23754 | jjdelcerro | public boolean hasStrongChanges() { |
52 | if (hasStrongChanges) {
|
||
53 | return true; |
||
54 | } |
||
55 | Iterator iter = this.iterator(); |
||
56 | DefaultEditableFeatureAttributeDescriptor attr; |
||
57 | while (iter.hasNext()) {
|
||
58 | attr = (DefaultEditableFeatureAttributeDescriptor) iter.next(); |
||
59 | if (attr.hasStrongChanges()) {
|
||
60 | return true; |
||
61 | } |
||
62 | } |
||
63 | return false; |
||
64 | } |
||
65 | |||
66 | public FeatureType getCopy() {
|
||
67 | return new DefaultEditableFeatureType(this); |
||
68 | } |
||
69 | |||
70 | public EditableFeatureType getEditable() {
|
||
71 | throw new UnsupportedOperationException(); |
||
72 | } |
||
73 | |||
74 | public boolean addAll(DefaultFeatureType other) { |
||
75 | Iterator iter = other.iterator();
|
||
76 | DefaultFeatureAttributeDescriptor attr; |
||
77 | DefaultEditableFeatureAttributeDescriptor editableAttr; |
||
78 | while (iter.hasNext()) {
|
||
79 | attr = (DefaultFeatureAttributeDescriptor) iter.next(); |
||
80 | if (attr instanceof DefaultEditableFeatureAttributeDescriptor) { |
||
81 | editableAttr = new DefaultEditableFeatureAttributeDescriptor(
|
||
82 | attr); |
||
83 | } else {
|
||
84 | editableAttr = new DefaultEditableFeatureAttributeDescriptor(
|
||
85 | attr); |
||
86 | } |
||
87 | super.add(editableAttr);
|
||
88 | } |
||
89 | 25917 | jmvivo | this.fixAll();
|
90 | 23754 | jjdelcerro | return true; |
91 | } |
||
92 | |||
93 | 23772 | jjdelcerro | public FeatureType getSource() {
|
94 | 23754 | jjdelcerro | return source;
|
95 | } |
||
96 | |||
97 | public FeatureType getNotEditableCopy() {
|
||
98 | DefaultFeatureType copy = new DefaultFeatureType(this, false); |
||
99 | Iterator iter = this.iterator(); |
||
100 | DefaultFeatureAttributeDescriptor attr; |
||
101 | while (iter.hasNext()) {
|
||
102 | attr = (DefaultFeatureAttributeDescriptor) iter.next(); |
||
103 | copy.add(new DefaultFeatureAttributeDescriptor(attr));
|
||
104 | } |
||
105 | return copy;
|
||
106 | } |
||
107 | |||
108 | public EditableFeatureAttributeDescriptor add(String name, int type) { |
||
109 | DefaultEditableFeatureAttributeDescriptor attr = new DefaultEditableFeatureAttributeDescriptor();
|
||
110 | // FIXME: Comprobar que no hay nombres repetidos.
|
||
111 | attr.setName(name); |
||
112 | attr.setDataType(type); |
||
113 | attr.setIndex(this.size());
|
||
114 | super.add(attr);
|
||
115 | hasStrongChanges = true;
|
||
116 | return attr;
|
||
117 | } |
||
118 | |||
119 | public EditableFeatureAttributeDescriptor add(String name, int type, |
||
120 | int size) {
|
||
121 | hasStrongChanges = true;
|
||
122 | return this.add(name, type).setSize(size); |
||
123 | } |
||
124 | |||
125 | public EditableFeatureAttributeDescriptor add(String name, int type, |
||
126 | Evaluator evaluator) { |
||
127 | return this.add(name, type).setEvaluator(evaluator); |
||
128 | } |
||
129 | |||
130 | public Object remove(String name) { |
||
131 | DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this
|
||
132 | .get(name); |
||
133 | if (attr == null) { |
||
134 | return null; |
||
135 | } |
||
136 | if (attr.getEvaluator() != null) { |
||
137 | hasStrongChanges = true;
|
||
138 | } |
||
139 | 25917 | jmvivo | super.remove(attr);
|
140 | this.fixAll();
|
||
141 | 23754 | jjdelcerro | return attr;
|
142 | } |
||
143 | |||
144 | protected void fixAll() { |
||
145 | int i = 0; |
||
146 | Iterator iter = super.iterator(); |
||
147 | 25917 | jmvivo | DefaultFeatureAttributeDescriptor attr; |
148 | |||
149 | 23754 | jjdelcerro | while (iter.hasNext()) {
|
150 | 25917 | jmvivo | attr = (DefaultFeatureAttributeDescriptor) iter |
151 | 23754 | jjdelcerro | .next(); |
152 | attr.setIndex(i++); |
||
153 | 25917 | jmvivo | if (attr instanceof DefaultEditableFeatureAttributeDescriptor) { |
154 | ((DefaultEditableFeatureAttributeDescriptor) attr).fixAll(); |
||
155 | } |
||
156 | 23754 | jjdelcerro | } |
157 | 25917 | jmvivo | if (this.defaultGeometryAttributeName == null) { |
158 | return;
|
||
159 | } |
||
160 | this.defaultGeometryAttributeIndex = this |
||
161 | .getIndex(this.defaultGeometryAttributeName);
|
||
162 | 23754 | jjdelcerro | } |
163 | |||
164 | 23772 | jjdelcerro | public void checkIntegrity() throws DataListException { |
165 | 23754 | jjdelcerro | Iterator iter = super.iterator(); |
166 | 23772 | jjdelcerro | FeatureTypeIntegrityException ex = null;
|
167 | |||
168 | 23754 | jjdelcerro | while (iter.hasNext()) {
|
169 | DefaultEditableFeatureAttributeDescriptor attr = (DefaultEditableFeatureAttributeDescriptor) iter |
||
170 | .next(); |
||
171 | try {
|
||
172 | attr.checkIntegrity(); |
||
173 | 23772 | jjdelcerro | } catch (Exception e) { |
174 | if (ex == null) { |
||
175 | ex = new FeatureTypeIntegrityException(this.getId()); |
||
176 | } |
||
177 | ex.add(e); |
||
178 | 23754 | jjdelcerro | } |
179 | } |
||
180 | 23772 | jjdelcerro | if (ex != null) { |
181 | throw ex;
|
||
182 | } |
||
183 | 23754 | jjdelcerro | } |
184 | |||
185 | public boolean remove(EditableFeatureAttributeDescriptor attribute) { |
||
186 | if (attribute.getEvaluator() != null) { |
||
187 | hasStrongChanges = true;
|
||
188 | } |
||
189 | if (!super.remove(attribute)) { |
||
190 | return false; |
||
191 | } |
||
192 | this.fixAll();
|
||
193 | return true; |
||
194 | } |
||
195 | |||
196 | public void setDefaultGeometryAttributeName(String name) { |
||
197 | 26057 | jmvivo | if (name == null || name.length() == 0) { |
198 | this.defaultGeometryAttributeName = null; |
||
199 | this.defaultGeometryAttributeIndex = -1; |
||
200 | return;
|
||
201 | } |
||
202 | 23754 | jjdelcerro | DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this
|
203 | .get(name); |
||
204 | if (attr == null) { |
||
205 | throw new IllegalArgumentException("Attribute '" + name |
||
206 | + "' not found.");
|
||
207 | } |
||
208 | 23894 | jjdelcerro | if (attr.getDataType() != DataTypes.GEOMETRY) {
|
209 | 23754 | jjdelcerro | throw new IllegalArgumentException("Attribute '" + name |
210 | + "' is not a geometry.");
|
||
211 | } |
||
212 | this.defaultGeometryAttributeName = name;
|
||
213 | this.defaultGeometryAttributeIndex = attr.getIndex();
|
||
214 | } |
||
215 | 24248 | jjdelcerro | |
216 | public void setHasOID(boolean hasOID) { |
||
217 | this.hasOID = hasOID;
|
||
218 | } |
||
219 | 26323 | jmvivo | |
220 | protected Iterator getIterator(Iterator iter) { |
||
221 | return new EditableDelegatedIterator(iter, this); |
||
222 | } |
||
223 | |||
224 | protected class EditableDelegatedIterator extends DelegatedIterator { |
||
225 | |||
226 | private DefaultEditableFeatureType fType;
|
||
227 | |||
228 | public EditableDelegatedIterator(Iterator iter, |
||
229 | DefaultEditableFeatureType fType) { |
||
230 | super(iter);
|
||
231 | this.fType = fType;
|
||
232 | } |
||
233 | |||
234 | public void remove() { |
||
235 | this.iterator.remove();
|
||
236 | this.fType.fixAll();
|
||
237 | } |
||
238 | |||
239 | } |
||
240 | |||
241 | 27439 | jmvivo | protected void setAllowAutomaticValues(boolean value) { |
242 | this.allowAtomaticValues = value;
|
||
243 | } |
||
244 | 23754 | jjdelcerro | } |