Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » [Databinding] A complex scenario
[Databinding] A complex scenario [message #1245060] Thu, 13 February 2014 08:31 Go to next message
Cedric Moonen is currently offline Cedric MoonenFriend
Messages: 274
Registered: August 2009
Senior Member
Hello,

Sometimes I have I encounter situations where I do not see how to use databinding to bind all UI functionality to my model.
For instance, suppose that my user can select a "destination" on the UI: this is a choice (radio buttons) between a file or a socket connection. Each option comes with specific settings (for the file, the user has to specify the filename and for socket, he has to specify the hostname and port number). Of course, if the user selected the file as destination, the hostname and port number widgets are disabled (see on the attached image).

For the model, I use EMf (I guess this doesn't make too much difference) and I have a base "Destination" class, a "FileDestination" (extending "Destination") and a "SocketDestination" class (also extending "Destination"). The 2 concrete classes have specific fields (e.g. the filename or the hostname and port number fields).

I'm now wondering how I can use databind for this scenario ? I need to bind the selection of the radio buttons based on the type of the destination in the model (instanceof), I guess this is not possible ?

Is it possible to use databinding to fully cover this case?

Thank you.

Re: [Databinding] A complex scenario [message #1277785 is a reply to message #1245060] Wed, 26 March 2014 13:19 Go to previous messageGo to next message
Nigel Westbury is currently offline Nigel WestburyFriend
Messages: 18
Registered: July 2009
Junior Member
Hi Cedric,

If you were doing a one-way binding then you can CalculatedValue to
cover almost anything. For example, here is a boolean observable than
can be bound to the 'selected' property

IObservableValue<Boolean> observeIsFile = new ComputedValue<Boolean>() {
@Override
protected Boolean calculate() {
return model.getDestination() instanceof FileDestination;
}
};

The above will work only if 'getDestination' is a tracked getter. If it
is not then you have to create an observable and get the value from that:

IObservableValue<Boolean> observeIsFile = new ComputedValue<Boolean>() {
@Override
protected Boolean calculate() {
IValueProperty<Model, Destination> destinationProperty =
BeanProperties.value(Model.class, "destination", Destination.class);
return destinationProperty.observe(model).getValue() instanceof
FileDestination;
}
};

Or in this simple case it may be cleaner just to listen to changes in
the destination object directly, so not make full use of data binding.

The problem is going back the other way. Changing the radio button
needs to result in a change in the destination instance. One way of
doing that would be to create two instances, one a FileDestination and
one a SocketDestination. Bind each control to a field in the
appropriate one, and then assign the appropriate one when 'OK' is
pressed. Lots of ways, really, and the 'pure databinding' way is
probably not going to be the simplest in this case.

Nigel Westbury

On 13/02/2014 08:31, Cedric Moonen wrote:
> Hello,
>
> Sometimes I have I encounter situations where I do not see how to use databinding to bind all UI functionality to my model.
> For instance, suppose that my user can select a "destination" on the UI: this is a choice (radio buttons) between a file or a socket connection. Each option comes with specific settings (for the file, the user has to specify the filename and for socket, he has to specify the hostname and port number). Of course, if the user selected the file as destination, the hostname and port number widgets are disabled (see on the attached image).
>
> For the model, I use EMf (I guess this doesn't make too much difference) and I have a base "Destination" class, a "FileDestination" (extending "Destination") and a "SocketDestination" class (also extending "Destination"). The 2 concrete classes have specific fields (e.g. the filename or the hostname and port number fields).
>
> I'm now wondering how I can use databind for this scenario ? I need to bind the selection of the radio buttons based on the type of the destination in the model (instanceof), I guess this is not possible ?
>
> Is it possible to use databinding to fully cover this case?
>
> Thank you.
>
>
Re: [Databinding] A complex scenario [message #1279364 is a reply to message #1245060] Fri, 28 March 2014 16:33 Go to previous message
Christophe Bouhier is currently offline Christophe BouhierFriend
Messages: 937
Registered: July 2009
Senior Member
On 13-02-14 09:31, Cedric Moonen wrote:
> Hello,
>
> Sometimes I have I encounter situations where I do not see how to use databinding to bind all UI functionality to my model.
> For instance, suppose that my user can select a "destination" on the UI: this is a choice (radio buttons) between a file or a socket connection. Each option comes with specific settings (for the file, the user has to specify the filename and for socket, he has to specify the hostname and port number). Of course, if the user selected the file as destination, the hostname and port number widgets are disabled (see on the attached image).
>
> For the model, I use EMf (I guess this doesn't make too much difference) and I have a base "Destination" class, a "FileDestination" (extending "Destination") and a "SocketDestination" class (also extending "Destination"). The 2 concrete classes have specific fields (e.g. the filename or the hostname and port number fields).
>
> I'm now wondering how I can use databind for this scenario ? I need to bind the selection of the radio buttons based on the type of the destination in the model (instanceof), I guess this is not possible ?
>
I Cedric, I have many similar cases which I have developed a pattern
for. I call it aggregate databinding. The concept is straightforward.
What is does is aggregate the value changes in an Class and maintain
intermediate state, I'll try to explain how it works.

Let say you have multiple UI widgets like in your case, radio button,
text fields etc. Make sure you have an IObservable for each of them.
(Typically an SWTObservable).

....then create what I call an aggregate class which implements
IValueChangeListener and/or IListChangeListener depending on your
IObservable.

Add an instance of this class to all your UI Widget Observables, with:

observeText.addValueChangeListener(bindingAggregate);
obsverveSelection.addValueChangeListener(bindingAggregate);
obsverveList.addListChangeListener(bindingAggregate);


.... next create IObservables for your model with EMFEditProperties or
EMFProperties depending if you have EditingDomain or not and them as well.

observeFeature1.addValueChangeListener(bindingAggregate);
observeFeature1.addValueChangeListener(bindingAggregate);


Make sure your class knows about these observables (They should be
accessible from with your aggregate).

Now all your observations are received in a single class, which make
things very handy.


You are now in control of maintaining an intermediate state for your
binding. What I mean by this is best illustrated with an example:
Imagine two UI widget's values should map to one Model value.

Wid1 => Check button
Wid2 => Check button
=
Model1 => Wid1 & Wid2 are true.

With the aggregate class you can keep a state (i.e. field boolean
wid1&2AreTrue) when both widgets notify you they have the correct value,
you set wid1&2AreTrue = true, and call setValue on corresponding the
model IObservable for Model1.

This is just an example, but will work for your case. I use it to map
very complex UI states to reduced or expanded number of model features
(Speaking in EMF terms). You can even elaborate on this concept and
include validation or work with intermediate writable's to allow
transformations before you hit the aggregate.

HTH , Christophe



> Is it possible to use databinding to fully cover this case?
>
> Thank you.
>
>
Previous Topic:JFace databinding issues after migration
Next Topic:TreeViewer vs TableViewer
Goto Forum:
  


Current Time: Fri Mar 29 05:08:14 GMT 2024

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

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

Back to the top