Skip to content

Commit

Permalink
优化界面提示
Browse files Browse the repository at this point in the history
  • Loading branch information
zouzg committed Feb 3, 2017
1 parent 86bdc20 commit 4f70420
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.zzg.mybatis.generator.model.UITableColumnVO;
import com.zzg.mybatis.generator.util.ConfigHelper;
import com.zzg.mybatis.generator.util.DbUtil;
import com.zzg.mybatis.generator.util.StringUtils;
import com.zzg.mybatis.generator.util.MyStringUtils;
import com.zzg.mybatis.generator.view.AlertUtil;
import com.zzg.mybatis.generator.view.UIProgressCallback;
import javafx.collections.FXCollections;
Expand All @@ -18,9 +18,10 @@
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.util.Callback;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.mybatis.generator.config.ColumnOverride;
import org.mybatis.generator.config.IgnoredColumn;
Expand All @@ -29,6 +30,7 @@

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.ResourceBundle;
Expand All @@ -38,15 +40,7 @@
public class MainUIController extends BaseFXController {

private static final Logger _LOG = LoggerFactory.getLogger(MainUIController.class);
private static final String PROJECT_FOLDER_NOT_SELECT = "Please choose project folder!";
private static final String PROJECT_FOLDER_NOT_EXIST = "Project folder not exist,create?";
private static final String MODEL_FOLDER_NOT_INPUT = "Please input model folder!";
private static final String MODEL_FOLDER_NOT_EXIST = "Model target folder not exist, create?";
private static final String DAO_FOLDER_NOT_INPUT = "Please input dao folder!";
private static final String DAO_FOLDER_NOT_EXIST = "Dao target folder not exist, create?";
private static final String MAPPER_FOLDER_NOT_INPUT = "Please input mapping folder!";
private static final String MAPPER_FOLDER_NOT_EXIST = "Mapping target folder not exist, create?";

private static final String FOLDER_NO_EXIST = "部分目录不存在,是否创建";
// tool bar buttons
@FXML
private Label connectionLabel;
Expand Down Expand Up @@ -174,7 +168,7 @@ public void initialize(URL location, ResourceBundle resources) {
selectedDatabaseConfig = (DatabaseConfig) treeItem.getParent().getGraphic().getUserData();
this.tableName = tableName;
tableNameField.setText(tableName);
domainObjectNameField.setText(StringUtils.dbStringToCamelStyle(tableName));
domainObjectNameField.setText(MyStringUtils.dbStringToCamelStyle(tableName));
}
}
});
Expand Down Expand Up @@ -217,7 +211,7 @@ public void chooseProjectFolder() {
@FXML
public void generateCode() {
if (tableName == null) {
AlertUtil.showErrorAlert("Please select table from left DB tree first");
AlertUtil.showWarnAlert("请先在左侧选择数据库表");
return;
}
GeneratorConfig generatorConfig = getGeneratorConfigFromUI();
Expand Down Expand Up @@ -292,10 +286,10 @@ public void setGeneratorConfigIntoUI(GeneratorConfig generatorConfig) {
@FXML
public void openTableColumnCustomizationPage() {
if (tableName == null) {
AlertUtil.showErrorAlert("Please select table from left DB treee first");
AlertUtil.showWarnAlert("请先在左侧选择数据库表");
return;
}
SelectTableColumnController controller = (SelectTableColumnController) loadFXMLPage("Select Columns", FXMLPage.SELECT_TABLE_COLUMN, true);
SelectTableColumnController controller = (SelectTableColumnController) loadFXMLPage("定制列", FXMLPage.SELECT_TABLE_COLUMN, true);
controller.setMainUIController(this);
try {
// If select same schema and another table, update table data
Expand All @@ -320,91 +314,42 @@ public void setColumnOverrides(List<ColumnOverride> columnOverrides) {
}

/**
* check dirs exist
* 检查并创建不存在的文件夹
*
* @return
*/
private boolean checkDirs(GeneratorConfig config) {
assertNotNull(config);

/* check project folder */
String projectFolder = config.getProjectFolder();
if (StringUtils.isBlank(projectFolder)) {
AlertUtil.showInfoAlert(PROJECT_FOLDER_NOT_SELECT);
return false;
}

if(!accept(projectFolder, PROJECT_FOLDER_NOT_EXIST)) {
return false;
}

if(!projectFolder.endsWith(File.separator)) {
projectFolder = projectFolder.concat(File.separator);
}

/* check model target folder */
String modelTargetFolder = config.getModelPackageTargetFolder();
if (StringUtils.isBlank(modelTargetFolder)) {
AlertUtil.showInfoAlert(MODEL_FOLDER_NOT_INPUT);
return false;
}

if(!accept(projectFolder.concat(modelTargetFolder), MODEL_FOLDER_NOT_EXIST)) {
return false;
}

/* check dao target folder */
String daoTargetFolder = config.getDaoTargetFolder();
if (StringUtils.isBlank(daoTargetFolder)) {
AlertUtil.showInfoAlert(DAO_FOLDER_NOT_INPUT);
return false;
}

if(!accept(projectFolder.concat(daoTargetFolder), DAO_FOLDER_NOT_EXIST)) {
return false;
}

/* check mapper target folder */
String mapperTargetFolder = config.getMappingXMLTargetFolder();
if (StringUtils.isBlank(mapperTargetFolder)) {
AlertUtil.showInfoAlert(MAPPER_FOLDER_NOT_INPUT);
return false;
}

if(!accept(projectFolder.concat(mapperTargetFolder), MAPPER_FOLDER_NOT_EXIST)) {
return false;
}

List<String> dirs = new ArrayList<>();
dirs.add(FilenameUtils.normalize(config.getProjectFolder().concat("/").concat(config.getModelPackageTargetFolder())));
dirs.add(FilenameUtils.normalize(config.getProjectFolder().concat("/").concat(config.getDaoTargetFolder())));
dirs.add(FilenameUtils.normalize(config.getProjectFolder().concat("/").concat(config.getMappingXMLTargetFolder())));
boolean haveNotExistFolder = false;
for (String dir : dirs) {
File file = new File(dir);
if (!file.exists()) {
haveNotExistFolder = true;
}
}
if (haveNotExistFolder) {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setContentText(FOLDER_NO_EXIST);
Optional<ButtonType> optional = alert.showAndWait();
if (optional.isPresent()) {
if (ButtonType.OK == optional.get()) {
try {
for (String dir : dirs) {
FileUtils.forceMkdir(new File(dir));
}
return true;
} catch (Exception e) {
AlertUtil.showErrorAlert("创建目录失败,请检查目录是否是文件而非目录");
}
} else {
return false;
}
}
}
return true;
}

/**
* check dir exist && confirm mkdir
*
* @param dirPath
* @param message
* @return
*/
private boolean accept(String dirPath, String message) {
File file = new File(dirPath);
if (file.exists()) {
return true;
}

Alert confirmationAlert = AlertUtil.buildConfirmationAlert(message);
Optional<ButtonType> optional = confirmationAlert.showAndWait();
if (optional.isPresent()) {
if (ButtonType.OK == optional.get()) {
try {
FileUtils.forceMkdir(new File(dirPath));
return true;
} catch (Exception e) {
AlertUtil.showErrorAlert("创建失败");
}
}
}

return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Created by Owen on 6/18/16.
*/
public class StringUtils {
public class MyStringUtils {

/**
*
Expand Down Expand Up @@ -59,13 +59,4 @@ public static String dbStringToCamelStyle2(String str) {
return null;
}

/**
* check string is blank
*
* @param str
* @return
*/
public static boolean isBlank(String str) {
return org.apache.commons.lang3.StringUtils.isBlank(str);
}
}
18 changes: 9 additions & 9 deletions src/main/resources/fxml/MainUI.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
</rowConstraints>
<children>
<Label text="表名" />
<TextField fx:id="tableNameField" prefHeight="27.0" prefWidth="156.0" promptText="person" GridPane.columnIndex="1">
<TextField fx:id="tableNameField" disable="true" editable="false" prefHeight="27.0" prefWidth="156.0" promptText="person" GridPane.columnIndex="1">
<GridPane.margin>
<Insets left="5.0" right="5.0" />
</GridPane.margin>
Expand All @@ -86,7 +86,7 @@
<Insets right="5.0" />
</HBox.margin>
</TextField>
<Button mnemonicParsing="false" onAction="#openTableColumnCustomizationPage" text="Choose">
<Button mnemonicParsing="false" onAction="#openTableColumnCustomizationPage" text="定制列">
<styleClass>
<String fx:value="btn" />
<String fx:value="btn-default" />
Expand All @@ -97,7 +97,7 @@
<Insets left="5.0" />
</GridPane.margin>
</HBox>
<Label text="主键" GridPane.rowIndex="2" />
<Label text="主键(选填)" GridPane.rowIndex="2" />
<HBox alignment="CENTER_LEFT" GridPane.columnIndex="1" GridPane.columnSpan="3" GridPane.rowIndex="2" GridPane.valignment="CENTER">
<children>
<TextField fx:id="generateKeysField" prefHeight="27.0" prefWidth="154.0" promptText="primary key, such as id" GridPane.columnIndex="1" GridPane.rowIndex="3">
Expand All @@ -121,7 +121,7 @@
<Insets left="5.0" right="5.0" />
</HBox.margin>
</TextField>
<Button mnemonicParsing="false" onAction="#chooseProjectFolder" text="Choose">
<Button mnemonicParsing="false" onAction="#chooseProjectFolder" text="选择">
<styleClass>
<String fx:value="btn" />
<String fx:value="btn-default" />
Expand All @@ -138,7 +138,7 @@
</GridPane.margin>
</TextField>
<Label text="存放目录" GridPane.columnIndex="2" GridPane.rowIndex="4" />
<TextField fx:id="modelTargetProject" prefHeight="27.0" prefWidth="155.0" promptText="src/main/java" GridPane.columnIndex="3" GridPane.rowIndex="4">
<TextField fx:id="modelTargetProject" prefHeight="27.0" prefWidth="155.0" promptText="src/main/java" text="src/main/java" GridPane.columnIndex="3" GridPane.rowIndex="4">
<GridPane.margin>
<Insets left="5.0" />
</GridPane.margin></TextField>
Expand All @@ -152,7 +152,7 @@
</GridPane.margin>
</TextField>
<Label text="存放目录" GridPane.columnIndex="2" GridPane.rowIndex="5" />
<TextField fx:id="daoTargetProject" prefHeight="27.0" prefWidth="155.0" promptText="src/main/java" GridPane.columnIndex="3" GridPane.rowIndex="5">
<TextField fx:id="daoTargetProject" prefHeight="27.0" prefWidth="155.0" promptText="src/main/java" text="src/main/java" GridPane.columnIndex="3" GridPane.rowIndex="5">
<GridPane.margin>
<Insets left="5.0" />
</GridPane.margin></TextField>
Expand All @@ -166,7 +166,7 @@
</GridPane.margin>
</TextField>
<Label text="存放目录" GridPane.columnIndex="2" GridPane.rowIndex="6" />
<TextField fx:id="mappingTargetProject" prefHeight="27.0" prefWidth="155.0" promptText="src/main/resources" GridPane.columnIndex="3" GridPane.rowIndex="6">
<TextField fx:id="mappingTargetProject" prefHeight="27.0" prefWidth="155.0" promptText="src/main/resources" text="src/main/resources" GridPane.columnIndex="3" GridPane.rowIndex="6">
<GridPane.margin>
<Insets left="5.0" />
</GridPane.margin></TextField>
Expand Down Expand Up @@ -202,12 +202,12 @@
</styleClass></Button>
</children>
</HBox>
<Label text="DAO名称(选填)" GridPane.columnIndex="4" GridPane.rowIndex="4">
<Label text="DAO名称(选填)" GridPane.columnIndex="4" GridPane.rowIndex="5">
<padding>
<Insets left="5.0" />
</padding>
</Label>
<TextField fx:id="mapperName" prefHeight="27.0" prefWidth="138.0" promptText="PersonDAO" GridPane.columnIndex="5" GridPane.rowIndex="4">
<TextField fx:id="mapperName" prefHeight="27.0" prefWidth="138.0" promptText="PersonDAO" GridPane.columnIndex="5" GridPane.rowIndex="5">
<GridPane.margin>
<Insets left="5.0" />
</GridPane.margin></TextField>
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/com/zzg/mybatis/generator/util/StringUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,31 @@ public class StringUtilTest {

@Test
public void testDbStringToCamelStyle() {
String result = StringUtils.dbStringToCamelStyle("person_address");
String result = MyStringUtils.dbStringToCamelStyle("person_address");
Assert.assertEquals("PersonAddress", result);
}

@Test
public void testDbStringToCamelStyle_case2() {
String result = StringUtils.dbStringToCamelStyle("person_address_name");
String result = MyStringUtils.dbStringToCamelStyle("person_address_name");
Assert.assertEquals("PersonAddressName", result);
}

@Test
public void testDbStringToCamelStyle_case3() {
String result = StringUtils.dbStringToCamelStyle("person_DB_name");
String result = MyStringUtils.dbStringToCamelStyle("person_DB_name");
Assert.assertEquals("PersonDBName", result);
}

@Test
public void testDbStringToCamelStyle_case4() {
String result = StringUtils.dbStringToCamelStyle("person_jobs_");
String result = MyStringUtils.dbStringToCamelStyle("person_jobs_");
Assert.assertEquals("PersonJobs", result);
}

@Test
public void testDbStringToCamelStyle_case5() {
String result = StringUtils.dbStringToCamelStyle("a");
String result = MyStringUtils.dbStringToCamelStyle("a");
Assert.assertEquals("A", result);
}

Expand Down

0 comments on commit 4f70420

Please sign in to comment.