Editable Grid (1)

A table built with a Form around a Grid to allow creation of up to 5 persons.

To demonstrate a server-side validation error, leave one or more fields of a person empty.
First NameLast NameRegionStart Date
[Show]
[Show]
[Show]
[Show]
[Show]

Refresh
Features:
For more flexibility, see the Editable Loop examples.

References: Grid, Forms and Validation.

Home

The source for PersonFinderService, @EJB handling, etc. is shown in the @EJB example.

EditableGrid1.tml


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- We need a doctype to allow us to use special characters like &nbsp; 
     We use a "strict" DTD to make IE follow the alignment rules. -->
     
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd" xmlns:p="tapestry:parameter">
<head>
    <link rel="stylesheet" type="text/css" href="${context:css/examples/tables/styledgrid.css}"/>
</head>
<body>
    <h1>Editable Grid (1)</h1>

    A table built with a Form around a Grid to allow creation of up to ${LIST_SIZE} persons.<br/><br/>
    
    To demonstrate a server-side validation error, leave one or more fields of a person empty.<br/>
    
    <div class="eg">
        <form t:type="form" t:id="personsCreate">
            <t:errors/>
            <table t:type="Grid" t:source="persons" t:row="person" t:model="model" 
                t:include="firstname,lastname,region,startdate">[Persons Grid here]
                <p:firstNameCell>
                    <input t:id="firstName" t:type="TextField" t:value="person.firstName" t:validate="maxlength=10" size="10"/>
                </p:firstNameCell>
                <p:lastNameCell>
                    <input t:id="lastName" t:type="TextField" t:value="person.lastName" t:validate="maxlength=10" size="10"/>
                </p:lastNameCell>
                <p:regionCell>
                    <input t:id="region" t:type="Select" t:value="person.region"/>
                </p:regionCell>
                <p:startDateCell>
                    <input t:id="startDate" t:type="DateField" t:value="person.startDate" t:format="prop:dateFormat" size="14"/>
                </p:startDateCell>
            </table><br/>
            <input type="submit" value="Save"/>
            <a t:type="eventlink" t:event="refresh" href="#" style="margin-left: 5px;">Refresh</a>
        </form>
    </div>

    Features:<br/>
    <ul>
    <li>On error, we redisplay the list. Tapestry redirects the browser to redisplay the page.</li>
    <li>Your input values are redisplayed correctly because a feature of Form is that it carries them through the redirect and redisplays them.</li>
    </ul>
    
    For more flexibility, see the Editable Loop examples.<br/><br/>
    
    References: 
    <a href="http://tapestry.apache.org/5.3.7/apidocs/org/apache/tapestry5/corelib/components/Grid.html">Grid</a>,
    <a href="http://tapestry.apache.org/forms-and-validation.html">Forms and Validation</a>.<br/><br/> 
    
    <a t:type="pagelink" t:page="Index" href="#">Home</a><br/><br/>
    
    The source for PersonFinderService, @EJB handling, etc. is shown in the @EJB example.<br/><br/>

    <t:sourcecodedisplay src="/web/src/main/java/jumpstart/web/pages/examples/tables/EditableGrid1.tml"/>
    <t:sourcecodedisplay src="/web/src/main/java/jumpstart/web/pages/examples/tables/EditableGrid1.java"/>
    <t:sourcecodedisplay src="/web/src/main/java/jumpstart/web/css/examples/tables/styledgrid.css"/>
    <t:sourcecodedisplay src="/web/src/main/java/jumpstart/web/commons/FieldCopy.java"/>
    <t:sourcecodedisplay src="/business/src/main/java/jumpstart/business/domain/person/Person.java"/>
    <t:sourcecodedisplay src="/business/src/main/java/jumpstart/business/domain/person/Regions.java"/>
</body>
</html>

EditableGrid1.java


package jumpstart.web.pages.examples.tables;

import java.text.DateFormat;
import java.text.Format;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.ejb.EJB;

import jumpstart.business.domain.person.Person;
import jumpstart.business.domain.person.Regions;
import jumpstart.business.domain.person.iface.IPersonFinderServiceLocal;
import jumpstart.util.ExceptionUtil;
import jumpstart.util.StringUtil;
import jumpstart.web.commons.FieldCopy;

import org.apache.tapestry5.Field;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.beaneditor.BeanModel;
import org.apache.tapestry5.corelib.components.DateField;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.corelib.components.Select;
import org.apache.tapestry5.corelib.components.TextField;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.BeanModelSource;

public class EditableGrid1 {
    private static final String REQUIRED_MSG_KEY = "required";

    // Screen fields

    @Property
    private List<Person> persons;

    @Property
    private Person person;

    @Property
    private BeanModel<Person> model;

    @Property
    private final int LIST_SIZE = 5;

    // Work fields

    private int rowNum;
    private Map<Integer, FieldCopy> firstNameCopyByRowNum;
    private Map<Integer, FieldCopy> lastNameCopyByRowNum;
    private Map<Integer, FieldCopy> regionCopyByRowNum;
    private Map<Integer, FieldCopy> startDateCopyByRowNum;

    private List<Person> personsToCreate;

    // Other pages

    @InjectPage
    private EditableGrid2 page2;

    // Generally useful bits and pieces

    @Component(id = "personsCreate")
    private Form form;

    @InjectComponent
    private TextField firstName;

    @InjectComponent
    private TextField lastName;

    @InjectComponent
    private Select region;

    @InjectComponent
    private DateField startDate;

    @Inject
    private BeanModelSource beanModelSource;

    @Inject
    private Messages messages;

    @EJB
    private IPersonFinderServiceLocal personFinderService;

    @Inject
    private Locale currentLocale;

    // The code

    // Form bubbles up the PREPARE_FOR_RENDER event during form render.

    void onPrepareForRender() {
        createPersonsList();

        // If fresh start (ie. not rendering after a redirect), add an example person.

        if (form.isValid()) {
            persons.set(0, new Person("Example", "Person", Regions.EAST_COAST, getTodayDate()));
        }
    }

    // Form bubbles up the PREPARE_FOR_SUBMIT event during form submission.

    void onPrepareForSubmit() {
        // Create the same list as was rendered.
        // Loop will write its input field values into the list's objects.

        createPersonsList();

        // Prepare to take a copy of each field.

        rowNum = 0;
        firstNameCopyByRowNum = new HashMap<Integer, FieldCopy>();
        lastNameCopyByRowNum = new HashMap<Integer, FieldCopy>();
        regionCopyByRowNum = new HashMap<Integer, FieldCopy>();
        startDateCopyByRowNum = new HashMap<Integer, FieldCopy>();
    }

    // Form bubbles up the PREPARE event during form render and form submission.

    void onPrepare() {

        // Configure the Grid to be unsortable

        model = beanModelSource.createDisplayModel(Person.class, messages);

        for (String propertyName : model.getPropertyNames()) {
            model.get(propertyName).sortable(false);
        }
    }

    void createPersonsList() {
        persons = new ArrayList<Person>();

        // Populate the list with as many empty objects as you want displayed.

        for (int i = 0; i < LIST_SIZE; i++) {
            persons.add(new Person());
        }
    }

    void onValidateFromFirstName() {
        rowNum++;
        firstNameCopyByRowNum.put(rowNum, new FieldCopy(firstName));
    }

    void onValidateFromLastName() {
        lastNameCopyByRowNum.put(rowNum, new FieldCopy(lastName));
    }

    void onValidateFromRegion() {
        regionCopyByRowNum.put(rowNum, new FieldCopy(region));
    }

    void onValidateFromStartDate() {
        startDateCopyByRowNum.put(rowNum, new FieldCopy(startDate));
    }

    void onValidateFromPersonsCreate() {

        if (form.getHasErrors()) {
            // We get here only if a server-side validator detected an error.
            return;
        }

        personsToCreate = new ArrayList<Person>();

        // Error if any person has fields entered but not all of them.

        rowNum = 0;

        for (Person person : persons) {
            rowNum++;

            if (StringUtil.isNotEmpty(person.getFirstName()) || StringUtil.isNotEmpty(person.getLastName())
                    || person.getRegion() != null || person.getStartDate() != null) {

                // Unfortunately, at this point the fields firstName, lastName, etc. are from the final row of the Grid.
                // Fortunately, we have a copy of the correct fields, so we can record the error with those.

                if (StringUtil.isEmpty(person.getFirstName())) {
                    Field field = firstNameCopyByRowNum.get(rowNum);
                    form.recordError(field, messages.format(REQUIRED_MSG_KEY, field.getLabel()));
                    return;
                }
                else if (StringUtil.isEmpty(person.getLastName())) {
                    Field field = lastNameCopyByRowNum.get(rowNum);
                    form.recordError(field, messages.format(REQUIRED_MSG_KEY, field.getLabel()));
                    return;
                }
                else if (person.getRegion() == null) {
                    Field field = regionCopyByRowNum.get(rowNum);
                    form.recordError(field, messages.format(REQUIRED_MSG_KEY, field.getLabel()));
                    return;
                }
                else if (person.getStartDate() == null) {
                    Field field = startDateCopyByRowNum.get(rowNum);
                    form.recordError(field, messages.format(REQUIRED_MSG_KEY, field.getLabel()));
                    return;
                }

                personsToCreate.add(person);
            }
        }

        try {
            System.out.println(">>> personsToCreate = " + personsToCreate);
            // In a real application we would persist them to the database instead of printing them
            // personManagerService.createPersons(personsToCreate);
        }
        catch (Exception e) {
            // Display the cause. In a real system we would try harder to get a user-friendly message.
            form.recordError(ExceptionUtil.getRootCauseMessage(e));
        }
    }

    Object onSuccess() {
        page2.set(personsToCreate);
        return page2;
    }

    void onFailure() {
        // Unnecessary method. Loop will carry the submitted input field values through the redirect.
    }

    void onRefresh() {
        // By doing nothing the page will be displayed afresh.
    }

    public Format getDateFormat() {
        return DateFormat.getDateInstance(DateFormat.SHORT, currentLocale);
    }

    private Date getTodayDate() {
        Calendar now = Calendar.getInstance();
        Calendar today = Calendar.getInstance();
        today.clear();
        today.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH));
        return today.getTime();
    }

}

styledgrid.css


body            { font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 13px; font-weight: normal; color: #333;
                    line-height: 17px; }
h1              { font-size: 26px; line-height: 20px; } /* For IE 7 */

.eg             { margin: 20px 0; padding: 20px; 
                    border: 1px solid #ddd; border-radius: 4px; -webkit-border-radius: 4px; -mox-border-radius: 4px; }

a               { text-decoration: none; color: #3D69B6; }
a:hover         { text-decoration: underline; }

/* 
    The following elements demonstrate one way to override Tapestry's Grid CSS.
    Another way is to assign a your own CSS class in Grid's class parameter. 
*/ 

.eg img.t-sort-icon         { vertical-align: bottom; }
.eg div.t-data-grid         { font-family: Arial, Helvetica, sans-serif; }
.eg div.t-data-grid-pager   { margin: 0 0 8px; }
.eg div.t-data-grid-pager a, 
.eg div.t-data-grid-pager span.current  { font-size: 13px; }
.eg div.t-data-grid-pager span.current  { text-shadow: 0px -1px 0px #4D5F99; }

.eg table.t-data-grid th    { min-width: 130px; }
.eg table.t-data-grid th a  { text-decoration: none; text-shadow: 0px -1px 0px #4D5F99; }

.eg table.t-data-grid thead tr  { 
                    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#8DA9FF', endColorstr='#738FE6'); /* for IE */
                    background: -webkit-gradient(linear, left top, left bottom, from(#8DA9FF), to(#738FE6)); /* for webkit browsers */
                    background: -moz-linear-gradient(top, #8DA9FF, #738FE6); /* for firefox 3.6+ */ }

FieldCopy.java


// Based on a solution by Stephan Windm��ller in http://tapestry.1045711.n5.nabble.com/Cross-Validation-in-dynamic-Forms-td2427275.html 
// and Shing Hing Man in http://tapestry.1045711.n5.nabble.com/how-to-recordError-against-a-form-field-in-a-loop-td5719832.html .

package jumpstart.web.commons;

import org.apache.tapestry5.Field;

/**
 * An immutable copy of a Field. Handy for taking a copy of a Field in a row as a Loop iterates through them.
 */
public class FieldCopy implements Field {
    private String clientId;
    private String controlName;
    private String label;
    private boolean disabled;
    private boolean required;

    public FieldCopy(Field field) {
        clientId = field.getClientId();
        controlName = field.getControlName();
        label = field.getLabel();
        disabled = field.isDisabled();
        required = field.isRequired();
    }

    @Override
    public String getClientId() {
        return clientId;
    }

    @Override
    public String getControlName() {
        return controlName;
    }

    @Override
    public String getLabel() {
        return label;
    }

    @Override
    public boolean isDisabled() {
        return disabled;
    }

    @Override
    public boolean isRequired() {
        return required;
    }

}

Person.java


package jumpstart.business.domain.person;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;


/**
 * The Person entity.
 */
@Entity
@SuppressWarnings("serial")
public class Person implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(nullable = false)
    private Long id;

    @Version
    @Column(nullable = false)
    private Integer version;

    @Column(length = 10, nullable = false)
    private String firstName;

    @Column(length = 10, nullable = false)
    private String lastName;
    
    @Enumerated(EnumType.STRING)
    private Regions region;

    @Temporal(TemporalType.DATE)
    private Date startDate;

    public String toString() {
        final String DIVIDER = ", ";
        
        StringBuilder buf = new StringBuilder();
        buf.append(this.getClass().getSimpleName() + ": ");
        buf.append("[");
        buf.append("id=" + id + DIVIDER);
        buf.append("version=" + version + DIVIDER);
        buf.append("firstName=" + firstName + DIVIDER);
        buf.append("lastName=" + lastName + DIVIDER);
        buf.append("region=" + region + DIVIDER);
        buf.append("startDate=" + startDate);
        buf.append("]");
        return buf.toString();
    }

    // Default constructor is required by EJB3.
    public Person() {
    }

    public Person(String firstName, String lastName, Regions region, Date startDate) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
        this.region = region;
        this.startDate = startDate;
    }

    // The need for an equals() method is discussed at http://www.hibernate.org/109.html
    
    @Override
    public boolean equals(Object obj) {
        return (obj == this) || (obj instanceof Person) && id != null && id.equals(((Person) obj).getId());
    }

    // The need for a hashCode() method is discussed at http://www.hibernate.org/109.html

    @Override
    public int hashCode() {
        return id == null ? super.hashCode() : id.hashCode();
    }

    @PrePersist
    @PreUpdate
    public void validate() throws ValidationException {

        // Validate syntax...

        if ((firstName == null) || (firstName.trim().length() == 0)) {
            throw new ValidationException("First name is required.");
        }

        if ((lastName == null) || (lastName.trim().length() == 0)) {
            throw new ValidationException("Last name is required.");
        }

        if (region == null) {
            throw new ValidationException("Region is required.");
        }

        if (startDate == null) {
            throw new ValidationException("Start date is required.");
        }

    }

    public Long getId() {
        return id;
    }

    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Regions getRegion() {
        return region;
    }

    public void setRegion(Regions region) {
        this.region = region;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

}

Regions.java


package jumpstart.business.domain.person;

public enum Regions {
    EAST_COAST, WEST_COAST;
}