1 | /**
|
---|
2 | * IDE.java
|
---|
3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
4 | *
|
---|
5 | * This program is free software: you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 | package org.expeditee.actions;
|
---|
20 |
|
---|
21 | import java.awt.Color;
|
---|
22 | import java.io.BufferedReader;
|
---|
23 | import java.io.File;
|
---|
24 | import java.io.InputStreamReader;
|
---|
25 |
|
---|
26 | import org.expeditee.agents.WriteTree;
|
---|
27 | import org.expeditee.gui.DisplayIO;
|
---|
28 | import org.expeditee.gui.Frame;
|
---|
29 | import org.expeditee.gui.FrameIO;
|
---|
30 | import org.expeditee.gui.MessageBay;
|
---|
31 | import org.expeditee.io.StreamGobbler;
|
---|
32 |
|
---|
33 |
|
---|
34 | public class IDE {
|
---|
35 | protected static int CompileClassReturnStatus() {
|
---|
36 |
|
---|
37 | int exitVal = 0; // default's to status OK
|
---|
38 |
|
---|
39 | Frame source = DisplayIO.getCurrentFrame();
|
---|
40 | String title = source.getTitleItem().getText();
|
---|
41 | String[] tokens = title.split("\\s+");
|
---|
42 | String className = tokens[tokens.length - 1];
|
---|
43 | String fileName = "expeditee/src/" + className.replaceAll("\\.", "/") + ".java";
|
---|
44 | WriteTree wt = new WriteTree("Java", fileName);
|
---|
45 | if (wt.initialise(source, null)) {
|
---|
46 | wt.run();
|
---|
47 | try {
|
---|
48 | String class_output_dirname = FrameIO.EXPORTS_DIR + "expeditee/bin";
|
---|
49 | File class_output_dir = new File(class_output_dirname);
|
---|
50 |
|
---|
51 | if (!class_output_dir.isDirectory()) {
|
---|
52 | class_output_dir.mkdirs();
|
---|
53 | }
|
---|
54 |
|
---|
55 | String javac_cmd_args[] = new String[] { "javac", "-d", class_output_dirname, FrameIO.EXPORTS_DIR + fileName };
|
---|
56 |
|
---|
57 | Process p = Runtime.getRuntime().exec(javac_cmd_args,null);
|
---|
58 |
|
---|
59 | // monitor error output
|
---|
60 | StreamGobbler errorGobbler = new StreamGobbler("javac errorStream", p.getErrorStream(), StreamGobbler.MessageBayType.error);
|
---|
61 | // monitor standard output
|
---|
62 | StreamGobbler outputGobbler = new StreamGobbler("javac outputStream", p.getInputStream(), StreamGobbler.MessageBayType.display);
|
---|
63 |
|
---|
64 | errorGobbler.start();
|
---|
65 | outputGobbler.start();
|
---|
66 |
|
---|
67 | exitVal = p.waitFor(); // need to wait to ensure the latest generated 'class' file is the one run in RunClass()
|
---|
68 |
|
---|
69 |
|
---|
70 | if (exitVal != 0) {
|
---|
71 | System.err.println("ExitValue for javac compile: " + exitVal);
|
---|
72 | }
|
---|
73 | else {
|
---|
74 | MessageBay.displayMessage("Compiled " + fileName, Color.darkGray);
|
---|
75 | }
|
---|
76 | } catch (Exception e) {
|
---|
77 | e.printStackTrace();
|
---|
78 | MessageBay.errorMessage("Could not compile class!");
|
---|
79 | MessageBay.errorMessage("Is javac on your PATH environment variable?!");
|
---|
80 |
|
---|
81 | }
|
---|
82 | } else {
|
---|
83 | MessageBay.errorMessage("Could not initialise agent!");
|
---|
84 | }
|
---|
85 |
|
---|
86 | return exitVal;
|
---|
87 | }
|
---|
88 |
|
---|
89 | public static void CompileClass() {
|
---|
90 | CompileClassReturnStatus();
|
---|
91 | }
|
---|
92 |
|
---|
93 | public static String getClassName(Frame source) {
|
---|
94 | return source.getTitleItem().getText().trim();
|
---|
95 | //String title = source.getTitle().getTextNoList();
|
---|
96 | //String[] tokens = title.split(" ");
|
---|
97 | //return tokens[tokens.length - 1];
|
---|
98 | }
|
---|
99 |
|
---|
100 |
|
---|
101 |
|
---|
102 | public static void RunClass() {
|
---|
103 | Frame source = DisplayIO.getCurrentFrame();
|
---|
104 | String className = getClassName(source);
|
---|
105 | try {
|
---|
106 | String class_dirname = FrameIO.EXPORTS_DIR + "expeditee/bin";
|
---|
107 | String java_cmd_args[] = new String[] { "java", "-cp", class_dirname, className };
|
---|
108 |
|
---|
109 | Process p = Runtime.getRuntime().exec(java_cmd_args,null);
|
---|
110 |
|
---|
111 | // monitor error output
|
---|
112 | StreamGobbler errorGobbler = new StreamGobbler("java errorStream",p.getErrorStream(), StreamGobbler.MessageBayType.error);
|
---|
113 | // monitor standard output
|
---|
114 | StreamGobbler outputGobbler = new StreamGobbler("java outputStream",p.getInputStream(), StreamGobbler.MessageBayType.display);
|
---|
115 |
|
---|
116 | // Run the two 'gobbling' monitor threads in parallel
|
---|
117 | errorGobbler.start();
|
---|
118 | outputGobbler.start();
|
---|
119 |
|
---|
120 | int exitVal = p.waitFor();
|
---|
121 |
|
---|
122 | if (exitVal !=0) {
|
---|
123 | System.out.println("ExitValue: " + exitVal);
|
---|
124 | }
|
---|
125 |
|
---|
126 | }
|
---|
127 | catch (Exception e) {
|
---|
128 | MessageBay.errorMessage("Could not run class!");
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | public static void CompileAndRunClass() {
|
---|
134 | int compileExitVal = CompileClassReturnStatus();
|
---|
135 | if (compileExitVal==0) {
|
---|
136 | RunClass();
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|