Reputation: 804
How do i get a panel to use the some modelObject as the parent page?
I have a form that uses an OwnedAccount as its model, and in the form i have a custom panel which has a refreshingview containing a list of financeAccount. The problem is that changes to the financeaccounts are not being changed in the modelobject of the form.
Some code, i removed a lot of code where there is 3 dots "..."
@Entity
@Table(name = "ownedaccount")
public class OwnedAccount implements Serializable {
...
//used for multiple currencies
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "ownedAccount")
private List<FinanceAccount> financeAccounts = new ArrayList<FinanceAccount>();
...
}
public class AssetsPage extends LoggedInPage {
...
// bookmarkable constructor
public AssetsPage(PageParameters parameters) {
super(parameters);
init();
}
private void init() {
final OwnedAccount ownedAccount = getCurrentSelections().getSelectedOwnedAccount();
add(new FeedbackPanel("feedback"));
entityEdit = new OwnedAccountForm("entityEdit", ownedAccount);
add(entityEdit);
}
@Override
protected void selectionsChanged() {
OwnedAccount selectedOwnedAccount = getCurrentSelections().getSelectedOwnedAccount();
CompoundPropertyModel<OwnedAccount> model = new CompoundPropertyModel<OwnedAccount>(selectedOwnedAccount);
entityEdit.setModel(model);
}
...
class OwnedAccountForm extends BaseCreateEditForm<OwnedAccount, Void> {
...
public OwnedAccountForm(String s, OwnedAccount entity) {
super(s, entity, null);
assetType = entity.getAssetType();
}
@Override
protected void initComponents(Void initParams) {
...
multipleCurrenciesPanel = new MultipleCurrenciesPanel("multipleCurrenciesPanel", ownedAccountService, getCurrentSelections());
add(multipleCurrenciesPanel);
...
}
...
public class MultipleCurrenciesPanel extends Panel {
OwnedAccountService ownedAccountService;
CurrentSelections currentSelections;
public MultipleCurrenciesPanel(String id, OwnedAccountService ownedAccountService, CurrentSelections currentSelections) {
super(id);
this.ownedAccountService = ownedAccountService;
this.currentSelections = currentSelections;
init();
}
private void init() {
DepositoryLabel currencyLabel = new DepositoryLabel("currency", new ResourceModel("currency"));
add(currencyLabel);
DepositoryLabel accountForSecuritasLabel = new DepositoryLabel("account.for.securitas", new ResourceModel("account.for.securitas"));
add(accountForSecuritasLabel);
DepositoryLabel accountForCashLabel = new DepositoryLabel("account.for.cash", new ResourceModel("account.for.cash"));
add(accountForCashLabel);
DepositoryLabel buttonDeleteLabel = new DepositoryLabel("button.delete", new ResourceModel("button.delete"));
add(buttonDeleteLabel);
CurrenciesView currenciesView = new CurrenciesView("financeAccounts", ownedAccountService, currentSelections, this);
add(currenciesView);
setOutputMarkupId(true);
}
}
Updated 25/9 - 15:15
public class CurrenciesView extends RefreshingView<FinanceAccount> {
private OwnedAccountService ownedAccountService;
private CurrentSelections currentSelections;
private WebMarkupContainer multipleCurrenciesForDepot;
public CurrenciesView(String id, OwnedAccountService ownedAccountService, CurrentSelections currentSelections, WebMarkupContainer multipleCurrenciesForDepot) {
super(id);
this.ownedAccountService = ownedAccountService;
this.currentSelections = currentSelections;
this.multipleCurrenciesForDepot = multipleCurrenciesForDepot;
}
@Override
protected Iterator getItemModels() {
List<FinanceAccount> financeAccounts = ownedAccountService.getFinanceAccounts(currentSelections.getSelectedOwnedAccount());
return new ModelIteratorAdapter<FinanceAccount>(financeAccounts.iterator()) {
@Override
protected IModel<FinanceAccount> model(FinanceAccount object) {
return new CompoundPropertyModel<FinanceAccount>((object));
}
};
}
@Override
protected void populateItem(Item<FinanceAccount> item) {
final FinanceAccount financeAccount = item.getModelObject();
item.add(new EnumDropDownChoice<MoneyCurrency>("forCurrency"));
item.add(new TextField("accountNumber"));
item.add(new OwnedAccountDropDownChoice("accountForCash", currentSelections.getSelectedLegalEntity().getOwnedBankAccounts()));
item.add(new AjaxButton("deleteFinanceAccount") {
@Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
//TODO Delete finance account
ownedAccountService.deleteFinanceAccount(currentSelections.getSelectedOwnedAccount(), financeAccount);
target.add(multipleCurrenciesForDepot);
}
@Override
protected void onError(AjaxRequestTarget target, Form<?> form) {
//TODO create error message
}
});
}
}
Upvotes: 0
Views: 130
Reputation: 804
I saved the model reference passed in the constructor to the panel, so far its working ok.
BankAccountPanel bankAccountPanel = new BankAccountPanel("bankAccountPanel", getModel());
add(bankAccountPanel);
...
public class BankAccountPanel extends Panel {
IModel<OwnedAccount> iModel;
public BankAccountPanel(String id, IModel<OwnedAccount> model) {
super(id, model);
this.iModel = model;
init();
}
Upvotes: 0
Reputation: 955
CurrentSelections needs to model if its going to be used and mutiplated by two different wicket page/components.
For example theres a parent page, which has a constructor, a new String object and a Panel that uses the String object as a parameter,
public class ParentPage extends WebPage {
public ParentPage() {
String string = new String("Dave");
add(new Panel("childPanel", string));
string = new String("Brian");
}
}
If the string object is updated after the panel has been added, then that updated string isn't what the panel has. What the panel has is "Dave" when thought the ParentPage now has the string as "Brian".
But if we made a model and made it use the string object then when we change the string the childPanel will get the update.
public class ParentPage extends WebPage {
public ParentPage() {
String string = new String("Dave");
IModel model = new Model(string);
add(new Panel("childPanel", model));
model.setObject(new String("Brian"));
}
}
Its a very simple example but i hope it helps.
Upvotes: 1