diff --git a/src/main/java/ink/wgink/code/factory/StartUp.java b/src/main/java/ink/wgink/code/factory/StartUp.java index 3a9d206..1ed1efe 100644 --- a/src/main/java/ink/wgink/code/factory/StartUp.java +++ b/src/main/java/ink/wgink/code/factory/StartUp.java @@ -1,5 +1,6 @@ package ink.wgink.code.factory; +import ink.wgink.code.factory.controller.LoginController; import javafx.application.Application; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; @@ -20,8 +21,10 @@ import javafx.stage.Stage; public class StartUp extends Application { @Override public void start(Stage primaryStage) throws Exception { - FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/route/main.fxml")); + FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/route/login.fxml")); primaryStage.setScene(new Scene(fxmlLoader.load())); + LoginController loginController = fxmlLoader.getController(); + loginController.setPrimaryStage(primaryStage); primaryStage.show(); } diff --git a/src/main/java/ink/wgink/code/factory/controller/GenerateController.java b/src/main/java/ink/wgink/code/factory/controller/GenerateController.java index a077b09..89d81fa 100644 --- a/src/main/java/ink/wgink/code/factory/controller/GenerateController.java +++ b/src/main/java/ink/wgink/code/factory/controller/GenerateController.java @@ -19,6 +19,8 @@ import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.TextField; import javafx.scene.control.cell.PropertyValueFactory; +import javafx.stage.DirectoryChooser; +import javafx.stage.Stage; import org.apache.commons.lang3.StringUtils; import java.io.IOException; @@ -70,6 +72,7 @@ public class GenerateController implements Initializable { private TableVO tableVO; private FieldService fieldService; private GenerateService generateService; + private Stage fieldStage; @Override public void initialize(URL location, ResourceBundle resources) { @@ -130,8 +133,13 @@ public class GenerateController implements Initializable { } @FXML - public void onRefreshClick(ActionEvent actionEvent) { + public void onRefreshClick(ActionEvent actionEvent) throws IOException { this.fieldService.showField(); } + public void setFieldStage(Stage fieldStage) { + this.fieldStage = fieldStage; + fieldService.setFieldStage(fieldStage); + generateService.setFieldStage(fieldStage); + } } diff --git a/src/main/java/ink/wgink/code/factory/controller/LoadingController.java b/src/main/java/ink/wgink/code/factory/controller/LoadingController.java new file mode 100644 index 0000000..3cf8dca --- /dev/null +++ b/src/main/java/ink/wgink/code/factory/controller/LoadingController.java @@ -0,0 +1,14 @@ +package ink.wgink.code.factory.controller; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: LoadingController + * @Description: + * @Author: WangGeng + * @Date: 2021/3/11 20:33 + * @Version: 1.0 + **/ +public class LoadingController { +} diff --git a/src/main/java/ink/wgink/code/factory/controller/LoginController.java b/src/main/java/ink/wgink/code/factory/controller/LoginController.java index 42214ff..379e860 100644 --- a/src/main/java/ink/wgink/code/factory/controller/LoginController.java +++ b/src/main/java/ink/wgink/code/factory/controller/LoginController.java @@ -1,12 +1,20 @@ package ink.wgink.code.factory.controller; import com.sun.javafx.robot.impl.FXRobotHelper; +import ink.wgink.code.factory.utils.MsgUtil; +import javafx.application.Platform; +import javafx.concurrent.Service; +import javafx.concurrent.Task; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.scene.control.Button; +import javafx.scene.control.PasswordField; +import javafx.scene.control.TextField; import javafx.stage.Stage; +import lombok.SneakyThrows; +import org.apache.commons.lang3.StringUtils; import java.io.IOException; @@ -22,14 +30,60 @@ import java.io.IOException; **/ public class LoginController { + public final static String DEFAULT_USERNAME = "admin"; + public final static String DEFAULT_PASSWORD = "aaa111!!!"; @FXML - private Button clickButton; + private TextField usernameField; + @FXML + private PasswordField passwordField; + private Stage primaryStage; @FXML - public void onAction(ActionEvent actionEvent) throws IOException { - Scene scene = new Scene(FXMLLoader.load(getClass().getResource("/route/main.fxml")), 400, 300); - Stage stage = FXRobotHelper.getStages().get(0); - stage.setScene(scene); + public void onLoginClick(ActionEvent actionEvent) throws IOException { + if (StringUtils.isBlank(usernameField.getText())) { + MsgUtil.errorAlert("用户名不能为空"); + return; + } + if (StringUtils.isBlank(passwordField.getText())) { + MsgUtil.errorAlert("密码不能为空"); + return; + } + if (!StringUtils.equalsIgnoreCase(DEFAULT_USERNAME, usernameField.getText()) + || !StringUtils.equals(DEFAULT_PASSWORD, passwordField.getText())) { + MsgUtil.errorAlert("用户名或密码错误"); + return; + } + MsgUtil.loading(primaryStage); + new Service() { + @Override + protected Task createTask() { + return new Task() { + @Override + protected Integer call() throws Exception { + Thread.sleep(1000); + Platform.runLater(new Runnable() { + @SneakyThrows + @Override + public void run() { + primaryStage.close(); + MsgUtil.closeNewLoading(); + FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/route/main.fxml")); + final Stage stage = new Stage(); + stage.setScene(new Scene(fxmlLoader.load())); + MainController mainController = fxmlLoader.getController(); + mainController.setMainStage(stage); + stage.show(); + } + }); + return 0; + } + }; + } + }.start(); + } + public void setPrimaryStage(Stage primaryStage) { + this.primaryStage = primaryStage; + } } diff --git a/src/main/java/ink/wgink/code/factory/controller/MainController.java b/src/main/java/ink/wgink/code/factory/controller/MainController.java index ec5969f..79d899f 100644 --- a/src/main/java/ink/wgink/code/factory/controller/MainController.java +++ b/src/main/java/ink/wgink/code/factory/controller/MainController.java @@ -53,6 +53,7 @@ public class MainController implements Initializable { @FXML private TableView dbTableTableView; private TableService tableService; + private Stage mainStage; @Override @@ -73,7 +74,7 @@ public class MainController implements Initializable { } @FXML - public void onConnectDatabase(ActionEvent event) { + public void onConnectDatabase(ActionEvent event) throws IOException { String dbUrl = dbUrlField.getText(); if (StringUtils.isBlank(dbUrl)) { MsgUtil.errorAlert("数据库地址不能为空"); @@ -99,6 +100,7 @@ public class MainController implements Initializable { MsgUtil.errorAlert("数据库密码不能为空"); return; } + tableService.connectionDatabase(dbUrl, dbPort, dbName, dbUsername, dbPassword); } @@ -158,4 +160,8 @@ public class MainController implements Initializable { } } + public void setMainStage(Stage mainStage) { + this.mainStage = mainStage; + this.tableService.setMainStage(mainStage); + } } diff --git a/src/main/java/ink/wgink/code/factory/service/FieldService.java b/src/main/java/ink/wgink/code/factory/service/FieldService.java index 4204aae..a31d0ce 100644 --- a/src/main/java/ink/wgink/code/factory/service/FieldService.java +++ b/src/main/java/ink/wgink/code/factory/service/FieldService.java @@ -5,6 +5,7 @@ import ink.wgink.code.factory.enums.ColumnIsNullableEnum; import ink.wgink.code.factory.enums.FormFieldTypeEnum; import ink.wgink.code.factory.enums.PropertyTypeEnum; import ink.wgink.code.factory.manager.JdbcManager; +import ink.wgink.code.factory.utils.MsgUtil; import ink.wgink.code.factory.utils.WStringUtil; import ink.wgink.code.factory.vos.FieldVO; import javafx.collections.FXCollections; @@ -12,8 +13,10 @@ import javafx.collections.ObservableList; import javafx.concurrent.Service; import javafx.concurrent.Task; import javafx.scene.control.TableView; +import javafx.stage.Stage; import org.apache.commons.lang3.StringUtils; +import java.io.IOException; import java.util.List; import java.util.Map; @@ -44,6 +47,7 @@ public class FieldService { private ObservableList fields = FXCollections.observableArrayList(); private TableView tableView; private String tableName; + private Stage fieldStage; /** * 表单字段类型 @@ -89,7 +93,8 @@ public class FieldService { return PropertyTypeEnum.STRING.getValue(); } - public void showField() { + public void showField() throws IOException { + MsgUtil.loading(fieldStage); new Service() { @Override @@ -133,6 +138,7 @@ public class FieldService { @Override protected void succeeded() { tableView.setItems(fields); + MsgUtil.closeNewLoading(); super.succeeded(); } }.start(); @@ -188,4 +194,8 @@ public class FieldService { }.start(); } + + public void setFieldStage(Stage fieldStage) { + this.fieldStage = fieldStage; + } } diff --git a/src/main/java/ink/wgink/code/factory/service/GenerateService.java b/src/main/java/ink/wgink/code/factory/service/GenerateService.java index 76a94e4..e0ba6b1 100644 --- a/src/main/java/ink/wgink/code/factory/service/GenerateService.java +++ b/src/main/java/ink/wgink/code/factory/service/GenerateService.java @@ -4,12 +4,16 @@ import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; import ink.wgink.code.factory.utils.DateUtil; +import ink.wgink.code.factory.utils.MsgUtil; import ink.wgink.code.factory.utils.WStringUtil; import ink.wgink.code.factory.vos.FieldVO; import ink.wgink.code.factory.vos.GenerateVO; import ink.wgink.code.factory.vos.TableVO; import javafx.collections.ObservableList; +import javafx.scene.control.Alert; import javafx.scene.control.TableView; +import javafx.stage.DirectoryChooser; +import javafx.stage.Stage; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; @@ -36,6 +40,7 @@ import java.util.Map; @Slf4j public class GenerateService { + private Stage fieldStage; private TableView tableView; Configuration freemarkerConfiguration = new Configuration(Configuration.getVersion()); @@ -46,7 +51,13 @@ public class GenerateService { } public void generateCode(TableVO tableVO, GenerateVO generateVO) throws IOException, TemplateException { - String outFolder = "/Users/wanggeng/Desktop/UploadFiles/code/"; + // 选择文件夹 + DirectoryChooser directoryChooser = new DirectoryChooser(); + File outFolderFile = directoryChooser.showDialog(fieldStage); + if (outFolderFile == null) { + return; + } + String outFolder = outFolderFile.getPath(); // 无前缀表名 String tableFullName = tableVO.getTableName(); String tableNameWithoutPrefix = tableFullName.replaceFirst(generateVO.getTablePrefix(), ""); @@ -105,6 +116,7 @@ public class GenerateService { dtoCode("/normal/pojo/dto.ftl", String.format("%s/pojo/dtos", outFolder, lowerTableName), firstUpperTableName, dataModel); poCode("/normal/pojo/po.ftl", String.format("%s/pojo/pos", outFolder, lowerTableName), firstUpperTableName, dataModel); voCode("/normal/pojo/vo.ftl", String.format("%s/pojo/vos", outFolder, lowerTableName), firstUpperTableName, dataModel); + MsgUtil.alert(Alert.AlertType.CONFIRMATION, "生成成功"); } /** @@ -442,4 +454,8 @@ public class GenerateService { template.process(dataModel, out); out.close(); } + + public void setFieldStage(Stage fieldStage) { + this.fieldStage = fieldStage; + } } diff --git a/src/main/java/ink/wgink/code/factory/service/TableService.java b/src/main/java/ink/wgink/code/factory/service/TableService.java index 223f6e8..e57c089 100644 --- a/src/main/java/ink/wgink/code/factory/service/TableService.java +++ b/src/main/java/ink/wgink/code/factory/service/TableService.java @@ -1,13 +1,16 @@ package ink.wgink.code.factory.service; import ink.wgink.code.factory.manager.JdbcManager; +import ink.wgink.code.factory.utils.MsgUtil; import ink.wgink.code.factory.vos.TableVO; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.concurrent.Service; import javafx.concurrent.Task; import javafx.scene.control.TableView; +import javafx.stage.Stage; +import java.io.IOException; import java.util.List; import java.util.Map; @@ -24,6 +27,7 @@ import java.util.Map; **/ public class TableService { + private Stage mainStage; private TableView dbTableTableView; private ObservableList tableVOObservableList = FXCollections.observableArrayList(); @@ -40,12 +44,13 @@ public class TableService { * @param dbUsername 数据库用户名 * @param dbPassword 数据库密码 */ - public void connectionDatabase(String dbUrl, String dbPort, String dbName, String dbUsername, String dbPassword) { + public void connectionDatabase(String dbUrl, String dbPort, String dbName, String dbUsername, String dbPassword) throws IOException { JdbcManager.getInstance().setDatabaseInfo(dbUrl, dbPort, dbName, dbUsername, dbPassword); showTable(); } - private void showTable() { + private void showTable() throws IOException { + MsgUtil.loading(mainStage); new Service() { @Override protected Task createTask() { @@ -69,12 +74,15 @@ public class TableService { @Override protected void succeeded() { dbTableTableView.setItems(tableVOObservableList); + MsgUtil.closeNewLoading(); super.succeeded(); } }; } }.start(); + } - + public void setMainStage(Stage mainStage) { + this.mainStage = mainStage; } } diff --git a/src/main/java/ink/wgink/code/factory/utils/MsgUtil.java b/src/main/java/ink/wgink/code/factory/utils/MsgUtil.java index c8cc2cb..ca98f7e 100644 --- a/src/main/java/ink/wgink/code/factory/utils/MsgUtil.java +++ b/src/main/java/ink/wgink/code/factory/utils/MsgUtil.java @@ -10,8 +10,11 @@ import javafx.beans.property.ReadOnlyStringProperty; import javafx.concurrent.Service; import javafx.concurrent.Task; import javafx.concurrent.Worker; +import javafx.fxml.FXML; +import javafx.fxml.FXMLLoader; import javafx.geometry.Insets; import javafx.geometry.Pos; +import javafx.geometry.Rectangle2D; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.control.Alert.AlertType; @@ -19,11 +22,13 @@ import javafx.scene.layout.HBox; import javafx.scene.layout.Pane; import javafx.scene.layout.StackPane; import javafx.stage.Modality; +import javafx.stage.Screen; import javafx.stage.Stage; import javafx.stage.StageStyle; import javax.xml.soap.Text; -import java.util.Stack; +import java.io.IOException; +import java.util.*; /** * When you feel like quitting. Think about why you started @@ -37,6 +42,8 @@ import java.util.Stack; **/ public class MsgUtil { + public final static List LOADING_LIST = new ArrayList<>(); + /** * 错误 * @@ -53,7 +60,6 @@ public class MsgUtil { * @param msg */ public static void alert(Alert.AlertType alertType, String msg) { - Alert alert = new Alert(alertType, msg, ButtonType.CLOSE); alert.setTitle("提示"); alert.getDialogPane().setPrefWidth(150); @@ -61,63 +67,43 @@ public class MsgUtil { } /** - * 提醒 + * 等待 * - * @param msg + * @throws IOException */ - public static void msg(final String msg) { - HBox hBox = new HBox(); - hBox.setPadding(new Insets(5, 5, 5, 5)); - hBox.setAlignment(Pos.CENTER); - - Label label = new Label(msg); - label.setId("msgLabel"); - hBox.getChildren().add(label); - Stage stage = new Stage(StageStyle.UNDECORATED); - stage.setScene(new Scene(hBox)); + public static void loading(Stage parentStage) throws IOException { + FXMLLoader fxmlLoader = new FXMLLoader(MsgUtil.class.getResource("/route/loading.fxml")); + Stage stage = new Stage(StageStyle.TRANSPARENT); + stage.setScene(new Scene(fxmlLoader.load())); + stage.initModality(Modality.APPLICATION_MODAL); + stage.setAlwaysOnTop(true); + if (parentStage != null) { + stage.setX(parentStage.getX() + (parentStage.getWidth() - 200) / 2); + stage.setY(parentStage.getY() + (parentStage.getHeight() - 100) / 2); + } stage.show(); + LOADING_LIST.add(0, stage); + } -// Platform.runLater(() -> { -// // 子线程更新主线程UI的代码 -// stage.close(); -// }); - // - - // 这种方式提倡,call中编写代码,successed方法中填写刷新UI的操作, - Service service = new Service() { - @Override - protected Task createTask() { - return new Task() { - @Override - protected String call() throws Exception { - Thread.sleep(2000); - return "success"; - } - - @Override - protected void scheduled() { - // 任务开始, 填写刷新UI的操作 - super.scheduled(); - } - - @Override - protected void running() { - // 任务执行 - super.running(); - } - - // 任务成功之后,关闭提示 - @Override - protected void succeeded() { - // 任务执行成功 - stage.close(); - super.succeeded(); - } - }; - } - }; - service.start(); + /** + * 关闭最新的Loading + */ + public static void closeNewLoading() { + if (LOADING_LIST.isEmpty()) { + return; + } + LOADING_LIST.get(0).close(); + LOADING_LIST.remove(0); + } + /** + * 关闭全部Loading + */ + public static void closeAllLoading() { + for (Stage stage : LOADING_LIST) { + stage.close(); + } + LOADING_LIST.clear(); } } diff --git a/src/main/resources/assets/images/loading.gif b/src/main/resources/assets/images/loading.gif new file mode 100644 index 0000000..fe9e484 Binary files /dev/null and b/src/main/resources/assets/images/loading.gif differ diff --git a/src/main/resources/route/loading.fxml b/src/main/resources/route/loading.fxml new file mode 100644 index 0000000..f898563 --- /dev/null +++ b/src/main/resources/route/loading.fxml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/route/login.fxml b/src/main/resources/route/login.fxml index 4e7a728..b55423f 100644 --- a/src/main/resources/route/login.fxml +++ b/src/main/resources/route/login.fxml @@ -10,40 +10,64 @@ - - - - - - - - - - - - - - + +
+ + + + + + + + + + -
+ + + + + + + + -
- - - - - - -
+ + + + + + + + + + + +