Skip to content

Commit 1ce9cdb

Browse files
committed
优化了文件复制功能。
1 parent 09d5298 commit 1ce9cdb

7 files changed

Lines changed: 758 additions & 573 deletions

File tree

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

Lines changed: 193 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@
33
import com.xwintop.xJavaFxTool.model.FileCopyTableBean;
44
import com.xwintop.xJavaFxTool.services.littleTools.FileCopyService;
55
import com.xwintop.xJavaFxTool.utils.JavaFxViewUtil;
6+
import com.xwintop.xJavaFxTool.utils.XJavaFxSystemUtil;
67
import com.xwintop.xJavaFxTool.view.littleTools.FileCopyView;
78
import com.xwintop.xcore.util.javafx.FileChooserUtil;
8-
9-
import java.io.File;
10-
import java.net.URL;
11-
import java.util.ResourceBundle;
12-
9+
import com.xwintop.xcore.util.javafx.TooltipUtil;
1310
import javafx.beans.value.ChangeListener;
1411
import javafx.beans.value.ObservableValue;
1512
import javafx.collections.FXCollections;
@@ -25,178 +22,204 @@
2522
import javafx.scene.control.cell.PropertyValueFactory;
2623
import javafx.scene.control.cell.TextFieldTableCell;
2724
import javafx.scene.input.MouseButton;
25+
import lombok.Data;
26+
import lombok.extern.slf4j.Slf4j;
27+
28+
import java.io.File;
29+
import java.net.URL;
30+
import java.util.ResourceBundle;
2831

2932
/**
3033
* @ClassName: FileCopyController
3134
* @Description: 文件复制工具
3235
* @author: xufeng
3336
* @date: 2018/1/21 0021 1:08
3437
*/
35-
38+
@Slf4j
39+
@Data
3640
public class FileCopyController extends FileCopyView {
37-
private FileCopyService fileCopyService = new FileCopyService();
38-
private ObservableList<FileCopyTableBean> tableData = FXCollections.observableArrayList();
39-
private String[] quartzChoiceBoxStrings = new String[] { "简单表达式", "Cron表达式" };
40-
41-
@Override
42-
public void initialize(URL location, ResourceBundle resources) {
43-
initView();
44-
initEvent();
45-
}
46-
47-
private void initView() {
48-
fileCopyService.setTableData(tableData);
49-
fileCopyService.loadingConfigure();
50-
JavaFxViewUtil.setSpinnerValueFactory(spinnerCopyNumber, 1, Integer.MAX_VALUE);
51-
tableColumnCopyFileOriginalPath
52-
.setCellValueFactory(new PropertyValueFactory<FileCopyTableBean, String>("copyFileOriginalPath"));
53-
tableColumnCopyFileOriginalPath.setCellFactory(TextFieldTableCell.<FileCopyTableBean>forTableColumn());
54-
tableColumnCopyFileOriginalPath.setOnEditCommit((CellEditEvent<FileCopyTableBean, String> t) -> {
55-
t.getRowValue().setCopyFileOriginalPath(t.getNewValue());
56-
// ((FileCopyTableBean)
57-
// t.getTableView().getItems().get(t.getTablePosition().getRow()))
58-
// .setCopyFileOriginalPath(t.getNewValue());
59-
});
60-
61-
tableColumnCopyFileTargetPath
62-
.setCellValueFactory(new PropertyValueFactory<FileCopyTableBean, String>("copyFileTargetPath"));
63-
tableColumnCopyFileTargetPath.setCellFactory(TextFieldTableCell.<FileCopyTableBean>forTableColumn());
64-
tableColumnCopyFileTargetPath.setOnEditCommit((CellEditEvent<FileCopyTableBean, String> t) -> {
65-
t.getRowValue().setCopyFileTargetPath(t.getNewValue());
66-
});
67-
68-
tableColumnCopyNumber.setCellValueFactory(new PropertyValueFactory<FileCopyTableBean, String>("copyNumber"));
69-
tableColumnCopyNumber.setCellFactory(TextFieldTableCell.<FileCopyTableBean>forTableColumn());
70-
tableColumnCopyNumber.setOnEditCommit((CellEditEvent<FileCopyTableBean, String> t) -> {
71-
t.getRowValue().setCopyNumber(t.getNewValue());
72-
});
73-
74-
tableColumnIsCopy.setCellValueFactory(new PropertyValueFactory<FileCopyTableBean, Boolean>("isCopy"));
75-
tableColumnIsCopy.setCellFactory(CheckBoxTableCell.forTableColumn(tableColumnIsCopy));
76-
tableColumnIsRename.setCellValueFactory(new PropertyValueFactory<FileCopyTableBean, Boolean>("isRename"));
77-
tableColumnIsRename.setCellFactory(CheckBoxTableCell.forTableColumn(tableColumnIsRename));
78-
tableColumnIsDelete.setCellValueFactory(new PropertyValueFactory<FileCopyTableBean, Boolean>("isDelete"));
79-
tableColumnIsDelete.setCellFactory(CheckBoxTableCell.forTableColumn(tableColumnIsDelete));
80-
tableViewMain.setItems(tableData);
81-
82-
quartzChoiceBox.getItems().addAll(quartzChoiceBoxStrings);
83-
quartzChoiceBox.getSelectionModel().select(0);
84-
JavaFxViewUtil.setSpinnerValueFactory(intervalSpinner, 1, Integer.MAX_VALUE);
85-
JavaFxViewUtil.setSpinnerValueFactory(repeatCountSpinner, -1, Integer.MAX_VALUE);
86-
}
87-
88-
private void initEvent() {
89-
FileChooserUtil.setOnDrag(textFieldCopyFileOriginalPath, FileChooserUtil.FileType.FILE);
90-
FileChooserUtil.setOnDrag(textFieldCopyFileTargetPath, FileChooserUtil.FileType.FOLDER);
91-
tableData.addListener((Change<? extends FileCopyTableBean> tableBean)->{
92-
try {
93-
saveConfigure(null);
94-
} catch (Exception e) {
95-
e.printStackTrace();
96-
}
97-
});
98-
tableViewMain.setOnMouseClicked(event -> {
99-
if (event.getButton() == MouseButton.SECONDARY) {
100-
MenuItem menu_Copy = new MenuItem("复制选中行");
101-
menu_Copy.setOnAction(event1 -> {
102-
FileCopyTableBean tableBean = tableViewMain.getSelectionModel().getSelectedItem();
103-
FileCopyTableBean tableBean2= new FileCopyTableBean(tableBean.getPropertys());
104-
tableData.add(tableViewMain.getSelectionModel().getSelectedIndex(),tableBean2);
105-
});
106-
MenuItem menu_Remove = new MenuItem("删除选中行");
107-
menu_Remove.setOnAction(event1 -> {
108-
deleteSelectRowAction(null);
109-
});
110-
MenuItem menu_RemoveAll = new MenuItem("删除所有");
111-
menu_RemoveAll.setOnAction(event1 -> {
112-
tableData.clear();
113-
});
114-
tableViewMain.setContextMenu(new ContextMenu(menu_Copy,menu_Remove, menu_RemoveAll));
115-
}
116-
});
117-
quartzChoiceBox.valueProperty().addListener(new ChangeListener<String>() {
118-
@Override
119-
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
120-
if (quartzChoiceBoxStrings[0].equals(newValue)) {
121-
cronTextField.setVisible(false);
122-
simpleScheduleAnchorPane.setVisible(true);
123-
} else if (quartzChoiceBoxStrings[1].equals(newValue)) {
124-
cronTextField.setVisible(true);
125-
simpleScheduleAnchorPane.setVisible(false);
126-
}
127-
}
128-
});
129-
}
130-
131-
@FXML
132-
private void chooseOriginalPathAction(ActionEvent event) {
133-
File file = FileChooserUtil.chooseFile();
134-
if (file != null) {
135-
textFieldCopyFileOriginalPath.setText(file.getPath());
136-
}
137-
}
138-
139-
@FXML
140-
private void chooseTargetPathAction(ActionEvent event) {
141-
File file = FileChooserUtil.chooseDirectory();
142-
if (file != null) {
143-
textFieldCopyFileTargetPath.setText(file.getPath());
144-
}
145-
}
146-
147-
@FXML
148-
private void addItemAction(ActionEvent event) {
149-
tableData.add(new FileCopyTableBean(textFieldCopyFileOriginalPath.getText(),
150-
textFieldCopyFileTargetPath.getText(), spinnerCopyNumber.getValue().toString(),
151-
checkBoxIsCopy.isSelected(),checkBoxIsRename.isSelected(), checkBoxIsDelete.isSelected()));
152-
}
153-
154-
@FXML
155-
private void deleteSelectRowAction(ActionEvent event) {
156-
tableData.remove(tableViewMain.getSelectionModel().getSelectedItem());
157-
}
158-
159-
@FXML
160-
private void saveConfigure(ActionEvent event) throws Exception {
161-
fileCopyService.saveConfigure();
162-
}
163-
164-
@FXML
165-
private void copyAction(ActionEvent event) throws Exception {
166-
fileCopyService.copyAction();
167-
}
168-
169-
@FXML
170-
private void otherSaveConfigureAction(ActionEvent event) throws Exception {
171-
fileCopyService.otherSaveConfigureAction();
172-
}
173-
174-
@FXML
175-
private void loadingConfigureAction(ActionEvent event) {
176-
fileCopyService.loadingConfigureAction();
177-
}
178-
179-
@FXML
180-
private void runQuartzAction(ActionEvent event) throws Exception {
181-
if("定时运行".equals(runQuartzButton.getText())){
182-
boolean isTrue = fileCopyService.runQuartzAction(quartzChoiceBox.getValue(), cronTextField.getText(), intervalSpinner.getValue(),
183-
repeatCountSpinner.getValue());
184-
if(isTrue){
185-
runQuartzButton.setText("停止运行");
186-
}
187-
}else{
188-
boolean isTrue = fileCopyService.stopQuartzAction();
189-
if(isTrue){
190-
runQuartzButton.setText("定时运行");
191-
}
192-
}
193-
}
194-
195-
/**
196-
* 父控件被移除前调用
197-
*/
198-
public void onCloseRequest(Event event) throws Exception {
199-
fileCopyService.stopQuartzAction();
200-
}
41+
private FileCopyService fileCopyService = new FileCopyService();
42+
private ObservableList<FileCopyTableBean> tableData = FXCollections.observableArrayList();
43+
private String[] quartzChoiceBoxStrings = new String[]{"简单表达式", "Cron表达式"};
44+
45+
@Override
46+
public void initialize(URL location, ResourceBundle resources) {
47+
initView();
48+
initEvent();
49+
}
50+
51+
private void initView() {
52+
fileCopyService.setTableData(tableData);
53+
fileCopyService.loadingConfigure();
54+
JavaFxViewUtil.setSpinnerValueFactory(spinnerCopyNumber, 1, Integer.MAX_VALUE);
55+
tableColumnCopyFileOriginalPath.setCellValueFactory(new PropertyValueFactory<>("copyFileOriginalPath"));
56+
tableColumnCopyFileOriginalPath.setCellFactory(TextFieldTableCell.forTableColumn());
57+
tableColumnCopyFileOriginalPath.setOnEditCommit((CellEditEvent<FileCopyTableBean, String> t) -> {
58+
t.getRowValue().setCopyFileOriginalPath(t.getNewValue());
59+
// ((FileCopyTableBean) t.getTableView().getItems().get(t.getTablePosition().getRow())).setCopyFileOriginalPath(t.getNewValue());
60+
});
61+
62+
JavaFxViewUtil.setTableColumnButonFactory(tableColumnViewCopyFileOriginalPath, "查看", (me, index) -> {
63+
XJavaFxSystemUtil.openDirectory(tableData.get(index).getCopyFileOriginalPath());
64+
});
65+
66+
tableColumnCopyFileTargetPath.setCellValueFactory(new PropertyValueFactory<>("copyFileTargetPath"));
67+
tableColumnCopyFileTargetPath.setCellFactory(TextFieldTableCell.forTableColumn());
68+
tableColumnCopyFileTargetPath.setOnEditCommit((CellEditEvent<FileCopyTableBean, String> t) -> {
69+
t.getRowValue().setCopyFileTargetPath(t.getNewValue());
70+
});
71+
72+
JavaFxViewUtil.setTableColumnButonFactory(tableColumnViewCopyFileTargetPath, "查看", (me, index) -> {
73+
XJavaFxSystemUtil.openDirectory(tableData.get(index).getCopyFileTargetPath());
74+
});
75+
76+
tableColumnCopyNumber.setCellValueFactory(new PropertyValueFactory<>("copyNumber"));
77+
tableColumnCopyNumber.setCellFactory(TextFieldTableCell.<FileCopyTableBean>forTableColumn());
78+
tableColumnCopyNumber.setOnEditCommit((CellEditEvent<FileCopyTableBean, String> t) -> {
79+
t.getRowValue().setCopyNumber(t.getNewValue());
80+
});
81+
82+
tableColumnIsCopy.setCellValueFactory(new PropertyValueFactory<>("isCopy"));
83+
tableColumnIsCopy.setCellFactory(CheckBoxTableCell.forTableColumn(tableColumnIsCopy));
84+
tableColumnIsRename.setCellValueFactory(new PropertyValueFactory<>("isRename"));
85+
tableColumnIsRename.setCellFactory(CheckBoxTableCell.forTableColumn(tableColumnIsRename));
86+
tableColumnIsDelete.setCellValueFactory(new PropertyValueFactory<FileCopyTableBean, Boolean>("isDelete"));
87+
tableColumnIsDelete.setCellFactory(CheckBoxTableCell.forTableColumn(tableColumnIsDelete));
88+
tableColumnIsDeleteSourceFile.setCellValueFactory(new PropertyValueFactory<>("isDeleteSourceFile"));
89+
tableColumnIsDeleteSourceFile.setCellFactory(CheckBoxTableCell.forTableColumn(tableColumnIsDeleteSourceFile));
90+
JavaFxViewUtil.setTableColumnButonFactory(tableColumnRun, "运行", (me, index) -> {
91+
try {
92+
fileCopyService.copyAction(tableData.get(index));
93+
} catch (Exception e) {
94+
log.error("拷贝文件失败:" + e.getMessage());
95+
TooltipUtil.showToast("拷贝文件失败:" + e.getMessage());
96+
}
97+
});
98+
tableColumnRemarks.setCellValueFactory(new PropertyValueFactory<FileCopyTableBean, String>("remarks"));
99+
tableColumnRemarks.setCellFactory(TextFieldTableCell.<FileCopyTableBean>forTableColumn());
100+
tableColumnRemarks.setOnEditCommit((CellEditEvent<FileCopyTableBean, String> t) -> {
101+
t.getRowValue().setCopyNumber(t.getNewValue());
102+
});
103+
tableViewMain.setItems(tableData);
104+
105+
quartzChoiceBox.getItems().addAll(quartzChoiceBoxStrings);
106+
quartzChoiceBox.getSelectionModel().select(0);
107+
JavaFxViewUtil.setSpinnerValueFactory(intervalSpinner, 1, Integer.MAX_VALUE);
108+
JavaFxViewUtil.setSpinnerValueFactory(repeatCountSpinner, -1, Integer.MAX_VALUE);
109+
}
110+
111+
private void initEvent() {
112+
FileChooserUtil.setOnDrag(textFieldCopyFileOriginalPath, FileChooserUtil.FileType.FILE);
113+
FileChooserUtil.setOnDrag(textFieldCopyFileTargetPath, FileChooserUtil.FileType.FOLDER);
114+
tableData.addListener((Change<? extends FileCopyTableBean> tableBean) -> {
115+
try {
116+
saveConfigure(null);
117+
} catch (Exception e) {
118+
e.printStackTrace();
119+
}
120+
});
121+
tableViewMain.setOnMouseClicked(event -> {
122+
if (event.getButton() == MouseButton.SECONDARY) {
123+
MenuItem menu_Copy = new MenuItem("复制选中行");
124+
menu_Copy.setOnAction(event1 -> {
125+
FileCopyTableBean tableBean = tableViewMain.getSelectionModel().getSelectedItem();
126+
FileCopyTableBean tableBean2 = new FileCopyTableBean(tableBean.getPropertys());
127+
tableData.add(tableViewMain.getSelectionModel().getSelectedIndex(), tableBean2);
128+
});
129+
MenuItem menu_Remove = new MenuItem("删除选中行");
130+
menu_Remove.setOnAction(event1 -> {
131+
deleteSelectRowAction(null);
132+
});
133+
MenuItem menu_RemoveAll = new MenuItem("删除所有");
134+
menu_RemoveAll.setOnAction(event1 -> {
135+
tableData.clear();
136+
});
137+
tableViewMain.setContextMenu(new ContextMenu(menu_Copy, menu_Remove, menu_RemoveAll));
138+
}
139+
});
140+
quartzChoiceBox.valueProperty().addListener(new ChangeListener<String>() {
141+
@Override
142+
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
143+
if (quartzChoiceBoxStrings[0].equals(newValue)) {
144+
cronTextField.setVisible(false);
145+
simpleScheduleAnchorPane.setVisible(true);
146+
} else if (quartzChoiceBoxStrings[1].equals(newValue)) {
147+
cronTextField.setVisible(true);
148+
simpleScheduleAnchorPane.setVisible(false);
149+
}
150+
}
151+
});
152+
}
153+
154+
@FXML
155+
private void chooseOriginalPathAction(ActionEvent event) {
156+
File file = FileChooserUtil.chooseFile();
157+
if (file != null) {
158+
textFieldCopyFileOriginalPath.setText(file.getPath());
159+
}
160+
}
161+
162+
@FXML
163+
private void chooseTargetPathAction(ActionEvent event) {
164+
File file = FileChooserUtil.chooseDirectory();
165+
if (file != null) {
166+
textFieldCopyFileTargetPath.setText(file.getPath());
167+
}
168+
}
169+
170+
@FXML
171+
private void addItemAction(ActionEvent event) {
172+
tableData.add(new FileCopyTableBean(textFieldCopyFileOriginalPath.getText(),
173+
textFieldCopyFileTargetPath.getText(), spinnerCopyNumber.getValue().toString(),
174+
checkBoxIsCopy.isSelected(), checkBoxIsRename.isSelected(), checkBoxIsDelete.isSelected(), true, ""));
175+
}
176+
177+
@FXML
178+
private void deleteSelectRowAction(ActionEvent event) {
179+
tableData.remove(tableViewMain.getSelectionModel().getSelectedItem());
180+
}
181+
182+
@FXML
183+
private void saveConfigure(ActionEvent event) throws Exception {
184+
fileCopyService.saveConfigure();
185+
}
186+
187+
@FXML
188+
private void copyAction(ActionEvent event) throws Exception {
189+
fileCopyService.copyAction();
190+
}
191+
192+
@FXML
193+
private void otherSaveConfigureAction(ActionEvent event) throws Exception {
194+
fileCopyService.otherSaveConfigureAction();
195+
}
196+
197+
@FXML
198+
private void loadingConfigureAction(ActionEvent event) {
199+
fileCopyService.loadingConfigureAction();
200+
}
201+
202+
@FXML
203+
private void runQuartzAction(ActionEvent event) throws Exception {
204+
if ("定时运行".equals(runQuartzButton.getText())) {
205+
boolean isTrue = fileCopyService.runQuartzAction(quartzChoiceBox.getValue(), cronTextField.getText(), intervalSpinner.getValue(),
206+
repeatCountSpinner.getValue());
207+
if (isTrue) {
208+
runQuartzButton.setText("停止运行");
209+
}
210+
} else {
211+
boolean isTrue = fileCopyService.stopQuartzAction();
212+
if (isTrue) {
213+
runQuartzButton.setText("定时运行");
214+
}
215+
}
216+
}
217+
218+
/**
219+
* 父控件被移除前调用
220+
*/
221+
public void onCloseRequest(Event event) throws Exception {
222+
fileCopyService.stopQuartzAction();
223+
}
201224

202225
}

0 commit comments

Comments
 (0)