📄 defaultgraphmodelfileformatxml.java
字号:
public static String[] knownKeys =
new String[] {
GraphConstants.ABSOLUTE,
GraphConstants.AUTOSIZE,
GraphConstants.BACKGROUND,
GraphConstants.BEGINFILL,
GraphConstants.BEGINSIZE,
GraphConstants.BENDABLE,
GraphConstants.BORDER,
GraphConstants.BORDERCOLOR,
GraphConstants.BOUNDS,
GraphConstants.CONNECTABLE,
GraphConstants.DASHPATTERN,
GraphConstants.DISCONNECTABLE,
GraphConstants.EDITABLE,
GraphConstants.ENDFILL,
GraphConstants.ENDSIZE,
GraphConstants.FONT,
GraphConstants.FOREGROUND,
GraphConstants.HORIZONTAL_ALIGNMENT,
GraphConstants.VERTICAL_ALIGNMENT,
GraphConstants.ICON,
GraphConstants.LABELPOSITION,
GraphConstants.LINEBEGIN,
GraphConstants.LINECOLOR,
GraphConstants.LINEEND,
GraphConstants.LINESTYLE,
GraphConstants.LINEWIDTH,
GraphConstants.MOVEABLE,
GraphConstants.OFFSET,
GraphConstants.OPAQUE,
GraphConstants.POINTS,
GraphConstants.ROUTING,
GraphConstants.SIZE,
GraphConstants.SIZEABLE,
GraphConstants.VALUE };
public static Class[] keyTypes =
new Class[] {
Boolean.class,
Boolean.class,
Color.class,
Boolean.class,
Integer.class,
Boolean.class,
Border.class,
Color.class,
Rectangle.class,
Boolean.class,
float[].class,
Boolean.class,
Boolean.class,
Boolean.class,
Integer.class,
Font.class,
Color.class,
Integer.class,
Integer.class,
Icon.class,
Point.class,
Integer.class,
Color.class,
Integer.class,
Integer.class,
Float.class,
Boolean.class,
Point.class,
Boolean.class,
List.class,
Edge.Routing.class,
Dimension.class,
Boolean.class,
Object.class };
public static String encodeMap(
String indent,
Map attributes,
boolean invert,
Set excludeAttributes,
boolean URLencodeValues) {
String xml = new String("");
Iterator it = attributes.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Object key = entry.getKey();
if (excludeAttributes == null
|| !excludeAttributes.contains(key)) {
Object value = entry.getValue();
if (invert) {
Object tmp = key;
key = value;
value = tmp;
}
if (URLencodeValues) {
try {
key = URLEncoder.encode(key.toString(), "UTF-8");
value = URLEncoder.encode(encodeValue(value), "UTF-8");
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
xml += indent
+ "<a key=\""
+ encodeKey(key.toString())
+ "\" val=\""
+ encodeValue(value)
+ "\"/>\n";
}
}
return xml;
}
public static String encodeUserObjects(
String indent,
Map userObjects) {
String xml = new String("");
Iterator it = userObjects.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Object key = entry.getValue();
Object value = entry.getKey();
if (value instanceof GPUserObject)
{
xml += indent
+ "<a key=\""
+ encodeKey(key.toString())
+ "\"";
Map map = ((GPUserObject) value).getProperties();
xml += ">\n" + encodeMap(indent+"\t", map, false, null, true)
+ indent + "</a>\n";
}
else if ( value instanceof JGpdModelNode )
{
xml += indent
+ "<a key=\""
+ encodeKey(key.toString())
+ "\" val=\""
+ ((JGpdModelNode) value).getExportModelPrefix()
+ ((JGpdModelNode) value).getDisplayedNodeType()
+ "\">\n";
String local_indent = indent + "\t";
// Output the entire XML representation of this object
xml += ((JGpdModelNode) value).writeXML(local_indent + " ");
xml += indent
+ "</a>\n";
}
else
{
try {
value = URLEncoder.encode(encodeValue(value), "UTF-8");
} catch (Exception e) {
System.err.println(e.getMessage());
}
xml += indent
+ "<a key=\""
+ encodeKey(key.toString())
+ "\" val=\""
+ value
+ "\"/>\n";
}
}
return xml;
}
public static String encodeKey(String key) {
// for (int i = 0; i < knownKeys.length; i++)
// if (key.equals(knownKeys[i]))
// return Integer.toString(i);
return key;
}
public static String encodeValue(Object value) {
String ret = "";
if (value instanceof Rectangle) {
Rectangle r = (Rectangle) value;
ret = r.x + "," + r.y + "," + r.width + "," + r.height;
} else if (value instanceof List) { // TODO: non-points
List list = (List) value;
String s = "";
for (int i = 0; i < list.size(); i++) {
if (list.get(i) instanceof Point) {
Point pt = (Point) list.get(i);
s = s + pt.x + "," + pt.y + ",";
}
}
ret = (s.length() > 0) ? s.substring(0, s.length() - 1) : s;
} else if (value instanceof Font) {
Font font = (Font) value;
ret = font.getName()
+ ","
+ font.getSize()
+ ","
+ font.getStyle();
} else if (value instanceof Color) {
Color color = (Color) value;
ret = Integer.toString(color.getRed())
+ ","
+ Integer.toString(color.getGreen())
+ ","
+ Integer.toString(color.getBlue());
} else if (value instanceof Point) {
Point point = (Point) value;
ret = point.x + "," + point.y;
} else if (value instanceof float[]) {
float[] f = (float[]) value;
String s = "";
for (int i = 0; i < f.length; i++) {
s = s + Float.toString(f[i]) + ",";
}
ret = s.substring(0, s.length() - 1);
} else if (value instanceof Border) {
if (value instanceof LineBorder) {
LineBorder lb = (LineBorder) value;
ret = "L,"
+ lb.getLineColor().getRGB()
+ ","
+ lb.getThickness();
} else if (value instanceof BevelBorder) {
BevelBorder bb = (BevelBorder) value;
ret = "B," + bb.getBevelType();
}
} else if (value instanceof ImageIconBean) {
ImageIconBean icon = (ImageIconBean) value;
ret = icon.getFileName();
} else if (value instanceof Edge.Routing) {
if (value instanceof DefaultEdge.DefaultRouting)
ret = "simple";
} else if (value != null)
ret = value.toString();
return ret;
}
public static Map decodeMap(Node node, boolean useKnownKeys, boolean URLdecodeValues) {
Hashtable map = new Hashtable();
for (int i = 0; i < node.getChildNodes().getLength(); i++) {
Node child = node.getChildNodes().item(i);
if (child.getNodeName().toLowerCase().equals("a")) {
Node key = child.getAttributes().getNamedItem("key");
Node value = child.getAttributes().getNamedItem("val");
if (key != null && value != null) {
String keyVal = key.getNodeValue().toString();
Object valueS = value.getNodeValue().toString();
if (useKnownKeys) {
int index = -1;
for (int j=0; j<knownKeys.length; j++)
if (keyVal.equals(knownKeys[j]))
index = j;
if (index != -1)
valueS =
decodeValue(keyTypes[index], valueS.toString());
} else if (URLdecodeValues) {
try {
keyVal = URLDecoder.decode(keyVal.toString(), "UTF-8");
valueS = URLDecoder.decode(valueS.toString(), "UTF-8");
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
if (valueS != null)
map.put(keyVal, valueS);
}
}
}
return map;
}
public static Map decodeUserObjects(Node node) {
Hashtable map = new Hashtable();
for (int i = 0; i < node.getChildNodes().getLength(); i++) {
Node child = node.getChildNodes().item(i);
if (child.getNodeName().toLowerCase().equals("a")) {
Node key = child.getAttributes().getNamedItem("key");
Node value = child.getAttributes().getNamedItem("val");
if (key != null) {
String keyVal = key.getNodeValue().toString();
if (value != null) {
Object valueS = value.getNodeValue().toString();
if (valueS != null) {
if (((String)valueS).charAt(0) == 'x')
{
for (int j = 0; j < child.getChildNodes().getLength(); j++)
{
Node next_child = child.getChildNodes().item(j);
JGpdModelNode importNode = importModel.importNode(next_child,(String)valueS);
if ( importNode != null )
{
map.put(keyVal, importNode);
}
}
}
else
{
try {
valueS = URLDecoder.decode(valueS.toString(), "UTF-8");
} catch (Exception e) {
System.err.println(e.getMessage());
}
map.put(keyVal, valueS);
}
}
} else {
Map properties = decodeMap(child, false, true);
if (properties != null)
map.put(keyVal, properties);
}
}
}
}
return map;
}
public static String[] tokenize(String s, String token) {
StringTokenizer tokenizer = new StringTokenizer(s, token);
String[] tok = new String[tokenizer.countTokens()];
int i = 0;
while (tokenizer.hasMoreElements()) {
tok[i++] = tokenizer.nextToken();
}
return tok;
}
public static Object decodeValue(Class key, String value) {
if (key != String.class
&& key != Object.class
&& (value == null || value.equals("")))
return EMPTY;
if (key == Rectangle.class) {
String[] tok = tokenize(value, ",");
if (tok.length == 4) {
int x = Integer.parseInt(tok[0]);
int y = Integer.parseInt(tok[1]);
int w = Integer.parseInt(tok[2]);
int h = Integer.parseInt(tok[3]);
return new Rectangle(x, y, w, h);
}
} else if (key == List.class) { // FIX: Do not assume Points!
List list = new LinkedList();
String[] tok = tokenize(value, ",");
for (int i = 0; i < tok.length; i = i + 2) {
int x = Integer.parseInt(tok[i]);
int y = Integer.parseInt(tok[i + 1]);
list.add(new Point(x, y));
}
return list;
} else if (key == Font.class) {
String[] tok = tokenize(value, ",");
if (tok.length == 3) {
String name = tok[0];
int size = Integer.parseInt(tok[1]);
int style = Integer.parseInt(tok[2]);
return new Font(name, style, size);
}
} else if (key == Color.class) {
String[] tok = tokenize(value, ",");
if (tok.length == 3) {
int r = Integer.parseInt(tok[0]);
int g = Integer.parseInt(tok[1]);
int b = Integer.parseInt(tok[2]);
return new Color(r, g, b);
}
return new Color(Integer.parseInt(value));
} else if (key == Point.class) {
String[] tok = tokenize(value, ",");
if (tok.length == 2) {
int x = Integer.parseInt(tok[0]);
int y = Integer.parseInt(tok[1]);
return new Point(x, y);
}
} else if (key == float[].class) {
String[] tok = tokenize(value, ",");
float[] f = new float[tok.length];
for (int i = 0; i < tok.length; i++)
f[i] = Float.parseFloat(tok[i]);
return f;
} else if (key == Integer.class) {
return new Integer(value);
} else if (key == Border.class) {
String[] tok = tokenize(value, ",");
if (tok[0].equals("L")) { // LineBorder
Color c = new Color(Integer.parseInt(tok[1]));
int thickness = Integer.parseInt(tok[2]);
return BorderFactory.createLineBorder(c, thickness);
} else if (tok[0].equals("B")) { // BevelBorder
int type = Integer.parseInt(tok[1]);
return BorderFactory.createBevelBorder(type);
}
return BorderFactory.createLineBorder(Color.black, 1);
} else if (key == Boolean.class) {
return new Boolean(value);
} else if (key == Float.class) {
return new Float(value);
} else if (key == Icon.class) {
try {
return new ImageIconBean(new URL(value));
} catch (Exception e) {
System.err.println("Invalid URL: "+value);
return new ImageIconBean(value);
}
} else if (key == Edge.Routing.class) {
if (value.equals("simple"))
return GraphConstants.ROUTING_SIMPLE;
}
return value;
}
//
// Cell Factory
//
public static DefaultGraphCell createCell(String type, Object userObject) {
if (userObject instanceof Map)
userObject = new GPUserObject((Map) userObject);
if (type.equals("rect"))
return new DefaultGraphCell(userObject);
else if (type.equals("text"))
return new TextCell(userObject);
else if (type.equals("activity"))
return new ActivityCell(userObject);
else if (type.equals("decision"))
return new DecisionCell(userObject);
//如果是inclusive通路
else if (type.equals("inclusiveGateway"))
return new InclusiveGatewayCell(userObject);
//如果是parallel通路
else if (type.equals("parallelGateway"))
return new ParallelGatewayCell(userObject);
//如果是complex通路
else if (type.equals("complexGateway"))
return new ComplexGatewayCell(userObject);
else if (type.equals("start"))
return new StartCell(userObject);
else if (type.equals("end"))
return new EndCell(userObject);
else if (type.equals("split"))
return new SplitCell(userObject);
else if (type.equals("join"))
return new JoinCell(userObject);
else if (type.equals("port"))
return new DefaultPort(userObject);
else if (type.equals("edge"))
return new DefaultEdge(userObject);
return null;
}
public static String getType(Object cell) {
if (cell instanceof DefaultPort)
return "port";
else if (cell instanceof TextCell)
return "text";
else if (cell instanceof ActivityCell)
return "activity";
else if (cell instanceof DecisionCell)
return "decision";
//如果是inclusive通路
else if (cell instanceof InclusiveGatewayCell)
return "inclusiveGateway";
//如果是complex通路
else if (cell instanceof ComplexGatewayCell)
return "complexGateway";
//如果是complex通路
else if (cell instanceof ParallelGatewayCell)
return "parallelGateway";
else if (cell instanceof StartCell)
return "start";
else if (cell instanceof EndCell)
return "end";
else if (cell instanceof SplitCell)
return "split";
else if (cell instanceof JoinCell)
return "join";
else if (cell instanceof DefaultEdge)
return "edge";
return "rect";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -