Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » DataBindingContext for TableViewer in ViewPart
DataBindingContext for TableViewer in ViewPart [message #12017] Mon, 08 June 2009 04:53 Go to next message
Igor Ganapolsky is currently offline Igor GanapolskyFriend
Messages: 39
Registered: July 2009
Location: New York
Member

Hi,
I have created a TableViewer in a class that extends a ViewPart. This
TableViewer is called from yet another class. I wish to use a
DataBindingContext for this TableViewer, but I get an error "Realm cannot
be null". There is no way I can start a realm in a class that does not
have a main method, right? Does that mean that I should forget about
using DataBindingContext, and just manually synchronize TableViewer's
values with the model?
Re: DataBindingContext for TableViewer in ViewPart [message #12030 is a reply to message #12017] Mon, 08 June 2009 14:08 Go to previous messageGo to next message
vincent pucheux is currently offline vincent pucheuxFriend
Messages: 15
Registered: July 2009
Junior Member
Igor Ganapolsky a écrit :
> Hi,
> I have created a TableViewer in a class that extends a ViewPart. This
> TableViewer is called from yet another class. I wish to use a
> DataBindingContext for this TableViewer, but I get an error "Realm
> cannot be null". There is no way I can start a realm in a class that
> does not have a main method, right? Does that mean that I should forget
> about using DataBindingContext, and just manually synchronize
> TableViewer's values with the model?
>
hi,

As I remember you can use Realm.getDefault()

like this :
IObservableValue amountBinding = BeansObservables.observeDetailValue(
Realm.getDefault(), sel, "amount", Integer.class);
dbc.bindValue(
SWTObservables.observeText(amount, SWT.Modify), amountBinding, null);

vincent
Re: DataBindingContext for TableViewer in ViewPart [message #12043 is a reply to message #12017] Mon, 08 June 2009 14:37 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Hi,

It looks like the call is not done in UI-Thread hence no default Realm
is available.

Tom

Igor Ganapolsky schrieb:
> Hi,
> I have created a TableViewer in a class that extends a ViewPart. This
> TableViewer is called from yet another class. I wish to use a
> DataBindingContext for this TableViewer, but I get an error "Realm
> cannot be null". There is no way I can start a realm in a class that
> does not have a main method, right? Does that mean that I should forget
> about using DataBindingContext, and just manually synchronize
> TableViewer's values with the model?
>
Re: DataBindingContext for TableViewer in ViewPart [message #12056 is a reply to message #12030] Mon, 08 June 2009 16:28 Go to previous messageGo to next message
Igor Ganapolsky is currently offline Igor GanapolskyFriend
Messages: 39
Registered: July 2009
Location: New York
Member

That sounds like a good idea, except you'll see a nasty error on the line
DataBindingContext dbc = new DataBindingContext();
The error will tell you that Realm cannot be null. So your workaround
won't solve this particular issue. Let me know what you think.
Re: DataBindingContext for TableViewer in ViewPart [message #12068 is a reply to message #12043] Mon, 08 June 2009 16:29 Go to previous messageGo to next message
Igor Ganapolsky is currently offline Igor GanapolskyFriend
Messages: 39
Registered: July 2009
Location: New York
Member

Tom,
does this mean I cannot instantiate a DataBindingContext in this
particular class?
Re: DataBindingContext for TableViewer in ViewPart [message #12078 is a reply to message #12068] Mon, 08 June 2009 16:39 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Hi,

It depends on what you are trying to do. The default realm created by
the RCP-Framework when starting is then one bound to Display-Thread.

So if you are doing the call out of the UI-Thread you need to create
your own Realm because this is needed to synchronize between different
threads (e.g. ensuring that all operation on SWT-Controls are done on
the UI-Thread).

Do get back from an UI to the Display-Thread you normally use
Display#asyncExec or Display#syncExec-methods.

You mention that you are access a TableViewer which should be done
outside the UI-Thread either!

To summerize you can instantiate a DatabindingContext but you need to
create your own realm because there's not default one available but then
databinding has to sync all call UI-Calls using Realm#asyncExec /
Realm#syncExec (which makes sense if for example the validation is a a
heavy weight operation and you don't want to block the UI until it's
done) or simply wrapp your Databinding-Creation stuff in a
Display#asyncExec.


Tom

Igor Ganapolsky schrieb:
> Tom,
> does this mean I cannot instantiate a DataBindingContext in this
> particular class?
Re: DataBindingContext for TableViewer in ViewPart [message #12091 is a reply to message #12078] Mon, 08 June 2009 16:43 Go to previous messageGo to next message
Igor Ganapolsky is currently offline Igor GanapolskyFriend
Messages: 39
Registered: July 2009
Location: New York
Member

The problem is this is not an RCP application, so there is no default
Realm. It is simply null. I don't see a way to create my own realm in
this class. I've tried several code snippets that I found online, but my
Realm still ends up being null.
Re: DataBindingContext for TableViewer in ViewPart [message #12103 is a reply to message #12091] Mon, 08 June 2009 16:51 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Hi,

So you are the one controling the main-method? You talked about ViewPart
so I assumed that this is an RCP-Application.

Normally the only thing you do is wrap your code into

public static void main(String[] args) {
Display d = new Display();

Ream.runWithDefault( SWTObservables.getRealm(d), new Runnable() {
public void run() {
internalMain();
}
});
}

private static void internalMain() {
}


Tom

Igor Ganapolsky schrieb:
> The problem is this is not an RCP application, so there is no default
> Realm. It is simply null. I don't see a way to create my own realm in
> this class. I've tried several code snippets that I found online, but
> my Realm still ends up being null.
>
Re: DataBindingContext for TableViewer in ViewPart [message #12115 is a reply to message #12103] Mon, 08 June 2009 16:57 Go to previous messageGo to next message
Igor Ganapolsky is currently offline Igor GanapolskyFriend
Messages: 39
Registered: July 2009
Location: New York
Member

I already have a main method in another ApplicationWindow (the activator
class of my program). That class in turn calls several other composites
(which call other viewers). This is how my program is set up. Even if I
didn't extend ViewPart in this particular case, and just extended
TableViewer, I don't think that would make any difference as far as
instantiating a Realm because there is no main method in this class.
Re: DataBindingContext for TableViewer in ViewPart [message #12128 is a reply to message #12115] Mon, 08 June 2009 17:11 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Hi,

So do you have the lines of code i showed you or not. If you are the one
starting up the application you are the one responsible to define a
default realm it can't come from nirvana.

I'm still not sure why you are talking about ViewPart which is an
RCP-Concept normally in Eclipse-World and BTW subclassing TableViewer is
discouraged and bad practice!

I'm out of ideas on what to suggest because it's a bit confusing. If you
can't fix the problem with the information I've given to you I guess the
only thing that helps is an example-project I can run and fix locally.

Tom

Igor Ganapolsky schrieb:
> I already have a main method in another ApplicationWindow (the activator
> class of my program). That class in turn calls several other composites
> (which call other viewers). This is how my program is set up. Even if
> I didn't extend ViewPart in this particular case, and just extended
> TableViewer, I don't think that would make any difference as far as
> instantiating a Realm because there is no main method in this class.
Re: DataBindingContext for TableViewer in ViewPart [message #12156 is a reply to message #12103] Mon, 08 June 2009 19:36 Go to previous messageGo to next message
Igor Ganapolsky is currently offline Igor GanapolskyFriend
Messages: 39
Registered: July 2009
Location: New York
Member

I'm trying this code right now, but I'm getting a compilation error "Realm
cannot be resolved." How can I declare and instantiate a Realm?
Thank you.
Re: DataBindingContext for TableViewer in ViewPart [message #12166 is a reply to message #12128] Mon, 08 June 2009 19:51 Go to previous messageGo to next message
Igor Ganapolsky is currently offline Igor GanapolskyFriend
Messages: 39
Registered: July 2009
Location: New York
Member

I don't think I'm subclassing a TableViewer because I'm extending
ViewPart. Does that sound right?
Re: DataBindingContext for TableViewer in ViewPart [message #12176 is a reply to message #12128] Tue, 09 June 2009 00:31 Go to previous messageGo to next message
Igor Ganapolsky is currently offline Igor GanapolskyFriend
Messages: 39
Registered: July 2009
Location: New York
Member

I want to send you an example project. How do I attach it? Do you have
an email I can send it to?
Re: DataBindingContext for TableViewer in ViewPart [message #12911 is a reply to message #12103] Tue, 09 June 2009 00:45 Go to previous messageGo to next message
Igor Ganapolsky is currently offline Igor GanapolskyFriend
Messages: 39
Registered: July 2009
Location: New York
Member

I was able to compile your code snippet no problem (it was my spelling
error). However, when I do the following in another class which defines
the TableViewer I get an error "Realm cannot be null":

bindingContext = new DataBindingContext(Realm.getDefault());

Why is Realm null?
Re: DataBindingContext for TableViewer in ViewPart [message #12940 is a reply to message #12176] Tue, 09 June 2009 07:56 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Hi Igor,

I can't accept personal mails because of law issues so you need to post
your code to a public forum like this.

Please ensure that your code is as simple as possible and only
reproduces the problem I'm not interested in any of your domain code. So
please remove as many stuff as possible and then attach the code to this
thread and I'll take a look at it.

I hope that helps :-)

Tom

Igor Ganapolsky schrieb:
> I want to send you an example project. How do I attach it? Do you have
> an email I can send it to?
>
Re: DataBindingContext for TableViewer in ViewPart [message #13055 is a reply to message #12940] Tue, 09 June 2009 14:25 Go to previous messageGo to next message
Igor Ganapolsky is currently offline Igor GanapolskyFriend
Messages: 39
Registered: July 2009
Location: New York
Member

Here is the partial code of my TableViewer class. At I get an error
"Realm cannot be null when I create a DataBindingContext. There is no
need for you to see the rest of the code since it doesn't make it there.

public class OrderTableViewer extends TableViewer{
static int orderNumber;
BigDecimal subtotal;
//TableViewer for ordered items from DB
private TableViewer viewer;
TableViewerColumn column;
private DataBindingContext bindingContext;
//constructor
public OrderTableViewer(Composite parent) {
super(parent);
createPartControl(parent);
}

public void createPartControl(Composite parent) {
createViewer(parent);
// Get the content for the viewer, setInput will call getElements in the
// contentProvider
try{
viewer.setInput(ModelProvider.getInstance().getItems());
}
catch(Exception e){
System.err.println("Exception setting input for the viewer: " +
e.getMessage());
}

bindingContext = new DataBindingContext(Realm.getDefault());
bindingContext.getBindings().getRealm().exec(new Runnable() {
public void run() {
bindingContext.dispose();
bindingContext = null;
}
});
bindData();
}
Re: DataBindingContext for TableViewer in ViewPart [message #13083 is a reply to message #12940] Tue, 09 June 2009 14:27 Go to previous messageGo to next message
Igor Ganapolsky is currently offline Igor GanapolskyFriend
Messages: 39
Registered: July 2009
Location: New York
Member

Here is the partial code of my TableViewer class. I get an error "Realm
cannot be null" when I create a DataBindingContext. There is no need for
you to see the rest of the code since it doesn't make it there.

public class OrderTableViewer extends TableViewer{
static int orderNumber;
BigDecimal subtotal;
//TableViewer for ordered items from DB
private TableViewer viewer;
TableViewerColumn column;
private DataBindingContext bindingContext;
//constructor
public OrderTableViewer(Composite parent) {
super(parent);
createPartControl(parent);
}

public void createPartControl(Composite parent) {
createViewer(parent);
// Get the content for the viewer, setInput will call getElements in the
// contentProvider
try{
viewer.setInput(ModelProvider.getInstance().getItems());
}
catch(Exception e){
System.err.println("Exception setting input for the viewer: " +
e.getMessage());
}

bindingContext = new DataBindingContext(Realm.getDefault());
bindingContext.getBindings().getRealm().exec(new Runnable() {
public void run() {
bindingContext.dispose();
bindingContext = null;
}
});
bindData();
}
Re: DataBindingContext for TableViewer in ViewPart [message #13112 is a reply to message #13055] Tue, 09 June 2009 14:43 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Hi Igor,

Sorry that's not really helping me I need a *fully running example*
still even this code is not makeing sense to me either:
* you are disposing the binding-context after creating it?
* You are subclassing org.eclipse.jface.viewers.TableViewer or which
type is TableViewer

Tom

Igor Ganapolsky schrieb:
> Here is the partial code of my TableViewer class. At I get an error
> "Realm cannot be null when I create a DataBindingContext. There is no
> need for you to see the rest of the code since it doesn't make it there.
>
> public class OrderTableViewer extends TableViewer{
> static int orderNumber;
> BigDecimal subtotal;
> //TableViewer for ordered items from DB
> private TableViewer viewer;
> TableViewerColumn column;
> private DataBindingContext bindingContext;
> //constructor
> public OrderTableViewer(Composite parent) {
> super(parent);
> createPartControl(parent);
> }
>
> public void createPartControl(Composite parent) {
> createViewer(parent);
> // Get the content for the viewer, setInput will call
> getElements in the
> // contentProvider
> try{
> viewer.setInput(ModelProvider.getInstance().getItems());
> }
> catch(Exception e){
> System.err.println("Exception setting input for the viewer:
> " + e.getMessage());
> }
>
> bindingContext = new DataBindingContext(Realm.getDefault());
> bindingContext.getBindings().getRealm().exec(new Runnable() {
> public void run() {
> bindingContext.dispose();
> bindingContext = null;
> }
> });
> bindData();
> }
Re: DataBindingContext for TableViewer in ViewPart [message #13141 is a reply to message #13112] Tue, 09 June 2009 18:23 Go to previous messageGo to next message
Igor Ganapolsky is currently offline Igor GanapolskyFriend
Messages: 39
Registered: July 2009
Location: New York
Member

Sorry for not being clear. By saying "a fully running example" you mean a
class with a main method, right? Well I have a complex application with
many classes calling each other, so the Activator class where my main
method resides is not suitable for creating a DataBindingContext. That's
why I'm trying to create a DataBindingContext in the same class that
defines a TableViewer. And all it is is a plain class that has
TableViewer and column definitions to get from the model.
As far as the code I posted, I cleaned it up and and now its not extending
anything and I'm not disposing the DataBindingContext. Lets just say that
I have a line there:
DataBindingContext context = new DataBindingContext();
A runtime stack trace will show an error "Realm cannot be null" for this
line.
That's my main concern. Am I creating a DataBindingContext in the wrong
class? What general guidelines do you follow to decide which class to
create the DataBindingContext in?
Re: DataBindingContext for TableViewer in ViewPart [message #13170 is a reply to message #13141] Tue, 09 June 2009 19:27 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Hi,

Rule 1: If you control the start up you need to ensure a default realm
is available
Rule 2: 99% of the time you should create your DatabindingContext in the
Display-Thread

Start with a fresh plugin and try to come up with an example showing the
problem and runs on my host without modification. When I mean runs I
mean that I take you code paste it to my Eclipse and execute it to see
the stack my own.

Tom

Igor Ganapolsky schrieb:
> Sorry for not being clear. By saying "a fully running example" you mean
> a class with a main method, right? Well I have a complex application
> with many classes calling each other, so the Activator class where my
> main method resides is not suitable for creating a DataBindingContext.
> That's why I'm trying to create a DataBindingContext in the same class
> that defines a TableViewer. And all it is is a plain class that has
> TableViewer and column definitions to get from the model.
> As far as the code I posted, I cleaned it up and and now its not
> extending anything and I'm not disposing the DataBindingContext. Lets
> just say that I have a line there:
> DataBindingContext context = new DataBindingContext();
> A runtime stack trace will show an error "Realm cannot be null" for this
> line.
> That's my main concern. Am I creating a DataBindingContext in the wrong
> class? What general guidelines do you follow to decide which class to
> create the DataBindingContext in?
>
Re: DataBindingContext for TableViewer in ViewPart [message #13224 is a reply to message #13170] Tue, 09 June 2009 22:18 Go to previous messageGo to next message
Igor Ganapolsky is currently offline Igor GanapolskyFriend
Messages: 39
Registered: July 2009
Location: New York
Member

Just one more question before I create the plugin. My application is
single-threaded, so what do you mean Display-thread? How is the
Display-thread significant here if the entire application runs in a single
thread?
Re: DataBindingContext for TableViewer in ViewPart [message #13252 is a reply to message #13224] Tue, 09 June 2009 23:04 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Hi,

Are you sure once you talked about an Activator which would indicate
OSGi and then you by definition have multiple threads.

Tom

Igor Ganapolsky schrieb:
> Just one more question before I create the plugin. My application is
> single-threaded, so what do you mean Display-thread? How is the
> Display-thread significant here if the entire application runs in a
> single thread?
Re: DataBindingContext for TableViewer in ViewPart [message #13277 is a reply to message #13252] Wed, 10 June 2009 00:49 Go to previous messageGo to next message
Igor Ganapolsky is currently offline Igor GanapolskyFriend
Messages: 39
Registered: July 2009
Location: New York
Member

My fault, the only reason I said Activator is because that was created for
me by default (I guess I chose an OSGi project template or something).
But for the sake of clarity I'm not even using the Activator class. I'm
just referring to my MainClass as an activator,which has the main method
and calls all the other classes in my project. Does that sound fair?
Re: DataBindingContext for TableViewer in ViewPart [message #13305 is a reply to message #13277] Wed, 10 June 2009 07:44 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Hi,

Sounds better but it's not helping us either because you are not able to
provide me with code I can run :-( so what I do is shooting in the dark
and see if somebody is crying which in the end leads to nothing.

I'm afraid and sorry but I think here our support ends :-( Once more to
repeate all you are telling sounds a bit scary:
* You are having an Activator but your are not running OSGi but instead
you are using a class with a main method

* You are talking about ViewPart which is a concept of RCP but once more
you are not running an RCP

One final idea. Could you show me the code of your main-Method still a
complete stripped down version to reproduce is highly appreciated.

Tom

Igor Ganapolsky schrieb:
> My fault, the only reason I said Activator is because that was created
> for me by default (I guess I chose an OSGi project template or
> something). But for the sake of clarity I'm not even using the
> Activator class. I'm just referring to my MainClass as an
> activator,which has the main method and calls all the other classes in
> my project. Does that sound fair?
>
Re: DataBindingContext for TableViewer in ViewPart [message #14115 is a reply to message #12156] Wed, 10 June 2009 16:13 Go to previous messageGo to next message
Boris Bokowski is currently offline Boris BokowskiFriend
Messages: 272
Registered: July 2009
Senior Member
Hi Igor,

We recommend that somewhere early in your application, you use
Realm.runWithDefault similar to what Tom has suggested:
public static void main(String[] args) {
Display d = new Display();

Ream.runWithDefault( SWTObservables.getRealm(d), new Runnable() {
public void run() {
internalMain();
}
});
}

private static void internalMain() {
}

In fact, when you look at the data binding snippets, they all contain code
like this.

Without this, you will need to specify the realm explicitly for things like
creating observables, setting up a data binding context, etc. for example
like this:

Realm realm = SWTObservables.getRealm(viewer.getTable().getDisplay());
bindingContext = new DataBindingContext(realm);

Boris

"Igor Ganapolsky" <eazyigz@gmail.com> wrote in message
news:3dad193d5ed5fd12428395cfb7f0709b$1@www.eclipse.org...
> I'm trying this code right now, but I'm getting a compilation error "Realm
> cannot be resolved." How can I declare and instantiate a Realm?
> Thank you.
>
Re: DataBindingContext for TableViewer in ViewPart [message #17643 is a reply to message #12911] Mon, 29 June 2009 22:25 Go to previous message
Matthew Hall is currently offline Matthew HallFriend
Messages: 368
Registered: July 2009
Senior Member
Igor,

There is no default realm provided for you automatically--you must set
the default realm yourself. As Tom and Boris have suggested earlier,
the easiest way to fix this problem is to go back to your main class and
wrap the body in something like this:

before:

public class Main {
public void main(String[] args) {
// my main method
}
}

after:

public class Main {
public void main(String[] args) {
Realm.runWithDefault(
SWTObservables.getRealm(Display.getDefault()),
new Runnable() {
public void run() {
// my main method
}
} );
}
}

Hope this helps,

Matthew

Igor Ganapolsky wrote:
> I was able to compile your code snippet no problem (it was my spelling
> error). However, when I do the following in another class which defines
> the TableViewer I get an error "Realm cannot be null":
>
> bindingContext = new DataBindingContext(Realm.getDefault());
>
> Why is Realm null?
>
Previous Topic:ViewerSupport.bind issue in a ViewPart
Next Topic:SWTException: Invalid thread access when binding in non-UI thread
Goto Forum:
  


Current Time: Fri Mar 29 15:28:58 GMT 2024

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

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

Back to the top