Java Database Application Library


Get JDAL (Java Database Application Library) at SourceForge.net. Fast, secure and Free Open Source software downloads

AbstractView


Introduction


AbstractView is the principal template ( Template , GoF) of JDAL for dealing with forms.

AbstractView permits you condense the development work of forms in one unique job: Implement the method buildPanel().

AbstractView supports the automatic way of the following commune jobs in the development of Vaadin forms:


The process of building a form based on Abstractview can be summarised in the following steps:



Data Binding


JDAL binding is based on JDAL core library and do not use the new Vaadin 7 FieldGroups.

AbstractView supports at the moment two forms of realizing the binding of data between user interface controls and the domain models:

In both cases, in creating binding between a Vaadin component and a property of the model, it will undertake the following actions:

Finally, AbstractView provides three template methods to customize the data binding process:



Structure


AbstractView delegates much of the work in strategies or support classes of JDAL. It is interesting to take a look as you might be interested in using them directly o personalising the behaviours.



CompositeBinder

Performs all work in data binding between the conttols and models. It can be used independently outside of AbstractView. It is not necessary to extend JDAL classes to dispose of data binding system. You can find more information in the data binding chapter..

ErrorProcessor

Processing strategy of binding errors. The implementation by default, BackgroundErrorProcessor changes the colour of the background of the controls that have failed and add a tooltip with the information of the error.

BinderFactory

PropertyBinder factory, CompositeBinder uses it to find the Binder appropriate for each type of control.

ControlAccessorFactory

Allow access to generic Swing controls. That is to say, perform control operations without the specific knowledge of the type of control.

ControlInitializer

Strategy of control initialization, usually with data from the respositories or other entities. The implementatión by default DefaultControlInitializer is able to detect annotationes JPA and the annotation @Reference of JDAL Core.



Example


Model

We will create a View for the following Book entity:

@Entity
@Table(name="books")
public class Book implements Serializable {
 
	@Id
	@GeneratedValue
	private Long id;
	@NotEmpty
	private String name = "";
	@NotNull
	@ManyToOne
	@JoinColumn(name="authorid")
	private Author author;
	@NotNull
	@ManyToOne
	@JoinColumn(name="categoryid")
	private Category category;
	private String isbn = "";
	private Date publishedDate;
 
 
	public String toString() {
		return name;
	}
 
	// Getters and Setters
}
 

View

The target objective is to create a form to edit books with data binding, internacionalization and validation of the backing model



The following is the code of the book editor form using AbstracView


public class BookView extends AbstractView<Book> {
 
	// We use autobind jdal feature, name the controls like Book properties
	private TextField name = new TextField();
	private TextField isbn = new TextField();
	@Property(name="publishedDate") // map to publishedDate model property (the default)
	private DateField publishedDate = new DateField();
	@Initializer(orderBy="name")    // autofill with authors, sorted by name
	private ComboBox author = new ComboBox();
	@Initializer(orderBy="name")   // autofill with categories, sorted by name
	private ComboBox category = new ComboBox();
 
	public BookView() {
		this(new Book());
	}
 
	public BookView(Book model) {
		super(model);
	}
 
	@PostConstruct
	public void init()  {
		// Just our binding code!
		autobind();
	}
 
	@Override
	protected Component buildPanel() {
		BoxFormBuilder fb = new BoxFormBuilder();
		fb.setDefaultWidth(SimpleBoxFormBuilder.SIZE_FULL);
		fb.setFixedHeight();
 
		fb.row();
		fb.add(name, getMessage("Book.title"));
		fb.row();
		fb.startBox();
		fb.row();
		fb.add(author, getMessage("Book.author"));
		fb.add(category, getMessage("Book.category"));
		fb.endBox();
		fb.row();
		fb.startBox();
		fb.row();
		fb.add(isbn, getMessage("Book.isbn"));
		fb.add(publishedDate, getMessage("Book.publishedDate"), 120);
		fb.endBox();
 
		return fb.getForm();
	}
 
	 // The view name is shown as title on ViewDialog.
	@Override
	public String getName() { 
		Book model = getModel();
 
		if (model != null && model.getId() != null) {
			return model.getName();
		}
 
		return null;
	}

Context Configuration

The book editor looks simple, but need some spring configuration to work. the most important is the abstract view bean definition that inject the validator, error processor and control initializer to views. Instead declaring all views in context bean definition files, we could use the JDAL @Parent annotation to inherit xml abstract bean definition when using the Spring auto scan packages for beans feature.
   <!-- Register default Vaadin components -->
   <vaadin:defaults />
 
   <!-- Vaadin scoped beans are linked to UI class instances, Use with care, is still a beta component. -->
   <bean id="vaadinScopeConfigurer" class="org.springframework.beans.factory.config.CustomScopeConfigurer">
       <property name="scopes">
           <map>
                <entry key="ui" value="org.jdal.vaadin.beans.VaadinScope" />
           </map>
       </property>
   </bean>
 
 
   <!-- Abstract definition for Views -->
   <bean id="view" abstract="true">
        <property name="controlInitializer" ref="controlInitializer"/>
        <property name="errorProcessors">
             <list>
                   <ref bean="errorProcessor" />
             </list>
        </property>
        <property name="validator" ref="validator" />
   </bean>
 
   <!-- Default control initializer -->
   <bean id="controlInitializer" class="org.jdal.vaadin.ui.bind.VaadinControlInitializer">
        <property name="persistentService" ref="contextPersistentService" />
   </bean>
 
    <!-- Spring JSR-303 validator -->
   <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
       <jdal:serializable-proxy />
   </bean>
 
   <!-- Error Processor -->
   <bean id="errorProcessor" class="org.jdal.vaadin.ui.bind.UserErrorProcessor" />  
 
   <!-- View Dialog Prototype -->
   <bean id="viewDialog" class="org.jdal.vaadin.ui.form.ViewDialog" scope="prototype"/>
 
   <!-- Book Form Editor -->
   <bean id="bookEditor" class="org.jdal.samples.vaadin.BookView" parent="view" scope="prototype" >
        <property name="width" value="600" />
        <property name="height" value="300" />
        <property name="persistentService" ref="bookService" />
   </bean>
 
   <!-- Book DAO -->
   <jdal:service entity="org.jdal.samples.model.Book" />
 
 

Now, we can get instances of the project editor by the bean "bookEditor".

For a full sample application see vaadin-sample-jpa.