jbpm流程設計器開發(fā)(1)
<?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="狀態(tài)"/>
<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主要顯示流程節(jié)點列表
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中被選中的節(jié)點,并在面板上繪制該節(jié)點
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插入節(jié)點
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){
//獲取被選中節(jié)點
Object obj = lp.getSelectedValue();
if(obj instanceof Node){
Node n = (Node)obj;
Point p = e.getPoint();
if(!n.getType().equals("straight")){
//鼠標點擊時,在jgraph面板上插入節(jié)點
inser(p, n);
lp.clearSelection();
}
}
}

public void mousePressed(MouseEvent e){
}

public void mouseReleased(MouseEvent e){
}

public void mouseEntered(MouseEvent e){
//只有在線條節(jié)點被選中時,才能繪制連接線
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
本篇主要實現了多個流程節(jié)點的繪制。
部分功能代碼
FlowNode.xml主要配置流程節(jié)點的屬性。























LeftPanel.java主要顯示流程節(jié)點列表













































































































GraphView.java獲取LeftPanel中被選中的節(jié)點,并在面板上繪制該節(jié)點

















































































繪制的流程圖

現在已經基本實現流程的繪制了,下一篇將會介紹流程定義文件保存打開。
源碼:源碼
打包jar:jgraphxflow.jar