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

📄 fileimportgxl.java

📁 用JGraph编的软件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * @(#)FileExportGXL.java	1.2 01.02.2003
 *
 * Copyright (C) 2003 sven.luzar
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */
package org.jgraph.pad.actions;

import java.awt.Color;
import java.awt.FileDialog;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.jgraph.GPGraphpad;
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.GraphConstants;
import org.jgraph.graph.GraphModel;
import org.jgraph.layout.LayoutAlgorithm;
import org.jgraph.layout.LayoutController;
import org.jgraph.layout.LayoutRegistry;
import org.jgraph.pad.GPGraph;
import org.jgraph.pad.GPUserObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * 
 * @author sven.luzar
 * @version 1.0
 *
 */
public class FileImportGXL extends AbstractActionDefault {

	/**
	 * Constructor for FileExportGXL.
	 * @param graphpad
	 */
	public FileImportGXL(GPGraphpad graphpad) {
		super(graphpad);
	}

	/**
	 * @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
	 */
	public void actionPerformed(ActionEvent e) {
		FileDialog f = new FileDialog(graphpad.getFrame(), "GXL File", FileDialog.LOAD);
		f.show();
		if (f.getFile() == null)
			return;

		try {
			String file = f.getDirectory() + f.getFile();
			GPGraph graph = getCurrentGraph();
			parseGXLFileInto(file, graph);
		} catch (Exception ex) {
			graphpad.error(ex.toString());
		}
	}

	//
	//
	// IMPORT GXL
	//
	//

    /**
     * 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);
    }
	
	public static void parseGXLFileInto(
		String name,
		GPGraph graph) throws Exception {
            String defaultLayout = null;
		GraphModel model = graph.getModel();
		File f = new File(name);
		// 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
                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();

⌨️ 快捷键说明

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