Skip to content

Commit

Permalink
Finished pack project.
Browse files Browse the repository at this point in the history
  • Loading branch information
Htoonlin authored and Htoonlin committed Nov 10, 2017
1 parent a392fa5 commit ba6b5f6
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 85 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.sdm</groupId>
<artifactId>MasterIDE</artifactId>
<version>2.2</version>
<version>2.3</version>
<packaging>jar</packaging>

<name>MasterIDE</name>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/sdm/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ public interface IDE {
String RESOURCE_DIR = ("/src/main/resources/").replace('/', File.separatorChar);
String WEB_DIR = ("/src/main/webapp/").replace('/', File.separatorChar);
String[] MODULE_DIRS = {"dao", "entity", "resource"};

String PREV_PROJECT_DIR = "com.sdm.ide.PREV_DIR";
String MVN_DIR = "com.sdm.ide.MVN_DIR";
String MVN_PACK_COMMAND = "clean package";
}

/**
Expand Down
37 changes: 34 additions & 3 deletions src/main/java/com/sdm/ide/component/ProgressDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,23 @@
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class ProgressDialog extends Stage {
public class ProgressDialog<T> extends Stage {

private final ProgressBar mainProgressBar;

public ProgressDialog(final Task<?> task, final boolean showLog) {
private Callback<T> succeedHandler;

private final Task<T> task;

public void onSucceedHandler(Callback<T> handler) {
this.succeedHandler = handler;
}

public ProgressDialog(Task<T> task, final boolean showLog) {
super(StageStyle.UTILITY);

this.task = task;

mainProgressBar = new ProgressBar();
mainProgressBar.setPrefWidth(showLog ? 400 : 250);
this.mainProgressBar.progressProperty().bind(task.progressProperty());
Expand Down Expand Up @@ -65,16 +76,36 @@ public ProgressDialog(final Task<?> task, final boolean showLog) {
btnClose.setDisable(true);
btnClose.setOnAction(event -> close());
rootPane.getChildren().add(btnClose);
task.setOnSucceeded(event -> {
mainProgressBar.setVisible(false);
btnClose.setDisable(false);
if (succeedHandler != null) {
succeedHandler.call(task.getValue());
}
});
} else {
Label lblMessage = new Label();
lblMessage.setPrefWidth(250);
lblMessage.textProperty().bind(task.messageProperty());
rootPane.getChildren().add(lblMessage);
task.setOnSucceeded(event -> {
close();
if (succeedHandler != null) {
succeedHandler.call(task.getValue());
}
});
}

}

public ProgressBar getProgressBar() {
return mainProgressBar;
}

public void start() {
show();
Thread thread = new Thread(task);
thread.setDaemon(true);
thread.start();
}

}
29 changes: 9 additions & 20 deletions src/main/java/com/sdm/ide/controller/EntityManagerController.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,9 @@ public void loadEntity(File entityFile) {

try {
ParseEntityTask task = new ParseEntityTask(entityFile);
ProgressDialog dialog = new ProgressDialog(task, false);
dialog.show();
task.setOnSucceeded((event) -> {
currentEntity = task.getValue();
ProgressDialog<EntityModel> dialog = new ProgressDialog(task, false);
dialog.onSucceedHandler(value -> {
currentEntity = value;
TableHelper.generateColumns(PropertyModel.class, propertyTable);
propertyTable.setItems(FXCollections.observableArrayList(currentEntity.getProperties()));
propertyTable.getColumns().forEach(col -> {
Expand All @@ -103,14 +102,9 @@ public void loadEntity(File entityFile) {
}
});
propertyTable.refresh();

this.showDetail(null);
dialog.close();
this.showDetail(null);
});

Thread thread = new Thread(task);
thread.setDaemon(true);
thread.start();
dialog.start();
} catch (Exception ex) {
AlertDialog.showException(ex);
}
Expand Down Expand Up @@ -165,20 +159,15 @@ public void writeEntity(ActionEvent event) {
}

WriteEntityTask task = new WriteEntityTask(currentEntity);
ProgressDialog dialog = new ProgressDialog(task, false);
dialog.show();
task.setOnSucceeded(worker -> {
dialog.close();
if (task.getValue()) {
ProgressDialog<Boolean> dialog = new ProgressDialog<>(task, false);
dialog.onSucceedHandler(value -> {
if (value) {
this.loadEntity(this.currentEntity.getFile());
} else {
AlertDialog.showWarning("Something wrong in code generation process.");
}
});

Thread thread = new Thread(task);
thread.setDaemon(true);
thread.start();
dialog.start();
} catch (Exception ex) {
AlertDialog.showException(ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private void autoMapped(ActionEvent event) {
private void mapAllEntities(TreeItem<ProjectTreeModel> item) {
ProjectTreeModel model = item.getValue();
if (model.getType() == ProjectTreeModel.Type.ENTITY) {
String javaClass = ProjectManager.getClassNameWithPackage(model.getFile().getPath());
String javaClass = ProjectManager.getClassNameWithPackage(model.getFile());
HibernateManager.getInstance().addEntity(javaClass);
}

Expand Down
93 changes: 36 additions & 57 deletions src/main/java/com/sdm/ide/controller/MainController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@
import com.sdm.ide.model.ProjectTreeModel.Type;
import com.sdm.ide.task.LoadProjectTask;
import com.sdm.ide.task.NewProjectTask;
import java.io.BufferedReader;
import com.sdm.ide.task.PackProjectTask;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.Optional;
import java.util.ResourceBundle;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
Expand All @@ -31,10 +28,13 @@
import javafx.scene.control.TreeView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class MainController implements Initializable {
Expand Down Expand Up @@ -85,16 +85,12 @@ private boolean loadProject() {
&& resDir.exists() && resDir.isDirectory()) {

LoadProjectTask task = new LoadProjectTask(this.projectDirectory);
ProgressDialog dialog = new ProgressDialog(task, false);
dialog.show();
task.setOnSucceeded((event) -> {
projectTreeView.setRoot(task.getValue());
ProgressDialog<TreeItem<ProjectTreeModel>> dialog = new ProgressDialog<>(task, false);
dialog.onSucceedHandler(value -> {
projectTreeView.setRoot(value);
projectTreeView.refresh();
dialog.close();
});
Thread thread = new Thread(task);
thread.setDaemon(true);
thread.start();
dialog.start();
return true;
} else {
AlertDialog.showWarning("Invalid project directory.");
Expand Down Expand Up @@ -183,16 +179,13 @@ private void chooseProjectFolder(boolean isNew) {
if (isNew) {

NewProjectTask task = new NewProjectTask(this.projectDirectory);
ProgressDialog dialog = new ProgressDialog(task, false);
dialog.show();
task.setOnSucceeded((result) -> {
dialog.close();
File downloadFile = task.getValue();
ProgressDialog<File> dialog = new ProgressDialog<>(task, false);
dialog.onSucceedHandler(value -> {
//Remove downloadFile
Optional<ButtonType> respButton = AlertDialog.showQuestion("Do you want to remove downloaded zip file?");
if (respButton.isPresent() && respButton.get().equals(ButtonType.YES)) {
try {
Files.delete(downloadFile.toPath());
Files.delete(value.toPath());
} catch (IOException ex) {
AlertDialog.showException(ex);
}
Expand All @@ -202,9 +195,7 @@ private void chooseProjectFolder(boolean isNew) {
IDE.getPrefs().put(Constants.IDE.PREV_PROJECT_DIR, savePath);
}
});
Thread thread = new Thread(task);
thread.setDaemon(true);
thread.start();
dialog.start();
} else if (this.loadProject()) {
IDE.getPrefs().put(Constants.IDE.PREV_PROJECT_DIR, savePath);
}
Expand Down Expand Up @@ -380,44 +371,32 @@ public void initialize(URL location, ResourceBundle resources) {

}

private boolean isMaven(File mvnFile) {
return mvnFile.exists() && mvnFile.isFile() && mvnFile.getName().startsWith("mvn");
}

@FXML
private void packProject(ActionEvent event) {
Task<Void> packTask = new Task() {
@Override
protected Void call() throws Exception {
String os = System.getProperty("os.name", "windows");
String[] cmd;
String setDir = "cd " + projectDirectory.getPath();
if(os.equalsIgnoreCase("windows")){
cmd = {"cmd.exe /c", setDir, "mvn package"};
}else{
cmd = {setDir, "mvn package"};
}

Process proc = Runtime.getRuntime().exec(cmd);
InputStream istr = proc.getInputStream();

BufferedReader br = new BufferedReader(new InputStreamReader(istr));
String message;
while ((message = br.readLine()) != null) {
updateMessage(message);
}

try {
proc.waitFor();
} catch (InterruptedException e) {
}
br.close();
return null;
}
};

ProgressDialog dialog = new ProgressDialog(packTask, true);
dialog.show();

Thread thread = new Thread(packTask);
thread.setDaemon(true);
thread.start();
final String mvnDirectory = IDE.getPrefs().get(Constants.IDE.MVN_DIR, "");
File mvnFile = new File(mvnDirectory);
if (!isMaven(mvnFile)) {
FileChooser fileChooser = new FileChooser();
fileChooser.setInitialDirectory(projectDirectory);
mvnFile = fileChooser.showOpenDialog(primaryStage);
IDE.getPrefs().put(Constants.IDE.MVN_DIR, mvnFile.getPath());
}

if (isMaven(mvnFile)) {
PackProjectTask task = new PackProjectTask(projectDirectory, mvnFile);
ProgressDialog<String> dialog = new ProgressDialog<>(task, true);
dialog.onSucceedHandler(value -> {
Clipboard clipboard = Clipboard.getSystemClipboard();
ClipboardContent content = new ClipboardContent();
content.putString(value);
clipboard.setContent(content);
AlertDialog.showInfo("Copied path : " + value);
});
dialog.start();
}
}
}
5 changes: 3 additions & 2 deletions src/main/java/com/sdm/ide/helper/ProjectManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ public static String getFilePath(File file) {
return file.getPath().replace(File.separatorChar, '/');
}

public static String getClassNameWithPackage(String filePath) {
return filePath.replaceAll(".*/java/", "").replaceAll("\\.java", "").replace(File.separatorChar, '.');
public static String getClassNameWithPackage(File file) {
String filePath = getFilePath(file);
return filePath.replaceAll("(.*/java/)|(\\.java)", "").replace('/', '.');
}

public static void createModule(File moduleDir) {
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/com/sdm/ide/task/PackProjectTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.sdm.ide.task;

import com.sdm.Constants;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javafx.concurrent.Task;

/**
*
* @author Htoonlin
*/
public class PackProjectTask extends Task<String>{

private final Pattern IS_WAR_PATH = Pattern.compile("(Building war:)(\\s)+(.*)\\.war");
private final File projectDir;
private final File mvnDir;

public PackProjectTask(File projectDir, File mvnDir){
this.projectDir = projectDir;
this.mvnDir = mvnDir;
}

@Override
protected String call() throws Exception {
String command = mvnDir.getPath() + " " + Constants.IDE.MVN_PACK_COMMAND;
Process proc = Runtime.getRuntime().exec(command, null, projectDir);
InputStream istr = proc.getInputStream();
String result = projectDir.getPath() + File.pathSeparator + "target" + "master-api-2.2.war";
try (BufferedReader br = new BufferedReader(new InputStreamReader(istr))) {
String message;
while ((message = br.readLine()) != null) {
updateMessage(message);
Matcher matcher = IS_WAR_PATH.matcher(message);
if(matcher.find()){
result = matcher.group().substring(14);
}
}
proc.waitFor();
}
return result;
}

}

0 comments on commit ba6b5f6

Please sign in to comment.