KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > jmiimpl > javamodel > ReferenceListWrapper


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.modules.javacore.jmiimpl.javamodel;
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 import org.netbeans.mdr.handlers.*;
27
28 /** This list wrapper is intended to assotiations persistent outside of MDR
29  * repository, in this case in inner list. So this wrapper simulates MDR event
30  * system and asks MDR for lock/unlock support.
31  * Wrapper can also verify check type for add/remove operations.
32  *
33  * @author Vladimir Hudec
34  */

35 public class ReferenceListWrapper extends ReferenceColWrapper implements List {
36     private List innerList;
37     
38     /** Creates new ListWrapper */
39     public ReferenceListWrapper(MdrStorage storage, AssociationHandler source, RefObject fixed, String JavaDoc endName, SemiPersistentElement parent, int changeMask, List inner) {
40         super(storage, source, fixed, endName, parent, changeMask);
41         setInnerList(inner);
42     }
43     
44     public void setInnerList(List inner) {
45         super.setInnerList(inner);
46         this.innerList = inner;
47     }
48     
49     public List getInnerList() {
50         return innerList;
51     }
52     
53     /** List operations */
54
55     public Object JavaDoc remove(int param) {
56         boolean fail = true;
57         lock(true);
58         try {
59             Object JavaDoc result = (RefObject) get(param);
60             if (storage.eventsEnabled()) {
61                 AssociationEvent event = new AssociationEvent(
62                 source,
63                 AssociationEvent.EVENT_ASSOCIATION_REMOVE,
64                 fixed, endName,
65                 (RefObject) result, null,
66                 param);
67                 notifier.firePlannedChange(source, event);
68             }
69             innerList.remove(param);
70             objectChanged(result);
71             fail = false;
72             return result;
73         } finally {
74             unlock(fail);
75         }
76     }
77     
78     public void add(int param, Object JavaDoc obj) {
79         checkType(obj);
80         boolean fail = true;
81         lock(true);
82         try {
83             if (storage.eventsEnabled()) {
84                 AssociationEvent event = new AssociationEvent(
85                 source,
86                 AssociationEvent.EVENT_ASSOCIATION_ADD,
87                 fixed, endName,
88                 null, (RefObject) obj,
89                 param);
90                 notifier.firePlannedChange(source, event);
91             }
92             innerList.add(param, obj);
93             objectChanged(obj);
94             fail = false;
95         } finally {
96             unlock(fail);
97         }
98     }
99     
100     
101     public Object JavaDoc set(int param, Object JavaDoc obj) {
102         checkType(obj);
103         boolean fail = true;
104         lock(true);
105         try {
106             Object JavaDoc result = get(param);
107             if (storage.eventsEnabled()) {
108                 AssociationEvent event = new AssociationEvent(
109                 source,
110                 AssociationEvent.EVENT_ASSOCIATION_SET,
111                 fixed, endName,
112                 (RefObject) result, (RefObject) obj,
113                 param);
114                 notifier.firePlannedChange(source, event);
115             }
116             innerList.set(param, obj);
117             objectChanged(result);
118             objectChanged(obj);
119             fail = false;
120             return result;
121         } finally {
122             unlock(fail);
123         }
124     }
125     
126     public boolean addAll(int param, Collection collection) {
127         boolean fail = true;
128         lock(true);
129         try {
130             ListIterator lit = listIterator(param);
131             for (Iterator it = collection.iterator(); it.hasNext();) {
132                 lit.add(it.next());
133             }
134             fail = false;
135             return true;
136         } finally {
137             unlock(fail);
138         }
139     }
140     
141     public int indexOf(Object JavaDoc obj) {
142         lock(false);
143         try {
144             return innerList.indexOf(obj);
145         } finally {
146             unlock();
147         }
148     }
149     
150     public int lastIndexOf(Object JavaDoc obj) {
151         lock(false);
152         try {
153             return innerList.lastIndexOf(obj);
154         } finally {
155             unlock();
156         }
157     }
158     
159     public Object JavaDoc get(int param) {
160         lock(false);
161         try {
162             return innerList.get(param);
163         } finally {
164             unlock();
165         }
166     }
167     
168     public ListIterator listIterator() {
169         lock(false);
170         try {
171             return new ReferenceListIteratorWrapper(innerList.listIterator());
172         } finally {
173             unlock();
174         }
175     }
176     
177     public ListIterator listIterator(int param) {
178         lock(false);
179         try {
180             return new ReferenceListIteratorWrapper(innerList.listIterator(param));
181         } finally {
182             unlock();
183         }
184     }
185     
186     public List subList(int param, int param1) {
187         throw new UnsupportedOperationException JavaDoc();
188         // return new ListWrapper(((List) innerList).subList(param, param1), source);
189
}
190     
191     public boolean equals(Object JavaDoc object) {
192         if (object instanceof List) {
193             return super.equals(object);
194         } else {
195             return false;
196         }
197     }
198     
199     protected class ReferenceListIteratorWrapper extends ReferenceIteratorWrapper implements ListIterator {
200         private final ListIterator listIterator;
201         private int lastReadIndex = 0;
202         
203         public ReferenceListIteratorWrapper(ListIterator innerIterator) {
204             super(innerIterator);
205             this.listIterator = innerIterator;
206         }
207         
208         public void set(Object JavaDoc obj) {
209             testModCount();
210             checkType(obj);
211             boolean fail = true;
212             lock(true);
213             try {
214                 if (storage.eventsEnabled()) {
215                     AssociationEvent event = new AssociationEvent(
216                     source,
217                     AssociationEvent.EVENT_ASSOCIATION_SET,
218                     fixed, endName,
219                     (RefObject) lastRead, (RefObject) obj,
220                     lastReadIndex);
221                     notifier.firePlannedChange(source, event);
222                 }
223                 listIterator.set(obj);
224                 objectChanged(obj);
225                 objectChanged(lastRead);
226                 fail = false;
227             } finally {
228                 unlock(fail);
229             }
230         }
231         
232         public void add(Object JavaDoc obj) {
233             testModCount();
234             checkType(obj);
235             boolean fail = true;
236             lock(true);
237             try {
238                 if (storage.eventsEnabled()) {
239                     AssociationEvent event = new AssociationEvent(
240                     source,
241                     AssociationEvent.EVENT_ASSOCIATION_ADD,
242                     fixed, endName,
243                     null, (RefObject) obj,
244                     nextIndex());
245                     notifier.firePlannedChange(source, event);
246                 }
247                 listIterator.add(obj);
248                 objectChanged(obj);
249                 fail = false;
250             } finally {
251                 unlock(fail);
252             }
253         }
254         
255         public int previousIndex() {
256             testModCount();
257             lock(false);
258             try {
259                 return listIterator.previousIndex();
260             } finally {
261                 unlock();
262             }
263         }
264         
265         public int nextIndex() {
266             testModCount();
267             lock(false);
268             try {
269                 return listIterator.nextIndex();
270             } finally {
271                 unlock();
272             }
273         }
274         
275         public boolean hasPrevious() {
276             testModCount();
277             lock(false);
278             try {
279                 return listIterator.hasPrevious();
280             } finally {
281                 unlock();
282             }
283         }
284         
285         public Object JavaDoc previous() {
286             testModCount();
287             lock(false);
288             try {
289                 lastReadIndex = previousIndex();
290                 return (lastRead = (RefObject) listIterator.previous());
291             } finally {
292                 unlock();
293             }
294         }
295         
296         public Object JavaDoc next() {
297             testModCount();
298             lock(false);
299             try {
300                 lastReadIndex = nextIndex();
301                 return super.next();
302             } finally {
303                 unlock();
304             }
305         }
306         
307         public void remove() {
308             testModCount();
309             checkType(lastRead);
310             boolean fail = true;
311             lock(true);
312             try {
313                 if (storage.eventsEnabled()) {
314                     AssociationEvent event = new AssociationEvent(
315                     source,
316                     AssociationEvent.EVENT_ASSOCIATION_REMOVE,
317                     fixed,
318                     endName,
319                     (RefObject) lastRead,
320                     null,
321                     lastReadIndex);
322                     notifier.firePlannedChange(source, event);
323                 }
324                 innerIterator.remove();
325                 objectChanged(lastRead);
326                 fail = false;
327             } finally {
328                 unlock(fail);
329             }
330         }
331     }
332 }
333
Popular Tags