如何将.Text属性正确绑定到SimpleObjectProperty的Overridden .asString()方法?

时间:2014-10-29 18:56:18

标签: java file binding javafx object-property

我正在创建一个简单的控件来浏览和采样音频文件。我想使用ObjectProperty<File>,以便我可以绑定负责播放文件的按钮的某些属性:

PlayButton.disableProperty.bind(this.BGMFile.isNull());
PlayButton.textProperty.bind(this.BGMFile.asString());

那么我将需要覆盖三件事,其中两件我已成功完成,因此不会进入

第三个是asString方法:

new SimpleObjectProperty<File>(this, "BGM File", null){
    /*yadda yadda overrides*/
    @Override public StringBinding asString(){
        if (super.get() != null && super.get().exists())
            return (StringBinding) Bindings.format(
                super.get().getName(), this
            );
        else return (StringBinding) Bindings.format("[NONE]", this);
    }
}

这对我来说是正确的,我甚至从grepCode here中删除了代码,但是当我使用FileChooser浏览文件时,我已经设置并选择了我想要使用的文件,然后将其设置为SimpleProperty按钮文本保持为[NONE]。

这是浏览文件的代码:

this.btnBrowseBGM.setOnAction((ActionEvent E) -> {
    FileChooser FC = new FileChooser();
    FC.getExtensionFilters().add(Filters.AudioExtensions());
    FC.setTitle("Browse for Background Audio File");
    File F = FC.showOpenDialog(this.getScene().getWindow());
    if (F != null && F.exists()) try {
        this.BGMFile.set(Files.copy(
            F.toPath(),
            Paths.get("Settings/Sound/", F.getName()),
            StandardCopyOption.REPLACE_EXISTING
        ).toFile());
    } catch(IOException ex) {
        Methods.Exception(
            "Unable to copy file to Settings Sound Directory.",
            "Failed to copy Sound File", ex);
        this.BGMFile.set(F);
    } else this.BGMFile.set(null);
    E.consume();
});

因为路径不存在,所以它会对我大吼大叫(我预期)但它仍然应该将BGMFile属性设置为F。我知道这样做是因为切换按钮变为活动状态并按下它播放声音文件。

那么我在这里错过/做错了什么?

编辑:

我想我可能有一个想法: 我覆盖的方法之一是set方法:

@Override public void set(File newValue){
    if (newValue != null && newValue.exists())
        super.set(newValue);
    else super.set(null);
}

重写set方法会导致它不会触发被覆盖的asString方法吗?

1 个答案:

答案 0 :(得分:1)

问题是每次调用asString()方法时都会创建一个新的Binding。由于您在文件null时首次调用它,因此您将获得Bindings.format("[NONE]", this)创建的绑定。所以你对按钮的绑定相当于:

playButton.textProperty().bind(Bindings.format("[NONE]", bgmFile));

因此,即使在文件属性更改时重新评估字符串值,它仍然会格式化"[NONE]"

如果你这样做,你可以看到相反的问题

ObjectProperty<File> fileProperty = new SimpleObjectProperty<File>() { 
    /* your previous implementation */
};
fileProperty.set(new File("/path/to/some/valid/file"));
// now bind when you get the filename:
playButton.textProperty().bind(fileProperty.asString());
// setting the fileProperty to null will now invoke the binding that was provided when it wasn't null
// and you'll see a nice bunch of null pointer exceptions:
fileProperty.set(null);

另一种说法是,检查是否存在您想要的名称的有效文件的逻辑在绑定中不存在,它位于asString()方法中。不会因为属性更改而调用该方法。

所以你需要创建一个处理所有逻辑的StringBinding(检查文件是否为空,如果不存在,检查是否存在,如果是,则获取名称等)get()方法被调用。您可以通过继承StringBinding并将逻辑放在computeValue()方法中,或使用实用程序Bindings.createStringBinding(...)方法来执行此操作,如下所示:

new SimpleObjectProperty<File>(this, "BGM File", null){

    final StringBinding string = Bindings.createStringBinding(() -> {
        File file = this.get();
        if (file != null && file.exists()) {
            return file.getName();
        } else {
            return "[NONE]";
        }
    }, this);

    @Override public StringBinding asString(){
        return string ;
    }
}

对于它的价值,我倾向于选择一种风格,除非必要,否则我会避免使用子类化。在这种情况下,我将StringBinding设置为仅绑定到文件属性的单独对象。这里的选择取决于用例,但这适用于大多数用例,你永远不会发现自己在问“我的被覆盖的方法是否以不起作用的方式进行交互”,而且一般来说,你所拥有的错误更多这种风格很明显:

ObjectProperty<File> bgmFile = new SimpleObjectProperty(this, "bgmFile", null);
StringBinding fileName = Bindings.createStringBinding( () -> {
    File file = bgmFile.get();
    if (file != null && file.exists()) {
        return file.getName();
    } else return "[NONE]";
}, bgmFile);

然后当然只做playButton.textProperty().bind(fileName);