Pages

Wednesday, May 9, 2012

Vaadin - Form Data Saving into a second form

Add the following unit into your vaadin application (it runs under the lehel.sipos.vaadin package):

/*
 * MyApplication.java
 *
 * Created on 2012. május 9., 12:16
 */

package lehel.sipos.vaadin;          

import com.vaadin.Application;
import com.vaadin.data.Validator.EmptyValueException;
import com.vaadin.data.util.ObjectProperty;
import com.vaadin.data.util.PropertysetItem;
import com.vaadin.data.validator.StringLengthValidator;
import com.vaadin.ui.*;
import com.vaadin.ui.themes.Reindeer;
/**
 *
 * @author Sipos Lehel
 * @version
 */

public class MyApplication extends Application {

    @Override
    public void init() {
    Window mainWindow = new Window("MyApplication");
              
        // Some data source
        PropertysetItem item = new PropertysetItem();
        item.addItemProperty("firstName", new ObjectProperty<String>(""));
        item.addItemProperty("lastName",  new ObjectProperty<String>(""));
       
        // A buffered form bound to a data source - main form for input data
        final Form form = new Form();
        form.setItemDataSource(item);
        form.setWriteThrough(false);
       
        //caption of text fields in main form
        form.getField("firstName").setCaption("Keresztnév");
        form.getField("lastName").setCaption("Vezetéknév");       

        // A required field
        form.getField("firstName").setRequired(true);
       
        // A required field with an error message
        TextField lastName = (TextField) form.getField("lastName");
        lastName.setRequired(true);
        lastName.setRequiredError("Add meg a vezetéknevet");
        lastName.setImmediate(true);
        lastName.setValidationVisible(true);
        lastName.addValidator(new StringLengthValidator("Nem lehet üres", 1, 100, false));

        // Commit the buffered form data to the data source
        form.getFooter().addComponent(new Button("Mentés", new Button.ClickListener() {
            public void buttonClick(Button.ClickEvent event) {
                try {
                    if (!form.isValid())
                        form.setValidationVisible(true);
                    else
                        form.commit();
                } catch (EmptyValueException e) {
                    // A required value was missing
                }
            }
        }));

        // Have a read-only form that displays the content of the first form after a data commit
        Form roform = new Form();
        roform.setItemDataSource(item);       
        roform.addStyleName(Reindeer.LAYOUT_WHITE);       
        roform.setReadOnly(true);
        //caption of text fields in the readonly form form
        roform.getField("firstName").setCaption("Elmentett keresztnév");
        roform.getField("lastName").setCaption("Elmentett vezetéknév");       
       
       
       
        //putting the input form near the readonly form
        HorizontalLayout hlayout = new HorizontalLayout();
        hlayout.setSpacing(true);
        hlayout.setWidth("600px");
        hlayout.addComponent(form);
        hlayout.addComponent(roform);       
       
        mainWindow.addComponent(hlayout);
        setMainWindow(mainWindow);
    }

}

No comments:

Post a Comment