Posted on 2010-08-25 15:24
penngo 阅读(4253)
评论(1) 编辑 收藏 所属分类:
JBPM
jbpm流程设计器开发(1)
本篇主要实现了多个流程节点的绘制。
部分功能代码
FlowNode.xml主要配置流程节点的属性。
<?xml version="1.0" encoding="GBK" standalone="yes"?>
<flowNode>
<node height="40" width="40" type="straight" shape="shape=ellipse;perimeter=ellipsePerimeter;"

icon="images/straight.gif" label="直线"/>
<node height="40" width="40" type="start" shape="shape=ellipse;perimeter=ellipsePerimeter;" icon="images/start.gif"

label="开始"/>
<node height="40" width="40" type="end" shape="shape=doubleEllipse;perimeter=ellipsePerimeter;" icon="images/end.gif"

label="结束"/>
<node height="40" width="80" type="state" shape="rounded=1;" icon="images/state.gif" label="状态"/>
<node height="40" width="80" type="task" shape="rounded=1;" icon="images/task.gif" label="任务"/>
<node height="40" width="40" type="decision" shape="shape=rhombus;perimeter=rhombusPerimeter;"

icon="images/decision.gif" label="判断"/>
<node height="40" width="40" type="fork" shape="shape=rhombus;perimeter=rhombusPerimeter;" icon="images/fork.gif"

label="分支"/>
<node height="40" width="40" type="join" shape="shape=rhombus;perimeter=rhombusPerimeter;" icon="images/join.gif"

label="合并"/>
</flowNode>
LeftPanel.java主要显示流程节点列表
package com.workflow.designer.view;
import java.awt.Color;
import java.awt.Component;
import java.awt.Desktop;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.filechooser.FileSystemView;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

import com.mxgraph.view.mxGraph;
import com.workflow.designer.model.FlowNode;
import com.workflow.designer.model.Node;
import com.workflow.designer.util.Logger;


public class LeftPanel extends JList
{
//private JList list = null;
private GraphImpl graph = null;
private DefaultListModel listModel = null;

public LeftPanel()
{
init();
}

public DefaultListModel getListModel()
{
return listModel;
}


public void setListModel(DefaultListModel listModel)
{
this.listModel = listModel;
}


private void init()
{
listModel = new DefaultListModel();

try
{
//JList数据初始化
JAXBContext jc = JAXBContext.newInstance("com.workflow.designer.model");
Unmarshaller u = jc.createUnmarshaller();
FlowNode fn = (FlowNode) u
.unmarshal(FlowNode.class.getClassLoader().getResourceAsStream("com/workflow/designer/model/FlowNode.xml"));

for(Node n:fn.getNode())
{
listModel.addElement(n);
}
}

catch(Exception e)
{
e.printStackTrace();
}
this.setModel(listModel);
this.setCellRenderer(new MyCellRenderer());

this.addMouseListener(new MouseAdapter()
{

public void mouseClicked(MouseEvent e)
{
super.mouseClicked(e);
}

public void mousePressed(MouseEvent e)
{

super.mousePressed(e);
}
});
}

class MyCellRenderer extends JLabel implements ListCellRenderer
{
public Component getListCellRendererComponent(JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus)

{

if (value instanceof Node)
{
Node n = (Node) value;
Icon icon = new ImageIcon(getClass().getClassLoader().getResource(n.getIcon()));
setIcon(icon);
}
String s = value.toString();
setText(s);
this.setToolTipText(s);

if (isSelected)
{
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());

} else
{
setBackground(Color.WHITE);
setForeground(list.getForeground());
}
setEnabled(list.isEnabled());
setOpaque(true);
return this;
}
}

public void setGraph(GraphImpl gi)
{
this.graph = gi;
}

public static void main(String arg[])
{
new LeftPanel();
}
}
GraphView.java获取LeftPanel中被选中的节点,并在面板上绘制该节点
package com.workflow.designer.view;

import java.awt.Point;
import java.awt.event.MouseEvent;

import com.mxgraph.model.mxCell;
import com.mxgraph.view.mxGraph;
import com.workflow.designer.model.Node;
import com.workflow.designer.util.Logger;


public class GraphView extends GraphImpl
{
private mxGraph graph = this.getGraph();
private Object parent = graph.getDefaultParent();
private LeftPanel lp = null;

public GraphView(LeftPanel lp)
{
this.lp = lp;
}


public void inser(Point p, Node n)
{
this.getGraph().getModel().beginUpdate();
try

{
//调用jgraph插入节点
Object v1 = graph.insertVertex(parent, null, n.getLabel(), p.getX() - 5, p.getY() - 5,

n.getWidth(),
n.getHeight(), n.getShape());
((mxCell)v1).setConnectable(false);
}
finally

{
graph.getModel().endUpdate();
}
}

public void mouseClicked(MouseEvent e)
{
//获取被选中节点
Object obj = lp.getSelectedValue();

if(obj instanceof Node)
{
Node n = (Node)obj;
Point p = e.getPoint();

if(!n.getType().equals("straight"))
{
//鼠标点击时,在jgraph面板上插入节点
inser(p, n);
lp.clearSelection();
}
}
}


public void mousePressed(MouseEvent e)
{
}


public void mouseReleased(MouseEvent e)
{
}


public void mouseEntered(MouseEvent e)
{
//只有在线条节点被选中时,才能绘制连接线
Object obj = lp.getSelectedValue();

if(obj instanceof Node)
{
Node n = (Node)obj;
Object parent = graph.getDefaultParent();
Object childs[] = graph.getChildCells(parent);

if(n.getType().equals("straight"))
{

for(Object cell:childs)
{
((mxCell)cell).setConnectable(true); //设置可连线
}
}

else
{

for(Object cell:childs)
{
((mxCell)cell).setConnectable(false);
}
}
}
}


public void mouseExited(MouseEvent e)
{
}
}

绘制的流程图
现在已经基本实现流程的绘制了,下一篇将会介绍流程定义文件保存打开。
源码:
源码
打包jar:
jgraphxflow.jar