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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.graphviz;
\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
22 import javax.swing.SwingUtilities;
\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
32 * A class containing only static methods for operating
\r
35 * @author Hannu Niemist�
\r
37 public class Graphs {
\r
42 * Parses a dot graph from an input stream.
\r
44 public static Graph parse(InputStream stream) throws IOException {
\r
46 return new DotParser(stream).document();
\r
47 } catch(ParseException e) {
\r
49 e.currentToken.beginLine + ":" + e.currentToken.beginColumn + " - " +
\r
50 e.currentToken.endLine + ":" + e.currentToken.endColumn);
\r
51 throw new IOException(e);
\r
56 * Parses a dot graph from a file.
\r
58 public static Graph parse(File file) throws IOException {
\r
59 InputStream stream = new FileInputStream(file);
\r
60 Graph graph = parse(stream);
\r
67 * Layouts a graph with a given algorithm and writes it to the file using given output format.
\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
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
83 PrintStream stream = new PrintStream(process.getOutputStream());
\r
85 graph.write(stream);
\r
88 InputStream errorStream = process.getErrorStream();
\r
90 int c = errorStream.read();
\r
93 System.err.print((char)c);
\r
95 errorStream.close();
\r
99 } catch (InterruptedException e) {
\r
100 // TODO Auto-generated catch block
\r
101 e.printStackTrace();
\r
107 * Layouts the graph.
\r
109 public static Computation<Graph> createXDot(Graph graph, String algorithm) {
\r
110 CreateXDot process = new CreateXDot(graph, algorithm);
\r
115 public static byte[] read(InputStream stream) throws IOException {
\r
116 byte[] buffer = new byte[1024];
\r
119 int len = stream.read(buffer, pos, buffer.length-pos);
\r
121 return Arrays.copyOf(buffer, pos);
\r
123 if(pos == buffer.length) {
\r
124 buffer = Arrays.copyOf(buffer, (int)(buffer.length * 1.5));
\r
130 * Layouts the graph using given algorithm and visualizes it
\r
131 * in a new viewer window.
\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
142 * Layouts the graph using dot algorithm and visualizes it
\r
143 * in a new viewer window.
\r
145 public static void show(final Graph graph) {
\r
146 show(graph, "dot");
\r