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