WindowBuilder: installing and EMF databinding

In the tutorial we saw how to, starting from an EMF model, we can automatically generate a database-aware application.
The advantage is clear: we do not need to write code, aside from a very few initialization lines. But there is also a drawback: the application may look quite “generic” for a software product. The approach is therefore right for testing the model correctness or for a prototype, or otherwise for a flat handling of the model data; but it is not ideal for a customizable and marketable application.

If, on the other hand, we decide to write code for our application, then EMF and WindowBuilder can help us being particularly productive.
If you followed the previous tutorials you should already have installed the needed plugins for EMF. Now follow the instruction on the page  .

In this tutorial we will show how to obtain an RCP Application that shows a window (View) with a table that lists items from our domain model.
We will start from the Application we obtained at the end of the previous tutorial: CDO – Connected Data Objects.

Now our goal is creating a menu item that will allow opening a View with a list of all the Authors inserted in the past article. In order to do this we need first to create a View.

So let’s open the Extensions tab from file plugin.xml.

and add the needed Extension to handle the View, pressing the Add… button

and typing “view” to filter the content of the list

Let’s select item  org.eclipse.ui.views and press  Finish

Now select the just created extension and right-click  New -> view

choose an id, a name, a name for the class to be created and click on class hyperlink

when the  New Java Class pop-up shows, just confirm it pressing Finish.
We now have an empty View ready to use

Now it’s time for creating the menu item. Click button Add… and add the extension
org.eclipse.ui.menus

and inside it, add a menuContribution

fill the  locationURI field with value  menu:org.eclipse.ui.main.menu

and, inside the  menuContribution item create a command

press   Browse… button near the commandId field and begin typing *showView in order to filter all the available commands

and select the command org.eclipse.ui.views.showView; type a value for the Label for this command that will appear on the menu

now add a parameter to the command

this parameter will inform the command about which View has to be opened. So type (with the help of the Browse… button)  org.eclipse.ui.views.showView.viewId into name field and put the  View id into value field

Now we are almost ready to launch the application, but let’s make a little update into  Perspective class, adding the following line

this is because we are not using Eclipse Editors here, just Views

Ok, let’s launch the application; we should see the Open View Authors menu item that, when clicked, shows Authors View, obviously empty for now

In the end we can start working with WindowBuilder: select ListAuthorViewPart class and right-click  Open with -> WindowBuilder Editor

and open the Design tab

 

here is how the WindowBuilder visual designer looks like

 

Now let’s add a table, a TableViewer to be precise, on this View. You can find it on the Palette, into JFace section; let’s click on it

and then click on the View

obtaining this

then add a couple of table columns, with a click on TableViewerColumn on the Palette and another (drop) them on the TableViewer; please note that you will be asked for a name of the columns: just insert Name and Surname.

Before working with Databinding we need to load the data we stored with the JUnit test we launched in the previous tutorial (CDO – Connected Data Objects). To do this we need to go to Source tab into the View

add the following lines (see //****… markers)

 

package it.rcpvision.rcptutorial.application;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.swt.widgets.Table;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.jface.viewers.TableViewerColumn;

//*******************************************************
import library.Library;
import org.eclipse.emf.cdo.net4j.CDOSession;
import org.eclipse.emf.cdo.view.CDOView;
import org.eclipse.emf.cdo.eresource.CDOResource;
import java.io.IOException;
//*******************************************************

public class ListAuthorViewPart extends ViewPart {
	private Table table;

	//*******************************************************
	private Library library; //The library instance
	private CDOSession cdoSession; //The CDO session
	//*******************************************************

	public ListAuthorViewPart() {
	}

	@Override
	public void createPartControl(Composite parent) {

		//*******************************************************
		//Open CDO session and view
		cdoSession = TestCdoClient.openSession("demo");
		CDOView view = cdoSession.openView();
		CDOResource resource = view.getResource("/myResource");
		try {
			//Load resource library
			resource.load(null);
			library = (Library) resource.getContents().get(0);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//*******************************************************

		TableViewer tableViewer = new TableViewer(parent, SWT.BORDER
				| SWT.FULL_SELECTION);
		table = tableViewer.getTable();

		TableViewerColumn tableViewerColumn = new TableViewerColumn(
				tableViewer, SWT.NONE);
		TableColumn tblclmnName = tableViewerColumn.getColumn();
		tblclmnName.setWidth(100);
		tblclmnName.setText("Name");

		TableViewerColumn tableViewerColumn_1 = new TableViewerColumn(
				tableViewer, SWT.NONE);
		TableColumn tblclmnSurname = tableViewerColumn_1.getColumn();
		tblclmnSurname.setWidth(100);
		tblclmnSurname.setText("Surname");
		// TODO Auto-generated method stub

	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

}

Ok, now we are ready for working with Databinding. Go back to Design tab, select the tableViewer object (which is inside the table), right-click -> Bindings -> input