前言
本人在做此功能的時候在網上搜了一圈,都是基于activiti5或activiti6實作的。如果你的項目使用的activiti7實作流程跟蹤/曆史圖,那麼恭喜你下面的代碼拷貝到你的項目就行了,下面代碼實作的最終效果如下圖:
代碼
- 重寫DefaultProcessDiagramGenerator類
import java.awt.*;
import java.io.InputStream;
import java.util.*;
import java.util.List;
import org.activiti.bpmn.model.Activity;
import org.activiti.bpmn.model.Artifact;
import org.activiti.bpmn.model.Association;
import org.activiti.bpmn.model.AssociationDirection;
import org.activiti.bpmn.model.BaseElement;
import org.activiti.bpmn.model.BoundaryEvent;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.bpmn.model.BusinessRuleTask;
import org.activiti.bpmn.model.CallActivity;
import org.activiti.bpmn.model.CompensateEventDefinition;
import org.activiti.bpmn.model.EndEvent;
import org.activiti.bpmn.model.ErrorEventDefinition;
import org.activiti.bpmn.model.Event;
import org.activiti.bpmn.model.EventDefinition;
import org.activiti.bpmn.model.EventGateway;
import org.activiti.bpmn.model.EventSubProcess;
import org.activiti.bpmn.model.ExclusiveGateway;
import org.activiti.bpmn.model.FlowElement;
import org.activiti.bpmn.model.FlowElementsContainer;
import org.activiti.bpmn.model.FlowNode;
import org.activiti.bpmn.model.Gateway;
import org.activiti.bpmn.model.GraphicInfo;
import org.activiti.bpmn.model.InclusiveGateway;
import org.activiti.bpmn.model.IntermediateCatchEvent;
import org.activiti.bpmn.model.Lane;
import org.activiti.bpmn.model.ManualTask;
import org.activiti.bpmn.model.MessageEventDefinition;
import org.activiti.bpmn.model.MultiInstanceLoopCharacteristics;
import org.activiti.bpmn.model.ParallelGateway;
import org.activiti.bpmn.model.Pool;
import org.activiti.bpmn.model.Process;
import org.activiti.bpmn.model.ReceiveTask;
import org.activiti.bpmn.model.ScriptTask;
import org.activiti.bpmn.model.SendTask;
import org.activiti.bpmn.model.SequenceFlow;
import org.activiti.bpmn.model.ServiceTask;
import org.activiti.bpmn.model.SignalEventDefinition;
import org.activiti.bpmn.model.StartEvent;
import org.activiti.bpmn.model.SubProcess;
import org.activiti.bpmn.model.Task;
import org.activiti.bpmn.model.TextAnnotation;
import org.activiti.bpmn.model.ThrowEvent;
import org.activiti.bpmn.model.TimerEventDefinition;
import org.activiti.bpmn.model.UserTask;
import org.activiti.bpmn.model.Transaction;
import org.activiti.image.ProcessDiagramGenerator;
import org.activiti.image.exception.ActivitiInterchangeInfoNotFoundException;
import org.activiti.image.exception.ActivitiImageException;
/**
1. Class to generate an svg based the diagram interchange information in a
2. BPMN 2.0 process.
*/
public class DefaultProcessDiagramGenerator implements ProcessDiagramGenerator {
private static final String DEFAULT_ACTIVITY_FONT_NAME = "Arial";
private static final String DEFAULT_LABEL_FONT_NAME = "Arial";
private static final String DEFAULT_ANNOTATION_FONT_NAME = "Arial";
private static final String DEFAULT_DIAGRAM_IMAGE_FILE_NAME = "/image/na.svg";
protected Map<Class<? extends BaseElement>, org.jeecg.activiti.util.DefaultProcessDiagramGenerator.ActivityDrawInstruction> activityDrawInstructions = new HashMap<Class<? extends BaseElement>, org.jeecg.activiti.util.DefaultProcessDiagramGenerator.ActivityDrawInstruction>();
protected Map<Class<? extends BaseElement>, org.jeecg.activiti.util.DefaultProcessDiagramGenerator.ArtifactDrawInstruction> artifactDrawInstructions = new HashMap<Class<? extends BaseElement>, org.jeecg.activiti.util.DefaultProcessDiagramGenerator.ArtifactDrawInstruction>();
@Override
public String getDefaultActivityFontName() {
return DEFAULT_ACTIVITY_FONT_NAME;
}
@Override
public String getDefaultLabelFontName() {
return DEFAULT_LABEL_FONT_NAME;
}
@Override
public String getDefaultAnnotationFontName() {
return DEFAULT_ANNOTATION_FONT_NAME;
}
@Override
public String getDefaultDiagramImageFileName() {
return DEFAULT_DIAGRAM_IMAGE_FILE_NAME;
}
// The instructions on how to draw a certain construct is
// created statically and stored in a map for performance.
public DefaultProcessDiagramGenerator() {
// start event
activityDrawInstructions.put(StartEvent.class,
new org.jeecg.activiti.util.DefaultProcessDiagramGenerator.ActivityDrawInstruction() {
@Override
public void draw(org.jeecg.activiti.util.DefaultProcessDiagramCanvas processDiagramCanvas,
BpmnModel bpmnModel,
FlowNode flowNode) {
GraphicInfo graphicInfo = bpmnModel.getGraphicInfo(flowNode.getId());
StartEvent startEvent = (StartEvent) flowNode;
if (startEvent.getEventDefinitions() != null && !startEvent.getEventDefinitions().isEmpty()) {
EventDefinition eventDefinition = startEvent.getEventDefinitions().get(0);
if (eventDefinition instanceof TimerEventDefinition) {
processDiagramCanvas.drawTimerStartEvent(flowNode.getId(),
graphicInfo);
} else if (eventDefinition instanceof ErrorEventDefinition) {
processDiagramCanvas.drawErrorStartEvent(flowNode.getId(),
graphicInfo);
} else if (eventDefinition instanceof SignalEventDefinition) {
processDiagramCanvas.drawSignalStartEvent(flowNode.getId(),
graphicInfo);
} else if (eventDefinition instanceof MessageEventDefinition) {
processDiagramCanvas.drawMessageStartEvent(flowNode.getId(),
graphicInfo);
} else {
processDiagramCanvas.drawNoneStartEvent(flowNode.getId(),
graphicInfo);
}
} else {
processDiagramCanvas.drawNoneStartEvent(flowNode.getId(),
graphicInfo);
}
}
});
// signal catch
activityDrawInstructions.put(IntermediateCatchEvent.class,
new org.jeecg.activiti.util.DefaultProcessDiagramGenerator.ActivityDrawInstruction() {
@Override
public void draw(org.jeecg.activiti.util.DefaultProcessDiagramCanvas processDiagramCanvas,
BpmnModel bpmnModel,
FlowNode flowNode) {
GraphicInfo graphicInfo = bpmnModel.getGraphicInfo(flowNode.getId());
IntermediateCatchEvent intermediateCatchEvent = (IntermediateCatchEvent) flowNode;
if (intermediateCatchEvent.getEventDefinitions() != null && !intermediateCatchEvent.getEventDefinitions()
.isEmpty()) {
if (intermediateCatchEvent.getEventDefinitions().get(0) instanceof SignalEventDefinition) {
processDiagramCanvas.drawCatchingSignalEvent(flowNode.getId(),
flowNode.getName(),
graphicInfo,
true);
} else if (intermediateCatchEvent.getEventDefinitions().get(0) instanceof TimerEventDefinition) {
processDiagramCanvas.drawCatchingTimerEvent(flowNode.getId(),
flowNode.getName(),
graphicInfo,
true);
} else if (intermediateCatchEvent.getEventDefinitions().get(0) instanceof MessageEventDefinition) {
processDiagramCanvas.drawCatchingMessageEvent(flowNode.getId(),
flowNode.getName(),
graphicInfo,
true);
}
}
}
});
// signal throw
activityDrawInstructions.put(ThrowEvent.class,
new org.jeecg.activiti.util.DefaultProcessDiagramGenerator.ActivityDrawInstruction() {
@Override
public void draw(org.jeecg.activiti.util.DefaultProcessDiagramCanvas processDiagramCanvas,
BpmnModel bpmnModel,
FlowNode flowNode) {
GraphicInfo graphicInfo = bpmnModel.getGraphicInfo(flowNode.getId());
ThrowEvent throwEvent = (ThrowEvent) flowNode;
if (throwEvent.getEventDefinitions() != null && !throwEvent.getEventDefinitions().isEmpty()) {
if (throwEvent.getEventDefinitions().get(0) instanceof SignalEventDefinition) {
processDiagramCanvas.drawThrowingSignalEvent(flowNode.getId(),
graphicInfo);
} else if (throwEvent.getEventDefinitions().get(0) instanceof CompensateEventDefinition) {
processDiagramCanvas.drawThrowingCompensateEvent(flowNode.getId(),
graphicInfo);
} else {
processDiagramCanvas.drawThrowingNoneEvent(flowNode.getId(),
graphicInfo);
}
} else {
processDiagramCanvas.drawThrowingNoneEvent(flowNode.getId(),
graphicInfo);
}
}
});
// end event
activityDrawInstructions.put(EndEvent.class,
new org.jeecg.activiti.util.DefaultProcessDiagramGenerator.ActivityDrawInstruction() {
@Override
public void draw(org.jeecg.activiti.util.DefaultProcessDiagramCanvas processDiagramCanvas,
BpmnModel bpmnModel,
FlowNode flowNode) {
GraphicInfo graphicInfo = bpmnModel.getGraphicInfo(flowNode.getId());
EndEvent endEvent = (EndEvent) flowNode;
if (endEvent.getEventDefinitions() != null && !endEvent.getEventDefinitions().isEmpty()) {
if (endEvent.getEventDefinitions().get(0) instanceof ErrorEventDefinition) {
processDiagramCanvas.drawErrorEndEvent(flowNode.getId(),
flowNode.getName(),
graphicInfo);
} else {
processDiagramCanvas.drawNoneEndEvent(flowNode.getId(),
flowNode.getName(),
graphicInfo);
}
} else {
processDiagramCanvas.drawNoneEndEvent(flowNode.getId(),
flowNode.getName(),
graphicInfo);
}
}
});
// task
activityDrawInstructions.put(Task.class,
new org.jeecg.activiti.util.DefaultProcessDiagramGenerator.ActivityDrawInstruction() {
@Override
public void draw(org.jeecg.activiti.util.DefaultProcessDiagramCanvas processDiagramCanvas,
BpmnModel bpmnModel,
FlowNode flowNode) {
GraphicInfo graphicInfo = bpmnModel.getGraphicInfo(flowNode.getId());
processDiagramCanvas.drawTask(flowNode.getId(),
flowNode.getName(),
graphicInfo);
}
});
// user task
activityDrawInstructions.put(UserTask.class,
new org.jeecg.activiti.util.DefaultProcessDiagramGenerator.ActivityDrawInstruction() {
@Override
public void draw(org.jeecg.activiti.util.DefaultProcessDiagramCanvas processDiagramCanvas,
BpmnModel bpmnModel,
FlowNode flowNode) {
GraphicInfo graphicInfo = bpmnModel.getGraphicInfo(flowNode.getId());
processDiagramCanvas.drawUserTask(flowNode.getId(),
flowNode.getName(),
graphicInfo);
}
});
// script task
activityDrawInstructions.put(ScriptTask.class,
new org.jeecg.activiti.util.DefaultProcessDiagramGenerator.ActivityDrawInstruction() {
@Override
public void draw(org.jeecg.activiti.util.DefaultProcessDiagramCanvas processDiagramCanvas,
BpmnModel bpmnModel,
FlowNode flowNode) {
GraphicInfo graphicInfo = bpmnModel.getGraphicInfo(flowNode.getId());
processDiagramCanvas.drawScriptTask(flowNode.getId(),
flowNode.getName(),
graphicInfo);
}
});
// service task
activityDrawInstructions.put(ServiceTask.class,
new org.jeecg.activiti.util.DefaultProcessDiagramGenerator.ActivityDrawInstruction() {
@Override
public void draw(org.jeecg.activiti.util.DefaultProcessDiagramCanvas processDiagramCanvas,
BpmnModel bpmnModel,
FlowNode flowNode) {
GraphicInfo graphicInfo = bpmnModel.getGraphicInfo(flowNode.getId());
ServiceTask serviceTask = (ServiceTask) flowNode;
processDiagramCanvas.drawServiceTask(flowNode.getId(),
serviceTask.getName(),
graphicInfo);
}
});
// receive task
activityDrawInstructions.put(ReceiveTask.class,
new org.jeecg.activiti.util.DefaultProcessDiagramGenerator.ActivityDrawInstruction() {
@Override
public void draw(org.jeecg.activiti.util.DefaultProcessDiagramCanvas processDiagramCanvas,
BpmnModel bpmnModel,
FlowNode flowNode) {
GraphicInfo graphicInfo = bpmnModel.getGraphicInfo(flowNode.getId());
processDiagramCanvas.drawReceiveTask(flowNode.getId(),
flowNode.getName(),
graphicInfo);
}
});
// send task
activityDrawInstructions.put(SendTask.class,
new org.jeecg.activiti.util.DefaultProcessDiagramGenerator.ActivityDrawInstruction() {
@Override
public void draw(org.jeecg.activiti.util.DefaultProcessDiagramCanvas processDiagramCanvas,
BpmnModel bpmnModel,
FlowNode flowNode) {
GraphicInfo graphicInfo = bpmnModel.getGraphicInfo(flowNode.getId());
processDiagramCanvas.drawSendTask(flowNode.getId(),
flowNode.getName(),
graphicInfo);
}
});
// manual task
activityDrawInstructions.put(ManualTask.class,
new org.jeecg.activiti.util.DefaultProcessDiagramGenerator.ActivityDrawInstruction() {
@Override
public void draw(org.jeecg.activiti.util.DefaultProcessDiagramCanvas processDiagramCanvas,
BpmnModel bpmnModel,
FlowNode flowNode) {
GraphicInfo graphicInfo = bpmnModel.getGraphicInfo(flowNode.getId());
processDiagramCanvas.drawManualTask(flowNode.getId(),
flowNode.getName(),
graphicInfo);
}
});
// businessRuleTask task
activityDrawInstructions.put(BusinessRuleTask.class,
new org.jeecg.activiti.util.DefaultProcessDiagramGenerator.ActivityDrawInstruction() {
@Override
public void draw(org.jeecg.activiti.util.DefaultProcessDiagramCanvas processDiagramCanvas,
BpmnModel bpmnModel,
FlowNode flowNode) {
GraphicInfo graphicInfo = bpmnModel.getGraphicInfo(flowNode.getId());
processDiagramCanvas.drawBusinessRuleTask(flowNode.getId(),
flowNode.getName(),
graphicInfo);
}
});
// exclusive gateway
activityDrawInstructions.put(ExclusiveGateway.class,
new org.jeecg.activiti.util.DefaultProcessDiagramGenerator.ActivityDrawInstruction() {
@Override
public void draw(org.jeecg.activiti.util.DefaultProcessDiagramCanvas processDiagramCanvas,
BpmnModel bpmnModel,
FlowNode flowNode) {
GraphicInfo graphicInfo = bpmnModel.getGraphicInfo(flowNode.getId());
processDiagramCanvas.drawExclusiveGateway(flowNode.getId(),
graphicInfo);
}
});
// inclusive gateway
activityDrawInstructions.put(InclusiveGateway.class,
new org.jeecg.activiti.util.DefaultProcessDiagramGenerator.ActivityDrawInstruction() {
@Override
public void draw(org.jeecg.activiti.util.DefaultProcessDiagramCanvas processDiagramCanvas,
BpmnModel bpmnModel,
FlowNode flowNode) {
GraphicInfo graphicInfo = bpmnModel.getGraphicInfo(flowNode.getId());
processDiagramCanvas.drawInclusiveGateway(flowNode.getId(),
graphicInfo);
}
});
// parallel gateway
activityDrawInstructions.put(ParallelGateway.class,
new org.jeecg.activiti.util.DefaultProcessDiagramGenerator.ActivityDrawInstruction() {
@Override
public void draw(org.jeecg.activiti.util.DefaultProcessDiagramCanvas processDiagramCanvas,
BpmnModel bpmnModel,
FlowNode flowNode) {
GraphicInfo graphicInfo = bpmnModel.getGraphicInfo(flowNode.getId());
processDiagramCanvas.drawParallelGateway(flowNode.getId(),
graphicInfo);
}
});
// event based gateway
activityDrawInstructions.put(EventGateway.class,
new org.jeecg.activiti.util.DefaultProcessDiagramGenerator.ActivityDrawInstruction() {
@Override
public void draw(org.jeecg.activiti.util.DefaultProcessDiagramCanvas processDiagramCanvas,
BpmnModel bpmnModel,
FlowNode flowNode) {
GraphicInfo graphicInfo = bpmnModel.getGraphicInfo(flowNode.getId());
processDiagramCanvas.drawEventBasedGateway(flowNode.getId(),
graphicInfo);
}
});
// Boundary timer
activityDrawInstructions.put(BoundaryEvent.class,
new org.jeecg.activiti.util.DefaultProcessDiagramGenerator.ActivityDrawInstruction() {
@Override
public void draw(org.jeecg.activiti.util.DefaultProcessDiagramCanvas processDiagramCanvas,
BpmnModel bpmnModel,
FlowNode flowNode) {
GraphicInfo graphicInfo = bpmnModel.getGraphicInfo(flowNode.getId());
BoundaryEvent boundaryEvent = (BoundaryEvent) flowNode;
if (boundaryEvent.getEventDefinitions() != null && !boundaryEvent.getEventDefinitions().isEmpty()) {
if (boundaryEvent.getEventDefinitions().get(0) instanceof TimerEventDefinition) {
processDiagramCanvas.drawCatchingTimerEvent(flowNode.getId(),
flowNode.getName(),
graphicInfo,
boundaryEvent.isCancelActivity());
} else if (boundaryEvent.getEventDefinitions().get(0) instanceof ErrorEventDefinition) {
processDiagramCanvas.drawCatchingErrorEvent(flowNode.getId(),
graphicInfo,
boundaryEvent.isCancelActivity());
} else if (boundaryEvent.getEventDefinitions().get(0) instanceof SignalEventDefinition) {
processDiagramCanvas.drawCatchingSignalEvent(flowNode.getId(),
flowNode.getName(),
graphicInfo,
boundaryEvent.isCancelActivity());
} else if (boundaryEvent.getEventDefinitions().get(0) instanceof MessageEventDefinition) {
processDiagramCanvas.drawCatchingMessageEvent(flowNode.getId(),
flowNode.getName(),
graphicInfo,
boundaryEvent.isCancelActivity());
} else if (boundaryEvent.getEventDefinitions().get(0) instanceof CompensateEventDefinition) {
processDiagramCanvas.drawCatchingCompensateEvent(flowNode.getId(),
graphicInfo,
boundaryEvent.isCancelActivity());
}
}
}
});
// subprocess
activityDrawInstructions.put(SubProcess.class,
new org.jeecg.activiti.util.DefaultProcessDiagramGenerator.ActivityDrawInstruction() {
@Override
public void draw(org.jeecg.activiti.util.DefaultProcessDiagramCanvas processDiagramCanvas,
BpmnModel bpmnModel,
FlowNode flowNode) {
GraphicInfo graphicInfo = bpmnModel.getGraphicInfo(flowNode.getId());
if (graphicInfo.getExpanded() != null && !graphicInfo.getExpanded()) {
processDiagramCanvas.drawCollapsedSubProcess(flowNode.getId(),
flowNode.getName(),
graphicInfo,
false);
} else {
processDiagramCanvas.drawExpandedSubProcess(flowNode.getId(),
flowNode.getName(),
graphicInfo,
SubProcess.class);
}
}
});
// transaction
activityDrawInstructions.put(Transaction.class,
new org.jeecg.activiti.util.DefaultProcessDiagramGenerator.ActivityDrawInstruction() {
@Override
public void draw(org.jeecg.activiti.util.DefaultProcessDiagramCanvas processDiagramCanvas,
BpmnModel bpmnModel,
FlowNode flowNode) {
GraphicInfo graphicInfo = bpmnModel.getGraphicInfo(flowNode.getId());
if (graphicInfo.getExpanded() != null && !graphicInfo.getExpanded()) {
processDiagramCanvas.drawCollapsedSubProcess(flowNode.getId(),
flowNode.getName(),
graphicInfo,
false);
} else {
processDiagramCanvas.drawExpandedSubProcess(flowNode.getId(),
flowNode.getName(),
graphicInfo,
Transaction.class);
}
}
});
// Event subprocess
activityDrawInstructions.put(EventSubProcess.class,
new org.jeecg.activiti.util.DefaultProcessDiagramGenerator.ActivityDrawInstruction() {
@Override
public void draw(org.jeecg.activiti.util.DefaultProcessDiagramCanvas processDiagramCanvas,
BpmnModel bpmnModel,
FlowNode flowNode) {
GraphicInfo graphicInfo = bpmnModel.getGraphicInfo(flowNode.getId());
if (graphicInfo.getExpanded() != null && !graphicInfo.getExpanded()) {
processDiagramCanvas.drawCollapsedSubProcess(flowNode.getId(),
flowNode.getName(),
graphicInfo,
true);
} else {
processDiagramCanvas.drawExpandedSubProcess(flowNode.getId(),
flowNode.getName(),
graphicInfo,
EventSubProcess.class);
}
}
});
// call activity
activityDrawInstructions.put(CallActivity.class,
new org.jeecg.activiti.util.DefaultProcessDiagramGenerator.ActivityDrawInstruction() {
@Override
public void draw(org.jeecg.activiti.util.DefaultProcessDiagramCanvas processDiagramCanvas,
BpmnModel bpmnModel,
FlowNode flowNode) {
GraphicInfo graphicInfo = bpmnModel.getGraphicInfo(flowNode.getId());
processDiagramCanvas.drawCollapsedCallActivity(flowNode.getId(),
flowNode.getName(),
graphicInfo);
}
});
// text annotation
artifactDrawInstructions.put(TextAnnotation.class,
new org.jeecg.activiti.util.DefaultProcessDiagramGenerator.ArtifactDrawInstruction() {
@Override
public void draw(org.jeecg.activiti.util.DefaultProcessDiagramCanvas processDiagramCanvas,
BpmnModel bpmnModel,
Artifact artifact) {
GraphicInfo graphicInfo = bpmnModel.getGraphicInfo(artifact.getId());
TextAnnotation textAnnotation = (TextAnnotation) artifact;
processDiagramCanvas.drawTextAnnotation(textAnnotation.getId(),
textAnnotation.getText(),
graphicInfo);
}
});
// association
artifactDrawInstructions.put(Association.class,
new org.jeecg.activiti.util.DefaultProcessDiagramGenerator.ArtifactDrawInstruction() {
@Override
public void draw(org.jeecg.activiti.util.DefaultProcessDiagramCanvas processDiagramCanvas,
BpmnModel bpmnModel,
Artifact artifact) {
Association association = (Association) artifact;
String sourceRef = association.getSourceRef();
String targetRef = association.getTargetRef();
// source and target can be instance of FlowElement or Artifact
BaseElement sourceElement = bpmnModel.getFlowElement(sourceRef);
BaseElement targetElement = bpmnModel.getFlowElement(targetRef);
if (sourceElement == null) {
sourceElement = bpmnModel.getArtifact(sourceRef);
}
if (targetElement == null) {
targetElement = bpmnModel.getArtifact(targetRef);
}
List<GraphicInfo> graphicInfoList = bpmnModel.getFlowLocationGraphicInfo(artifact.getId());
graphicInfoList = connectionPerfectionizer(processDiagramCanvas,
bpmnModel,
sourceElement,
targetElement,
graphicInfoList);
int xPoints[] = new int[graphicInfoList.size()];
int yPoints[] = new int[graphicInfoList.size()];
for (int i = 1; i < graphicInfoList.size(); i++) {
GraphicInfo graphicInfo = graphicInfoList.get(i);
GraphicInfo previousGraphicInfo = graphicInfoList.get(i - 1);
if (i == 1) {
xPoints[0] = (int) previousGraphicInfo.getX();
yPoints[0] = (int) previousGraphicInfo.getY();
}
xPoints[i] = (int) graphicInfo.getX();
yPoints[i] = (int) graphicInfo.getY();
}
AssociationDirection associationDirection = association.getAssociationDirection();
processDiagramCanvas.drawAssociation(xPoints,
yPoints,
associationDirection,
false);
}
});
}
@Override
public InputStream generateDiagram(BpmnModel bpmnModel,
List<String> highLightedActivities,
List<String> highLightedFlows,
String activityFontName,
String labelFontName,
String annotationFontName) {
return generateDiagram(bpmnModel,
highLightedActivities,
highLightedFlows,
activityFontName,
labelFontName,
annotationFontName,
false,
null);
}
@Override
public InputStream generateDiagram(BpmnModel bpmnModel,
List<String> highLightedActivities,
List<String> highLightedFlows,
String activityFontName,
String labelFontName,
String annotationFontName,
boolean generateDefaultDiagram) {
return generateDiagram(bpmnModel,
highLightedActivities,
highLightedFlows,
activityFontName,
labelFontName,
annotationFontName,
generateDefaultDiagram,
null);
}
@Override
public InputStream generateDiagram(BpmnModel bpmnModel,
List<String> highLightedActivities,
List<String> highLightedFlows,
String activityFontName,
String labelFontName,
String annotationFontName,
boolean generateDefaultDiagram,
String defaultDiagramImageFileName) {
if (!bpmnModel.hasDiagramInterchangeInfo()) {
if (!generateDefaultDiagram) {
throw new ActivitiInterchangeInfoNotFoundException("No interchange information found.");
}
return getDefaultDiagram(defaultDiagramImageFileName);
}
return generateProcessDiagram(bpmnModel,
highLightedActivities,
highLightedFlows,
activityFontName,
labelFontName,
annotationFontName).generateImage();
}
/**
* Get default diagram image as bytes array
* @return the default diagram image
*/
protected InputStream getDefaultDiagram(String diagramImageFileName) {
String imageFileName = diagramImageFileName != null ?
diagramImageFileName :
getDefaultDiagramImageFileName();
InputStream imageStream = getClass().getResourceAsStream(imageFileName);
if (imageStream == null) {
throw new ActivitiImageException("Error occurred while getting default diagram image from file: " + imageFileName);
}
return imageStream;
}
@Override
public InputStream generateDiagram(BpmnModel bpmnModel,
List<String> highLightedActivities,
List<String> highLightedFlows) {
return generateDiagram(bpmnModel,
highLightedActivities,
highLightedFlows,
null,
null,
null,
false,
null);
}
@Override
public InputStream generateDiagram(BpmnModel bpmnModel,
List<String> highLightedActivities) {
return generateDiagram(bpmnModel,
highLightedActivities,
Collections.<String>emptyList());
}
@Override
public InputStream generateDiagram(BpmnModel bpmnModel,
String activityFontName,
String labelFontName,
String annotationFontName) {
return generateDiagram(bpmnModel,
Collections.<String>emptyList(),
Collections.<String>emptyList(),
activityFontName,
labelFontName,
annotationFontName);
}
protected org.jeecg.activiti.util.DefaultProcessDiagramCanvas generateProcessDiagram(BpmnModel bpmnModel,
List<String> highLightedActivities,
List<String> highLightedFlows,
String activityFontName,
String labelFontName,
String annotationFontName) {
prepareBpmnModel(bpmnModel);
org.jeecg.activiti.util.DefaultProcessDiagramCanvas processDiagramCanvas = initProcessDiagramCanvas(bpmnModel,
activityFontName,
labelFontName,
annotationFontName);
// Draw pool shape, if process is participant in collaboration
for (Pool pool : bpmnModel.getPools()) {
GraphicInfo graphicInfo = bpmnModel.getGraphicInfo(pool.getId());
processDiagramCanvas.drawPoolOrLane(pool.getId(),
pool.getName(),
graphicInfo);
}
// Draw lanes
for (Process process : bpmnModel.getProcesses()) {
for (Lane lane : process.getLanes()) {
GraphicInfo graphicInfo = bpmnModel.getGraphicInfo(lane.getId());
processDiagramCanvas.drawPoolOrLane(lane.getId(),
lane.getName(),
graphicInfo);
}
}
// Draw activities and their sequence-flows
for (Process process : bpmnModel.getProcesses()) {
for (FlowNode flowNode : process.findFlowElementsOfType(FlowNode.class)) {
drawActivity(processDiagramCanvas,
bpmnModel,
flowNode,
highLightedActivities,
highLightedFlows);
}
}
// Draw artifacts
for (Process process : bpmnModel.getProcesses()) {
for (Artifact artifact : process.getArtifacts()) {
drawArtifact(processDiagramCanvas,
bpmnModel,
artifact);
}
List<SubProcess> subProcesses = process.findFlowElementsOfType(SubProcess.class,
true);
if (subProcesses != null) {
for (SubProcess subProcess : subProcesses) {
for (Artifact subProcessArtifact : subProcess.getArtifacts()) {
drawArtifact(processDiagramCanvas,
bpmnModel,
subProcessArtifact);
}
}
}
}
return processDiagramCanvas;
}
protected void prepareBpmnModel(BpmnModel bpmnModel) {
// Need to make sure all elements have positive x and y.
// Check all graphicInfo and update the elements accordingly
List<GraphicInfo> allGraphicInfos = new ArrayList<GraphicInfo>();
if (bpmnModel.getLocationMap() != null) {
allGraphicInfos.addAll(bpmnModel.getLocationMap().values());
}
if (bpmnModel.getLabelLocationMap() != null) {
allGraphicInfos.addAll(bpmnModel.getLabelLocationMap().values());
}
if (bpmnModel.getFlowLocationMap() != null) {
for (List<GraphicInfo> flowGraphicInfos : bpmnModel.getFlowLocationMap().values()) {
allGraphicInfos.addAll(flowGraphicInfos);
}
}
if (allGraphicInfos.size() > 0) {
boolean needsTranslationX = false;
boolean needsTranslationY = false;
double lowestX = 0.0;
double lowestY = 0.0;
// Collect lowest x and y
for (GraphicInfo graphicInfo : allGraphicInfos) {
double x = graphicInfo.getX();
double y = graphicInfo.getY();
if (x < lowestX) {
needsTranslationX = true;
lowestX = x;
}
if (y < lowestY) {
needsTranslationY = true;
lowestY = y;
}
}
// Update all graphicInfo objects
if (needsTranslationX || needsTranslationY) {
double translationX = Math.abs(lowestX);
double translationY = Math.abs(lowestY);
for (GraphicInfo graphicInfo : allGraphicInfos) {
if (needsTranslationX) {
graphicInfo.setX(graphicInfo.getX() + translationX);
}
if (needsTranslationY) {
graphicInfo.setY(graphicInfo.getY() + translationY);
}
}
}
}
}
protected void drawActivity(org.jeecg.activiti.util.DefaultProcessDiagramCanvas processDiagramCanvas,
BpmnModel bpmnModel,
FlowNode flowNode,
List<String> highLightedActivities,
List<String> highLightedFlows) {
org.jeecg.activiti.util.DefaultProcessDiagramGenerator.ActivityDrawInstruction drawInstruction = activityDrawInstructions.get(flowNode.getClass());
if (drawInstruction != null) {
drawInstruction.draw(processDiagramCanvas,
bpmnModel,
flowNode);
// Gather info on the multi instance marker
boolean multiInstanceSequential = false;
boolean multiInstanceParallel = false;
boolean collapsed = false;
if (flowNode instanceof Activity) {
Activity activity = (Activity) flowNode;
MultiInstanceLoopCharacteristics multiInstanceLoopCharacteristics = activity.getLoopCharacteristics();
if (multiInstanceLoopCharacteristics != null) {
multiInstanceSequential = multiInstanceLoopCharacteristics.isSequential();
multiInstanceParallel = !multiInstanceSequential;
}
}
// Gather info on the collapsed marker
GraphicInfo graphicInfo = bpmnModel.getGraphicInfo(flowNode.getId());
if (flowNode instanceof SubProcess) {
collapsed = graphicInfo.getExpanded() != null && !graphicInfo.getExpanded();
} else if (flowNode instanceof CallActivity) {
collapsed = true;
}
// Actually draw the markers
processDiagramCanvas.drawActivityMarkers((int) graphicInfo.getX(),
(int) graphicInfo.getY(),
(int) graphicInfo.getWidth(),
(int) graphicInfo.getHeight(),
multiInstanceSequential,
multiInstanceParallel,
collapsed);
// Draw highlighted activities
if (highLightedActivities.contains(flowNode.getId())) {
drawHighLight(processDiagramCanvas,
bpmnModel.getGraphicInfo(flowNode.getId()));
}
}
// Outgoing transitions of activity
for (SequenceFlow sequenceFlow : flowNode.getOutgoingFlows()) {
boolean highLighted = (highLightedFlows.contains(sequenceFlow.getId()));
String defaultFlow = null;
if (flowNode instanceof Activity) {
defaultFlow = ((Activity) flowNode).getDefaultFlow();
} else if (flowNode instanceof Gateway) {
defaultFlow = ((Gateway) flowNode).getDefaultFlow();
}
boolean isDefault = false;
if (defaultFlow != null && defaultFlow.equalsIgnoreCase(sequenceFlow.getId())) {
isDefault = true;
}
boolean drawConditionalIndicator = sequenceFlow.getConditionExpression() != null && !(flowNode instanceof Gateway);
String sourceRef = sequenceFlow.getSourceRef();
String targetRef = sequenceFlow.getTargetRef();
FlowElement sourceElement = bpmnModel.getFlowElement(sourceRef);
FlowElement targetElement = bpmnModel.getFlowElement(targetRef);
List<GraphicInfo> graphicInfoList = bpmnModel.getFlowLocationGraphicInfo(sequenceFlow.getId());
if (graphicInfoList != null && graphicInfoList.size() > 0) {
graphicInfoList = connectionPerfectionizer(processDiagramCanvas,
bpmnModel,
sourceElement,
targetElement,
graphicInfoList);
int xPoints[] = new int[graphicInfoList.size()];
int yPoints[] = new int[graphicInfoList.size()];
for (int i = 1; i < graphicInfoList.size(); i++) {
GraphicInfo graphicInfo = graphicInfoList.get(i);
GraphicInfo previousGraphicInfo = graphicInfoList.get(i - 1);
if (i == 1) {
xPoints[0] = (int) previousGraphicInfo.getX();
yPoints[0] = (int) previousGraphicInfo.getY();
}
xPoints[i] = (int) graphicInfo.getX();
yPoints[i] = (int) graphicInfo.getY();
}
processDiagramCanvas.drawSequenceflow(xPoints,
yPoints,
drawConditionalIndicator,
isDefault,
highLighted);
// Draw sequenceflow label
GraphicInfo labelGraphicInfo = bpmnModel.getLabelGraphicInfo(sequenceFlow.getId());
if (labelGraphicInfo != null) {
processDiagramCanvas.drawLabel(sequenceFlow.getName(),
labelGraphicInfo,
false);
}
}
}
// Nested elements
if (flowNode instanceof FlowElementsContainer) {
for (FlowElement nestedFlowElement : ((FlowElementsContainer) flowNode).getFlowElements()) {
if (nestedFlowElement instanceof FlowNode) {
drawActivity(processDiagramCanvas,
bpmnModel,
(FlowNode) nestedFlowElement,
highLightedActivities,
highLightedFlows);
}
}
}
}
/**
* This method makes coordinates of connection flow better.
* @param processDiagramCanvas
* @param bpmnModel
* @param sourceElement
* @param targetElement
* @param graphicInfoList
* @return
*/
protected static List<GraphicInfo> connectionPerfectionizer(org.jeecg.activiti.util.DefaultProcessDiagramCanvas processDiagramCanvas,
BpmnModel bpmnModel,
BaseElement sourceElement,
BaseElement targetElement,
List<GraphicInfo> graphicInfoList) {
GraphicInfo sourceGraphicInfo = bpmnModel.getGraphicInfo(sourceElement.getId());
GraphicInfo targetGraphicInfo = bpmnModel.getGraphicInfo(targetElement.getId());
org.jeecg.activiti.util.DefaultProcessDiagramCanvas.SHAPE_TYPE sourceShapeType = getShapeType(sourceElement);
org.jeecg.activiti.util.DefaultProcessDiagramCanvas.SHAPE_TYPE targetShapeType = getShapeType(targetElement);
return processDiagramCanvas.connectionPerfectionizer(sourceShapeType,
targetShapeType,
sourceGraphicInfo,
targetGraphicInfo,
graphicInfoList);
}
/**
* This method returns shape type of base element.<br>
* Each element can be presented as rectangle, rhombus, or ellipse.
* @param baseElement
* @return DefaultProcessDiagramCanvas.SHAPE_TYPE
*/
protected static org.jeecg.activiti.util.DefaultProcessDiagramCanvas.SHAPE_TYPE getShapeType(BaseElement baseElement) {
if (baseElement instanceof Task || baseElement instanceof Activity || baseElement instanceof TextAnnotation) {
return org.jeecg.activiti.util.DefaultProcessDiagramCanvas.SHAPE_TYPE.Rectangle;
} else if (baseElement instanceof Gateway) {
return org.jeecg.activiti.util.DefaultProcessDiagramCanvas.SHAPE_TYPE.Rhombus;
} else if (baseElement instanceof Event) {
return org.jeecg.activiti.util.DefaultProcessDiagramCanvas.SHAPE_TYPE.Ellipse;
}
// unknown source element, just do not correct coordinates
return null;
}
protected static GraphicInfo getLineCenter(List<GraphicInfo> graphicInfoList) {
GraphicInfo gi = new GraphicInfo();
int xPoints[] = new int[graphicInfoList.size()];
int yPoints[] = new int[graphicInfoList.size()];
double length = 0;
double[] lengths = new double[graphicInfoList.size()];
lengths[0] = 0;
double m;
for (int i = 1; i < graphicInfoList.size(); i++) {
GraphicInfo graphicInfo = graphicInfoList.get(i);
GraphicInfo previousGraphicInfo = graphicInfoList.get(i - 1);
if (i == 1) {
xPoints[0] = (int) previousGraphicInfo.getX();
yPoints[0] = (int) previousGraphicInfo.getY();
}
xPoints[i] = (int) graphicInfo.getX();
yPoints[i] = (int) graphicInfo.getY();
length += Math.sqrt(
Math.pow((int) graphicInfo.getX() - (int) previousGraphicInfo.getX(),
2) +
Math.pow((int) graphicInfo.getY() - (int) previousGraphicInfo.getY(),
2)
);
lengths[i] = length;
}
m = length / 2;
int p1 = 0;
int p2 = 1;
for (int i = 1; i < lengths.length; i++) {
double len = lengths[i];
p1 = i - 1;
p2 = i;
if (len > m) {
break;
}
}
GraphicInfo graphicInfo1 = graphicInfoList.get(p1);
GraphicInfo graphicInfo2 = graphicInfoList.get(p2);
double AB = (int) graphicInfo2.getX() - (int) graphicInfo1.getX();
double OA = (int) graphicInfo2.getY() - (int) graphicInfo1.getY();
double OB = lengths[p2] - lengths[p1];
double ob = m - lengths[p1];
double ab = AB * ob / OB;
double oa = OA * ob / OB;
double mx = graphicInfo1.getX() + ab;
double my = graphicInfo1.getY() + oa;
gi.setX(mx);
gi.setY(my);
return gi;
}
protected void drawArtifact(org.jeecg.activiti.util.DefaultProcessDiagramCanvas processDiagramCanvas,
BpmnModel bpmnModel,
Artifact artifact) {
org.jeecg.activiti.util.DefaultProcessDiagramGenerator.ArtifactDrawInstruction drawInstruction = artifactDrawInstructions.get(artifact.getClass());
if (drawInstruction != null) {
drawInstruction.draw(processDiagramCanvas,
bpmnModel,
artifact);
}
}
private static void drawHighLight(org.jeecg.activiti.util.DefaultProcessDiagramCanvas processDiagramCanvas,
GraphicInfo graphicInfo) {
processDiagramCanvas.drawHighLight((int) graphicInfo.getX(),
(int) graphicInfo.getY(),
(int) graphicInfo.getWidth(),
(int) graphicInfo.getHeight());
}
private static void drawHighLight(DefaultProcessDiagramCanvas processDiagramCanvas, GraphicInfo graphicInfo, Color color, FlowNode flowNode) {
if(flowNode instanceof Event){
processDiagramCanvas.drawHighLightEvent((int)graphicInfo.getX(), (int)graphicInfo.getY(), (int)graphicInfo.getWidth(), (int)graphicInfo.getHeight(),color);
}else if(flowNode instanceof Gateway){
processDiagramCanvas.drawHighLightGateway((int)graphicInfo.getX(), (int)graphicInfo.getY(), (int)graphicInfo.getWidth(), (int)graphicInfo.getHeight(),color);
}else if(flowNode instanceof Task){
processDiagramCanvas.drawHighLightTask((int)graphicInfo.getX(), (int)graphicInfo.getY(), (int)graphicInfo.getWidth(), (int)graphicInfo.getHeight(),color);
}else if(flowNode instanceof SubProcess){
processDiagramCanvas.drawHighLightSubProcess((int)graphicInfo.getX(), (int)graphicInfo.getY(), (int)graphicInfo.getWidth(), (int)graphicInfo.getHeight(),color);
}else{
processDiagramCanvas.drawHighLight((int)graphicInfo.getX(), (int)graphicInfo.getY(), (int)graphicInfo.getWidth(), (int)graphicInfo.getHeight(),color);
}
}
protected static org.jeecg.activiti.util.DefaultProcessDiagramCanvas initProcessDiagramCanvas(BpmnModel bpmnModel,
String activityFontName,
String labelFontName,
String annotationFontName) {
// We need to calculate maximum values to know how big the image will be in its entirety
double minX = Double.MAX_VALUE;
double maxX = 0;
double minY = Double.MAX_VALUE;
double maxY = 0;
for (Pool pool : bpmnModel.getPools()) {
GraphicInfo graphicInfo = bpmnModel.getGraphicInfo(pool.getId());
minX = graphicInfo.getX();
maxX = graphicInfo.getX() + graphicInfo.getWidth();
minY = graphicInfo.getY();
maxY = graphicInfo.getY() + graphicInfo.getHeight();
}
List<FlowNode> flowNodes = gatherAllFlowNodes(bpmnModel);
for (FlowNode flowNode : flowNodes) {
GraphicInfo flowNodeGraphicInfo = bpmnModel.getGraphicInfo(flowNode.getId());
if (flowNodeGraphicInfo == null) {
continue;
}
// width
if (flowNodeGraphicInfo.getX() + flowNodeGraphicInfo.getWidth() > maxX) {
maxX = flowNodeGraphicInfo.getX() + flowNodeGraphicInfo.getWidth();
}
if (flowNodeGraphicInfo.getX() < minX) {
minX = flowNodeGraphicInfo.getX();
}
// height
if (flowNodeGraphicInfo.getY() + flowNodeGraphicInfo.getHeight() > maxY) {
maxY = flowNodeGraphicInfo.getY() + flowNodeGraphicInfo.getHeight();
}
if (flowNodeGraphicInfo.getY() < minY) {
minY = flowNodeGraphicInfo.getY();
}
for (SequenceFlow sequenceFlow : flowNode.getOutgoingFlows()) {
List<GraphicInfo> graphicInfoList = bpmnModel.getFlowLocationGraphicInfo(sequenceFlow.getId());
if (graphicInfoList != null) {
for (GraphicInfo graphicInfo : graphicInfoList) {
// width
if (graphicInfo.getX() > maxX) {
maxX = graphicInfo.getX();
}
if (graphicInfo.getX() < minX) {
minX = graphicInfo.getX();
}
// height
if (graphicInfo.getY() > maxY) {
maxY = graphicInfo.getY();
}
if (graphicInfo.getY() < minY) {
minY = graphicInfo.getY();
}
}
}
}
}
List<Artifact> artifacts = gatherAllArtifacts(bpmnModel);
for (Artifact artifact : artifacts) {
GraphicInfo artifactGraphicInfo = bpmnModel.getGraphicInfo(artifact.getId());
if (artifactGraphicInfo != null) {
// width
if (artifactGraphicInfo.getX() + artifactGraphicInfo.getWidth() > maxX) {
maxX = artifactGraphicInfo.getX() + artifactGraphicInfo.getWidth();
}
if (artifactGraphicInfo.getX() < minX) {
minX = artifactGraphicInfo.getX();
}
// height
if (artifactGraphicInfo.getY() + artifactGraphicInfo.getHeight() > maxY) {
maxY = artifactGraphicInfo.getY() + artifactGraphicInfo.getHeight();
}
if (artifactGraphicInfo.getY() < minY) {
minY = artifactGraphicInfo.getY();
}
}
List<GraphicInfo> graphicInfoList = bpmnModel.getFlowLocationGraphicInfo(artifact.getId());
if (graphicInfoList != null) {
for (GraphicInfo graphicInfo : graphicInfoList) {
// width
if (graphicInfo.getX() > maxX) {
maxX = graphicInfo.getX();
}
if (graphicInfo.getX() < minX) {
minX = graphicInfo.getX();
}
// height
if (graphicInfo.getY() > maxY) {
maxY = graphicInfo.getY();
}
if (graphicInfo.getY() < minY) {
minY = graphicInfo.getY();
}
}
}
}
int nrOfLanes = 0;
for (Process process : bpmnModel.getProcesses()) {
for (Lane l : process.getLanes()) {
nrOfLanes++;
GraphicInfo graphicInfo = bpmnModel.getGraphicInfo(l.getId());
if (graphicInfo != null) {
// width
if (graphicInfo.getX() + graphicInfo.getWidth() > maxX) {
maxX = graphicInfo.getX() + graphicInfo.getWidth();
}
if (graphicInfo.getX() < minX) {
minX = graphicInfo.getX();
}
// height
if (graphicInfo.getY() + graphicInfo.getHeight() > maxY) {
maxY = graphicInfo.getY() + graphicInfo.getHeight();
}
if (graphicInfo.getY() < minY) {
minY = graphicInfo.getY();
}
}
}
}
// Special case, see https://activiti.atlassian.net/browse/ACT-1431
if (flowNodes.isEmpty() && bpmnModel.getPools().isEmpty() && nrOfLanes == 0) {
// Nothing to show
minX = 0;
minY = 0;
}
return new org.jeecg.activiti.util.DefaultProcessDiagramCanvas((int) maxX + 10,
(int) maxY + 10,
(int) minX,
(int) minY,
activityFontName,
labelFontName,
annotationFontName);
}
protected static List<Artifact> gatherAllArtifacts(BpmnModel bpmnModel) {
List<Artifact> artifacts = new ArrayList<Artifact>();
for (Process process : bpmnModel.getProcesses()) {
artifacts.addAll(process.getArtifacts());
}
return artifacts;
}
protected static List<FlowNode> gatherAllFlowNodes(BpmnModel bpmnModel) {
List<FlowNode> flowNodes = new ArrayList<FlowNode>();
for (Process process : bpmnModel.getProcesses()) {
flowNodes.addAll(gatherAllFlowNodes(process));
}
return flowNodes;
}
protected static List<FlowNode> gatherAllFlowNodes(FlowElementsContainer flowElementsContainer) {
List<FlowNode> flowNodes = new ArrayList<FlowNode>();
for (FlowElement flowElement : flowElementsContainer.getFlowElements()) {
if (flowElement instanceof FlowNode) {
flowNodes.add((FlowNode) flowElement);
}
if (flowElement instanceof FlowElementsContainer) {
flowNodes.addAll(gatherAllFlowNodes((FlowElementsContainer) flowElement));
}
}
return flowNodes;
}
public Map<Class<? extends BaseElement>, org.jeecg.activiti.util.DefaultProcessDiagramGenerator.ActivityDrawInstruction> getActivityDrawInstructions() {
return activityDrawInstructions;
}
public void setActivityDrawInstructions(
Map<Class<? extends BaseElement>, org.jeecg.activiti.util.DefaultProcessDiagramGenerator.ActivityDrawInstruction> activityDrawInstructions) {
this.activityDrawInstructions = activityDrawInstructions;
}
public Map<Class<? extends BaseElement>, org.jeecg.activiti.util.DefaultProcessDiagramGenerator.ArtifactDrawInstruction> getArtifactDrawInstructions() {
return artifactDrawInstructions;
}
public void setArtifactDrawInstructions(
Map<Class<? extends BaseElement>, org.jeecg.activiti.util.DefaultProcessDiagramGenerator.ArtifactDrawInstruction> artifactDrawInstructions) {
this.artifactDrawInstructions = artifactDrawInstructions;
}
protected interface ActivityDrawInstruction {
void draw(org.jeecg.activiti.util.DefaultProcessDiagramCanvas processDiagramCanvas,
BpmnModel bpmnModel,
FlowNode flowNode);
}
protected interface ArtifactDrawInstruction {
void draw(DefaultProcessDiagramCanvas processDiagramCanvas,
BpmnModel bpmnModel,
Artifact artifact);
}
public InputStream generateDiagram(BpmnModel bpmnModel, String imageType,List<String> highLightedFinishes, List<String> highLightedActivities, List<String> highLightedFlows, String activityFontName, String labelFontName, String annotationFontName, ClassLoader customClassLoader, double scaleFactor) {
return this.generateProcessDiagram(bpmnModel, imageType,highLightedFinishes, highLightedActivities, highLightedFlows, activityFontName, labelFontName, annotationFontName, customClassLoader, scaleFactor).generateImage();
}
protected DefaultProcessDiagramCanvas generateProcessDiagram(BpmnModel bpmnModel, String imageType,List<String> highLightedFinishes, List<String> highLightedActivities, List<String> highLightedFlows, String activityFontName, String labelFontName, String annotationFontName, ClassLoader customClassLoader, double scaleFactor) {
this.prepareBpmnModel(bpmnModel);
DefaultProcessDiagramCanvas processDiagramCanvas = initProcessDiagramCanvas(bpmnModel, imageType, activityFontName, labelFontName, annotationFontName, customClassLoader);
Iterator var12 = bpmnModel.getPools().iterator();
while(var12.hasNext()) {
Pool pool = (Pool)var12.next();
GraphicInfo graphicInfo = bpmnModel.getGraphicInfo(pool.getId());
processDiagramCanvas.drawPoolOrLane(pool.getId(), pool.getName(), graphicInfo);
}
var12 = bpmnModel.getProcesses().iterator();
Process process;
Iterator var20;
while(var12.hasNext()) {
process = (Process)var12.next();
var20 = process.getLanes().iterator();
while(var20.hasNext()) {
Lane lane = (Lane)var20.next();
GraphicInfo graphicInfo = bpmnModel.getGraphicInfo(lane.getId());
processDiagramCanvas.drawPoolOrLane(lane.getId(), lane.getName(), graphicInfo);
}
}
var12 = bpmnModel.getProcesses().iterator();
while(var12.hasNext()) {
process = (Process)var12.next();
var20 = process.findFlowElementsOfType(FlowNode.class).iterator();
while(var20.hasNext()) {
FlowNode flowNode = (FlowNode)var20.next();
this.drawActivity(processDiagramCanvas, bpmnModel, flowNode, highLightedFinishes,highLightedActivities, highLightedFlows, scaleFactor);
}
}
var12 = bpmnModel.getProcesses().iterator();
while(true) {
List subProcesses;
do {
if(!var12.hasNext()) {
return processDiagramCanvas;
}
process = (Process)var12.next();
var20 = process.getArtifacts().iterator();
while(var20.hasNext()) {
Artifact artifact = (Artifact)var20.next();
this.drawArtifact(processDiagramCanvas, bpmnModel, artifact);
}
subProcesses = process.findFlowElementsOfType(SubProcess.class, true);
} while(subProcesses == null);
Iterator var24 = subProcesses.iterator();
while(var24.hasNext()) {
SubProcess subProcess = (SubProcess)var24.next();
Iterator var17 = subProcess.getArtifacts().iterator();
while(var17.hasNext()) {
Artifact subProcessArtifact = (Artifact)var17.next();
this.drawArtifact(processDiagramCanvas, bpmnModel, subProcessArtifact);
}
}
}
}
protected static DefaultProcessDiagramCanvas initProcessDiagramCanvas(BpmnModel bpmnModel, String imageType, String activityFontName, String labelFontName, String annotationFontName, ClassLoader customClassLoader) {
double minX = 1.7976931348623157E308D;
double maxX = 0.0D;
double minY = 1.7976931348623157E308D;
double maxY = 0.0D;
GraphicInfo graphicInfo;
for(Iterator var14 = bpmnModel.getPools().iterator(); var14.hasNext(); maxY = graphicInfo.getY() + graphicInfo.getHeight()) {
Pool pool = (Pool)var14.next();
graphicInfo = bpmnModel.getGraphicInfo(pool.getId());
minX = graphicInfo.getX();
maxX = graphicInfo.getX() + graphicInfo.getWidth();
minY = graphicInfo.getY();
}
List<FlowNode> flowNodes = gatherAllFlowNodes(bpmnModel);
Iterator var24 = flowNodes.iterator();
label155:
while(var24.hasNext()) {
FlowNode flowNode = (FlowNode)var24.next();
GraphicInfo flowNodeGraphicInfo = bpmnModel.getGraphicInfo(flowNode.getId());
if(flowNodeGraphicInfo.getX() + flowNodeGraphicInfo.getWidth() > maxX) {
maxX = flowNodeGraphicInfo.getX() + flowNodeGraphicInfo.getWidth();
}
if(flowNodeGraphicInfo.getX() < minX) {
minX = flowNodeGraphicInfo.getX();
}
if(flowNodeGraphicInfo.getY() + flowNodeGraphicInfo.getHeight() > maxY) {
maxY = flowNodeGraphicInfo.getY() + flowNodeGraphicInfo.getHeight();
}
if(flowNodeGraphicInfo.getY() < minY) {
minY = flowNodeGraphicInfo.getY();
}
Iterator var18 = flowNode.getOutgoingFlows().iterator();
while(true) {
List graphicInfoList;
do {
if(!var18.hasNext()) {
continue label155;
}
SequenceFlow sequenceFlow = (SequenceFlow)var18.next();
graphicInfoList = bpmnModel.getFlowLocationGraphicInfo(sequenceFlow.getId());
} while(graphicInfoList == null);
Iterator var21 = graphicInfoList.iterator();
while(var21.hasNext()) {
graphicInfo = (GraphicInfo)var21.next();
if(graphicInfo.getX() > maxX) {
maxX = graphicInfo.getX();
}
if(graphicInfo.getX() < minX) {
minX = graphicInfo.getX();
}
if(graphicInfo.getY() > maxY) {
maxY = graphicInfo.getY();
}
if(graphicInfo.getY() < minY) {
minY = graphicInfo.getY();
}
}
}
}
List<Artifact> artifacts = gatherAllArtifacts(bpmnModel);
Iterator var27 = artifacts.iterator();
while(var27.hasNext()) {
Artifact artifact = (Artifact)var27.next();
GraphicInfo artifactGraphicInfo = bpmnModel.getGraphicInfo(artifact.getId());
if(artifactGraphicInfo != null) {
if(artifactGraphicInfo.getX() + artifactGraphicInfo.getWidth() > maxX) {
maxX = artifactGraphicInfo.getX() + artifactGraphicInfo.getWidth();
}
if(artifactGraphicInfo.getX() < minX) {
minX = artifactGraphicInfo.getX();
}
if(artifactGraphicInfo.getY() + artifactGraphicInfo.getHeight() > maxY) {
maxY = artifactGraphicInfo.getY() + artifactGraphicInfo.getHeight();
}
if(artifactGraphicInfo.getY() < minY) {
minY = artifactGraphicInfo.getY();
}
}
List<GraphicInfo> graphicInfoList = bpmnModel.getFlowLocationGraphicInfo(artifact.getId());
if(graphicInfoList != null) {
Iterator var35 = graphicInfoList.iterator();
while(var35.hasNext()) {
graphicInfo = (GraphicInfo)var35.next();
if(graphicInfo.getX() > maxX) {
maxX = graphicInfo.getX();
}
if(graphicInfo.getX() < minX) {
minX = graphicInfo.getX();
}
if(graphicInfo.getY() > maxY) {
maxY = graphicInfo.getY();
}
if(graphicInfo.getY() < minY) {
minY = graphicInfo.getY();
}
}
}
}
int nrOfLanes = 0;
Iterator var30 = bpmnModel.getProcesses().iterator();
while(var30.hasNext()) {
Process process = (Process)var30.next();
Iterator var34 = process.getLanes().iterator();
while(var34.hasNext()) {
Lane l = (Lane)var34.next();
++nrOfLanes;
graphicInfo = bpmnModel.getGraphicInfo(l.getId());
if(graphicInfo.getX() + graphicInfo.getWidth() > maxX) {
maxX = graphicInfo.getX() + graphicInfo.getWidth();
}
if(graphicInfo.getX() < minX) {
minX = graphicInfo.getX();
}
if(graphicInfo.getY() + graphicInfo.getHeight() > maxY) {
maxY = graphicInfo.getY() + graphicInfo.getHeight();
}
if(graphicInfo.getY() < minY) {
minY = graphicInfo.getY();
}
}
}
if(flowNodes.isEmpty() && bpmnModel.getPools().isEmpty() && nrOfLanes == 0) {
minX = 0.0D;
minY = 0.0D;
}
return new DefaultProcessDiagramCanvas((int)maxX + 10, (int)maxY + 10, (int)minX, (int)minY, imageType, activityFontName, labelFontName, annotationFontName, customClassLoader);
}
protected void drawActivity(DefaultProcessDiagramCanvas processDiagramCanvas, BpmnModel bpmnModel, FlowNode flowNode,List<String> highLightedFinishes, List<String> highLightedActivities, List<String> highLightedFlows, double scaleFactor) {
DefaultProcessDiagramGenerator.ActivityDrawInstruction drawInstruction = this.activityDrawInstructions.get(flowNode.getClass());
boolean highLighted;
if(drawInstruction != null) {
drawInstruction.draw(processDiagramCanvas, bpmnModel, flowNode);
boolean multiInstanceSequential = false;
boolean multiInstanceParallel = false;
highLighted = false;
if(flowNode instanceof Activity) {
Activity activity = (Activity)flowNode;
MultiInstanceLoopCharacteristics multiInstanceLoopCharacteristics = activity.getLoopCharacteristics();
if(multiInstanceLoopCharacteristics != null) {
multiInstanceSequential = multiInstanceLoopCharacteristics.isSequential();
multiInstanceParallel = !multiInstanceSequential;
}
}
GraphicInfo graphicInfo = bpmnModel.getGraphicInfo(flowNode.getId());
if(!(flowNode instanceof SubProcess)) {
if(flowNode instanceof CallActivity) {
highLighted = true;
}
} else {
highLighted = graphicInfo.getExpanded() != null && !graphicInfo.getExpanded().booleanValue();
}
if(scaleFactor == 1.0D) {
processDiagramCanvas.drawActivityMarkers((int)graphicInfo.getX(), (int)graphicInfo.getY(), (int)graphicInfo.getWidth(), (int)graphicInfo.getHeight(), multiInstanceSequential, multiInstanceParallel, highLighted);
}
if(highLightedActivities.contains(flowNode.getId())) {
drawHighLight(processDiagramCanvas, bpmnModel.getGraphicInfo(flowNode.getId()),DefaultProcessDiagramCanvas.HIGHLIGHT_COLOR,flowNode);
}
if(highLightedFinishes.contains(flowNode.getId()) && !highLightedActivities.contains(flowNode.getId())) {
drawHighLight(processDiagramCanvas, bpmnModel.getGraphicInfo(flowNode.getId()),DefaultProcessDiagramCanvas.FINISHHIGHLIGHT_COLOR,flowNode);
}
}
Iterator var25 = flowNode.getOutgoingFlows().iterator();
while(var25.hasNext()) {
SequenceFlow sequenceFlow = (SequenceFlow)var25.next();
highLighted = highLightedFlows.contains(sequenceFlow.getId());
String defaultFlow = null;
if(flowNode instanceof Activity) {
defaultFlow = ((Activity)flowNode).getDefaultFlow();
} else if(flowNode instanceof Gateway) {
defaultFlow = ((Gateway)flowNode).getDefaultFlow();
}
boolean isDefault = false;
if(defaultFlow != null && defaultFlow.equalsIgnoreCase(sequenceFlow.getId())) {
isDefault = true;
}
boolean drawConditionalIndicator = sequenceFlow.getConditionExpression() != null && !(flowNode instanceof Gateway);
String sourceRef = sequenceFlow.getSourceRef();
String targetRef = sequenceFlow.getTargetRef();
FlowElement sourceElement = bpmnModel.getFlowElement(sourceRef);
FlowElement targetElement = bpmnModel.getFlowElement(targetRef);
List<GraphicInfo> graphicInfoList = bpmnModel.getFlowLocationGraphicInfo(sequenceFlow.getId());
if(graphicInfoList != null && graphicInfoList.size() > 0) {
graphicInfoList = connectionPerfectionizer(processDiagramCanvas, bpmnModel, sourceElement, targetElement, graphicInfoList);
int[] xPoints = new int[graphicInfoList.size()];
int[] yPoints = new int[graphicInfoList.size()];
for(int i = 1; i < graphicInfoList.size(); ++i) {
GraphicInfo graphicInfo = (GraphicInfo)graphicInfoList.get(i);
GraphicInfo previousGraphicInfo = (GraphicInfo)graphicInfoList.get(i - 1);
if(i == 1) {
xPoints[0] = (int)previousGraphicInfo.getX();
yPoints[0] = (int)previousGraphicInfo.getY();
}
xPoints[i] = (int)graphicInfo.getX();
yPoints[i] = (int)graphicInfo.getY();
}
processDiagramCanvas.drawSequenceflow(xPoints, yPoints, drawConditionalIndicator, isDefault, highLighted, scaleFactor);
GraphicInfo labelGraphicInfo = bpmnModel.getLabelGraphicInfo(sequenceFlow.getId());
if(labelGraphicInfo != null) {
processDiagramCanvas.drawLabel(sequenceFlow.getName(), labelGraphicInfo, false);
}
}
}
if(flowNode instanceof FlowElementsContainer) {
var25 = ((FlowElementsContainer)flowNode).getFlowElements().iterator();
while(var25.hasNext()) {
FlowElement nestedFlowElement = (FlowElement)var25.next();
if(nestedFlowElement instanceof FlowNode) {
this.drawActivity(processDiagramCanvas, bpmnModel, (FlowNode)nestedFlowElement, highLightedFinishes,highLightedActivities, highLightedFlows, scaleFactor);
}
}
}
}
}
- 重寫DefaultProcessDiagramCanvas類
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Paint;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.font.FontRenderContext;
import java.awt.font.LineBreakMeasurer;
import java.awt.font.TextAttribute;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import java.awt.geom.PathIterator;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;
import java.util.ArrayList;
import java.util.List;
import org.activiti.bpmn.model.AssociationDirection;
import org.activiti.bpmn.model.EventSubProcess;
import org.activiti.bpmn.model.GraphicInfo;
import org.activiti.bpmn.model.Transaction;
import org.activiti.image.exception.ActivitiImageException;
import org.activiti.image.impl.ProcessDiagramSVGGraphics2D;
import org.activiti.image.impl.icon.BusinessRuleTaskIconType;
import org.activiti.image.impl.icon.CompensateIconType;
import org.activiti.image.impl.icon.CompensateThrowIconType;
import org.activiti.image.impl.icon.ErrorIconType;
import org.activiti.image.impl.icon.ErrorThrowIconType;
import org.activiti.image.impl.icon.IconType;
import org.activiti.image.impl.icon.ManualTaskIconType;
import org.activiti.image.impl.icon.MessageIconType;
import org.activiti.image.impl.icon.ReceiveTaskIconType;
import org.activiti.image.impl.icon.ScriptTaskIconType;
import org.activiti.image.impl.icon.SendTaskIconType;
import org.activiti.image.impl.icon.ServiceTaskIconType;
import org.activiti.image.impl.icon.SignalIconType;
import org.activiti.image.impl.icon.SignalThrowIconType;
import org.activiti.image.impl.icon.TaskIconType;
import org.activiti.image.impl.icon.TimerIconType;
import org.activiti.image.impl.icon.UserTaskIconType;
import org.apache.batik.dom.GenericDOMImplementation;
import org.apache.batik.svggen.SVGGraphics2DIOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import javax.imageio.ImageIO;
/**
1. Represents a canvas on which BPMN 2.0 constructs can be drawn.
2. <p>
3. @see org.activiti.image.impl.DefaultProcessDiagramGenerator
*/
public class DefaultProcessDiagramCanvas {
protected static final Logger LOGGER = LoggerFactory.getLogger(org.jeecg.activiti.util.DefaultProcessDiagramCanvas.class);
public enum SHAPE_TYPE {
Rectangle,
Rhombus,
Ellipse
}
// Predefined sized
protected static final int ARROW_WIDTH = 5;
protected static final int CONDITIONAL_INDICATOR_WIDTH = 16;
protected static final int DEFAULT_INDICATOR_WIDTH = 10;
protected static final int MARKER_WIDTH = 12;
protected static final int FONT_SIZE = 11;
protected static final int FONT_SPACING = 2;
protected static final int TEXT_PADDING = 3;
protected static final int ANNOTATION_TEXT_PADDING = 7;
protected static final int LINE_HEIGHT = FONT_SIZE + FONT_SPACING;
// Colors
protected static Color TASK_BOX_COLOR = new Color(249,
249,
249);
protected static Color SUBPROCESS_BOX_COLOR = new Color(255,
255,
255);
protected static Color EVENT_COLOR = new Color(255,
255,
255);
protected static Color CONNECTION_COLOR = new Color(88,
88,
88);
protected static Color CONDITIONAL_INDICATOR_COLOR = new Color(255,
255,
255);
protected static Color HIGHLIGHT_COLOR = Color.decode("#F68535");
protected static Color FINISHHIGHLIGHT_COLOR = Color.GREEN;
protected static Color LABEL_COLOR = new Color(112,
146,
190);
protected static Color TASK_BORDER_COLOR = new Color(187,
187,
187);
protected static Color EVENT_BORDER_COLOR = new Color(88,
88,
88);
protected static Color SUBPROCESS_BORDER_COLOR = new Color(0,
0,
0);
// Fonts
protected static Font LABEL_FONT = null;
protected static Font ANNOTATION_FONT = null;
// Strokes
protected static Stroke THICK_TASK_BORDER_STROKE = new BasicStroke(3.0f);
protected static Stroke GATEWAY_TYPE_STROKE = new BasicStroke(3.0f);
protected static Stroke END_EVENT_STROKE = new BasicStroke(3.0f);
protected static Stroke MULTI_INSTANCE_STROKE = new BasicStroke(1.3f);
protected static Stroke EVENT_SUBPROCESS_STROKE = new BasicStroke(1.0f,
BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER,
1.0f,
new float[]{1.0f},
0.0f);
protected static Stroke NON_INTERRUPTING_EVENT_STROKE = new BasicStroke(1.0f,
BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER,
1.0f,
new float[]{4.0f, 3.0f},
0.0f);
protected static Stroke HIGHLIGHT_FLOW_STROKE = new BasicStroke(1.3f);
protected static Stroke ANNOTATION_STROKE = new BasicStroke(2.0f);
protected static Stroke ASSOCIATION_STROKE = new BasicStroke(2.0f,
BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER,
1.0f,
new float[]{2.0f, 2.0f},
0.0f);
// icons
protected static int ICON_PADDING = 5;
protected static TaskIconType USERTASK_IMAGE;
protected static TaskIconType SCRIPTTASK_IMAGE;
protected static TaskIconType SERVICETASK_IMAGE;
protected static TaskIconType RECEIVETASK_IMAGE;
protected static TaskIconType SENDTASK_IMAGE;
protected static TaskIconType MANUALTASK_IMAGE;
protected static TaskIconType BUSINESS_RULE_TASK_IMAGE;
protected static IconType TIMER_IMAGE;
protected static IconType COMPENSATE_THROW_IMAGE;
protected static IconType COMPENSATE_CATCH_IMAGE;
protected static IconType ERROR_THROW_IMAGE;
protected static IconType ERROR_CATCH_IMAGE;
protected static IconType MESSAGE_CATCH_IMAGE;
protected static IconType SIGNAL_CATCH_IMAGE;
protected static IconType SIGNAL_THROW_IMAGE;
protected int canvasWidth = -1;
protected int canvasHeight = -1;
protected int minX = -1;
protected int minY = -1;
protected ProcessDiagramSVGGraphics2D g;
protected FontMetrics fontMetrics;
protected boolean closed;
protected BufferedImage processDiagram;
protected ClassLoader customClassLoader;
protected String activityFontName = "Arial";
protected String labelFontName = "Arial";
protected String annotationFontName = "Arial";
/**
* Creates an empty canvas with given width and height.
* <p>
* Allows to specify minimal boundaries on the left and upper side of the
* canvas. This is useful for diagrams that have white space there.
* Everything beneath these minimum values will be cropped.
* It's also possible to pass a specific font name and a class loader for the icon images.
*/
public DefaultProcessDiagramCanvas(int width,
int height,
int minX,
int minY,
String activityFontName,
String labelFontName,
String annotationFontName) {
this.canvasWidth = width;
this.canvasHeight = height;
this.minX = minX;
this.minY = minY;
if (activityFontName != null) {
this.activityFontName = activityFontName;
}
if (labelFontName != null) {
this.labelFontName = labelFontName;
}
if (annotationFontName != null) {
this.annotationFontName = annotationFontName;
}
initialize();
}
public DefaultProcessDiagramCanvas(int width, int height, int minX, int minY, String imageType, String activityFontName, String labelFontName, String annotationFontName, ClassLoader customClassLoader) {
this.canvasWidth = width;
this.canvasHeight = height;
this.minX = minX;
this.minY = minY;
if(activityFontName != null) {
this.activityFontName = activityFontName;
}
if(labelFontName != null) {
this.labelFontName = labelFontName;
}
if(annotationFontName != null) {
this.annotationFontName = annotationFontName;
}
this.customClassLoader = customClassLoader;
this.initialize(imageType);
}
/**
* Creates an empty canvas with given width and height.
* <p>
* Allows to specify minimal boundaries on the left and upper side of the
* canvas. This is useful for diagrams that have white space there (eg
* Signavio). Everything beneath these minimum values will be cropped.
* @param minX Hint that will be used when generating the image. Parts that fall
* below minX on the horizontal scale will be cropped.
* @param minY Hint that will be used when generating the image. Parts that fall
* below minX on the horizontal scale will be cropped.
*/
public DefaultProcessDiagramCanvas(int width,
int height,
int minX,
int minY) {
this.canvasWidth = width;
this.canvasHeight = height;
this.minX = minX;
this.minY = minY;
initialize();
}
public void initialize(String imageType) {
if("png".equalsIgnoreCase(imageType)) {
this.processDiagram = new BufferedImage(this.canvasWidth, this.canvasHeight, 2);
} else {
this.processDiagram = new BufferedImage(this.canvasWidth, this.canvasHeight, 1);
}
// Get a DOMImplementation.
DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
// Create an instance of org.w3c.dom.Document.
String svgNS = "http://www.w3.org/2000/svg";
Document document = domImpl.createDocument(svgNS,
"svg",
null);
// Create an instance of the SVG Generator.
this.g = new ProcessDiagramSVGGraphics2D(document);
this.g.setSVGCanvasSize(new Dimension(this.canvasWidth, this.canvasHeight));
this.g.setBackground(new Color(255,
255,
255,
0));
this.g.clearRect(0,
0,
canvasWidth,
canvasHeight);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setPaint(Color.black);
Font font = new Font(activityFontName,
Font.BOLD,
FONT_SIZE);
g.setFont(font);
this.fontMetrics = g.getFontMetrics();
LABEL_FONT = new Font(labelFontName,
Font.ITALIC,
10);
ANNOTATION_FONT = new Font(annotationFontName,
Font.PLAIN,
FONT_SIZE);
USERTASK_IMAGE = new UserTaskIconType();
SCRIPTTASK_IMAGE = new ScriptTaskIconType();
SERVICETASK_IMAGE = new ServiceTaskIconType();
RECEIVETASK_IMAGE = new ReceiveTaskIconType();
SENDTASK_IMAGE = new SendTaskIconType();
MANUALTASK_IMAGE = new ManualTaskIconType();
BUSINESS_RULE_TASK_IMAGE = new BusinessRuleTaskIconType();
TIMER_IMAGE = new TimerIconType();
COMPENSATE_THROW_IMAGE = new CompensateThrowIconType();
COMPENSATE_CATCH_IMAGE = new CompensateIconType();
ERROR_THROW_IMAGE = new ErrorThrowIconType();
ERROR_CATCH_IMAGE = new ErrorIconType();
MESSAGE_CATCH_IMAGE = new MessageIconType();
SIGNAL_THROW_IMAGE = new SignalThrowIconType();
SIGNAL_CATCH_IMAGE = new SignalIconType();
}
public void initialize() {
// Get a DOMImplementation.
DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
// Create an instance of org.w3c.dom.Document.
String svgNS = "http://www.w3.org/2000/svg";
Document document = domImpl.createDocument(svgNS,
"svg",
null);
// Create an instance of the SVG Generator.
this.g = new ProcessDiagramSVGGraphics2D(document);
this.g.setSVGCanvasSize(new Dimension(this.canvasWidth, this.canvasHeight));
this.g.setBackground(new Color(255,
255,
255,
0));
this.g.clearRect(0,
0,
canvasWidth,
canvasHeight);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setPaint(Color.black);
Font font = new Font(activityFontName,
Font.BOLD,
FONT_SIZE);
g.setFont(font);
this.fontMetrics = g.getFontMetrics();
LABEL_FONT = new Font(labelFontName,
Font.ITALIC,
10);
ANNOTATION_FONT = new Font(annotationFontName,
Font.PLAIN,
FONT_SIZE);
USERTASK_IMAGE = new UserTaskIconType();
SCRIPTTASK_IMAGE = new ScriptTaskIconType();
SERVICETASK_IMAGE = new ServiceTaskIconType();
RECEIVETASK_IMAGE = new ReceiveTaskIconType();
SENDTASK_IMAGE = new SendTaskIconType();
MANUALTASK_IMAGE = new ManualTaskIconType();
BUSINESS_RULE_TASK_IMAGE = new BusinessRuleTaskIconType();
TIMER_IMAGE = new TimerIconType();
COMPENSATE_THROW_IMAGE = new CompensateThrowIconType();
COMPENSATE_CATCH_IMAGE = new CompensateIconType();
ERROR_THROW_IMAGE = new ErrorThrowIconType();
ERROR_CATCH_IMAGE = new ErrorIconType();
MESSAGE_CATCH_IMAGE = new MessageIconType();
SIGNAL_THROW_IMAGE = new SignalThrowIconType();
SIGNAL_CATCH_IMAGE = new SignalIconType();
}
/**
* Generates an image of what currently is drawn on the canvas.
* <p>
* Throws an {@link ActivitiImageException} when {@link #close()} is already
* called.
*/
public InputStream generateImage() {
if (closed) {
throw new ActivitiImageException("ProcessDiagramGenerator already closed");
}
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Writer out;
out = new OutputStreamWriter(stream,
"UTF-8");
g.stream(out,
true);
return new ByteArrayInputStream(stream.toByteArray());
} catch (UnsupportedEncodingException | SVGGraphics2DIOException e) {
throw new ActivitiImageException("Error while generating process image",
e);
}
}
/**
* Closes the canvas which dissallows further drawing and releases graphical
* resources.
*/
public void close() {
g.dispose();
closed = true;
}
public void drawNoneStartEvent(String id,
GraphicInfo graphicInfo) {
drawStartEvent(id,
graphicInfo,
null);
}
public void drawTimerStartEvent(String id,
GraphicInfo graphicInfo) {
drawStartEvent(id,
graphicInfo,
TIMER_IMAGE);
}
public void drawSignalStartEvent(String id,
GraphicInfo graphicInfo) {
drawStartEvent(id,
graphicInfo,
SIGNAL_CATCH_IMAGE);
}
public void drawMessageStartEvent(String id,
GraphicInfo graphicInfo) {
drawStartEvent(id,
graphicInfo,
MESSAGE_CATCH_IMAGE);
}
public void drawStartEvent(String id,
GraphicInfo graphicInfo,
IconType icon) {
Paint originalPaint = g.getPaint();
g.setPaint(EVENT_COLOR);
Ellipse2D circle = new Ellipse2D.Double(graphicInfo.getX(),
graphicInfo.getY(),
graphicInfo.getWidth(),
graphicInfo.getHeight());
g.fill(circle);
g.setPaint(EVENT_BORDER_COLOR);
g.draw(circle);
g.setPaint(originalPaint);
// calculate coordinates to center image
if (icon != null) {
int imageX = (int) Math.round(graphicInfo.getX() + (graphicInfo.getWidth() / 2) - (icon.getWidth() / 2));
int imageY = (int) Math.round(graphicInfo.getY() + (graphicInfo.getHeight() / 2) - (icon.getHeight() / 2));
icon.drawIcon(imageX,
imageY,
ICON_PADDING,
g);
}
// set element's id
g.setCurrentGroupId(id);
}
public void drawNoneEndEvent(String id,
String name,
GraphicInfo graphicInfo) {
Paint originalPaint = g.getPaint();
Stroke originalStroke = g.getStroke();
g.setPaint(EVENT_COLOR);
Ellipse2D circle = new Ellipse2D.Double(graphicInfo.getX(),
graphicInfo.getY(),
graphicInfo.getWidth(),
graphicInfo.getHeight());
g.fill(circle);
g.setPaint(EVENT_BORDER_COLOR);
g.setStroke(END_EVENT_STROKE);
g.draw(circle);
g.setStroke(originalStroke);
g.setPaint(originalPaint);
// set element's id
g.setCurrentGroupId(id);
drawLabel(name,
graphicInfo);
}
public void drawErrorEndEvent(String id,
String name,
GraphicInfo graphicInfo) {
drawNoneEndEvent(id,
name,
graphicInfo);
int imageX = (int) (graphicInfo.getX() + (graphicInfo.getWidth() / 4));
int imageY = (int) (graphicInfo.getY() + (graphicInfo.getHeight() / 4));
ERROR_THROW_IMAGE.drawIcon(imageX,
imageY,
ICON_PADDING,
g);
}
public void drawErrorStartEvent(String id,
GraphicInfo graphicInfo) {
drawNoneStartEvent(id,
graphicInfo);
int imageX = (int) (graphicInfo.getX() + (graphicInfo.getWidth() / 4));
int imageY = (int) (graphicInfo.getY() + (graphicInfo.getHeight() / 4));
ERROR_THROW_IMAGE.drawIcon(imageX,
imageY,
ICON_PADDING,
g);
}
public void drawCatchingEvent(String id,
GraphicInfo graphicInfo,
boolean isInterrupting,
IconType icon,
String eventType) {
// event circles
Ellipse2D outerCircle = new Ellipse2D.Double(graphicInfo.getX(),
graphicInfo.getY(),
graphicInfo.getWidth(),
graphicInfo.getHeight());
int innerCircleSize = 4;
int innerCircleX = (int) graphicInfo.getX() + innerCircleSize;
int innerCircleY = (int) graphicInfo.getY() + innerCircleSize;
int innerCircleWidth = (int) graphicInfo.getWidth() - (2 * innerCircleSize);
int innerCircleHeight = (int) graphicInfo.getHeight() - (2 * innerCircleSize);
Ellipse2D innerCircle = new Ellipse2D.Double(innerCircleX,
innerCircleY,
innerCircleWidth,
innerCircleHeight);
Paint originalPaint = g.getPaint();
Stroke originalStroke = g.getStroke();
g.setPaint(EVENT_COLOR);
g.fill(outerCircle);
g.setPaint(EVENT_BORDER_COLOR);
if (!isInterrupting) {
g.setStroke(NON_INTERRUPTING_EVENT_STROKE);
}
g.draw(outerCircle);
g.setStroke(originalStroke);
g.setPaint(originalPaint);
g.draw(innerCircle);
if (icon != null) {
// calculate coordinates to center image
int imageX = (int) (graphicInfo.getX() + (graphicInfo.getWidth() / 2) - (icon.getWidth() / 2));
int imageY = (int) (graphicInfo.getY() + (graphicInfo.getHeight() / 2) - (icon.getHeight() / 2));
if ("timer".equals(eventType)) {
// move image one pixel to center timer image
imageX++;
imageY++;
}
icon.drawIcon(imageX,
imageY,
ICON_PADDING,
g);
}
// set element's id
g.setCurrentGroupId(id);
}
public void drawCatchingCompensateEvent(String id,
String name,
GraphicInfo graphicInfo,
boolean isInterrupting) {
drawCatchingCompensateEvent(id,
graphicInfo,
isInterrupting);
drawLabel(name,
graphicInfo);
}
public void drawCatchingCompensateEvent(String id,
GraphicInfo graphicInfo,
boolean isInterrupting) {
drawCatchingEvent(id,
graphicInfo,
isInterrupting,
COMPENSATE_CATCH_IMAGE,
"compensate");
}
public void drawCatchingTimerEvent(String id,
String name,
GraphicInfo graphicInfo,
boolean isInterrupting) {
drawCatchingTimerEvent(id,
graphicInfo,
isInterrupting);
drawLabel(name,
graphicInfo);
}
public void drawCatchingTimerEvent(String id,
GraphicInfo graphicInfo,
boolean isInterrupting) {
drawCatchingEvent(id,
graphicInfo,
isInterrupting,
TIMER_IMAGE,
"timer");
}
public void drawCatchingErrorEvent(String id,
String name,
GraphicInfo graphicInfo,
boolean isInterrupting) {
drawCatchingErrorEvent(id,
graphicInfo,
isInterrupting);
drawLabel(name,
graphicInfo);
}
public void drawCatchingErrorEvent(String id,
GraphicInfo graphicInfo,
boolean isInterrupting) {
drawCatchingEvent(id,
graphicInfo,
isInterrupting,
ERROR_CATCH_IMAGE,
"error");
}
public void drawCatchingSignalEvent(String id,
String name,
GraphicInfo graphicInfo,
boolean isInterrupting) {
drawCatchingSignalEvent(id,
graphicInfo,
isInterrupting);
drawLabel(name,
graphicInfo);
}
public void drawCatchingSignalEvent(String id,
GraphicInfo graphicInfo,
boolean isInterrupting) {
drawCatchingEvent(id,
graphicInfo,
isInterrupting,
SIGNAL_CATCH_IMAGE,
"signal");
}
public void drawCatchingMessageEvent(String id,
GraphicInfo graphicInfo,
boolean isInterrupting) {
drawCatchingEvent(id,
graphicInfo,
isInterrupting,
MESSAGE_CATCH_IMAGE,
"message");
}
public void drawCatchingMessageEvent(String id,
String name,
GraphicInfo graphicInfo,
boolean isInterrupting) {
drawCatchingEvent(id,
graphicInfo,
isInterrupting,
MESSAGE_CATCH_IMAGE,
"message");
drawLabel(name,
graphicInfo);
}
public void drawThrowingCompensateEvent(String id,
GraphicInfo graphicInfo) {
drawCatchingEvent(id,
graphicInfo,
true,
COMPENSATE_THROW_IMAGE,
"compensate");
}
public void drawThrowingSignalEvent(String id,
GraphicInfo graphicInfo) {
drawCatchingEvent(id,
graphicInfo,
true,
SIGNAL_THROW_IMAGE,
"signal");
}
public void drawThrowingNoneEvent(String id,
GraphicInfo graphicInfo) {
drawCatchingEvent(id,
graphicInfo,
true,
null,
"none");
}
public void drawSequenceflow(int srcX,
int srcY,
int targetX,
int targetY,
boolean conditional) {
drawSequenceflow(srcX,
srcY,
targetX,
targetY,
conditional,
false);
}
public void drawSequenceflow(int srcX,
int srcY,
int targetX,
int targetY,
boolean conditional,
boolean highLighted) {
Paint originalPaint = g.getPaint();
if (highLighted) {
g.setPaint(HIGHLIGHT_COLOR);
}
Line2D.Double line = new Line2D.Double(srcX,
srcY,
targetX,
targetY);
g.draw(line);
drawArrowHead(line);
if (conditional) {
drawConditionalSequenceFlowIndicator(line);
}
if (highLighted) {
g.setPaint(originalPaint);
}
}
public void drawSequenceflow(int[] xPoints, int[] yPoints, boolean conditional, boolean isDefault, boolean highLighted, double scaleFactor) {
this.drawConnection(xPoints, yPoints, conditional, isDefault, "sequenceFlow", AssociationDirection.ONE, highLighted, scaleFactor);
}
public void drawConnection(int[] xPoints, int[] yPoints, boolean conditional, boolean isDefault, String connectionType, AssociationDirection associationDirection, boolean highLighted, double scaleFactor) {
Paint originalPaint = this.g.getPaint();
Stroke originalStroke = this.g.getStroke();
this.g.setPaint(CONNECTION_COLOR);
if(connectionType.equals("association")) {
this.g.setStroke(ASSOCIATION_STROKE);
} else if(highLighted) {
this.g.setPaint(FINISHHIGHLIGHT_COLOR);
this.g.setStroke(HIGHLIGHT_FLOW_STROKE);
}
for(int i = 1; i < xPoints.length; ++i) {
Integer sourceX = Integer.valueOf(xPoints[i - 1]);
Integer sourceY = Integer.valueOf(yPoints[i - 1]);
Integer targetX = Integer.valueOf(xPoints[i]);
Integer targetY = Integer.valueOf(yPoints[i]);
Line2D.Double line = new Line2D.Double((double)sourceX.intValue(), (double)sourceY.intValue(), (double)targetX.intValue(), (double)targetY.intValue());
this.g.draw(line);
}
Line2D.Double line;
if(isDefault) {
line = new Line2D.Double((double)xPoints[0], (double)yPoints[0], (double)xPoints[1], (double)yPoints[1]);
this.drawDefaultSequenceFlowIndicator(line);
}
if(conditional) {
line = new Line2D.Double((double)xPoints[0], (double)yPoints[0], (double)xPoints[1], (double)yPoints[1]);
this.drawConditionalSequenceFlowIndicator(line);
}
if(associationDirection.equals(AssociationDirection.ONE) || associationDirection.equals(AssociationDirection.BOTH)) {
line = new Line2D.Double((double)xPoints[xPoints.length - 2], (double)yPoints[xPoints.length - 2], (double)xPoints[xPoints.length - 1], (double)yPoints[xPoints.length - 1]);
this.drawArrowHead(line);
}
if(associationDirection.equals(AssociationDirection.BOTH)) {
line = new Line2D.Double((double)xPoints[1], (double)yPoints[1], (double)xPoints[0], (double)yPoints[0]);
this.drawArrowHead(line);
}
this.g.setPaint(originalPaint);
this.g.setStroke(originalStroke);
}
public void drawAssociation(int[] xPoints,
int[] yPoints,
AssociationDirection associationDirection,
boolean highLighted) {
boolean conditional = false;
boolean isDefault = false;
drawConnection(xPoints,
yPoints,
conditional,
isDefault,
"association",
associationDirection,
highLighted);
}
public void drawSequenceflow(int[] xPoints,
int[] yPoints,
boolean conditional,
boolean isDefault,
boolean highLighted) {
drawConnection(xPoints,
yPoints,
conditional,
isDefault,
"sequenceFlow",
AssociationDirection.ONE,
highLighted);
}
public void drawConnection(int[] xPoints,
int[] yPoints,
boolean conditional,
boolean isDefault,
String connectionType,
AssociationDirection associationDirection,
boolean highLighted) {
Paint originalPaint = g.getPaint();
Stroke originalStroke = g.getStroke();
g.setPaint(CONNECTION_COLOR);
if ("association".equals(connectionType)) {
g.setStroke(ASSOCIATION_STROKE);
} else if (highLighted) {
g.setPaint(HIGHLIGHT_COLOR);
g.setStroke(HIGHLIGHT_FLOW_STROKE);
}
for (int i = 1; i < xPoints.length; i++) {
Integer sourceX = xPoints[i - 1];
Integer sourceY = yPoints[i - 1];
Integer targetX = xPoints[i];
Integer targetY = yPoints[i];
Line2D.Double line = new Line2D.Double(sourceX,
sourceY,
targetX,
targetY);
g.draw(line);
}
if (isDefault) {
Line2D.Double line = new Line2D.Double(xPoints[0],
yPoints[0],
xPoints[1],
yPoints[1]);
drawDefaultSequenceFlowIndicator(line);
}
if (conditional) {
Line2D.Double line = new Line2D.Double(xPoints[0],
yPoints[0],
xPoints[1],
yPoints[1]);
drawConditionalSequenceFlowIndicator(line);
}
if (associationDirection.equals(AssociationDirection.ONE) || associationDirection.equals(AssociationDirection.BOTH)) {
Line2D.Double line = new Line2D.Double(xPoints[xPoints.length - 2],
yPoints[xPoints.length - 2],
xPoints[xPoints.length - 1],
yPoints[xPoints.length - 1]);
drawArrowHead(line);
}
if (associationDirection.equals(AssociationDirection.BOTH)) {
Line2D.Double line = new Line2D.Double(xPoints[1],
yPoints[1],
xPoints[0],
yPoints[0]);
drawArrowHead(line);
}
g.setPaint(originalPaint);
g.setStroke(originalStroke);
}
public void drawSequenceflowWithoutArrow(int srcX,
int srcY,
int targetX,
int targetY,
boolean conditional) {
drawSequenceflowWithoutArrow(srcX,
srcY,
targetX,
targetY,
conditional,
false);
}
public void drawSequenceflowWithoutArrow(int srcX,
int srcY,
int targetX,
int targetY,
boolean conditional,
boolean highLighted) {
Paint originalPaint = g.getPaint();
if (highLighted) {
g.setPaint(HIGHLIGHT_COLOR);
}
Line2D.Double line = new Line2D.Double(srcX,
srcY,
targetX,
targetY);
g.draw(line);
if (conditional) {
drawConditionalSequenceFlowIndicator(line);
}
if (highLighted) {
g.setPaint(originalPaint);
}
}
public void drawArrowHead(Line2D.Double line) {
int doubleArrowWidth = (int) (2 * ARROW_WIDTH);
if (doubleArrowWidth == 0) {
doubleArrowWidth = 2;
}
Polygon arrowHead = new Polygon();
arrowHead.addPoint(0,
0);
int arrowHeadPoint = (int) (-ARROW_WIDTH);
if (arrowHeadPoint == 0) {
arrowHeadPoint = -1;
}
arrowHead.addPoint(arrowHeadPoint,
-doubleArrowWidth);
arrowHeadPoint = (int) (ARROW_WIDTH);
if (arrowHeadPoint == 0) {
arrowHeadPoint = 1;
}
arrowHead.addPoint(arrowHeadPoint,
-doubleArrowWidth);
AffineTransform transformation = new AffineTransform();
transformation.setToIdentity();
double angle = Math.atan2(line.y2 - line.y1,
line.x2 - line.x1);
transformation.translate(line.x2,
line.y2);
transformation.rotate((angle - Math.PI / 2d));
AffineTransform originalTransformation = g.getTransform();
g.setTransform(transformation);
g.fill(arrowHead);
g.setTransform(originalTransformation);
}
public void drawDefaultSequenceFlowIndicator(Line2D.Double line) {
double length = DEFAULT_INDICATOR_WIDTH;
double halfOfLength = length / 2;
double f = 8;
Line2D.Double defaultIndicator = new Line2D.Double(-halfOfLength,
0,
halfOfLength,
0);
double angle = Math.atan2(line.y2 - line.y1,
line.x2 - line.x1);
double dx = f * Math.cos(angle);
double dy = f * Math.sin(angle);
double x1 = line.x1 + dx;
double y1 = line.y1 + dy;
AffineTransform transformation = new AffineTransform();
transformation.setToIdentity();
transformation.translate(x1,
y1);
transformation.rotate((angle - 3 * Math.PI / 4));
AffineTransform originalTransformation = g.getTransform();
g.setTransform(transformation);
g.draw(defaultIndicator);
g.setTransform(originalTransformation);
}
public void drawConditionalSequenceFlowIndicator(Line2D.Double line) {
int horizontal = (int) (CONDITIONAL_INDICATOR_WIDTH * 0.7);
int halfOfHorizontal = horizontal / 2;
int halfOfVertical = CONDITIONAL_INDICATOR_WIDTH / 2;
Polygon conditionalIndicator = new Polygon();
conditionalIndicator.addPoint(0,
0);
conditionalIndicator.addPoint(-halfOfHorizontal,
halfOfVertical);
conditionalIndicator.addPoint(0,
CONDITIONAL_INDICATOR_WIDTH);
conditionalIndicator.addPoint(halfOfHorizontal,
halfOfVertical);
AffineTransform transformation = new AffineTransform();
transformation.setToIdentity();
double angle = Math.atan2(line.y2 - line.y1,
line.x2 - line.x1);
transformation.translate(line.x1,
line.y1);
transformation.rotate((angle - Math.PI / 2d));
AffineTransform originalTransformation = g.getTransform();
g.setTransform(transformation);
g.draw(conditionalIndicator);
Paint originalPaint = g.getPaint();
g.setPaint(CONDITIONAL_INDICATOR_COLOR);
g.fill(conditionalIndicator);
g.setPaint(originalPaint);
g.setTransform(originalTransformation);
}
public void drawTask(TaskIconType icon,
String id,
String name,
GraphicInfo graphicInfo) {
drawTask(id,
name,
graphicInfo);
icon.drawIcon((int) graphicInfo.getX(),
(int) graphicInfo.getY(),
ICON_PADDING,
g);
}
public void drawTask(String id,
String name,
GraphicInfo graphicInfo) {
drawTask(id,
name,
graphicInfo,
false);
}
public void drawPoolOrLane(String id,
String name,
GraphicInfo graphicInfo) {
int x = (int) graphicInfo.getX();
int y = (int) graphicInfo.getY();
int width = (int) graphicInfo.getWidth();
int height = (int) graphicInfo.getHeight();
g.drawRect(x,
y,
width,
height);
// Add the name as text, vertical
if (name != null && name.length() > 0) {
// Include some padding
int availableTextSpace = height - 6;
// Create rotation for derived font
AffineTransform transformation = new AffineTransform();
transformation.setToIdentity();
transformation.rotate(270 * Math.PI / 180);
Font currentFont = g.getFont();
Font theDerivedFont = currentFont.deriveFont(transformation);
g.setFont(theDerivedFont);
String truncated = fitTextToWidth(name,
availableTextSpace);
int realWidth = fontMetrics.stringWidth(truncated);
g.drawString(truncated,
x + 2 + fontMetrics.getHeight(),
3 + y + availableTextSpace - (availableTextSpace - realWidth) / 2);
g.setFont(currentFont);
}
// set element's id
g.setCurrentGroupId(id);
}
protected void drawTask(String id,
String name,
GraphicInfo graphicInfo,
boolean thickBorder) {
Paint originalPaint = g.getPaint();
int x = (int) graphicInfo.getX();
int y = (int) graphicInfo.getY();
int width = (int) graphicInfo.getWidth();
int height = (int) graphicInfo.getHeight();
// Create a new gradient paint for every task box, gradient depends on x and y and is not relative
g.setPaint(TASK_BOX_COLOR);
int arcR = 6;
if (thickBorder) {
arcR = 3;
}
// shape
RoundRectangle2D rect = new RoundRectangle2D.Double(x,
y,
width,
height,
arcR,
arcR);
g.fill(rect);
g.setPaint(TASK_BORDER_COLOR);
if (thickBorder) {
Stroke originalStroke = g.getStroke();
g.setStroke(THICK_TASK_BORDER_STROKE);
g.draw(rect);
g.setStroke(originalStroke);
} else {
g.draw(rect);
}
g.setPaint(originalPaint);
// text
if (name != null && name.length() > 0) {
int boxWidth = width - (2 * TEXT_PADDING);
int boxHeight = height - 16 - ICON_PADDING - ICON_PADDING - MARKER_WIDTH - 2 - 2;
int boxX = x + width / 2 - boxWidth / 2;
int boxY = y + height / 2 - boxHeight / 2 + ICON_PADDING + ICON_PADDING - 2 - 2;
drawMultilineCentredText(name,
boxX,
boxY,
boxWidth,
boxHeight);
}
// set element's id
g.setCurrentGroupId(id);
}
protected void drawMultilineCentredText(String text,
int x,
int y,
int boxWidth,
int boxHeight) {
drawMultilineText(text,
x,
y,
boxWidth,
boxHeight,
true);
}
protected void drawMultilineAnnotationText(String text,
int x,
int y,
int boxWidth,
int boxHeight) {
drawMultilineText(text,
x,
y,
boxWidth,
boxHeight,
false);
}
protected void drawMultilineText(String text,
int x,
int y,
int boxWidth,
int boxHeight,
boolean centered) {
// Create an attributed string based in input text
AttributedString attributedString = new AttributedString(text);
attributedString.addAttribute(TextAttribute.FONT,
g.getFont());
attributedString.addAttribute(TextAttribute.FOREGROUND,
Color.black);
AttributedCharacterIterator characterIterator = attributedString.getIterator();
int currentHeight = 0;
// Prepare a list of lines of text we'll be drawing
List<TextLayout> layouts = new ArrayList<TextLayout>();
String lastLine = null;
LineBreakMeasurer measurer = new LineBreakMeasurer(characterIterator,
g.getFontRenderContext());
TextLayout layout = null;
while (measurer.getPosition() < characterIterator.getEndIndex() && currentHeight <= boxHeight) {
int previousPosition = measurer.getPosition();
// Request next layout
layout = measurer.nextLayout(boxWidth);
int height = ((Float) (layout.getDescent() + layout.getAscent() + layout.getLeading())).intValue();
if (currentHeight + height > boxHeight) {
// The line we're about to add should NOT be added anymore, append three dots to previous one instead
// to indicate more text is truncated
if (!layouts.isEmpty()) {
layouts.remove(layouts.size() - 1);
if (lastLine.length() >= 4) {
lastLine = lastLine.substring(0,
lastLine.length() - 4) + "...";
}
layouts.add(new TextLayout(lastLine,
g.getFont(),
g.getFontRenderContext()));
} else {
// at least, draw one line
// even if text does not fit
// in order to avoid empty box
layouts.add(layout);
currentHeight += height;
}
break;
} else {
layouts.add(layout);
lastLine = text.substring(previousPosition,
measurer.getPosition());
currentHeight += height;
}
}
int currentY = y + (centered ? ((boxHeight - currentHeight) / 2) : 0);
int currentX = 0;
// Actually draw the lines
for (TextLayout textLayout : layouts) {
currentY += textLayout.getAscent();
currentX = x + (centered ? ((boxWidth - ((Double) textLayout.getBounds().getWidth()).intValue()) / 2) : 0);
textLayout.draw(g,
currentX,
currentY);
currentY += textLayout.getDescent() + textLayout.getLeading();
}
}
protected String fitTextToWidth(String original,
int width) {
String text = original;
// remove length for "..."
int maxWidth = width - 10;
while (fontMetrics.stringWidth(text + "...") > maxWidth && text.length() > 0) {
text = text.substring(0,
text.length() - 1);
}
if (!text.equals(original)) {
text = text + "...";
}
return text;
}
public void drawUserTask(String id,
String name,
GraphicInfo graphicInfo) {
drawTask(USERTASK_IMAGE,
id,
name,
graphicInfo);
}
public void drawScriptTask(String id,
String name,
GraphicInfo graphicInfo) {
drawTask(SCRIPTTASK_IMAGE,
id,
name,
graphicInfo);
}
public void drawServiceTask(String id,
String name,
GraphicInfo graphicInfo) {
drawTask(SERVICETASK_IMAGE,
id,
name,
graphicInfo);
}
public void drawReceiveTask(String id,
String name,
GraphicInfo graphicInfo) {
drawTask(RECEIVETASK_IMAGE,
id,
name,
graphicInfo);
}
public void drawSendTask(String id,
String name,
GraphicInfo graphicInfo) {
drawTask(SENDTASK_IMAGE,
id,
name,
graphicInfo);
}
public void drawManualTask(String id,
String name,
GraphicInfo graphicInfo) {
drawTask(MANUALTASK_IMAGE,
id,
name,
graphicInfo);
}
public void drawBusinessRuleTask(String id,
String name,
GraphicInfo graphicInfo) {
drawTask(BUSINESS_RULE_TASK_IMAGE,
id,
name,
graphicInfo);
}
public void drawExpandedSubProcess(String id,
String name,
GraphicInfo graphicInfo,
Class<?> type) {
RoundRectangle2D rect = new RoundRectangle2D.Double(graphicInfo.getX(),
graphicInfo.getY(),
graphicInfo.getWidth(),
graphicInfo.getHeight(),
8,
8);
if (type.equals(EventSubProcess.class)) {
Stroke originalStroke = g.getStroke();
g.setStroke(EVENT_SUBPROCESS_STROKE);
g.draw(rect);
g.setStroke(originalStroke);
} else if (type.equals(Transaction.class)) {
RoundRectangle2D outerRect = new RoundRectangle2D.Double(graphicInfo.getX()-3,
graphicInfo.getY()-3,
graphicInfo.getWidth()+6,
graphicInfo.getHeight()+6,
8,
8);
Paint originalPaint = g.getPaint();
g.setPaint(SUBPROCESS_BOX_COLOR);
g.fill(outerRect);
g.setPaint(SUBPROCESS_BORDER_COLOR);
g.draw(outerRect);
g.setPaint(SUBPROCESS_BOX_COLOR);
g.fill(rect);
g.setPaint(SUBPROCESS_BORDER_COLOR);
g.draw(rect);
g.setPaint(originalPaint);
} else {
Paint originalPaint = g.getPaint();
g.setPaint(SUBPROCESS_BOX_COLOR);
g.fill(rect);
g.setPaint(SUBPROCESS_BORDER_COLOR);
g.draw(rect);
g.setPaint(originalPaint);
}
if (name != null && !name.isEmpty()) {
String text = fitTextToWidth(name,
(int) graphicInfo.getWidth());
g.drawString(text,
(int) graphicInfo.getX() + 10,
(int) graphicInfo.getY() + 15);
}
// set element's id
g.setCurrentGroupId(id);
}
public void drawCollapsedSubProcess(String id,
String name,
GraphicInfo graphicInfo,
Boolean isTriggeredByEvent) {
drawCollapsedTask(id,
name,
graphicInfo,
false);
}
public void drawCollapsedCallActivity(String id,
String name,
GraphicInfo graphicInfo) {
drawCollapsedTask(id,
name,
graphicInfo,
true);
}
protected void drawCollapsedTask(String id,
String name,
GraphicInfo graphicInfo,
boolean thickBorder) {
// The collapsed marker is now visualized separately
drawTask(id,
name,
graphicInfo,
thickBorder);
}
public void drawCollapsedMarker(int x,
int y,
int width,
int height) {
// rectangle
int rectangleWidth = MARKER_WIDTH;
int rectangleHeight = MARKER_WIDTH;
Rectangle rect = new Rectangle(x + (width - rectangleWidth) / 2,
y + height - rectangleHeight - 3,
rectangleWidth,
rectangleHeight);
g.draw(rect);
// plus inside rectangle
Line2D.Double line = new Line2D.Double(rect.getCenterX(),
rect.getY() + 2,
rect.getCenterX(),
rect.getMaxY() - 2);
g.draw(line);
line = new Line2D.Double(rect.getMinX() + 2,
rect.getCenterY(),
rect.getMaxX() - 2,
rect.getCenterY());
g.draw(line);
}
public void drawActivityMarkers(int x,
int y,
int width,
int height,
boolean multiInstanceSequential,
boolean multiInstanceParallel,
boolean collapsed) {
if (collapsed) {
if (!multiInstanceSequential && !multiInstanceParallel) {
drawCollapsedMarker(x,
y,
width,
height);
} else {
drawCollapsedMarker(x - MARKER_WIDTH / 2 - 2,
y,
width,
height);
if (multiInstanceSequential) {
drawMultiInstanceMarker(true,
x + MARKER_WIDTH / 2 + 2,
y,
width,
height);
} else {
drawMultiInstanceMarker(false,
x + MARKER_WIDTH / 2 + 2,
y,
width,
height);
}
}
} else {
if (multiInstanceSequential) {
drawMultiInstanceMarker(true,
x,
y,
width,
height);
} else if (multiInstanceParallel) {
drawMultiInstanceMarker(false,
x,
y,
width,
height);
}
}
}
public void drawGateway(GraphicInfo graphicInfo) {
Polygon rhombus = new Polygon();
int x = (int) graphicInfo.getX();
int y = (int) graphicInfo.getY();
int width = (int) graphicInfo.getWidth();
int height = (int) graphicInfo.getHeight();
rhombus.addPoint(x,
y + (height / 2));
rhombus.addPoint(x + (width / 2),
y + height);
rhombus.addPoint(x + width,
y + (height / 2));
rhombus.addPoint(x + (width / 2),
y);
g.draw(rhombus);
}
public void drawParallelGateway(String id,
GraphicInfo graphicInfo) {
// rhombus
drawGateway(graphicInfo);
int x = (int) graphicInfo.getX();
int y = (int) graphicInfo.getY();
int width = (int) graphicInfo.getWidth();
int height = (int) graphicInfo.getHeight();
// plus inside rhombus
Stroke orginalStroke = g.getStroke();
g.setStroke(GATEWAY_TYPE_STROKE);
Line2D.Double line = new Line2D.Double(x + 10,
y + height / 2,
x + width - 10,
y + height / 2); // horizontal
g.draw(line);
line = new Line2D.Double(x + width / 2,
y + height - 10,
x + width / 2,
y + 10); // vertical
g.draw(line);
g.setStroke(orginalStroke);
// set element's id
g.setCurrentGroupId(id);
}
public void drawExclusiveGateway(String id,
GraphicInfo graphicInfo) {
// rhombus
drawGateway(graphicInfo);
int x = (int) graphicInfo.getX();
int y = (int) graphicInfo.getY();
int width = (int) graphicInfo.getWidth();
int height = (int) graphicInfo.getHeight();
int quarterWidth = width / 4;
int quarterHeight = height / 4;
// X inside rhombus
Stroke orginalStroke = g.getStroke();
g.setStroke(GATEWAY_TYPE_STROKE);
Line2D.Double line = new Line2D.Double(x + quarterWidth + 3,
y + quarterHeight + 3,
x + 3 * quarterWidth - 3,
y + 3 * quarterHeight - 3);
g.draw(line);
line = new Line2D.Double(x + quarterWidth + 3,
y + 3 * quarterHeight - 3,
x + 3 * quarterWidth - 3,
y + quarterHeight + 3);
g.draw(line);
g.setStroke(orginalStroke);
// set element's id
g.setCurrentGroupId(id);
}
public void drawInclusiveGateway(String id,
GraphicInfo graphicInfo) {
// rhombus
drawGateway(graphicInfo);
int x = (int) graphicInfo.getX();
int y = (int) graphicInfo.getY();
int width = (int) graphicInfo.getWidth();
int height = (int) graphicInfo.getHeight();
int diameter = width / 2;
// circle inside rhombus
Stroke orginalStroke = g.getStroke();
g.setStroke(GATEWAY_TYPE_STROKE);
Ellipse2D.Double circle = new Ellipse2D.Double(((width - diameter) / 2) + x,
((height - diameter) / 2) + y,
diameter,
diameter);
g.draw(circle);
g.setStroke(orginalStroke);
// set element's id
g.setCurrentGroupId(id);
}
public void drawEventBasedGateway(String id,
GraphicInfo graphicInfo) {
// rhombus
drawGateway(graphicInfo);
int x = (int) graphicInfo.getX();
int y = (int) graphicInfo.getY();
int width = (int) graphicInfo.getWidth();
int height = (int) graphicInfo.getHeight();
double scale = .6;
GraphicInfo eventInfo = new GraphicInfo();
eventInfo.setX(x + width * (1 - scale) / 2);
eventInfo.setY(y + height * (1 - scale) / 2);
eventInfo.setWidth(width * scale);
eventInfo.setHeight(height * scale);
drawCatchingEvent(null,
eventInfo,
true,
null,
"eventGateway");
double r = width / 6.;
// create pentagon (coords with respect to center)
int topX = (int) (.95 * r); // top right corner
int topY = (int) (-.31 * r);
int bottomX = (int) (.59 * r); // bottom right corner
int bottomY = (int) (.81 * r);
int[] xPoints = new int[]{0, topX, bottomX, -bottomX, -topX};
int[] yPoints = new int[]{-(int) r, topY, bottomY, bottomY, topY};
Polygon pentagon = new Polygon(xPoints,
yPoints,
5);
pentagon.translate(x + width / 2,
y + width / 2);
// draw
g.drawPolygon(pentagon);
// set element's id
g.setCurrentGroupId(id);
}
public void drawMultiInstanceMarker(boolean sequential,
int x,
int y,
int width,
int height) {
int rectangleWidth = MARKER_WIDTH;
int rectangleHeight = MARKER_WIDTH;
int lineX = x + (width - rectangleWidth) / 2;
int lineY = y + height - rectangleHeight - 3;
Stroke orginalStroke = g.getStroke();
g.setStroke(MULTI_INSTANCE_STROKE);
if (sequential) {
g.draw(new Line2D.Double(lineX,
lineY,
lineX + rectangleWidth,
lineY));
g.draw(new Line2D.Double(lineX,
lineY + rectangleHeight / 2,
lineX + rectangleWidth,
lineY + rectangleHeight / 2));
g.draw(new Line2D.Double(lineX,
lineY + rectangleHeight,
lineX + rectangleWidth,
lineY + rectangleHeight));
} else {
g.draw(new Line2D.Double(lineX,
lineY,
lineX,
lineY + rectangleHeight));
g.draw(new Line2D.Double(lineX + rectangleWidth / 2,
lineY,
lineX + rectangleWidth / 2,
lineY + rectangleHeight));
g.draw(new Line2D.Double(lineX + rectangleWidth,
lineY,
lineX + rectangleWidth,
lineY + rectangleHeight));
}
g.setStroke(orginalStroke);
}
public void drawHighLight(int x,
int y,
int width,
int height) {
Paint originalPaint = g.getPaint();
Stroke originalStroke = g.getStroke();
g.setPaint(HIGHLIGHT_COLOR);
g.setStroke(THICK_TASK_BORDER_STROKE);
RoundRectangle2D rect = new RoundRectangle2D.Double(x,
y,
width,
height,
20,
20);
g.draw(rect);
g.setPaint(originalPaint);
g.setStroke(originalStroke);
}
public void drawHighLight(int x, int y, int width, int height, Color color) {
Paint originalPaint = this.g.getPaint();
Stroke originalStroke = this.g.getStroke();
this.g.setPaint(color);
this.g.setStroke(THICK_TASK_BORDER_STROKE);
RoundRectangle2D rect = new RoundRectangle2D.Double((double)x, (double)y, (double)width, (double)height, 20.0D, 20.0D);
this.g.draw(rect);
this.g.setPaint(originalPaint);
this.g.setStroke(originalStroke);
}
public void drawHighLightEvent(int x, int y, int width, int height, Color color) {
Paint originalPaint = this.g.getPaint();
Stroke originalStroke = this.g.getStroke();
java.awt.geom.Ellipse2D.Double circle = new java.awt.geom.Ellipse2D.Double(x, y, width, height);
this.g.setStroke(THICK_TASK_BORDER_STROKE);
this.g.setPaint(color);
this.g.draw(circle);
this.g.setPaint(originalPaint);
this.g.setStroke(originalStroke);
}
public void drawHighLightGateway(int x, int y, int width, int height, Color color){
Paint originalPaint = this.g.getPaint();
Stroke originalStroke = this.g.getStroke();
Polygon rhombus = new Polygon();
this.g.setPaint(color);
rhombus.addPoint(x, y + (height / 2));
rhombus.addPoint(x + (width / 2), y + height);
rhombus.addPoint(x + width, y + (height / 2));
rhombus.addPoint(x + (width / 2), y);
this.g.draw(rhombus);
this.g.setPaint(originalPaint);
this.g.setStroke(originalStroke);
}
public void drawHighLightTask(int x, int y, int width, int height, Color color) {
Paint originalPaint = this.g.getPaint();
Stroke originalStroke = this.g.getStroke();
this.g.setPaint(color);
this.g.setStroke(THICK_TASK_BORDER_STROKE);
RoundRectangle2D rect = new RoundRectangle2D.Double((double)x, (double)y, (double)width, (double)height, 10.0D, 10.0D);
this.g.draw(rect);
this.g.setPaint(originalPaint);
this.g.setStroke(originalStroke);
}
public void drawHighLightSubProcess(int x, int y, int width, int height, Color color) {
Paint originalPaint = this.g.getPaint();
Stroke originalStroke = this.g.getStroke();
this.g.setPaint(color);
this.g.setStroke(THICK_TASK_BORDER_STROKE);
RoundRectangle2D rect = new RoundRectangle2D.Double(x + 1, y + 1,width - 2, height - 2, 5.0D, 5.0D);
this.g.draw(rect);
this.g.setPaint(originalPaint);
this.g.setStroke(originalStroke);
}
public void drawTextAnnotation(String id,
String text,
GraphicInfo graphicInfo) {
int x = (int) graphicInfo.getX();
int y = (int) graphicInfo.getY();
int width = (int) graphicInfo.getWidth();
int height = (int) graphicInfo.getHeight();
Font originalFont = g.getFont();
Stroke originalStroke = g.getStroke();
g.setFont(ANNOTATION_FONT);
Path2D path = new Path2D.Double();
x += .5;
int lineLength = 18;
path.moveTo(x + lineLength,
y);
path.lineTo(x,
y);
path.lineTo(x,
y + height);
path.lineTo(x + lineLength,
y + height);
path.lineTo(x + lineLength,
y + height - 1);
path.lineTo(x + 1,
y + height - 1);
path.lineTo(x + 1,
y + 1);
path.lineTo(x + lineLength,
y + 1);
path.closePath();
g.draw(path);
int boxWidth = width - (2 * ANNOTATION_TEXT_PADDING);
int boxHeight = height - (2 * ANNOTATION_TEXT_PADDING);
int boxX = x + width / 2 - boxWidth / 2;
int boxY = y + height / 2 - boxHeight / 2;
if (text != null && !text.isEmpty()) {
drawMultilineAnnotationText(text,
boxX,
boxY,
boxWidth,
boxHeight);
}
// restore originals
g.setFont(originalFont);
g.setStroke(originalStroke);
// set element's id
g.setCurrentGroupId(id);
}
public void drawLabel(String text,
GraphicInfo graphicInfo) {
drawLabel(text,
graphicInfo,
true);
}
public void drawLabel(String text,
GraphicInfo graphicInfo,
boolean centered) {
float interline = 1.0f;
// text
if (text != null && text.length() > 0) {
Paint originalPaint = g.getPaint();
Font originalFont = g.getFont();
g.setPaint(LABEL_COLOR);
g.setFont(LABEL_FONT);
int wrapWidth = 100;
int textY = (int) graphicInfo.getY();
// TODO: use drawMultilineText()
AttributedString as = new AttributedString(text);
as.addAttribute(TextAttribute.FOREGROUND,
g.getPaint());
as.addAttribute(TextAttribute.FONT,
g.getFont());
AttributedCharacterIterator aci = as.getIterator();
FontRenderContext frc = new FontRenderContext(null,
true,
false);
LineBreakMeasurer lbm = new LineBreakMeasurer(aci,
frc);
while (lbm.getPosition() < text.length()) {
TextLayout tl = lbm.nextLayout(wrapWidth);
textY += tl.getAscent();
Rectangle2D bb = tl.getBounds();
double tX = graphicInfo.getX();
if (centered) {
tX += (int) (graphicInfo.getWidth() / 2 - bb.getWidth() / 2);
}
tl.draw(g,
(float) tX,
textY);
textY += tl.getDescent() + tl.getLeading() + (interline - 1.0f) * tl.getAscent();
}
// restore originals
g.setFont(originalFont);
g.setPaint(originalPaint);
}
}
/**
* This method makes coordinates of connection flow better.
* @param sourceShapeType
* @param targetShapeType
* @param sourceGraphicInfo
* @param targetGraphicInfo
* @param graphicInfoList
*/
public List<GraphicInfo> connectionPerfectionizer(org.jeecg.activiti.util.DefaultProcessDiagramCanvas.SHAPE_TYPE sourceShapeType,
org.jeecg.activiti.util.DefaultProcessDiagramCanvas.SHAPE_TYPE targetShapeType,
GraphicInfo sourceGraphicInfo,
GraphicInfo targetGraphicInfo,
List<GraphicInfo> graphicInfoList) {
Shape shapeFirst = createShape(sourceShapeType,
sourceGraphicInfo);
Shape shapeLast = createShape(targetShapeType,
targetGraphicInfo);
if (graphicInfoList != null && graphicInfoList.size() > 0) {
GraphicInfo graphicInfoFirst = graphicInfoList.get(0);
GraphicInfo graphicInfoLast = graphicInfoList.get(graphicInfoList.size() - 1);
if (shapeFirst != null) {
graphicInfoFirst.setX(shapeFirst.getBounds2D().getCenterX());
graphicInfoFirst.setY(shapeFirst.getBounds2D().getCenterY());
}
if (shapeLast != null) {
graphicInfoLast.setX(shapeLast.getBounds2D().getCenterX());
graphicInfoLast.setY(shapeLast.getBounds2D().getCenterY());
}
Point p = null;
if (shapeFirst != null) {
Line2D.Double lineFirst = new Line2D.Double(graphicInfoFirst.getX(),
graphicInfoFirst.getY(),
graphicInfoList.get(1).getX(),
graphicInfoList.get(1).getY());
p = getIntersection(shapeFirst,
lineFirst);
if (p != null) {
graphicInfoFirst.setX(p.getX());
graphicInfoFirst.setY(p.getY());
}
}
if (shapeLast != null) {
Line2D.Double lineLast = new Line2D.Double(graphicInfoLast.getX(),
graphicInfoLast.getY(),
graphicInfoList.get(graphicInfoList.size() - 2).getX(),
graphicInfoList.get(graphicInfoList.size() - 2).getY());
p = getIntersection(shapeLast,
lineLast);
if (p != null) {
graphicInfoLast.setX(p.getX());
graphicInfoLast.setY(p.getY());
}
}
}
return graphicInfoList;
}
/**
* This method creates shape by type and coordinates.
* @param shapeType
* @param graphicInfo
* @return Shape
*/
private static Shape createShape(org.jeecg.activiti.util.DefaultProcessDiagramCanvas.SHAPE_TYPE shapeType,
GraphicInfo graphicInfo) {
if (org.jeecg.activiti.util.DefaultProcessDiagramCanvas.SHAPE_TYPE.Rectangle.equals(shapeType)) {
// source is rectangle
return new Rectangle2D.Double(graphicInfo.getX(),
graphicInfo.getY(),
graphicInfo.getWidth(),
graphicInfo.getHeight());
} else if (org.jeecg.activiti.util.DefaultProcessDiagramCanvas.SHAPE_TYPE.Rhombus.equals(shapeType)) {
// source is rhombus
Path2D.Double rhombus = new Path2D.Double();
rhombus.moveTo(graphicInfo.getX(),
graphicInfo.getY() + graphicInfo.getHeight() / 2);
rhombus.lineTo(graphicInfo.getX() + graphicInfo.getWidth() / 2,
graphicInfo.getY() + graphicInfo.getHeight());
rhombus.lineTo(graphicInfo.getX() + graphicInfo.getWidth(),
graphicInfo.getY() + graphicInfo.getHeight() / 2);
rhombus.lineTo(graphicInfo.getX() + graphicInfo.getWidth() / 2,
graphicInfo.getY());
rhombus.lineTo(graphicInfo.getX(),
graphicInfo.getY() + graphicInfo.getHeight() / 2);
rhombus.closePath();
return rhombus;
} else if (org.jeecg.activiti.util.DefaultProcessDiagramCanvas.SHAPE_TYPE.Ellipse.equals(shapeType)) {
// source is ellipse
return new Ellipse2D.Double(graphicInfo.getX(),
graphicInfo.getY(),
graphicInfo.getWidth(),
graphicInfo.getHeight());
}
// unknown source element, just do not correct coordinates
return null;
}
/**
* This method returns intersection point of shape border and line.
* @param shape
* @param line
* @return Point
*/
private static Point getIntersection(Shape shape,
Line2D.Double line) {
if (shape instanceof Ellipse2D) {
return getEllipseIntersection(shape,
line);
} else if (shape instanceof Rectangle2D || shape instanceof Path2D) {
return getShapeIntersection(shape,
line);
} else {
// something strange
return null;
}
}
/**
* This method calculates ellipse intersection with line
* @param shape Bounds of this shape used to calculate parameters of inscribed into this bounds ellipse.
* @param line
* @return Intersection point
*/
private static Point getEllipseIntersection(Shape shape,
Line2D.Double line) {
double angle = Math.atan2(line.y2 - line.y1,
line.x2 - line.x1);
double x = shape.getBounds2D().getWidth() / 2 * Math.cos(angle) + shape.getBounds2D().getCenterX();
double y = shape.getBounds2D().getHeight() / 2 * Math.sin(angle) + shape.getBounds2D().getCenterY();
Point p = new Point();
p.setLocation(x,
y);
return p;
}
/**
* This method calculates shape intersection with line.
* @param shape
* @param line
* @return Intersection point
*/
private static Point getShapeIntersection(Shape shape,
Line2D.Double line) {
PathIterator it = shape.getPathIterator(null);
double[] coords = new double[6];
double[] pos = new double[2];
Line2D.Double l = new Line2D.Double();
while (!it.isDone()) {
int type = it.currentSegment(coords);
switch (type) {
case PathIterator.SEG_MOVETO:
pos[0] = coords[0];
pos[1] = coords[1];
break;
case PathIterator.SEG_LINETO:
l = new Line2D.Double(pos[0],
pos[1],
coords[0],
coords[1]);
if (line.intersectsLine(l)) {
return getLinesIntersection(line,
l);
}
pos[0] = coords[0];
pos[1] = coords[1];
break;
case PathIterator.SEG_CLOSE:
break;
default:
// whatever
}
it.next();
}
return null;
}
/**
* This method calculates intersections of two lines.
* @param a Line 1
* @param b Line 2
* @return Intersection point
*/
private static Point getLinesIntersection(Line2D a,
Line2D b) {
double d = (a.getX1() - a.getX2()) * (b.getY2() - b.getY1()) - (a.getY1() - a.getY2()) * (b.getX2() - b.getX1());
double da = (a.getX1() - b.getX1()) * (b.getY2() - b.getY1()) - (a.getY1() - b.getY1()) * (b.getX2() - b.getX1());
double ta = da / d;
Point p = new Point();
p.setLocation(a.getX1() + ta * (a.getX2() - a.getX1()),
a.getY1() + ta * (a.getY2() - a.getY1()));
return p;
}
public InputStream generateImage(String imageType) {
if(this.closed) {
throw new ActivitiImageException("ProcessDiagramGenerator already closed");
} else {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
ImageIO.write(this.processDiagram, imageType, out);
} catch (IOException var11) {
throw new ActivitiImageException("Error while generating process image", var11);
} finally {
try {
if(out != null) {
out.close();
}
} catch (IOException var10) {
;
}
}
return new ByteArrayInputStream(out.toByteArray());
}
}
}
- controller類中實作
@GetMapping("/getFlowImgByInstanceId")
@ApiOperation(value = "擷取流程執行個體跟蹤", notes = "擷取流程執行個體跟蹤")
public Result<Object> getFlowImgByInstanceId(@ApiParam(value = "processInstanceId", name = "流程執行個體ID") String processInstanceId) {
Result<Object> result = new Result<Object>();
result.setResult(iActivitiInstanceService.getFlowImgByInstanceId(processInstanceId));
result.setSuccess(true);
return result;
}
- service類中實作
@Override
public String getFlowImgByInstanceId(String processInstanceId) {
InputStream imageStream = null;
try {
if (StringUtils.isEmpty(processInstanceId)) {
return null;
}
// 擷取曆史流程執行個體
HistoricProcessInstance historicProcessInstance = historyService
.createHistoricProcessInstanceQuery()
.processInstanceId(processInstanceId).singleResult();
// 擷取流程中已經執行的節點,按照執行先後順序排序
List<HistoricActivityInstance> historicActivityInstances = historyService
.createHistoricActivityInstanceQuery()
.processInstanceId(processInstanceId)
.orderByHistoricActivityInstanceId()
.asc().list();
// 高亮已經執行流程節點ID集合
List<String> highLightedActivitiIds = new ArrayList<>();
for (HistoricActivityInstance historicActivityInstance : historicActivityInstances) {
// 用預設顔色
highLightedActivitiIds.add(historicActivityInstance.getActivityId());
}
List<String> currIds = historicActivityInstances.stream()
.filter(item -> StringUtils.isEmpty(item.getEndTime()))
.map(HistoricActivityInstance::getActivityId).collect(Collectors.toList());
// 獲得流程引擎配置
ProcessEngineConfiguration processEngineConfiguration = processEngine.getProcessEngineConfiguration();
BpmnModel bpmnModel = repositoryService
.getBpmnModel(historicProcessInstance.getProcessDefinitionId());
// 高亮流程已發生流轉的線id集合
List<String> highLightedFlowIds = getHighLightedFlows(bpmnModel, historicActivityInstances);
imageStream = new DefaultProcessDiagramGenerator().generateDiagram(
bpmnModel,
"png",
highLightedActivitiIds,//所有活動過的節點,包括目前在激活狀态下的節點
currIds,//目前為激活狀态下的節點
highLightedFlowIds,//活動過的線
"宋體",
"宋體",
"宋體",
processEngineConfiguration.getClassLoader(),
1.0);
// 将圖檔檔案轉化為位元組數組字元串,并對其進行Base64編碼處理
byte[] data = new byte[imageStream.available()];
imageStream.read(data);
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (imageStream != null) {
try {
imageStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
/**
* 擷取已經流轉的線
* @param bpmnModel
* @param historicActivityInstances
* @return
*/
private static List<String> getHighLightedFlows(BpmnModel bpmnModel, List<HistoricActivityInstance> historicActivityInstances) {
// 高亮流程已發生流轉的線id集合
List<String> highLightedFlowIds = new ArrayList<>();
// 全部活動節點
List<FlowNode> historicActivityNodes = new ArrayList<>();
// 已完成的曆史活動節點
List<HistoricActivityInstance> finishedActivityInstances = new ArrayList<>();
for (HistoricActivityInstance historicActivityInstance : historicActivityInstances) {
FlowNode flowNode = (FlowNode) bpmnModel.getMainProcess().getFlowElement(historicActivityInstance.getActivityId(), true);
historicActivityNodes.add(flowNode);
if (historicActivityInstance.getEndTime() != null) {
finishedActivityInstances.add(historicActivityInstance);
}
}
FlowNode currentFlowNode = null;
FlowNode targetFlowNode = null;
// 周遊已完成的活動執行個體,從每個執行個體的outgoingFlows中找到已執行的
for (HistoricActivityInstance currentActivityInstance : finishedActivityInstances) {
// 獲得目前活動對應的節點資訊及outgoingFlows資訊
currentFlowNode = (FlowNode) bpmnModel.getMainProcess().getFlowElement(currentActivityInstance.getActivityId(), true);
List<SequenceFlow> sequenceFlows = currentFlowNode.getOutgoingFlows();
/**
* 周遊outgoingFlows并找到已已流轉的 滿足如下條件認為已已流轉:
* 1.目前節點是并行網關或相容網關,則通過outgoingFlows能夠在曆史活動中找到的全部節點均為已流轉
* 2.目前節點是以上兩種類型之外的,通過outgoingFlows查找到的時間最早的流轉節點視為有效流轉
*/
if ("parallelGateway".equals(currentActivityInstance.getActivityType())
|| "inclusiveGateway".equals(currentActivityInstance.getActivityType())) {
// 周遊曆史活動節點,找到比對流程目标節點的
for (SequenceFlow sequenceFlow : sequenceFlows) {
targetFlowNode = (FlowNode) bpmnModel.getMainProcess().getFlowElement(sequenceFlow.getTargetRef(), true);
if (historicActivityNodes.contains(targetFlowNode)) {
highLightedFlowIds.add(sequenceFlow.getId());
}
}
} else {
List<Map<String, Object>> tempMapList = new ArrayList<>();
for (SequenceFlow sequenceFlow : sequenceFlows) {
for (HistoricActivityInstance historicActivityInstance : historicActivityInstances) {
if (historicActivityInstance.getActivityId().equals(sequenceFlow.getTargetRef())) {
Map<String, Object> map = new HashMap<>();
map.put("highLightedFlowId", sequenceFlow.getId());
map.put("highLightedFlowStartTime", historicActivityInstance.getStartTime().getTime());
tempMapList.add(map);
}
}
}
if (!CollectionUtils.isEmpty(tempMapList)) {
// 周遊比對的集合,取得開始時間最早的一個
long earliestStamp = 0L;
String highLightedFlowId = null;
for (Map<String, Object> map : tempMapList) {
long highLightedFlowStartTime = Long.valueOf(map.get("highLightedFlowStartTime").toString());
if (earliestStamp == 0 || earliestStamp == highLightedFlowStartTime) {
highLightedFlowId = map.get("highLightedFlowId").toString();
earliestStamp = highLightedFlowStartTime;
}
}
highLightedFlowIds.add(highLightedFlowId);
}
}
}
return highLightedFlowIds;
}
結尾
如果我的部落格對你有幫助,請記得給我點贊,你的鼓勵才是我開源這部分代碼的動力!