forked from groehner/JavaEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUJavaCommands.pas
More file actions
1155 lines (1089 loc) · 34.8 KB
/
UJavaCommands.pas
File metadata and controls
1155 lines (1089 loc) · 34.8 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit UJavaCommands;
interface
uses
Windows,
Classes,
ComCtrls,
UEditorForm,
UModel,
UComJava1,
UUtils;
const BufSize = $4000; // of ReadBuf
// constants for sub-windows
K_Interpreter = 0; K_Compiler = 1; K_Messages = 4;
type
TJavaCommands = class
private
FReload: Boolean;
FErrFile: string;
FOutputLines: TStringList;
FConsoleMode: Integer;
FSuccessfullCompiled: Boolean;
FManyCompiling: Boolean;
FPipeOutput: TPipeHandles;
FEditForm: TFEditForm;
FManyCompilingErrorOccured: Boolean;
FTerminateProcess: Boolean;
FProcessRunningComJava: TComJava1;
FProcessInformation: TProcessInformation;
FProcessRunning: Boolean;
FCompileFilepath: string;
FCompileFilename: string;
FCompileDirectory: string;
FCompileParameter: string;
FCompilePackage: string;
FCompiler: string;
FCompileList: TStringList;
procedure CompileOneFile;
procedure CompileWithProcess;
procedure CompileInternally;
procedure LogCompile;
function GetCompilerNumber: Integer;
procedure ShowSuccessfull;
procedure ShowErrors;
procedure OutputCompileInfos(CompilerNr: Integer);
public
constructor Create;
destructor Destroy; override;
procedure Compile(const Pathname, Package: string);
procedure CompileForm(Form: TFEditForm);
procedure CompileAll;
function HasValidClass(const Programm: string): Boolean;
function AcceptableError(const Pathname: string): Boolean;
procedure AdjustCarets(StringList: TStringList);
procedure MakeRunJavaBat(const Directory, ApplicationName,
CommandLine: string; WithPause: Boolean = True);
procedure Run(const CallParameter, JavaProgram, Package: string;
Gui, JavaFX: Boolean);
procedure Upload(const JavaProgram: string);
procedure UploadRCX(const JavaProgram: string);
procedure UploadNXT(const JavaProgram: string);
procedure UploadEV3(const JavaProgram: string);
procedure CallEV3(const Command: string);
procedure MindstormsUtility(const Bat: string);
procedure EditBuffer(const Buffer: string);
function ExecAndWait(const ApplicationName, CommandLine, Dir,
Output: string; WindowState: Word;
ProcessMessage: Boolean = True): Boolean;
function ExecWithoutWait(const ApplicationName, CommandLine, Dir: string;
WindowState: Word): Boolean;
procedure ExecWithPipe(const ApplicationName, CommandLine, Dir: string;
JavaFX: Boolean = False);
procedure ExecWithConsoleBat(const Batchfile: string);
function ShellExecuteFile(const FileName, Params, DefaultDir: string;
ShowCmd: Integer): THandle;
procedure Terminate;
procedure AppletViewer(Pathname: string);
procedure SetManyCompiling(OnOff: Boolean);
procedure RunTests(AClass: TClass; const Method: string);
property ConsoleMode: Integer write FConsoleMode;
property SuccessfullCompiled: Boolean read FSuccessfullCompiled;
property EditForm: TFEditForm read FEditForm write FEditForm;
property ManyCompiling: Boolean read FManyCompiling;
property ManyCompilingErrorOccured: Boolean read FManyCompilingErrorOccured;
property TerminateProcess: Boolean read FTerminateProcess
write FTerminateProcess;
property ProcessRunningComJava: TComJava1 write FProcessRunningComJava;
property ProcessRunning: Boolean read FProcessRunning write FProcessRunning;
end;
var MyJavaCommands: TJavaCommands;
implementation
uses
Winapi.Messages,
SysUtils,
Forms,
Controls,
StdCtrls,
StrUtils,
Graphics,
ShellAPI,
IOUtils,
Threading,
JvGnugettext,
UStringRessources,
UJava,
UMessages,
UDlgAbout,
UConfiguration,
UJUnitTest,
UModelEntity;
constructor TJavaCommands.Create;
begin
FOutputLines := TStringList.Create;
FCompileList := TStringList.Create;
FProcessRunning := False;
FProcessRunningComJava := nil;
end;
destructor TJavaCommands.Destroy;
begin
inherited;
FreeAndNil(FOutputLines);
end;
procedure TJavaCommands.OutputCompileInfos(CompilerNr: Integer);
var Compiler: string;
begin
case CompilerNr of
1:
Compiler := ' javac.exe';
2:
Compiler := ' Lejos compiler';
3:
Compiler := ' Intern compiler';
end;
FMessages.OutputLineTo(K_Compiler, Format(_(LNGCompileWith),
[FCompileFilename]) + Compiler);
if FConfiguration.ShowCompilerCall then
FMessages.OutputLineTo(K_Compiler, Compiler + ' ' + FCompileParameter + ' '
+ FCompileFilepath);
if not FManyCompilingErrorOccured then
FMessages.StatusMessage(Compiler);
FMessages.Update;
FMessages.LBCompiler.Update;
end;
procedure TJavaCommands.AdjustCarets(StringList: TStringList);
var Posi, Int1, Int2: Integer; Str, Str1, Str2: string;
begin
FMessages.Canvas.Font.Assign(FMessages.MInterpreter.Font);
for var I := 0 to StringList.Count - 1 do
begin
Str2 := StringList[I];
if Trim(Str2) = '^' then
begin
Posi := Pos('^', Str2);
Str1 := StringList[I - 1];
Delete(Str1, Posi + 1, Length(Str1));
Int1 := FMessages.Canvas.TextWidth(Str1);
while FMessages.Canvas.TextWidth(Str2) < Int1 do
Str2 := ' ' + Str2;
StringList[I] := Str2;
Int1 := I;
Str := StringList[Int1];
while Pos('.java:', Str) = 0 do
begin
Dec(Int1);
Str := StringList[Int1];
end;
Int2 := Pos('.java:', Str) + 7;
while Str[Int2] <> ':' do
Inc(Int2);
Insert(Posi.ToString + ':', Str, Int2 + 1);
StringList[Int1] := Str;
end;
end;
end;
procedure TJavaCommands.LogCompile;
var AFile: TextFile;
begin
if FConfiguration.LogfileCompilerOK then
begin
AssignFile(AFile, FConfiguration.LogfileCompiler);
try
try
Append(AFile);
Writeln(AFile, DateTimeToStr(Now) + ' ' + GetComputerNetName +
' Version: ' + UDlgAbout.Version);
Writeln(AFile, FCompiler + ' ' + FCompileParameter + ' ' +
FCompileFilename);
Writeln(AFile, FCompileList.Text);
except
on E: Exception do
ErrorMsg(E.Message);
end;
finally
CloseFile(AFile);
end;
end;
end;
// the bat method gives better error messages than the ExecAndWait method
// but the bat method starts a cmd.exe which cannot use UNC paths
procedure TJavaCommands.CompileWithProcess;
var BatFile, Call, WinCodepage: string;
begin
FSuccessfullCompiled := False;
WinCodepage := GetACP.ToString; // Codepage of Windows
BatFile := FConfiguration.TempDir + 'CompileJava.bat';
Call := ExtractFileName(FCompiler) + ' ' + FCompileParameter + ' ' +
FCompileFilename;
if IsUNC(GetCurrentDir) then
SetCurrentDir('C:\');
var
CompileJava := TStringList.Create;
try
CompileJava.Add('@ECHO OFF');
CompileJava.Add('REM ' + BatFile);
// compatible to TEncoding.ANSI of .bat file
CompileJava.Add('CHCP ' + WinCodepage + ' >NUL');
if IsUNC(FCompileDirectory) then
begin
CompileJava.Add('PUSHD ' + HideBlanks(FCompileDirectory));
CompileJava.Add(Call);
CompileJava.Add('POPD');
end
else
begin
CompileJava.Add('CD ' + HideBlanks(FCompileDirectory));
CompileJava.Add(ExtractFileDrive(FCompileDirectory));
// No CD in BAT file because of codepage problems! should be solved
SetCurrentDir(FCompileDirectory);
CompileJava.Add(Call);
end;
try
CompileJava.SaveToFile(BatFile, TEncoding.ANSI);
if ExecAndWait(BatFile, '', FCompileDirectory, FErrFile, SW_HIDE) then
begin
FSuccessfullCompiled := (TFile.GetSize(FErrFile) = 0);
if not FSuccessfullCompiled then
FCompileList.LoadFromFile(FErrFile);
end
else
ErrorMsg(SysErrorMessage(GetLastError));
except
on E: Exception do
ErrorMsg(E.Message);
end;
finally
CompileJava.Free;
end;
end;
function TJavaCommands.GetCompilerNumber: Integer;
begin
if FConfiguration.CompileInternally and not FConfiguration.MindstormsMode then
Result := 3
else if FConfiguration.MindstormsMode and
(FConfiguration.MindstormsVersion < 2) then
Result := 2
else
Result := 1;
end;
procedure TJavaCommands.ShowSuccessfull;
begin
FMessages.OutputLineTo(K_Compiler, FCompileFilepath + ' ' +
_(LNGSuccessfullCompiled));
if not FManyCompilingErrorOccured then
FMessages.StatusMessage(FCompileFilepath + ' ' +
_(LNGSuccessfullCompiled), 1);
AdjustCarets(FCompileList);
FMessages.LBCompiler.Items.AddStrings(FCompileList);
end;
procedure TJavaCommands.ShowErrors;
function HasErrorMarker: Boolean;
begin
Result := False;
for var I := 0 to FCompileList.Count - 1 do
if Trim(FCompileList[I]) = '^' then
Exit(True);
end;
begin
AdjustCarets(FCompileList);
FMessages.LBCompiler.Items.AddStrings(FCompileList);
if (Pos('error', FCompileList.Text) + Pos('Error', FCompileList.Text) +
Pos('file not found', FCompileList.Text) = 0) and not HasErrorMarker then
begin
FSuccessfullCompiled := True;
if not FManyCompilingErrorOccured then
FMessages.StatusMessage(FCompileFilepath + ' ' +
LNGSuccessfullCompiled, 1);
end
else
begin
if not FMessages.MyIsVisible then
FMessages.ShowIt;
FMessages.ShowTab(K_Compiler);
FMessages.StatusMessage(FCompileFilepath + ' ' + _('contains errors.'), 2);
FManyCompilingErrorOccured := True;
end;
end;
procedure TJavaCommands.CompileOneFile;
begin
if not FileExists(FCompileFilepath) then
begin
FMessages.StatusMessage(Format(_(LNGFileNotFound), [FCompileFilepath]), 2);
Exit;
end;
FErrFile := TPath.Combine(FConfiguration.TempDir, 'error.txt');
FCompiler := FConfiguration.JavaCompiler;
FCompileDirectory := ExtractFilePath(FCompileFilepath);
FCompileFilename := ExtractFileName(FCompileFilepath);
if FCompilePackage <> '' then
begin
FCompileFilename := ReplaceStr(FCompilePackage, '.', '/') + '/' +
FCompileFilename;
var
CountSlashes := CountChar('/', FCompileFilename);
while CountSlashes > 0 do
begin
FCompileDirectory := Copy(FCompileDirectory, 1,
Length(FCompileDirectory) - 1);
FCompileDirectory := Copy(FCompileDirectory, 1,
LastDelimiter('\', FCompileDirectory));
Dec(CountSlashes);
end;
end;
//Screen.Cursor := crHourGlass;
FCompileList.Clear;
var ComJava := GetComJava;
if Assigned(ComJava) and Assigned(ComJava.ClassList) and
(ComJava.ClassList.IndexOf(ChangeFileExt(FCompileFilename, '')) > -1) then
// TODO check
FReload := True;
var
CompilerNum := GetCompilerNumber;
if CompilerNum = 3 then
begin
OutputCompileInfos(3);
CompileInternally;
if FSuccessfullCompiled then
FCompiler := _('Intern compiler')
else
begin
OutputCompileInfos(1);
CompileWithProcess;
end;
end
else
begin
if CompilerNum = 2 then
begin
FCompiler := FConfiguration.LejosCompiler;
FCompileParameter := ''; // parameters are for Uploader only
OutputCompileInfos(2);
end
else
OutputCompileInfos(1);
CompileWithProcess;
end;
FCompileList.Add('');
if FSuccessfullCompiled then
ShowSuccessfull
else
ShowErrors;
LogCompile;
end;
procedure TJavaCommands.CompileInternally;
begin
var
ComJava := GetComJava;
var
Str := ComJava.ExecuteCommand('compile'#4 +
FConfiguration.JavaCompilerParameter + #4 + FCompileFilepath);
FSuccessfullCompiled := (UUtils.Left(Str, 3) = '+OK');
if FSuccessfullCompiled then
FCompileList.Text := Right(Str, 6);
end;
procedure TJavaCommands.Compile(const Pathname, Package: string);
begin
FMessages.ChangeTab(K_Compiler);
if not FManyCompiling then
FMessages.DeleteTab(K_Compiler);
FReload := False;
var
E := TFEditForm(FJava.GetTDIWindowType(Pathname, '%E%'));
if Assigned(E) then
E.InitShowCompileErrors
else
begin
E := FJava.OpenEditForm(Pathname, True);
if E.Modified then
FJava.DoSave(E, False);
end;
FCompileParameter := FConfiguration.GetCompileParameter(Pathname, Package,
E.Encoding);
FCompileFilepath := Pathname;
FCompilePackage := Package;
CompileOneFile;
if FReload then
GetComJava.JavaReset;
end;
procedure TJavaCommands.CompileForm(Form: TFEditForm);
begin
if Assigned(Form) then
Compile(Form.Pathname, Form.GetPackage);
end;
procedure TJavaCommands.CompileAll;
var FileList: TStringList; EForm: TFEditForm;
begin
FMessages.ChangeTab(K_Compiler);
FMessages.DeleteTab(K_Compiler);
FMessages.StatusMessage('');
SetManyCompiling(True);
FReload := False;
FileList := TStringList.Create;
FileList.Sorted := True;
for var I := 0 to FJava.TDIEditFormCount - 1 do
begin
EForm := FJava.TDIEditFormGet(I);
if EForm.IsJava then
FileList.Add(EForm.Pathname);
end;
for var I := 0 to FileList.Count - 1 do
begin
FEditForm := TFEditForm(FJava.GetTDIWindow(FileList[I]));
if not FEditForm.Visible then
Continue;
FEditForm.InitShowCompileErrors;
FCompileParameter := FConfiguration.GetCompileParameter(FEditForm.Pathname,
FEditForm.GetPackage, FEditForm.Encoding);
FCompileFilepath := FEditForm.Pathname;
FCompilePackage := FEditForm.GetPackage;
CompileOneFile;
FMessages.LBCompiler.Items.Add('');
end;
if FManyCompilingErrorOccured then
FJava.ShowCompileErrors;
SetManyCompiling(False);
if FReload then
GetComJava.JavaReset;
FreeAndNil(FileList);
end;
function TJavaCommands.HasValidClass(const Programm: string): Boolean;
var Age1, Age2: TDateTime; Path: string;
begin
Result := False;
FileAge(Programm, Age1);
Path := GetClass(FConfiguration.GetClassPath, Programm);
if Path <> '' then
begin
FileAge(Path, Age2);
Result := Age1 <= Age2;
end;
end;
function TJavaCommands.AcceptableError(const Pathname: string): Boolean;
begin
var
Str := FMessages.GetCompileError(Pathname);
Result := (Pos('1 error', Str) > 0) and (Pos(': constructor ', Str) > 0);
end;
procedure TJavaCommands.MakeRunJavaBat(const Directory, ApplicationName,
CommandLine: string; WithPause: Boolean = True);
var Path, Codepage, WinCodepage: string;
begin
WinCodepage := GetACP.ToString; // Codepage of Windows
Path := FConfiguration.TempDir + 'RunJava.bat';
Codepage := FConfiguration.Codepage;
var
Posi := Pos(' ', Codepage);
if Posi > 0 then
Codepage := Copy(Codepage, 1, Posi - 1);
if IsUNC(GetCurrentDir) then
SetCurrentDir('C:\');
var
RunJava := TStringList.Create;
try
RunJava.Add('@ECHO OFF');
RunJava.Add('REM ' + Path);
// compatible to TEncoding.ANSI of .bat file
if Codepage = '' then
Codepage := WinCodepage;
RunJava.Add('CHCP ' + Codepage + ' > NUL');
if IsUNC(Directory) then
begin
RunJava.Add('PUSHD ' + HideBlanks(Directory)); // \\Server\Path\
RunJava.Add(HideBlanks(ApplicationName) + AddWithSpace(CommandLine));
RunJava.Add('POPD');
end
else
begin
RunJava.Add('CD ' + HideBlanks(Directory));
RunJava.Add(ExtractFileDrive(Directory));
RunJava.Add(HideBlanks(ApplicationName) + AddWithSpace(CommandLine));
end;
if WithPause then
begin
RunJava.Add('@ECHO ON');
RunJava.Add('PAUSE');
end;
try
RunJava.SaveToFile(Path, TEncoding.ANSI);
except
on E: Exception do
ErrorMsg(Format(_(LNGCanNotCreateFile), [Path, E.Message]));
end;
finally
FreeAndNil(RunJava);
end;
end;
procedure TJavaCommands.Run(const CallParameter, JavaProgram, Package: string;
Gui, JavaFX: Boolean);
var Path, Dir, ApplicationName, CommandLine, Classpath, CodePa,
MyProgram: string; Chars: Integer;
begin
Path := ExpandFileName(JavaProgram);
FMessages.ShowTab(K_Interpreter);
FMessages.DeleteTab(K_Interpreter);
FMessages.OutputLineTo(K_Interpreter, _('Starting') + ' ' + Path);
Classpath := FConfiguration.GetClassPath(JavaProgram, Package);
MyProgram := ChangeFileExt(ExtractFileName(Path), '');
Dir := ExtractFilePath(Path);
if Package <> '' then
begin
MyProgram := Package + '.' + MyProgram;
Chars := CountChar('.', Package) + 1;
while Chars > 0 do
begin
Dir := Copy(Dir, 1, Length(Dir) - 1);
Dir := Copy(Dir, 1, LastDelimiter('\', Dir));
Dec(Chars);
end;
end;
CodePa := '';
ApplicationName := FConfiguration.JavaInterpreter;
CommandLine := CodePa + ' -classpath ' + Classpath +
AddWithSpace(FConfiguration.GetJavaInterpreterParameter(JavaFX)) +
AddWithSpace(MyProgram) + AddWithSpace(CallParameter);
if FConfiguration.ShowInterpreterCall then
FMessages.OutputLineTo(K_Interpreter, ApplicationName +
AddWithSpace(CommandLine));
FMessages.OutputLineTo(K_Interpreter, #13#10);
if FConsoleMode = 0 then
if Gui then
FConsoleMode := 1
else if FConfiguration.UseInterpreterWindowAsConsole then
FConsoleMode := 2
else
begin
MakeRunJavaBat(Dir, ApplicationName, CommandLine);
FConsoleMode := 3;
end;
case FConsoleMode of
1:
TTask.Create(
procedure
begin
ExecWithPipe(ApplicationName, CommandLine, Dir, JavaFX);
// without console for GUI programs
end).Start;
2:
FMessages.Run(Classpath, MyProgram, CallParameter);
// with interpreter window as console
3:
ExecWithConsoleBat(FConfiguration.TempDir + 'RunJava.bat');
// with cmd console
end;
FConsoleMode := 0;
end;
procedure TJavaCommands.Upload(const JavaProgram: string);
begin
case FConfiguration.MindstormsVersion of
0:
UploadRCX(JavaProgram);
1:
UploadNXT(JavaProgram);
2:
begin
FConfiguration.MindstormsRun := True;
UploadEV3(JavaProgram);
end;
end;
end;
procedure TJavaCommands.UploadRCX(const JavaProgram: string);
var Str: string;
begin
var
Filepath := ExpandFileName(JavaProgram);
FMessages.ShowIt;
FMessages.ShowTab(K_Interpreter);
FMessages.DeleteTab(K_Interpreter);
if JavaProgram = 'Lejos-Firmware' then
begin
Str := 'Download Lejos firmware' + #13#10 + '0%' + #13#10;
MakeRunJavaBat(FConfiguration.TempDir, FConfiguration.RCXFlasher,
'', False);
end
else
begin
Str := Format(_(LNGTransferToRCX), [Filepath]) + #13#10 + '0%' + #13#10;
MakeRunJavaBat(FConfiguration.TempDir, FConfiguration.RCXUploader,
ChangeFileExt(ExtractFileName(JavaProgram), ''), False);
end;
FMessages.OutputLineTo(K_Interpreter, Str);
ExecWithPipe('', FConfiguration.TempDir + 'RunJava.bat',
ExtractFilePath(Filepath));
with FMessages.MInterpreter do
begin
if (Pos('100%', Lines.Text) > 0) or
(Pos('Firmware unlocked', Lines.Text) > 0) then
Str := _(LNGSuccessfullTransfered)
else
Str := _(LNGErrorDuringTransferToRCX);
FMessages.OutputLineTo(K_Interpreter, Str);
end;
end;
procedure TJavaCommands.UploadNXT(const JavaProgram: string);
var Path, Line, Parameter: string;
begin
Path := ExpandFileName(JavaProgram);
Parameter := '';
if FConfiguration.MindstormsVerbose then
Parameter := Parameter + ' -v ';
if FConfiguration.MindstormsDebug then
Parameter := Parameter + ' -g ';
if FConfiguration.MindstormsRun then
Parameter := Parameter + ' -r ';
Parameter := Parameter + ' ' + FConfiguration.MindstormsParameter + ' ';
FMessages.ShowIt;
FMessages.ShowTab(K_Interpreter);
FMessages.DeleteTab(K_Interpreter);
Line := Format(FConfiguration.SetRCXNXTEV3(_(LNGTransferToRCX)), [Path]);
FMessages.OutputLineTo(K_Interpreter, Line);
FMessages.OutputLineTo(K_Interpreter, Line);
ExecWithPipe('', HideBlanks(FConfiguration.LejosUploader) + ' ' + Parameter +
ChangeFileExt(ExtractFileName(Path), ''), ExtractFilePath(Path));
with FMessages.MInterpreter do
begin
if Pos('Upload successful', Lines.Text) > 0 then
Line := FConfiguration.SetRCXNXTEV3(_(LNGSuccessfullTransfered))
else
Line := FConfiguration.SetRCXNXTEV3(_(LNGErrorDuringTransferToRCX));
FMessages.OutputLineTo(K_Interpreter, Line);
end;
end;
procedure TJavaCommands.UploadEV3(const JavaProgram: string);
var Path, Parameter: string;
begin
Path := ExpandFileName(JavaProgram);
FJava.DoJarCreateEV3(Path);
if FConfiguration.MindstormsRun then
Parameter := ' -r '
else
Parameter := '';
CallEV3(Parameter + HideBlanks(ChangeFileExt(Path, '.jar')));
end;
procedure TJavaCommands.CallEV3(const Command: string);
begin
var
ApplicationName := FConfiguration.JavaInterpreter;
var
CommandLine := ' -cp ' + HideBlanks(FConfiguration.LejosVerzeichnis +
'\lib\ev3\ev3classes.jar;' + FConfiguration.JavaClasspathUser + ';' +
FConfiguration.JavaClasspathAdmin) + ' JEControl -n ' +
FConfiguration.MindstormsIP + ' ' + Command;
FMessages.OutputLineTo(K_Interpreter, ApplicationName + CommandLine);
ExecWithPipe(ApplicationName, CommandLine, FConfiguration.EditorFolder);
end;
procedure TJavaCommands.MindstormsUtility(const Bat: string);
begin
FMessages.ShowIt;
FMessages.ShowTab(K_Interpreter);
FMessages.DeleteTab(K_Interpreter);
var
Str := FConfiguration.LejosVerzeichnis + '\bin\' + Bat;
FMessages.OutputLineTo(K_Interpreter, Str);
ExecWithoutWait(Str, '', '', SW_HIDE);
end;
procedure TJavaCommands.EditBuffer(const Buffer: string);
var LengthS, LengthSMinus1, Line: Integer; Str, Percent: string;
begin
Str := Buffer;
LengthS := Length(Str);
while (LengthS > 0) and (Str[LengthS] <> '%') do
Dec(LengthS);
LengthSMinus1 := LengthS - 1;
while (LengthSMinus1 > 0) and ('0' <= Str[LengthSMinus1]) and
(Str[LengthSMinus1] <= '9') do
Dec(LengthSMinus1);
Percent := Copy(Str, LengthSMinus1 + 1, LengthS - LengthSMinus1 - 1);
if Percent = '' then
FMessages.OutputLineTo(K_Interpreter, Str)
else
begin
Line := FMessages.MInterpreter.Lines.Count;
FMessages.MInterpreter.Lines[Line - 1] :=
Copy('##################################################', 1,
Round(StrToInt(Percent) / 2)) + ' ' + Percent + '%';
end;
end;
// calls a program, waits until ready and collects the output to a file
function TJavaCommands.ExecAndWait(const ApplicationName, CommandLine, Dir,
Output: string; WindowState: Word;
ProcessMessage: Boolean = True): Boolean;
var StartupInfo: TStartupInfo; SecAttr: TSecurityAttributes;
ErrorHandle: THandle; Cmd, CurrentDir: string; WaitObject: Word;
begin
if FProcessRunning then
begin
FTerminateProcess := True;
Exit(False);
end;
FJava.RunButtonToStop(True);
FProcessRunning := True;
FTerminateProcess := False;
SecAttr.nLength := SizeOf(SecAttr);
SecAttr.lpSecurityDescriptor := nil;
SecAttr.bInheritHandle := True;
if Output <> '' then
begin
DeleteFile(PChar(Output));
ErrorHandle := CreateFile(PChar(Output), GENERIC_WRITE, FILE_SHARE_READ,
@SecAttr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if ErrorHandle = INVALID_HANDLE_VALUE then
begin
ErrorMsg('DEBUG ExecAndWait: ' + SysErrorMessage(GetLastError));
ErrorHandle := 0;
end;
end
else
ErrorHandle := 0;
FillChar(FProcessInformation, SizeOf(FProcessInformation), #0);
FillChar(StartupInfo, SizeOf(StartupInfo), #0);
with StartupInfo do
begin
cb := SizeOf(StartupInfo);
wShowWindow := WindowState;
if ErrorHandle > 0 then
begin
dwFlags := STARTF_USESHOWWINDOW + STARTF_USESTDHANDLES;
hStdInput := GetStdHandle(STD_INPUT_HANDLE);
hStdOutput := ErrorHandle;
hStdError := ErrorHandle;
end
else
dwFlags := STARTF_USESHOWWINDOW;
end;
Cmd := Trim(ApplicationName + ' ' + CommandLine);
CurrentDir := Dir;
if (CurrentDir = '') or not SysUtils.DirectoryExists(CurrentDir) then
if ApplicationName <> '' then
CurrentDir := ExtractFilePath(ApplicationName)
else
CurrentDir := ExtractFilePath(CommandLine);
if IsUNC(CurrentDir) then
CurrentDir := '.';
try
Result := CreateProcess(nil, PChar(Cmd), nil, nil, True,
CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil, PChar(CurrentDir),
StartupInfo, FProcessInformation);
if Result then
repeat
WaitObject := WaitForSingleObject(FProcessInformation.hProcess, 20);
until (WaitObject = WAIT_OBJECT_0) or FTerminateProcess;
finally
TerminateTask(FProcessInformation);
if ErrorHandle > 0 then
CloseHandle(ErrorHandle);
FProcessRunning := False;
FJava.RunButtonToStop(False);
end;
if FConfiguration.RunsUnderWine and ProcessMessage then
Application.ProcessMessages; // else wine produces IO-Error
// during TFJava.FormCreate/TFConfiguration.Init/getJavaVersion
// a ProcessMessages starts SystemExecuteMacro/TFJava.Open to early
{ Restarts Compile as long as mouse is over compile icon
// prevent MyTabBar movement
TThread.ForceQueue(nil, procedure
begin
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
end);
}
end;
// runs an external program, no need to wait for or close external program
function TJavaCommands.ExecWithoutWait(const ApplicationName, CommandLine,
Dir: string; WindowState: Word): Boolean;
var StartupInfo: TStartupInfo; LocalProcessInformation: TProcessInformation;
CmdLine, CurrentDir: string;
begin
// doesn't start chm-files
FillChar(LocalProcessInformation, SizeOf(LocalProcessInformation), #0);
FillChar(StartupInfo, SizeOf(StartupInfo), #0);
with StartupInfo do
begin
cb := SizeOf(StartupInfo);
dwFlags := STARTF_USESHOWWINDOW;
wShowWindow := WindowState;
end;
CmdLine := ApplicationName + ' ' + CommandLine; // HideBlanks
CurrentDir := Dir;
if (CurrentDir = '') or not SysUtils.DirectoryExists(CurrentDir) then
if ApplicationName <> '' then
CurrentDir := ExtractFilePath(ApplicationName)
else
CurrentDir := ExtractFilePath(CommandLine);
try
Result := CreateProcess(nil, PChar(CmdLine), nil, nil, True,
CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil, PChar(CurrentDir),
StartupInfo, LocalProcessInformation);
if not Result then
ErrorMsg(SysErrorMessage(GetLastError));
finally
CloseProcessinformationHandle(LocalProcessInformation);
end;
end;
procedure TJavaCommands.ExecWithPipe(const ApplicationName, CommandLine,
Dir: string; JavaFX: Boolean = False);
var SecAttr: TSecurityAttributes; StartupInfo: TStartupInfo; DWExitCode: DWORD;
UBuffer: RawByteString; Buffer: string; Read: Cardinal;
AndroidList: TStringList; HWnd: THandle; Max: Integer; CreateOK: Boolean;
CurrentDir, Cmd: string;
begin
if FProcessRunning then
begin
FTerminateProcess := True;
Exit;
end;
FOutputLines.Clear;
SecAttr.nLength := SizeOf(SecAttr);
SecAttr.lpSecurityDescriptor := nil;
SecAttr.bInheritHandle := True;
with FPipeOutput do
if not CreatePipe(hRead, hWrite, @SecAttr, BufSize) then
begin
ErrorMsg('Error on STDOUT pipe creation: ' +
SysErrorMessage(GetLastError));
Exit;
end;
TThread.Queue(nil,
procedure
begin
FJava.RunButtonToStop(True);
end);
FProcessRunning := True;
FTerminateProcess := False;
FillChar(FProcessInformation, SizeOf(FProcessInformation), #0);
FillChar(StartupInfo, SizeOf(StartupInfo), #0);
with StartupInfo do
begin
cb := SizeOf(StartupInfo);
wShowWindow := SW_HIDE;
dwFlags := STARTF_USESHOWWINDOW + STARTF_USESTDHANDLES;
hStdInput := GetStdHandle(STD_INPUT_HANDLE);
hStdOutput := FPipeOutput.hWrite;
hStdError := FPipeOutput.hWrite;
end;
if ApplicationName = '' then
Cmd := CommandLine
else
Cmd := ApplicationName + ' ' + CommandLine;
CurrentDir := Dir;
if (CurrentDir = '') or not DirectoryExists(CurrentDir) then
if ApplicationName <> '' then
CurrentDir := ExtractFilePath(ApplicationName)
else
CurrentDir := ExtractFilePath(CommandLine);
try
try
CreateOK := CreateProcess(nil, // lpApplicationName
PChar(Cmd), // lpCommandLine
nil, // lpProcessAttributess
nil, // lpThreadAttributess
True, // bInheritHandles
// dwCreationFlags
CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS or CREATE_UNICODE_ENVIRONMENT,
nil, // pEnvironment
PChar(CurrentDir), // pCurrentDirectory
StartupInfo, // lpStartupInfo,
FProcessInformation); // lpProcessInformation
if CreateOK then
begin
WaitForInputIdle(FProcessInformation.hProcess, MaxInt);
if JavaFX then
begin
Max := 0;
repeat
Sleep(100);
HWnd := FindWindow('GlassWndClass-GlassWindowClass-2', nil);
Inc(Max);
until (Max = 100) or (HWnd <> 0);
ShowWindow(HWnd, SW_SHOW);
end;
repeat
GetExitCodeProcess(FProcessInformation.hProcess, DWExitCode);
Application.ProcessMessages;
Sleep(20);
if PeekNamedPipe(FPipeOutput.hRead, nil, 0, nil, @Read, nil) and
(Read > 0) then
begin
SetLength(UBuffer, Read);
ReadFile(FPipeOutput.hRead, UBuffer[1], Read, Read, nil);
Buffer := string(AnsiToUtf8(string(UBuffer)));
if FConfiguration.MindstormsMode and
(FConfiguration.MindstormsVersion = 0) then
EditBuffer(Buffer)
else if FConfiguration.AndroidMode then
begin
AndroidList := TStringList.Create;
AndroidList.Text := Buffer;
AdjustCarets(AndroidList);
FMessages.ChangeTab(K_Compiler);
FMessages.OutputTo(K_Compiler, AndroidList);
FreeAndNil(AndroidList);
end
else
TThread.Queue(nil,
procedure
begin
FMessages.OutputToTerminal(Buffer);
end);
end;
until (DWExitCode <> STILL_ACTIVE) or FTerminateProcess;
end
else
ErrorMsg(Format(_(LNGUnabledToExecute), [Cmd]) + ' ' +
SysErrorMessage(GetLastError));
except
on E: Exception do
ErrorMsg(E.Message);
end;
finally
TerminateTask(FProcessInformation);
ClosePipeHandles(FPipeOutput);
FProcessRunning := False;
TThread.Queue(nil,
procedure
begin
FJava.RunButtonToStop(False);
end);
end;
end;
procedure TJavaCommands.ExecWithConsoleBat(const Batchfile: string);
var StartupInfo: TStartupInfo; DWExitCode: DWORD;
begin
if FProcessRunning then
begin
FTerminateProcess := True;
Exit;
end;