Panel | ||||
---|---|---|---|---|
| ||||
|
One way
Code Block |
---|
import wicket.markup.html.WebPage; import wicket.markup.html.panel.FeedbackPanel; import wicket.markup.html.form.Form; import wicket.markup.html.form.TextField; import wicket.markup.html.form.Button; import wicket.markup.html.link.Link; import wicket.markup.html.basic.Label; import wicket.markup.html.list.ListView; import wicket.markup.html.list.ListItem; import wicket.model.CompoundPropertyModel; import wicket.IFeedback; import java.util.List; class PersonList extends WebPage { {panel} private List peopleList; {panel} {panel} public PersonList(final List peopleList) { this.peopleList = peopleList; this.peopleList.add(new Person("Lila")); this.peopleList.add(new Person("Bender")); this.peopleList.add(new Person("Fry")); {panel} {panel} add(new ListView("peopleList", this.peopleList) { protected void populateItem(ListItem listItem) { final Person person = (Person) listItem.getModelObject(); add(new Label("name", person.getName())); add(new Link("editPerson") { {panel} {panel} public void onClick() { setResponsePage(new PersonEdit(person, peopleList)); } }); } }); {panel} {panel} add(new Link("newPerson") { {panel} {panel} public void onClick() { setResponsePage(new PersonEdit(new Person(), peopleList)); } }); } {panel} } class PersonEdit extends WebPage { {panel} private FeedbackPanel feedbackPanel; private Person person; private List peopleList; {panel} {panel} public PersonEdit(Person person, List peopleList) { feedbackPanel = new FeedbackPanel("feedbackPanel"); add(feedbackPanel); {panel} {panel} add(new PersonForm("personForm", person, feedbackPanel)); } {panel} {panel} class PersonForm extends Form { public PersonForm(String s, Person person, IFeedback iFeedback) { super(s, new CompoundPropertyModel(person), iFeedback); add(new TextField("name")); add(new Button("save")); } {panel} {panel} public void onSubmit() { if (!peopleList.contains(person)) { peopleList.add(person); } setResponsePage(new PersonList(peopleList)); } } {panel} } class Person { {panel} public Person() { } {panel} {panel} public Person(String name) { this.name = name; } {panel} {panel} public String getName() { return name; } {panel} {panel} public void setName(String name) { this.name = name; } {panel} {panel} private String name; {panel} } |
or another...
Very useful, thanks.
In similar vein, when you want to have a Form either editable or Read-only, and you don't want to create two different Markups, WebPages etc. One way of doing it, is to use exactly the same WebPage, Form and Markup, and add an AttributeModifyer to each form component (<input...> tag). You'd then set a boolean field to true when you want the page to be displayed/edited.
So normally you'd have something like this:
Code Block |
---|
add(new TextField("total").add(IntegerValidator.INT));
|
(of course you don't always need an IntegerValidator)
Now create an AttributeModifyer that adds a readonly="readonly" attribute to the markup. The boolean field isReadOnly dictates whether the attribute is actually added to the output.
Code Block |
---|
boolean isReadOnly = true; // set to true if you want the form to be readOnly; false to be editable
AttributeModifier ro =
new AttributeModifier("readonly", isReadOnly, new Model("readonly")); // For most Form components
AttributeModifier disabled =
new AttributeModifier("disabled", isReadOnly, new Model("disabled")); // For DropDowns etc.
|
And if you don't like the Gray-ish, washed-out look of read-only controls, define a style in your stylesheet and
add that as well.
Code Block |
---|
AttributeModifier calcField =
new AttributeModifier("class", isReadOnly, new Model("CalculatedField"));// The style class is called CalculatedField
|
Now add the AttributeModifier to all your components, and the style/class attribute, eg:
Code Block |
---|
this.add(new TextField("total").add(IntegerValidator.INT).add(ro).add(calcField));
|
When you set isReadOnly to true now, the controls will be read-only, and will have the format you defined in your stylesheetClick on "Discussion" above for another way of doing it.