001    /*
002     * Copyright (c) 2003, 2006 IBM Corporation and others.
003     * All rights reserved.   This program and the accompanying materials
004     * are made available under the terms of the Eclipse Public License v1.0
005     * which accompanies this distribution, and is available at
006     * http://www.eclipse.org/legal/epl-v10.html
007     *
008     * Contributors:
009     *   IBM - initial API and implementation
010     *
011     * $Id: SubsetEObjectEList.java,v 1.5 2006/12/14 15:47:33 khussey Exp $
012     */
013    package org.eclipse.uml2.common.util;
014    
015    import java.util.Collection;
016    import java.util.Iterator;
017    
018    import org.eclipse.emf.common.notify.Notification;
019    import org.eclipse.emf.common.notify.NotificationChain;
020    import org.eclipse.emf.common.util.EList;
021    import org.eclipse.emf.ecore.EStructuralFeature;
022    import org.eclipse.emf.ecore.InternalEObject;
023    import org.eclipse.emf.ecore.resource.Resource;
024    import org.eclipse.emf.ecore.util.EObjectEList;
025    
026    @Deprecated
027    public class SubsetEObjectEList<E>
028                    extends EObjectEList<E> {
029    
030            private static final long serialVersionUID = 1L;
031    
032            public static class Unsettable<E>
033                            extends SubsetEObjectEList<E> {
034    
035                    private static final long serialVersionUID = 1L;
036    
037                    protected boolean isSet;
038    
039                    public Unsettable(Class<?> dataClass, InternalEObject owner,
040                                    int featureID, int[] supersetFeatureIDs) {
041                            super(dataClass, owner, featureID, supersetFeatureIDs);
042                    }
043    
044                    public Unsettable(Class<?> dataClass, InternalEObject owner,
045                                    int featureID, int supersetFeatureID) {
046                            this(dataClass, owner, featureID, new int[]{supersetFeatureID});
047                    }
048    
049                    @Override
050                    protected void didChange() {
051                            isSet = true;
052                    }
053    
054                    @Override
055                    public boolean isSet() {
056                            return isSet;
057                    }
058    
059                    @Override
060                    public void unset() {
061                            super.unset();
062    
063                            if (isNotificationRequired()) {
064                                    boolean oldIsSet = isSet;
065                                    isSet = false;
066    
067                                    owner.eNotify(createNotification(Notification.UNSET, oldIsSet,
068                                            false));
069                            } else {
070                                    isSet = false;
071                            }
072                    }
073            }
074    
075            protected final int[] supersetFeatureIDs;
076    
077            public SubsetEObjectEList(Class<?> dataClass, InternalEObject owner,
078                            int featureID, int[] supersetFeatureIDs) {
079                    super(dataClass, owner, featureID);
080    
081                    this.supersetFeatureIDs = supersetFeatureIDs;
082            }
083    
084            public SubsetEObjectEList(Class<?> dataClass, InternalEObject owner,
085                            int featureID, int supersetFeatureID) {
086                    this(dataClass, owner, featureID, new int[]{supersetFeatureID});
087            }
088    
089            protected void supersetAdd(Object object) {
090    
091                    if (supersetFeatureIDs != null) {
092                            Resource.Internal eInternalResource = owner.eInternalResource();
093    
094                            if (eInternalResource != null && eInternalResource.isLoading()) {
095                                    return;
096                            }
097    
098                            for (int i = 0; i < supersetFeatureIDs.length; i++) {
099                                    EStructuralFeature supersetEStructuralFeature = owner.eClass()
100                                            .getEStructuralFeature(supersetFeatureIDs[i]);
101    
102                                    if (supersetEStructuralFeature.isMany()) {
103                                            @SuppressWarnings("unchecked")
104                                            EList<Object> supersetEList = (EList<Object>) owner
105                                                    .eGet(supersetEStructuralFeature);
106    
107                                            if (!supersetEList.contains(object)) {
108                                                    supersetEList.add(object);
109                                            }
110                                    }
111                            }
112                    }
113            }
114    
115            @Override
116            public NotificationChain basicAdd(E object, NotificationChain notifications) {
117                    notifications = super.basicAdd(object, notifications);
118    
119                    supersetAdd(object);
120    
121                    return notifications;
122            }
123    
124            @Override
125            public NotificationChain basicSet(int index, E object,
126                            NotificationChain notifications) {
127                    notifications = super.basicSet(index, object, notifications);
128    
129                    supersetAdd(object);
130    
131                    return notifications;
132            }
133    
134            @Override
135            public void add(int index, E object) {
136                    super.add(index, object);
137    
138                    supersetAdd(object);
139            }
140    
141            @Override
142            public boolean add(E object) {
143                    boolean result = super.add(object);
144    
145                    supersetAdd(object);
146    
147                    return result;
148            }
149    
150            @Override
151            public boolean addAll(Collection<? extends E> collection) {
152                    boolean result = super.addAll(collection);
153    
154                    for (Iterator<? extends E> elements = collection.iterator(); elements
155                            .hasNext();) {
156    
157                            supersetAdd(elements.next());
158                    }
159    
160                    return result;
161            }
162    
163            @Override
164            public boolean addAll(int index, Collection<? extends E> collection) {
165                    boolean result = super.addAll(index, collection);
166    
167                    for (Iterator<? extends E> elements = collection.iterator(); elements
168                            .hasNext();) {
169    
170                            supersetAdd(elements.next());
171                    }
172    
173                    return result;
174            }
175    
176            @Override
177            public E set(int index, E object) {
178                    E result = super.set(index, object);
179    
180                    supersetAdd(object);
181    
182                    return result;
183            }
184    
185    }