]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.graphviz;\r
13 \r
14 import java.io.File;\r
15 import java.io.FileInputStream;\r
16 import java.io.FileOutputStream;\r
17 import java.io.IOException;\r
18 import java.io.InputStream;\r
19 import java.io.PrintStream;\r
20 import java.util.Arrays;\r
21 \r
22 import javax.swing.SwingUtilities;\r
23 \r
24 import org.simantics.graphviz.continuation.Computation;\r
25 import org.simantics.graphviz.drawable.GraphDrawable;\r
26 import org.simantics.graphviz.drawable.JViewer;\r
27 import org.simantics.graphviz.internal.parser.DotParser;\r
28 import org.simantics.graphviz.internal.parser.ParseException;\r
29 import org.simantics.graphviz.internal.process.CreateXDot;\r
30 \r
31 /**\r
32  * A class containing only static methods for operating\r
33  * on graphs.\r
34  * \r
35  * @author Hannu Niemist�\r
36  */\r
37 public class Graphs {\r
38 \r
39     private Graphs() {}\r
40     \r
41     /**\r
42      * Parses a dot graph from an input stream.\r
43      */\r
44     public static Graph parse(InputStream stream) throws IOException {\r
45         try {\r
46             return new DotParser(stream).document();\r
47         } catch(ParseException e) {\r
48                 System.out.println(\r
49                                 e.currentToken.beginLine + ":" + e.currentToken.beginColumn + " - " +\r
50                                 e.currentToken.endLine + ":" + e.currentToken.endColumn);\r
51             throw new IOException(e);\r
52         }\r
53     }\r
54     \r
55     /**\r
56      * Parses a dot graph from a file.\r
57      */\r
58     public static Graph parse(File file) throws IOException {\r
59         InputStream stream = new FileInputStream(file);\r
60         Graph graph = parse(stream);\r
61         stream.close();\r
62         \r
63         return graph;\r
64     }\r
65     \r
66     /**\r
67      * Layouts a graph with a given algorithm and writes it to the file using given output format.\r
68      */\r
69     public static void createImage(Graph graph, String algorithm, String type, File file) throws IOException {\r
70         System.out.println("Create image");\r
71         if(type.equals("dot")) {\r
72             PrintStream stream = new PrintStream(new FileOutputStream(file));\r
73             graph.write(stream);\r
74             stream.close();\r
75         }\r
76         else {  \r
77                 File DOT_EXE = Activator.getDotExe();\r
78                 Process process = new ProcessBuilder(\r
79                   DOT_EXE.toString(), "-T" + type, "-K" + algorithm, "-o"+ file.getAbsolutePath())\r
80                   .directory(DOT_EXE.getParentFile())\r
81                   .start();\r
82             \r
83             PrintStream stream = new PrintStream(process.getOutputStream());\r
84             \r
85             graph.write(stream);\r
86             stream.close();\r
87            \r
88             InputStream errorStream = process.getErrorStream();\r
89             while(true) {\r
90                 int c = errorStream.read();\r
91                 if(c <= 0)\r
92                     break;\r
93                 System.err.print((char)c);\r
94             }\r
95             errorStream.close();\r
96             \r
97             try {\r
98                 process.waitFor();\r
99             } catch (InterruptedException e) {\r
100                 // TODO Auto-generated catch block\r
101                 e.printStackTrace();\r
102             }\r
103         }\r
104     }\r
105     \r
106     /**\r
107      * Layouts the graph.\r
108      */\r
109     public static Computation<Graph> createXDot(Graph graph, String algorithm) {\r
110         CreateXDot process = new CreateXDot(graph, algorithm);\r
111         process.start();\r
112         return process;\r
113     }\r
114     \r
115     public static byte[] read(InputStream stream) throws IOException {\r
116         byte[] buffer = new byte[1024];\r
117         int pos = 0;\r
118         while(true) {                   \r
119                 int len = stream.read(buffer, pos, buffer.length-pos);\r
120                 if(len < 0)\r
121                         return Arrays.copyOf(buffer, pos);\r
122                 pos += len;\r
123                 if(pos == buffer.length) {\r
124                         buffer = Arrays.copyOf(buffer, (int)(buffer.length * 1.5));\r
125                 }\r
126         }\r
127     }\r
128 \r
129     /**\r
130      * Layouts the graph using given algorithm and visualizes it\r
131      * in a new viewer window.\r
132      */\r
133     public static void show(final Graph graph, final String algorithm) {\r
134         SwingUtilities.invokeLater(new Runnable() {\r
135             public void run() {\r
136                 new JViewer(new GraphDrawable(graph, algorithm));\r
137             }\r
138         });        \r
139     }   \r
140     \r
141     /**\r
142      * Layouts the graph using dot algorithm and visualizes it\r
143      * in a new viewer window.\r
144      */\r
145     public static void show(final Graph graph) {\r
146         show(graph, "dot");      \r
147     }   \r
148     \r
149 }\r