sâmbătă, 4 iulie 2015

OmniFaces 2.1 - ReadOnlyValueExpression that can be set when the "real" value is not yet available

This post presents the features brought by OmniFaces 2.1 for the ReadOnlyValueExpression artifact. If you are not familiar with ReadOnlyValueExpression then please read this post before, "OmniFaces2.0 - ReadOnlyValueExpression that can be set when the "real"value is not yet available".

In OmniFaces 2.0 we can create a ReadOnlyValueExpression via a callback which takes one argument and returns one value (the object is the "real" needed value of this ValueExpression). Starting with OmniFaces 2.1, the Callback.Returning was replaced with the serializable version of it, Callback.SerializableReturning. By the other hand, the Callback.ReturningWithArgument is still there.

So, instead of writing:

ReadOnlyValueExpression readOnlyValueExpression = new 
     ReadOnlyValueExpression(type_of_object, new Returning<Object>() {
 @Override
 public Object invoke() {
  return object;
 }
});

Now, we write:

ReadOnlyValueExpression readOnlyValueExpression = new 
     ReadOnlyValueExpression(type_of_object, new SerializableReturning<Object>() {

 private static final long serialVersionUID = 1L;

 @Override
 public Object invoke() {
  return object;
 }
});

Moreover, note that in OmniFaces 2.0, the Callback can be "attached" only via ReadOnlyValueExpression constructors, while in OmniFaces 2.1 it can be "attached" via constructors, or later, via setCallbackReturning() and setCallbackWithArgument() methods. This adds more flexibility, since we are not restricted to "attach" the Callback when we instantiate the ReadOnlyValueExpression, and we can obtain the functional Callback interface that will be called when the value expression is resolved, via getCallbackReturning() and getCallbackWithArgument(). Below you can see these methods:

·         OmniFaces 2.1 set/getCallbackReturning():

·         OmniFaces 2.1 set/getCallbackWithArgument():


So, in OmniFaces 2.1 we can write the functional Callback interface, as below:

public static class MyCallbakImpl implements SerializableReturning<Object> {

 private static final long serialVersionUID = 1L;

 public MyCallbakImpl () { // empty constructor, not mandatory!
  // NOOP
 }

 // more constructors
 ...

 @Override
 public Object invoke() {
  ...
 }
}

And, whenever we need, we can create the ReadOnlyValueExpression without "attaching" the above functional now, MyCallbakImpl:

// this is just an example, you don't have to pass the UIComponent type, rather your expected type
ReadOnlyValueExpression readOnlyValueExpression = new ReadOnlyValueExpression(UIComponent.class);

Later, at the proper moment, we use the OmniFaces 2.1 setCallbackReturning() to "attach" the readOnlyValueExpression to MyCallbakImpl:

readOnlyValueExpression.setCallbackReturning(new MyCallbakImpl());

Is pretty obvious how to use the getCallbackReturning().

Niciun comentariu:

Trimiteți un comentariu