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 10:02   | 
 
Eclipse User  | 
 | 
 | 
   | 
 
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 #335975 is a reply to message #335959] | 
Tue, 12 May 2009 07:55    | 
 
Eclipse User  | 
 | 
 | 
   | 
 
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 #336013 is a reply to message #335983] | 
Wed, 13 May 2009 11:40   | 
 
Eclipse User  | 
 | 
 | 
   | 
 
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
 |  
 |  
  |   
Goto Forum:
 
 Current Time: Mon Nov 03 22:23:41 EST 2025 
 Powered by  FUDForum. Page generated in 0.04227 seconds  
 |