Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #141

Merged
merged 3 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions AppWish/AppWish/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>pn.dev</groupId>
<artifactId>code-generator-gui-ollama</artifactId>
<version>1.7.2</version>
<version>1.7.3</version>
<name>CodeGenerator-GUI</name>


Expand Down Expand Up @@ -45,7 +45,7 @@
<dependency>
<groupId>pn.dev</groupId>
<artifactId>code-generator-ollama</artifactId>
<version>1.7.2</version>
<version>1.7.3</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
Expand Down
88 changes: 44 additions & 44 deletions AppWish/AppWish/src/main/java/pn/app_wish/AppWish.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.image.ImageView;
import javafx.stage.FileChooser;

import javafx.scene.Parent;
Expand All @@ -18,11 +19,14 @@

import java.io.File;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.simple.SimpleLogger;

import pn.app_wish.constant.CodeEvent;
import pn.app_wish.constant.GUIConstants;
import pn.app_wish.constant.StaticAppWishConstants;
import pn.app_wish.util.AppWishUtil;
import pn.cg.app_system.AppSystem;
import pn.cg.app_system.code_generation.model.CompilationJob;
import pn.cg.datastorage.DataStorage;
Expand All @@ -43,6 +47,7 @@


public class AppWish extends Application {
private static final Logger log = LoggerFactory.getLogger(AppWish.class);
private static Stage mainStage;
@FXML
public TextField tf_input;
Expand All @@ -59,7 +64,9 @@ public class AppWish extends Application {
@FXML
public BorderPane bp_main;
@FXML
public Button btnStopGeneratedApp;
public Button btn_StopGeneratedApp;
@FXML
public ImageView logo;

private String javaExecutablePath;
private Process executingJavaAppProcess;
Expand Down Expand Up @@ -98,9 +105,8 @@ public void start(Stage primaryStage) throws IOException {
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");
}


/**
* Appwish GUI lifecycle
* AppWish GUI lifecycle
*/
private void onAppWish(CodeEvent codeEvent) {
isCodeGenerationOnGoing = true;
Expand All @@ -120,7 +126,6 @@ private void onAppWish(CodeEvent codeEvent) {
}
contentOfExistingJavaFile = readTextByLinesFromFile(file);
}

//**codeEvent == CodeEvent.CREATE_APPLICATION**//
else {
contentOfExistingJavaFile = null;
Expand All @@ -146,29 +151,18 @@ private void onAppWish(CodeEvent codeEvent) {

});

//Wait for 1.5 sec

try {
Thread.sleep(1500);
} catch (InterruptedException e) {

Thread.currentThread().interrupt();
}
}


@FXML
private void onRunJavaApp(ActionEvent ae) {
btn_run_application.setVisible(false);
btnStopGeneratedApp.setVisible(true);
btn_StopGeneratedApp.setVisible(true);

if (javaExecutablePath != null) {
System.out.println("Executing java app on path -> " + javaExecutablePath);
try {
ProcessBuilder pb;

pb = new ProcessBuilder(StaticAppWishConstants.BASH_PATH, StaticAppWishConstants.C_ARGUMENT, StaticAppWishConstants.JAVA_TEXT + javaExecutablePath);

executingJavaAppProcess = pb.inheritIO().start();
} catch (IOException e) {
throw new RuntimeException(e);
Expand All @@ -191,7 +185,9 @@ private void stopExecutedGeneratedJavaApp(ActionEvent ae) {

this.executingJavaAppProcess.toHandle().destroy();
btn_run_application.setVisible(true);
btnStopGeneratedApp.setVisible(false);
btn_StopGeneratedApp.setVisible(false);
setButtonGroupVisibilityForCodeGenerationButtons(true);

}

/**
Expand All @@ -201,7 +197,7 @@ private void stopExecutedGeneratedJavaApp(ActionEvent ae) {
private void startGuiThread(CodeEvent codeEvent) {
DataStorage.getInstance().setCompilationJob(new CompilationJob(GUIConstants.DEFAULT_STAGE_TITLE));
Platform.runLater(() -> {
setButtonGroupVisibilityForCodeGenerationBtns(false);
setButtonGroupVisibilityForCodeGenerationButtons(false);
output_label.setVisible(true);

switch (codeEvent) {
Expand All @@ -213,8 +209,8 @@ private void startGuiThread(CodeEvent codeEvent) {
break;
}

setButtonGroupVisibilityForCodeGenerationBtns(false);
btnStopGeneratedApp.setVisible(false);
setButtonGroupVisibilityForCodeGenerationButtons(false);
btn_StopGeneratedApp.setVisible(false);
});
}

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

if (!isCodeGenerationOnGoing)
if (!isCodeGenerationOnGoing) {
setButtonGroupVisibilityToFalseForStartAndStopApplicationsButtons();
onAppWish(CodeEvent.CREATE_APPLICATION);

}
}

/**
Expand All @@ -235,9 +232,10 @@ private void createApplication(ActionEvent ae) {
@FXML
private void continueOnExistingApplication(ActionEvent ae) {

if (!isCodeGenerationOnGoing)
if (!isCodeGenerationOnGoing) {
setButtonGroupVisibilityToFalseForStartAndStopApplicationsButtons();
onAppWish(CodeEvent.CONTINUE_ON_EXISTING_APPLICATION);

}
}


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

if (tf_input != null) {
// Stop displaying start/stop application buttons
// Make a recursive call to AppSystem
switch (codeEvent) {
case CREATE_APPLICATION:
Expand All @@ -257,7 +256,6 @@ private void startCodeGeneration(CodeEvent codeEvent, String pathToJavaApp, List
requireNonNull(contentOfExistingJavaFile));
break;
}

}
}

Expand All @@ -271,55 +269,57 @@ private void waitForCompilationResult() {
}

/**
* If a compilation result exist , check if the singleton in code-generator-ollama contains a path for a executable Java file
* If a compilation result exist , check if the singleton in code-generator-ollama contains a path for an executable Java file
* If the above is true , activate the "run application" button and remove the "generating code..." text
*/
private void handleCompilationResult() {

if (DataStorage.getInstance().getCompilationJob().isResult()) {
javaExecutablePath = DataStorage.getInstance().getJavaExecutionPath();
// Draw success or error texts, and show run app button
Platform.runLater(() -> {
if (DataStorage.getInstance().getJavaExecutionPath() != null) {
output_label.setText("Completed Successfully");
try {
Thread.sleep(2500);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
output_label.setVisible(false);
btn_run_application.setVisible(true);
setButtonGroupVisibilityForCodeGenerationBtns(true);
setButtonGroupVisibilityForCodeGenerationButtons(true);
isCodeGenerationOnGoing = false;
} else {
output_label.setText("Error");
setButtonGroupVisibilityForCodeGenerationBtns(true);
isCodeGenerationOnGoing = false;
output_label.setText("Something went wrong :(");
}
});
}

}
}

/**
* A button group broken out for reduce of redundancy
* Set the visibility for the create and continue application buttons
*/
private void setButtonGroupVisibilityForCodeGenerationBtns(boolean isVisible) {
private void setButtonGroupVisibilityForCodeGenerationButtons(boolean isVisible) {
btn_create_application.setVisible(isVisible);
btn_continue_on_application.setVisible(isVisible);
}

private File showOpenFileDialog() {
/**
* Set the visibility into false, for the start and stop application buttons (in the main gui)
*/
private void setButtonGroupVisibilityToFalseForStartAndStopApplicationsButtons() {
btn_run_application.setVisible(false);
btn_StopGeneratedApp.setVisible(false);
}


private File showOpenFileDialog() {
try {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Choose A Java file to add functionality to");
fileChooser.setInitialDirectory(new File(PathConstants.RESOURCE_PATH
+ StaticAppWishConstants.FOLDER_NAME_OF_GENERATED_JAVA_APPLICATIONS + File.separator));

// Create an extension filter that filters out .class files
FileChooser.ExtensionFilter extensionFilter = new FileChooser.ExtensionFilter(StaticAppWishConstants.CONTINUE_ON_APPLICATION_FILTER_ON_JAVA_EXTENSION_DESCRIPTION
, StaticAppWishConstants.CONTINUE_ON_APPLICATION_FILTER_ON_JAVA_EXTENSION);
fileChooser.getExtensionFilters().add(extensionFilter);
return fileChooser.showOpenDialog(getMainStage());
} catch (Exception e) {
e.printStackTrace();
log.error("Could not open the window for the file chooser");
return null;
}
}
Expand All @@ -328,7 +328,7 @@ private List<String> readTextByLinesFromFile(File file) {
try {
return Files.readAllLines(Paths.get(file.getAbsolutePath()));
} catch (Exception e) {
e.printStackTrace();
log.error("Could not read the lines of the specified file");
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public record StaticAppWishConstants() {
public static final String BASH_PATH="/bin/bash";
public static final String C_ARGUMENT = "-c";
public final static String JAVA_TEXT="java ";

public final static String CONTINUE_ON_APPLICATION_FILTER_ON_JAVA_EXTENSION="*.java";
public final static String CONTINUE_ON_APPLICATION_FILTER_ON_JAVA_EXTENSION_DESCRIPTION="A extension filter with purpose to only show .java files";
public final static String FOLDER_NAME_OF_GENERATED_JAVA_APPLICATIONS="java_source_code_classes_tmp";
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,10 @@ public class AppHistoryController implements Initializable {
private Process executingJavaAppProcess;



private final String R1 = ".class";
private final String R2 = ".java";


@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
AnchorPane.setRightAnchor(btnMainScene,0d);
AnchorPane.setRightAnchor(btnStopApp,80d);
AnchorPane.setRightAnchor(btnMainScene, 0d);
AnchorPane.setRightAnchor(btnStopApp, 80d);
btnStopApp.setVisible(false);
try {
listHistoryApplications();
Expand All @@ -70,26 +65,30 @@ private void handleSelectButtonAction(ActionEvent ae) {

btnStopApp.setVisible(true);
File selectedFile = fileListView.getSelectionModel().selectedItemProperty().getValue();

try {
ProcessBuilder processBuilder;

String executePath = selectedFile.getAbsolutePath().replace(R1,R2);

processBuilder = new ProcessBuilder(BASH_PATH, C_ARGUMENT,JAVA_TEXT + executePath);
executingJavaAppProcess = processBuilder.inheritIO().start();
final String r1 = ".class";
final String r2 = ".java";
final String executePath = selectedFile.getAbsolutePath().replace(r1, r2);

}
catch (IOException e) {
processBuilder = new ProcessBuilder(BASH_PATH, C_ARGUMENT, JAVA_TEXT + executePath);
executingJavaAppProcess = processBuilder.inheritIO().start();
} catch (IOException e) {
System.out.println("RuntimeException while starting Java executable");
throw new RuntimeException(e);
}
}



private void listHistoryApplications() throws IOException {
File selectedDirectory = new File(PathConstants.RESOURCE_PATH + "java_source_code_classes_tmp"+File.separator);
File selectedDirectory = new File(PathConstants.RESOURCE_PATH + "java_source_code_classes_tmp" + File.separator);

// Removes duplicate files from the generated java apps folder
AppWishUtil.removeDuplicateFilesWithAnDollarSign(Arrays.stream(Objects.requireNonNull(selectedDirectory.listFiles()))
.collect(Collectors.toList()));

List<File> files = AppWishUtil.filterOnClassPrefix(Arrays.stream(Objects.requireNonNull(selectedDirectory.listFiles()))
.collect(Collectors.toList()));
fileListView.getItems().clear();
Expand All @@ -106,6 +105,7 @@ protected void updateItem(File item, boolean empty) {
}
});
}

@FXML
private void goToMainScene(ActionEvent ae) {

Expand All @@ -124,8 +124,8 @@ private void goToMainScene(ActionEvent ae) {
}

@FXML
private void stopExecutedJavaApp(ActionEvent ae){

private void stopExecutedJavaApp(ActionEvent ae) {
btnStopApp.setVisible(false);
this.executingJavaAppProcess.toHandle().destroy();
}
}
Loading