package ch.difty.sipamato;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.OnChangeAjaxBehavior;
import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation;
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.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;

@AuthorizeInstantiation("USER")
public class PaperEntryPage extends BasePage {

    private static final long serialVersionUID = 1L;

    public PaperEntryPage(PageParameters parameters) {
        super(parameters);
    }

    protected void onInitialize() {
        super.onInitialize();

        Form<Paper> form = new Form<Paper>("form", new CompoundPropertyModel<Paper>(Model.of(new Paper()))) {
            private static final long serialVersionUID = 1L;

            @Override
            protected void onSubmit() {
                System.out.println("form submitted. Paper:");
                System.out.println(getModelObject());
                info("form submitted. Paper:" + getModelObject());
            }
        };
        add(form);

        form.add(new Label("authorLabel", new StringResourceModel("author.label", this, null)));
        TextField<String> authorField = new TextField<String>(Paper.AUTHOR);
        form.add(authorField);

        form.add(new Label("firstAuthorLabel", new StringResourceModel("firstAuthor.label", this, null)));
        TextField<String> firstAuthorField = new TextField<String>(Paper.FIRST_AUTHOR);
        firstAuthorField.setEnabled(false);
        firstAuthorField.setOutputMarkupId(true);
        form.add(firstAuthorField);

        authorField.add(new OnChangeAjaxBehavior() {
            private static final long serialVersionUID = 1L;

            @Override
            protected void onUpdate(AjaxRequestTarget target) {
                target.add(firstAuthorField);
            }
        });

        form.add(new Label("titleLabel", new StringResourceModel("title.label", this, null)));
        TextField<String> titleField = new TextField<String>(Paper.TITLE);
        form.add(titleField);

        form.add(new Label("locationLabel", new StringResourceModel("location.label", this, null)));
        TextField<String> locationField = new TextField<String>(Paper.LOCATION);
        form.add(locationField);
    };
}

@@ -326,16 +326,134 @@
 m.Form;%0A
+import org.apache.wicket.markup.html.form.FormComponent;%0Aimport org.apache.wicket.markup.html.form.RequiredTextField;%0A
 import o
@@ -931,16 +931,47 @@
 %0A    %7D%0A%0A
+    private Form%3CPaper%3E form;%0A%0A
     prot
@@ -1036,28 +1036,16 @@
 %0A       
- Form%3CPaper%3E
  form = 
@@ -1255,124 +1255,8 @@
 ) %7B%0A
-                System.out.println(%22form submitted. Paper:%22);%0A                System.out.println(getModelObject());%0A
     
@@ -1367,105 +1367,8 @@
 );%0A%0A
-        form.add(new Label(%22authorLabel%22, new StringResourceModel(%22author.label%22, this, null)));%0A
     
@@ -1399,32 +1399,40 @@
 thorField = new 
+Required
 TextField%3CString
@@ -1460,139 +1460,40 @@
     
-form.add(authorField);%0A%0A        form.add(new Label(%22firstAuthorLabel%22, new StringResourceModel(%22firstAuthor.label%22, this, null))
+addFieldAndLabel(authorField
 );%0A
+%0A
     
@@ -1678,24 +1678,32 @@
         
-form.add
+addFieldAndLabel
 (firstAu
@@ -2001,91 +2001,66 @@
     
-form.add(new Label(%22title
+addFieldAnd
 Label
-%22, 
+(
 new 
-StringResourceModel(%22title.label%22, this, null)
+RequiredTextField%3CString%3E(Paper.TITLE
 ));%0A
@@ -2071,39 +2071,25 @@
     
-Text
+add
 Field
-%3CString%3E titleField = 
+AndLabel(
 new 
@@ -2116,46 +2116,91 @@
 per.
-TITLE
+LOCATION)
 );%0A    
+%7D%0A%0A
     
-form.add(titleF
+private void addFieldAndLabel(FormComponent%3CString%3E f
 ield)
-;%0A
+ %7B
 %0A   
@@ -2208,43 +2208,75 @@
     
-form.add(new Label(%22locationLabel%22,
+String id = field.getId();%0A        StringResourceModel labelModel =
  new
@@ -2300,17 +2300,14 @@
 del(
-%22location
+id + %22
 .lab
@@ -2322,18 +2322,16 @@
 s, null)
-))
 ;%0A      
@@ -2336,107 +2336,138 @@
     
-TextField%3CString%3E locationFi
+Label lab
 el
-d
  = new 
-TextField%3CString%3E(Paper.LOCATION);%0A        form.add(locationF
+Label(id + %22Label%22, labelModel);%0A        field.setLabel(labelModel);%0A        form.add(label);%0A        form.add(f
 ield
@@ -2474,12 +2474,12 @@
 );%0A    %7D
-;
+%0A
 %0A%7D%0A

Simplify creating labeled field