]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.graphviz/src/org/simantics/graphviz/Graphs.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.graphviz / src / org / simantics / graphviz / Graphs.java
diff --git a/bundles/org.simantics.graphviz/src/org/simantics/graphviz/Graphs.java b/bundles/org.simantics.graphviz/src/org/simantics/graphviz/Graphs.java
new file mode 100644 (file)
index 0000000..3f087a3
--- /dev/null
@@ -0,0 +1,149 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.graphviz;\r
+\r
+import java.io.File;\r
+import java.io.FileInputStream;\r
+import java.io.FileOutputStream;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.io.PrintStream;\r
+import java.util.Arrays;\r
+\r
+import javax.swing.SwingUtilities;\r
+\r
+import org.simantics.graphviz.continuation.Computation;\r
+import org.simantics.graphviz.drawable.GraphDrawable;\r
+import org.simantics.graphviz.drawable.JViewer;\r
+import org.simantics.graphviz.internal.parser.DotParser;\r
+import org.simantics.graphviz.internal.parser.ParseException;\r
+import org.simantics.graphviz.internal.process.CreateXDot;\r
+\r
+/**\r
+ * A class containing only static methods for operating\r
+ * on graphs.\r
+ * \r
+ * @author Hannu Niemist�\r
+ */\r
+public class Graphs {\r
+\r
+    private Graphs() {}\r
+    \r
+    /**\r
+     * Parses a dot graph from an input stream.\r
+     */\r
+    public static Graph parse(InputStream stream) throws IOException {\r
+        try {\r
+            return new DotParser(stream).document();\r
+        } catch(ParseException e) {\r
+               System.out.println(\r
+                               e.currentToken.beginLine + ":" + e.currentToken.beginColumn + " - " +\r
+                               e.currentToken.endLine + ":" + e.currentToken.endColumn);\r
+            throw new IOException(e);\r
+        }\r
+    }\r
+    \r
+    /**\r
+     * Parses a dot graph from a file.\r
+     */\r
+    public static Graph parse(File file) throws IOException {\r
+        InputStream stream = new FileInputStream(file);\r
+        Graph graph = parse(stream);\r
+        stream.close();\r
+        \r
+        return graph;\r
+    }\r
+    \r
+    /**\r
+     * Layouts a graph with a given algorithm and writes it to the file using given output format.\r
+     */\r
+    public static void createImage(Graph graph, String algorithm, String type, File file) throws IOException {\r
+        System.out.println("Create image");\r
+        if(type.equals("dot")) {\r
+            PrintStream stream = new PrintStream(new FileOutputStream(file));\r
+            graph.write(stream);\r
+            stream.close();\r
+        }\r
+        else {  \r
+               File DOT_EXE = Activator.getDotExe();\r
+               Process process = new ProcessBuilder(\r
+                  DOT_EXE.toString(), "-T" + type, "-K" + algorithm, "-o"+ file.getAbsolutePath())\r
+                  .directory(DOT_EXE.getParentFile())\r
+                  .start();\r
+            \r
+            PrintStream stream = new PrintStream(process.getOutputStream());\r
+            \r
+            graph.write(stream);\r
+            stream.close();\r
+           \r
+            InputStream errorStream = process.getErrorStream();\r
+            while(true) {\r
+                int c = errorStream.read();\r
+                if(c <= 0)\r
+                    break;\r
+                System.err.print((char)c);\r
+            }\r
+            errorStream.close();\r
+            \r
+            try {\r
+                process.waitFor();\r
+            } catch (InterruptedException e) {\r
+                // TODO Auto-generated catch block\r
+                e.printStackTrace();\r
+            }\r
+        }\r
+    }\r
+    \r
+    /**\r
+     * Layouts the graph.\r
+     */\r
+    public static Computation<Graph> createXDot(Graph graph, String algorithm) {\r
+        CreateXDot process = new CreateXDot(graph, algorithm);\r
+        process.start();\r
+        return process;\r
+    }\r
+    \r
+    public static byte[] read(InputStream stream) throws IOException {\r
+       byte[] buffer = new byte[1024];\r
+       int pos = 0;\r
+       while(true) {                   \r
+               int len = stream.read(buffer, pos, buffer.length-pos);\r
+               if(len < 0)\r
+                       return Arrays.copyOf(buffer, pos);\r
+               pos += len;\r
+               if(pos == buffer.length) {\r
+                       buffer = Arrays.copyOf(buffer, (int)(buffer.length * 1.5));\r
+               }\r
+       }\r
+    }\r
+\r
+    /**\r
+     * Layouts the graph using given algorithm and visualizes it\r
+     * in a new viewer window.\r
+     */\r
+    public static void show(final Graph graph, final String algorithm) {\r
+        SwingUtilities.invokeLater(new Runnable() {\r
+            public void run() {\r
+                new JViewer(new GraphDrawable(graph, algorithm));\r
+            }\r
+        });        \r
+    }   \r
+    \r
+    /**\r
+     * Layouts the graph using dot algorithm and visualizes it\r
+     * in a new viewer window.\r
+     */\r
+    public static void show(final Graph graph) {\r
+        show(graph, "dot");      \r
+    }   \r
+    \r
+}\r