package com.persagy.filemove.app; import com.persagy.filemove.dialog.FileMoveHelpDialog; import com.persagy.filemove.dialog.MessageDialog; import com.persagy.filemove.dto.SagaFileMoveDTO; import com.persagy.filemove.dto.WorkerFactory; import com.persagy.filemove.service.SagaFileMoveService; import javafx.application.Application; import javafx.collections.FXCollections; import javafx.concurrent.Task; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.scene.layout.Pane; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.stage.Stage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.lang.reflect.Field; import java.util.LinkedHashMap; public class SagaFileMoveApp extends Application { private static final Logger log = LoggerFactory.getLogger(SagaFileMoveApp.class); private static final String aboutInfo = "Version:1.0\nJDK:1.8\nAuthor:cuixubin@persagy.com"; private static int appWidth = 640; private static int appHeight = 560; public static SagaFileMoveDTO dto = new SagaFileMoveDTO(); /** 数据平台默认systemId */ private static String sysId_dpf = "dataPlatform"; /** 数据平台默认密码 */ private static String secret_dpf = "9e0891a7a8c8e885"; /** revit默认systemId */ private static String sysId_rvt = "revit"; /** revit默认密码 */ private static String secret_rvt = "63afbef6906c342b"; /** key值校正 */ private static String keyValueTip = "key值校正"; private SagaFileMoveService service = new SagaFileMoveService(); /** 文件服务上传下载接口类型 */ private static final String[] apiTypes = {"default", "file", "image"}; /** 对象类型 */ private static String[] objTypes; /** 对象类型对应信息点 */ private static final LinkedHashMap objToInfoCode = new LinkedHashMap<>(); static { objToInfoCode.put("Fl", new String[]{"FloorMap"}); objToInfoCode.put("Eq", new String[]{"EquipQRCode"}); objToInfoCode.put("Sp", new String[]{"RoomQRCode"}); objToInfoCode.put("Sy", new String[]{"EquipQRCode"}); objToInfoCode.put("Ec", new String[]{"EquipQRCode"}); objTypes = objToInfoCode.keySet().stream().toArray(n -> new String[n]); } /** 数据平台地址 */ private TextField tfDPF = new TextField("http://api.sagacloud.cn/data-platform-3"); /** 项目id */ private TextField tfPjId = new TextField("Pj3101050003"); /** 项目密码 */ private TextField tfPjSecret = new TextField(); /** 对象类型 */ private ChoiceBox cbObjType = new ChoiceBox(); /** 对象信息点 */ private ChoiceBox cbObjInfoCodeArray = new ChoiceBox(); /** 文件服务from地址 */ private TextField tfImgFromURL = new TextField("http://47.93.33.207:28888/image-service"); /** 文件服务from的systemId */ private TextField tfImgFromSysId = new TextField(sysId_rvt); /** 文件服务from的secret */ private TextField tfImgFromSecret = new TextField(secret_rvt); /** 接口类型 */ private ChoiceBox cbImgFromApiType = new ChoiceBox(FXCollections.observableArrayList(apiTypes)); /** 文件服务To地址 */ private TextField tfImgToURL = new TextField("http://127.0.0.1:6666/image-service"); /** 文件服务To的systemId */ private TextField tfImgToSysId = new TextField(sysId_dpf); /** 文件服务To的secret */ private TextField tfImgToSecret = new TextField(secret_dpf); /** 接口类型 */ private ChoiceBox cbImgToApiType = new ChoiceBox(FXCollections.observableArrayList(apiTypes)); /** 是否自动去除key值前缀中的目录 */ ToggleButton tbKeyModify = new ToggleButton(keyValueTip+"-开"); /** 执行校验按钮 */ private Button btnValid = new Button("参数校验"); /** 文件传输执行按钮 */ private Button btnExecute = new Button("开始传输"); /** 执行信息 */ private Label lblExecute_sts = new Label(); /** 提示信息 */ private Label lblTips_sts = new Label(); private ProgressBar progressBar_sts = new ProgressBar(0); public static void main(String[] args) { launch(args); } /** * 初始化基础控件 */ private void initComponents() { bindDTO(); tfDPF.setPrefWidth(appWidth * .7); tfPjId.setPrefWidth(appWidth * .25); tfPjSecret.setPrefWidth(appWidth * .25); cbObjType.setItems(FXCollections.observableArrayList(objTypes)); cbObjType.setPrefWidth(appWidth * .1); // 设置默认选择第一项 cbObjType.getSelectionModel().selectFirst(); cbObjInfoCodeArray.setItems(FXCollections.observableArrayList(objToInfoCode.get(cbObjType.getSelectionModel().getSelectedItem().toString()))); cbObjInfoCodeArray.getSelectionModel().selectFirst(); cbObjInfoCodeArray.setPrefWidth(appWidth * .25); tfImgFromURL.setPrefWidth(appWidth * .5); tfImgFromSysId.setPrefWidth(appWidth * .25); tfImgFromSecret.setPrefWidth(appWidth * .25); cbImgFromApiType.setPrefWidth(appWidth * .15); cbImgFromApiType.getSelectionModel().selectFirst(); tfImgToURL.setPrefWidth(appWidth * .5); tfImgToSysId.setPrefWidth(appWidth * .25); tfImgToSecret.setPrefWidth(appWidth * .25); cbImgToApiType.setPrefWidth(appWidth * .15); cbImgToApiType.getSelectionModel().select(1); btnValid.setPrefSize(appWidth * .15, 30); btnExecute.setPrefSize(appWidth * .15, 30); progressBar_sts.setPrefWidth(appWidth * .55); tbKeyModify.setSelected(true); tbKeyModify.setStyle("-fx-base: #75bd43"); tbKeyModify.setOnAction((ob)->{ if(tbKeyModify.isSelected()) { tbKeyModify.setText(keyValueTip+"-开"); tbKeyModify.setStyle("-fx-base: #75bd43"); }else { tbKeyModify.setText(keyValueTip+"-关"); tbKeyModify.setStyle("-fx-base: #bbbbbb"); } }); // 点击验证参数按钮 btnValid.setOnAction(e -> { disableSet(true); lblTips_sts.textProperty().unbind(); lblTips_sts.setText(""); lblExecute_sts.textProperty().unbind(); progressBar_sts.progressProperty().unbind(); progressBar_sts.setProgress(0); showTipsInfo("校验参数..."); Task worker = WorkerFactory.getValidParamWorker(this); progressBar_sts.progressProperty().bind(worker.progressProperty()); new Thread(worker).start(); }); // 点击文件迁移按钮 btnExecute.setOnAction(e -> { disableSet(true); lblTips_sts.textProperty().unbind(); lblTips_sts.setText(""); lblExecute_sts.textProperty().unbind(); lblExecute_sts.setText(""); progressBar_sts.progressProperty().unbind(); progressBar_sts.setProgress(-1); Task worker = WorkerFactory.getTransRateWorker(this); lblTips_sts.textProperty().bind(worker.titleProperty()); lblExecute_sts.textProperty().bind(worker.messageProperty()); progressBar_sts.progressProperty().bind(worker.progressProperty()); new Thread(worker).start(); }); cbObjType.getSelectionModel().selectedIndexProperty().addListener((obv, ov, nv)->{ String objType = cbObjType.getItems().get(nv.intValue()) + ""; cbObjInfoCodeArray.setItems(FXCollections.observableArrayList(objToInfoCode.get(objType))); cbObjInfoCodeArray.getSelectionModel().selectFirst(); if("Fl".equals(objType)) { tfImgFromSysId.setText(sysId_rvt); tfImgFromSecret.setText(secret_rvt); }else { tfImgFromSysId.setText(sysId_dpf); tfImgFromSecret.setText(secret_dpf); } }); } /** * 设置视图中的所有控件是否为不可编辑状态 * @param disable true-控件不可编辑;false-控件可编辑 */ public void disableSet(boolean disable) { Field[] allFields = SagaFileMoveApp.class.getDeclaredFields(); for(Field field : allFields) { if(field.getName().endsWith("_sts")) { continue; } try { if(Node.class.isAssignableFrom(field.get(this).getClass())) { Node node = (Node) field.get(this); node.setDisable(disable); } }catch (Exception e) {} } } public void showTipsSuccess(String info) { lblExecute_sts.setTextFill(Color.DARKGREEN); lblExecute_sts.setText(info); } public void showTipsInfo(String info) { lblExecute_sts.setTextFill(Color.BLACK); lblExecute_sts.setText(info); } public void showTipsError(String errMsg) { lblExecute_sts.setTextFill(Color.RED); lblExecute_sts.setText(errMsg); } public void showTipsWarn(String warnMsg) { lblExecute_sts.setTextFill(Color.DARKORANGE); lblExecute_sts.setText(warnMsg); } /** * 将控件的值绑定到DTO对象的属性上 */ private void bindDTO() { dto.dpf.bind(tfDPF.textProperty()); dto.imgFromApiType.bind(cbImgFromApiType.valueProperty()); dto.imgFromSecret.bind(tfImgFromSecret.textProperty()); dto.imgFromSysId.bind(tfImgFromSysId.textProperty()); dto.imgFromUrl.bind(tfImgFromURL.textProperty()); dto.imgToApiType.bind(cbImgToApiType.valueProperty()); dto.imgToSecret.bind(tfImgToSecret.textProperty()); dto.imgToSysId.bind(tfImgToSysId.textProperty()); dto.imgToUrl.bind(tfImgToURL.textProperty()); dto.objInfoCode.bind(cbObjInfoCodeArray.valueProperty()); dto.objType.bind(cbObjType.valueProperty()); dto.pjId.bind(tfPjId.textProperty()); dto.pjSecret.bind(tfPjSecret.textProperty()); dto.keyModify.bind(tbKeyModify.selectedProperty()); } @Override public void start(Stage primaryStage) throws Exception { initComponents(); // 底层border布局 BorderPane baseBorderPane = new BorderPane(); // 菜单栏 MenuBar menuBar = new MenuBar(); menuBar.prefWidthProperty().bind(primaryStage.widthProperty()); menuBar.getMenus().addAll(getHelpMenu()); // 主视图内容布局 Pane centerPane = getCenterPane(); baseBorderPane.setTop(menuBar); baseBorderPane.setCenter(centerPane); primaryStage.setResizable(false); Scene scene = new Scene(baseBorderPane, appWidth, appHeight); primaryStage.setScene(scene); primaryStage.show(); } /** * 获取主要内容的布局面板实例 * @return */ private Pane getCenterPane() { Pane centerPane = new VBox(); centerPane.setPadding(new Insets(0, 5, 0, 5)); TitledPane paneObjParam = getObjTitlePane(); TitledPane paneImgFrom = getImgTitlePane(true); TitledPane paneImgTo = getImgTitlePane(false); Pane paneExe = getExePane(); Pane paneConsole = getConsolePane(); centerPane.getChildren().addAll(paneObjParam, paneImgFrom, paneImgTo, paneExe, paneConsole); Insets nodeInsets = new Insets(0, 0, 15, 0); VBox.setMargin(paneObjParam, nodeInsets); VBox.setMargin(paneImgFrom, nodeInsets); return centerPane; } /** * 获取执行控制面板 * @return */ private Pane getExePane() { HBox contentBox = new HBox(btnValid, btnExecute, progressBar_sts, lblTips_sts); HBox.setMargin(btnExecute, new Insets(0, 20, 0, 15)); HBox.setMargin(progressBar_sts, new Insets(0, 5, 0, 0)); contentBox.setAlignment(Pos.CENTER_LEFT); contentBox.setPadding(new Insets(15, 0, 0, 0)); return contentBox; } /** * 获取提示信息输出面板 * @return */ private Pane getConsolePane() { HBox contentBox = new HBox(lblExecute_sts); contentBox.setPadding(new Insets(15, 0, 0, 0)); return contentBox; } /** * 生成包含数据平台、项目、对象配置参数的带标题的布局实例 * @return */ private TitledPane getObjTitlePane() { String title = "物理对象 - 配置传输哪个项目,哪类对象,哪个信息点对应的文件数据"; String titlePaneBack = "-fx-background-color: wheat"; LinkedHashMap projectMap = new LinkedHashMap<>(); projectMap.put("*项目id ", tfPjId); projectMap.put("项目密码 ", tfPjSecret); LinkedHashMap objTypeMap = new LinkedHashMap<>(); objTypeMap.put("*对象类型 ", cbObjType); objTypeMap.put("*对象信息点 ", cbObjInfoCodeArray); VBox contentPane = new VBox(getHBoxPane("*数据平台 ", tfDPF), getHBoxPane(projectMap), getHBoxPane(objTypeMap)); contentPane.setPadding(new Insets(5, 0, 5,10)); contentPane.setStyle("-fx-background-color: antiquewhite"); TitledPane titledPane = new TitledPane(title, contentPane); titledPane.setCollapsible(false); return titledPane; } /** * 生成包含文件服务参数控件的带标题的布局实例 * @param isFrom true-数据来源方的文件服务;false-数据接收方的文件服务 * @return */ private TitledPane getImgTitlePane(boolean isFrom) { String titleFrom = "From:即获取文件资源数据所访问的image-service服务"; String titleTo = "To:即获上传文件资源所访问的image-service服务"; String title = "文件服务 - " + (isFrom ? titleFrom : titleTo); String titlePaneBack = "-fx-background-color: wheat"; Node systemId = null, secret = null, serviceURL = null, apiType; if(isFrom) { systemId = tfImgFromSysId; secret = tfImgFromSecret; serviceURL = tfImgFromURL; apiType = cbImgFromApiType; titlePaneBack = "-fx-background-color: bisque"; }else { systemId = tfImgToSysId; secret = tfImgToSecret; serviceURL = tfImgToURL; apiType = cbImgToApiType; } LinkedHashMap urlAndApiTypeMap = new LinkedHashMap<>(); urlAndApiTypeMap.put("*服务URL ", serviceURL); urlAndApiTypeMap.put("*接口类型 ", apiType); LinkedHashMap paramMap = new LinkedHashMap<>(); paramMap.put("*systemId ", systemId); paramMap.put("*secret ", secret); if(!isFrom) { paramMap.put("", tbKeyModify); } VBox contentPane = new VBox(getHBoxPane(urlAndApiTypeMap), getHBoxPane(paramMap)); contentPane.setPadding(new Insets(5, 0, 5,10)); contentPane.setStyle(titlePaneBack); TitledPane titledPane = new TitledPane(title, contentPane); titledPane.setCollapsible(false); return titledPane; } /** * 获取水平排布的含有Label和Node节点两个控件面板实例 * @param label 标签内容 * @param node 控件节点 * @return */ private Pane getHBoxPane(String label, Node node) { LinkedHashMap map = new LinkedHashMap<>(); map.put(label, node); return getHBoxPane(map); } /** * 获取水平排布的含多对(Label:Node)节点组的面板实例 * @param label_textField {标签名:控件节点} * @return */ private Pane getHBoxPane(LinkedHashMap label_textField) { HBox pane = new HBox(); Insets nodeInsets = new Insets(0, 15, 0, 0); for(String name : label_textField.keySet()) { Label label = new Label(name); Node node = label_textField.get(name); pane.getChildren().addAll(label, node); HBox.setMargin(node, nodeInsets); } pane.setPadding(new Insets(5, 5, 5, 0)); pane.setAlignment(Pos.CENTER_LEFT); return pane; } /** * 获取帮助菜单按钮 * @return */ private Menu getHelpMenu() { // 菜单项 MenuItem mi_help = new MenuItem("Help"); mi_help.setOnAction((e)->{ new FileMoveHelpDialog().showAndWait(); }); MenuItem mi_about = new MenuItem("About"); mi_about.setOnAction((e)->{ new MessageDialog("About", aboutInfo, 300, 120).showAndWait(); }); SeparatorMenuItem seline = new SeparatorMenuItem(); Menu helpMenu = new Menu("Help"); helpMenu.getItems().addAll(mi_help, seline, mi_about); return helpMenu; } }