From 04830a05c0071cc444a95cc81751aa2eeec5bfc2 Mon Sep 17 00:00:00 2001
From: AckerMen <835340812@qq.com>
Date: Mon, 20 Nov 2023 22:33:01 +0800
Subject: [PATCH] =?UTF-8?q?treeView=E7=BB=84=E4=BB=B6=E6=9B=B4=E6=96=B0?=
=?UTF-8?q?=EF=BC=88=E9=87=8D=E5=91=BD=E5=90=8D=E3=80=81=E5=88=A0=E9=99=A4?=
=?UTF-8?q?=E3=80=81=E6=96=B0=E5=BB=BA=EF=BC=89?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
main/resources/Main-view.fxml | 1 +
main/resources/icon/disk.png | Bin 0 -> 332 bytes
main/resources/icon/img.png | Bin 0 -> 173 bytes
main/src/Data/File.java | 15 +++
main/src/controller/MainController.java | 29 +++++-
main/src/controller/RenameController.java | 11 +++
main/src/util/TreeViewIterator.java | 108 ++++++++++++++++++++++
7 files changed, 162 insertions(+), 2 deletions(-)
create mode 100644 main/resources/icon/disk.png
create mode 100644 main/resources/icon/img.png
create mode 100644 main/src/util/TreeViewIterator.java
diff --git a/main/resources/Main-view.fxml b/main/resources/Main-view.fxml
index eaffc8b..4390376 100644
--- a/main/resources/Main-view.fxml
+++ b/main/resources/Main-view.fxml
@@ -101,6 +101,7 @@
+
diff --git a/main/resources/icon/disk.png b/main/resources/icon/disk.png
new file mode 100644
index 0000000000000000000000000000000000000000..177a739e8329633591dfa1c450fa91db5faeef22
GIT binary patch
literal 332
zcmV-S0ki&zP)Px$21!IgR5(wi)4wZ6Q5?qc*EeOb87)doY)aVVPw=Z`VzJo2*(hc6YhW@frA){q
zQT_mAlS#}r1ATPpbaAhnv)$Wy-p}X9a~l468kzpBChJ361mxcXn8z#bu!rYl5@w}<
zUCdztZRkY+r*Ml>zzlk@Txcn(nZ-~Q;0PNyO@MF@*Eok*#Z_4gkLXB(B}`%6CL
eEz}Wz-F*k1DnJpC&3upm0000}^&k8DzCZf^c-8;^^Dimz$%p^_T)(?Q^-#m#P3sLr
z1P?jbhL}9q@=S||wZ$Od{0c@5wiVNpG!$C=S4}_2+aR&%C94RJDi`zBMHzAd3=9F+
W=Qie+^R@x)XYh3Ob6Mw<&;$T8@;yEP
literal 0
HcmV?d00001
diff --git a/main/src/Data/File.java b/main/src/Data/File.java
index c723ce0..1dc0cce 100644
--- a/main/src/Data/File.java
+++ b/main/src/Data/File.java
@@ -148,4 +148,19 @@ public class File implements Serializable {
else return "普通文件";
}
}
+
+ /**
+ * 是否存在子节点
+ * @return 如果非空文件夹返回true
+ */
+ public boolean hasChild() {
+ return !childrenFile.isEmpty();
+ }
+ /**
+ * 是否非根目录
+ * @return 根目录返回false
+ */
+ public boolean hasParent() {
+ return parent != null;
+ }
}
diff --git a/main/src/controller/MainController.java b/main/src/controller/MainController.java
index 1f43833..e414690 100644
--- a/main/src/controller/MainController.java
+++ b/main/src/controller/MainController.java
@@ -34,6 +34,7 @@ import operations.deleteOperations;
import operations.openOperations;
import util.*;
+import static controller.RenameController.sentParam;
import static operations.renameOperations.renameFile;
public class MainController {
@@ -73,7 +74,7 @@ public class MainController {
private AnchorPane fileExplorerPane;
@FXML
- private TreeView> fileView;
+ private TreeView fileView;
@FXML
private ImageView frontBtn;
@@ -105,6 +106,9 @@ public class MainController {
@FXML
private Label rwLabel;
+ @FXML
+ private Label rwMes;
+
@FXML
private Label sizeLabel;
@@ -132,6 +136,7 @@ public class MainController {
// 更改文件属性
private MenuItem changItem;
public static File rootFile;
+ public static TreeItem rootNode;
public static File currentFile;
Path rootPath;
Path currentPath;
@@ -152,6 +157,9 @@ public class MainController {
contextMenuInit();
menuItemAction();
rootFile = new File(false,true,false,true,"c:",null);
+ rootNode = new TreeItem("C:",new ImageView("icon/disk.png"));
+ fileView.setRoot(rootNode);
+ rootNode.setExpanded(true);
currentFile = rootFile;
fileExplorer.setAlignment(Pos.TOP_LEFT);
fileExplorer.setHgap(15);
@@ -203,6 +211,11 @@ public class MainController {
typeMes.setText("文件类型:" + newFile.getTypeName());
icon = new Image("icon/文件夹.png");
}
+ if(newFile.getTypeName().equals("普通文件") || newFile.getTypeName().equals("系统文件")){
+ if (newFile.isReadOnly()) {
+ rwMes.setText("文件属性:只读");
+ } else rwMes.setText("文件属性:读写");
+ }
iconMes.setImage(icon);
memoryMes.setText("占用空间:" + newFile.getSize());
// timeMes.setText("建立时间:" + newFile.getTime());
@@ -211,6 +224,7 @@ public class MainController {
public void removeFilePreview(File newFile){
nameMes.setText("");
typeMes.setText("");
+ rwMes.setText("");
iconMes.setVisible(false);
memoryMes.setText("");
timeMes.setText("");
@@ -236,15 +250,21 @@ public class MainController {
}
});
}
-
+//新建文件
public void menuItemAction(){
createFileItem.setOnAction(ActionEvent -> {
File file = createFileOperations.createFile(fileExplorer,icons,actionContextMenu);
+ TreeViewIterator treeViewIterator = new TreeViewIterator();
+ treeViewIterator.Clear();
+ treeViewIterator.iterator(fileView);
LabelMouseEventForFile(icons.get(file));
});
createFolderItem.setOnAction(ActionEvent -> {
File folder = createFileOperations.createFolder(fileExplorer,icons,actionContextMenu);
+ TreeViewIterator treeViewIterator = new TreeViewIterator();
+ treeViewIterator.Clear();
+ treeViewIterator.iterator(fileView);
LabelMouseEventForFolder(icons.get(folder));
});
}
@@ -316,6 +336,7 @@ public class MainController {
currentFile = FindLabelToFile.findFile(icons, src);
try {
renameFile();
+
} catch (Exception e) {
throw new RuntimeException(e);
}
@@ -406,11 +427,15 @@ public class MainController {
openOperations.openFolder(fileExplorer, currentFile, icons);
}
deleteOperations.deleteFile(fileExplorer, icons, deleteFile);
+ TreeViewIterator treeViewIterator = new TreeViewIterator();
+ treeViewIterator.Clear();
+ treeViewIterator.iterator(fileView);
});
//右键重命名文件
actionContextMenu.getItems().get(2).setOnAction(ActionEvent -> {
currentFile = FindLabelToFile.findFile(icons, src);
try {
+ sentParam(fileView);
renameFile();
} catch (Exception e) {
throw new RuntimeException(e);
diff --git a/main/src/controller/RenameController.java b/main/src/controller/RenameController.java
index b36d77a..d9c51fe 100644
--- a/main/src/controller/RenameController.java
+++ b/main/src/controller/RenameController.java
@@ -7,9 +7,11 @@ import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
+import javafx.scene.control.TreeView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
+import util.TreeViewIterator;
import static controller.MainController.*;
import static operations.renameOperations.isAbleToRename;
@@ -25,6 +27,8 @@ public class RenameController {
@FXML
private TextField renameField;
+ private static TreeView tmpFileView;
+
@FXML
void initialize() {
renameField.setText(currentFile.getFileName());
@@ -46,6 +50,9 @@ public class RenameController {
} else {
currentFile = currentFile.getParent();
}
+ TreeViewIterator treeViewIterator = new TreeViewIterator();
+ treeViewIterator.Clear();
+ treeViewIterator.iterator(tmpFileView);
Stage stage = (Stage) renameField.getScene().getWindow();
stage.close();
} else {
@@ -62,4 +69,8 @@ public class RenameController {
stage.show();
}
}
+
+ public static void sentParam(TreeView fileView){
+ tmpFileView = fileView;
+ }
}
diff --git a/main/src/util/TreeViewIterator.java b/main/src/util/TreeViewIterator.java
new file mode 100644
index 0000000..317ea69
--- /dev/null
+++ b/main/src/util/TreeViewIterator.java
@@ -0,0 +1,108 @@
+package util;
+
+import Data.DiskBlock;
+import Data.File;
+import javafx.event.EventHandler;
+import javafx.scene.control.TextField;
+import javafx.scene.control.TreeCell;
+import javafx.scene.control.TreeItem;
+import javafx.scene.control.TreeView;
+import javafx.scene.image.ImageView;
+import javafx.scene.input.KeyCode;
+import javafx.scene.input.KeyEvent;
+import javafx.scene.input.MouseButton;
+import javafx.scene.input.MouseEvent;
+
+
+import static controller.MainController.rootFile;
+import static controller.MainController.rootNode;
+
+public class TreeViewIterator {
+
+ void addSearch(File file, TreeItem recentNode) {
+ if (file.isFolder()) {
+ for (File file1 : file.getChildrenFile()) {
+ if (file1.isFolder()) {
+ TreeItem tmpNode = new TreeItem<>(file1.getFileName(),new ImageView("icon/img.png"));
+ recentNode.getChildren().add(tmpNode);
+ tmpNode.setExpanded(true);
+ addSearch(file1, tmpNode);
+ }
+ }
+ }
+ }
+
+ public void iterator(TreeView fileView) {
+ fileView.setRoot(rootNode);
+ rootNode.setExpanded(true);
+ fileView.setCellFactory((TreeView p) -> new TextFieldTreeCellImpl());
+ addSearch(rootFile, rootNode);
+ }
+
+ public void Clear() {
+ rootNode.getChildren().clear();
+ }
+
+ public final class TextFieldTreeCellImpl extends TreeCell {
+ private TextField textField;
+
+ public TextFieldTreeCellImpl() {
+ }
+ @Override
+ public void startEdit() {
+ super.startEdit();
+
+ if (textField == null) {
+ createTextField();
+ }
+ setText(null);
+ setGraphic(textField);
+ textField.selectAll();
+ }
+
+ @Override
+ public void cancelEdit() {
+ super.cancelEdit();
+ setText((String) getItem());
+ setGraphic(getTreeItem().getGraphic());
+ }
+
+ @Override
+ public void updateItem(String item, boolean empty) {
+ super.updateItem(item, empty);
+
+ if (empty) {
+ setText(null);
+ setGraphic(null);
+ } else {
+ if (isEditing()) {
+ if (textField != null) {
+ textField.setText(getString());
+ }
+ setText(null);
+ setGraphic(textField);
+ } else {
+ setText(getString());
+ setGraphic(getTreeItem().getGraphic());
+ }
+ }
+ }
+
+ private void createTextField() {
+ textField = new TextField(getString());
+ textField.setOnKeyReleased((KeyEvent t) -> {
+ if (t.getCode() == KeyCode.ENTER) {
+ commitEdit(textField.getText());
+ } else if (t.getCode() == KeyCode.ESCAPE) {
+ cancelEdit();
+ }
+ });
+
+ }
+
+ private String getString() {
+ return getItem() == null ? "" : getItem().toString();
+ }
+ }
+}
+
--
Gitee