Ceres Data Binding

Skip to end of metadata
Go to start of metadata

Value Container

Lets assume we have a Java class which holds some parameters we want to be able to edit later, e.g. in a GUI for some image analysis tool:

public static class ClusterAlgoParams {
    int clusterCount;
    double threshold;
    // ...
}

This class shall serve as the data model in the MVC architecture of our GUI. Since our model is a POJO, it does not automatically have the ability to inform the controller (= the binding) if values change. The Ceres binding framework allows to "wrap" instances of such plain Java classes in a ValueContainer. A value container is basically a bucket of (name:String/value:Object) pairs associated with some meta data (represented by the ValueDescriptor class). A value containers also can fire the desired change event, if a a contained value changes.

ClusterAlgoParams params = new ClusterAlgoParams();
// ...
ValueContainer vc = ValueContainer.createObjectBacked(params);

Now that the parameters are wrapped by the value container, we can add meta data to each value in order to specify the units, a description, a Validator, a Converter or properties used to configure the bound GUI components:

ClusterAlgoParams params = new ClusterAlgoParams();
// ...
ValueDescriptor vd = vc.getValueDescriptor("clusterCount");
vd.setDisplayName("Number of clusters");
vd.setValueRange(new ValueRange(2, 256));
// ...

Swing Binding

Once we have the configured value container (model), we can create the bindings (controllers) for the Java Swing GUI components (views):

JTextField textField;
JSpinner spinner;
// ...
BindingContext ctx = new BindingContext(vc);
ctx.bind("clusterCount", textField);
ctx.bind("threshold", spinner);

Value Model

The values in the value container are represented by the ValueModel class. In the example above, all value models in the container are linked to the fields of the wrapped objects. Additionally, values can also be linked to entries in a Map<String, Object> or linked to nothing at all:

ValueContainer vc;
Map<String, Object> map;
// ...
vc.addModel(ValueModel.createMapEntryModel(map, "auxDataFile", File.class));
vc.addModel(ValueModel.createValueModel("debug", Boolean.TYPE));
//...

If neither a POJO nor a Map fits your needs, you can implement your own ValueAccessor and create a custom value model by using the ValueModel constructor directly:

ValueContainer vc;
Map<String, Object> map;
ValueDescriptor vd = new ValueDescriptor();
ValueAccessor va = new MyCustomValueAccessor(/*...*/);
// ...
vc.addModel(new ValueModel(vd, va));
//...
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.