1 | package org.expeditee.actions;
|
---|
2 |
|
---|
3 | import java.awt.Color;
|
---|
4 | import java.io.BufferedReader;
|
---|
5 | import java.io.File;
|
---|
6 | import java.io.InputStreamReader;
|
---|
7 |
|
---|
8 | import org.expeditee.agents.WriteTree;
|
---|
9 | import org.expeditee.gui.DisplayIO;
|
---|
10 | import org.expeditee.gui.Frame;
|
---|
11 | import org.expeditee.gui.FrameIO;
|
---|
12 | import org.expeditee.gui.MessageBay;
|
---|
13 | import org.expeditee.io.StreamGobbler;
|
---|
14 |
|
---|
15 |
|
---|
16 | public class IDE {
|
---|
17 | protected static int CompileClassReturnStatus() {
|
---|
18 |
|
---|
19 | int exitVal = 0; // default's to status OK
|
---|
20 |
|
---|
21 | Frame source = DisplayIO.getCurrentFrame();
|
---|
22 | String title = source.getTitleItem().getText();
|
---|
23 | String[] tokens = title.split("\\s+");
|
---|
24 | String className = tokens[tokens.length - 1];
|
---|
25 | String fileName = "expeditee/src/" + className.replaceAll("\\.", "/") + ".java";
|
---|
26 | WriteTree wt = new WriteTree("Java", fileName);
|
---|
27 | if (wt.initialise(source, null)) {
|
---|
28 | wt.run();
|
---|
29 | try {
|
---|
30 | String class_output_dirname = FrameIO.EXPORTS_DIR + "expeditee/bin";
|
---|
31 | File class_output_dir = new File(class_output_dirname);
|
---|
32 |
|
---|
33 | if (!class_output_dir.isDirectory()) {
|
---|
34 | class_output_dir.mkdirs();
|
---|
35 | }
|
---|
36 |
|
---|
37 | String javac_cmd_args[] = new String[] { "javac", "-d", class_output_dirname, FrameIO.EXPORTS_DIR + fileName };
|
---|
38 |
|
---|
39 | Process p = Runtime.getRuntime().exec(javac_cmd_args,null);
|
---|
40 |
|
---|
41 | // monitor error output
|
---|
42 | StreamGobbler errorGobbler = new StreamGobbler("javac errorStream", p.getErrorStream(), StreamGobbler.MessageBayType.error);
|
---|
43 | // monitor standard output
|
---|
44 | StreamGobbler outputGobbler = new StreamGobbler("javac outputStream", p.getInputStream(), StreamGobbler.MessageBayType.display);
|
---|
45 |
|
---|
46 | errorGobbler.start();
|
---|
47 | outputGobbler.start();
|
---|
48 |
|
---|
49 | exitVal = p.waitFor(); // need to wait to ensure the latest generated 'class' file is the one run in RunClass()
|
---|
50 |
|
---|
51 |
|
---|
52 | if (exitVal != 0) {
|
---|
53 | System.err.println("ExitValue for javac compile: " + exitVal);
|
---|
54 | }
|
---|
55 | else {
|
---|
56 | MessageBay.displayMessage("Compiled " + fileName, Color.darkGray);
|
---|
57 | }
|
---|
58 | } catch (Exception e) {
|
---|
59 | e.printStackTrace();
|
---|
60 | MessageBay.errorMessage("Could not compile class!");
|
---|
61 | MessageBay.errorMessage("Is javac on your PATH environment variable?!");
|
---|
62 |
|
---|
63 | }
|
---|
64 | } else {
|
---|
65 | MessageBay.errorMessage("Could not initialise agent!");
|
---|
66 | }
|
---|
67 |
|
---|
68 | return exitVal;
|
---|
69 | }
|
---|
70 |
|
---|
71 | public static void CompileClass() {
|
---|
72 | CompileClassReturnStatus();
|
---|
73 | }
|
---|
74 |
|
---|
75 | public static String getClassName(Frame source) {
|
---|
76 | return source.getTitleItem().getText().trim();
|
---|
77 | //String title = source.getTitle().getTextNoList();
|
---|
78 | //String[] tokens = title.split(" ");
|
---|
79 | //return tokens[tokens.length - 1];
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 |
|
---|
84 | public static void RunClass() {
|
---|
85 | Frame source = DisplayIO.getCurrentFrame();
|
---|
86 | String className = getClassName(source);
|
---|
87 | try {
|
---|
88 | String class_dirname = FrameIO.EXPORTS_DIR + "expeditee/bin";
|
---|
89 | String java_cmd_args[] = new String[] { "java", "-cp", class_dirname, className };
|
---|
90 |
|
---|
91 | Process p = Runtime.getRuntime().exec(java_cmd_args,null);
|
---|
92 |
|
---|
93 | // monitor error output
|
---|
94 | StreamGobbler errorGobbler = new StreamGobbler("java errorStream",p.getErrorStream(), StreamGobbler.MessageBayType.error);
|
---|
95 | // monitor standard output
|
---|
96 | StreamGobbler outputGobbler = new StreamGobbler("java outputStream",p.getInputStream(), StreamGobbler.MessageBayType.display);
|
---|
97 |
|
---|
98 | // Run the two 'gobbling' monitor threads in parallel
|
---|
99 | errorGobbler.start();
|
---|
100 | outputGobbler.start();
|
---|
101 |
|
---|
102 | int exitVal = p.waitFor();
|
---|
103 |
|
---|
104 | if (exitVal !=0) {
|
---|
105 | System.out.println("ExitValue: " + exitVal);
|
---|
106 | }
|
---|
107 |
|
---|
108 | }
|
---|
109 | catch (Exception e) {
|
---|
110 | MessageBay.errorMessage("Could not run class!");
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | public static void CompileAndRunClass() {
|
---|
116 | int compileExitVal = CompileClassReturnStatus();
|
---|
117 | if (compileExitVal==0) {
|
---|
118 | RunClass();
|
---|
119 | }
|
---|
120 | }
|
---|
121 | }
|
---|