List of usage examples for org.apache.wicket.markup.html.form AbstractChoice setChoices
public final AbstractChoice<T, E> setChoices(List<? extends E> choices)
From source file:org.yes.cart.web.page.component.customer.address.AddressForm.java
License:Apache License
/** * Create address form./*from w w w .ja va 2s.c om*/ * * @param s form id * @param addressIModel address model. * @param addressType address type * @param successPage success page class * @param successPageParameters success page parameters * @param cancelPage optional cancel page class * @param cancelPageParameters optional cancel page parameters */ public AddressForm(final String s, final IModel<Address> addressIModel, final String addressType, final Class<? extends Page> successPage, final PageParameters successPageParameters, final Class<? extends Page> cancelPage, final PageParameters cancelPageParameters) { super(s, addressIModel); this.successPage = successPage; this.successPageParameters = successPageParameters; final Address address = addressIModel.getObject(); preprocessAddress(address); final boolean creating = address.getAddressId() == 0L; final List<State> stateList = getStateList(address.getCountryCode()); final List<Country> countryList = addressBookFacade.getAllCountries(ShopCodeContext.getShopCode(), addressType); RepeatingView fields = new RepeatingView(FIELDS); add(fields); final String lang = getLocale().getLanguage(); values = addressBookFacade.getShopCustomerAddressAttributes(address.getCustomer(), ApplicationDirector.getCurrentShop(), addressType); final Map<String, AttrValue> valuesMap = new HashMap<String, AttrValue>(); for (final AttrValue av : values) { valuesMap.put(av.getAttribute().getVal(), av); try { final Object val = PropertyUtils.getProperty(address, av.getAttribute().getVal()); av.setVal(val != null ? String.valueOf(val) : null); } catch (Exception e) { LOG.error("Unable to get address property for {}, prop {}", av.getAttribute(), av.getAttribute().getVal()); } } final AttrValue stateCode = valuesMap.get("stateCode"); final AbstractChoice<State, State> stateDropDownChoice = new DropDownChoice<State>(STATE, new StateModel(new PropertyModel(stateCode, "val"), stateList), stateList) .setChoiceRenderer(new StateRenderer()); final boolean needState = !stateList.isEmpty(); stateDropDownChoice.setRequired(needState); stateDropDownChoice.setVisible(needState); final AttrValue countryCode = valuesMap.get("countryCode"); final AbstractChoice<Country, Country> countryDropDownChoice = new DropDownChoice<Country>(COUNTRY, new CountryModel(new PropertyModel(countryCode, "val"), countryList), countryList) { @Override protected void onSelectionChanged(final Country country) { super.onSelectionChanged(country); stateDropDownChoice.setChoices(getStateList(country.getCountryCode())); final boolean needState = !stateDropDownChoice.getChoices().isEmpty(); stateDropDownChoice.setRequired(needState); stateDropDownChoice.setVisible(needState); if (valuesMap.containsKey("stateCode")) { valuesMap.get("stateCode").setVal(StringUtils.EMPTY); } } @Override protected boolean wantOnSelectionChangedNotifications() { return true; } }.setChoiceRenderer(new CountryRenderer()); countryDropDownChoice.setRequired(true); for (final AttrValue attrValue : values) { WebMarkupContainer row; if ("countryCode".equals(attrValue.getAttribute().getVal())) { row = new WebMarkupContainer(fields.newChildId()); row.add(getLabel(attrValue, lang)); row.add(new Fragment("editor", "countryCodeEditor", this).add(countryDropDownChoice)); } else if ("stateCode".equals(attrValue.getAttribute().getVal())) { row = new WebMarkupContainer(fields.newChildId()) { @Override public boolean isVisible() { return stateDropDownChoice.isVisible(); } }; row.add(getLabel(attrValue, lang)); row.add(new Fragment("editor", "stateCodeEditor", this).add(stateDropDownChoice)); } else { row = new WebMarkupContainer(fields.newChildId()); row.add(getLabel(attrValue, lang)); row.add(getEditor(attrValue, false)); } fields.add(row); } final AbstractLink submit = new SubmitLink(ADD_ADDRESS) { @Override public void onSubmit() { final Address addr = getModelObject(); final boolean isNew = addr.getAddressId() == 0; final ShoppingCart cart = ((AbstractWebPage) getPage()).getCurrentCart(); if (isNew || cart.getLogonState() == ShoppingCart.LOGGED_IN) { for (final AttrValue value : values) { try { PropertyUtils.setProperty(addr, value.getAttribute().getVal(), value.getVal()); } catch (Exception e) { LOG.error("Unable to set address property for {}, prop {}", value.getAttribute(), value.getAttribute().getVal()); } } final Shop shop = ((AbstractWebPage) getPage()).getCurrentCustomerShop(); addressBookFacade.createOrUpdate(addr, shop); // if we just added new address that became new default or we modified an address that is in the cart // reset address if (isNew && addr.isDefaultAddress() || Long.valueOf(addr.getAddressId()).equals(cart.getOrderInfo().getBillingAddressId()) || Long.valueOf(addr.getAddressId()) .equals(cart.getOrderInfo().getDeliveryAddressId())) { final String key = Address.ADDR_TYPE_BILLING.equals(addressType) ? ShoppingCartCommand.CMD_SETADDRESES_P_BILLING_ADDRESS : ShoppingCartCommand.CMD_SETADDRESES_P_DELIVERY_ADDRESS; shoppingCartCommandFactory.execute(ShoppingCartCommand.CMD_SETADDRESES, cart, (Map) new HashMap() { { put(ShoppingCartCommand.CMD_SETADDRESES, ShoppingCartCommand.CMD_SETADDRESES); put(key, addr); } }); } } setResponsePage(successPage, successPageParameters); } }; add(submit); submit.add(new Label("addAddressLabel", WicketUtil.createStringResourceModel(this, creating ? "create" : "save"))); final Component cancel = new SubmitLink(CANCEL_LINK) { @Override public void onSubmit() { setResponsePage(cancelPage, cancelPageParameters); } }.setDefaultFormProcessing(false).setVisible(cancelPage != null); add(cancel); }