io.imunity.vaadin.elements.grid.SingleActionHandler Maven / Gradle / Ivy
/*
* Copyright (c) 2021 Bixbit - Krzysztof Benedyczak. All rights reserved.
* See LICENCE.txt file for licensing information.
*/
package io.imunity.vaadin.elements.grid;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import com.vaadin.flow.component.icon.VaadinIcon;
public class SingleActionHandler
{
protected static final Consumer>[] EMPTY = new Consumer[0];
private boolean needsTarget = true;
private boolean multiTarget = false;
private boolean hideIfInactive = false;
private Predicate disabledPredicate;
private Predicate> disabledCompositePredicate;
private Consumer> actionHandler;
private String caption;
private VaadinIcon icon;
private boolean forceDisabled = false;
private SingleActionHandler()
{
disabledPredicate = a -> false;
disabledCompositePredicate = a -> false;
}
public static Builder builder()
{
return new Builder<>();
}
public static Builder builder(Class clazz)
{
return new Builder<>();
}
/**
* Doesn't require target, sets icon and caption.
* @param msg
* @param clazz
* @return
*/
public static Builder builder4Add(Function msg, Class clazz)
{
return new Builder()
.withCaption(msg.apply("add"))
.withIcon(VaadinIcon.PLUS_CIRCLE_O)
.dontRequireTarget();
}
/**
* Doesn't require target, sets label and icon
* @param msg
* @param clazz
* @return
*/
public static Builder builder4Refresh(Function msg, Class clazz)
{
return new Builder()
.withCaption(msg.apply("refresh"))
.withIcon(VaadinIcon.REFRESH)
.dontRequireTarget();
}
/**
* Multitarget, sets icon and caption
* @param msg
* @param clazz
* @return
*/
public static Builder builder4Delete(Function msg, Class clazz)
{
return new Builder()
.withCaption(msg.apply("remove"))
.withIcon(VaadinIcon.TRASH)
.multiTarget();
}
/**
* Sets icon and caption
* @param msg
* @param clazz
* @return
*/
public static Builder builder4Edit(Function msg, Class clazz)
{
return new Builder()
.withCaption(msg.apply("edit"))
.withIcon(VaadinIcon.EDIT);
}
/**
* Sets icon and caption
* @param msg
* @param clazz
* @return
*/
public static Builder builder4ShowDetails(Function msg, Class clazz)
{
return new Builder()
.withCaption(msg.apply("showDetails"))
.withIcon(VaadinIcon.INFO);
}
/**
* Sets icon and caption
* @param msg
* @param clazz
* @return
*/
public static Builder builder4Copy(Function msg, Class clazz)
{
return new Builder()
.withCaption(msg.apply("copy"))
.withIcon(VaadinIcon.COPY_O);
}
public static class Builder
{
private final SingleActionHandler obj;
public Builder()
{
this.obj = new SingleActionHandler<>();
}
public Builder withCaption(String caption)
{
this.obj.caption = caption;
return this;
}
public Builder withIcon(VaadinIcon icon)
{
this.obj.icon = icon;
return this;
}
public Builder withHandler(Consumer> actionHandler)
{
this.obj.actionHandler = actionHandler;
return this;
}
public Builder dontRequireTarget()
{
this.obj.needsTarget = false;
return this;
}
public Builder multiTarget()
{
this.obj.multiTarget = true;
return this;
}
public Builder hideIfInactive()
{
this.obj.hideIfInactive = true;
return this;
}
public Builder withDisabledPredicate(Predicate filter)
{
this.obj.disabledPredicate = filter;
return this;
}
public Builder withDisabledCompositePredicate(Predicate> filter)
{
this.obj.disabledCompositePredicate = filter;
return this;
}
public SingleActionHandler build()
{
return obj;
}
}
public boolean isNeedsTarget()
{
return needsTarget;
}
public boolean isMultiTarget()
{
return multiTarget;
}
public boolean isHideIfInactive()
{
return hideIfInactive;
}
public String getCaption()
{
return caption;
}
public VaadinIcon getIcon()
{
return icon;
}
public boolean isEnabled(Set selection)
{
if (!multiTarget && needsTarget && selection.size() > 1)
return false;
if (needsTarget && selection.isEmpty())
return false;
if (selection.stream().anyMatch(disabledPredicate))
return false;
if (disabledCompositePredicate.test(selection))
return false;
if (forceDisabled)
return false;
return true;
}
public boolean isVisible(Set selection)
{
return hideIfInactive ? isEnabled(selection) : true;
}
public void handle(Set selection)
{
actionHandler.accept(selection);
}
public void setDisabled(boolean disabled)
{
this.forceDisabled = disabled;
}
public void setHideIfInactive(boolean hideIfInactive)
{
this.hideIfInactive = hideIfInactive;
}
}