diff --git a/src/main/java/com/zzg/mybatis/generator/MainUI.java b/src/main/java/com/zzg/mybatis/generator/MainUI.java index 7683bc6e..264889f5 100644 --- a/src/main/java/com/zzg/mybatis/generator/MainUI.java +++ b/src/main/java/com/zzg/mybatis/generator/MainUI.java @@ -33,20 +33,7 @@ public void start(Stage primaryStage) throws Exception { primaryStage.show(); MainUIController controller = fxmlLoader.getController(); - GeneratorConfig config = ConfigHelper.loadGeneratorConfig(); - if (config != null) { - controller.setGeneratorConfigIntoUI(config); - } controller.setPrimaryStage(primaryStage); - primaryStage.setOnCloseRequest(event -> { - GeneratorConfig generatorConfig = controller.getGeneratorConfigFromUI(); - try { - ConfigHelper.saveGeneratorConfig(generatorConfig); - } catch (Exception e) { - _LOG.error(e.getMessage(), e); - AlertUtil.showErrorAlert(e.getMessage()); - } - }); } diff --git a/src/main/java/com/zzg/mybatis/generator/controller/BaseFXController.java b/src/main/java/com/zzg/mybatis/generator/controller/BaseFXController.java index e6bc2c4f..cc84af1d 100644 --- a/src/main/java/com/zzg/mybatis/generator/controller/BaseFXController.java +++ b/src/main/java/com/zzg/mybatis/generator/controller/BaseFXController.java @@ -80,4 +80,10 @@ public void showDialogStage() { } } + public void closeDialogStage() { + if (dialogStage != null) { + dialogStage.close(); + } + } + } diff --git a/src/main/java/com/zzg/mybatis/generator/controller/FXMLPage.java b/src/main/java/com/zzg/mybatis/generator/controller/FXMLPage.java index 8267d94f..65f7876a 100644 --- a/src/main/java/com/zzg/mybatis/generator/controller/FXMLPage.java +++ b/src/main/java/com/zzg/mybatis/generator/controller/FXMLPage.java @@ -8,7 +8,8 @@ public enum FXMLPage { NEW_CONNECTION("fxml/newConnection.fxml"), - SELECT_TABLE_COLUMN("fxml/selectTableColumn.fxml"); + SELECT_TABLE_COLUMN("fxml/selectTableColumn.fxml"), + GENERATOR_CONFIG("fxml/generatorConfigs.fxml"),; private String fxml; diff --git a/src/main/java/com/zzg/mybatis/generator/controller/GeneratorConfigController.java b/src/main/java/com/zzg/mybatis/generator/controller/GeneratorConfigController.java new file mode 100644 index 00000000..9bcabd62 --- /dev/null +++ b/src/main/java/com/zzg/mybatis/generator/controller/GeneratorConfigController.java @@ -0,0 +1,100 @@ +package com.zzg.mybatis.generator.controller; + +import com.zzg.mybatis.generator.model.GeneratorConfig; +import com.zzg.mybatis.generator.util.ConfigHelper; +import com.zzg.mybatis.generator.view.AlertUtil; +import javafx.collections.FXCollections; +import javafx.fxml.FXML; +import javafx.scene.control.Button; +import javafx.scene.control.TableCell; +import javafx.scene.control.TableColumn; +import javafx.scene.control.TableView; +import javafx.scene.control.cell.PropertyValueFactory; +import javafx.scene.layout.HBox; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.URL; +import java.util.List; +import java.util.ResourceBundle; + +/** + * Created by Owen on 8/21/16. + */ +public class GeneratorConfigController extends BaseFXController { + + private static final Logger _LOG = LoggerFactory.getLogger(GeneratorConfigController.class); + + @FXML + private TableView configTable; + @FXML + private TableColumn nameColumn; + @FXML + private TableColumn opsColumn; + + private MainUIController mainUIController; + + private GeneratorConfigController controller; + + @Override + public void initialize(URL location, ResourceBundle resources) { + controller = this; + nameColumn.setCellValueFactory(new PropertyValueFactory<>("name")); + // 自定义操作列 + opsColumn.setCellValueFactory(new PropertyValueFactory<>("name")); + opsColumn.setCellFactory(cell -> { + return new TableCell() { + @Override + protected void updateItem(Object item, boolean empty) { + super.updateItem(item, empty); + if (item == null || empty) { + setText(null); + } else { + Button btn1 = new Button("应用"); + Button btn2 = new Button("删除"); + HBox hBox = new HBox(); + hBox.setSpacing(10); + hBox.getChildren().add(btn1); + hBox.getChildren().add(btn2); + btn1.setOnAction(event -> { + try { + // 应用配置 + GeneratorConfig generatorConfig = ConfigHelper.loadGeneratorConfig(item.toString()); + mainUIController.setGeneratorConfigIntoUI(generatorConfig); + controller.closeDialogStage(); + } catch (Exception e) { + AlertUtil.showErrorAlert(e.getMessage()); + } + }); + btn2.setOnAction(event -> { + try { + // 删除配置 + _LOG.debug("item: {}", item); + ConfigHelper.deleteGeneratorConfig(item.toString()); + refreshTableView(); + } catch (Exception e) { + AlertUtil.showErrorAlert(e.getMessage()); + } + }); + setGraphic(hBox); + } + } + }; + }); + refreshTableView(); + } + + public void refreshTableView() { + try { + List configs = ConfigHelper.loadGeneratorConfigs(); + configTable.setItems(FXCollections.observableList(configs)); + } catch (Exception e) { + AlertUtil.showErrorAlert(e.getMessage()); + } + } + + void setMainUIController(MainUIController mainUIController) { + this.mainUIController = mainUIController; + } + +} diff --git a/src/main/java/com/zzg/mybatis/generator/controller/MainUIController.java b/src/main/java/com/zzg/mybatis/generator/controller/MainUIController.java index e1c686ee..29a759a7 100644 --- a/src/main/java/com/zzg/mybatis/generator/controller/MainUIController.java +++ b/src/main/java/com/zzg/mybatis/generator/controller/MainUIController.java @@ -35,6 +35,8 @@ public class MainUIController extends BaseFXController { @FXML private Label connectionLabel; @FXML + private Label configsLabel; + @FXML private TextField connectorPathField; @FXML private TextField modelTargetPackage; @@ -83,6 +85,15 @@ public void initialize(URL location, ResourceBundle resources) { controller.setMainUIController(this); controller.showDialogStage(); }); + ImageView configImage = new ImageView("icons/config-list.png"); + configImage.setFitHeight(40); + configImage.setFitWidth(40); + configsLabel.setGraphic(configImage); + configsLabel.setOnMouseClicked(event -> { + GeneratorConfigController controller = (GeneratorConfigController) loadFXMLPage("Generator Config", FXMLPage.GENERATOR_CONFIG, false); + controller.setMainUIController(this); + controller.showDialogStage(); + }); leftDBTree.setShowRoot(false); leftDBTree.setRoot(new TreeItem<>()); @@ -213,6 +224,25 @@ public void generateCode() { } } + @FXML + public void saveGeneratorConfig() { + TextInputDialog dialog = new TextInputDialog("保存配置"); + dialog.setTitle("保存当前配置"); + dialog.setContentText("请输入配置名称"); + Optional result = dialog.showAndWait(); + if (result.isPresent()) { + String name = result.get(); + _LOG.info("user choose name: {}", name); + try { + GeneratorConfig generatorConfig = getGeneratorConfigFromUI(); + generatorConfig.setName(name); + ConfigHelper.saveGeneratorConfig(generatorConfig); + } catch (Exception e) { + AlertUtil.showErrorAlert("删除配置失败"); + } + } + } + public GeneratorConfig getGeneratorConfigFromUI() { GeneratorConfig generatorConfig = new GeneratorConfig(); generatorConfig.setConnectorJarPath(connectorPathField.getText()); diff --git a/src/main/java/com/zzg/mybatis/generator/model/GeneratorConfig.java b/src/main/java/com/zzg/mybatis/generator/model/GeneratorConfig.java index e54c1d37..f52cdde2 100644 --- a/src/main/java/com/zzg/mybatis/generator/model/GeneratorConfig.java +++ b/src/main/java/com/zzg/mybatis/generator/model/GeneratorConfig.java @@ -8,6 +8,11 @@ */ public class GeneratorConfig { + /** + * 本配置的名称 + */ + private String name; + private String connectorJarPath; private String projectFolder; @@ -32,6 +37,14 @@ public class GeneratorConfig { private boolean comment; + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + public String getTableName() { return tableName; } diff --git a/src/main/java/com/zzg/mybatis/generator/util/ConfigHelper.java b/src/main/java/com/zzg/mybatis/generator/util/ConfigHelper.java index e3552e42..f9be7f5c 100644 --- a/src/main/java/com/zzg/mybatis/generator/util/ConfigHelper.java +++ b/src/main/java/com/zzg/mybatis/generator/util/ConfigHelper.java @@ -124,7 +124,7 @@ public static void saveGeneratorConfig(GeneratorConfig generatorConfig) throws E conn = ConnectionManager.getConnection(); stat = conn.createStatement(); String jsonStr = JSON.toJSONString(generatorConfig); - String sql = String.format("INSERT INTO generator_config values('%s', '%s')", "current", jsonStr); + String sql = String.format("INSERT INTO generator_config values('%s', '%s')", generatorConfig.getName(), jsonStr); stat.executeUpdate(sql); } finally { if (rs != null) rs.close(); @@ -133,14 +133,14 @@ public static void saveGeneratorConfig(GeneratorConfig generatorConfig) throws E } } - public static GeneratorConfig loadGeneratorConfig() throws Exception { + public static GeneratorConfig loadGeneratorConfig(String name) throws Exception { Connection conn = null; Statement stat = null; ResultSet rs = null; try { conn = ConnectionManager.getConnection(); stat = conn.createStatement(); - String sql = String.format("SELECT * FROM generator_config where name='%s'", "current"); + String sql = String.format("SELECT * FROM generator_config where name='%s'", name); _LOG.info("sql: ", sql); rs = stat.executeQuery(sql); GeneratorConfig generatorConfig = null; @@ -156,5 +156,43 @@ public static GeneratorConfig loadGeneratorConfig() throws Exception { } } + public static List loadGeneratorConfigs() throws Exception { + Connection conn = null; + Statement stat = null; + ResultSet rs = null; + try { + conn = ConnectionManager.getConnection(); + stat = conn.createStatement(); + String sql = String.format("SELECT * FROM generator_config"); + _LOG.info("sql: ", sql); + rs = stat.executeQuery(sql); + List configs = new ArrayList<>(); + while (rs.next()) { + String value = rs.getString("value"); + configs.add(JSON.parseObject(value, GeneratorConfig.class)); + } + return configs; + } finally { + if (rs != null) rs.close(); + if (stat != null) stat.close(); + if (conn != null) conn.close(); + } + } + + public static int deleteGeneratorConfig(String name) throws Exception { + Connection conn = null; + Statement stat = null; + try { + conn = ConnectionManager.getConnection(); + stat = conn.createStatement(); + String sql = String.format("DELETE FROM generator_config where name='%s'", name); + _LOG.info("sql: {}", sql); + return stat.executeUpdate(sql); + } finally { + if (stat != null) stat.close(); + if (conn != null) conn.close(); + } + } + } diff --git a/src/main/resources/fxml/MainUI.fxml b/src/main/resources/fxml/MainUI.fxml index 94e21687..f378d247 100644 --- a/src/main/resources/fxml/MainUI.fxml +++ b/src/main/resources/fxml/MainUI.fxml @@ -35,11 +35,12 @@ - @@ -76,25 +77,25 @@ -