Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » EMF-TableViewer databinding Tutorial  () 1 Vote
EMF-TableViewer databinding Tutorial [message #734315] Fri, 07 October 2011 12:38 Go to next message
Vincenzo Caselli is currently offline Vincenzo CaselliFriend
Messages: 4
Registered: July 2009
Junior Member
Hi all,
we wrote a tutorial on how to use WindowBuilder with EMF databinding on
a TableViewer. Here is the link
http://www.rcp-vision.com/?p=1123&lang=en
The persistence is based on CDO over a MySQL database.
Any feedback about the article is welcome.
Thank you

Vincenzo Caselli
RCP Vision
Re: EMF-TableViewer databinding Tutorial [message #734330 is a reply to message #734315] Fri, 07 October 2011 13:34 Go to previous messageGo to next message
Matteo Miraz is currently offline Matteo MirazFriend
Messages: 38
Registered: March 2011
Member
Hi,

the article is great! I tried to replicate your project, but I experience a funny exception... I was wondering if you can put your resulting project (so I can identify what I'm doing wrong)

Thanks,
Matteo
Re: EMF-TableViewer databinding Tutorial [message #734337 is a reply to message #734330] Fri, 07 October 2011 13:47 Go to previous messageGo to next message
Vincenzo Caselli is currently offline Vincenzo CaselliFriend
Messages: 51
Registered: July 2009
Member
Hi Matteo,
probably we will publish the project soon.
In the meantime can you please report the exception you are experiencing?
Consider that the tutorial is based on Eclipse 3.7.1 (a.k.a Indigo SR1) Modeling and requires a MySQL database.
Thanks
Vincenzo
Re: EMF-TableViewer databinding Tutorial [message #734343 is a reply to message #734337] Fri, 07 October 2011 13:59 Go to previous messageGo to next message
Matteo Miraz is currently offline Matteo MirazFriend
Messages: 38
Registered: March 2011
Member
Ciao Vincenzo!

actually, I'm more interested in the EMF - JTable binding than in using CDO... as you can see in my other posts, I have some errors I need to fix.

Hence, I skipped the CDO part and performed all the other steps. Additionally, I created a memory on-the-fly, so that the table has something to show.

When I run the code (a simple shell with a composite and the jface jtable), it throws:

org.eclipse.core.runtime.AssertionFailedException: null argument:Validation realm cannot be null
	at org.eclipse.core.runtime.Assert.isNotNull(Assert.java:85)
	at org.eclipse.core.databinding.DataBindingContext.<init>(DataBindingContext.java:95)
	at org.eclipse.core.databinding.DataBindingContext.<init>(DataBindingContext.java:82)
	at variable.BohComposite.initDataBindings(BohComposite.java:107)
	at variable.BohComposite.<init>(BohComposite.java:76)
	at variable.Foo.<init>(Foo.java:39)
	at variable.Foo.main(Foo.java:18)


and at line 107 of my BohComposite there is the following line:
		DataBindingContext bindingContext = new DataBindingContext();


The problem is that I do not have a Realm, hence everything goes wrong... how can I fix that? (if it is related to the fact that I skipped the CDO part, you can ignore my request Wink )
Re: EMF-TableViewer databinding Tutorial [message #734355 is a reply to message #734343] Fri, 07 October 2011 14:23 Go to previous messageGo to next message
Vincenzo Caselli is currently offline Vincenzo CaselliFriend
Messages: 51
Registered: July 2009
Member
Ciao Matteo,
yes, you can cut off the entire CDO persistence part.
Just replace the following lines

		//*******************************************************
		//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();
		}
		//*******************************************************


with

		library = LibraryFactory.eINSTANCE.createLibrary();
		Author author = LibraryFactory.eINSTANCE.createAuthor();
		author.setName("John");
		author.setSurname("Doe");
		library.getListAuthor().add(author);
		author = LibraryFactory.eINSTANCE.createAuthor();
		author.setName("Mario");
		author.setSurname("Rossi");
		library.getListAuthor().add(author);


Let me know if it works
Ciao

Vincenzo
Re: EMF-TableViewer databinding Tutorial [message #734378 is a reply to message #734355] Fri, 07 October 2011 15:10 Go to previous messageGo to next message
Matteo Miraz is currently offline Matteo MirazFriend
Messages: 38
Registered: March 2011
Member
Hi,

That's exactly what I've done... After a few attempts, I discovered that it was due to the simple environment I created for the example... maybe I forgot to register something, because in a normal environment, everything works smoothly.

However, I tried to insert a sorter (by name) in the JTable, and I simulate the insertion and the renaming of elements in the model.
The first case is ok: new elements are put in the right position of the table,
However, in case of a rename, elements are not re-sorted according to their new name (see the attachment)... Does there is a way to fix it?

Thanks again for the help and the great tutorial Smile
Matteo

[Updated on: Fri, 07 October 2011 15:15]

Report message to a moderator

Re: EMF-TableViewer databinding Tutorial [message #734468 is a reply to message #734378] Fri, 07 October 2011 22:00 Go to previous messageGo to next message
Vincenzo Caselli is currently offline Vincenzo CaselliFriend
Messages: 51
Registered: July 2009
Member
Matteo,
JTable? it's a TableViewer isn't it?
I don't know how you implemented the sorter, however here is a way that works also for renaming.

Create a Comparator for Viewers, not Tables (tables works with cells, viewers work with objects)
package it.rcpvision.rcptutorial.application;

import library.Author;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerComparator;

public class AuthorViewerComparator extends ViewerComparator {
	private int sortColumnIndex;

	public AuthorViewerComparator() {
		this.sortColumnIndex = 0;
	}

	public void setSortColumnIndex(int column) {
		this.sortColumnIndex = column;
	}

	@Override
	public int compare(Viewer viewer, Object object1, Object object2) {
		Author author1 = (Author) object1;
		Author author2 = (Author) object2;
		int rc = 0;
		switch (sortColumnIndex) {
		case 0:
			rc = author1.getName().compareTo(author2.getName());
			break;
		case 1:
			rc = author1.getSurname().compareTo(author2.getSurname());
			break;
		default:
			rc = 0;
		}
		return rc;
	}

}


use it

public class ListAuthorViewPart extends ViewPart {
	...
	private AuthorViewerComparator comparator;
	...
	@Override
	public void createPartControl(Composite parent) {
		...
		comparator = new AuthorViewerComparator();
		tableViewer.setComparator(comparator);
		comparator.setSortColumnIndex(0);
	}

	...
	//Remember to refresh the viewer when a change happens (insert, update, delete)
	library.getListAuthor().get(0).setName(txtName.getText());
	library.getListAuthor().get(0).setSurname(txtSurname.getText());
	tableViewer.refresh();
	...



Let me know
Ciao

Vincenzo
RCP Vision
Re: EMF-TableViewer databinding Tutorial [message #734525 is a reply to message #734468] Sat, 08 October 2011 06:39 Go to previous messageGo to next message
Matteo Miraz is currently offline Matteo MirazFriend
Messages: 38
Registered: March 2011
Member
Hi,

yeah... sorry, I meant a TableViewer!

I did a similar thing for the comparator, and it works pretty well. It sorts the data already in the model and those inserted afterwards, even without a refresh. For what concern renames, the element's label is updated also without a refresh, but the item is only moved if the tableViewer.refresh() is invoked. This for me is an issue, because changes in the model are made by various part of my application, and some of them are not aware of the presence of that tableViewer (hence, they cannot issue a refresh).

To issue that refresh I thought of monitoring the elements shown in the table. In case of any change in the column being used for the ordering (or in those used by the filter), I can force an refresh of the tableViewer. Is this a good solution?

Thanks,
Matteo
Re: EMF-TableViewer databinding Tutorial [message #734534 is a reply to message #734525] Sat, 08 October 2011 08:09 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Hi,

You can fix this problem by creating a custom LabelProvider which
instead of fireing a LabelChangedEvent, call
TableViewer#update(elements, new String[] { ... }).

See the JavaDoc for TableViewer#update(), ViewerComparator#isSorterProperty

Tom

Am 08.10.11 08:39, schrieb Matteo:
> Hi,
>
> yeah... sorry, I meant a TableViewer!
>
> I did a similar thing for the comparator, and it works pretty well. It
> sorts the data already in the model and those inserted afterwards, even
> without a refresh. For what concern renames, the element's label is
> updated also without a refresh, but the item is only moved if the
> tableViewer.refresh() is invoked. This for me is an issue, because
> changes in the model are made by various part of my application, and
> some of them are not aware of the presence of that tableViewer (hence,
> they cannot issue a refresh).
>
> To issue that refresh I thought of monitoring the elements shown in the
> table. In case of any change in the column being used for the ordering
> (or in those used by the filter), I can force an refresh of the
> tableViewer. Is this a good solution?
> Thanks,
> Matteo
Re: EMF-TableViewer databinding Tutorial [message #734857 is a reply to message #734534] Mon, 10 October 2011 08:17 Go to previous messageGo to next message
Matteo Miraz is currently offline Matteo MirazFriend
Messages: 38
Registered: March 2011
Member
Tom,

thank you... I was missing methods ViewerComparator#isSorterProperty and ViewerFilter#isFilterProperty.

--
Matteo
Re: EMF-TableViewer databinding Tutorial [message #735081 is a reply to message #734534] Mon, 10 October 2011 21:35 Go to previous messageGo to next message
Vincenzo Caselli is currently offline Vincenzo CaselliFriend
Messages: 51
Registered: July 2009
Member
Hi Tom and Matteo,
I tried update(), but did not sorted the item correctly, while refresh() did it right.
Here is my code, are there bad side-effects with it?

Vincenzo

	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		ObservableListContentProvider listContentProvider = new ObservableListContentProvider();
		tableViewer.setContentProvider(listContentProvider);
		//
		IObservableMap[] observeMaps = EMFObservables.observeMaps(
				listContentProvider.getKnownElements(),
				new EStructuralFeature[] { Literals.AUTHOR__NAME,
						Literals.AUTHOR__SURNAME });

		//*******************************************************************************//
		//********  Changes on the code generated by WindowBuilder *********//
		//*******************************************************************************//
		ObservableMapLabelProvider labelProvider = new ObservableMapLabelProvider(observeMaps);
		labelProvider.addListener(new ILabelProviderListener() {
			@Override
			public void labelProviderChanged(LabelProviderChangedEvent event) {
				if (event.getElement()!=null) {
					tableViewer.refresh();
				}
			}
		});
		tableViewer.setLabelProvider(labelProvider);
		//*******************************************************************************//

		//
		IObservableList libraryListAuthorObserveList = EMFObservables
				.observeList(Realm.getDefault(), library,
						Literals.LIBRARY__LIST_AUTHOR);
		tableViewer.setInput(libraryListAuthorObserveList);
		//
		return bindingContext;
	}


[Updated on: Mon, 10 October 2011 22:05]

Report message to a moderator

Re: EMF-TableViewer databinding Tutorial [message #735249 is a reply to message #735081] Tue, 11 October 2011 10:35 Go to previous message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
When calling update(element,new String[] {"authorSurname"}) you need to
specialize your ViewerComparator/ViewerFilter!

Tom

Am 10.10.11 23:35, schrieb Vincenzo Caselli:
> Hi Tom and Matteo,
> I tried update(), but did not sorted the item correctly, while refresh()
> did it right.
> Here is my code, are there bad side-effects with it?
>
>
> protected DataBindingContext initDataBindings() {
> DataBindingContext bindingContext = new DataBindingContext();
> //
> ObservableListContentProvider listContentProvider = new
> ObservableListContentProvider();
> tableViewer.setContentProvider(listContentProvider);
> //
> IObservableMap[] observeMaps = EMFObservables.observeMaps(
> listContentProvider.getKnownElements(),
> new EStructuralFeature[] { Literals.AUTHOR__NAME,
> Literals.AUTHOR__SURNAME });
>
> //*******************************************************************************//
>
> ObservableMapLabelProvider labelProvider = new
> ObservableMapLabelProvider(observeMaps);
> labelProvider.addListener(new ILabelProviderListener() {
> @Override
> public void labelProviderChanged(LabelProviderChangedEvent
> event) {
> if (event.getElement()!=null) {
> tableViewer.refresh();
> }
> }
> });
> tableViewer
> .setLabelProvider(labelProvider);
>
> //*******************************************************************************//
>
> //
> IObservableList libraryListAuthorObserveList = EMFObservables
> .observeList(Realm.getDefault(), library,
> Literals.LIBRARY__LIST_AUTHOR);
> tableViewer.setInput(libraryListAuthorObserveList);
> //
> return bindingContext;
> }
>
>
>
Previous Topic:CDOQuery/java.util.concurrent memory leak
Next Topic:EMFProperties.resource() strange behaviour
Goto Forum:
  


Current Time: Fri Apr 26 06:06:24 GMT 2024

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

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

Back to the top