blob: 9b2893d701066923e82d428432daaa4ac6c0905d [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.process;
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.Notification;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.edit.command.CommandParameter;
import org.eclipse.emf.edit.provider.ITreeItemContentProvider;
import org.eclipse.emf.edit.provider.ViewerNotification;
import org.eclipse.epf.library.edit.IStatefulItemProvider;
import org.eclipse.epf.library.edit.util.TngUtil;
import com.ibm.uma.Activity;
import com.ibm.uma.Phase;
import com.ibm.uma.Process;
import com.ibm.uma.UmaPackage;
import com.ibm.uma.provider.UmaEditPlugin;
/**
* @author Phong Nguyen Le
* @since 1.0
*/
public class ActivityItemProvider extends
com.ibm.uma.provider.ActivityItemProvider implements
IStatefulItemProvider {
private Object parent;
/**
* @param adapterFactory
*/
public ActivityItemProvider(AdapterFactory adapterFactory) {
super(adapterFactory);
}
/*
* (non-Javadoc)
*
* @see org.eclipse.emf.edit.provider.ItemProviderAdapter#getText(java.lang.Object)
*/
public String getText(Object object) {
String label = ((Activity) object).getName();
return label == null || label.length() == 0 ? getString("_UI_Activity_type") : //$NON-NLS-1$
label;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.emf.edit.provider.ItemProviderAdapter#collectNewChildDescriptors(java.util.Collection,
* java.lang.Object)
*/
protected void collectNewChildDescriptors(Collection newChildDescriptors,
Object object) {
}
/*
* (non-Javadoc)
*
* @see org.eclipse.emf.edit.provider.ItemProviderAdapter#getChildrenFeatures(java.lang.Object)
*/
public Collection getChildrenFeatures(Object object) {
if (childrenFeatures == null) {
childrenFeatures = new ArrayList();
childrenFeatures.add(UmaPackage.eINSTANCE
.getActivity_BreakdownElements());
}
return childrenFeatures;
}
protected void setParentFor(Object child, Object parent) {
Object adapter = TngUtil.getBestAdapterFactory(adapterFactory).adapt(
child, ITreeItemContentProvider.class);
if (adapter instanceof ActivityItemProvider) {
((ActivityItemProvider) adapter).setParent(parent);
} else {
if (TngUtil.DEBUG) {
System.out.println("Could not set parent for child " + child); //$NON-NLS-1$
System.out.println(" child's adapter: " + adapter); //$NON-NLS-1$
}
}
}
void setParent(Object parent) {
this.parent = parent;
}
public Object getParent(Object object) {
if (parent != null)
return parent;
return super.getParent(object);
}
/*
* (non-Javadoc)
*
* @see org.eclipse.emf.edit.provider.ItemProviderAdapter#getChildren(java.lang.Object)
*/
public Collection getChildren(Object object) {
ChildrenStore store = getChildrenStore(object);
if (store != null) {
return store.getChildren();
}
// store = createChildrenStore(object);
List result = store != null ? null : new ArrayList();
EObject eObject = (EObject) object;
for (Iterator i = getChildrenFeatures(object).iterator(); i.hasNext();) {
EStructuralFeature feature = (EStructuralFeature) i.next();
if (feature.isMany()) {
List children = (List) eObject.eGet(feature);
int index = 0;
for (Iterator ci = children.iterator(); ci.hasNext(); index++) {
Object child = ci.next();
if (acceptAsChild(child)) {
child = wrap(eObject, feature, child, index);
setParentFor(child, object);
if (store != null) {
store.getList(feature).add(child);
} else {
result.add(child);
}
}
}
} else {
Object child = eObject.eGet(feature);
if (acceptAsChild(child)) {
child = wrap(eObject, feature, child,
CommandParameter.NO_INDEX);
setParentFor(child, object);
if (store != null) {
store.setValue(feature, child);
} else {
result.add(child);
}
}
}
}
return store != null ? store.getChildren() : result;
}
/**
* @param child
* @return
*/
protected boolean acceptAsChild(Object child) {
return child instanceof Activity;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.emf.common.notify.Adapter#notifyChanged(org.eclipse.emf.common.notify.Notification)
*/
public void notifyChanged(Notification notification) {
updateChildren(notification);
switch (notification.getFeatureID(Activity.class)) {
case UmaPackage.ACTIVITY__BREAKDOWN_ELEMENTS:
switch (notification.getEventType()) {
case Notification.ADD:
case Notification.ADD_MANY:
case Notification.REMOVE:
case Notification.REMOVE_MANY:
case Notification.MOVE:
fireNotifyChanged(new ViewerNotification(notification,
notification.getNotifier(), true, false));
return;
}
return;
}
super.notifyChanged(notification);
}
public Object getImage(Object object) {
if (object instanceof Phase) {
return UmaEditPlugin.INSTANCE.getImage("full/obj16/Phase"); //$NON-NLS-1$
} else if (object instanceof Process) {
return UmaEditPlugin.INSTANCE.getImage("full/obj16/Process"); //$NON-NLS-1$
}
return super.getImage(object);
}
}