KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.*;
26 import org.netbeans.mdr.storagemodel.*;
27
28 /**
29  *
30  * @author Martin Matula
31  */

32 public class AEIndexSetWrapper extends IndexSetWrapper {
33     protected final AssociationHandler source;
34     protected final EventNotifier.Association notifier;
35     protected final TypedCollection innerTC;
36     protected final RefObject fixed;
37     protected final String JavaDoc endName;
38     protected final boolean queryFirstEnd;
39     
40     /** Creates new CollectionWrapper */
41     public AEIndexSetWrapper(AssociationHandler source, RefObject fixed, String JavaDoc endName, AssocEndIndexSet inner, boolean queryFirstEnd) {
42         super(source._getMdrStorage(), inner);
43         this.source = source;
44         this.notifier = storage.getEventNotifier().ASSOCIATION;
45         this.innerTC = inner;
46         this.fixed = fixed;
47         this.endName = endName;
48         this.queryFirstEnd = queryFirstEnd;
49     }
50     
51     public void clear() {
52         boolean fail = true;
53         lock(true);
54         try {
55             Object JavaDoc elements[] = inner.toArray();
56             for (int i = 0; i < elements.length; i++) {
57                 remove(wrap(elements[i]));
58             }
59             fail = false;
60         } finally {
61             unlock(fail);
62         }
63     }
64     
65     public boolean addAll(Collection collection) {
66         boolean fail = true;
67         lock(true);
68         try {
69             boolean result = false;
70             for (Iterator it = collection.iterator(); it.hasNext();) {
71                 result |= add(it.next());
72             }
73             fail = false;
74             return result;
75         } finally {
76             unlock(fail);
77         }
78     }
79     
80     public boolean retainAll(Collection collection) {
81         boolean fail = true;
82         lock(true);
83         try {
84             boolean result = false;
85             Object JavaDoc elements[] = inner.toArray();
86             for (int i = 0; i < elements.length; i++) {
87                 Object JavaDoc w = wrap(elements[i]);
88                 if (!collection.contains(w)) {
89                     remove(w);
90                     result = true;
91                 }
92             }
93             fail = false;
94             return result;
95         } finally {
96             unlock(fail);
97         }
98     }
99     
100     public boolean removeAll(Collection collection) {
101         boolean fail = true;
102         lock(true);
103         try {
104             boolean result = false;
105             for (Iterator it = collection.iterator(); it.hasNext();) {
106                 result |= remove(it.next());
107             }
108             fail = false;
109             return result;
110         } finally {
111             unlock(fail);
112         }
113     }
114     
115     public boolean remove(Object JavaDoc obj) {
116         boolean fail = true;
117         innerTC.checkType(obj);
118         lock(true);
119         try {
120             if (storage.eventsEnabled()) {
121                 AssociationEvent event = new AssociationEvent(
122                 source,
123                 AssociationEvent.EVENT_ASSOCIATION_REMOVE,
124                 fixed,
125                 endName,
126                 (RefObject) obj,
127                 null,
128                 AssociationEvent.POSITION_NONE);
129                 notifier.firePlannedChange(source, event);
130             }
131             boolean result = inner.remove(unwrap(obj));
132             if (!this.fixed.refOutermostPackage().equals(((RefObject)obj).refOutermostPackage())) {
133                 RefObject objectA;
134                 RefObject objectB;
135                 if (!this.queryFirstEnd) {
136                     objectA = (RefObject) obj;
137                     objectB = this.fixed;
138                 }
139                 else {
140                     objectA = this.fixed;
141                     objectB = (RefObject) obj;
142                 }
143                 AssociationHandler._removeExternalLink(this.source,objectA,objectB);
144             }
145             fail = false;
146             return result;
147         } finally {
148             unlock(fail);
149         }
150     }
151     
152     public boolean add(Object JavaDoc obj) {
153         boolean fail = true;
154         innerTC.checkType(obj);
155         lock(true);
156         try {
157             if (storage.eventsEnabled()) {
158                 AssociationEvent event = new AssociationEvent(
159                 source,
160                 AssociationEvent.EVENT_ASSOCIATION_ADD,
161                 fixed,
162                 endName,
163                 null,
164                 (RefObject) obj,
165                 AssociationEvent.POSITION_NONE);
166                 notifier.firePlannedChange(source, event);
167             }
168             boolean result = inner.add(unwrap(obj));
169             if (!this.fixed.refOutermostPackage().equals(((RefObject)obj).refOutermostPackage())) {
170                 RefObject objectA;
171                 RefObject objectB;
172                 if (!this.queryFirstEnd) {
173                     objectA = (RefObject) obj;
174                     objectB = this.fixed;
175                 }
176                 else {
177                     objectA = this.fixed;
178                     objectB = (RefObject) obj;
179                 }
180                 AssociationHandler._addExternalLink(this.source,objectA,objectB);
181             }
182             fail = false;
183             return result;
184         } finally {
185             unlock(fail);
186         }
187     }
188     
189     public Iterator iterator() {
190         lock(false);
191         try {
192             return new AEIndexIteratorWrapper(inner.iterator());
193         } finally {
194             unlock();
195         }
196     }
197     
198     protected class AEIndexIteratorWrapper extends IndexIteratorWrapper {
199         protected RefObject lastRead = null;
200         
201         public AEIndexIteratorWrapper(Iterator innerIterator) {
202             super(innerIterator);
203         }
204         
205         public Object JavaDoc next() {
206             return (lastRead = (RefObject) super.next());
207         }
208         
209         public void remove() {
210             boolean fail = true;
211             lock(true);
212             try {
213                 if (storage.eventsEnabled()) {
214                     AssociationEvent event = new AssociationEvent(
215                     source,
216                     AssociationEvent.EVENT_ASSOCIATION_REMOVE,
217                     fixed,
218                     endName,
219                     lastRead,
220                     null,
221                     AssociationEvent.POSITION_NONE);
222                     notifier.firePlannedChange(source, event);
223                 }
224                 innerIterator.remove();
225                 if (!fixed.refOutermostPackage().equals(lastRead.refOutermostPackage())) {
226                     RefObject objectA;
227                     RefObject objectB;
228                     if (!queryFirstEnd) {
229                         objectA = lastRead;
230                         objectB = fixed;
231                     }
232                     else {
233                         objectA = fixed;
234                         objectB = lastRead;
235                     }
236                     AssociationHandler._removeExternalLink(source,objectA,objectB);
237                 }
238                 fail = false;
239             } finally {
240                 unlock(fail);
241             }
242         }
243     }
244 }
245
Popular Tags