I’m binding the text value of a label to my backend model using the following code (some details omitted):
Model model = entry.getModel();
Control nameControl = formToolkit.createLabel(labelPanel, null);
IObservableValue modelValue = BeanProperties.value(model.getClass(), Model.PROPERTY_NAME).observe(model);
IObservableValue widgetValue = SWTObservables.observeText(nameControl);
DataBindingContext context = new DataBindingContext();
UpdateValueStrategy widgetToModel = new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER);
UpdateValueStrategy modelToWidget = new UpdateValueStrategy(UpdateValueStrategy.POLICY_UPDATE);
context.bindValue(widgetValue, modelValue, widgetToModel, modelToWidget);
When the model is changed, the text of the label updates appropriately. However the label is not resized. So when the model changes to a longer name, the new name is cut off. When it changes to a shorter name, the label takes up more space than it should.
I thought about adding a property change listener (either for the model name or the label text) that would call layout on the label’s parent whenever the name is changed, but wouldn’t that defeat the purpose of data binding?
Well you could bind the size of your control with
to the text of the control (the observable you already have) and provide a IConverter in your UpdateStrategy that converts from text to size (so every time the text changes you can calculate the text and then set the size of the control).
Make sure that the size of the label is recognized by the parents layout-manager