Skip to content

Commit ff3a35b

Browse files
author
xuxiaoyan
committed
优化跨平台性。
1 parent 1ccb70c commit ff3a35b

3 files changed

Lines changed: 63 additions & 54 deletions

File tree

src/main/java/com/xwintop/xJavaFxTool/controller/littleTools/LinuxPathToWindowsPathController.java

Lines changed: 55 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.net.URL;
55
import java.util.ResourceBundle;
66

7+
import com.sun.jna.Platform;
78
import org.apache.commons.io.FileUtils;
89

910
import com.xwintop.xcore.util.javafx.AlertUtil;
@@ -27,59 +28,63 @@
2728
*/
2829

2930
public class LinuxPathToWindowsPathController implements Initializable {
30-
@FXML
31-
private TextField textFieldLinuxPath;
32-
@FXML
33-
private ChoiceBox<String> choiceBoxChooseLUN;
34-
@FXML
35-
private Button buttonCreateWindowsPath;
36-
@FXML
37-
private TextField textFieldWindowsPath;
38-
@FXML
39-
private Button buttonCreateLinuxPath;
31+
@FXML
32+
private TextField textFieldLinuxPath;
33+
@FXML
34+
private ChoiceBox<String> choiceBoxChooseLUN;
35+
@FXML
36+
private Button buttonCreateWindowsPath;
37+
@FXML
38+
private TextField textFieldWindowsPath;
39+
@FXML
40+
private Button buttonCreateLinuxPath;
4041

41-
@Override
42-
public void initialize(URL location, ResourceBundle resources) {
43-
initView();
44-
initEvent();
45-
}
42+
@Override
43+
public void initialize(URL location, ResourceBundle resources) {
44+
initView();
45+
initEvent();
46+
}
4647

47-
private void initView() {
48-
ObservableList<String> observableList = choiceBoxChooseLUN.getItems();
49-
for (char c = 'A'; c <= 'Z'; c++) {
50-
String dirName = c + ":/";
51-
File win = new File(dirName);
52-
if (win.exists()) {
53-
observableList.add(dirName);
54-
}
55-
}
56-
choiceBoxChooseLUN.setValue(observableList.get(0));
57-
}
58-
59-
private void initEvent() {
60-
textFieldLinuxPath.setOnKeyReleased(new EventHandler<KeyEvent>() {
61-
@Override
62-
public void handle(KeyEvent event) {
63-
if(event.getCode() == KeyCode.ENTER){
64-
createWindowsPath(null);
65-
}
66-
}
67-
});
68-
}
48+
private void initView() {
49+
if (Platform.isWindows()) {
50+
ObservableList<String> observableList = choiceBoxChooseLUN.getItems();
51+
for (char c = 'A'; c <= 'Z'; c++) {
52+
String dirName = c + ":/";
53+
File win = new File(dirName);
54+
if (win.exists()) {
55+
observableList.add(dirName);
56+
}
57+
}
58+
} else {
59+
choiceBoxChooseLUN.getItems().add("/");
60+
}
61+
choiceBoxChooseLUN.setValue(choiceBoxChooseLUN.getItems().get(0));
62+
}
6963

70-
@FXML
71-
private void createWindowsPath(ActionEvent event){
72-
try {
73-
String folderPath = choiceBoxChooseLUN.getValue()+textFieldLinuxPath.getText();
74-
FileUtils.forceMkdir(new File(folderPath));
75-
textFieldWindowsPath.setText(folderPath);
76-
} catch (Exception e) {
77-
AlertUtil.showWarnAlert("转换异常,请检查路径。");
78-
}
79-
}
64+
private void initEvent() {
65+
textFieldLinuxPath.setOnKeyReleased(new EventHandler<KeyEvent>() {
66+
@Override
67+
public void handle(KeyEvent event) {
68+
if (event.getCode() == KeyCode.ENTER) {
69+
createWindowsPath(null);
70+
}
71+
}
72+
});
73+
}
8074

81-
@FXML
82-
private void createLinuxPath(ActionEvent event) {
83-
}
75+
@FXML
76+
private void createWindowsPath(ActionEvent event) {
77+
try {
78+
String folderPath = choiceBoxChooseLUN.getValue() + textFieldLinuxPath.getText();
79+
FileUtils.forceMkdir(new File(folderPath));
80+
textFieldWindowsPath.setText(folderPath);
81+
} catch (Exception e) {
82+
AlertUtil.showWarnAlert("转换异常,请检查路径。");
83+
}
84+
}
85+
86+
@FXML
87+
private void createLinuxPath(ActionEvent event) {
88+
}
8489

8590
}

src/main/java/com/xwintop/xJavaFxTool/utils/JavaFxViewUtil.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.apache.commons.lang3.reflect.MethodUtils;
3535

3636
import java.awt.*;
37+
import java.lang.reflect.Method;
3738
import java.text.DecimalFormat;
3839
import java.util.HashMap;
3940
import java.util.Map;
@@ -140,9 +141,12 @@ public static Stage getNewStage(String title, String iconUrl, FXMLLoader fXMLLoa
140141
//设置窗口移除前回调
141142
public static void setControllerOnCloseRequest(Object controller, Event event) {
142143
try {
143-
MethodUtils.invokeMethod(controller, "onCloseRequest", event);
144+
Method method = MethodUtils.getAccessibleMethod(controller.getClass(), "onCloseRequest", event.getClass());
145+
if (method != null) {
146+
MethodUtils.invokeMethod(controller, "onCloseRequest", event);
147+
}
144148
} catch (Exception e) {
145-
log.error(e.getMessage());
149+
log.error("执行onCloseRequest方法失败", e);
146150
}
147151
}
148152

src/main/java/com/xwintop/xJavaFxTool/utils/XJavaFxSystemUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.xwintop.xJavaFxTool.utils;
22

33
import com.xwintop.xJavaFxTool.model.ToolFxmlLoaderConfiguration;
4+
import lombok.extern.slf4j.Slf4j;
45
import org.apache.commons.beanutils.BeanUtils;
56
import org.apache.commons.configuration.PropertiesConfiguration;
67
import org.apache.commons.configuration.XMLConfiguration;
@@ -35,9 +36,8 @@
3536
* @author: xufeng
3637
* @date: 2017年11月10日 下午4:35:17
3738
*/
39+
@Slf4j
3840
public class XJavaFxSystemUtil {
39-
private static Logger log = Logger.getLogger(XJavaFxSystemUtil.class);
40-
4141
/**
4242
* @Title: initSystemLocal
4343
* @Description: 初始化本地语言

0 commit comments

Comments
 (0)