WindowBuilder: advanced databinding with CDO

Now we will see how, starting from previous tutorial, we can handle the management of the data showed into the table.

Let’s get back to the Design tab and set a GridLayout, picking it up from the palette and dropping it to the main composite

now select a  Composite and drop it into the View

let both composite fill all the available room, by applying the following layout options to both of them

Now let’s give a GridLayout to the composite

and lay out a  Label with text “Name

and another, just under, with text “Surname

now let’s do the same with a couple of text fields

and add a  Save button

Our goal is to have a binding between these fields and the correspondent attributes of the selected item on the table above. To do this we need to define a variable of type  WritableValue

with which we can now perform our  binding (do the following for both fields)


 

Now we need to handle the viewer selection event

where we can insert the following code

...
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.emf.cdo.util.CommitException;

...
protected CDOTransaction transaction;
...

        	public void selectionChanged(SelectionChangedEvent event) {
        		if (transaction!=null) {
					transaction.rollback();
				}
				transaction = cdoSession.openTransaction();
				Author authorSelected = (Author) ((StructuredSelection) event.getSelection()).getFirstElement();
				Author authorTransactionalObject = (Author) transaction.getObject(authorSelected);
				author.setValue( authorTransactionalObject );
        	}

Here we will open a new CDO transaction upon every viewer selection event and do the binding with the transactional selected item.

As far as the save action, let’s do the following. Select the Save button and add a new event

and insert the following code

...
import org.eclipse.emf.cdo.util.CommitException;		
...
		public void widgetSelected(SelectionEvent e) {
        		try {
				transaction.commit();
			} catch (CommitException e1) {
				e1.printStackTrace();
			}
		}

 

 

If now we launch the application we can verify that:

  • fields Name and Surname are automatically synchronized with the selected item in the viewer
  • clicking on the Save button will save the data, with automatic synchronization of the viewer content

 

Moreover, one of the coolest things about CDO is the following.
Just adding this simple line of code

we can have all clients notified when some other has changed values; just try launching more application instances …

 

Back to index

This entry was tagged , , , , , . Bookmark the permalink.

One Response to WindowBuilder: advanced databinding with CDO

  1. Mark Rowe says:

    Vincenzo, thank you for a really excellent tutorial that is complete and simple enough for a beginner like myself to follow.

    I would like to ask if it is possible to extend this tutorial by including a “Country” EObject. Each author would have exactly one country object associated with them and this would be shown in the third column in the table. In the detail section the country should be shown in a combobox. I have tried to extend the tutorial in this way myself but I am not able to bind the “Name” attribute of the Country EObject to a tableviewer column (the closest I got was binding the object itself).

    Another really useful further tutorial would be similar, but when an author is selected a list of books written by them is shown in the detail area.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>