After adding databindings via the databindings editor to a composite someone else wrote I got compiler errors after saving it.
Turns out there was custom code inside initDataBindings() method. My question is if it should be possible to use anonymous converters inside initDataBinding method?
Enclosed is first the Runnable example app, then the result after editing in databindings editor and saving.
Runnable example:
- Code: Select all
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.conversion.Converter;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class AnonymousConverterTest {
private Label label;
private Text text;
protected Shell shell;
/**
* Launch the application
* @param args
*/
public static void main(String[] args) {
Display display = Display.getDefault();
Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
public void run() {
try {
AnonymousConverterTest window = new AnonymousConverterTest();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Open the window
*/
public void open() {
final Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
/**
* Create contents of the window
*/
protected void createContents() {
shell = new Shell();
shell.setSize(315, 270);
shell.setText("SWT Application");
text = new Text(shell, SWT.BORDER);
text.setBounds(30, 47, 80, 25);
final Label backwardsLabel = new Label(shell, SWT.NONE);
backwardsLabel.setText("Backwards");
backwardsLabel.setBounds(30, 93, 120, 13);
label = new Label(shell, SWT.NONE);
label.setText("Label");
label.setBounds(30, 112, 81, 13);
initDataBindings();
//
}
public DataBindingContext initDataBindings() {
IObservableValue labelTextObserveWidget = SWTObservables.observeText(label);
IObservableValue textTextObserveWidget = SWTObservables.observeText(text, SWT.Modify);
//
//
DataBindingContext bindingContext = new DataBindingContext();
//
UpdateValueStrategy strategy = new UpdateValueStrategy();
strategy.setConverter(new Converter(String.class, String.class){
public Object convert(Object arg) {
String str = (String) arg;
String converted = new String();
int len = str.length();
for( int i = len; --i >= 0; ) {
converted += str.charAt(i);
}
return converted;
}});
bindingContext.bindValue(labelTextObserveWidget, textTextObserveWidget, null, strategy);
//
return bindingContext;
}
}
Saved result after editing in databindings editor:
- Code: Select all
public DataBindingContext initDataBindings() {
IObservableValue labelTextObserveWidget = SWTObservables.observeText(label);
IObservableValue textTextObserveWidget = SWTObservables.observeText(text, SWT.Modify);
IObservableValue dummyLabelFontObserveWidget = SWTObservables.observeFont(label);
IObservableValue dummyBackwardsLabelFontObserveWidget = SWTObservables.observeFont(backwardsLabel);
//
//
DataBindingContext bindingContext = new DataBindingContext();
//
UpdateValueStrategy strategy = new UpdateValueStrategy();
strategy.setConverter(new AnonymousConverterTest.2());
bindingContext.bindValue(labelTextObserveWidget, textTextObserveWidget, null, strategy);
bindingContext.bindValue(dummyBackwardsLabelFontObserveWidget, dummyLabelFontObserveWidget, null, null);
//
return bindingContext;
}
Problem is easily found and fixed by extracting the anonymous class to outside, but to do a really easy conversion it feels like a bit too much overhead?
Best regards,
/Niklas Skeppstedt