Binding Data with a textbox [message #758210] |
Tue, 22 November 2011 10:58 |
Etienne Beaumont Messages: 9 Registered: November 2011 |
Junior Member |
|
|
Hi everyone,
My name is Etienne and I'm a electronic engineer who has to peak in the high-level computer science for a few months.
Please pardon my poor english as it's not my first language.
I am modifying the EMF generated tool and I am having trouble with the update of my text fields. With the code below, my model is modified when I change the text box but with a redo my textbox is not modified (logically) as It would be with a Jface databinding on a tableviewer.
I am confused with the numerous functions available for the EMF framework and JFace Databindings... I'm afraid my code won't seem beautiful to professional EMF programers... Sorry about that
Thanks for your help,
Etienne
package vtxml.page.tests;
import java.util.Iterator;
import java.util.List;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.edit.provider.AdapterFactoryItemDelegator;
import org.eclipse.emf.edit.provider.IItemPropertyDescriptor;
import org.eclipse.emf.edit.provider.ItemPropertyDescriptor.PropertyValueWrapper;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import vtxml.VtxmlPackage;
import vtxml.impl.DocImpl;
import vtxml.presentation.VtxmlEditor;
public class Snippet extends Composite {
private DocImpl docImpl;
private VtxmlEditor ourEditor=null;
Snippet(Composite parent, int style) {
super(parent, style);
createTextField(VtxmlPackage.DOC__TYPE);
createTextField(VtxmlPackage.DOC__TITLE);
}
private void createTextField(final int featureID) {
final List l = new AdapterFactoryItemDelegator(ourEditor.getAdapterFactory()).getPropertyDescriptors(docImpl);
for (Iterator i = l.iterator(); i.hasNext();) {
final IItemPropertyDescriptor desc = (IItemPropertyDescriptor) i.next();
if (((EStructuralFeature) desc.getFeature(docImpl)).getFeatureID() == featureID) {
if (desc.getPropertyValue(docImpl) == null)
desc.setPropertyValue(docImpl, "");
Text myText = new Text(this, SWT.NONE);
myText.setText(((PropertyValueWrapper) desc.getPropertyValue(docImpl)).getEditableValue(docImpl) + "");
myText.setEditable(true);
myText.addModifyListener(
new ModifyListener() {
public void modifyText(ModifyEvent e) {
for (Iterator j = l.iterator(); j.hasNext();) {
IItemPropertyDescriptor descmod = (IItemPropertyDescriptor) j.next();
if (((EStructuralFeature) descmod.getFeature(docImpl)).getFeatureID() == featureID) {
if (((PropertyValueWrapper) descmod.getPropertyValue(docImpl)).getEditableValue(docImpl) instanceof java.lang.Integer) {
if (!((Text) e.widget).getText().equals(""))
descmod.setPropertyValue(docImpl, new Integer(Integer.parseInt(((Text) e.widget).getText())));
} else if (((PropertyValueWrapper) descmod.getPropertyValue(docImpl)).getEditableValue(docImpl) instanceof java.lang.String) {
descmod.setPropertyValue(docImpl, ((Text) e.widget).getText());
}
}
}
}
});
}
}
}
}
|
|
|
Re: Binding Data with a textbox [message #758217 is a reply to message #758210] |
Tue, 22 November 2011 11:27 |
Ed Merks Messages: 33142 Registered: July 2009 |
Senior Member |
|
|
Etienne,
Comments below.
On 22/11/2011 11:58 AM, etienne4eclipse wrote:
> Hi everyone,
>
> My name is Etienne and I'm a electronic engineer who has to peak in
> the high-level computer science for a few months.
>
> Please pardon my poor english as it's not my first language.
>
> I am modifying the EMF generated tool and I am having trouble with the
> update of my text fields. With the code below, my model is modified
> when I change the text box but with a redo my textbox is not modified
> (logically) as It would be with a Jface databinding on a tableviewer.
>
> I am confused with the numerous functions available for the EMF
> framework and JFace Databindings... I'm afraid my code won't seem
> beautiful to professional EMF programers... Sorry about that :)
> Thanks for your help,
>
> Etienne
>
>
> package vtxml.page.tests;
>
> import java.util.Iterator;
> import java.util.List;
>
> import org.eclipse.emf.ecore.EStructuralFeature;
> import org.eclipse.emf.edit.provider.AdapterFactoryItemDelegator;
> import org.eclipse.emf.edit.provider.IItemPropertyDescriptor;
> import
> org.eclipse.emf.edit.provider.ItemPropertyDescriptor.PropertyValueWrapper;
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.events.ModifyEvent;
> import org.eclipse.swt.events.ModifyListener;
> import org.eclipse.swt.widgets.Composite;
> import org.eclipse.swt.widgets.Text;
>
> import vtxml.VtxmlPackage;
> import vtxml.impl.DocImpl;
> import vtxml.presentation.VtxmlEditor;
>
> public class Snippet extends Composite {
>
> private DocImpl docImpl;
> private VtxmlEditor ourEditor=null;
>
> Snippet(Composite parent, int style) {
> super(parent, style);
>
> createTextField(VtxmlPackage.DOC__TYPE);
> createTextField(VtxmlPackage.DOC__TITLE);
> }
>
> private void createTextField(final int featureID) {
> final List l = new
> AdapterFactoryItemDelegator(ourEditor.getAdapterFactory()).getPropertyDescriptors(docImpl);
> for (Iterator i = l.iterator(); i.hasNext();) {
> final IItemPropertyDescriptor desc =
> (IItemPropertyDescriptor) i.next();
> if (((EStructuralFeature)
> desc.getFeature(docImpl)).getFeatureID() == featureID) {
Better to use the feature instances rather than their IDs (because this
code won't work in general for multiple inheritance.) Note that
AdapterFactoryItemDelegator has a getPropertyDescriptor method that
takes a feature argument so you can avoid a loop.
>
> if (desc.getPropertyValue(docImpl) == null)
> desc.setPropertyValue(docImpl, "");
Do you really want to update the model itself? That seems problematic.
>
> Text myText = new Text(this, SWT.NONE);
> myText.setText(((PropertyValueWrapper)
> desc.getPropertyValue(docImpl)).getEditableValue(docImpl) + "");
What are you adding an empty string for?
> myText.setEditable(true);
>
> myText.addModifyListener(
> new ModifyListener() {
> public void modifyText(ModifyEvent e) {
> for (Iterator j = l.iterator();
> j.hasNext();) {
> IItemPropertyDescriptor descmod =
> (IItemPropertyDescriptor) j.next();
> if (((EStructuralFeature)
> descmod.getFeature(docImpl)).getFeatureID() == featureID) {
Why find the descriptor again when you already have it above?
> if (((PropertyValueWrapper)
> descmod.getPropertyValue(docImpl)).getEditableValue(docImpl)
> instanceof java.lang.Integer) {
> if (!((Text)
> e.widget).getText().equals(""))
>
> descmod.setPropertyValue(docImpl, new Integer(Integer.parseInt(((Text)
> e.widget).getText())));
Horrible how you repeat large subexpressions rather than caching the
value. Do you step through this with the debugger. If undo isn't
working, I imagine it's because the editing domain docImpl can't be
determined. I can't say why from what you've shown so far.
> } else if
> (((PropertyValueWrapper)
> descmod.getPropertyValue(docImpl)).getEditableValue(docImpl)
> instanceof java.lang.String) {
>
> descmod.setPropertyValue(docImpl, ((Text) e.widget).getText());
> }
> }
> }
> }
> });
> }
> }
> }
> }
>
>
>
>
>
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
Re: Binding Data with a textbox [message #758296 is a reply to message #758217] |
Tue, 22 November 2011 15:44 |
Etienne Beaumont Messages: 9 Registered: November 2011 |
Junior Member |
|
|
Hi Ed,
Thank you for your reply !
I modified my code to use your getPropertyDescriptor method, and i win something like half the code I wrote for this function
Too bad the proposition you made in this thread
eclipsezone.com/eclipse/forums/t109581.html
didn't work
For the "undo" problem, the model is correctly undoing the set command, but the textfield itself is not set back to reflect the model.
I tought you would tell me about some observable-related mecanism... What is the correct class family to use for this purpose in emf ?
Thanks again !
I put back the modified code as as a reader I don't like to find a topic without a "correct" code at the end
package vtxml.page.tests;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.edit.provider.AdapterFactoryItemDelegator;
import org.eclipse.emf.edit.provider.IItemPropertyDescriptor;
import org.eclipse.emf.edit.provider.ItemPropertyDescriptor.PropertyValueWrapper;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import vtxml.VtxmlPackage;
import vtxml.impl.DocImpl;
import vtxml.presentation.VtxmlEditor;
public class Snippet extends Composite {
//just for snippeting purpose
private DocImpl docImpl;
private VtxmlEditor ourEditor=null;
Snippet(Composite parent, int style) {
super(parent, style);
createTextField(VtxmlPackage.eINSTANCE.getDoc_Type());
createTextField(VtxmlPackage.eINSTANCE.getDoc_Title());
}
private void createTextField(EStructuralFeature feature) {
AdapterFactoryItemDelegator adapterFactoryItemDelegator = new AdapterFactoryItemDelegator(ourEditor.getAdapterFactory());
final IItemPropertyDescriptor iIPd = adapterFactoryItemDelegator.getPropertyDescriptor(docImpl, feature.getName());
Text myText = new Text(this, SWT.NONE);
myText.setText((String)((PropertyValueWrapper) iIPd.getPropertyValue(docImpl)).getEditableValue(docImpl));
myText.setEditable(true);
myText.addModifyListener(
new ModifyListener() {
public void modifyText(ModifyEvent e) {
iIPd.setPropertyValue(docImpl, ((Text) e.widget).getText());
}
}
);
}
}
[Updated on: Tue, 22 November 2011 16:18] Report message to a moderator
|
|
|
Re: Binding Data with a textbox [message #758320 is a reply to message #758296] |
Tue, 22 November 2011 17:54 |
Ed Merks Messages: 33142 Registered: July 2009 |
Senior Member |
|
|
Etienne,
Given you do nothing to listen to the model and update the text field,
you shouldn't expect that. You should learn about data binding. That
will be much simpler.
On 22/11/2011 4:44 PM, etienne4eclipse wrote:
> Hi Ed,
>
> Thank you for your reply !
>
> I modified my code to use your getPropertyDescriptor method, and i win
> something like half the code I wrote for this function Too bad the
> proposition you made in this thread didn't work
>
> For the "undo" problem, the model is correctly undoing the set
> command, but the textfield itself is not set back to reflect the model.
>
> I tought you would tell me about some observable-related mecanism...
> What is the correct class family to use for this purpose in emf ?
>
> Thanks again !
>
> I put back the modified code as as a reader I don't like to find a
> topic without a "correct" code at the end
>
>
> package vtxml.page.tests;
>
> import org.eclipse.emf.edit.provider.AdapterFactoryItemDelegator;
> import org.eclipse.emf.edit.provider.IItemPropertyDescriptor;
> import
> org.eclipse.emf.edit.provider.ItemPropertyDescriptor.PropertyValueWrapper;
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.events.ModifyEvent;
> import org.eclipse.swt.events.ModifyListener;
> import org.eclipse.swt.widgets.Composite;
> import org.eclipse.swt.widgets.Text;
>
> import vtxml.impl.DocImpl;
> import vtxml.presentation.VtxmlEditor;
>
> public class Snippet extends Composite {
>
> //just for snippeting purpose
> private DocImpl docImpl;
> private VtxmlEditor ourEditor=null;
>
> Snippet(Composite parent, int style) {
> super(parent, style);
>
> createTextField("type");
> createTextField("title");
> }
>
> private void createTextField(String featureName) {
> AdapterFactoryItemDelegator adapterFactoryItemDelegator = new
> AdapterFactoryItemDelegator(ourEditor.getAdapterFactory());
> final IItemPropertyDescriptor iIPd =
> adapterFactoryItemDelegator.getPropertyDescriptor(docImpl, featureName);
>
> Text myText = new Text(this, SWT.NONE);
> myText.setText((String)((PropertyValueWrapper)
> iIPd.getPropertyValue(docImpl)).getEditableValue(docImpl));
> myText.setEditable(true);
>
> myText.addModifyListener(
> new ModifyListener() {
> public void modifyText(ModifyEvent e) {
> iIPd.setPropertyValue(docImpl, ((Text)
> e.widget).getText());
> }
> }
> );
> }
>
>
>
> }
>
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
Powered by
FUDForum. Page generated in 0.03583 seconds