Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [riena-dev] Unbind the modelBinding and rebind to an other attribute

Hi Elias,
 
thanks for the snippet special created for me with text -> Florian - Pirchner. ;-)
 
 
My usecase is different.
 
If i am binding an IObservableValue, i would like to remove the old binded IObservableValue.
Referring your example: I'd like to unbind the labelRidget, before the labelRidget2 is bound. So the valueUpdate from the text only updates the labelRidget2. The labelRidget should not be updated anymore (After binding the labelRidget2).
 
 
I am not sure that i understand the reply right:
>textRidget.bindToModel(...model1....)
>// textRidget now bound to model 1
>// later:
>textRidget.bindToModel(...model2...) //
>// textRidget now bound to model 2 and NOT bound to model1
 
textRidget now bound to model 2 and NOT bound to model1
If i push the "rebind" button, Output and Output2 are updated directly with the textValue. So i think model1 (as labelRidget from your snippet) is also bound? Or did i get something wrong?
 
 
 
Redview requires the function in the designer mode. The user can bind each attribute of a bean.
 
For example:
In the first step the user binds the attribute "name". Redview creates the binding using the related ridget and calles updateFromModel(). So the swtControl shows the correct value of the bean.
After this step the user recognizes that he binded the wrong attribute. So he will change the bounded attribute to "street". Again redview uses the ridget (which is already bound to name) and binds it to the attribute "street" of the bean. After calling updateFromModel() the control does not show the value of the attribute street, but again the value of the attribute name.
Before binding the attribute "street", I would like to remove the binding to "name".
 
 
I modified your snippet and made 2 different snippets.
SnippedChangedBean.java
SnippetChangedAttribute.java
 
SnippedChangedBean simulates the binding of two beans. If you press the button, the beans are alternate bound to the ridget. Bean1, bean2, bean1, bean2,..
On the first button Press, the Value changes from "value text of Bean 1" to "value text of Bean 2". Correct. Each other press only changes the beanName which was last bound. Not the value.
Try pressing the button several times.
 
SnippedChangedAttribute simulates the alternate binding of two different attributes of the same bean on pushing the button. "text1", "text2", "text1", "text2",... are bound alternate.
On the first button Press, the Value changes from "value of Text 2" to "value of Text 1". Each other press only changes the property which was last bound. Not the value.
Try pressing the button several times. 
 
Currently it works for redview. I am destroying the whole bindingDelegate and controller, and creating them completely new, if a bindingAttribute changes.
 
 
Thanks, Flo
 
 
 
 
-------- Original-Nachricht -------- Betreff: Re: [riena-dev] Unbind the modelBinding and rebind to an other attribute
Datum: Mon, 23 Feb 2009 20:01:52 -0800
Von: Elias Volanakis <elias@xxxxxxxxxxxxxxxxx>
Antwort an: Riena Developers list <riena-dev@xxxxxxxxxxx>
An: Riena Developers list <riena-dev@xxxxxxxxxxx>
Referenzen: <AcmT83pLvTQ/Db9pQBmJUWIT5H5XsA==> <d58a25cf0902202310x3cfc7eedl1afb09fa0f1501fe@xxxxxxxxxxxxxx>
 
Florian,
there is no API for unbinding. However you should be able to re-bind, i.e.:
textRidget.bindToModel(...model1....)
// textRidget now bound to model 1
// later:
textRidget.bindToModel(...model2...) //
// textRidget now bound to model 2 and NOT bound to model1
See attached snippet.
Elias.
On Fri, Feb 20, 2009 at 11:10 PM, Florian Pirchner
<florian.pirchner@xxxxxxxxx> wrote:
> Hi riena-team,
>
> i have a question to a very special binding case:
>
> Redview can be run in different modes. One of them is a designer mode, which
> offers a very dynamically behavior on manipulating the ui. With a designer
> editor the ui elements like controls, layouts and layoutdatas can be
> designed. The editor offers a direct update. This means, that changes to the
> model are directly reflected to the ui. This works quite well..
>
> Now i am implementing the modelBindings. With the great riena binding api it
> is also easy to accieve this. But i am not sure how to process binding
> changes in the designer editor best. If a ridget is already bound to the
> model and the bindingAttribute changes, i have to unbind the modelBinding
> and create a new one. I am not sure how to do this riena like. There is no
> api like ridget.unbindFromModel(). So i have to destroy the controller
> completely an create it again. It works well, but maybe there is a better
> way.
>
> For example:
> TextRidget is bound to the attribute "lastName" of the Adress bean. The
> designer shows "Pirchner".
> Now the designer decides to bind an other attribute like Address.firstName.
> After the rebinding, the designer should show "Flo".
> So i have to unbind the TextRidget<->Adress.lastName binding and create a
> new one: TextRidget<->Adress.firstName
>
> Thanks in advance for your answer
> Flo
>
>
 
--
Elias Volanakis
Technical Lead
Innoopract (EclipseSource)
351 NW 12th Avenue
Portland, Oregon 97209
elias@xxxxxxxxxxxxxxxxx
evolanakis@xxxxxxxxxxxxxx
Tel: +1-503-552-1457
Fax: +1-503-715-4915
Mobile: +1-503-929-5537
---
Innoopract is becoming EclipseSource - www.eclipsesource.com
/*******************************************************************************
 * Copyright (c) 2007, 2008 compeople AG and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    compeople AG - initial API and implementation
 *******************************************************************************/
package com.redview.ui.riena.snippets;

import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.riena.beans.common.AbstractBean;
import org.eclipse.riena.ui.ridgets.ITextRidget;
import org.eclipse.riena.ui.ridgets.swt.SwtRidgetFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * Text field ridget with minimum length validation rule and direct writing.
 */
public final class SnippetChangedAttribute {

	// specifies the bindingProperty to PROP_TEXT2
	private String nextBindingProperty = TestBean.PROP_TEXT2;

	// Instantiate the bean
	private TestBean bean = new TestBean();

	public static void main(String[] args) {
		new SnippetChangedAttribute().testIt();
	}

	private void testIt() {

		Display display = Display.getDefault();
		try {
			/**
			 * Create the ui
			 */
			Shell shell = new Shell();
			GridLayoutFactory.fillDefaults().numColumns(2).margins(10, 10)
					.spacing(20, 10).applyTo(shell);
			Label label = createLabel(shell, "Value"); //$NON-NLS-1$
			GridDataFactory.fillDefaults().grab(true, false).applyTo(label);
			Text text = new Text(shell, SWT.BORDER);
			GridDataFactory.fillDefaults().grab(true, false).applyTo(text);
			createLabel(shell, "Current property"); //$NON-NLS-1$
			final Label currentProperty = createLabel(shell, ""); //$NON-NLS-1$
			GridDataFactory.fillDefaults().grab(true, false).applyTo(
					currentProperty);

			/**
			 * Bind to the model
			 */
			final ITextRidget textRidget = (ITextRidget) SwtRidgetFactory
					.createRidget(text);
			textRidget.setDirectWriting(true);
			String currentBindedProperty = getBindingProperty();
			textRidget.bindToModel(BeansObservables.observeValue(bean,
					currentBindedProperty));
			textRidget.updateFromModel();

			updateCurrentProperty(currentProperty, currentBindedProperty);

			/**
			 * Creates the button
			 */
			Button button = new Button(shell, SWT.PUSH);
			button.setText("switch the binded property");
			button.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					String currentBindedProperty = getBindingProperty();
					textRidget.bindToModel(BeansObservables.observeValue(bean,
							currentBindedProperty));
					textRidget.updateFromModel();
					updateCurrentProperty(currentProperty,
							currentBindedProperty);
				}
			});

			shell.setSize(400, 200);
			shell.open();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} finally {
			display.dispose();
		}

	}

	protected void updateCurrentProperty(final Label currentProperty,
			String currentBindedProperty) {
		currentProperty.setText(currentBindedProperty);
	}

	private String getBindingProperty() {
		String tempProperty = nextBindingProperty;
		// initialize the next property to bind -> If current = text1 -> next is
		// text2 and revers.
		nextBindingProperty = nextBindingProperty.equals(TestBean.PROP_TEXT1) ? TestBean.PROP_TEXT2
				: TestBean.PROP_TEXT1;
		return tempProperty;
	}

	private static Label createLabel(Shell shell, String caption) {
		Label result = new Label(shell, SWT.NONE);
		result.setText(caption);
		return result;
	}

	private class TestBean extends AbstractBean {

		protected static final String PROP_TEXT1 = "text1";
		protected static final String PROP_TEXT2 = "text2";

		private String text1 = "value of Text 1";
		private String text2 = "value of Text 2";

		public String getText1() {
			System.out.println("getting value of text1");
			return text1;
		}

		public void setText1(String text1) {
			firePropertyChanged(PROP_TEXT1, this.text1, this.text1 = text1);
		}

		public String getText2() {
			System.out.println("getting value of text2");
			return text2;
		}

		public void setText2(String text2) {
			firePropertyChanged(PROP_TEXT2, this.text2, this.text2 = text2);
		}
	}
}
/*******************************************************************************
 * Copyright (c) 2007, 2008 compeople AG and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    compeople AG - initial API and implementation
 *******************************************************************************/
package com.redview.ui.riena.snippets;

import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.riena.beans.common.AbstractBean;
import org.eclipse.riena.ui.ridgets.ILabelRidget;
import org.eclipse.riena.ui.ridgets.swt.SwtRidgetFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

/**
 * Text field ridget with minimum length validation rule and direct writing.
 */
public final class SnippetChangedBean {

	/**
	 * The bean which is bounded next
	 */
	private TestBean nextBean;

	// Instantiate the bean
	private TestBean bean1;
	private TestBean bean2;

	public SnippetChangedBean() {
		bean1 = new TestBean("Bean 1");
		bean1.setText("value text of Bean 1");

		bean2 = new TestBean("Bean 2");
		bean2.setText("value text of Bean 2");

		nextBean = bean1;
	}

	public static void main(String[] args) {
		new SnippetChangedBean().testIt();
	}

	private void testIt() {

		Display display = Display.getDefault();
		try {
			/**
			 * Create the ui
			 */
			Shell shell = new Shell();
			GridLayoutFactory.fillDefaults().numColumns(2).margins(10, 10)
					.spacing(20, 10).applyTo(shell);
			Label label = createLabel(shell, "Value"); //$NON-NLS-1$
			GridDataFactory.fillDefaults().grab(true, false).applyTo(label);
			Label value = createLabel(shell, "");
			GridDataFactory.fillDefaults().grab(true, false).applyTo(value);
			createLabel(shell, "Current binded bean"); //$NON-NLS-1$

			final Label currentBean = createLabel(shell, ""); //$NON-NLS-1$
			GridDataFactory.fillDefaults().grab(true, false).applyTo(
					currentBean);

			/**
			 * Bind to the model
			 */
			final ILabelRidget valueRidget = (ILabelRidget) SwtRidgetFactory
					.createRidget(value);
			TestBean current = getBindingBean();
			valueRidget.bindToModel(BeansObservables.observeValue(current,
					"text"));
			valueRidget.updateFromModel();
			currentBean.setText(current.name);

			/**
			 * Creates the button
			 */
			Button button = new Button(shell, SWT.PUSH);
			button.setText("switch the binded bean");
			button.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					TestBean current = getBindingBean();
					valueRidget.bindToModel(BeansObservables.observeValue(
							current, "text"));
					valueRidget.updateFromModel();
					currentBean.setText(current.name);
				}
			});

			shell.setSize(400, 200);
			shell.open();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} finally {
			display.dispose();
		}

	}

	protected void updateCurrentProperty(final Label currentProperty,
			String currentBindedProperty) {
		currentProperty.setText(currentBindedProperty);
	}

	private TestBean getBindingBean() {
		TestBean currentBean = nextBean;
		// initialize the next bean to bind -> If current = bean1 -> next is
		// bean2 and revers.
		nextBean = nextBean == bean1 ? bean2 : bean1;
		return currentBean;
	}

	private static Label createLabel(Shell shell, String caption) {
		Label result = new Label(shell, SWT.NONE);
		result.setText(caption);
		return result;
	}

	private class TestBean extends AbstractBean {

		protected static final String PROP_TEXT = "text";
		protected int counter;

		private String text;
		private String name;

		private TestBean(String name) {
			this.name = name;
		}

		public String getText() {
			counter++;
			System.out.println("getting text of " + name);
			System.out.println("Readed " + counter + " times.");
			return text;
		}

		public void setText(String text) {
			firePropertyChanged(PROP_TEXT, this.text, this.text = text);
		}
	}
}

Back to the top