public class BindTooltipAspectDefinition implements LinkkiAspectDefinition {
public static final String NAME = "tooltip";
private final TooltipType tooltipType;
private final String staticValue;
public BindTooltipAspectDefinition(TooltipType tooltipType, String staticValue) {
this.tooltipType = tooltipType;
this.staticValue = staticValue;
}
@Override
public Handler createUiUpdater(PropertyDispatcher propertyDispatcher, ComponentWrapper componentWrapper) {
Aspect<String> aspect = createAspect();
Consumer<String> setter = componentWrapper::setTooltip;
if (tooltipType == TooltipType.STATIC) {
setter.accept(propertyDispatcher.pull(aspect));
return Handler.NOP_HANDLER;
} else {
return () -> setter.accept(propertyDispatcher.pull(aspect));
}
}
public Aspect<String> createAspect() {
if (tooltipType == TooltipType.STATIC) {
return Aspect.of(NAME, staticValue);
} else {
return Aspect.of(NAME);
}
}
public static enum TooltipType {
STATIC,
/**
* Tooltip is bound to the property using the method get<PropertyName>Tooltip().
*/
DYNAMIC;
}
}
Extending linkki
Creating a New Aspect
In addition to built-in aspects, you can easily create your own. This guide walks you through the process of implementing a simple aspect that allows components to show a dynamically bound tooltip. In this example, you will build the aspect as a standalone annotation that can be used in addition to any BindingDefinition
.
A similar aspect is already included in linkki. It is only used as an example due to its simplicity. |
AspectDefinition
An aspect definition defines how an aspect is created. To define your own definition you have to implement LinkkiAspectDefinition
with the following two methods:
-
initModelUpdate(PropertyDispatcher, ComponentWrapper, Handler)
: Method that registers a listener to the wrapped UI component which react to changes in the UI. This method is only mandatory if the defined aspect needs to write into the model. -
createUiUpdater(PropertyDispatcher, ComponentWrapper)
: Creates aHandler
that is triggered when the UI has to be updated.
We want to define an aspect that can either have a static value or read a value from a get<Property>Tooltip()
method, depending on a TooltipType
enumeration:
-
initModelUpdate
is not needed in this example because the tooltip won’t change in the model upon UI change. -
In the method
createUiUpdater
, thepropertyDispatcher
is asked for the tooltip value which is then set in the component usingComponentWrapper#setTooltip
. ThepropertyDispatcher
needs an aspect to know which value it should retrieve, and how. This aspect is created in the methodcreateAspect()
. -
createAspect()
creates the aspect depending on the values given in the annotation.
Create a New Aspect Annotation
We first create a new annotation that later links the annotated component to our aspect definition:
@Retention(RetentionPolicy.RUNTIME)
@Target(value = { ElementType.METHOD })
public @interface BindTooltip {
TooltipType tooltipType() default TooltipType.STATIC;
String value() default "";
}
-
The annotation must have the retention policy
RetentionPolicy#RUNTIME
to be able to be discovered by linkki at runtime. -
The attribute
tooltipType
determines whether the tooltip is static text or is provided by a method dynamically. -
If the
tooltipType
isBindTooltipType#STATIC
, the attributevalue
allows the user to define the content of the tooltip.
Using the name value for an annotation’s attribute allows users to omit the attribute name if it is the only attribute: @BindTooltip("My tooltip") instead of @BindTooltip(value="My tooltip")
|
@LinkkiAspect Annotation
Lastly, BindTooltip
has to be annotated with @LinkkiAspect
to tell linkki how to create the aspect from the annotation. The value for that annotation is a class implementing AspectDefinitionCreator<BindTooltip>
that can be created as an inner class:
@LinkkiAspect(BindTooltipAspectDefintionCreator.class)
public @interface BindTooltip {
class BindTooltipAspectDefintionCreator implements AspectDefinitionCreator<BindTooltip> {
@Override
public LinkkiAspectDefinition create(BindTooltip annotation) {
return new BindTooltipAspectDefinition(annotation.tooltipType(), annotation.value());
}
}
Summary
Congratulations! You have implemented a new tooltip aspect that can be used with any other linkki UI annotation. With this aspect, you can add a static or dynamic tooltip to a UI element that is bound by linkki.