Skip to content

Commit

Permalink
增加OverSSH功能,优化连接设置界面设计
Browse files Browse the repository at this point in the history
  • Loading branch information
slankka committed Jan 26, 2019
1 parent 2af59d1 commit 48427bb
Show file tree
Hide file tree
Showing 16 changed files with 1,092 additions and 210 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.54</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.zzg.mybatis.generator.bridge;

import com.jcraft.jsch.Session;
import com.zzg.mybatis.generator.controller.PictureProcessStateController;
import com.zzg.mybatis.generator.model.DatabaseConfig;
import com.zzg.mybatis.generator.model.DbType;
import com.zzg.mybatis.generator.model.GeneratorConfig;
Expand Down Expand Up @@ -259,7 +261,6 @@ public void generate() throws Exception {
mappingXMLFile.delete();
}
}

myBatisGenerator.generate(progressCallback, contexts, fullyqualifiedTables);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.zzg.mybatis.generator.controller;

import com.zzg.mybatis.generator.exception.DbDriverLoadingException;
import com.zzg.mybatis.generator.model.DatabaseConfig;
import com.zzg.mybatis.generator.util.ConfigHelper;
import com.zzg.mybatis.generator.util.DbUtil;
import com.zzg.mybatis.generator.view.AlertUtil;
import javafx.fxml.FXML;
import javafx.scene.control.ChoiceBox;
Expand All @@ -17,113 +15,88 @@

public class DbConnectionController extends BaseFXController {

private static final Logger _LOG = LoggerFactory.getLogger(DbConnectionController.class);
private static final Logger _LOG = LoggerFactory.getLogger(DbConnectionController.class);

@FXML
private TextField nameField;
@FXML
private TextField hostField;
@FXML
private TextField portField;
@FXML
private TextField userNameField;
@FXML
private TextField passwordField;
@FXML
private TextField schemaField;
@FXML
private ChoiceBox<String> encodingChoice;
@FXML
private ChoiceBox<String> dbTypeChoice;
private MainUIController mainUIController;
private boolean isUpdate = false;
private Integer primayKey;
@FXML
protected TextField nameField;
@FXML
protected TextField hostField;
@FXML
protected TextField portField;
@FXML
protected TextField userNameField;
@FXML
protected TextField passwordField;
@FXML
protected TextField schemaField;
@FXML
protected ChoiceBox<String> encodingChoice;
@FXML
protected ChoiceBox<String> dbTypeChoice;
protected MainUIController mainUIController;
protected boolean isUpdate = false;
protected Integer primayKey;

@Override
public void initialize(URL location, ResourceBundle resources) {
}

@Override
public void initialize(URL location, ResourceBundle resources) {
}
final void saveConnection() {
DatabaseConfig config = extractConfigForUI();
if (config == null) {
return;
}
try {
ConfigHelper.saveDatabaseConfig(this.isUpdate, primayKey, config);
getDialogStage().close();
mainUIController.loadLeftDBTree();
} catch (Exception e) {
_LOG.error(e.getMessage(), e);
AlertUtil.showErrorAlert(e.getMessage());
}
}

@FXML
void saveConnection() {
DatabaseConfig config = extractConfigForUI();
if (config == null) {
return;
}
try {
ConfigHelper.saveDatabaseConfig(this.isUpdate, primayKey, config);
getDialogStage().close();
mainUIController.loadLeftDBTree();
} catch (Exception e) {
_LOG.error(e.getMessage(), e);
AlertUtil.showErrorAlert(e.getMessage());
}
}
void setMainUIController(MainUIController controller) {
this.mainUIController = controller;
super.setDialogStage(mainUIController.getDialogStage());
}

@FXML
void testConnection() {
DatabaseConfig config = extractConfigForUI();
if (config == null) {
return;
}
try {
DbUtil.getConnection(config);
AlertUtil.showInfoAlert("连接成功");
} catch (DbDriverLoadingException e){
_LOG.error("{}", e);
AlertUtil.showWarnAlert("连接失败, "+e.getMessage());
} catch (Exception e) {
_LOG.error(e.getMessage(), e);
AlertUtil.showWarnAlert("连接失败");
}
public DatabaseConfig extractConfigForUI() {
String name = nameField.getText();
String host = hostField.getText();
String port = portField.getText();
String userName = userNameField.getText();
String password = passwordField.getText();
String encoding = encodingChoice.getValue();
String dbType = dbTypeChoice.getValue();
String schema = schemaField.getText();
DatabaseConfig config = new DatabaseConfig();
config.setName(name);
config.setDbType(dbType);
config.setHost(host);
config.setPort(port);
config.setUsername(userName);
config.setPassword(password);
config.setSchema(schema);
config.setEncoding(encoding);
if (StringUtils.isAnyEmpty(name, host, port, userName, encoding, dbType, schema)) {
AlertUtil.showWarnAlert("密码以外其他字段必填");
return null;
}
return config;
}

}

@FXML
void cancel() {
getDialogStage().close();
}

void setMainUIController(MainUIController controller) {
this.mainUIController = controller;
}

private DatabaseConfig extractConfigForUI() {
String name = nameField.getText();
String host = hostField.getText();
String port = portField.getText();
String userName = userNameField.getText();
String password = passwordField.getText();
String encoding = encodingChoice.getValue();
String dbType = dbTypeChoice.getValue();
String schema = schemaField.getText();
DatabaseConfig config = new DatabaseConfig();
config.setName(name);
config.setDbType(dbType);
config.setHost(host);
config.setPort(port);
config.setUsername(userName);
config.setPassword(password);
config.setSchema(schema);
config.setEncoding(encoding);
if (StringUtils.isAnyEmpty(name, host, port, userName, encoding, dbType, schema)) {
AlertUtil.showWarnAlert("密码以外其他字段必填");
return null;
}
return config;
}

public void setConfig(DatabaseConfig config) {
isUpdate = true;
primayKey = config.getId(); // save id for update config
nameField.setText(config.getName());
hostField.setText(config.getHost());
portField.setText(config.getPort());
userNameField.setText(config.getUsername());
passwordField.setText(config.getPassword());
encodingChoice.setValue(config.getEncoding());
dbTypeChoice.setValue(config.getDbType());
schemaField.setText(config.getSchema());
}
public void setConfig(DatabaseConfig config) {
isUpdate = true;
primayKey = config.getId(); // save id for update config
nameField.setText(config.getName());
hostField.setText(config.getHost());
portField.setText(config.getPort());
userNameField.setText(config.getUsername());
passwordField.setText(config.getPassword());
encodingChoice.setValue(config.getEncoding());
dbTypeChoice.setValue(config.getDbType());
schemaField.setText(config.getSchema());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public enum FXMLPage {

NEW_CONNECTION("fxml/newConnection.fxml"),
SELECT_TABLE_COLUMN("fxml/selectTableColumn.fxml"),
GENERATOR_CONFIG("fxml/generatorConfigs.fxml"),;
GENERATOR_CONFIG("fxml/generatorConfigs.fxml"),
;

private String fxml;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.zzg.mybatis.generator.controller;

import com.jcraft.jsch.Session;
import com.zzg.mybatis.generator.bridge.MybatisGeneratorBridge;
import com.zzg.mybatis.generator.model.DatabaseConfig;
import com.zzg.mybatis.generator.model.GeneratorConfig;
Expand All @@ -11,6 +12,7 @@
import com.zzg.mybatis.generator.view.UIProgressCallback;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.Label;
Expand Down Expand Up @@ -117,7 +119,7 @@ public void initialize(URL location, ResourceBundle resources) {
dbImage.setFitWidth(40);
connectionLabel.setGraphic(dbImage);
connectionLabel.setOnMouseClicked(event -> {
DbConnectionController controller = (DbConnectionController) loadFXMLPage("新建数据库连接", FXMLPage.NEW_CONNECTION, false);
TabPaneController controller = (TabPaneController) loadFXMLPage("新建数据库连接", FXMLPage.NEW_CONNECTION, false);
controller.setMainUIController(this);
controller.showDialogStage();
});
Expand Down Expand Up @@ -154,7 +156,7 @@ public void initialize(URL location, ResourceBundle resources) {
MenuItem item2 = new MenuItem("编辑连接");
item2.setOnAction(event1 -> {
DatabaseConfig selectedConfig = (DatabaseConfig) treeItem.getGraphic().getUserData();
DbConnectionController controller = (DbConnectionController) loadFXMLPage("编辑数据库连接", FXMLPage.NEW_CONNECTION, false);
TabPaneController controller = (TabPaneController) loadFXMLPage("编辑数据库连接", FXMLPage.NEW_CONNECTION, false);
controller.setMainUIController(this);
controller.setConfig(selectedConfig);
controller.showDialogStage();
Expand All @@ -178,7 +180,6 @@ public void initialize(URL location, ResourceBundle resources) {
}
treeItem.setExpanded(true);
if (level == 1) {
System.out.println("index: " + leftDBTree.getSelectionModel().getSelectedIndex());
DatabaseConfig selectedConfig = (DatabaseConfig) treeItem.getGraphic().getUserData();
try {
List<String> tables = DbUtil.getTableNames(selectedConfig);
Expand Down Expand Up @@ -287,7 +288,32 @@ public void generateCode() {
bridge.setProgressCallback(alert);
alert.show();
try {
//Engage PortForwarding
Session sshSession = DbUtil.getSSHSession(selectedDatabaseConfig);
DbUtil.engagePortForwarding(sshSession, selectedDatabaseConfig);
PictureProcessStateController pictureProcessStateController = null;
if (sshSession != null) {
pictureProcessStateController = new PictureProcessStateController();
pictureProcessStateController.setDialogStage(getDialogStage());
pictureProcessStateController.startPlay();
}

bridge.generate();

if (pictureProcessStateController != null) {
Task task = new Task<Void>() {
@Override
protected Void call() throws Exception {
Thread.sleep(3000);
return null;
}
};
PictureProcessStateController finalPictureProcessStateController = pictureProcessStateController;
task.setOnSucceeded(event -> {
finalPictureProcessStateController.close();
});
new Thread(task).start();
}
} catch (Exception e) {
e.printStackTrace();
AlertUtil.showErrorAlert(e.getMessage());
Expand Down
Loading

0 comments on commit 48427bb

Please sign in to comment.