如何将信息从一个视图(可停靠)传递到另一个视图?

How to pass information from one view (dockable) to another?

我遇到了设计问题。如果我(例如)在左侧可停靠视图上有一个包含一些 pojo 的列表视图,我如何通知中心可停靠选择了哪个?我正在尝试实现某种主细节视图,用户可以在其中选择一个项目,然后可以在中心区域和右侧区域对其进行配置。 提前致谢:)

这取决于您要如何设计您的应用程序。

如果您想为每个 pojo 创建一个单独的编辑器,那么您可以查看 drombler fx archetype 提供的 LeftTestPane 示例。

@FXML
private void onNewSampleAction(ActionEvent event) {
    sampleCounter++;
    Sample sample = new Sample("Sample " + sampleCounter);
    SampleEditorPane sampleEditorPane = new SampleEditorPane(sample);
    Dockables.inject(sampleEditorPane);
    Dockables.open(sampleEditorPane);
}

目前没有 API 用于选择已打开的编辑器,但请注意,编辑器目前正在改进,针对问题 #111

如果您想要单个详细信息视图,则可以使用 Context Framework,它允许 Dockable 和 Actions 等组件以松散耦合的方式进行通信。

ListView 应该实现 LocalContextProvider and keep the selected pojo in its local Context

@ViewDocking(...)
public class ListView extends SomeNode implements LocalContextProvider {

    private final SimpleContextContent contextContent = new SimpleContextContent();
    private final SimpleContext context = new SimpleContext(contextContent);
    private MyPojo currentSelection;

    ...

    @Override
    public Context getLocalContext() {
        return context;
    }

    ...

    if (currentSelection != null){
        contextContent.remove(currentSelection);
    }
    currentSelection = <current selection>
    if (currentSelection != null){
        contextContent.add(currentSelection);
    }
    ...
}

在这种情况下,DetailsView 也应该注册为视图(单例),并实现 LocalContextProvider as well as ActiveContextSensitive:

@ViewDocking(...)
public class DetailsPane extends SomeNode implements ActiveContextSensitive, LocalContextProvider {

    private final SimpleContextContent contextContent = new SimpleContextContent();
    private final SimpleContext context = new SimpleContext(contextContent);
    private Context activeContext;
    private MyPojo myPojo;

    ...

    @Override
    public Context getLocalContext() {
        return context;
    }

    @Override
    public void setActiveContext(Context activeContext) {
        this.activeContext = activeContext;
        this.activeContext.addContextListener(MyPojo.class, (ContextEvent event) -> contextChanged());
        contextChanged();
    }

    private void contextChanged() {
        MyPojo newMyPojo = activeContext.find(MyPojo.class);
        if ((myPojo == null && newMyPojo != null) || (myPojo null && !sample.equals(newMyPojo))) {
            if (myPojo != null) {
                unregister();
            }
            myPojo = newMyPojo;
            if (myPojo != null) {
                register();
            }
        }
    }

    private void unregister() {
        contextContent.remove(myPojo);

        //reset DetailsView
    }

    private void register() {
        // configure DetailsView

        contextContent.add(myPojo);
    }
...
}

查看 drombler fx archetype 提供的 RightTestPane 示例。