stupid dot finder
[eclipsethinslicer.git] / Svelte / src / edu / berkeley / cs / bodik / svelte / plugin / sgview / SGViewActionDelegate.java
blob93cbaee25c0798009932fff2aacc523f883a94e8
1 /*******************************************************************************
2 * This program and the accompanying materials
3 * are made available under the terms of the Eclipse Public License v1.0
4 * which accompanies this distribution, and is available at
5 * http://www.eclipse.org/legal/epl-v10.html.
6 *
7 * This file is a derivative of code released by the University of
8 * California under the terms listed below.
10 * Refinement Analysis Tools is Copyright ©2007 The Regents of the
11 * University of California (Regents). Provided that this notice and
12 * the following two paragraphs are included in any distribution of
13 * Refinement Analysis Tools or its derivative work, Regents agrees
14 * not to assert any of Regents' copyright rights in Refinement
15 * Analysis Tools against recipient for recipient’s reproduction,
16 * preparation of derivative works, public display, public
17 * performance, distribution or sublicensing of Refinement Analysis
18 * Tools and derivative works, in source code and object code form.
19 * This agreement not to assert does not confer, by implication,
20 * estoppel, or otherwise any license or rights in any intellectual
21 * property of Regents, including, but not limited to, any patents
22 * of Regents or Regents’ employees.
24 * IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT,
25 * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
26 * INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE
27 * AND ITS DOCUMENTATION, EVEN IF REGENTS HAS BEEN ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
32 * FOR A PARTICULAR PURPOSE AND FURTHER DISCLAIMS ANY STATUTORY
33 * WARRANTY OF NON-INFRINGEMENT. THE SOFTWARE AND ACCOMPANYING
34 * DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS
35 * IS". REGENTS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
36 * UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
38 package edu.berkeley.cs.bodik.svelte.plugin.sgview;
40 import java.io.BufferedInputStream;
41 import java.io.File;
42 import java.io.FileWriter;
43 import java.io.IOException;
44 import java.util.Arrays;
46 import org.eclipse.jdt.core.IJavaElement;
47 import org.eclipse.jdt.core.IJavaProject;
48 import org.eclipse.jdt.ui.JavaUI;
49 import org.eclipse.jface.action.IAction;
50 import org.eclipse.jface.viewers.ISelection;
51 import org.eclipse.ui.IEditorActionDelegate;
52 import org.eclipse.ui.IEditorPart;
53 import org.eclipse.ui.PartInitException;
54 import org.eclipse.ui.PlatformUI;
56 import edu.berkeley.cs.bodik.svelte.plugin.EclipseJdtUtils;
57 import edu.berkeley.cs.bodik.svelte.plugin.SliceEnvironment;
58 import edu.berkeley.cs.bodik.svelte.plugin.sgview.SliceGraphView.IClickHandler;
60 public class SGViewActionDelegate implements IEditorActionDelegate {
62 private SliceGraph cgs;
63 private IJavaProject proj;
65 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
66 // TODO Auto-generated method stub
67 IJavaElement compunit = JavaUI.getWorkingCopyManager().getWorkingCopy(
68 targetEditor.getEditorInput());
69 proj = compunit.getJavaProject();
73 private static void writeDotFile(String s, File f) throws IOException {
74 FileWriter fw = new FileWriter(f);
75 fw.write(s);
76 fw.close();
79 private static void spawnDot(String dotExe, String pngFile, String cmapxFile, File dotFile)
80 throws IOException {
81 if (dotFile == null) {
82 throw new IllegalArgumentException("dotFile is null");
84 String[] cmdarray = { dotExe, "-Tpng", "-o", pngFile, "-Tcmapx", "-o", cmapxFile, "-v",
85 dotFile.getAbsolutePath() };
86 System.out.println("spawning process " + Arrays.toString(cmdarray));
87 Process p = Runtime.getRuntime().exec(cmdarray);
88 BufferedInputStream output = new BufferedInputStream(p.getInputStream());
89 BufferedInputStream error = new BufferedInputStream(p.getErrorStream());
90 boolean repeat = true;
91 while (repeat) {
92 try {
93 Thread.sleep(500);
94 } catch (InterruptedException e1) {
95 e1.printStackTrace();
96 // just ignore and continue
98 if (output.available() > 0) {
99 byte[] data = new byte[output.available()];
100 int nRead = output.read(data);
101 System.err.println("read " + nRead + " bytes from output stream");
103 if (error.available() > 0) {
104 byte[] data = new byte[error.available()];
105 int nRead = error.read(data);
106 System.err.println("read " + nRead + " bytes from error stream");
108 try {
109 p.exitValue();
110 // if we get here, the process has terminated
111 repeat = false;
112 System.out.println("process terminated with exit code " + p.exitValue());
113 } catch (IllegalThreadStateException e) {
114 // this means the process has not yet terminated.
115 repeat = true;
120 public void run(IAction action) {
121 // make the slice
122 cgs = new SliceGraph(SliceEnvironment.getDepGraph(), SliceEnvironment.getSeed(),
123 SliceEnvironment.getDepth());
124 reload();
127 public void reload() {
128 assert (cgs != null);
129 System.err.println("generateDot start " + System.currentTimeMillis() + "\n");
130 StringBuffer buf = cgs.generateDot();
131 System.err.println("generateDot end " + System.currentTimeMillis() + "\n");
133 // TODO FIXME: how / when to delete?
134 File tmpfile_dot = null, tmpfile_png = null, tmpfile_cmapx = null;
135 try {
136 tmpfile_dot = File.createTempFile("tmp_eclipsethinslicer", ".dot");
137 tmpfile_png = File.createTempFile("tmp_eclipsethinslicer", ".png");
138 tmpfile_cmapx = File.createTempFile("tmp_eclipsethinslicer", ".cmapx");
140 // TODO FIXME: delete it before exit! these PNGS can be big.
141 tmpfile_dot.deleteOnExit();
142 tmpfile_png.deleteOnExit();
143 tmpfile_cmapx.deleteOnExit();
145 } catch (IOException e1) {
146 e1.printStackTrace();
147 return; // TODO FIXME: should really show an error message!
150 // run dot and make a temporary png
151 try {
152 writeDotFile(buf.toString(), tmpfile_dot);
153 System.err.println("file written " + System.currentTimeMillis() + "\n");
154 spawnDot(StupidDotFinder.getDotLocation(), tmpfile_png.getAbsolutePath(), tmpfile_cmapx.getAbsolutePath(), tmpfile_dot);
155 System.out.println("C:\\Program Files\\Graphviz2.18\\bin\\dot.exe" + " -- " + tmpfile_png.getAbsolutePath() + " -- " + tmpfile_cmapx.getAbsolutePath() + " -- " + tmpfile_dot);
156 System.err.println("dot ran" + System.currentTimeMillis() + "\n");
158 } catch (IOException e) {
159 e.printStackTrace();
160 return;
163 // find the cgview and load the png
164 // ResourcesPlugin.getWorkspace()
165 try {
166 SliceGraphView cgviewpart = (SliceGraphView) PlatformUI.getWorkbench()
167 .getActiveWorkbenchWindow().getActivePage().showView(
168 "edu.berkeley.cs.bodik.svelte.plugin.sgview.SliceGraphView");
169 System.err.println("going to load image" + System.currentTimeMillis() + "\n");
171 cgviewpart.setImage(tmpfile_png.getAbsolutePath());
172 System.err.println("image loaded" + System.currentTimeMillis() + "\n");
173 cgviewpart.setImageMap(tmpfile_cmapx.getAbsolutePath());
174 System.err.println("imagemap loaded" + System.currentTimeMillis() + "\n");
176 // set up handling of clicks to do various things
178 cgviewpart.setClickHandler(new IClickHandler() {
180 public void nodeClicked(String node) {
181 EclipseJdtUtils.gotoNode(node, proj);
184 public void nodeLeftClicked(String node) {
185 // cgs.toggleCollapsed(node);
186 // reload();
191 } catch (PartInitException e) {
192 e.printStackTrace();
195 System.err.println("done" + System.currentTimeMillis() + "\n");
199 public void selectionChanged(IAction action, ISelection selection) {
200 // TODO Auto-generated method stub