KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > handlers > AEIndexUListWrapper


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.mdr.handlers;
20
21 import java.util.*;
22 import javax.jmi.reflect.*;
23 import org.netbeans.api.mdr.events.AssociationEvent;
24
25 import org.netbeans.mdr.storagemodel.*;
26
27 /**
28  *
29  * @author Martin Matula
30  */

31 public class AEIndexUListWrapper extends AEIndexSetWrapper implements List {
32     private final List innerList;
33
34     /** Creates new ListWrapper */
35     public AEIndexUListWrapper(AssociationHandler source, RefObject fixed, String JavaDoc endName, AssocEndIndexUList inner, boolean queryFirstEnd) {
36         super(source, fixed, endName, inner, queryFirstEnd);
37         this.innerList = inner;
38     }
39     
40     public Object JavaDoc remove(int param) {
41         boolean fail = true;
42         lock(true);
43         try {
44             if (storage.eventsEnabled()) {
45                 AssociationEvent event = new AssociationEvent(
46                 source,
47                 AssociationEvent.EVENT_ASSOCIATION_REMOVE,
48                 fixed, endName,
49                 (RefObject) get(param), null,
50                 param);
51                 notifier.firePlannedChange(source, event);
52             }
53             Object JavaDoc result = wrap(innerList.remove(param));
54             if (!this.fixed.refOutermostPackage().equals(((RefObject)result).refOutermostPackage())) {
55                 RefObject objectA;
56                 RefObject objectB;
57                 if (!queryFirstEnd) {
58                     objectA = (RefObject) result;
59                     objectB = this.fixed;
60                 }
61                 else {
62                     objectA = this.fixed;
63                     objectB = (RefObject) result;
64                 }
65                 AssociationHandler._removeExternalLink(this.source, objectA, objectB);
66             }
67             fail = false;
68             return result;
69         } finally {
70             unlock(fail);
71         }
72     }
73     
74     public void add(int param, Object JavaDoc obj) {
75         boolean fail = true;
76         innerTC.checkType(obj);
77         lock(true);
78         try {
79             if (storage.eventsEnabled()) {
80                 AssociationEvent event = new AssociationEvent(
81                 source,
82                 AssociationEvent.EVENT_ASSOCIATION_ADD,
83                 fixed, endName,
84                 null, (RefObject) obj,
85                 param);
86                 notifier.firePlannedChange(source, event);
87             }
88             innerList.add(param, unwrap(obj));
89             if (!this.fixed.refOutermostPackage().equals(((RefObject)obj).refOutermostPackage())) {
90                 RefObject objectA;
91                 RefObject objectB;
92                 if (!queryFirstEnd) {
93                     objectA = (RefObject) obj;
94                     objectB = this.fixed;
95                 }
96                 else {
97                     objectA = this.fixed;
98                     objectB = (RefObject) obj;
99                 }
100                 AssociationHandler._addExternalLink(this.source, objectA, objectB);
101             }
102             fail = false;
103         } finally {
104             unlock(fail);
105         }
106     }
107     
108     
109     public Object JavaDoc set(int param, Object JavaDoc obj) {
110         boolean fail = true;
111         innerTC.checkType(obj);
112         lock(true);
113         try {
114             if (storage.eventsEnabled()) {
115                 AssociationEvent event = new AssociationEvent(
116                 source,
117                 AssociationEvent.EVENT_ASSOCIATION_SET,
118                 fixed, endName,
119                 (RefObject) get(param), (RefObject) obj,
120                 param);
121                 notifier.firePlannedChange(source, event);
122             }
123             Object JavaDoc result = wrap(innerList.set(param, unwrap(obj)));
124             if (!this.fixed.refOutermostPackage().equals(((RefObject)result).refOutermostPackage())) {
125                 RefObject objectA;
126                 RefObject objectB;
127                 if (!this.queryFirstEnd) {
128                     objectA = (RefObject) result;
129                     objectB = this.fixed;
130                 }
131                 else {
132                     objectA = this.fixed;
133                     objectB = (RefObject) result;
134                 }
135                 AssociationHandler._removeExternalLink(this.source, objectA, objectB);
136             }
137             if (!this.fixed.refOutermostPackage().equals(((RefObject)obj).refOutermostPackage())) {
138                 RefObject objectA;
139                 RefObject objectB;
140                 if (!this.queryFirstEnd) {
141                     objectA = (RefObject) obj;
142                     objectB = this.fixed;
143                 }
144                 else {
145                     objectA = this.fixed;
146                     objectB = (RefObject) obj;
147                 }
148                 AssociationHandler._addExternalLink (this.source, objectA, objectB);
149             }
150             fail = false;
151             return result;
152         } finally {
153             unlock(fail);
154         }
155     }
156     
157     public boolean addAll(Collection collection) {
158         boolean fail = true;
159         lock(true);
160         try {
161             ListIterator lit = listIterator(size());
162             for (Iterator it = collection.iterator(); it.hasNext();) {
163                 lit.add(it.next());
164             }
165             fail = false;
166             return true;
167         } finally {
168             unlock(fail);
169         }
170     }
171     
172     public boolean addAll(int param, Collection collection) {
173         boolean fail = true;
174         lock(true);
175         try {
176             ListIterator lit = listIterator(param);
177             for (Iterator it = collection.iterator(); it.hasNext();) {
178                 lit.add(it.next());
179             }
180             fail = false;
181             return true;
182         } finally {
183             unlock(fail);
184         }
185     }
186     
187     public int indexOf(Object JavaDoc obj) {
188         lock(false);
189         try {
190             return innerList.indexOf(unwrap(obj));
191         } finally {
192             unlock();
193         }
194     }
195     
196     public int lastIndexOf(Object JavaDoc obj) {
197         lock(false);
198         try {
199             return innerList.lastIndexOf(unwrap(obj));
200         } finally {
201             unlock();
202         }
203     }
204     
205     public Object JavaDoc get(int param) {
206         lock(false);
207         try {
208             return wrap(innerList.get(param));
209         } finally {
210             unlock();
211         }
212     }
213     
214     public ListIterator listIterator() {
215         lock(false);
216         try {
217             return new AEIndexListIteratorWrapper(innerList.listIterator());
218         } finally {
219             unlock();
220         }
221     }
222     
223     public ListIterator listIterator(int param) {
224         lock(false);
225         try {
226             return new AEIndexListIteratorWrapper(innerList.listIterator(param));
227         } finally {
228             unlock();
229         }
230     }
231     
232     public List subList(int param, int param1) {
233         throw new UnsupportedOperationException JavaDoc();
234         // return new ListWrapper(((List) innerList).subList(param, param1), source);
235
}
236     
237     public boolean equals(Object JavaDoc object) {
238         if (object instanceof List) {
239             return super.equals(object);
240         } else {
241             return false;
242         }
243     }
244     
245     protected class AEIndexListIteratorWrapper extends AEIndexIteratorWrapper implements ListIterator {
246         private final ListIterator listIterator;
247         private int lastReadIndex = 0;
248         
249         public AEIndexListIteratorWrapper(ListIterator innerIterator) {
250             super(innerIterator);
251             this.listIterator = innerIterator;
252         }
253         
254         public void set(Object JavaDoc obj) {
255             boolean fail = true;
256             innerTC.checkType(obj);
257             lock(true);
258             try {
259                 if (storage.eventsEnabled()) {
260                     AssociationEvent event = new AssociationEvent(
261                     source,
262                     AssociationEvent.EVENT_ASSOCIATION_SET,
263                     fixed, endName,
264                     lastRead, (RefObject) obj,
265                     lastReadIndex);
266                     notifier.firePlannedChange(source, event);
267                 }
268                 listIterator.set(unwrap(obj));
269                 if (!fixed.refOutermostPackage().equals(lastRead.refOutermostPackage())) {
270                     RefObject objectA;
271                     RefObject objectB;
272                     if (!queryFirstEnd) {
273                         objectA = lastRead;
274                         objectB = fixed;
275                     }
276                     else {
277                         objectA = fixed;
278                         objectB = lastRead;
279                     }
280                     AssociationHandler._removeExternalLink(source, objectA, objectB);
281                 }
282                 if (!fixed.refOutermostPackage().equals(((RefObject)obj).refOutermostPackage())) {
283                     RefObject objectA;
284                     RefObject objectB;
285                     if (!queryFirstEnd) {
286                         objectA = (RefObject) obj;
287                         objectB = fixed;
288                     }
289                     else {
290                         objectA = fixed;
291                         objectB = (RefObject) obj;
292                     }
293                     AssociationHandler._addExternalLink(source, objectA, objectB);
294                 }
295                 fail = false;
296             } finally {
297                 unlock(fail);
298             }
299         }
300         
301         public void add(Object JavaDoc obj) {
302             boolean fail = true;
303             innerTC.checkType(obj);
304             lock(true);
305             try {
306                 if (storage.eventsEnabled()) {
307                     AssociationEvent event = new AssociationEvent(
308                     source,
309                     AssociationEvent.EVENT_ASSOCIATION_ADD,
310                     fixed, endName,
311                     null, (RefObject) obj,
312                     nextIndex());
313                     notifier.firePlannedChange(source, event);
314                 }
315                 listIterator.add(unwrap(obj));
316                 if (!fixed.refOutermostPackage().equals(((RefObject)obj).refOutermostPackage())) {
317                     RefObject objectA;
318                     RefObject objectB;
319                     if (!queryFirstEnd) {
320                         objectA = (RefObject) obj;
321                         objectB = fixed;
322                     }
323                     else {
324                         objectA = fixed;
325                         objectB = (RefObject) obj;
326                     }
327                     AssociationHandler._addExternalLink(source, objectA, objectB);
328                 }
329                 fail = false;
330             } finally {
331                 unlock(fail);
332             }
333         }
334         
335         public int previousIndex() {
336             lock(false);
337             try {
338                 return listIterator.previousIndex();
339             } finally {
340                 unlock();
341             }
342         }
343         
344         public int nextIndex() {
345             lock(false);
346             try {
347                 return listIterator.nextIndex();
348             } finally {
349                 unlock();
350             }
351         }
352         
353         public boolean hasPrevious() {
354             lock(false);
355             try {
356                 return listIterator.hasPrevious();
357             } finally {
358                 unlock();
359             }
360         }
361         
362         public Object JavaDoc previous() {
363             lock(false);
364             try {
365                 lastReadIndex = previousIndex();
366                 return (lastRead = (RefObject) wrap(listIterator.previous()));
367             } finally {
368                 unlock();
369             }
370         }
371         
372         public Object JavaDoc next() {
373             lock(false);
374             try {
375                 lastReadIndex = nextIndex();
376                 return super.next();
377             } finally {
378                 unlock();
379             }
380         }
381         
382         public void remove() {
383             boolean fail = true;
384             lock(true);
385             try {
386                 if (storage.eventsEnabled()) {
387                     AssociationEvent event = new AssociationEvent(
388                     source,
389                     AssociationEvent.EVENT_ASSOCIATION_REMOVE,
390                     fixed,
391                     endName,
392                     lastRead,
393                     null,
394                     lastReadIndex);
395                     notifier.firePlannedChange(source, event);
396                 }
397                 innerIterator.remove();
398                 if (!fixed.refOutermostPackage().equals(lastRead.refOutermostPackage())) {
399                     RefObject objectA;
400                     RefObject objectB;
401                     if (!queryFirstEnd) {
402                         objectA = lastRead;
403                         objectB = fixed;
404                     }
405                     else {
406                         objectA = fixed;
407                         objectB = lastRead;
408                     }
409                     AssociationHandler._removeExternalLink(source, objectA, objectB);
410                 }
411                 fail = false;
412             } finally {
413                 unlock(fail);
414             }
415         }
416     }
417 }
418
Popular Tags