Lookup with TreeTable to get selected Subtree

Hi,

i’ve a question regarding the openLookup method. Here’s what i want to achieve: The lookup screen that i have created has a TreeTable inside of it. When i select a element within this tree normally i would get the exact element in the tree in the Lookup.Handler.handleLookup(Collection items). What i want to achieve is that instead of getting only this element i want to get all parents and / or all children with its sub-trees from the selection.

To achieve this there a few options come into my mind but i’m not really sure which one to use. I tried instead of using the handleLookup method to add a Window.CloseListener like so:

AbstractLookup lookup = openLookup(...)
        lookup.addCloseListener(new Window.CloseListener() {
            @Override
            void windowClosed(String actionId) {
                def dsContext = lookup.getDsContext()
                def datasource = (HierarchicalDatasource)dsContext.get("treeTableDs")
                def selectedItem = datasource.item

                def children = datasource.getChildren(selectedItem.id)
            }
        })

Although that might be possible as well with the handleLookup method…

Next thing that came to mind is the possibility to change the way the SelectAction works. I saw that in the buttonClick method of the SelectAction the handleLookup is called - so it might be possible to extend it and instead of only returning the exact selection add the parents and / or the children to it.

The last this i came up is go from a TreeTable to a Tree. Then i could extend the WebTree class in order to override the getSelected method to get the correct subtree (this line got me thinking about it).

As there are so many options to chose from (and probably even more i never thought of), my question would be: What would you do if you have a scenario like this?

Bye,
Mario

1 Like

Hi Mario,
I would go for the first option, because there it’s quite difficult to override the default behavior of the standard lookup mechanism.
To test the solution (see the project attached), I’ve created two entities - hierarchical Regions and simple Customers that have a reference to Regions.
In the RegionBrowse, I’ve exposed its datasource:


public class RegionBrowse extends AbstractLookup {
    @Inject
    private HierarchicalDatasource<Region, UUID> regionsDs;

    public HierarchicalDatasource<Region, UUID> getRegionsDs() {
        return regionsDs;
    }
}

In the Customer editor, I’ve created a new action replacing the standard LookupAction for PickerField:


public class CustomerEdit extends AbstractEditor<Customer> {

    @Named("fieldGroup.region")
    private PickerField regionField;

    @Override
    public void init(Map<String, Object> params) {
        BaseAction action = new BaseAction("lookup") {
            @Override
            public String getIcon() {
                return "components/pickerfield/images/lookup-btn.png";
            }

            @Override
            public void actionPerform(Component component) {
                RegionBrowse regionBrowse = (RegionBrowse) openLookup("demo$Region.browse",
                        items -> {  }, // lookup handler is not used
                        WindowManager.OpenType.THIS_TAB);

                regionBrowse.addCloseListener(actionId -> {
                    if (Window.SELECT_ACTION_ID.equals(actionId)) {
                        Region selected = regionBrowse.getRegionsDs().getItem();
                        Collection<UUID> childrenIds = regionBrowse.getRegionsDs().getChildren(selected.getId());
                        regionField.setValue(selected);
                        showNotification("Children IDs: " + childrenIds, NotificationType.HUMANIZED);
                    }
                });
            }
        };

        regionField.removeAllActions();
        regionField.addAction(action);
        regionField.addClearAction();
    }
}

The lookup handler is ignored, and the work is done in the CloseListener as you suggested. I’ve just added the check for actionId to ignore closing by Cancel button.

Perhaps we should think about making the lookup process more extension-friendly. Thank you for pointing out the problem!

tree-select.zip (29.7K)

Hi Konstantin,

thanks for the example. I’ll take a closer look!

Bye,
Mario