fi.evolver.ai.vaadin.component.form.PromptMessageInput Maven / Gradle / Ivy
package fi.evolver.ai.vaadin.component.form;
import org.vaadin.lineawesome.LineAwesomeIcon;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.select.Select;
import com.vaadin.flow.component.textfield.TextArea;
import com.vaadin.flow.theme.lumo.LumoUtility;
import fi.evolver.ai.spring.chat.prompt.Message;
import fi.evolver.ai.vaadin.entity.HasValueGetterSetter;
public class PromptMessageInput extends VerticalLayout implements HasValueGetterSetter {
private final Select messageRoleInput = new Select();
private final TextArea messageContentInput = new TextArea();
private Runnable onRemove;
public PromptMessageInput() {
setPadding(false);
setSpacing(false);
setWidthFull();
HorizontalLayout row = new HorizontalLayout();
row.setWidthFull();
row.addClassNames(LumoUtility.JustifyContent.BETWEEN, LumoUtility.AlignItems.END);
add(row);
messageRoleInput.setLabel(getTranslation("component.form.promptMessageInput.role"));
messageRoleInput.setItems(Message.Role.values());
messageRoleInput.setItemLabelGenerator(Message.Role::getCode);
messageRoleInput.setWidth("80%");
row.add(messageRoleInput);
Button removeBtn = new Button(LineAwesomeIcon.TRASH_ALT.create());
removeBtn.addClickListener(e -> {
if (onRemove != null)
onRemove.run();
});
row.add(removeBtn);
messageContentInput.setLabel(getTranslation("component.form.promptMessageInput.message"));
messageContentInput.setWidthFull();
add(messageContentInput);
}
public PromptMessageInput(Message message) {
this();
setValue(message);
}
public void addRemoveHandler(Runnable onRemove) {
this.onRemove = onRemove;
}
@Override
public Message getValue() {
return new Message(messageRoleInput.getValue(), messageContentInput.getValue());
}
@Override
public void setValue(Message value) {
messageRoleInput.setValue(value.getRole());
messageContentInput.setValue(value.getContent());
}
}