Use Tree Component inside AbstractEditor

Hello

I want do select categories for my products so I created a category entity. I want to have a tree inside the category editor so I can visually select the hierarchy. Now, I am not able to have the tree item preselected when I want to edit an item. I managed to get IllegalStateException: Datasource doesn’t contain items, although the Tree shows datasource items

Please help

Entity


@NamePattern("%s|nume")
@Table(name = "SPICA_PRODUS_CATEGORIE")
@Entity(name = "spica$ProdusCategorie")
public class ProdusCategorie extends StandardEntity {
    private static final long serialVersionUID = 1264937965825142240L;

    @Column(name = "NUME")
    protected String nume;

    @Lookup(type = LookupType.DROPDOWN, actions = {"lookup"})
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "CATEGORIE_ID")
    protected ProdusCategorie categorie;

    public ProdusCategorie getCategorie() {
        return categorie;
    }

    public void setCategorie(ProdusCategorie categorie) {
        this.categorie = categorie;
    }
    public void setNume(String nume) {
        this.nume = nume;
    }
    public String getNume() {
        return nume;
    }
}

XML editor


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<window xmlns="http://schemas.haulmont.com/cuba/window.xsd"
        caption="msg://editorCaption"
        class="com.company.spica.web.produscategorie.ProdusCategorieEdit"
        datasource="produsCategoriesDs"
        focusComponent="fieldGroup"
        messagesPack="com.company.spica.web.produscategorie">
    <dsContext>
        <datasource id="produsCategoriesDs"
                    class="com.company.spica.entity.ProdusCategorie"
                    view="produsCategorie-view"/>
        <collectionDatasource id="categoriesDs"
                              class="com.company.spica.entity.ProdusCategorie"
                              view="_minimal">
            <query>
                <![CDATA[select e from spica$ProdusCategorie e]]>
            </query>
        </collectionDatasource>
        <hierarchicalDatasource id="produsCategoriesDsH"
                                class="com.company.spica.entity.ProdusCategorie"
                                hierarchyProperty="categorie"
                                view="produsCategorie-view">
            <query>
                <![CDATA[select e from spica$ProdusCategorie e]]>
            </query>
        </hierarchicalDatasource>
    </dsContext>
    <dialogMode height="600"
                width="800"/>
    <layout expand="windowActions"
            spacing="true">
        <fieldGroup id="fieldGroup"
                    datasource="produsCategoriesDs">
            <column width="250px">
                <field id="nume"/>
                <field id="categorie"
                       optionsDatasource="categoriesDs"/>
            </column>
        </fieldGroup>
        <tree id="treeCat"
              height="200px"
              width="300px">
            <treechildren datasource="produsCategoriesDsH"/>

        </tree>
        <frame id="windowActions"
               screen="editWindowActions"/>
    </layout>
</window>

and Controller


public class ProdusCategorieEdit extends AbstractEditor<ProdusCategorie> {

    @Inject
    private Tree<ProdusCategorie> treeCat;

    @Named("fieldGroup.categorie")
    private LookupPickerField categorieField;

    @Override
    public void init(Map<String, Object> params) {
        super.init(params);
    }

    @Override
    protected void postInit() {
        super.postInit();
        treeCat.expandUpTo(3); //now working
        treeCat.setCaption("bau"); //working
        treeCat.setSelected(getItem().getCategorie()); //not working - IllegalStateException: Datasource doesn't contain items
    }
}

Thank you

Hi George,

This exception is thrown when the datasource does not contain an item (or items, because you can select multiple items at once) which is passed to setSelected(). The exception message is confusing so we should fix it - thanks for the report.

Hi Konstantin

in postInit() i use


treeCat.expandUpTo(3); //not working
treeCat.expandTree //not working, no error
treeCat.setCaption(getItem().getCategorie().getNume()); //working
treeCat.setSelected(getItem().getCategorie()); //not working - IllegalStateException: Datasource doesn't contain items ... the datasource contains the item because I can see it after i manually expand the tree

se comments above,

what am I not getting right?

Thank you

You can find out what’s wrong if you set a breakpoint in your IDE and step into the setSelected method.

Hi George,

I would suggest to try and move all these lines to the overridden

ready()

method (you can find it in this doc). Seems UI context is not ready for such operations on the

postInit()

stage.

Regards,

Aleksey

using ready() works! thank you