- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试开发一个文件复制应用程序。我已经使用文件系统上的当前目录创建了一个复选框树项。
但是当我选择第一个节点(c:/目录)时,需要很长时间。如何轻松快速地选择所有目录?
这是我的第一个 FXML 加载类:
@Override
public void initialize(URL location, ResourceBundle resources) {
TreeView pathTree = new MyFileTreeView().getMyFilePathTree();
vBoxFileTree.getChildren().add(pathTree);
}
这是我的 TreeView 组件:
public class MyFileTreeView {
private TreeView<Path> filePathTree;
private List<Path> rootDirectories;
private Logger logger = Logger.getLogger(MyFileTreeView.class);
public MyFileTreeView() {
rootDirectories = new ArrayList<>();
Iterable<Path> roots = FileSystems.getDefault().getRootDirectories();
for (Path root : roots) {
rootDirectories.add(root);
}
}
public TreeView getMyFilePathTree() {
if (filePathTree == null) {
filePathTree = new TreeView<>(getRootItem());
filePathTree.setPrefHeight(600.0d);
filePathTree.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
filePathTree.setCellFactory((TreeView<Path> t) -> new TreeCellImpl());
filePathTree.setShowRoot(false);
}
return filePathTree;
}
private TreeItem getRootItem() {
TreeItem rootItem = new TreeItem();
for (Path path : rootDirectories) {
MyFileTreeItem item = new MyFileTreeItem(path);
item.setIndependent(false);
rootItem.getChildren().add(item);
logger.info(path.toString() + " directory has been added to fileTree!");
}
return rootItem;
}
}
这是树项目:
public class MyFileTreeItem extends CheckBoxTreeItem<Path> {
private boolean isLeaf;
private boolean isFirstTimeChildren = true;
private boolean isFirstTimeLeaf = true;
public MyFileTreeItem(Path path) {
super(path);
}
@Override
public boolean isLeaf() {
if (isFirstTimeLeaf) {
isFirstTimeLeaf = false;
Path path = getValue();
isLeaf = Files.isRegularFile(path);
}
return isLeaf;
}
@Override
public ObservableList<TreeItem<Path>> getChildren() {
if (isFirstTimeChildren) {
isFirstTimeChildren = false;
super.getChildren().setAll(buildChildren(this));
}
return super.getChildren();
}
private ObservableList<TreeItem<Path>> buildChildren(CheckBoxTreeItem<Path> treeItem) {
Path path = treeItem.getValue();
if ((path != null) && (Files.isDirectory(path))) {
try (Stream<Path> pathStream = Files.list(path)) {
return pathStream
.map(p -> new MyFileTreeItem(p))
.collect(Collectors.toCollection(() ->
FXCollections.observableArrayList()));
} catch (IOException e) {
}
}
return FXCollections.emptyObservableList();
}
}
更新:
感谢@fabian,我添加了不确定属性
public class FileTreeItem extends TreeItem<Path> {
private boolean isLeaf;
private boolean isFirstTimeChildren = true;
private boolean isFirstTimeLeaf = true;
private BooleanProperty selected;
private BooleanProperty indeterminate;
public FileTreeItem(Path path) {
this(path, false, false);
}
protected FileTreeItem(Path path, boolean selected, boolean indeterminate) {
super(path);
this.selected = new SimpleBooleanProperty(selected);
this.indeterminate = new SimpleBooleanProperty(indeterminate);
this.selected.addListener((o, oldValue, newValue) -> {
if (!isLeaf() && !isFirstTimeChildren) {
if (!isIndeterminate()) {
for (TreeItem<Path> ti : getChildren()) {
((FileTreeItem) ti).setSelected(newValue);
}
}
if (isIndeterminate() && newValue) {
setIndeterminate(false);
for (TreeItem<Path> ti : getChildren()) {
((FileTreeItem) ti).setSelected(newValue);
}
}
}
if (!newValue) {
if (getParent() instanceof FileTreeItem) {
FileTreeItem parent = (FileTreeItem) getParent();
parent.setIndeterminate(true);
parent.setSelected(false);
}
} else {
if (getParent() instanceof FileTreeItem) {
boolean allChildSelected = true;
FileTreeItem parent = (FileTreeItem) getParent();
for (TreeItem<Path> child : parent.getChildren()) {
if (!((FileTreeItem) child).isSelected()) {
allChildSelected = false;
break;
}
}
if (allChildSelected && !parent.isSelected()) {
setIndeterminate(false);
parent.setIndeterminate(false);
parent.setSelected(true);
}
}
}
});
}
@Override
public boolean isLeaf() {
if (isFirstTimeLeaf) {
isFirstTimeLeaf = false;
Path path = getValue();
isLeaf = Files.isRegularFile(path);
}
return isLeaf;
}
@Override
public ObservableList<TreeItem<Path>> getChildren() {
if (isFirstTimeChildren) {
isFirstTimeChildren = false;
super.getChildren().setAll(buildChildren(this));
}
return super.getChildren();
}
private List<TreeItem<Path>> buildChildren(FileTreeItem treeItem) {
Path path = treeItem.getValue();
if ((path != null) && (Files.isDirectory(path))) {
final boolean select = treeItem.isSelected();
boolean indeterminate = treeItem.isIndeterminate();
try (Stream<Path> pathStream = Files.list(path)) {
List<TreeItem<Path>> res = new ArrayList<>();
pathStream
.map(p -> new FileTreeItem(p, select, indeterminate))
.forEach(res::add);
return res;
} catch (IOException e) {
}
}
return Collections.emptyList();
}
public boolean isSelected() {
return selected.get();
}
public BooleanProperty selectedProperty() {
return selected;
}
public void setSelected(boolean value) {
selected.set(value);
}
public boolean isIndeterminate() {
return indeterminate.get();
}
public BooleanProperty indeterminateProperty() {
return indeterminate;
}
public void setIndeterminate(boolean indeterminate) {
this.indeterminate.set(indeterminate);
}
}
最佳答案
当您更改 CheckBoxTreeItem
的选定状态时,子项的状态将设置为相同的值。这意味着调用了 getChildren 方法,并且还为所有子项设置了 selected 属性。这样您就可以有效地对目录的所有内容进行深度优先遍历。
因此,您需要直接扩展 TreeItem
并实现所需的属性。您需要确保在更新 selected
属性时,仅当已调用 getChildren
时才迭代子项:
public class FileTreeItem extends TreeItem<Path> {
private boolean isLeaf;
private boolean isFirstTimeChildren = true;
private boolean isFirstTimeLeaf = true;
private final BooleanProperty selected;
private final BooleanProperty indeterminate;
protected FileTreeItem(Path path, boolean selected) {
super(path);
this.indeterminate = new SimpleBooleanProperty();
this.selected = new SimpleBooleanProperty(selected);
this.selected.addListener((o, oldValue, newValue) -> {
if (!updating) {
if (!isLeaf() && !isFirstTimeChildren) {
// propagate selection to children if they were created yet
for (TreeItem<Path> ti : getChildren()) {
FileTreeItem fti = (FileTreeItem) ti;
fti.setSelected(newValue);
}
}
// update ancestors
TreeItem<Path> parent = getParent();
while ((parent instanceof FileTreeItem)
&& updateAncestorState((FileTreeItem) parent)) {
parent = parent.getParent();
}
}
});
}
/**
* flag preventing circular calls during update.
*/
private boolean updating;
protected static boolean updateAncestorState(FileTreeItem item) {
List<TreeItem<Path>> children = item.getChildren();
boolean hasUnselected = false;
boolean hasSelected = false;
for (Iterator<TreeItem<Path>> it = children.iterator();!(hasSelected && hasUnselected) && it.hasNext();) {
TreeItem<Path> ti = it.next();
FileTreeItem child = (FileTreeItem) ti;
if (child.isSelected()) {
hasSelected = true;
} else {
hasUnselected = true;
if (child.isIndeterminate()) {
hasSelected = true;
}
}
}
item.updating = true;
boolean changed = false;
if (hasUnselected) {
if (item.isSelected() || item.isIndeterminate() != hasSelected) {
changed = true;
item.setSelected(false);
item.setIndeterminate(hasSelected);
}
} else {
if (!item.isSelected()) {
changed = true;
item.setSelected(true);
}
item.setIndeterminate(false);
}
item.updating = false;
return changed;
}
public FileTreeItem(Path path) {
this(path, false);
}
@Override
public boolean isLeaf() {
if (isFirstTimeLeaf) {
isFirstTimeLeaf = false;
Path path = getValue();
isLeaf = Files.isRegularFile(path);
}
return isLeaf;
}
@Override
public ObservableList<TreeItem<Path>> getChildren() {
if (isFirstTimeChildren) {
isFirstTimeChildren = false;
super.getChildren().setAll(buildChildren(this));
}
return super.getChildren();
}
private List<TreeItem<Path>> buildChildren(FileTreeItem treeItem) {
Path path = treeItem.getValue();
if ((path != null) && (Files.isDirectory(path))) {
final boolean select = treeItem.isSelected();
try (Stream<Path> pathStream = Files.list(path)) {
List<TreeItem<Path>> res = new ArrayList<>();
pathStream
.map(p -> new FileTreeItem(p, select))
.forEach(res::add);
return res;
} catch (IOException e) {
}
}
return Collections.emptyList();
}
/* methods for selected & indeterminate properties */
}
编辑
显示不确定
属性需要您实现自己的TreeCell
:
public class FileItemCheckBoxTreeCell extends TreeCell<Path> {
private BooleanProperty oldSelectedProperty;
private BooleanProperty oldIndeterminateProperty;
private final CheckBox checkBox;
private final StringConverter<TreeItem<Path>> converter;
public FileItemCheckBoxTreeCell(StringConverter<TreeItem<Path>> converter) {
if (converter == null) {
throw new IllegalArgumentException();
}
this.converter = converter;
this.checkBox = new CheckBox();
}
@Override
protected void updateItem(Path item, boolean empty) {
// clear old binding
if (oldSelectedProperty != null) {
checkBox.selectedProperty().unbindBidirectional(oldSelectedProperty);
checkBox.indeterminateProperty().unbindBidirectional(oldIndeterminateProperty);
oldSelectedProperty = null;
oldIndeterminateProperty = null;
}
checkBox.indeterminateProperty().unbind();
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
setText("");
} else {
TreeItem<Path> treeItem = getTreeItem();
setText(converter.toString(treeItem));
if (treeItem instanceof FileTreeItem) {
setGraphic(checkBox);
FileTreeItem fti = (FileTreeItem) treeItem;
oldSelectedProperty = fti.selectedProperty();
oldIndeterminateProperty = fti.indeterminateProperty();
checkBox.selectedProperty().bindBidirectional(oldSelectedProperty);
checkBox.indeterminateProperty().bindBidirectional(oldIndeterminateProperty);
} else {
setGraphic(null);
}
}
}
public static Callback<TreeView<Path>, TreeCell<Path>> forTreeView(StringConverter<TreeItem<Path>> converter) {
if (converter == null) {
throw new IllegalArgumentException();
}
return tv -> new FileItemCheckBoxTreeCell(converter);
}
}
public TreeView getMyFilePathTree() {
if (filePathTree == null) {
filePathTree = new TreeView<>(getRootItem());
filePathTree.setPrefHeight(600.0d);
filePathTree.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
// tell cell factory to use the selected property for checkbox
filePathTree.setCellFactory(FileItemCheckBoxTreeCell.forTreeView(new StringConverter<TreeItem<Path>>() {
@Override
public String toString(TreeItem<Path> object) {
if (object == null) {
return "";
}
Path p = object.getValue();
if (p == null) {
return "";
}
p = p.getFileName();
return p == null ? object.getValue().toString() : p.toString();
}
@Override
public TreeItem<Path> fromString(String string) {
throw new UnsupportedOperationException();
}
}));
filePathTree.setShowRoot(false);
}
return filePathTree;
}
关于JavaFx CheckBoxTreeItem<Path> 选择根项错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49086335/
这是我的作业 What does echo PATH $PATH \$PATH do? 我不知道它是如何工作的。 echo PATH打印“路径” $PATH创建一个“PATH”变量......也许?
我想弄清楚两者之间的区别 路径=路径+[节点1] 路径+=[节点1] path.append(node1) 我得到的是 path = path + [node1] 的正确路径,但不是其他两个。 def
我使用 Robot 框架在 Ride 中创建了一个测试用例。运行时出现错误。 我更新了python的路径。我更新了库和 Ride。我换了文件夹还是不行 *** Settings *** Documen
我尝试使用额外的功能自定义 pathlib.Path()。特别是,我真的很喜欢使用上下文管理器作为移入和移出目录的方法。我一直在使用它,但我似乎在让 Path() 与自定义上下文管理器一起工作时遇到错
编辑:基于 Ulf Rompe 的评论,重要的是使用“1”而不是“0”,否则您将破坏 sys.path . 我已经做 python 很长一段时间了(一年多),我总是很困惑为什么人们建议你使用 sys.
我有兴趣这样做的原因是因为我的路径中有一部分将保持不变,但我希望将其与其所有父部分一起删除。 所以如果我们说, some/unknown/path/foo/bar/baz 我想回去 bar/baz 但
在几个 SO 的问题中,有这些行可以访问代码的父目录,例如os.path.join(os.path.dirname(__file__)) returns nothing和 os.path.join(o
我已经在我的 Linux 中安装了 anaconda 来导入 python 包。 安装 anaconda 后,我无法在 python 中使用 anaconda,经过一番搜索后我发现输入此命令我能够使用
哪个更好用,为什么?我的意思是这两个命令在哪些方面不同以及如何不同?性能、可读性…… new FileInfo(path).Name 或 Path.GetFileName(path) 最佳答案 因为您
这不适用于某些设备。 在三星设备中,他们不允许使用下载管理器下载文件。 我已经在 list 中定义了权限并获得了运行时权限。 DownloadManager downloadManager = (Do
我想知道在这个例子中使用 Paths.get() 和 Path.resolve 有什么区别: public static void main(String[] args) { Path p1
目前我正在开发一个转换由 Inkscape 创建的 svg-paths 的应用程序。现在我不清楚关于绝对和相对路径组合的路径规范。规范是否说明了同时包含相对和绝对坐标的路径定义? 特别是关于绝对贝塞尔
我正在编写脚本,我需要在用户的 $PATH 上查找命令并获取该命令的完整路径。问题是我不知道用户的登录 shell 是什么,或者他们的 do 文件中可能有什么奇怪的东西。我将 bourne shell
Metalsmith 的文档对 path() 函数没有太多解释:#path(paths...): Resolve any amount of paths... relative to the work
我知道我可以通过 regedit 更改我的 wine PATH,但实际上我只需要为一次运行更改 PATH。 例如,我的软件名为frontend.exe,这取决于example/mylib.dll,我需
因此,绝对路径是一种到达某个文件或位置的方法,描述了它的完整路径、完整路径,并且它依赖于操作系统(Windows 和 Linux 的绝对路径,例如,不同)。另一方面,相对路径是从当前位置 ..(两个点
我对编程有点陌生(不是真的,但我仍在学习 - 我们不是吗?)。虽然我了解 Java 和 Python,并且了解 C、C++、JS、C#、HTML、CSS 等(并且我可以在终端中很好地导航),但我不熟悉
我对编程有点陌生(不是真的,但我仍在学习 - 我们不是吗?)。虽然我了解 Java 和 Python,并且了解 C、C++、JS、C#、HTML、CSS 等(并且我可以在终端中很好地导航),但我不熟悉
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
使用环境变量(如 PATH)作为 $PATH 或 ${PATH} 有什么区别? 最佳答案 在大多数情况下没有区别。唯一重要的是你是否想在扩展后包含尾随文本。例如,假设您的 PATH 包含字符串 FOO
我是一名优秀的程序员,十分优秀!