source: trunk/src/org/expeditee/actions/IDE.java@ 1102

Last change on this file since 1102 was 1102, checked in by davidb, 6 years ago

Reworking of the code-base to separate logic from graphics. This version of Expeditee now supports a JFX graphics as an alternative to SWING

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