Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » Problem setup databinding with TableViewer(Table showing Books where a Book has a list of Writers)
Problem setup databinding with TableViewer [message #685729] Sat, 18 June 2011 12:29
Andreas Fagschlunger is currently offline Andreas Fagschlunger
Messages: 12
Registered: June 2011
Junior Member
Hy all!

I have made the following Example to show my problem:

package at.library.test;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.list.WritableList;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.viewers.IViewerUpdater;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class LibraryTest implements Runnable {

	abstract class AbstractModelObject {

		private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
				this);

		public void addPropertyChangeListener(PropertyChangeListener listener) {
			propertyChangeSupport.addPropertyChangeListener(listener);
		}

		public void addPropertyChangeListener(String propertyName,
				PropertyChangeListener listener) {
			propertyChangeSupport.addPropertyChangeListener(propertyName,
					listener);
		}

		public void removePropertyChangeListener(PropertyChangeListener listener) {
			propertyChangeSupport.removePropertyChangeListener(listener);
		}

		public void removePropertyChangeListener(String propertyName,
				PropertyChangeListener listener) {
			propertyChangeSupport.removePropertyChangeListener(propertyName,
					listener);
		}

		protected void firePropertyChange(String propertyName, Object oldValue,
				Object newValue) {
			propertyChangeSupport.firePropertyChange(propertyName, oldValue,
					newValue);
		}
	}

	class Writer extends AbstractModelObject {
		private String firstName = null;
		private String lastName = null;

		public Writer(String firstName, String lastName) {
			this.firstName = firstName;
			this.lastName = lastName;
		}

		public String getFirstName() {
			return firstName;
		}

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

		public String getLastName() {
			return lastName;
		}

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

	class Book extends AbstractModelObject {
		private String title = null;
		private Boolean read = null;
		private List<Writer> writers = null;

		public Book() {
			writers = new ArrayList<Writer>();
		}

		public Book(String title) {
			this();
			this.title = title;
		}

		public String getTitle() {
			return title;
		}

		public void setTitle(String title) {
			firePropertyChange("title", this.title, this.title = title);
		}

		public Boolean getRead() {
			return read;
		}

		public void setRead(Boolean read) {
			this.read = read;
		}

		public List<Writer> getWriters() {
			return writers;
		}

		public void setWriters(List<Writer> writers) {
			firePropertyChange("writers", this.writers, this.writers = writers);
		}

	}

	private Display display = null;

	private Table table = null;

	private TableViewer viewer = null;

	private WritableList input = null;

	private LibraryTest() {
		display = new Display();
		Realm.runWithDefault(SWTObservables.getRealm(display), this);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		LibraryTest libraryTest = new LibraryTest();
	}

	@Override
	public void run() {
		Shell shell = new Shell(display);
		shell.setLayout(new GridLayout());
		table = new Table(shell, SWT.FULL_SELECTION);
		table.setHeaderVisible(true);
		table.setLayoutData(new GridData(GridData.FILL_BOTH));
		viewer = new TableViewer(table);
		createColumns();
		bindValues();
		for (TableItem tableItem : table.getItems()) {
			createBookReadTableEditor(tableItem);
		}
		for (TableColumn column : table.getColumns()) {
			column.pack();
		}
		Button button = new org.eclipse.swt.widgets.Button(shell, SWT.PUSH);
		button.setText("Modify title");
		button.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				Book book = (Book) input.get(0);
				book.setTitle(book.getTitle() + " Modify");
			}

		});
		button = new Button(shell, SWT.PUSH);
		button.setText("Modify writer");
		button.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				Book book = (Book) input.get(0);
				Writer writer = book.getWriters().get(0);
				writer.setFirstName(writer.getFirstName() + " Modify");
			}

		});
		button = new Button(shell, SWT.PUSH);
		button.setText("Add book");
		button.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				byte[] b = new byte[10];
				new Random().nextBytes(b);
				Book book = new Book(new String(b));
				book.setWriters(((Book) input.get(0)).getWriters());
				book.setRead(Boolean.valueOf(new Random().nextBoolean()));
				input.add(book);
			}

		});
		shell.open();
		// The SWT event loop
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	private void createColumns() {
		TableViewerColumn viewerColumn = new TableViewerColumn(viewer, SWT.NONE);
		viewerColumn.getColumn().setText("Title");
		viewerColumn.setLabelProvider(new ColumnLabelProvider() {

			@Override
			public String getText(Object element) {
				Book book = (Book) element;
				return book.title;
			}

		});
		viewerColumn = new TableViewerColumn(viewer, SWT.NONE);
		viewerColumn.getColumn().setText("Writers");
		viewerColumn.setLabelProvider(new ColumnLabelProvider() {

			@Override
			public String getText(Object element) {
				Book book = (Book) element;
				List<Writer> writers = book.getWriters();
				StringBuilder text = new StringBuilder();
				int iMax = writers.size() - 1;
				if (iMax == -1) {
					return "";
				}
				for (int i = 0;; i++) {
					Writer writer = writers.get(i);
					text.append(writer.getFirstName() + " "
							+ writer.getLastName());
					if (i == iMax) {
						break;
					}
					text.append(", ");
				}
				return text.toString();
			}

		});
		viewerColumn = new TableViewerColumn(viewer, SWT.NONE);
		viewerColumn.getColumn().setText("Read?");
		viewerColumn.setLabelProvider(new ColumnLabelProvider() {

			@Override
			public String getText(Object element) {
				return null;
			}

		});
	}

	private void bindValues() {
		createInput();
		viewer.setContentProvider(new ObservableListContentProvider(
				new IViewerUpdater() {

					@Override
					public void replace(Object oldElement, Object newElement,
							int position) {
					}

					@Override
					public void remove(Object element, int position) {
					}

					@Override
					public void remove(Object[] elements) {
					}

					@Override
					public void move(Object element, int oldPosition,
							int newPosition) {
					}

					@Override
					public void insert(Object element, int position) {
						viewer.insert(element, position);
						for (TableItem tableItem : table.getItems()) {
							if (element.equals(tableItem.getData())) {
								createBookReadTableEditor(tableItem);
								return;
							}
						}
					}

					@Override
					public void add(Object[] elements) {
					}
				}));
		viewer.setInput(input);
	}

	private void createInput() {
		List<Writer> writers = new ArrayList<Writer>();
		writers.add(new Writer("Johann Wolfgang", "von Goethe"));
		List<Book> books = new ArrayList<Book>();
		Book book = new Book("Faust: Der Tragödie erster Teil");
		book.setWriters(writers);
		books.add(book);
		input = new WritableList(books, Book.class);
	}

	private void createBookReadTableEditor(TableItem tableItem) {
		final TableEditor editor = new TableEditor(table);
		final Button checkbox = new Button(table, SWT.CHECK);
		Book book = (Book) tableItem.getData();
		checkbox.setSelection(book.getRead() != null
				&& book.getRead().booleanValue());
		checkbox.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				Book book = (Book) editor.getItem().getData();
				book.setRead(Boolean.valueOf(checkbox.getSelection()));
			}

		});
		checkbox.pack();
		editor.minimumWidth = checkbox.getSize().x;
		editor.horizontalAlignment = SWT.LEFT;
		editor.setEditor(checkbox, tableItem, 2);
	}
}



Now the problem, when the "Modify"-Buttons are clicked, the Model changes but the View doesn't get refreshed.

How can I get this to work with databinding?

Best regards,
Billie
Previous Topic:using 32 by 32 image in treeviewer
Next Topic:observeImage
Goto Forum:
  


Current Time: Sat Oct 25 22:49:53 GMT 2014

Powered by FUDForum. Page generated in 0.01487 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software