1 package real;
2
3 import intellego.Intellego;
4 import interfaces.*;
5 import main.*;
6
7 import java.io.*;
8 import java.lang.*;
9
10
16 public class ControllerDL
17 {
18 private File currentFile;
20
21 private String currentFileName;
23
24 private File currentDir;
26
27
33 public ControllerDL(File sourceFile, File dir)
34 {
35 currentFile=sourceFile;
36 currentFileName=currentFile.getName();
37 currentDir=dir;
38
39 currentFile=createFile(currentFileName.substring(0,currentFileName.length() - 5));
40
41 compileFile();
42 downloadFile();
43 }
44
45
51 public File createFile(String className)
52 {
53 File file=new File("real","Real"+className+".java");
55
56
57 try
59 {
60 FileWriter fw=new FileWriter(file);
61 fw.write(
63 "\n// This file was generated by Intellego. Do not modify\n\n"+
64 "class Real"+className+"\n{\n\tpublic static void main(String args[])\n"+
65 "\t{\n\t\t"+className+" a=new "+className+"();"+
66 "\n\t\ta.initController(new real.RealRCX(a));\n\t\ta.run();\n\t}\n}");
67
68 fw.close();
69
70 Intellego.addToLog("ControllerDL.createFile(): Created file: "+file.getName());
71 }
72
73 catch(Exception e)
74 {
75 MainInterface.displayMessage("Error: cannot download controller");
76 Intellego.addToLog("ControllerDL.createFile() failedto create file: "+e);
77 }
78
79 return file;
80 }
81
82
85 private void compileFile()
86 {
87 if(currentFile!=null)
88 {
89 externalCommand("lejosc "+currentFile.toString(),0);
91 }
92 else
93 {
94 MainInterface.displayMessage("ControllerDL.compileFile(): cannot compile null file");
95 }
96 }
97
98
101 private void downloadFile()
102 {
103 if(currentFile!=null)
104 {
105
107 String className=currentFile.getName();
109 className=className.substring(0,className.length() - 5);
110
111 externalCommand(("lejos "+className),1);
113
114 }
115 else
116 {
117 MainInterface.displayMessage("ControllerDL.downloadFile(): cannot download null file");
118 }
119 }
120
121
125 private void externalCommand(String cmd, int num)
126 {
127 int len;
128 byte buffer[] = new byte[1000];
129
130 Intellego.addToLog("ControllerDL.externalCommand(): External Command attempted: "+cmd);
131
132 try
133 {
134 Process external=Runtime.getRuntime().exec(cmd);
135 InputStream ees = external.getErrorStream();
136
137 try
138 {
139 ExternalMessager output=MainInterface.createExternalMessagerFrame(num);
141
142 while ((len = ees.read(buffer)) != -1)
143 {
144 String eo = new String (buffer, 0, len);
145 output.append(eo);
146 }
147 external.waitFor();
148
149 if(num==0){
150 if(output.successfullCompile()){
151
152 output.append("\n"+"\n"+" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
153 output.append("\n"+"\n"+" | COMPILING SUCCESSFUL: NO ERRORS FOUND |");
154 output.append("\n"+"\n"+" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
155 }
156
157 else{
158
159 output.append("\n"+"\n"+" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
160 output.append("\n"+"\n"+" | COMPILING FAILED: ERRORS FOUND |");
161 output.append("\n"+"\n"+" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
162
163 }
164 }
165
166
185
186 }
187 catch (Exception e)
188 {
189 MainInterface.displayMessage("Error attempting external command");
190 Intellego.addToLog("ControllerDL.externalCommand(): error: "+e.getMessage());
191 }
192 }
193 catch (Exception e)
194 {
195 MainInterface.displayMessage("Error attempting external command");
196 Intellego.addToLog("ControllerDL.externalCommand(): error: "+e.getMessage());
197 }
198 }
199 }
200