blob: 82d778a4778938c6edff8f29c0d64d5d29da0cad [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.command;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.eclipse.epf.library.edit.ui.IActionTypeProvider;
import org.eclipse.epf.library.edit.util.ActivityHandler;
import org.eclipse.epf.library.edit.util.ProcessUtil;
import org.eclipse.epf.library.edit.util.TngUtil;
import org.eclipse.jface.viewers.Viewer;
import com.ibm.uma.Activity;
import com.ibm.uma.BreakdownElement;
import com.ibm.uma.CapabilityPattern;
import com.ibm.uma.DeliveryProcess;
import com.ibm.uma.DescribableElement;
import com.ibm.uma.Process;
import com.ibm.uma.ProcessPackage;
/**
* @author Phong Nguyen Le
* @since 1.0
*/
public class ActivityDropCommand extends BSDropCommand {
private Viewer viewer;
private List oldPatterns;
private List patterns;
private Process targetProcess;
private boolean isDeliveryProcess;
private boolean canExtend;
private int type;
public ActivityDropCommand(Activity target, List activities, Viewer viewer) {
super(target, activities);
this.viewer = viewer;
targetProcess = TngUtil.getOwningProcess(target);
isDeliveryProcess = targetProcess instanceof DeliveryProcess;
if (isDeliveryProcess) {
oldPatterns = new ArrayList(((DeliveryProcess) targetProcess)
.getIncludesPatterns());
}
for (Iterator iter = activities.iterator(); iter.hasNext();) {
Process proc = TngUtil.getOwningProcess((BreakdownElement) iter
.next());
if (proc instanceof CapabilityPattern && proc != targetProcess) {
canExtend = true;
break;
}
}
}
public void setType(int type) {
this.type = type;
}
/*
* (non-Javadoc)
*
* @see com.ibm.library.edit.process.command.BSDropCommand#execute()
*/
public void execute() {
IActionTypeProvider actionTypeProvider = (IActionTypeProvider) viewer;
if (canExtend && viewer != null) {
viewer = null;
// delegate the execution of this command
//
actionTypeProvider.execute(this);
return;
}
super.execute();
}
/*
* (non-Javadoc)
*
* @see com.ibm.library.edit.process.command.BSDropCommand#preExecute()
*/
protected boolean preExecute() {
if (!super.preExecute())
return false;
// List copiedActivities = new ArrayList();
ActivityHandler activityHandler = new ActivityHandler();
// process the selected activities and deep-copy the valid ones
//
if (isDeliveryProcess) {
patterns = new ArrayList();
}
// boolean copy = true;
for (Iterator iter = dropElements.iterator(); iter.hasNext();) {
Object element = (Object) iter.next();
if ((element instanceof Activity)
// && element != target
) {
Activity act = (Activity) element;
Process proc = TngUtil.getOwningProcess(act);
if (proc instanceof CapabilityPattern && proc != targetProcess) {
if (type == IActionTypeProvider.EXTEND) {
// copy = false;
activityHandler.extend(act);
if (patterns != null)
patterns.add(proc);
} else {
activityHandler.copy(act);
}
} else {
activityHandler.copy(act);
}
}
}
// String desc;
// if(copy) {
// desc = MessageFormat.format("Copy Activit{0}", new Object[]
// {dropElements.size() > 1 ? "ies" : "y"});
//
// }
// else {
// desc = MessageFormat.format("Extend Activit{0}", new Object[]
// {dropElements.size() > 1 ? "ies" : "y"});
// }
// setDescription(desc);
dropElements = activityHandler.getActivities();
return !dropElements.isEmpty();
}
/*
* (non-Javadoc)
*
* @see com.ibm.library.edit.process.command.BSDropCommand#doExecute()
*/
protected void doExecute() {
Iterator itor = dropElements.iterator();
ProcessPackage pkg = (ProcessPackage) activity.eContainer();
while (itor.hasNext()) {
Activity act = (Activity) itor.next();
List breakdownElements = activity.getBreakdownElements();
if (breakdownElements.size() > 0) {
setName(breakdownElements, act);
setDefaultPresentationName(breakdownElements, act);
}
if (act.eContainer() != null
&& act.eContainer().eContainer() != pkg) {
pkg.getChildPackages().add(act.eContainer());
}
if (patterns != null && !patterns.isEmpty()) {
DeliveryProcess proc = (DeliveryProcess) targetProcess;
for (Iterator iter = patterns.iterator(); iter.hasNext();) {
Object pattern = iter.next();
if (!proc.getIncludesPatterns().contains(pattern)) {
proc.getIncludesPatterns().add(pattern);
}
}
}
}
activity.getBreakdownElements().addAll(dropElements);
getModifiedResources().add(activity.eResource());
}
/*
* (non-Javadoc)
*
* @see com.ibm.library.edit.process.command.BSDropCommand#doUndo()
*/
protected void doUndo() {
activity.getBreakdownElements().removeAll(dropElements);
if (isDeliveryProcess) {
DeliveryProcess proc = (DeliveryProcess) targetProcess;
proc.getIncludesPatterns().clear();
proc.getIncludesPatterns().addAll(oldPatterns);
}
}
public static void setName(List siblings, Activity e) {
String baseName = e.getName();
if (!isNameTaken(siblings, e, baseName)) {
return;
}
for (int i = 1; true; i++) {
String name = baseName + '_' + i;
if (!isNameTaken(siblings, e, name)) {
e.setName(name);
return;
}
}
}
public static void setDefaultPresentationName(List siblings, Activity e) {
// if(e.getPresentationName() != null &&
// e.getPresentationName().trim().length() > 0) return;
String basePresentationName = ProcessUtil.getPresentationName(e);
if (!isPresentationNameTaken(siblings, e, basePresentationName)) {
// e.setPresentationName(basePresentationName);
return;
}
for (int i = 1; true; i++) {
String name = basePresentationName + '_' + i;
if (!isPresentationNameTaken(siblings, e, name)) {
e.setPresentationName(name);
return;
}
}
}
private static boolean isNameTaken(List siblings, DescribableElement e,
String name) {
for (int i = siblings.size() - 1; i > -1; i--) {
BreakdownElement sibling = (BreakdownElement) siblings.get(i);
// if(sibling != e && name.equals(sibling.getPresentationName())) {
if (sibling != e && name.equals(sibling.getName())) {
return true;
}
}
return false;
}
private static boolean isPresentationNameTaken(List siblings,
DescribableElement e, String name) {
for (int i = siblings.size() - 1; i > -1; i--) {
BreakdownElement sibling = (BreakdownElement) siblings.get(i);
// if(sibling != e && name.equals(sibling.getPresentationName())) {
if (sibling != e
&& name.equals(ProcessUtil.getPresentationName(sibling))) {
return true;
}
}
return false;
}
}