Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » [Databinding] bind modify of text to boolean
[Databinding] bind modify of text to boolean [message #335952] Mon, 11 May 2009 14:02 Go to next message
rollo is currently offline rolloFriend
Messages: 117
Registered: July 2009
Senior Member
Hi,

I'm quite new to eclipse databinding.

I have a Text which is bind to model.

public static void bindTextToValue( Text text, Object object, String
propertyName, IValidator validator ) {

ISWTObservableValue textValue = SWTObservables.observeText( text,
SWT.Modify );

IObservableValue objectValue = BeansObservables.observeValue(
getRealm(), object, propertyName );

UpdateValueStrategy strategy = new UpdateValueStrategy();
strategy.setAfterGetValidator( validator );

getContext().bindValue( textValue, objectValue, strategy, strategy );
}

This works.

But now I want to bind the text also to a boolean value, to notify if
the text has changed.

I did it like that:

ISWTObservableValue targetObservableValue = SWTObservables.observeText(
text, SWT.Modify );

IObservableValue modelObservableValue = BeansObservables.observeValue(
this, "changed" );

getContext().bindValue( targetObservableValue, modelObservableValue );

I want that the boolean "changed" is set to "true" if the user modify
the text.

Is it possible to bind selveral things to one text?

Roland
Re: [Databinding] bind modify of text to boolean [message #335959 is a reply to message #335952] Mon, 11 May 2009 16:21 Go to previous messageGo to next message
Matthew Hall is currently offline Matthew HallFriend
Messages: 368
Registered: July 2009
Senior Member
Roland Siebert wrote:
> I'm quite new to eclipse databinding.

Welcome to the party :)

> I want that the boolean "changed" is set to "true" if the user modify
> the text.

You need to customize your binding with update strategies to do that.
Assuming that the text field is the target and the changed property is
the model:
* Set your model-to-target update strategy's update policy to
POLICY_NEVER to make sure the binding is one-way
* Set your target-to-model update strategy's converter to a custom
converter that always returns Boolean.TRUE.
* Make sure this binding is created *after* the other one(s) that bind
to the text field. Otherwise your "changed" property will be set to
true before you form is even set up.

This is a brittle solution and it will break down in master-detail
forms. But hopefully it is enough to solve your current problem.

> Is it possible to bind selveral things to one text?

Yes, however you have to be careful about ripple effects. e.g. suppose
M1 and M2 are both bound to T:

* A change to T will update M1 and M2 at once.
* A change to M1 will update T, which will in turn update M2.

Be very careful not to create a diamond-shaped binding scenario or you
will likely get a stack overflow. e.g. suppose M1 binds to T1, T1 binds
to M2, M2 binds to T2, T2 binds to M1. An update to any of these could
end up sending the update round and round until the stack overflows.

Matthew
Re: [Databinding] bind modify of text to boolean [message #335975 is a reply to message #335959] Tue, 12 May 2009 11:55 Go to previous messageGo to next message
rollo is currently offline rolloFriend
Messages: 117
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------030703080209030208070609
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Hi Matthew,

Thank you very much. Your explanation really helped.

Now I have only one problem.
When I select the "saveButton", which was enabled when the "changed" was
set to "true", "changed" should set back to "false".

But I can't get it working.

I attached a little snippet.

--------------030703080209030208070609
Content-Type: text/plain;
name="TestSnippet.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="TestSnippet.java"

package org.eclipse.jface.examples.databinding.snippets;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.beans.PojoObservables;
import org.eclipse.core.databinding.conversion.IConverter;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.value.ComputedValue;
import org.eclipse.core.databinding.observable.value.IObservableVal ue;
import org.eclipse.jface.databinding.swt.ISWTObservableValue;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class TestSnippet {

static Person person = new Person("John", "Smith");

public static void main(String[] args) {

Display display = new Display();

Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
public void run() {
Display display = Display.getCurrent();

Shell shell = new Shell(display);
shell.setLayout(new RowLayout(SWT.VERTICAL));

Text first = new Text(shell, SWT.BORDER);

Text last = new Text(shell, SWT.BORDER);

Button button = new Button(shell, SWT.NONE);
button.setText("Save");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
// save...
}
});

// here comes the binding
DataBindingContext context = new DataBindingContext();

// bind first to Person#firstName
ISWTObservableValue firstValue = SWTObservables.observeText(
first, SWT.Modify);
IObservableValue firstModelValue = PojoObservables
.observeValue(person, "firstName");
context.bindValue(firstValue, firstModelValue,
new UpdateValueStrategy(), new UpdateValueStrategy());

// bind last to Person#lastName
ISWTObservableValue lastValue = SWTObservables.observeText(
last, SWT.Modify);
IObservableValue lastModelValue = PojoObservables.observeValue(
person, "lastName");
context.bindValue(lastValue, lastModelValue,
new UpdateValueStrategy(), new UpdateValueStrategy());

// bind modify of first/last to dirty
IObservableValue needSavingValue = PojoObservables
.observeValue(person, "needSaving");
UpdateValueStrategy t2m = new UpdateValueStrategy();
t2m.setConverter(new IConverter() {

public Object convert(Object fromObject) {
return Boolean.TRUE;
}

public Object getFromType() {
return String.class;
}

public Object getToType() {
return Boolean.class;
}

});
context.bindValue(firstValue, needSavingValue, t2m,
new UpdateValueStrategy(
UpdateValueStrategy.POLICY_NEVER));
context.bindValue(lastValue, needSavingValue, t2m,
new UpdateValueStrategy(
UpdateValueStrategy.POLICY_NEVER));

// bind the button to dirty - enable when dirty
ISWTObservableValue enableValue = SWTObservables
.observeEnabled(button);
context.bindValue(enableValue, needSavingValue,
new UpdateValueStrategy(), new UpdateValueStrategy());

// TODO bind the selection of button to dirty
final ISWTObservableValue buttonselectValue = SWTObservables
.observeSelection(button);
ComputedValue value = new ComputedValue(Boolean.TYPE) {

protected Object calculate() {
return new Boolean(!((Boolean) buttonselectValue
.getValue()).booleanValue());
}
};
context.bindValue(value, needSavingValue,
new UpdateValueStrategy(), new UpdateValueStrategy(
UpdateValueStrategy.POLICY_NEVER));

shell.pack();
shell.open();

while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
});
}

static class Person {
private String firstName;
private String lastName;
boolean needSaving;

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

public void setFirstName(String firstName) {
System.err.println("firstName changed from '" + this.firstName
+ "' to '" + firstName + "'.");
this.firstName = firstName;
}

public String getFirstName() {
return firstName;
}

public void setLastName(String lastName) {
System.err.println("lastName changed from '" + this.lastName
+ "' to '" + lastName + "'.");
this.lastName = lastName;
}

public String getLastName() {
return lastName;
}

public void setNeedSaving(boolean needSaving) {
System.err.println("needSaving changed from '" + this.needSaving
+ "' to '" + needSaving + "'.");
this.needSaving = needSaving;
}

public boolean isNeedSaving() {
return needSaving;
}
}

}

--------------030703080209030208070609--
Re: [Databinding] bind modify of text to boolean [message #335983 is a reply to message #335975] Tue, 12 May 2009 18:17 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: eclipse-news.rizzoweb.com

Roland wrote:
> Hi Matthew,
>
> Thank you very much. Your explanation really helped.
>
> Now I have only one problem.
> When I select the "saveButton", which was enabled when the "changed" was
> set to "true", "changed" should set back to "false".

In the event handler of the Save button, it will need to set the model
value back to false [call setNeedSaving(false)]. As long as you've set
up the binding correctly (bi-directional), that should automatically
make the button disabled again.

Hope this helps,
Eric
Re: [Databinding] bind modify of text to boolean [message #336013 is a reply to message #335983] Wed, 13 May 2009 15:40 Go to previous message
rollo is currently offline rolloFriend
Messages: 117
Registered: July 2009
Senior Member
Thanks it works well now.

Roland


Eric Rizzo schrieb:
> Roland wrote:
>> Hi Matthew,
>>
>> Thank you very much. Your explanation really helped.
>>
>> Now I have only one problem.
>> When I select the "saveButton", which was enabled when the "changed"
>> was set to "true", "changed" should set back to "false".
>
> In the event handler of the Save button, it will need to set the model
> value back to false [call setNeedSaving(false)]. As long as you've set
> up the binding correctly (bi-directional), that should automatically
> make the button disabled again.
>
> Hope this helps,
> Eric
Previous Topic:How do I manually drop plugins into an eclipse install in 3.5M7?
Next Topic:What is the difference between the eclipse packages and the SDK?
Goto Forum:
  


Current Time: Sun Jul 21 17:23:44 GMT 2024

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

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

Back to the top