Skip to content

Commit 91dd664

Browse files
authored
Merge pull request #141 from pwgit-create/develop
Develop
2 parents a5aa663 + 3a4e8af commit 91dd664

17 files changed

Lines changed: 179 additions & 341 deletions

File tree

AppWish/AppWish/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<modelVersion>4.0.0</modelVersion>
66
<groupId>pn.dev</groupId>
77
<artifactId>code-generator-gui-ollama</artifactId>
8-
<version>1.7.2</version>
8+
<version>1.7.3</version>
99
<name>CodeGenerator-GUI</name>
1010

1111

@@ -45,7 +45,7 @@
4545
<dependency>
4646
<groupId>pn.dev</groupId>
4747
<artifactId>code-generator-ollama</artifactId>
48-
<version>1.7.2</version>
48+
<version>1.7.3</version>
4949
</dependency>
5050
<dependency>
5151
<groupId>org.openjfx</groupId>

AppWish/AppWish/src/main/java/pn/app_wish/AppWish.java

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import javafx.event.ActionEvent;
66
import javafx.fxml.FXML;
77
import javafx.fxml.FXMLLoader;
8+
import javafx.scene.image.ImageView;
89
import javafx.stage.FileChooser;
910

1011
import javafx.scene.Parent;
@@ -18,11 +19,14 @@
1819

1920
import java.io.File;
2021

22+
import org.slf4j.Logger;
23+
import org.slf4j.LoggerFactory;
2124
import org.slf4j.simple.SimpleLogger;
2225

2326
import pn.app_wish.constant.CodeEvent;
2427
import pn.app_wish.constant.GUIConstants;
2528
import pn.app_wish.constant.StaticAppWishConstants;
29+
import pn.app_wish.util.AppWishUtil;
2630
import pn.cg.app_system.AppSystem;
2731
import pn.cg.app_system.code_generation.model.CompilationJob;
2832
import pn.cg.datastorage.DataStorage;
@@ -43,6 +47,7 @@
4347

4448

4549
public class AppWish extends Application {
50+
private static final Logger log = LoggerFactory.getLogger(AppWish.class);
4651
private static Stage mainStage;
4752
@FXML
4853
public TextField tf_input;
@@ -59,7 +64,9 @@ public class AppWish extends Application {
5964
@FXML
6065
public BorderPane bp_main;
6166
@FXML
62-
public Button btnStopGeneratedApp;
67+
public Button btn_StopGeneratedApp;
68+
@FXML
69+
public ImageView logo;
6370

6471
private String javaExecutablePath;
6572
private Process executingJavaAppProcess;
@@ -98,9 +105,8 @@ public void start(Stage primaryStage) throws IOException {
98105
System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
99106
}
100107

101-
102108
/**
103-
* Appwish GUI lifecycle
109+
* AppWish GUI lifecycle
104110
*/
105111
private void onAppWish(CodeEvent codeEvent) {
106112
isCodeGenerationOnGoing = true;
@@ -120,7 +126,6 @@ private void onAppWish(CodeEvent codeEvent) {
120126
}
121127
contentOfExistingJavaFile = readTextByLinesFromFile(file);
122128
}
123-
124129
//**codeEvent == CodeEvent.CREATE_APPLICATION**//
125130
else {
126131
contentOfExistingJavaFile = null;
@@ -146,29 +151,18 @@ private void onAppWish(CodeEvent codeEvent) {
146151

147152
});
148153

149-
//Wait for 1.5 sec
150-
151-
try {
152-
Thread.sleep(1500);
153-
} catch (InterruptedException e) {
154-
155-
Thread.currentThread().interrupt();
156-
}
157154
}
158155

159-
160156
@FXML
161157
private void onRunJavaApp(ActionEvent ae) {
162158
btn_run_application.setVisible(false);
163-
btnStopGeneratedApp.setVisible(true);
159+
btn_StopGeneratedApp.setVisible(true);
164160

165161
if (javaExecutablePath != null) {
166162
System.out.println("Executing java app on path -> " + javaExecutablePath);
167163
try {
168164
ProcessBuilder pb;
169-
170165
pb = new ProcessBuilder(StaticAppWishConstants.BASH_PATH, StaticAppWishConstants.C_ARGUMENT, StaticAppWishConstants.JAVA_TEXT + javaExecutablePath);
171-
172166
executingJavaAppProcess = pb.inheritIO().start();
173167
} catch (IOException e) {
174168
throw new RuntimeException(e);
@@ -191,7 +185,9 @@ private void stopExecutedGeneratedJavaApp(ActionEvent ae) {
191185

192186
this.executingJavaAppProcess.toHandle().destroy();
193187
btn_run_application.setVisible(true);
194-
btnStopGeneratedApp.setVisible(false);
188+
btn_StopGeneratedApp.setVisible(false);
189+
setButtonGroupVisibilityForCodeGenerationButtons(true);
190+
195191
}
196192

197193
/**
@@ -201,7 +197,7 @@ private void stopExecutedGeneratedJavaApp(ActionEvent ae) {
201197
private void startGuiThread(CodeEvent codeEvent) {
202198
DataStorage.getInstance().setCompilationJob(new CompilationJob(GUIConstants.DEFAULT_STAGE_TITLE));
203199
Platform.runLater(() -> {
204-
setButtonGroupVisibilityForCodeGenerationBtns(false);
200+
setButtonGroupVisibilityForCodeGenerationButtons(false);
205201
output_label.setVisible(true);
206202

207203
switch (codeEvent) {
@@ -213,8 +209,8 @@ private void startGuiThread(CodeEvent codeEvent) {
213209
break;
214210
}
215211

216-
setButtonGroupVisibilityForCodeGenerationBtns(false);
217-
btnStopGeneratedApp.setVisible(false);
212+
setButtonGroupVisibilityForCodeGenerationButtons(false);
213+
btn_StopGeneratedApp.setVisible(false);
218214
});
219215
}
220216

@@ -224,9 +220,10 @@ private void startGuiThread(CodeEvent codeEvent) {
224220
@FXML
225221
private void createApplication(ActionEvent ae) {
226222

227-
if (!isCodeGenerationOnGoing)
223+
if (!isCodeGenerationOnGoing) {
224+
setButtonGroupVisibilityToFalseForStartAndStopApplicationsButtons();
228225
onAppWish(CodeEvent.CREATE_APPLICATION);
229-
226+
}
230227
}
231228

232229
/**
@@ -235,9 +232,10 @@ private void createApplication(ActionEvent ae) {
235232
@FXML
236233
private void continueOnExistingApplication(ActionEvent ae) {
237234

238-
if (!isCodeGenerationOnGoing)
235+
if (!isCodeGenerationOnGoing) {
236+
setButtonGroupVisibilityToFalseForStartAndStopApplicationsButtons();
239237
onAppWish(CodeEvent.CONTINUE_ON_EXISTING_APPLICATION);
240-
238+
}
241239
}
242240

243241

@@ -247,6 +245,7 @@ private void continueOnExistingApplication(ActionEvent ae) {
247245
private void startCodeGeneration(CodeEvent codeEvent, String pathToJavaApp, List<String> contentOfExistingJavaFile) {
248246

249247
if (tf_input != null) {
248+
// Stop displaying start/stop application buttons
250249
// Make a recursive call to AppSystem
251250
switch (codeEvent) {
252251
case CREATE_APPLICATION:
@@ -257,7 +256,6 @@ private void startCodeGeneration(CodeEvent codeEvent, String pathToJavaApp, List
257256
requireNonNull(contentOfExistingJavaFile));
258257
break;
259258
}
260-
261259
}
262260
}
263261

@@ -271,55 +269,57 @@ private void waitForCompilationResult() {
271269
}
272270

273271
/**
274-
* If a compilation result exist , check if the singleton in code-generator-ollama contains a path for a executable Java file
272+
* If a compilation result exist , check if the singleton in code-generator-ollama contains a path for an executable Java file
275273
* If the above is true , activate the "run application" button and remove the "generating code..." text
276274
*/
277275
private void handleCompilationResult() {
278-
279276
if (DataStorage.getInstance().getCompilationJob().isResult()) {
280277
javaExecutablePath = DataStorage.getInstance().getJavaExecutionPath();
281278
// Draw success or error texts, and show run app button
282279
Platform.runLater(() -> {
283280
if (DataStorage.getInstance().getJavaExecutionPath() != null) {
284-
output_label.setText("Completed Successfully");
285-
try {
286-
Thread.sleep(2500);
287-
} catch (InterruptedException e) {
288-
Thread.currentThread().interrupt();
289-
}
290281
output_label.setVisible(false);
291282
btn_run_application.setVisible(true);
292-
setButtonGroupVisibilityForCodeGenerationBtns(true);
283+
setButtonGroupVisibilityForCodeGenerationButtons(true);
293284
isCodeGenerationOnGoing = false;
294285
} else {
295-
output_label.setText("Error");
296-
setButtonGroupVisibilityForCodeGenerationBtns(true);
297-
isCodeGenerationOnGoing = false;
286+
output_label.setText("Something went wrong :(");
298287
}
299288
});
300-
}
301289

290+
}
302291
}
303292

304293
/**
305-
* A button group broken out for reduce of redundancy
294+
* Set the visibility for the create and continue application buttons
306295
*/
307-
private void setButtonGroupVisibilityForCodeGenerationBtns(boolean isVisible) {
296+
private void setButtonGroupVisibilityForCodeGenerationButtons(boolean isVisible) {
308297
btn_create_application.setVisible(isVisible);
309298
btn_continue_on_application.setVisible(isVisible);
310299
}
311300

312-
private File showOpenFileDialog() {
301+
/**
302+
* Set the visibility into false, for the start and stop application buttons (in the main gui)
303+
*/
304+
private void setButtonGroupVisibilityToFalseForStartAndStopApplicationsButtons() {
305+
btn_run_application.setVisible(false);
306+
btn_StopGeneratedApp.setVisible(false);
307+
}
313308

309+
310+
private File showOpenFileDialog() {
314311
try {
315312
FileChooser fileChooser = new FileChooser();
316313
fileChooser.setTitle("Choose A Java file to add functionality to");
317314
fileChooser.setInitialDirectory(new File(PathConstants.RESOURCE_PATH
318315
+ StaticAppWishConstants.FOLDER_NAME_OF_GENERATED_JAVA_APPLICATIONS + File.separator));
319-
316+
// Create an extension filter that filters out .class files
317+
FileChooser.ExtensionFilter extensionFilter = new FileChooser.ExtensionFilter(StaticAppWishConstants.CONTINUE_ON_APPLICATION_FILTER_ON_JAVA_EXTENSION_DESCRIPTION
318+
, StaticAppWishConstants.CONTINUE_ON_APPLICATION_FILTER_ON_JAVA_EXTENSION);
319+
fileChooser.getExtensionFilters().add(extensionFilter);
320320
return fileChooser.showOpenDialog(getMainStage());
321321
} catch (Exception e) {
322-
e.printStackTrace();
322+
log.error("Could not open the window for the file chooser");
323323
return null;
324324
}
325325
}
@@ -328,7 +328,7 @@ private List<String> readTextByLinesFromFile(File file) {
328328
try {
329329
return Files.readAllLines(Paths.get(file.getAbsolutePath()));
330330
} catch (Exception e) {
331-
e.printStackTrace();
331+
log.error("Could not read the lines of the specified file");
332332
return null;
333333
}
334334
}

AppWish/AppWish/src/main/java/pn/app_wish/constant/StaticAppWishConstants.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ public record StaticAppWishConstants() {
44
public static final String BASH_PATH="/bin/bash";
55
public static final String C_ARGUMENT = "-c";
66
public final static String JAVA_TEXT="java ";
7-
7+
public final static String CONTINUE_ON_APPLICATION_FILTER_ON_JAVA_EXTENSION="*.java";
8+
public final static String CONTINUE_ON_APPLICATION_FILTER_ON_JAVA_EXTENSION_DESCRIPTION="A extension filter with purpose to only show .java files";
89
public final static String FOLDER_NAME_OF_GENERATED_JAVA_APPLICATIONS="java_source_code_classes_tmp";
910
}

AppWish/AppWish/src/main/java/pn/app_wish/controller/AppHistoryController.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,10 @@ public class AppHistoryController implements Initializable {
4848
private Process executingJavaAppProcess;
4949

5050

51-
52-
private final String R1 = ".class";
53-
private final String R2 = ".java";
54-
55-
5651
@Override
5752
public void initialize(URL url, ResourceBundle resourceBundle) {
58-
AnchorPane.setRightAnchor(btnMainScene,0d);
59-
AnchorPane.setRightAnchor(btnStopApp,80d);
53+
AnchorPane.setRightAnchor(btnMainScene, 0d);
54+
AnchorPane.setRightAnchor(btnStopApp, 80d);
6055
btnStopApp.setVisible(false);
6156
try {
6257
listHistoryApplications();
@@ -70,26 +65,30 @@ private void handleSelectButtonAction(ActionEvent ae) {
7065

7166
btnStopApp.setVisible(true);
7267
File selectedFile = fileListView.getSelectionModel().selectedItemProperty().getValue();
73-
68+
7469
try {
7570
ProcessBuilder processBuilder;
7671

77-
String executePath = selectedFile.getAbsolutePath().replace(R1,R2);
78-
79-
processBuilder = new ProcessBuilder(BASH_PATH, C_ARGUMENT,JAVA_TEXT + executePath);
80-
executingJavaAppProcess = processBuilder.inheritIO().start();
72+
final String r1 = ".class";
73+
final String r2 = ".java";
74+
final String executePath = selectedFile.getAbsolutePath().replace(r1, r2);
8175

82-
}
83-
catch (IOException e) {
76+
processBuilder = new ProcessBuilder(BASH_PATH, C_ARGUMENT, JAVA_TEXT + executePath);
77+
executingJavaAppProcess = processBuilder.inheritIO().start();
78+
} catch (IOException e) {
8479
System.out.println("RuntimeException while starting Java executable");
8580
throw new RuntimeException(e);
8681
}
8782
}
8883

8984

90-
9185
private void listHistoryApplications() throws IOException {
92-
File selectedDirectory = new File(PathConstants.RESOURCE_PATH + "java_source_code_classes_tmp"+File.separator);
86+
File selectedDirectory = new File(PathConstants.RESOURCE_PATH + "java_source_code_classes_tmp" + File.separator);
87+
88+
// Removes duplicate files from the generated java apps folder
89+
AppWishUtil.removeDuplicateFilesWithAnDollarSign(Arrays.stream(Objects.requireNonNull(selectedDirectory.listFiles()))
90+
.collect(Collectors.toList()));
91+
9392
List<File> files = AppWishUtil.filterOnClassPrefix(Arrays.stream(Objects.requireNonNull(selectedDirectory.listFiles()))
9493
.collect(Collectors.toList()));
9594
fileListView.getItems().clear();
@@ -106,6 +105,7 @@ protected void updateItem(File item, boolean empty) {
106105
}
107106
});
108107
}
108+
109109
@FXML
110110
private void goToMainScene(ActionEvent ae) {
111111

@@ -124,8 +124,8 @@ private void goToMainScene(ActionEvent ae) {
124124
}
125125

126126
@FXML
127-
private void stopExecutedJavaApp(ActionEvent ae){
128-
127+
private void stopExecutedJavaApp(ActionEvent ae) {
128+
btnStopApp.setVisible(false);
129129
this.executingJavaAppProcess.toHandle().destroy();
130130
}
131131
}

0 commit comments

Comments
 (0)