/* * Class to generate the graphic out of the .dot file */ package testapp; import java.io.File; import java.io.IOException; import java.util.concurrent.ExecutionException; import javax.swing.SwingWorker; import java.util.logging.*; /** * * @author eisenhauer */ public class GraphGenerator extends SwingWorker { int retVal=-1; Process proc; File dotFile = null; public GraphGenerator(File dotFile) { super(); this.dotFile = dotFile; } @Override protected File doInBackground() throws Exception { return getImageFileForGraph(); } @Override protected void done() { try { if (retVal == 0) { AppFrame af = AppFrame.getInstance(); if (af != null) { af.setImage(get()); //TODO //dotFile.delete(); } } return; } catch (InterruptedException ie) { ie.printStackTrace(); } catch (ExecutionException ee) { ee.printStackTrace(); } } //Convert the .dot file to a .png grafic public File getImageFileForGraph(File dotFile) { File tmpFile = null; if (!dotFile.canRead()) { return null; } try { tmpFile = File.createTempFile("tmp", ".png"); File dotPath = new File(App.getInstance().getAppPath() + "/lib/graphviz/bin/"); File dotExe = new File(dotPath.toString()+"/dot.exe"); String dotArgType = "-Tpng"; String dotArgOutfile = "-o"+tmpFile.toString(); String dotArgInfile = dotFile.toString(); String graphvizCmd = dotExe.toString(); String[] cmd = new String[] {graphvizCmd,dotArgType,dotArgOutfile,dotArgInfile}; ProcessBuilder procBuilder = new ProcessBuilder(cmd); procBuilder.redirectErrorStream(true); proc = procBuilder.start(); try { retVal = proc.waitFor(); } catch (InterruptedException ie) { ie.printStackTrace(); } } catch (IOException ioe) { ioe.printStackTrace(); } return tmpFile; } public File getImageFileForGraph() { //File dotFile = new File(System.getProperty("user.dir")+"/dist/resource/test.dot"); return getImageFileForGraph(dotFile); } }