📄 testgraph.java
字号:
package test;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.swing.JApplet;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.graph.DirectedGraph;
import org.eclipse.draw2d.graph.DirectedGraphLayout;
import org.eclipse.draw2d.graph.Edge;
import org.eclipse.draw2d.graph.Node;
import org.jgraph.JGraph;
import org.jgraph.graph.ConnectionSet;
import org.jgraph.graph.DefaultEdge;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.DefaultGraphModel;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.GraphModel;
import test.bean.ActionInfo;
import test.bean.ActionStat;
import test.bean.UnionEdge;
import com.realpersist.gef.layout.NodeJoiningDirectedGraphLayout;
/**
* @author walnut
* @version 创建时间:2007-7-18
*/
public class TestGraph extends JApplet {
public Map gefNodeMap = null;
public Map graphNodeMap = null;
public List edgeList = null;
DirectedGraph directedGraph = null;
JGraph graph = null;
public TestGraph() {
}
public void init() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
graphInit();
paintGraph();
}
private void paintGraph() {
try {
// 测试数据
ActionInfo a1=new ActionInfo("a1");
ActionInfo a11=new ActionInfo("a11");
ActionInfo a12=new ActionInfo("a12");
ActionInfo a13=new ActionInfo("a13");
ActionInfo a121=new ActionInfo("a121");
ActionInfo a122=new ActionInfo("a122");
ActionInfo a123=new ActionInfo("a123");
ActionInfo a1231=new ActionInfo("a1231");
ActionInfo a1232=new ActionInfo("a1232");
ActionInfo a1233=new ActionInfo("a1233");
ActionInfo a1234=new ActionInfo("a1234");
ActionInfo b1=new ActionInfo("b1");
ActionInfo b2=new ActionInfo("b2");
ActionInfo c=new ActionInfo("c");
ActionStat as1=new ActionStat(a1,a11,new Long(20));
ActionStat as2=new ActionStat(a1,a12,new Long(20));
ActionStat as3=new ActionStat(a1,a13,new Long(20));
ActionStat as4=new ActionStat(a12,a121,new Long(20));
ActionStat as5=new ActionStat(a12,a122,new Long(20));
ActionStat as6=new ActionStat(a12,a123,new Long(20));
ActionStat as7=new ActionStat(a123,a1231,new Long(20));
ActionStat as8=new ActionStat(a123,a1232,new Long(20));
ActionStat as9=new ActionStat(a123,a1233,new Long(20));
ActionStat as10=new ActionStat(a123,a1234,new Long(20));
ActionStat as11=new ActionStat(a1232,a11,new Long(20));
ActionStat as12=new ActionStat(a1233,a122,new Long(20));
ActionStat as13=new ActionStat(a123,a1,new Long(20));
// 这一条边和其他边没有连通
ActionStat as14=new ActionStat(b1,b2,new Long(20));
// 这一条边和其他边没有连通且指向自身
ActionStat as15=new ActionStat(c,c,new Long(20));
List actionStatList =new ArrayList();
actionStatList.add(as1);
actionStatList.add(as2);
actionStatList.add(as3);
actionStatList.add(as4);
actionStatList.add(as5);
actionStatList.add(as6);
actionStatList.add(as7);
actionStatList.add(as8);
actionStatList.add(as9);
actionStatList.add(as10);
actionStatList.add(as11);
actionStatList.add(as12);
actionStatList.add(as13);
actionStatList.add(as14);
actionStatList.add(as15);
// 解析数据,构造图
gefNodeMap = new HashMap();
graphNodeMap = new HashMap();
edgeList = new ArrayList();
directedGraph = new DirectedGraph();
GraphModel model = new DefaultGraphModel();
graph.setModel(model);
Map attributes = new Hashtable();
// Set Arrow
Map edgeAttrib = new Hashtable();
GraphConstants.setLineEnd(edgeAttrib, GraphConstants.ARROW_CLASSIC);
GraphConstants.setEndFill(edgeAttrib, true);
graph.setJumpToDefaultPort(true);
if (actionStatList == null || actionStatList.size() == 0) {
graph.repaint();
return;
}
Iterator actionStatIt = actionStatList.iterator();
while (actionStatIt.hasNext()) {
ActionStat actionStat = (ActionStat) actionStatIt.next();
ActionInfo sourceAction = actionStat.getSourceAction();
ActionInfo targetAction = actionStat.getTargetAction();
Long statCount = actionStat.getStatCount();
String edgeString = "(" + statCount + ")";
addEdge(sourceAction, targetAction, 20, edgeString);
}
// 自动布局 首先用DirectedGraphLayout如果出现异常(图不是整体连通的)则采用NodeJoiningDirectedGraphLayout
// 后者可以对非连通图进行布局坐标计算,但效果不如前者好,所以首选前者,当前者不可以处理时采用后者
try{
new DirectedGraphLayout().visit(directedGraph);
}catch(Exception e1){
new NodeJoiningDirectedGraphLayout().visit(directedGraph);
}
int self_x=50;
int self_y=50;
int base_y=10;
if(graphNodeMap!=null&&gefNodeMap!=null&&graphNodeMap.size()>gefNodeMap.size()){
base_y=self_y+GraphProp.NODE_HEIGHT;
}
// 向图中添加节点node
Collection nodeCollection = graphNodeMap.values();
if (nodeCollection != null) {
Iterator nodeIterator = nodeCollection.iterator();
if (nodeIterator != null) {
while (nodeIterator.hasNext()) {
DefaultGraphCell node = (DefaultGraphCell) nodeIterator.next();
ActionInfo userObject = (ActionInfo) node.getUserObject();
if (userObject == null) {
continue;
}
Node gefNode = (Node) gefNodeMap.get(userObject);
if(gefNode==null){
// 这是因为当一个节点有一个指向自身的边的时候,我们在gefGraph中并没有计算这条边(gefGraph不能计算包含指向自己边的布局),
// 所以当在所要画的图中该节点只有一条指向自身的边的时候,我们在gefNodeMap中就找不到相应节点了
// 这个时候,我们手工给出该节点的 X,Y 坐标
gefNode=new Node();
gefNode.x=self_x;
gefNode.y=self_y-base_y;
self_x+=(10+GraphProp.NODE_WIDTH);
}
Map nodeAttrib = new Hashtable();
GraphConstants.setBorderColor(nodeAttrib, Color.black);
Rectangle2D Bounds = new Rectangle2D.Double(gefNode.x,gefNode.y+base_y, GraphProp.NODE_WIDTH,GraphProp.NODE_HEIGHT);
GraphConstants.setBounds(nodeAttrib, Bounds);
attributes.put(node, nodeAttrib);
}// while
}
}
// 向图中添加边
if (edgeList == null) {
//logger.error("edge list is null");
return;
}
for (int i = 0; i < edgeList.size(); i++) {
UnionEdge unionEdge = (UnionEdge) edgeList.get(i);
if (unionEdge == null) {
//logger.error("union edge is null");
continue;
}
ConnectionSet cs = new ConnectionSet(unionEdge.getEdge(),unionEdge.getSourceNode().getChildAt(0), unionEdge.getTargetNode().getChildAt(0));
Object[] cells = new Object[] { unionEdge.getEdge(),unionEdge.getSourceNode(), unionEdge.getTargetNode() };
attributes.put(unionEdge.getEdge(), edgeAttrib);
model.insert(cells, attributes, cs, null, null);
}
graph.repaint();
} catch (Exception e) {
e.printStackTrace();
}
}
private void graphInit() {
// Construct Model and Graph
GraphModel model = new DefaultGraphModel();
graph = new JGraph(model);
graph.setSelectionEnabled(true);
// 显示applet
JScrollPane scroll=new JScrollPane(graph);
this.getContentPane().add(scroll);
this.setSize(new Dimension(800, 800));
}
/**
* @param source
* @param target
*/
private void addEdge(ActionInfo source, ActionInfo target, int weight,String edgeString) {
if (source == null || target == null) {
return;
}
if (gefNodeMap == null) {
gefNodeMap = new HashMap();
}
if (graphNodeMap == null) {
graphNodeMap = new HashMap();
}
if (edgeList == null) {
edgeList = new ArrayList();
}
if (directedGraph == null) {
directedGraph = new DirectedGraph();
}
// 建立GEF的 node edge将来用来计算graph node的layout
addEdgeGef(source, target,weight,edgeString);
// 建立真正要用的graph的 node edge
DefaultGraphCell sourceNode = null;
DefaultGraphCell targetNode = null;
sourceNode = (DefaultGraphCell) graphNodeMap.get(source);
if (sourceNode == null) {
sourceNode = new DefaultGraphCell(source);
sourceNode.addPort();
graphNodeMap.put(source, sourceNode);
}
targetNode = (DefaultGraphCell) graphNodeMap.get(target);
if (targetNode == null) {
targetNode = new DefaultGraphCell(target);
targetNode.addPort();
graphNodeMap.put(target, targetNode);
}
DefaultEdge edge = new DefaultEdge(edgeString);
UnionEdge unionEdge = new UnionEdge();
unionEdge.setEdge(edge);
unionEdge.setSourceNode(sourceNode);
unionEdge.setTargetNode(targetNode);
edgeList.add(unionEdge);
}
/**
* @param source
* @param target
* @param weight
* @param edgeString
*/
private void addEdgeGef(ActionInfo source, ActionInfo target, int weight, String edgeString) {
if(source.equals(target)){
return;
}
// 建立GEF的 node edge将来用来计算graph node的layout
Node gefSourceNode = null;
Node gefTargetNode = null;
gefSourceNode = (Node) gefNodeMap.get(source);
if (gefSourceNode == null) {
gefSourceNode = new Node();
gefSourceNode.width = GraphProp.NODE_WIDTH;
gefSourceNode.height = GraphProp.NODE_WIDTH;
gefSourceNode.setPadding(new Insets(GraphProp.NODE_TOP_PAD,
GraphProp.NODE_LEFT_PAD, GraphProp.NODE_BOTTOM_PAD,
GraphProp.NODE_RIGHT_PAD));
directedGraph.nodes.add(gefSourceNode);
gefNodeMap.put(source, gefSourceNode);
}
gefTargetNode = (Node) gefNodeMap.get(target);
if (gefTargetNode == null) {
gefTargetNode = new Node();
gefTargetNode.width = GraphProp.NODE_WIDTH;
gefTargetNode.height = GraphProp.NODE_WIDTH;
gefTargetNode.setPadding(new Insets(GraphProp.NODE_TOP_PAD,
GraphProp.NODE_LEFT_PAD, GraphProp.NODE_BOTTOM_PAD,
GraphProp.NODE_RIGHT_PAD));
directedGraph.nodes.add(gefTargetNode);
gefNodeMap.put(target, gefTargetNode);
}
Edge gefEdge1=null;
try{
gefEdge1 = new Edge(gefSourceNode, gefTargetNode);
gefEdge1.weight = weight;
directedGraph.edges.add(gefEdge1);
}catch(Exception e){
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -