Darya Zheltoukhova Darya Zheltoukhova - 1 year ago 75
Java Question

ZK, custom component. Different function (load and save) for one value

ZK Framework. I have custom component v_dualListbox.zul:



<hlayout hflex="1">
<listbox id="candidateLb" hflex="1" vflex="true" multiple="true" rows="8">
<template name="model">
<listitem>
<listcell label="${each.description}"/>
</listitem>
</template>
</listbox>
<vbox spacing="10px" width="24px">
<image style="cursor:pointer" id="chooseAllBtn" src="/img/001_25.png"/>
<image style="cursor:pointer" id="chooseBtn" src="/img/001_25.png"/>
<image style="cursor:pointer" id="removeBtn" src="/img/001_27.png"/>
<image style="cursor:pointer" id="removeAllBtn" src="/img/001_27.png"/>
</vbox>
<listbox id="chosenLb" hflex="1" vflex="true" multiple="true" rows="8">
<template name="model">
<listitem>
<listcell label="${each.description}"/>
</listitem>
</template>
</listbox> </hlayout>





I use it on zul page:



<?component name="dual-listbox" extends="div" class="ru.it_constanta.pguAdmin.components.DualListbox"?>
<dual-listbox id="scopeDualLBox" chosenDataList="@bind(vm.orgScopeList)" model="@bind(vm.scopeList)"/>





DualListbox.java:

package ru.it_constanta.pguAdmin.components;

import ...
public class DualListbox<T> extends HtmlMacroComponent implements IdSpace {

private static final long serialVersionUID = 5183321186606483396L;

@Wire
private Listbox candidateLb;
@Wire
private Listbox chosenLb;

private ListModelList<T> candidateModel = new ListModelList<>();
private ListModelList<T> chosenDataModel = new ListModelList<>();

private boolean isLoad = false;

public DualListbox() {
Executions.createComponents("v_dualListbox.zul", this, null);
Selectors.wireComponents(this, this, false);
Selectors.wireEventListeners(this, this);
chosenLb.setModel(chosenDataModel = new ListModelList<T>());
chosenDataModel.setMultiple(true);
}

@Listen("onClick = #chooseBtn")
public void chooseItem() {
Events.postEvent(new ChooseEvent(this, chooseOne()));
}

@Listen("onClick = #removeBtn")
public void unchooseItem() {
Events.postEvent(new ChooseEvent(this, unchooseOne()));
}

@Listen("onClick = #chooseAllBtn")
public void chooseAllItem() {
Events.postEvent(new ChooseEvent(this, chooseAll()));
}

@Listen("onClick = #removeAllBtn")
public void unchooseAllItem() {
Events.postEvent(new ChooseEvent(this, unchooseAll()));
}

/**
* Set new candidate ListModelList.
*
* @param candidate is the data of candidate list model
*/
public void setModel(List<T> candidate) {
candidateLb.setModel(this.candidateModel = new ListModelList<>(candidate));
this.candidateModel.setMultiple(true);
chosenDataModel.clear();
}

@ComponentAnnotation(
"@ZKBIND(ACCESS=load, LOAD_EVENT=onLoad)")
public void setChosenDataList(List<T> chosen) {
chosenDataModel.addAll(chosen);
candidateModel.removeAll(chosen);
}

/**
* @return current chosen data list
*/
@ComponentAnnotation(
"@ZKBIND(ACCESS=save, SAVE_EVENT=onChoose)")
public List<T> getChosenDataList() {
return new ArrayList<>(chosenDataModel);
}

private Set<T> chooseOne() {
Set<T> set = candidateModel.getSelection();
chosenDataModel.addAll(set);
candidateModel.removeAll(set);
return set;
}

private Set<T> unchooseOne() {
Set<T> set = chosenDataModel.getSelection();
candidateModel.addAll(set);
chosenDataModel.removeAll(set);
return set;
}

private Set<T> chooseAll() {
chosenDataModel.addAll(candidateModel);
candidateModel.clear();
return chosenDataModel.getSelection();
}

private Set<T> unchooseAll() {
candidateModel.addAll(chosenDataModel);
chosenDataModel.clear();
return candidateModel.getSelection();
}

// Customized Event
public class ChooseEvent extends Event {
private static final long serialVersionUID = -7334906383953342976L;

public ChooseEvent(Component target, Set<T> data) {
super("onChoose", target, data);
}
}
}


The data from database. On load page I want put list of entity (orgScopeList) which already object has, I want to see them in chosenLb listbox. I use chosenDataList attribute for this. Also I want to save chosen object in same list (orgScopeList), i. e when client chooses more entity or unchooses it, I want to save it in orgScopeList, that why I use @bind annotation (for loading and for saving). So I need to listen two events onLoad and my onChoose for two commands (load and save). I wrote two methods with @ComponentAnnotation, but setChosenDataList doesn't work, nothing happens on load page and I don't know why.
I hope somebody understands me :) Help please!

Answer Source

Thanks to Malte Hartwig for answer, both of his advices are working (to use @ComponentAnnotations only on getter and to use onCreate event as LOAD_EVENT), so that's what I have now in my DualListbox.java:

public void setChosenDataList(List<T> chosen) {
    chosenDataModel.clear();
    chosenDataModel.addAll(chosen);
    candidateModel.removeAll(chosen);
}

@ComponentAnnotation(
        "@ZKBIND(ACCESS=both, SAVE_EVENT=onChoose, LOAD_EVENT=onCreate)")
public List<T> getChosenDataList() {
    return new ArrayList<>(chosenDataModel);
}

But it is important to remember that LOAD_EVENT works after SAVE_EVENT too, so that's why chosenDataModel.clear(); is written.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download