Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » WindowBuilder » EMF DataBinding Converter(Need help getting Converter to work for EMF based model to Label)
EMF DataBinding Converter [message #1032602] Wed, 03 April 2013 07:40 Go to next message
Matthew Fitzgerald is currently offline Matthew FitzgeraldFriend
Messages: 13
Registered: July 2009
Junior Member
Having a really tough time finding much in the way of documentation or examples on the use of the WindowBuilder -> Edit Data Binding -> Model to Target Strategy Properties -> Converter configuration. Needing a nudge in the right direction....

I have an EMF model generated from ecore with an EFloat value that I want to bind to a org.eclipse.swt.widgets.Label. This is a uni-directional (Model to Label) only binding for my purposes. The EMF model getter returns a float primitive. I would like to convert and format the float to something like "0.000" along with some other boundary messages - hence the desire to create a custom Convert.

I would really like to get this working via what I understand to be the intended UpdateValueStrategy.setConverter scheme in WindowBuilder and not via a manual hack.

I have read the posts about not being able to use org.eclipse.core.databinding.conversion.Converter and NumberToString directly which is fine. But I am having trouble getting WindowBuilder to accept my converter class:

import org.eclipse.core.databinding.conversion.Converter;

public class FloatToStringConverter extends Converter {
  public FloatToStringConverter(){
    super(Float.class, String.class);
  }

  @Override
  public Object convert(Object fromObject) {
    String s;

    // cool conversion stuff here
		
    return s;
  }
}


WindowBuilder gives me an error of "Model converter Class does not exist." when I use the following settings in the WindowBuilder Edit Data Binding Dialog:
Target, UpdateValueStrategy: POLICY_NEVER
Model, UpdateValueStrategy: POLICY_CONVERT
Model, Converter: myFancyPackageName.FloatToStringConverter

I recognize that my Model provides a float and my Converter is Float, so I will take a look at that aspect. Not having much experience with WindowBuilder Converter, I am hoping that someone can point out some good documentation or examples - I have spent a fair amount of time on google with no joy.

THANKS IN ADVANCE for any help!!
Re: EMF DataBinding Converter [message #1032967 is a reply to message #1032602] Wed, 03 April 2013 17:41 Go to previous message
Matthew Fitzgerald is currently offline Matthew FitzgeraldFriend
Messages: 13
Registered: July 2009
Junior Member
RESOLVED

I figured out the error message and one other problem:

1. The EMF model was of primitive type float hence causing the "Model converter Class does not exist." error... which I now understand. Changing the model to EFloatObject (java.lang.Float) fixed that problem. org.eclipse.core.databinding.conversion.Converter must, of course, be used with 'tos' and 'froms' which are descendants of java.lang.Object.... it was a late night....

2. The second problem was the POLICY_CONVERT. This policy performs the conversion, but stops short of updating the value to the Label - the update is a separate request. Changing back to POLICY_UPDATE fixed this issue.

So, my converter class ends up being this:
import org.eclipse.core.databinding.conversion.Converter;

public class FloatToStringConverter extends Converter {
	public FloatToStringConverter(){
		super(Float.class, String.class);
	}

	@Override
	public Object convert(Object fromObject) {
		if (fromObject == null) return "";
		if (fromObject.getClass() != Float.class) return "";
		Float f = (Float)fromObject;
                String s;
                .... fancy conversion code ...              
		return s;
	}
}


And the WindowsBuilder generated initDataBindings are:
IObservableValue observeTextLblAvgReadElapsedTimeObserveWidget = WidgetProperties.text().observe(lblAvgReadElapsedTime);
IObservableValue commsStatisticsResponseTimeMsAvgObserveValue = EMFObservables.observeValue(commsStatistics,
    Literals.COMMS_STATISTICS__RESPONSE_TIME_MS_AVG);
UpdateValueStrategy strategy_4 = new UpdateValueStrategy();
strategy_4.setConverter(new FloatToStringConverter());
bindingContext.bindValue(observeTextLblAvgReadElapsedTimeObserveWidget, commsStatisticsResponseTimeMsAvgObserveValue,
    new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER), strategy_4);


Hope this helps anyone else with a similar issue.
Previous Topic:Console Output to WIndowBuilder GUI
Next Topic:How to generate event handlers in separate class? is this even possible?
Goto Forum:
  


Current Time: Tue Nov 25 21:27:57 GMT 2014

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

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