?? graphencoder.java
字號:
package flow.graph.gui.graph.file;
//修改原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.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
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 java.util.Vector;
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.DefaultGraphModel;
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 flow.graph.gui.graph.GPCellViewFactory;
/**
* @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 GraphEncoder {
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());
Object[] cells = graph.getRoots();
System.out.println("cells.length="+cells.length);
Vector vec = new Vector();
for(int i=0;i<cells.length;i++){
vec.add(cells[i]);
}
ByteArrayOutputStream bout = new ByteArrayOutputStream();
XMLEncoder enc = new XMLEncoder(bout);
enc.writeObject(vec);
enc.close();
System.out.println(bout);
/*
try {
XMLEncoder enc = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("D:/tmp/kuangzyc.xml")));
enc.writeObject(vec);
enc.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
*/
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 (DefaultGraphModel.isVertex(graph.getModel(), 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;
}
public static Vector encoderVertex(JGraph graph, Object obj){
Vector v = new Vector();
String label = graph.convertValueToString(obj);
LabelBean lb = new LabelBean();
lb.setType("VERTEX");
lb.setValue(label);
Map attributes = ( (GraphCell) obj).getAttributes();
v.add(lb);
v.add(attributes);
return v;
}
public static Vector encoderEdge(JGraph graph, Object obj){
Vector v = new Vector();
String label = graph.convertValueToString(obj);
LabelBean lb = new LabelBean();
lb.setType("VERTEX");
lb.setValue(label);
Map attributes = ( (GraphCell) obj).getAttributes();
v.add(lb);
v.add(attributes);
return v;
}
/**
* 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)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -