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: SupersetEObjectEList.java,v 1.3 2006/12/14 15:47:32 khussey Exp $
012 */
013 package org.eclipse.uml2.common.util;
014
015 import org.eclipse.emf.common.notify.Notification;
016 import org.eclipse.emf.common.notify.NotificationChain;
017 import org.eclipse.emf.common.util.EList;
018 import org.eclipse.emf.ecore.EStructuralFeature;
019 import org.eclipse.emf.ecore.InternalEObject;
020 import org.eclipse.emf.ecore.util.EObjectEList;
021
022 @Deprecated
023 public class SupersetEObjectEList<E>
024 extends EObjectEList<E> {
025
026 private static final long serialVersionUID = 1L;
027
028 public static class Unsettable<E>
029 extends SupersetEObjectEList<E> {
030
031 private static final long serialVersionUID = 1L;
032
033 protected boolean isSet;
034
035 public Unsettable(Class<?> dataClass, InternalEObject owner,
036 int featureID, int[] subsetFeatureIDs) {
037 super(dataClass, owner, featureID, subsetFeatureIDs);
038 }
039
040 public Unsettable(Class<?> dataClass, InternalEObject owner,
041 int featureID, int subsetFeatureID) {
042 this(dataClass, owner, featureID, new int[]{subsetFeatureID});
043 }
044
045 @Override
046 protected void didChange() {
047 isSet = true;
048 }
049
050 @Override
051 public boolean isSet() {
052 return isSet;
053 }
054
055 @Override
056 public void unset() {
057 super.unset();
058
059 if (isNotificationRequired()) {
060 boolean oldIsSet = isSet;
061 isSet = false;
062
063 owner.eNotify(createNotification(Notification.UNSET, oldIsSet,
064 false));
065 } else {
066 isSet = false;
067 }
068 }
069 }
070
071 protected final int[] subsetFeatureIDs;
072
073 public SupersetEObjectEList(Class<?> dataClass, InternalEObject owner,
074 int featureID, int[] subsetFeatureIDs) {
075 super(dataClass, owner, featureID);
076
077 this.subsetFeatureIDs = subsetFeatureIDs;
078 }
079
080 public SupersetEObjectEList(Class<?> dataClass, InternalEObject owner,
081 int featureID, int subsetFeatureID) {
082 this(dataClass, owner, featureID, new int[]{subsetFeatureID});
083 }
084
085 protected void subsetRemove(Object object) {
086
087 if (subsetFeatureIDs != null) {
088
089 for (int i = 0; i < subsetFeatureIDs.length; i++) {
090 EStructuralFeature subsetEStructuralFeature = owner.eClass()
091 .getEStructuralFeature(subsetFeatureIDs[i]);
092
093 if (subsetEStructuralFeature.isMany()) {
094 ((EList<?>) owner.eGet(subsetEStructuralFeature))
095 .remove(object);
096 } else if (object.equals(owner.eGet(subsetEStructuralFeature))) {
097 owner.eSet(subsetEStructuralFeature, null);
098 }
099 }
100 }
101 }
102
103 @Override
104 protected void didRemove(int index, E oldObject) {
105 super.didRemove(index, oldObject);
106
107 subsetRemove(oldObject);
108 }
109
110 @Override
111 public NotificationChain basicSet(int index, E object,
112 NotificationChain notifications) {
113 Object oldObject = data[index];
114
115 notifications = super.basicSet(index, object, notifications);
116
117 if (oldObject != object) {
118 subsetRemove(oldObject);
119 }
120
121 return notifications;
122 }
123
124 @Override
125 public E set(int index, E object) {
126 E result = super.set(index, object);
127
128 if (result != object) {
129 subsetRemove(result);
130 }
131
132 return result;
133 }
134
135 }