1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2001 Michael Powers
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/
package net.wotonomy.web;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import net.wotonomy.control.EOObserverCenter;
import net.wotonomy.control.EOObserving;
import net.wotonomy.foundation.NSMutableArray;
import net.wotonomy.foundation.NSRange;
/**
* A package class that extends NSMutableArray but makes use of the fact that
* wotonomy's implementation extends ArrayList to intercept insertions and
* deletion and register and unregister objects for change notifications as
* appropriate. Since we can't be sure of ArrayList's implementation, we're
* forced to override each and every add and remove method, some of which
* probably call each other. However, EOObserverCenter will only register us
* once per object.
*/
class ObservableArray extends NSMutableArray {
EOObserving observer;
ObservableArray(EOObserving anObserver) {
observer = anObserver;
}
/**
* Removes the last object from the array.
*/
public void removeLastObject() {
remove(count() - 1);
}
/**
* Removes the object at the specified index.
*/
public void removeObjectAtIndex(int index) {
remove(index);
}
/**
* Adds all objects in the specified collection.
*/
public void addObjectsFromArray(Collection aCollection) {
addAll(aCollection);
}
/**
* Removes all objects from the array.
*/
public void removeAllObjects() {
clear();
}
/**
* Removes all objects equivalent to the specified object within the range of
* specified indices.
*/
public void removeObject(Object anObject, NSRange aRange) {
if ((anObject == null) || (aRange == null))
return;
int loc = aRange.location();
int max = aRange.maxRange();
for (int i = loc; i < max; i++) {
if (anObject.equals(get(i))) {
remove(i);
i = i - 1;
max = max - 1;
}
}
}
/**
* Removes all instances of the specified object within the range of specified
* indices, comparing by reference.
*/
public void removeIdenticalObject(Object anObject, NSRange aRange) {
if ((anObject == null) || (aRange == null))
return;
int loc = aRange.location();
int max = aRange.maxRange();
for (int i = loc; i < max; i++) {
if (anObject == get(i)) {
remove(i);
i = i - 1;
max = max - 1;
}
}
}
/**
* Removes all objects in the specified collection from the array.
*/
public void removeObjectsInArray(Collection aCollection) {
removeAll(aCollection);
}
/**
* Removes all objects in the indices within the specified range from the array.
*/
public void removeObjectsInRange(NSRange aRange) {
if (aRange == null)
return;
for (int i = 0; i < aRange.length(); i++) {
remove(aRange.location());
}
}
/**
* Replaces objects in the current range with objects from the specified range
* of the specified array. If currentRange is larger than otherRange, the extra
* objects are removed. If otherRange is larger than currentRange, the extra
* objects are added.
*/
public void replaceObjectsInRange(NSRange currentRange, List otherArray, NSRange otherRange) {
if ((currentRange == null) || (otherArray == null) || (otherRange == null))
return;
// transform otherRange if out of bounds for array
if (otherRange.maxRange() > otherArray.size()) {
// TODO: Test this logic.
int loc = Math.min(otherRange.location(), otherArray.size() - 1);
otherRange = new NSRange(loc, otherArray.size() - loc);
}
Object o;
List subList = subList(currentRange.location(), currentRange.maxRange());
int otherIndex = otherRange.location();
// TODO: Test this logic.
for (int i = 0; i < subList.size(); i++) {
if (otherIndex < otherRange.maxRange()) { // set object
subList.set(i, otherArray.get(otherIndex));
} else { // remove extra elements from currentRange
subList.remove(i);
i--;
}
otherIndex++;
}
// TODO: Test this logic.
for (int i = otherIndex; i < otherRange.maxRange(); i++) {
add(otherArray.get(i));
}
}
/**
* Clears the current array and then populates it with the contents of the
* specified collection.
*/
public void setArray(Collection aCollection) {
clear();
addAll(aCollection);
}
/**
* Removes all objects equivalent to the specified object.
*/
public void removeObject(Object anObject) {
remove(anObject);
}
/**
* Removes all occurences of the specified object, comparing by reference.
*/
public void removeIdenticalObject(Object anObject) {
EOObserverCenter.removeObserver(observer, anObject);
super.removeIdenticalObject(anObject);
}
/**
* Inserts the specified object into this array at the specified index.
*/
public void insertObjectAtIndex(Object anObject, int anIndex) {
add(anIndex, anObject);
}
/**
* Replaces the object at the specified index with the specified object.
*/
public void replaceObjectAtIndex(int anIndex, Object anObject) {
set(anIndex, anObject);
}
/**
* Adds the specified object to the end of this array.
*/
public void addObject(Object anObject) {
add(anObject);
}
// interface List: mutators
public void add(int index, Object element) {
EOObserverCenter.addObserver(observer, element);
super.add(index, element);
}
public boolean add(Object o) {
EOObserverCenter.addObserver(observer, o);
return super.add(o);
}
public boolean addAll(Collection coll) {
Iterator it = coll.iterator();
while (it.hasNext()) {
EOObserverCenter.addObserver(observer, it.next());
}
return super.addAll(coll);
}
public boolean addAll(int index, Collection c) {
Iterator it = c.iterator();
while (it.hasNext()) {
EOObserverCenter.addObserver(observer, it.next());
}
return super.addAll(index, c);
}
public void clear() {
Iterator it = iterator();
while (it.hasNext()) {
EOObserverCenter.removeObserver(observer, it.next());
}
super.clear();
}
public Object remove(int index) {
EOObserverCenter.removeObserver(observer, get(index));
return super.remove(index);
}
public boolean remove(Object o) {
EOObserverCenter.removeObserver(observer, o);
return super.remove(o);
}
public boolean removeAll(Collection coll) {
Iterator it = coll.iterator();
while (it.hasNext()) {
EOObserverCenter.removeObserver(observer, it.next());
}
return super.removeAll(coll);
}
public boolean retainAll(Collection coll) {
throw new UnsupportedOperationException();
}
public Object set(int index, Object element) {
EOObserverCenter.removeObserver(observer, get(index));
EOObserverCenter.addObserver(observer, element);
return super.set(index, element);
}
}
/*
* $Log$ Revision 1.2 2006/02/19 01:44:02 cgruber Add xmlrpc files Remove jclark
* and replace with dom4j and javax.xml.sax stuff Re-work dependencies and
* imports so it all compiles.
*
* Revision 1.1 2006/02/16 13:22:22 cgruber Check in all sources in
* eclipse-friendly maven-enabled packages.
*
* Revision 1.1 2003/01/18 23:31:32 mpowers Needed for WODisplayGroup.
*
* Revision 1.2 2002/10/24 21:15:36 mpowers New implementations of NSArray and
* subclasses.
*
* Revision 1.1 2001/02/20 16:38:55 mpowers MasterDetailAssociations now observe
* their controlled display group's objects for changes to that the parent
* object will be marked as updated. Before, only inserts and deletes to an
* object's items are registered. Also, moved ObservableArray to package access.
*
* Revision 1.1 2001/01/24 14:37:24 mpowers Contributing a delegate useful for
* debugging.
*
*
*/
|