0

how to set the selected item in a listbox related to a bandbox

asked 2013-08-27 08:44:29 +0800

houari gravatar image houari
1

Hi all, and thanks for your precious help. Actually I am working in project developed using kz framework, and I am facing a problem, I have a window (entityCRUD.zul) to edit an entity, and I am using a bandbox to set a value for an attribute for that entity, when I select an item in the bandbox (ex for a new entity), I can set the attribute for the entity via (SubEntityListbox.getSelectedItem().getValue()), but when I try to edit an existing one (the bandbox is already filled with the stored value) without selecting in the bandbox, the instruction (SubEntityListbox.getSelectedItem()) returns a null, certainly, there’s something missing with my code, but I can’t find out what !

The source code :


-------- zul file --------


<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" arg0="./entityCRUD" ?>
<zk>
    <window title="Edit Entity" id="entityCRUD" border="normal"
         width="70%"
        apply="ah.ui.zk.controller.EntityCRUDController"
        mode="modal" maximizable="false" closable="true">
        <panel width="100%">
            <panelchildren>
                <grid>
                    <columns>
                        <column width="10%"></column>
                        <column width="20%"></column>
                        <column width="15%"></column>
                        <column width="65%"></column>
                    </columns>
                    <rows>
                        <row>
                            <label
                                value="${c:l('entity.codeEntity')}" />
                            <textbox id="codeEntity" hflex="1"
                                value="@{controller.selectedEntity.codeJEntity, access='both', save-when='none'}"
                                readonly="@{controller.makeAsReadOnly}" mold="rounded"
                                width="100%" />
                            <label
                                value="${c:l('entity.designation')}" />
                            <textbox id="designation" hflex="1"
                                value="@{controller.selectedEntity.designation, access='both', save-when='none'}"
                                readonly="@{controller.makeAsReadOnly}" mold="rounded"
                                width="100%" />
                        </row>
                        <row>
                            <label
                                value="${c:l('entity.subEntity')}" />
                            <bandbox id="bdSubEntity"
                                mold="rounded" autodrop="true"
                                value="@{controller.selectedEntity.subEntity.codeSubEntity}"
                                apply="ah.ui.zk.controller.SubEntitySearchController"
                                width="100%">
                                <bandpopup>
                                    <listbox height="250px"
                                        width="330px" id="SubEntityJournalListbox" multiple="false"
                                        emptyMessage="${c:l('MSG.EMPTY_ENTITY')}" mold="paging"
                                        pageSize="20" sizedByContent="false"
                                        onSelect="bdSubEntity.value=self.selectedItem.label; bdSubEntity.close();">
                                        <listhead sizable="true">
                                            <listheader
                                                label="${c:l('subEntity.codeSubEntity')}"/>
                                            <listheader
                                                label="${c:l('subEntity.designation')}"/>
                                        </listhead>
                                        <template name="model">
                                            <listitem>
                                                <listcell
                                                    label="${each.codeSubEntity}" />
                                                <listcell
                                                    label="${each.designation}" />
                                            </listitem>
                                        </template>
                                    </listbox>
                                </bandpopup>
                            </bandbox>
                        </row>
                    </rows>
                </grid>
            </panelchildren>
        </panel>
        ...
        <separator />
        <separator />
        <separator />
        <div align="right">
            <button label="${c:l('crud.save.lbl')}" id="save"
                width="73px" />
            <button label="${c:l('crud.cancel.lbl')}" id="cancel"
                width="73px" />
        </div>
        <separator />
    </window>
</zk>
*****************************************
--------- the controller ----------
*****************************************
public class EntityCRUDController extends GenericForwardComposer {
    @WireVariable
    private IEntityService CRUDService;

    private Entity selectedEntity;
    private String recordMode;
    private AnnotateDataBinder binder;
    protected Window entityCRUD;
    protected Button save;
    protected Button cancel;
    private Window parentWindow;
    private boolean makeAsReadOnly;
    private Listbox OperationsListbox;
    protected Listbox SubEntityListbox; 

    @SuppressWarnings("unchecked")
    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        CRUDService = (IEntityService) SpringUtil
                .getBean("EntityCRUDService");
        this.self.setAttribute("controller", this, false);

        final Execution execution = Executions.getCurrent();
        setSelectedJournal((Journal) execution.getArg().get("selectedRecord"));
        setRecordMode((String) execution.getArg().get("recordMode"));
        setParentWindow((Window) execution.getArg().get("parentWindow"));
        setMakeAsReadOnly(false);
        if (recordMode.equals("NEW")) {
            this.selectedEntity = new Entity();
        }
        if (recordMode.equals("EDIT")) {
            this.selectedEntity = getSelectedEntity();
        }
        if (recordMode == "READ") {
            setMakeAsReadOnly(true);
            save.setVisible(false);
            cancel.setLabel("Ok");
            this.selectedEntity = getSelectedEntity();
            entityCRUD.setTitle(entityCRUD.getTitle() + " (Read)");
        }
    }

    public void onCreate(Event event) {
        this.binder = (AnnotateDataBinder) event.getTarget().getAttribute(
                "binder", true);

    }

    public void onClick$save(Event event) {
        Map<String, Object> args = new HashMap<String, Object>();
        binder.saveAll();
        this.selectedEntity.setSubEntity((SubEntity) SubEntityListbox
                .getSelectedItem().getValue()); // pb here, in 'EDIT' mode ---> .getSelectedItem() is null !!!!
        CRUDService.save(this.selectedEntity);
        entityCRUD.detach();
    }

    public void onClick$cancel(Event event) {
        entityCRUD.detach();
    }

    public Entity getSelectedEntity() {
        return selectedEntity;
    }

    public void setSelectedEntity(Entity selectedEntity) {
        this.selectedEntity = selectedEntity;
    }

    public String getRecordMode() {
        return recordMode;
    }

    public void setRecordMode(String recordMode) {
        this.recordMode = recordMode;
    }

    public Window getParentWindow() {
        return parentWindow;
    }

    public void setParentWindow(Window parentWindow) {
        this.parentWindow = parentWindow;
    }

    public boolean isMakeAsReadOnly() {
        return makeAsReadOnly;
    }

    public void setMakeAsReadOnly(boolean makeAsReadOnly) {
        this.makeAsReadOnly = makeAsReadOnly;
    }
}
*****************************************
-------- the bandbox controller --------
*****************************************
public class CompteSearchController extends SelectorComposer<Component> {

    /**
     * 
     */
    private static final long serialVersionUID = -3099290267296712244L;

    @WireVariable
    private ISubEntityService CRUDService;

    @Wire
    protected Listbox SubEntityListbox;


    private ListModelList<SubEntity> entitiesList;
    private SubEntity subEntity;
    private SubEntity selectedSubEntity;

    public CompteSearchController() {
        super();
    }

    @Override
    public void doAfterCompose(Component window) throws Exception {
        super.doAfterCompose(window);       
        setSelectedCompte(null);
        doFillListbox();
    }   

    public void doFillListbox() {
        CRUDService = (ISubEntityService) SpringUtil
                .getBean("SubEntityCRUDService");
        List<SubEntity> list = CRUDService.getAll(SubEntity.class);
        subEntitiesList = new ListModelList<SubEntity>(list, true);
        setSubEntitiesList(SubEntitiesList);
        this.SubEntityListbox.setModel(SubEntitiesList);
    }


    public ListModelList<SubEntity> getSubEntitiesList() {
        return subEntitiesList;
    }

    public void setSubEntitiesList(ListModelList<SubEntity> subEntitiesList) {
        this.subEntitiesList = subEntitiesList;
    }

    public SubEntity getSelectedSubEntity() {
        return selectedSubEntity;
    }

    public void setSelectedSubEntity(SubEntity selectedSubEntity) {
        this.selectedSubEntity = selectedSubEntity;
    }

    public SubEntity getSubEntity() {
        return SubEntity;
    }

    public void setSubEntity(SubEntity subEntity) {
        this.subEntity = subEntity;
    }
}
delete flag offensive retag edit
Be the first one to answer this question!
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2013-08-27 08:44:29 +0800

Seen: 68 times

Last updated: Aug 27 '13

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More