diff --git a/main/src/controller/RenameController.java b/main/src/controller/RenameController.java index 70541f79fb464b7ce0d0539e44a383db046ae9b0..b36d77a3649f328dd5ca80b041773dc9b6578c61 100644 --- a/main/src/controller/RenameController.java +++ b/main/src/controller/RenameController.java @@ -2,12 +2,17 @@ package controller; import javafx.event.ActionEvent; import javafx.fxml.FXML; +import javafx.scene.Group; +import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; +import javafx.scene.layout.AnchorPane; +import javafx.scene.layout.Pane; import javafx.stage.Stage; import static controller.MainController.*; +import static operations.renameOperations.isAbleToRename; public class RenameController { @@ -33,14 +38,28 @@ public class RenameController { @FXML void renameConfirm(ActionEvent event) { - currentFile.setFileName(renameField.getText()); - icons.get(currentFile).setText(currentFile.getFileName()); - if (currentFile.getParent() == rootFile) { - currentFile = rootFile; + if (isAbleToRename(currentFile, renameField.getText())) { + currentFile.setFileName(renameField.getText()); + icons.get(currentFile).setText(currentFile.getFileName()); + if (currentFile.getParent() == rootFile) { + currentFile = rootFile; + } else { + currentFile = currentFile.getParent(); + } + Stage stage = (Stage) renameField.getScene().getWindow(); + stage.close(); } else { - currentFile = currentFile.getParent(); + AnchorPane anchorPane = new AnchorPane(); + Scene scene = new Scene(anchorPane, 200, 20); + Label label = new Label("文件不能重名!"); + label.setLayoutX(65); + label.setLayoutY(5); + anchorPane.getChildren().add(label); + Stage stage = new Stage(); + stage.setScene(scene); + stage.setTitle("错误"); + stage.setResizable(false); + stage.show(); } - Stage stage = (Stage) renameField.getScene().getWindow(); - stage.close(); } } diff --git a/main/src/operations/renameOperations.java b/main/src/operations/renameOperations.java index ffbfbbd796706be7e17ebf3188f072418d6ea102..db1b1821c0f674d1f052d7616aa12fcc7adc770f 100644 --- a/main/src/operations/renameOperations.java +++ b/main/src/operations/renameOperations.java @@ -1,5 +1,6 @@ package operations; +import Data.File; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.stage.Stage; @@ -21,4 +22,16 @@ public class renameOperations { stage.setScene(scene); stage.show(); } + + public static boolean isAbleToRename(File file, String renameStr) { + for (File file1 : file.getParent().getChildrenFile()) { + if (file1 == file) { + continue; + } + if (file1.getFileName().equals(renameStr)) { + return false; + } + } + return true; + } }