Hi, what is the best solution to add a button to a component like the DateField ?
so, when i press the butotn i can do something with the component.
I created a custom field to do it, is the proper solution ?
Thanks
package com.ado2k.appframework.ui.customField;
import java.io.Serializable;
import com.ado2k.appframework.generator.filter.Compare.Operation;
import com.ado2k.appframework.ui.customField.GridFilterComponent.GridFilterComponentValue;
import com.vaadin.flow.component.AbstractField;
import com.vaadin.flow.component.HasValue;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.button.ButtonVariant;
import com.vaadin.flow.component.customfield.CustomField;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
public class GridFilterComponent extends CustomField<GridFilterComponentValue> {
/**
*
*/
private static final long serialVersionUID = 8587430677859310197L;
private AbstractField<?, ?> component = null;
private Button compareButton = new Button();
private Operation operation = Operation.EQUAL;
public GridFilterComponent(AbstractField<?, ?> component) {
super();
this.component = component;
compareButton.setText("=");
compareButton.addClickListener(event -> {
if (operation.equals(Operation.EQUAL)) {
compareButton.setText(">=");
operation = Operation.GREATER_OR_EQUAL;
} else if (operation.equals(Operation.GREATER_OR_EQUAL)) {
compareButton.setText("<=");
operation = Operation.LESS_OR_EQUAL;
} else if (operation.equals(Operation.LESS_OR_EQUAL)) {
compareButton.setText("=");
operation = Operation.EQUAL;
}
updateValue();
});
component.addValueChangeListener(event -> {
updateValue();
});
compareButton.addThemeVariants(ButtonVariant.LUMO_SMALL, ButtonVariant.LUMO_ICON);
HorizontalLayout hl = new HorizontalLayout(compareButton, component);
hl.expand(component);
hl.setWidth("100%");
hl.setSpacing(false);
hl.setMargin(false);
hl.setPadding(false);
add(hl);
}
@Override
protected GridFilterComponentValue generateModelValue() {
Object value = ((HasValue) component).getValue();
if(value instanceof String && ((String) value).trim().length() == 0) return null;
if(value == null || operation == null) return null;
GridFilterComponentValue gfcv = new GridFilterComponentValue(((HasValue) component).getValue(), operation);
return gfcv;
}
@Override
protected void setPresentationValue(GridFilterComponentValue newPresentationValue) {
if (newPresentationValue != null) {
if (newPresentationValue.getOperation().equals(Operation.EQUAL)) {
compareButton.setText(">=");
operation = Operation.GREATER_OR_EQUAL;
} else if (newPresentationValue.getOperation().equals(Operation.GREATER_OR_EQUAL)) {
compareButton.setText("<=");
operation = Operation.LESS_OR_EQUAL;
} else if (newPresentationValue.getOperation().equals(Operation.LESS_OR_EQUAL)) {
compareButton.setText("=");
operation = Operation.EQUAL;
}
if (newPresentationValue.getValue() != null) {
((HasValue) component).setValue(newPresentationValue.getValue());
}
}
}
public final class GridFilterComponentValue implements Serializable {
Object value = null;
Operation operationValue = null;
public GridFilterComponentValue(Object value, Operation operation) {
super();
this.value = value;
this.operationValue = operation;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public Operation getOperation() {
return operationValue;
}
public void setOperation(Operation operation) {
this.operationValue = operation;
}
@Override
public boolean equals(Object obj) {
if(obj == null) return false;
if(!getOperation().equals(((GridFilterComponentValue)obj).getOperation()) ||
!getValue().equals(((GridFilterComponentValue)obj).getValue())){
return false;
}
return true;
}
}
}