package org.apache.wicket.examples.forminput;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import org.apache.wicket.examples.WicketExamplePage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Check;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.CheckGroup;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponentUpdatingBehavior;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.ListMultipleChoice;
import org.apache.wicket.markup.html.form.Radio;
import org.apache.wicket.markup.html.form.RadioChoice;
import org.apache.wicket.markup.html.form.RadioGroup;
import org.apache.wicket.markup.html.form.SimpleFormComponentLabel;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.http.WebRequest;
import org.apache.wicket.util.convert.ConversionException;
import org.apache.wicket.util.convert.IConverter;
import org.apache.wicket.util.convert.MaskConverter;
import org.apache.wicket.validation.validator.RangeValidator;
@author
@author
public class FormInput extends WicketExamplePage
{
private class InputForm extends Form<FormInputModel>
{
@param
@SuppressWarnings("serial")
public InputForm(String name)
{
super(name, new CompoundPropertyModel<>(new FormInputModel()));
add(new LocaleDropDownChoice("localeSelect"));
add(new Link<Void>("defaultLocaleLink") {
public void onClick() {
WebRequest request = (WebRequest)getRequest();
setLocale(request.getLocale());
}
});
add(new TextField<String>("stringProperty").setRequired(true).setLabel(
new Model<>("String")));
add(new TextField<>("integerProperty", Integer.class).setRequired(true).add(
new RangeValidator<>(1, Integer.MAX_VALUE)));
add(new TextField<>("doubleProperty", Double.class).setRequired(true));
add(new TextField<Integer>("integerInRangeProperty").setRequired(true).add(
new RangeValidator<>(0, 100)));
add(new CheckBox("booleanProperty"));
add(new Multiply("multiply"));
Label multiplyLabel = new Label("multiplyLabel", new PropertyModel<Integer>(
getDefaultModel(), "multiply"));
multiplyLabel.add(new BeforeAndAfterBorder());
add(multiplyLabel);
RadioChoice<String> rc = new RadioChoice<>("numberRadioChoice", NUMBERS).setSuffix("");
rc.setLabel(new Model<>("number"));
rc.setRequired(true);
add(rc);
RadioGroup<String> group = new RadioGroup<>("numbersGroup");
add(group);
ListView<String> persons = new ListView<String>("numbers", NUMBERS)
{
@Override
protected void populateItem(ListItem<String> item)
{
Radio<String> radio = new Radio<>("radio", item.getModel());
radio.setLabel(item.getModel());
item.add(radio);
item.add(new SimpleFormComponentLabel("number", radio));
}
}.setReuseItems(true);
group.add(persons);
CheckGroup<String> checks = new CheckGroup<>("numbersCheckGroup");
add(checks);
ListView<String> checksList = new ListView<String>("numbers", NUMBERS)
{
@Override
protected void populateItem(ListItem<String> item)
{
Check<String> check = new Check<>("check", item.getModel());
check.setLabel(item.getModel());
item.add(check);
item.add(new SimpleFormComponentLabel("number", check));
}
}.setReuseItems(true);
checks.add(checksList);
add(new ListMultipleChoice<>("siteSelection", SITES));
add(new TextField<URL>("urlProperty", URL.class)
{
@Override
protected IConverter<?> createConverter(Class<?> type)
{
if (URL.class.isAssignableFrom(type))
{
return URLConverter.INSTANCE;
}
return null;
}
});
add(new TextField<UsPhoneNumber>("phoneNumberUS", UsPhoneNumber.class)
{
@Override
protected IConverter<?> createConverter(Class<?> type)
{
if (UsPhoneNumber.class.isAssignableFrom(type))
{
return new MaskConverter<>("(###) ###-####", UsPhoneNumber.class);
}
return null;
}
});
add(new LinesListView("lines"));
add(new Button("saveButton"));
add(new Button("resetButton") {
@Override
public void onSubmit()
{
setResponsePage(FormInput.class);
}
}.setDefaultFormProcessing(false));
}
@Override
public void onSubmit()
{
info("Saved model " + getDefaultModelObject());
}
}
private static final class LinesListView extends ListView<String>
{
@param
public LinesListView(String id)
{
super(id);
setReuseItems(true);
}
@Override
protected void populateItem(ListItem<String> item)
{
item.add(new TextField<>("lineEdit", new PropertyModel<String>(
item.getDefaultModel(), "text")));
}
}
private final class LocaleChoiceRenderer implements IChoiceRenderer<Locale>
{
@Override
public Object getDisplayValue(Locale locale)
{
return locale.getDisplayName(getLocale());
}
}
private final class LocaleDropDownChoice extends DropDownChoice<Locale>
{
@param
public LocaleDropDownChoice(String id)
{
super(id, FormInputApplication.LOCALES, new LocaleChoiceRenderer());
setModel(new PropertyModel<>(FormInput.this, "locale"));
add(new FormComponentUpdatingBehavior() {
@Override
protected void onUpdate()
{
setResponsePage(FormInput.class);
}
});
}
}
private static final List<String> SITES = Arrays.asList("The Server Side", "Java Lobby",
"Java.Net");
static final List<String> NUMBERS = Arrays.asList("1", "2", "3");
public FormInput()
{
final FeedbackPanel feedback = new FeedbackPanel("feedback");
add(feedback);
add(new InputForm("inputForm"));
}
@param
public void setLocale(Locale locale)
{
if (locale != null)
{
getSession().setLocale(locale);
}
}
private static class URLConverter implements IConverter<URL>
{
public static final URLConverter INSTANCE = new URLConverter();
@Override
public URL convertToObject(String value, Locale locale)
{
try
{
return new URL(value);
}
catch (MalformedURLException e)
{
throw new ConversionException("'" + value + "' is not a valid URL");
}
}
@Override
public String convertToString(URL value, Locale locale)
{
return value != null ? value.toString() : null;
}
}
}