📄 workflow_jgraphgxlcodec.java
字号:
package treedoc;
// 修改原JGRPAH的类,添加写ICON属性的方法...
// 将流程图解码为GXL文件的主类和方法
// 实际上要XPDL格式的文档也是在这里实现
/**
* 名称 : WORKFLOW_JGRAPHGXLCODEC
* 描述 : WWW.FANGFA.NET 工作流管理系统--生成流程拓扑图GXL文件类
* 版权信息 : Copyright (c) 2004 COMSCI
* @作者 : COMSCI Sichuan Fangfa Digital
* @版本 : 0.9 builder 2004091910
* @日期 : 2004/09/19
*/
import javax.swing.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.jgraph.JGraph;
import org.jgraph.graph.ConnectionSet;
import org.jgraph.graph.DefaultEdge;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.DefaultPort;
import org.jgraph.graph.GraphCell;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.GraphModel;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.jgraph.util.JGraphUtilities;
/**
* @author Gaudenz Alder
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public class workflow_JGraphGXLCodec {
static transient Hashtable encodeHash;
static transient Hashtable decodeHash;
/**
* Retrieves the encoding Hashtable with the node's Id.
*
* It may be usefull to sirialize the values of the nodes.
* @return Hastable with elements : ((key : node), (value : GXL id)).
*/
public static Hashtable getLastEncodingHashtable() {
return encodeHash;
}
/**
* Retrieves the decoding Hashtable with the node's Id.
*
* It may be usefull to sirialize the values of the nodes.
* @return Hastable with elements : ((key : node), (value : GXL id)).
*/
public static Hashtable getLastDecodingHashtable() {
return decodeHash;
}
/**
* Create a GXL-representation for all the cells.
*
* @param graph JGraph to encode.
* @return Encoded string.
*/
public static String encode(JGraph graph) {
Object[] cells = graph.getDescendants(graph.getRoots());
return encode(graph, cells);
}
/**
* Create a GXL-representation for the specified cells.
*
* @param graph JGraph to encode.
* @param cells Selected cells to be encoded.
* @return Encoded string.
*/
public static String encode(JGraph graph, Object[] cells) {
int counter = 0;
encodeHash = new Hashtable();
String gxl = "<?xml version=\"1.0\"?>\n" +
"<!DOCTYPE gxl>\n" +
"<gxl xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n" +
"<graph id=\"jGraph\">\n";
// Create external keys for nodes
for (int i = 0; i < cells.length; i++) {
if (JGraphUtilities.isVertex(graph, cells[i])) {
encodeHash.put(cells[i], "node" + counter);
counter++;
}
}
// Convert Nodes
Iterator it = encodeHash.keySet().iterator();
while (it.hasNext()) {
Object node = it.next();
gxl += encodeVertex(graph, (String) encodeHash.get(node), node);
}
// Convert Edges
int edges = 0;
for (int i = 0; i < cells.length; i++) {
if (graph.getModel().isEdge(cells[i])) {
gxl += encodeEdge(graph, new Integer(edges++), cells[i]);
// Close main tags
}
}
gxl += "\n</graph>\n</gxl>";
return gxl;
}
/**
* Create a string with tabs.
*
* @param level Tab level.
* @return Tab string.
*/
private static String createTab(int level) {
String tab = "";
for (int i = 0; i < level; i++) {
tab += "\t";
}
return tab;
}
/**
* Basic value encoding.
*
* @param type GXL Type of the value (int, bool, ...)
* @param value Value to be encoded.
* @param level Tab level.
* @return Encoded string.
*/
protected static String encodeValue(String type, String value, int level) {
return createTab(level) + "<" + type + ">" + value + "</" + type + ">\n";
}
/**
* Basic boolean encoding.
*
* @param value Value to be encoded.
* @param level Tab level.
* @return Encoded string.
*/
private static String encodeValue(boolean value, int level) {
return createTab(level) + "<bool>" + value + "</bool>\n";
}
/**
* Basic integer encoding.
*
* @param value Value to be encoded.
* @param level Tab level.
* @return Encoded string.
*/
protected static String encodeValue(int value, int level) {
return createTab(level) + "<int>" + value + "</int>\n";
}
/**
* Basic String encoding.
*
* @param value Value to be encoded.
* @param level Tab level.
* @return Encoded string.
*/
protected static String encodeValue(String value, int level) {
return createTab(level) + "<string>" + value + "</string>\n";
}
/**
* Attribute encoding.
*
* @param values Values of the attribute.
* @param attributeName name of the attribute.
* @param level Tab level.
* @return Encoded string.
*/
protected static String encodeAttribute(String values, String attributeName,
int level) {
String tab = createTab(level);
return tab + "<attr name=\"" + attributeName + "\">\n" + values + tab +
"</attr>\n";
}
/**
* String encoding.
*
* @param value Value of the attribute.
* @param attributeName name of the attribute.
* @param level Tab level.
* @return Encoded string.
*/
protected static String encodeString(String value, String attributeName,
int level) {
if (value != null) {
return encodeAttribute(encodeValue(value, level + 1), attributeName,
level);
}
else {
return "";
}
}
/**
* Integer encoding.
*
* @param value Value of the attribute.
* @param attributeName name of the attribute.
* @param level Tab level.
* @return Encoded string.
*/
protected static String encodeInteger(int value, String attributeName,
int level) {
return encodeAttribute(encodeValue(value, level + 1), attributeName, level);
}
/**
* Boolean encoding.
*
* @param value Value of the attribute.
* @param attributeName name of the attribute.
* @param level Tab level.
* @return Encoded string.
*/
protected static String encodeBoolean(boolean value, String attributeName,
int level) {
return encodeAttribute(encodeValue(value, level + 1), attributeName, level);
}
/**
* Color encoding.
*
* @param color Color of the attribute.
* @param attributeName name of the attribute.
* @param level Tab level.
* @return Encoded string.
*/
protected static String encodeColor(Color color, String attributeName,
int level) {
if (color != null) {
String tab1 = createTab(level + 1);
int level2 = level + 2;
String values = tab1 + "<tup>\n" +
encodeValue(color.getRed(), level2) +
encodeValue(color.getGreen(), level2) +
encodeValue(color.getBlue(), level2) +
tab1 + "</tup>\n";
return encodeAttribute(values, attributeName, level);
}
else {
return "";
}
}
/**
* Font encoding.
*
* @param font Font of the attribute.
* @param attributeName name of the attribute.
* @param level Tab level.
* @return Encoded string.
*/
protected static String encodeFont(Font font, String attributeName, int level) {
if (font != null) {
String tab1 = createTab(level + 1);
int level2 = level + 2;
String values = tab1 + "<tup>\n" +
encodeValue(font.getFontName(), level2) +
encodeValue(font.getStyle(), level2) +
encodeValue(font.getSize(), level2) +
tab1 + "</tup>\n";
return encodeAttribute(values, attributeName, level);
}
else {
return "";
}
}
/**
* Rectangle encoding.
*
* @param rec Rectangle to be encoded.
* @param attributeName name of the attribute.
* @param level Tab level.
* @return Encoded string.
*/
protected static String encodeRectangle(Rectangle2D rec, String attributeName,
int level) {
if (rec != null) {
String tab1 = createTab(level + 1);
int level2 = level + 2;
String values = tab1 + "<tup>\n" +
encodeValue( (int) rec.getCenterX(), level2) +
encodeValue( (int) rec.getCenterY(), level2) +
encodeValue( (int) rec.getWidth(), level2) +
encodeValue( (int) rec.getHeight(), level2) +
tab1 + "</tup>\n";
return encodeAttribute(values, attributeName, level);
}
else {
return "";
}
}
/**
* Bean encoding.
* This is usefull to encode the userObject in the Vertex.
* It must be a bean with a beanInfo class in order to inspect it.
*
* @param bean Bean to be encoded.
* @param attributeName name of the attribute.
* @param level Tab level.
* @return Encoded string.
*/
protected static String encodeBean(Object bean, String attributeName,
int level) {
String encoded = "";
if (bean != null) {
try {
int level1 = level + 1;
String tab1 = createTab(level1);
int level2 = level + 2;
BeanInfo bi = null;
PropertyDescriptor tmpProperties[] = null;
PropertyDescriptor prop = null;
bi = Introspector.getBeanInfo(bean.getClass());
tmpProperties = bi.getPropertyDescriptors();
encoded += encodeString(bean.getClass().getName(), "ClassName", level1);
for (int i = 0; i < tmpProperties.length; i++) {
prop = tmpProperties[i];
encoded +=
encodeString(prop.getReadMethod().invoke(bean, null).toString(),
prop.getDisplayName(), level1);
}
encoded = encodeAttribute(encoded, attributeName, level);
}
catch (Exception e) {
e.printStackTrace();
}
}
return encoded;
}
/**
* Encode a Vertex of a graph
* @param graph Graph containing the vertex.
* @param id Id of the vertex.
* @param vertex Vertex to be encoded.
* @return Encoded string.
*/
protected static String encodeVertex(JGraph graph, String id, Object vertex) {
int level = 2;
String label = graph.convertValueToString(vertex);
Map attributes = ( (GraphCell) vertex).getAttributes();
String encoded = "\n\t<node id=\"" + id + "\">\n"
+ encodeString(label, "Label", level)
+ encodeRectangle(GraphConstants.getBounds(attributes), "Bounds", level)
+
encodeColor(GraphConstants.getBorderColor(attributes), "BorderColor",
level)
+
encodeColor(GraphConstants.getForeground(attributes), "BorderColor",
level)
+
encodeColor(GraphConstants.getBackground(attributes), "BorderColor",
level)
+ encodeFont(GraphConstants.getFont(attributes), "Font", level)
+
encodeColor(GraphConstants.getLineColor(attributes), "BorderColor",
level)
+ encodeIcon(GraphConstants.getIcon(attributes), "Icon", level)
//+ encodeBoolean(GraphConstants.getOpaque(attributes), "Opaque", level)
//+ encodeBean(GraphConstants.getValue(attributes), "Value", level)
+ "\t</node>";
return encoded;
}
protected static String encodeIcon(Icon icon, String attributeName, int level) {
if (icon != null) {
String tab1 = createTab(level + 1);
int level2 = level + 2;
String values = tab1 + "<tup>\n" +
encodeValue(icon.toString(), level2) +
tab1 + "</tup>\n";
return encodeAttribute(values, attributeName, level);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -