在BPM Diagram中实现Boundary Event的中断与非中断。
一、在org.autumn.bpm.diagram插件中增加扩展点
<extension
point="org.eclipse.gmf.runtime.common.ui.services.action.contributionItemProviders">
<contributionItemProvider
checkPluginLoaded="true"
class="org.autumn.bpm.diagram.providers.DiagramActionProvider">
<Priority
name="Low">
</Priority>
<popupContribution
class="org.eclipse.gmf.runtime.diagram.ui.providers.DiagramContextMenuProvider">
<popupAction
id="SetInterruptingAction"
path="/additionsGroup">
</popupAction>
<popupStructuredContributionCriteria
objectClass="org.autumn.bpm.model.diagram.edit.parts.BoundaryEventEditPart"
objectCount="1">
</popupStructuredContributionCriteria>
</popupContribution>
<popupContribution
class="org.eclipse.gmf.runtime.diagram.ui.providers.DiagramContextMenuProvider">
<popupAction
id="SetInterruptingAction"
path="/additionsGroup">
</popupAction>
<popupStructuredContributionCriteria
objectClass="org.autumn.bpm.model.diagram.edit.parts.BoundaryEvent2EditPart"
objectCount="1">
</popupStructuredContributionCriteria>
</popupContribution>
</contributionItemProvider>
</extension>
在DiagramActionProvider中创建SetInterruptingAction。
package org.autumn.bpm.diagram.providers;
import org.autumn.bpm.diagram.action.SetInterruptingAction;
import org.eclipse.gmf.runtime.common.ui.services.action.contributionitem.AbstractContributionItemProvider;
import org.eclipse.gmf.runtime.common.ui.util.IWorkbenchPartDescriptor;
import org.eclipse.jface.action.IAction;
import org.eclipse.ui.IWorkbenchPage;
/**
* Provide Action for Diagram context menu.
*
* @author sunny
*
*/
public class DiagramActionProvider extends AbstractContributionItemProvider {
@Override
protected IAction createAction(String actionId, IWorkbenchPartDescriptor partDescriptor) {
IWorkbenchPage workbenchPage = partDescriptor.getPartPage();
if (SetInterruptingAction.ID.equals(actionId)) {
return new SetInterruptingAction(workbenchPage);
}
return super.createAction(actionId, partDescriptor);
}
}
在扩展中注册SetInterruptingAction到diagram的右键菜单,并与BoundaryEventEditPart、BoundaryEvent2EditPart绑定。
SetInterruptingAction在init方法中设置好注册的ID,并可以设置右键菜单上对应的图标。SetInterruptingAction在refresh方法中设置右键菜单上Action示意的Label以及Action是否可用。SetInterruptingAction在doRun方法中执行Action涉及的命令,比如设置BoundaryEvent的cancelActivity属性。
package org.autumn.bpm.diagram.action;
import org.autumn.bpm.model.BoundaryEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.common.ui.action.AbstractActionHandler;
import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
/**
* Sets the cancelActivity property of Boundary Event. Repaints the Boundary
* Event figure.
*
* @author sunny
*
*/
public class SetInterruptingAction extends AbstractActionHandler {
public static final String ID = "SetInterruptingAction"; //$NON-NLS-1$
public SetInterruptingAction(IWorkbenchPart workbenchPart) {
super(workbenchPart);
}
public SetInterruptingAction(IWorkbenchPage workbenchPage) {
super(workbenchPage);
}
@Override
public void init() {
super.init();
setId(ID);
setDescription("Sets the cancelActivity property of the selected element");
setToolTipText(getDescription());
}
@Override
public void refresh() {
IStructuredSelection selection = getStructuredSelection();
if (selection == null || (selection.isEmpty())) {
setEnabled(false);
return;
}
if (selection.getFirstElement() instanceof IGraphicalEditPart) {
EObject object = ((IGraphicalEditPart) selection.getFirstElement()).getNotationView().getElement();
if (object instanceof BoundaryEvent) {
BoundaryEvent boundaryEvent = (BoundaryEvent) object;
String name = boundaryEvent.getName() == null ? "BoundaryEvent" : boundaryEvent.getName();
if (boundaryEvent.isCancelActivity()) {
setText("Set " + name + " as Non-Interrupting");
} else {
setText("Set " + name + " as Interrupting");
}
setEnabled(true);
return;
}
}
setText("Sets the interrupting property");
setEnabled(false);
}
@Override
protected void doRun(IProgressMonitor progressMonitor) {
IStructuredSelection selection = getStructuredSelection();
if (selection == null || (selection.isEmpty())) {
return;
}
if (selection.getFirstElement() instanceof IGraphicalEditPart) {
IGraphicalEditPart part = (IGraphicalEditPart) selection.getFirstElement();
final EObject object = ((IGraphicalEditPart) selection.getFirstElement()).getNotationView().getElement();
if (object instanceof BoundaryEvent) {
part.getDiagramEditDomain().getDiagramCommandStack().execute(new ICommandProxy(new MyCommand(object) {
@Override
protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info)
throws ExecutionException {
if (object instanceof BoundaryEvent) {
((BoundaryEvent) object).setCancelActivity(!(((BoundaryEvent) object).isCancelActivity()));
}
return CommandResult.newOKCommandResult();
}
}), progressMonitor);
}
}
}
private abstract class MyCommand extends AbstractTransactionalCommand {
public MyCommand(EObject elt) {
super((TransactionalEditingDomain) AdapterFactoryEditingDomain.getEditingDomainFor(elt),
"SetInterruptingAction", getWorkspaceFiles(elt));
}
}
}
二、处理BoundaryEventEditPart的监听事件
/**
* @author sunny
*/
@Override
protected void handleNotificationEvent(Notification notification) {
if (notification.getEventType() == Notification.SET || notification.getEventType() == Notification.UNSET) {
if (ModelPackage.eINSTANCE.getBoundaryEvent_CancelActivity().equals(notification.getFeature())) {
getPrimaryShape().setInterrupting(notification.getNewBooleanValue());
}
}
super.handleNotificationEvent(notification);
}
在cancelActivity的属性发生set或unset事件后重画BoundaryEvent图形
/**
* @author sunny
*/
public void setInterrupting(boolean isInterrupting) {
this.isInterrupting = isInterrupting;
revalidate();
repaint();
}
@Override
protected void paintOuter(Graphics graphics) {
ActivityPainter.paintEventIntermediate(graphics, this.getBoundsForHandle(), isInterrupting);
}
在创建BoundaryEvent的时候,把BoundaryEvent模型的cancelActivity赋值给BoundaryEvent图形的isInterrupting属性
/**
* @generated NOT
* @author sunny
*/
private void createContents() {
createContentsGen();
if (resolveSemanticElement() instanceof BoundaryEvent) {
BoundaryEvent boundaryEvent = (BoundaryEvent) resolveSemanticElement();
isInterrupting = boundaryEvent.isCancelActivity();
}
}