setGraphic()在递归创建的TreeItems上无法正常工作

时间:2016-01-08 07:35:45

标签: java recursion javafx treeview assign

我正在写一个文件夹同步应用程序。目前,我正在处理负责递归遍历两个用户指定的目录结构的部分,将其中的文件夹和文件与其他结构进行比较,并显示每个文件或文件夹是否未更改,更改或更新,有色点的意思。问题是当前状态的程序在正确评估关系时,只在每个点颜色上显示一个 TreeItem上的点而不是所有这些点。图片供参考: enter image description here

造成这种情况的原因是什么?我怀疑它与Java中对象赋值的工作方式有关,所以我将某个同一个对象以某种方式重新分配给所有正确的TreeItems,只停留在最后一个TreeItems,但是这个太宽泛了。请参阅下面的违规功能。

private void compareAndFillInSourceTreeView(Path x, TreeItem root) throws IOException {
    String xSourceName = x.getName(x.getNameCount() - 1).toString();
    String xTargetName = (getEquivalentFileInTarget(x).getName(getEquivalentFileInTarget(x).getNameCount() - 1))
            .toString();

    System.out.println("-----------------------------------------------------------------------------------------");
    System.out.println("NEW CALL: " + x.toString() + " " + root);
    System.out.println("EQUIVALENT: " + getEquivalentFileInTarget(x) + " EXISTS: " +
            getEquivalentFileInTarget(x).toFile().exists());
    System.out.println("IS NEW: " + xTargetName + ", " + (xTargetName == null));
    System.out.println("UNCHANGED: " + x + " " + getEquivalentFileInTarget(x) + " NAMES: " + xSourceName + ", "
            + xTargetName);
    System.out.println("CHANGED: " + ((x.getName(x.getNameCount() - 1)) ==
            getEquivalentFileInTarget(x).getName(getEquivalentFileInTarget(x).getNameCount() - 1)));

    if (x.toFile().isFile()) {
        System.out.println("THIS IS A FILE: " + x.toString());

        //if new, i.e. doesn't exist in the target
        if (!getEquivalentFileInTarget(x).toFile().exists()) {
            System.out.println("EQUIVALENT DOESN'T EXIST FOR THIS FILE IN TARGET");
            TreeItem newBranch = makeBranch(xSourceName, root);
            newBranch.setGraphic(blueDotIcon);
        }

        //if unchanged
        else if (sameContents(x, getEquivalentFileInTarget(x)) && (xSourceName.equals(xTargetName))) {
            System.out.println("THIS FILE AND ITS EQUIVALENT ARE EQUAL");
            TreeItem newBranch = makeBranch(x.getName(x.getNameCount() - 1).toString(), root);
            newBranch.setGraphic(greenDotIcon);
        }

        //if same name, but different contents, i.e. changed
        else if ((x.getName(x.getNameCount() - 1)).equals(
                getEquivalentFileInTarget(x).getName(getEquivalentFileInTarget(x).getNameCount() - 1))) {
            TreeItem newBranch = makeBranch(x.getName(x.getNameCount() - 1).toString(), root);
            newBranch.setGraphic(yellowDotIcon);

        } else {
            System.out.println("BAD, putInTreeView() Error, it should never reach this line");
            System.out.println("Error log: " + x + ", " + getEquivalentFileInTarget(x));
        }

    } else if (x.toFile().isDirectory()){ //if it's a folder, checked explicitly because it's behaving weird
        System.out.println("THIS IS A DIRECTORY: " + x.toString());

        if (getEquivalentFileInTarget(x).toFile().exists()) {
            System.out.println("EQUIVALENT EXISTS FOR THIS DIRECTORY IN TARGET.");
            //make new branches and mark them as existing folders
            TreeItem currentSourceTreeViewRoot = makeBranch(x.getName(x.getNameCount() - 1).toString(), root);
            currentSourceTreeViewRoot.setExpanded(true);
            currentSourceTreeViewRoot.setGraphic(greenDotIcon);
            for (File i : x.toFile().listFiles()) {
                System.out.println("Rec. called for: " + currentSourceTreeViewRoot);
                compareAndFillInSourceTreeView(i.toPath(), currentSourceTreeViewRoot);
            }

        } else {
            System.out.println("EQUIVALENT DOESN'T EXIST FOR THIS DIRECTORY IN TARGET.");
            //if they don't exist, make the branches anyway and mark them as representing nonexistent folders
            TreeItem currentSourceTreeViewRoot = makeBranch((x.getName(x.getNameCount() - 1)).toString(), root);
            currentSourceTreeViewRoot.setExpanded(true);

            for (File i : x.toFile().listFiles()) {
                System.out.println("Rec. called for: " + currentSourceTreeViewRoot);
                compareAndFillInSourceTreeView(i.toPath(), currentSourceTreeViewRoot);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

你的假设是正确的。在使用setGraphic分配图形时,您正在告诉JavaFX在场景图中的哪个位置找到此节点。当您使用与参数相同的对象再次呼叫setGraphic时,您实际上将其移动到场景图中的其他位置。

为每件商品创建一个新的圆点/圆圈,您的问题应该得到解决。