📄 workflow_fileimportgxl.java
字号:
package treedoc;
// 功能
// 还原显示外部GXL流程文件为GRAPH图类.....
/**
* 名称 : WORKFLOW_FILEIMPORTGXL
* 描述 : WWW.FANGFA.NET 工作流管理系统--流程拓扑图显示类
* 版权信息 : Copyright (c) 2004 COMSCI
* @作者 : COMSCI Sichuan Fangfa Digital
* @版本 : 0.9 builder 2004091910
* @日期 : 2004/09/19
*/
import java.io.*;
import java.util.*;
import java.util.List;
import javax.xml.parsers.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import org.jgraph.*;
import org.jgraph.graph.*;
import org.jgraph.layout.*;
import org.jgraph.pad.*;
import org.jgraph.util.*;
import org.w3c.dom.*;
public class workflow_FileImportGXL
extends workflow_AbstractActionDefault {
String files;
public workflow_FileImportGXL(String file) {
files = file;
}
public void actionPerformed(ActionEvent e) {
try {
// String file = f.getDirectory() + f.getFile();
JGraph graph = getCurrentGraph();
parseGXLFileInto(files, graph);
}
catch (Exception ex) {
// graphpad.error(ex.toString());
}
}
/**
* Extracts visual properties of the node from the child 'view' element
* Currently recognized properties:
* - font, attrs: name, size, style(plain, bold, italic)
* - color
* - background-color
* - autosize
*/
static void fetchNodeViewProperties(Node gnode, Map gnode_attrs) {
NodeList gnode_children = gnode.getChildNodes();
for (int gnode_child_i = 0; gnode_child_i < gnode_children.getLength();
gnode_child_i++) {
Node gnode_child = gnode_children.item(gnode_child_i);
if (gnode_child.getNodeName().equals("view")) { // View properties of the node
Element node_view = (Element) gnode_child;
Font font = GraphConstants.DEFAULTFONT;
String fontName = null;
if (node_view.getAttribute("font-name") != null) {
fontName = node_view.getAttribute("font-name").toString();
}
float fontSize = font.getSize2D();
if (node_view.getAttribute("font-size") != null) {
try {
fontSize = Float.parseFloat(node_view.getAttribute("font-size"));
}
catch (NumberFormatException nfe) {
// Will use default size
}
}
int styleMask = 0;
if (node_view.getAttribute("font-style") != null) {
String style = node_view.getAttribute("font-style");
if (style.equals("plain")) {
styleMask = Font.PLAIN;
}
if (style.indexOf("italic") != -1) {
styleMask += Font.ITALIC;
}
if (style.indexOf("bold") != -1) {
styleMask += Font.BOLD;
}
}
if (fontName != null) {
font = new Font(fontName, styleMask, (int) fontSize);
}
else {
font = font.deriveFont(styleMask, fontSize);
}
GraphConstants.setFont(gnode_attrs, font);
if (node_view.getAttribute("color") != null) {
try {
int color = Integer.parseInt(node_view.getAttribute("color"));
GraphConstants.setForeground(gnode_attrs, new Color(color));
GraphConstants.setBorderColor(gnode_attrs, new Color(color));
GraphConstants.setLineColor(gnode_attrs, new Color(color));
}
catch (NumberFormatException nfe) {
}
}
if (node_view.getAttribute("background-color") != null) {
try {
int color = Integer.parseInt(node_view.getAttribute(
"background-color"));
GraphConstants.setBackground(gnode_attrs, new Color(color));
}
catch (NumberFormatException nfe) {
}
}
if (node_view.getAttribute("auto-size") != null) {
GraphConstants.setAutoSize(gnode_attrs,
"true".equals(node_view.
getAttribute("auto-size")));
}
}
}
}
static void fetchEdgeViewProperties(Node enode, Map enode_attrs) {
fetchNodeViewProperties(enode, enode_attrs);
GraphConstants.setLineEnd(enode_attrs, GraphConstants.ARROW_SIMPLE);
GraphConstants.setLineStyle(enode_attrs, GraphConstants.STYLE_BEZIER);
}
public void parseGXLFileInto(
String name,
JGraph graph) throws Exception {
String defaultLayout = null;
GraphModel model = graph.getModel();
File f = new File(name);
Object[] oldCells = graph.getRoots();
if (oldCells != null) {
oldCells =
DefaultGraphModel.getDescendants(model, oldCells).toArray();
model.remove(oldCells);
}
// Create a DocumentBuilderFactory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// Create a DocumentBuilder
DocumentBuilder db = dbf.newDocumentBuilder();
// Parse the input file to get a Document object
Document doc = db.parse(f);
// Get the first child (the graph-element)
// List for the new Cells
Element gxl = (Element) doc.getDocumentElement(); // First gxl element
List newCells = new ArrayList();
// ConnectionSet for the Insert method
ConnectionSet cs = new ConnectionSet();
// Hashtable for the ID lookup (ID to Vertex)
Map ids = new Hashtable();
// Hashtable for Attributes (Vertex to Map)
Hashtable attributes = new Hashtable();
NodeList graph_list = gxl.getChildNodes();
if (graph_list.getLength() == 0) {
return;
}
for (int graph_index = 0; graph_index < graph_list.getLength();
graph_index++) {
Node graph_node = graph_list.item(graph_index);
if (graph_node.getNodeName().equals("graph")) {
Element graph_elem = (Element) graph_node;
NodeList list = graph_elem.getChildNodes();
boolean defaultDirected = "directed".equals(graph_elem.getAttribute(
"edgemode")) ||
"defaultdirected".equals(graph_elem.getAttribute("edgemode"));
// End of Opheamro
// Get Graph's Child Nodes (the cells)
// Loop Children
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
// Fetch Label
String label = getLabel(node);
// If Valid Node
if (node.getAttributes() != null && node.getNodeName() != null) {
// Fetch Type
String type = node.getNodeName().toString().toLowerCase();
// Create Vertex
if (type.equals("node")) {
// Fetch ID Node
String id = null;
Node tmp = node.getAttributes().getNamedItem("id");
// Fetch ID Value
if (tmp != null) {
id = tmp.getNodeValue();
// Need unique valid ID
}
if (id != null && !ids.keySet().contains(id)) {
// Create Vertex with label
DefaultGraphCell vertex = new DefaultGraphCell(new
GPUserObject(
label));
// Add One Floating Port
vertex.add(new DefaultPort());
// Add ID, Vertex pair to Hashtable
ids.put(id, vertex);
// Add Default Attributes
Map node_attrs = createDefaultAttributes();
// get Bounds attri AND PUT IT TO NODE'S ATT
int idd = 0;
int iddd[] = {
0, 0, 0, 0};
for (Enumeration e = getBound(node).elements();
e.hasMoreElements(); ) {
iddd[idd] = Integer.parseInt(e.nextElement().toString());
idd = idd + 1;
}
GraphConstants.setBounds(node_attrs,
new Rectangle2D.Double(iddd[0],
iddd[1],
iddd[2], iddd[3]));
fetchNodeViewProperties(node, node_attrs);
GraphConstants.setBendable(node_attrs, true);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -