RSS

Search Engine

Wednesday, June 23, 2010

JFace Databinding

4.1. Overview

JFace provides a flexible way of defining a view, the data and the representation of data separately. The following focuses in using JFace with Eclipse Databinding. For a larger example of JFace please see JFace Tables With Eclipse RCP

The following will demonstrate how list, table and tree viewers can be used with Eclipse Databinding.

Create a new plugin "de.vogella.databinding.model".

4.2. JFace Listviewer

In the databinding for JFace viewers it is possible to distinct between changes in the collection and changes in the individual object. The following focus on the propagation of changes from the collection to the UI, e.g. if an element will be added to the collection or if an element will be removed from the collection this change should be visible in the UI.

JFace databinding provides several classes which allows to bind lists to viewers. ObservableListContentProvider is a content provider which requires a list which implements IObservableCollection. We use WritableList as input.

Create a new Eclipse RCP project "de.vogella.databinding.person.listviewer" use "Hello RCP" as a template (see Create your first Eclipse RCP application for details).

Add again the dependency to your model and the databinding plugins to your plugin project.

Add a view to your application. Create the following class for this view.

    
package de.vogella.databinding.person.listviewer;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.databinding.observable.list.WritableList;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

import de.vogella.databinding.person.model.Person;

public class View extends ViewPart {

private ListViewer viewer;
private WritableList input;

@Override
public void createPartControl(Composite parent) {
// Just a little bit layout
parent.setLayout(new GridLayout(3, false));

// Define the viewer
viewer = new ListViewer(parent);
viewer.setContentProvider(new ObservableListContentProvider());
List persons = new ArrayList();
// Just for testing we create sample data
createExampleData(persons);
input = new WritableList(persons, Person.class);
// Set the writeableList as input for the viewer
viewer.setInput(input);

Button delete = new Button(parent, SWT.PUSH);
delete.setText("Delete");
delete.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
if (!viewer.getSelection().isEmpty()) {
IStructuredSelection selection = (IStructuredSelection) viewer
.getSelection();
Person p = (Person) selection.getFirstElement();
input.remove(p);
}
}
});

Button add = new Button(parent, SWT.PUSH);
add.setText("Add");
add.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
Person p = new Person();
p.setFirstName("Test");
p.setLastName("Test");
input.add(p);
}
});
}

protected void createExampleData(List persons) {
Person p = new Person();
p.setFirstName("Joe");
p.setLastName("Darcey");
persons.add(p);
p = new Person();
p.setFirstName("Jim");
p.setLastName("Knopf");
persons.add(p);
p = new Person();
p.setFirstName("Jim");
p.setLastName("Bean");
persons.add(p);
}

@Override
public void setFocus() {

}

}

In this example the UI will be updated if you remove or add an element to the collection. Run this example and test it.

0 comments:

Post a Comment