blob: b792ef6b1de91b4073cfc90bb7bf02149b7d17e2 [file] [log] [blame]
//------------------------------------------------------------------------------
// Copyright (c) 2005, 2006 IBM Corporation and others.
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// Contributors:
// IBM Corporation - initial implementation
//------------------------------------------------------------------------------
package org.eclipse.epf.library.edit.category;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.notify.Notifier;
/**
* @author Phong Nguyen Le
* @since 1.0
*/
public class ObjectLinkItemProviderList {
public static List createList(AdapterFactory adapterFactory, List oldList,
Object parent, Collection children) {
List newChildren = new ArrayList();
for (Iterator iter = children.iterator(); iter.hasNext();) {
Notifier obj = (Notifier) iter.next();
// look if there is an existing ObjectLinkItemProvider for this task
// in the current children list first
ObjectLinkItemProvider child = findExistingObjectLinkItemProvider(
oldList, obj);
if (child == null) {
child = new ObjectLinkItemProvider(adapterFactory, parent, obj);
} else {
oldList.remove(child);
}
newChildren.add(child);
}
// dispose the old children
if (oldList != null) {
for (Iterator iter = oldList.iterator(); iter.hasNext();) {
ObjectLinkItemProvider child = (ObjectLinkItemProvider) iter
.next();
child.dispose();
}
}
return newChildren;
}
private static ObjectLinkItemProvider findExistingObjectLinkItemProvider(
List children, Object obj) {
if (children == null || children.isEmpty())
return null;
for (Iterator iter = children.iterator(); iter.hasNext();) {
ObjectLinkItemProvider child = (ObjectLinkItemProvider) iter.next();
if (child.getLinkedObject() == obj)
return child;
}
return null;
}
}