4. 视图间的交互

用户应该能通过点击地图中的标记修改并保存工作位置。但是我们的应用程序目前还没有这个功能。

打开 LocationLookupView 视图控制器,提供用于读写位置信息的 getter 和 setter。

public Location getSelected() {
    return selected;
}

public void setSelected(@Nullable Location selected) {
    this.selected = selected;

    currentLocationField.setValue(selected);

    if (selected != null) {
        setMapCenter(selected.getBuilding().getCoordinate());
    }
}

打开 UserDetailView 视图控制器,找到 onLocationFieldSelect() 处理方法。我们的需求不仅仅是打开对话框,而是能在打开对话框的同时传入之前保存的位置信息。此外,我们还需要在对话框关闭时获取地点选择的结果。下面是按这个需求修改的内容:

@ViewComponent
private JmixValuePicker<Location> locationField;

@Subscribe("locationField.select")
public void onLocationFieldSelect(final ActionPerformedEvent event) {
    dialogWindows.view(this, LocationLookupView.class)
            .withAfterCloseListener(closeEvent -> {
                if (closeEvent.closedWith(StandardOutcome.SELECT)) {
                    locationField.setValue(closeEvent.getView().getSelected()); (1)
                }
            })
            .open()
            .getView()
            .setSelected(getEditedEntity().getLocation()); (2)
}
1 AfterCloseEvent 对象包含 CloseAction 视图中 close() 传入的参数。我们用 event 对象的 closedWith() 方法分析视图的关闭操作。提醒:在前一章中,我们为 select 操作创建了 处理方法
2 将被编辑用户的位置信息传递给 LocationLookupView 的 setter。

启动应用程序,检查 UserDetailViewLocationLookupView 之间能正常交互。