List of usage examples for org.apache.wicket.markup.html.form ListChoice ListChoice
public ListChoice(String id, IModel<? extends List<? extends T>> choices)
From source file:org.jabylon.rest.ui.wicket.config.sections.VersionConfigSection.java
License:Open Source License
public VersionConfigSection(String id, IModel<ProjectVersion> model, ListModel<String> locales, Preferences prefs) {/*from w w w . j a va 2 s. co m*/ super(id, model); IModel<String> nameProperty = new EObjectPropertyModel<String, ProjectVersion>(model, PropertiesPackage.Literals.RESOLVABLE__NAME); TextField<String> field = new RequiredTextField<String>("inputName", nameProperty); field.add(UniqueNameValidator.fromCollection(getBranches(model), PropertiesPackage.Literals.RESOLVABLE__NAME, model.getObject())); add(field); ControlGroup readOnlyGroup = new ControlGroup("readonly-group", null, nls("readonly.help")); CheckBox readonly = new CheckBox("readonly", new EObjectPropertyModel<Boolean, ProjectVersion>(model, PropertiesPackage.Literals.PROJECT_VERSION__READ_ONLY)); readOnlyGroup.add(readonly); add(readOnlyGroup); // add(buildAddNewLink(getModel())); final WebMarkupContainer rowPanel = new WebMarkupContainer("rowPanel"); rowPanel.setOutputMarkupId(true); add(rowPanel); Form<?> form = new Form<Object>("locale-form"); form.setOutputMarkupId(true); rowPanel.add(form); final TextField<String> languageField = new TextField<String>("inputLanguage", Model.of("")); languageField.setOutputMarkupId(true); form.add(languageField); final TextField<String> countryField = new TextField<String>("inputCountry", Model.of("")); countryField.setOutputMarkupId(true); form.add(countryField); final TextField<String> variantField = new TextField<String>("inputVariant", Model.of("")); variantField.setOutputMarkupId(true); form.add(variantField); choiceModel = locales; final ListChoice<String> choice = new ListChoice<String>("children", choiceModel); choice.setModel( choiceModel.getObject().isEmpty() ? Model.of("") : Model.of(choiceModel.getObject().get(0))); choice.setMaxRows(15); choice.add(new AjaxFormComponentUpdatingBehavior("onchange") { private static final long serialVersionUID = 1L; @Override protected void onUpdate(AjaxRequestTarget target) { target.add(languageField); target.add(countryField); target.add(variantField); String localeString = choice.getModelObject(); if (localeString == null || TEMPLATE_ID.equals(localeString)) { languageField.setModelObject(""); countryField.setModelObject(""); variantField.setModelObject(""); } else { Locale locale = (Locale) PropertiesFactory.eINSTANCE .createFromString(PropertiesPackage.Literals.LOCALE, localeString); languageField.setModelObject(locale.getLanguage()); countryField.setModelObject(locale.getCountry()); variantField.setModelObject(locale.getVariant()); } } }); choice.setChoiceRenderer(new IChoiceRenderer<String>() { private static final long serialVersionUID = 1L; @Override public Object getDisplayValue(String object) { if (TEMPLATE_ID.equals(object)) return "Template"; Locale locale = (Locale) PropertiesFactory.eINSTANCE .createFromString(PropertiesPackage.Literals.LOCALE, object); String displayName = locale == null ? "Template" : locale.getDisplayName(getSession().getLocale()); return displayName; } @Override public String getIdValue(String locale, int index) { if (locale == null) return TEMPLATE_ID; return locale.toString(); } }); rowPanel.add(choice); AjaxSubmitLink addLink = new AjaxSubmitLink("addLocale", form) { private static final long serialVersionUID = 1L; @Override public void onSubmit(AjaxRequestTarget target, Form<?> form) { // ProjectLocale newLocale = PropertiesFactory.eINSTANCE.createProjectLocale(); Locale locale = new Locale(safeGet(languageField.getModelObject()), safeGet(countryField.getModelObject()), safeGet(variantField.getModelObject())); //make sure to avoid duplicates if (!choiceModel.getObject().contains(locale.toString())) choiceModel.getObject().add(locale.toString()); choice.setModelObject(locale.toString()); // newLocale.setLocale(locale); // PropertyResourceUtil.addNewLocale(newLocale, getModelObject()); if (target != null) target.add(rowPanel); } }; // addLink.setDefaultFormProcessing(false); form.add(addLink); AjaxSubmitLink removeLink = new AjaxSubmitLink("removeLocale", form) { private static final long serialVersionUID = 1L; @Override public void onSubmit(AjaxRequestTarget target, Form<?> form) { choiceModel.getObject().remove(choice.getModelObject()); // provider.getObject().remove(choice.getModelObject()); if (target != null) target.add(rowPanel); } }; // removeLink.setDefaultFormProcessing(false); form.add(removeLink); }