summaryrefslogtreecommitdiffstats
path: root/Master/Agile Software Development/TestApp/src/testapp/GraphGenerator.java
blob: 06645d292d261c90b7b51b62baa171ab8111f01f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
 * 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<File,Object> {
    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);
    }
}