diff --git a/main/src/controller/MainController.java b/main/src/controller/MainController.java index 77e1aa3c1b7283d7f21730ffc497247ede31e6b4..a8410642eaa3d6608dd3a3ce7e861adeef81b72c 100644 --- a/main/src/controller/MainController.java +++ b/main/src/controller/MainController.java @@ -26,6 +26,7 @@ import java.io.IOException; import java.util.*; import operations.createFileOperations; +import operations.deleteOperations; import operations.openOperations; import util.*; @@ -181,32 +182,38 @@ public class MainController { this.hours.setText(hour + ":" + minute + ":" + second); this.data.setText("20" + year + "/" + month + "/" + day); } - - public void setFilePreview(File newFile,Label label){ - label.setOnMouseEntered(event1 -> { - iconMes.setVisible(true); - Image icon; - ((Label) event1.getSource()).setStyle("-fx-background-color: rgba(29,29,31,0.15);"); - nameMes.setText("文件名称:" + newFile.getFileName()); - if (newFile.getTypeName().equals("普通文件")||newFile.getTypeName().equals("系统文件")) { - typeMes.setText("文件类型:" + newFile.getTypeName()); - icon = new Image("icon/文本文件.png"); - } else { - typeMes.setText("文件类型:" + newFile.getTypeName()); - icon = new Image("icon/文件夹.png"); - } - iconMes.setImage(icon); - memoryMes.setText("占用空间:" + newFile.getSize()); -// timeMes.setText("建立时间:" + newFile.getTime()); - }); - label.setOnMouseExited(event12 -> { - ((Label) event12.getSource()).setStyle(null); +// 预览文件鼠标悬停移入事件 + public void setFilePreview(File newFile) { + iconMes.setVisible(true); + Image icon; + nameMes.setText("文件名称:" + newFile.getFileName()); + if (newFile.getTypeName().equals("普通文件") || newFile.getTypeName().equals("系统文件")) { + typeMes.setText("文件类型:" + newFile.getTypeName()); + icon = new Image("icon/文本文件.png"); + } else { + typeMes.setText("文件类型:" + newFile.getTypeName()); + icon = new Image("icon/文件夹.png"); + } + iconMes.setImage(icon); + memoryMes.setText("占用空间:" + newFile.getSize()); +// timeMes.setText("建立时间:" + newFile.getTime()); + } +// 预览文件鼠标悬停移除事件 + public void removeFilePreview(File newFile){ nameMes.setText(""); typeMes.setText(""); iconMes.setVisible(false); memoryMes.setText(""); timeMes.setText(""); - }); + } +// 点击选中文件信息事件 + public void selectAction(File file) { + nameLabel.setText("文件名: " + file.getFileName()); + sizeLabel.setText("文件大小:" + file.getSize() + "B"); + if (file.isReadOnly()) { + rwLabel.setText("文件属性:只读"); + } else rwLabel.setText("文件属性:读写"); + pathLabel.setText("绝对路径:" + GetFileAbsolutePath.getAbsPath(file)); } //FlowPane的鼠标点击事件 @@ -215,7 +222,6 @@ public class MainController { fileExplorer.addEventHandler(MouseEvent.MOUSE_CLICKED,(MouseEvent click) -> { if(click.getButton() == MouseButton.SECONDARY && !actionContextMenu.isShowing()){ //右键点击事件 createContextMenu.show(fileExplorer,click.getScreenX(),click.getScreenY()); - }else { createContextMenu.hide(); } @@ -225,14 +231,12 @@ public class MainController { public void menuItemAction(){ createFileItem.setOnAction(ActionEvent -> { File file = createFileOperations.createFile(fileExplorer,icons,actionContextMenu); - setFilePreview(file,icons.get(file)); - //createFile(); + LabelMouseEventForFile(icons.get(file)); }); createFolderItem.setOnAction(ActionEvent -> { File folder = createFileOperations.createFolder(fileExplorer,icons,actionContextMenu); - setFilePreview(folder,icons.get(folder)); - //createFolder(); + LabelMouseEventForFolder(icons.get(folder)); }); } @@ -245,6 +249,138 @@ public class MainController { } } + void LabelMouseEventForFile(Label icon){ + icon.setOnMouseEntered(new EventHandler() { + + @Override + public void handle(MouseEvent event) { + ((Label) event.getSource()).setStyle("-fx-background-color: rgba(116,194,238,0.35);"); + setFilePreview(Objects.requireNonNull(FindLabelToFile.findFile(icons, icon))); + + } + }); + + icon.setOnMouseExited(new EventHandler() { + + @Override + public void handle(MouseEvent event) { + ((Label) event.getSource()).setStyle("-fx-background-color: transparent;"); + removeFilePreview(FindLabelToFile.findFile(icons,icon)); + } + }); + + icon.setOnMouseClicked(new EventHandler() { + + @Override + public void handle(MouseEvent event) { + Label src = (Label) event.getSource(); + if (event.getButton() == MouseButton.SECONDARY && event.getClickCount() == 1) { + actionContextMenu.getItems().get(0).setOnAction(ActionEvent -> { + currentFile = FindLabelToFile.findFile(icons, src); + if(currentFile != null) { + if (currentFile.isFolder()) { + System.out.println(1); + System.out.println(src); + openOperations.openFolder(fileExplorer, currentFile, icons); + } else { + openOperations.openFile(); + } + } + }); + 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); + }); + actionContextMenu.getItems().get(2).setOnAction(ActionEvent -> { + currentFile = FindLabelToFile.findFile(icons, src); + + }); + actionContextMenu.show(src, event.getScreenX(), event.getScreenY()); + } else if (event.getButton() == MouseButton.PRIMARY && event.getClickCount() == 2) { + System.out.println("打开文件"); + } else if (event.getButton() == MouseButton.PRIMARY && event.getClickCount() == 1) { + System.out.println("选择"); + File file = FindLabelToFile.findFile(icons, icon); + selectAction(file); + } else { + actionContextMenu.hide(); + } + } + }); + } + + void LabelMouseEventForFolder(Label icon){ + icon.setOnMouseEntered(new EventHandler() { + @Override + public void handle(MouseEvent event) { + ((Label) event.getSource()).setStyle("-fx-background-color: rgba(116,194,238,0.35);"); + setFilePreview(Objects.requireNonNull(FindLabelToFile.findFile(icons, icon))); + } + }); + + icon.setOnMouseExited(new EventHandler() { + @Override + public void handle(MouseEvent event) { + ((Label) event.getSource()).setStyle("-fx-background-color: transparent;"); + removeFilePreview(FindLabelToFile.findFile(icons,icon)); + } + }); + + icon.setOnMouseClicked(new EventHandler() { + + @Override + public void handle(MouseEvent event) { + Label src = (Label) event.getSource(); + System.out.println(src); + if (event.getButton() == MouseButton.SECONDARY && event.getClickCount() == 1) { + actionContextMenu.getItems().get(0).setOnAction(ActionEvent -> { + currentFile = FindLabelToFile.findFile(icons, src); + if(currentFile != null) { + if (currentFile.isFolder()) { + System.out.println(1); + System.out.println(src); + openOperations.openFolder(fileExplorer, currentFile, icons); + } else { + openOperations.openFile(); + } + } + }); + 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); + }); + actionContextMenu.show(src, event.getScreenX(), event.getScreenY()); + } else if (event.getButton() == MouseButton.PRIMARY && event.getClickCount() == 2) { + System.out.println("打开文件"); + currentFile = FindLabelToFile.findFile(icons, icon); + System.out.println(currentFile.getFileName()); + if (currentFile.isFolder()) { + openOperations.openFolder(fileExplorer, currentFile, icons); + } else { + openOperations.openFile(); + } + + } else if (event.getButton() == MouseButton.PRIMARY && event.getClickCount() == 1) { + System.out.println("选择"); + File file = FindLabelToFile.findFile(icons, icon); + selectAction(file); + } else { + actionContextMenu.hide(); + } + } + }); + } + } diff --git a/main/src/operations/createFileOperations.java b/main/src/operations/createFileOperations.java index c01087ea38f931235ce335b194da4c4a3ab03e3e..1cfadd479926e36194bf1aa911c8f8e165cfbda8 100644 --- a/main/src/operations/createFileOperations.java +++ b/main/src/operations/createFileOperations.java @@ -60,7 +60,7 @@ public class createFileOperations { currentFile.getChildrenFile().add(file); // currentFile.getPath().getChildren().add(newPath); - icon.setOnMouseEntered(new EventHandler() { + /* icon.setOnMouseEntered(new EventHandler() { @Override public void handle(MouseEvent event) { @@ -112,11 +112,13 @@ public class createFileOperations { System.out.println("打开文件"); } else if (event.getButton() == MouseButton.PRIMARY && event.getClickCount() == 1) { System.out.println("选择"); + File file = FindLabelToFile.findFile(icons, icon); + MainController.selectAction(file); } else { actionContextMenu.hide(); } } - }); + });*/ return file; } @@ -155,67 +157,6 @@ public class createFileOperations { } } currentFile.getChildrenFile().add(file); -// currentFile.getPath().getChildren().add(newPath); - icon.setOnMouseEntered(new EventHandler() { - @Override - public void handle(MouseEvent event) { - ((Label) event.getSource()).setStyle("-fx-background-color: rgba(116,194,238,0.35);"); - } - }); - - icon.setOnMouseExited(new EventHandler() { - @Override - public void handle(MouseEvent event) { - ((Label) event.getSource()).setStyle("-fx-background-color: transparent;"); - } - }); - - icon.setOnMouseClicked(new EventHandler() { - - @Override - public void handle(MouseEvent event) { - Label src = (Label) event.getSource(); - System.out.println(src); - if (event.getButton() == MouseButton.SECONDARY && event.getClickCount() == 1) { - actionContextMenu.getItems().get(0).setOnAction(ActionEvent -> { - currentFile = FindLabelToFile.findFile(icons, src); - if(currentFile != null) { - if (currentFile.isFolder()) { - System.out.println(1); - System.out.println(src); - openOperations.openFolder(flowPane, currentFile, icons); - } else { - openOperations.openFile(); - } - } - }); - actionContextMenu.getItems().get(1).setOnAction(ActionEvent -> { - currentFile = FindLabelToFile.findFile(icons, src); - File deleteFile = currentFile; - currentFile = currentFile.getParent(); - if (currentFile != rootFile && currentFile.isFolder()) { - openOperations.openFolder(flowPane, currentFile, icons); - } - deleteOperations.deleteFile(flowPane, icons, deleteFile); - }); - actionContextMenu.show(src, event.getScreenX(), event.getScreenY()); - } else if (event.getButton() == MouseButton.PRIMARY && event.getClickCount() == 2) { - System.out.println("打开文件"); - currentFile = FindLabelToFile.findFile(icons, icon); - System.out.println(currentFile.getFileName()); - if (currentFile.isFolder()) { - openOperations.openFolder(flowPane, currentFile, icons); - } else { - openOperations.openFile(); - } - - } else if (event.getButton() == MouseButton.PRIMARY && event.getClickCount() == 1) { - System.out.println("选择"); - } else { - actionContextMenu.hide(); - } - } - }); return file; } }