activiti获取偏移量minx与miny
背景:因为后期框架将需要,最近在学习activiti。现在想要将工作流图片展示出来。但是我想将所有节点都框起来(和高亮显示当前任务节点或者高亮显示历史节点思路完全一致)。
分为两步:第一步是controller跳转,需要获取所有节点坐标。(当前任务或者历史任务就是获取节点不一样而已)。第二步:jsp页面用绝对定位自己定义一个div(获取其他框).高亮显示即可。番外:activiti实际距离与xml中参数有偏移,偏移量一般为x-minx+2,y-miny+2(2可能跟不同api还有关系)。获取minx和miny可按如下方式进行。
/**
* 根据流程定义id获取minx和miny.
* @param processDefinitionId
* @return minx和miny
*/
public Map<String,Integer> getMinxMinY(String processDefinitionId){
int minX = Integer.MAX_VALUE;
int minY = Integer.MAX_VALUE;
RepositoryService repositoryService = processEngine.getRepositoryService();
ProcessDefinitionEntity processDefinition = (ProcessDefinitionEntity) ((RepositoryServiceImpl)repositoryService).getDeployedProcessDefinition(processDefinitionId);
if(processDefinition.getParticipantProcess() != null) {
ParticipantProcess pProc = processDefinition.getParticipantProcess();
minX = pProc.getX();
minY = pProc.getY();
}
for (ActivityImpl activity : processDefinition.getActivities()) {
if (activity.getX() < minX) {
minX = activity.getX();
}
if (activity.getY() < minY) {
minY = activity.getY();
}
for (PvmTransition sequenceFlow : activity.getOutgoingTransitions()) {
List<Integer> waypoints = ((TransitionImpl) sequenceFlow).getWaypoints();
for (int i = 0; i < waypoints.size(); i += 2) {
if (waypoints.get(i) < minX) {
minX = waypoints.get(i);
}
if (waypoints.get(i + 1) < minY) {
minY = waypoints.get(i + 1);
}
}
}
}
Map<String,Integer> map=new HashMap<String, Integer>();
map.put("minX", minX);
map.put("minY", minY);
return map;
}
1.
通过processDefinitionId,获得所有节点的x,y,width和height,当然也可包括name与id。这是一个controller方法,跳转至pic.jsp页面。代码如下:
/**
* 获取流程图片,根据流程定义ID
* @param processDefinitionId
* @return
*/
@RequestMapping(value="getProcessDiagramsByDefinId")
public String getProcessDiagramsByDefinId(String processDefinitionId,Model model)
{
RepositoryService repositoryService = processEngine.getRepositoryService();
List<Map<String, Object>> activitys = new ArrayList<Map<String, Object>>();
ProcessDefinitionEntity def = (ProcessDefinitionEntity) ((RepositoryServiceImpl)repositoryService).getDeployedProcessDefinition(processDefinitionId);
List<ActivityImpl> activitiList = def.getActivities(); // 根据流程定义,获取所有节点
//将流程所有节点的长宽高都记录一下
Map<String,Integer> m=this.getMinxMinY(processDefinitionId);
for(ActivityImpl activityImpl:activitiList){
Map<String,Object> map=new HashMap<String, Object>();
map.put("id", activityImpl.getId());
map.put("name", activityImpl.getProperty("name"));
map.put("x", activityImpl.getX()-m.get("minX")+2);
map.put("y", activityImpl.getY()-m.get("minY")+2);
map.put("width", activityImpl.getWidth());
map.put("height", activityImpl.getHeight());
activitys.add(map);
}
model.addAttribute("activitys", activitys);//将所有节点的坐标放在model中
model.addAttribute("processDefinitionId", processDefinitionId);
return "workflow/pic";
}
2.
Pic.jsp代码如下。其中,img调用的是activiti的API方法。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/view/include/taglib.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>显示流程图</title>
<script type="text/javascript">
function test(){
alert(123);
}
</script>
</head>
<body>
<img src="${ctx}/workflow/getProcessDiagram/${processDefinitionId}" style="position:absolute;left:0px;top:0px;"/>
<c:if test="${activitys!=null }">
<c:forEach items="${activitys }" var="a">
<a href="javascript:void(0)" οnclick="test()"
style="position:absolute;border:3px solid blue;left:${a.x}px;top:${a.y}px;width:${a.width}px;height:${a.height}px;display:block;"></a>
</c:forEach>
</c:if>
</body>
</html>