diff --git a/main/src/Data/FAT.java b/main/src/Data/FAT.java index 1fc362b0e0ef85c9451b2b41e4bc9252eb39de96..ef79cbcf12e50fe11e25002c4f10c2f8a8d829e6 100644 --- a/main/src/Data/FAT.java +++ b/main/src/Data/FAT.java @@ -31,6 +31,7 @@ public class FAT implements Serializable { } } int needNum = file.getNeed(); + System.out.println("needNum:" + needNum); if(needNum <= sum){ return true; }else { @@ -38,10 +39,6 @@ public class FAT implements Serializable { } } public void applySpace(File file){ - if(!judge(file)){ - System.out.println("磁盘空间不足"); - return; - } int neededDiskNum = file.getLength(); int record = neededDiskNum ; int previous = 0; diff --git a/main/src/Data/File.java b/main/src/Data/File.java index b1fe257f39d159561b24855bfaf282f47a97ec66..2353fc662df11f1fbfbae8c11bfa514aee36986e 100644 --- a/main/src/Data/File.java +++ b/main/src/Data/File.java @@ -110,8 +110,7 @@ public class File implements Serializable { } public int getLength() { - getSize(); - length = content.length()/64; + length = (content.length())/64; if(content.length()%64 != 0){ length += 1; } @@ -259,7 +258,8 @@ public class File implements Serializable { } public int getNeed(){ - return length - getLength(); + int frontLength = length; + return getLength() - frontLength; } diff --git a/main/src/controller/FileController.java b/main/src/controller/FileController.java index 53b2bb819260d087553a3bf03f789e2254c13d1c..790684095a4a3c2f4f2d6e47192491b08248d60e 100644 --- a/main/src/controller/FileController.java +++ b/main/src/controller/FileController.java @@ -3,6 +3,7 @@ package controller; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.chart.PieChart; +import javafx.scene.control.Alert; import javafx.scene.control.Button; import javafx.scene.control.TextArea; import javafx.scene.control.ToolBar; @@ -45,9 +46,24 @@ public class FileController { @FXML void saveContent(ActionEvent event) throws IOException, ClassNotFoundException { if (currentFile.isReadOnly()) { - saveItem.setDisable(true); + contentField.setDisable(true); } else { + String front = currentFile.getContent(); currentFile.setContent(contentField.getText()); + if(!fat.judge(currentFile)){ + currentFile.setContent(front); + Alert alert = new Alert(Alert.AlertType.WARNING); + alert.setContentText("磁盘空间不足"); + alert.show(); + if (currentFile.getParent() == rootFile) { + currentFile = rootFile; + } else { + currentFile = currentFile.getParent(); + } + Stage stage = (Stage) contentField.getScene().getWindow(); + stage.close(); + return; + } fat.applySpace(currentFile); if (currentFile.getParent() == rootFile) { currentFile = rootFile; @@ -59,6 +75,7 @@ public class FileController { UpdatePieChart.updatePieChart(diskChart); writeDisk(); } + currentFile.setOpen(false); } public static void setDiskChart(PieChart Chart){ diff --git a/main/src/controller/MainController.java b/main/src/controller/MainController.java index 7d95a74316061e6260096e276937ea300b0f63b6..dc9b38b5b9aee3226203498596ab225559d55e8b 100644 --- a/main/src/controller/MainController.java +++ b/main/src/controller/MainController.java @@ -320,6 +320,18 @@ public class MainController { //新建文件 public void menuItemAction(){ createFileItem.setOnAction(ActionEvent -> { + int sum = 0; + for(int i = 2; i < 128; i++){ + if(fat.getDiskBlocks()[i].getNextDiskNum() == 0){ + sum++; + } + } + if(sum == 0){ + Alert alert = new Alert(Alert.AlertType.WARNING); + alert.setContentText("磁盘空间不足"); + alert.show(); + return; + } File file = createFileOperations.createFile(fileExplorer,icons,actionContextMenu); TreeViewIterator treeViewIterator = new TreeViewIterator(); treeViewIterator.Clear(); @@ -335,6 +347,18 @@ public class MainController { }); createFolderItem.setOnAction(ActionEvent -> { + int sum = 0; + for(int i = 2; i < 128; i++){ + if(fat.getDiskBlocks()[i].getNextDiskNum() == 0){ + sum++; + } + } + if(sum == 0){ + Alert alert = new Alert(Alert.AlertType.WARNING); + alert.setContentText("磁盘空间不足"); + alert.show(); + return; + } File folder = createFileOperations.createFolder(fileExplorer,icons,actionContextMenu); TreeViewIterator treeViewIterator = new TreeViewIterator(); treeViewIterator.Clear(); @@ -396,10 +420,18 @@ public class MainController { actionContextMenu.getItems().get(0).setOnAction(ActionEvent -> { currentFile = FindLabelToFile.findFile(icons, src); if(currentFile != null) { + if (currentFile.isFolder()) { openOperations.openFolder(fileExplorer, currentFile, icons); } else { + if(currentFile.isOpen()){ + Alert alert = new Alert(Alert.AlertType.WARNING); + alert.setContentText("文件已打开!"); + alert.show(); + return; + } try { + currentFile.setOpen(true); openOperations.openFile(); } catch (IOException e) { throw new RuntimeException(e); @@ -411,16 +443,23 @@ public class MainController { actionContextMenu.getItems().get(1).setOnAction(ActionEvent -> { currentFile = FindLabelToFile.findFile(icons, src); File deleteFile = currentFile; - currentFile = currentFile.getParent(); - if (currentFile != rootFile && currentFile.isFolder()) { - openOperations.openFolder(fileExplorer, currentFile, icons); - } - deleteOperations.deleteFile(fileExplorer, icons, deleteFile,fat); - updatePieChart(diskChart); - try { - writeDisk(); - } catch (IOException | ClassNotFoundException e) { - throw new RuntimeException(e); + if(!deleteFile.isOpen()) { + currentFile = currentFile.getParent(); + if (currentFile != rootFile && currentFile.isFolder()) { + openOperations.openFolder(fileExplorer, currentFile, icons); + } + deleteOperations.deleteFile(fileExplorer, icons, deleteFile, fat); + updatePieChart(diskChart); + try { + writeDisk(); + } catch (IOException | ClassNotFoundException e) { + throw new RuntimeException(e); + } + }else { + System.out.println("1"); + Alert alert = new Alert(Alert.AlertType.WARNING); + alert.setContentText("文件未关闭!"); + alert.show(); } }); //右键重命名文件 @@ -456,7 +495,14 @@ public class MainController { String pathName = getAbsPath(currentFile); pathText.setText(pathName); } else { + if(currentFile.isOpen()){ + Alert alert = new Alert(Alert.AlertType.WARNING); + alert.setContentText("文件已打开!"); + alert.show(); + return; + } try { + currentFile.setOpen(true); openOperations.openFile(); } catch (IOException e) { throw new RuntimeException(e); diff --git a/main/src/operations/openOperations.java b/main/src/operations/openOperations.java index 41040f4093c3a38fe062fcf522469915be8246bc..f6889044503995dc4f7d2fab1662e2418428be8b 100644 --- a/main/src/operations/openOperations.java +++ b/main/src/operations/openOperations.java @@ -53,11 +53,14 @@ public class openOperations { stage.setResizable(false); stage.setScene(scene); stage.setOnCloseRequest(event -> { + currentFile.setOpen(false); if (currentFile.getParent() == rootFile) { currentFile = rootFile; } else { currentFile = currentFile.getParent(); } + + }); stage.show(); }