I have a listview and inside that listview is an
EnumdropDrop – Textfield – DropdownChoice – Ajaxbutton | for every item
The first dropdown is a currency and the second is an Account object, one of the choices in the account dropdown is to create a new Account which is then named after the currency.
When the new account is created is should be the selected value in the account dropdown.
This works fine for the first account, but the second and all subsequent newly created accounts are created correct and added to the list of possibilites but they are not the selected account. Everytime i make an ajax refresh the selected account in all newly created items are set as the first created account. The funny thing is when i submit the form, the account selections are correct.
package com.trifork.pengeplan.web.components.lists;
import com.trifork.pengeplan.domain.*;
import com.trifork.pengeplan.web.components.choice.EnumDropDownChoice;
import com.trifork.pengeplan.web.components.choice.OwnedAccountCreateNewDropDownChoice;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PropertyListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import java.util.List;
/**
* Created by IntelliJ IDEA.
* User: tommysadiqhinrichsen
* Date: 21/09/12
* Time: 11.55
*/
public class MultipleCurrenciesListView extends PropertyListView<FinanceAccountCurrencyMapping> {
IModel<OwnedAccount> ownedAccountIModel;
public MultipleCurrenciesListView(String id, IModel model) {
super(id, new PropertyModel<List<FinanceAccountCurrencyMapping>>(model, "financeAccountCurrencyMappings"));
ownedAccountIModel = model;
}
@Override
protected void populateItem(final ListItem<FinanceAccountCurrencyMapping> item) {
EnumDropDownChoice<MoneyCurrency> moneyCurrency = new EnumDropDownChoice<MoneyCurrency>("moneyCurrency");
moneyCurrency.add(new AjaxFormComponentUpdatingBehavior("onchange") {
@Override
protected void onUpdate(AjaxRequestTarget target) {
MoneyCurrency moneyCurrency = (MoneyCurrency) getFormComponent().getModelObject();
}
});
item.add(moneyCurrency);
item.add(new TextField("financeAccount.accountNumber"));
final OwnedAccountCreateNewDropDownChoice accountForCash = new OwnedAccountCreateNewDropDownChoice("accountForCash", ownedAccountIModel.getObject().getLegalEntity().getOwnedBankAccounts());
accountForCash.add(new AjaxFormComponentUpdatingBehavior("onchange") {
@Override
protected void onUpdate(AjaxRequestTarget target) {
OwnedAccount ownedAccount = (OwnedAccount) getFormComponent().getModelObject();
if (ownedAccount != null && ownedAccount.getId() == -1) {
OwnedAccount newOwnedAccount = new OwnedAccount();
FinanceAccountCurrencyMapping financeAccountCurrencyMapping = item.getModelObject();
financeAccountCurrencyMapping.setAccountForCash(ownedAccount);
newOwnedAccount.setName(financeAccountCurrencyMapping.getOwnedAccount().getName() + " - " + financeAccountCurrencyMapping.getMoneyCurrency().name());
newOwnedAccount.setLegalEntity(financeAccountCurrencyMapping.getOwnedAccount().getLegalEntity());
newOwnedAccount.setAssetOrLiability(AssetOrLiability.ASSET);
newOwnedAccount.setAssetType(AssetType.BANK_ACCOUNT);
newOwnedAccount.setCurrency(financeAccountCurrencyMapping.getMoneyCurrency());
newOwnedAccount.getFinanceAccountCurrencyMappings().add(new FinanceAccountCurrencyMapping(newOwnedAccount));
ownedAccountIModel.getObject().getLegalEntity().getOwnedAccounts().add(ownedAccountIModel.getObject().getLegalEntity().getOwnedAccounts().size() - 1, newOwnedAccount);
accountForCash.setDefaultModelObject(newOwnedAccount);
target.add(getFormComponent().getForm().getPage());
}
}
});
item.add(accountForCash);
item.add(new AjaxButton("deleteFinanceAccount", new ResourceModel("button.delete")) {
@Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
ownedAccountIModel.getObject().getFinanceAccountCurrencyMappings().remove(item.getModelObject());
target.add(this.getForm());
}
@Override
protected void onError(AjaxRequestTarget target, Form<?> form) {
super.onError();
}
});
}
}
Can someone tell me what the problem is?






UPDATED
package com.trifork.pengeplan.web.components.choice;
import com.trifork.pengeplan.domain.OwnedAccount;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import java.util.List;
/**
* Created by IntelliJ IDEA.
* User: tommysadiqhinrichsen
* Date: 18/09/12
* Time: 16.01
* To change this template use File | Settings | File Templates.
*/
public class OwnedAccountCreateNewDropDownChoice extends DropDownChoice<OwnedAccount> {
public OwnedAccountCreateNewDropDownChoice(String id) {
super(id);
List<OwnedAccount> choices = (List<OwnedAccount>) getChoices();
choices.add(new OwnedAccount(-1, new ResourceModel("create.new").getObject()));
setChoices(choices);
init();
}
public OwnedAccountCreateNewDropDownChoice(String id, IModel model) {
super(id, model);
List<OwnedAccount> choices = (List<OwnedAccount>) getChoices();
choices.add(new OwnedAccount(-1, new ResourceModel("create.new").getObject()));
setChoices(choices);
init();
}
public OwnedAccountCreateNewDropDownChoice(String id, List<OwnedAccount> choices) {
super(id, choices);
List<OwnedAccount> choices1 = (List<OwnedAccount>) getChoices();
choices.add(new OwnedAccount(-1, new ResourceModel("create.new").getObject()));
setChoices(choices);
init();
}
public OwnedAccountCreateNewDropDownChoice(String id, List<OwnedAccount> choices, ChoiceRenderer<OwnedAccount> renderer) {
super(id, choices, renderer);
List<OwnedAccount> choices2 = (List<OwnedAccount>) getChoices();
choices.add(new OwnedAccount(-1, new ResourceModel("create.new").getObject()));
setChoices(choices);
init();
}
public OwnedAccountCreateNewDropDownChoice(String id, Model<OwnedAccount> model, List<OwnedAccount> choices, ChoiceRenderer<OwnedAccount> renderer) {
super(id, model, choices, renderer);
List<OwnedAccount> choices3 = (List<OwnedAccount>) getChoices();
choices.add(new OwnedAccount(-1, new ResourceModel("create.new").getObject()));
setChoices(choices);
init();
}
public OwnedAccountCreateNewDropDownChoice(String id, PropertyModel<OwnedAccount> model, List<OwnedAccount> choices, ChoiceRenderer<OwnedAccount> renderer) {
super(id, model, choices, renderer);
List<OwnedAccount> choices4 = (List<OwnedAccount>) getChoices();
choices.add(new OwnedAccount(-1, new ResourceModel("create.new").getObject()));
setChoices(choices);
init();
}
public void init() {
}
public void addChoice(OwnedAccount ownedAccount) {
List<OwnedAccount> choices = (List<OwnedAccount>) getChoices();
choices.add(choices.size() - 1, ownedAccount);
setChoices(choices);
}
}

I found the problem 🙂
Listviews are not good in forms because the can’t handle dynamic changes because the list items dont keep track of their index.
I’ve implemented a listview based on a guide found at this website
http://wicketinaction.com/2008/10/building-a-listeditor-form-component/
I works like a charm.