// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package demo;

import java.util.List;
import java.util.ArrayList;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PropertyListView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.util.value.ValueMap;

public class TestPage extends WebPage {
    // setup page
    public static class AddForm extends Form<ValueMap> {
        // form options list
        public List<String> getOptions() {
            List<String> res = new ArrayList<String>();
            res.add(T1.VER);
            res.add(T1.NOTVER);
            return res;
        }
        // define form
        public AddForm(String name) {
            super(name, new CompoundPropertyModel<ValueMap>(new ValueMap()));
            add(new TextField<String>("f1"));
            add(new TextField<String>("f2"));
            add(new DropDownChoice<String>("f3", getOptions()));
        }
        // form submit
        public void onSubmit() {
            ValueMap values = getModelObject();
            int f1 = Integer.parseInt((String)values.get("f1"));
            String f2 = (String)values.get("f2");
            String f3 = (String)values.get("f3");
            DB.saveOne(new T1(f1, f2, f3));
        }
    }
    public TestPage() {
        // define table
        add(new PropertyListView<T1>("data", DB.getAll()) {
            @Override
            protected void populateItem(ListItem<T1> item) {
                item.add(new Label("f1"));
                item.add(new Label("f2"));
                item.add(new Label("f3"));
            }
        });
        add(new AddForm("addForm"));
    }
}
