⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileimportgxl.java

📁 用JGraph编的软件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                // Hashtable for Attributes (Vertex to Map)
                Hashtable attributes = new Hashtable();

                Element gxl = (Element)doc.getDocumentElement(); // First gxl element

                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();
                                        fetchNodeViewProperties(node, node_attrs);
                                        attributes.put(vertex, node_attrs);
                                        // Add Vertex to new Cells
                                        newCells.add(vertex);
                                    }

                                    // Create Edge
                                } else if (type.equals("edge")) {
                                    Element edge_node = (Element)node;
                                    // Fetch Source ID Node
                                    Node tmp = node.getAttributes().getNamedItem("from");
                                    // Fetch Source ID Value
                                    String source = null;
                                    if (tmp != null)
                                        source = tmp.getNodeValue();
                                    // Fetch Target ID Node
                                    tmp = node.getAttributes().getNamedItem("to");
                                    // Fetch Target ID Value
                                    String target = null;
                                    if (tmp != null)
                                        target = tmp.getNodeValue();
                                    // Create Edge with label
                                    DefaultEdge edge = new DefaultEdge(new GPUserObject(label));
                                    // Find Source Port
                                    if (source != null) {
                                        // Fetch Vertex for Source ID
                                        DefaultGraphCell vertex =
                                            (DefaultGraphCell) ids.get(source);
                                        if (vertex != null)
                                            // Connect to Source Port
                                            cs.connect(edge, vertex.getChildAt(0), true);
                                    }
                                    // Find Target Port
                                    if (target != null) {
                                        // Fetch Vertex for Target ID
                                        DefaultGraphCell vertex =
                                            (DefaultGraphCell) ids.get(target);
                                        if (vertex != null)
                                            // Connect to Target Port
                                            cs.connect(edge, vertex.getChildAt(0), false);
                                    }

                                    boolean edge_directed = ("true".equals(edge_node.getAttribute("isdirected")) || defaultDirected) 
                                        && !("false".equals(edge_node.getAttribute("isdirected")));
                                    Map map = GraphConstants.createMap();
                                    if (edge_directed) {
                                        GraphConstants.setLineEnd(map, GraphConstants.ARROW_CLASSIC);
                                        GraphConstants.setEndFill(map, true);
                                    }
                                    fetchEdgeViewProperties(edge_node, map);
                                    attributes.put(edge, map);

                                    // Add Edge to new Cells             
                                    newCells.add(edge);
                                } else if (type.equals("view")) { // Graph view attributes
                                    // Currently defined: defaultlayout
                                    defaultLayout = ((Element)node).getAttribute("defaultlayout");
                                }
                            }
                        }
                    }
                }
		// Insert the cells (View stores attributes)
		model.insert(newCells.toArray(), attributes, cs, null, null);
                if (defaultLayout != null) {
                    applyLayout(graph, defaultLayout);
                }
	}


    /**
     * Applies the layout given by name in <code>layout</code>.
     * Searches LayoutRegistry for the layout controller with string representation
     * equals to the <code>layout</code> and performs its algorithm on the imported
     * graph.
     */
    static void applyLayout(JGraph graph, String layout) {
        if (layout == null) {
            return;
        }
        Iterator controllers = LayoutRegistry.registeredLayoutControllers();
        while (controllers.hasNext()) {
            LayoutController controller = (LayoutController)controllers.next();
            if (layout.equals(controller.toString())) {
                LayoutAlgorithm algorithm = controller.getLayoutAlgorithm();
                algorithm.perform(
                                  graph,
                                  true, // apply to all
                                  controller.getConfiguration());
                return;                
            }
        }
    }

	/**
	 * Returns an attributeMap for the specified position and color.
	 */
	public static Map createDefaultAttributes() {
		// Create an AttributeMap
		Map map = GraphConstants.createMap();
		// Set a Black Line Border (the Border-Attribute must be Null!)
		GraphConstants.setBorderColor(map, Color.black);
		// Return the Map
		return map;
	}


	// Fetch Cell Label from Node
	protected static String getLabel(Node node) {
		String lab = null;
		NodeList children = node.getChildNodes();
		for (int j = 0; j < children.getLength(); j++) {
			Node attr = children.item(j);
			if (attr.getNodeName().equals("attr")
				&& attr
					.getAttributes()
					.getNamedItem("name")
					.getNodeValue()
					.equals(
					"Label")) {
				NodeList values = attr.getChildNodes();
				for (int k = 0; k < values.getLength(); k++) {
					if (values.item(k).getNodeName().equals("string")) {
						Node labelNode = values.item(k).getFirstChild();
						if (labelNode != null)
							lab = labelNode.getNodeValue();
					}
				}
			}
		}
		return (lab != null) ? lab : new String("");
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -