From f1b2eb3519b863d71b2b6844061dc8a19da25f6b Mon Sep 17 00:00:00 2001 From: ScarletRedMan Date: Thu, 16 Nov 2023 22:15:21 +0700 Subject: [PATCH] Added management buttons --- .../loadbalancer/web/component/NodeList.java | 74 ++++++++++++++++++- 1 file changed, 70 insertions(+), 4 deletions(-) diff --git a/LoadBalancerWeb/src/main/java/ru/dragonestia/loadbalancer/web/component/NodeList.java b/LoadBalancerWeb/src/main/java/ru/dragonestia/loadbalancer/web/component/NodeList.java index 61d7a6b..af47982 100644 --- a/LoadBalancerWeb/src/main/java/ru/dragonestia/loadbalancer/web/component/NodeList.java +++ b/LoadBalancerWeb/src/main/java/ru/dragonestia/loadbalancer/web/component/NodeList.java @@ -1,11 +1,19 @@ package ru.dragonestia.loadbalancer.web.component; +import com.vaadin.flow.component.button.Button; +import com.vaadin.flow.component.button.ButtonVariant; +import com.vaadin.flow.component.dialog.Dialog; import com.vaadin.flow.component.grid.Grid; import com.vaadin.flow.component.html.H2; +import com.vaadin.flow.component.html.Paragraph; import com.vaadin.flow.component.icon.Icon; import com.vaadin.flow.component.icon.VaadinIcon; +import com.vaadin.flow.component.notification.Notification; +import com.vaadin.flow.component.notification.NotificationVariant; +import com.vaadin.flow.component.orderedlayout.HorizontalLayout; import com.vaadin.flow.component.orderedlayout.VerticalLayout; import com.vaadin.flow.component.textfield.TextField; +import com.vaadin.flow.component.textfield.TextFieldVariant; import ru.dragonestia.loadbalancer.web.model.Node; import java.util.List; @@ -49,17 +57,75 @@ public class NodeList extends VerticalLayout { var grid = new Grid<>(Node.class, false); grid.addColumn(Node::identifier).setHeader("Identifier"); grid.addColumn(node -> node.method().getName()).setHeader("Mode"); + grid.addComponentColumn(this::createManageButtons).setHeader("Manage"); return grid; } + private HorizontalLayout createManageButtons(Node node) { + var layout = new HorizontalLayout(); + + { + var button = new Button("Details"); + button.addThemeVariants(ButtonVariant.LUMO_PRIMARY); + button.addClickListener(event -> clickDetailsButton(node)); + layout.add(button); + } + + { + var button = new Button("Remove"); + button.addThemeVariants(ButtonVariant.LUMO_PRIMARY, ButtonVariant.LUMO_ERROR); + button.addClickListener(event -> clickRemoveButton(node)); + layout.add(button); + } + + return layout; + } + + private void clickDetailsButton(Node node) { + // TODO + } + + private void clickRemoveButton(Node node) { + var dialog = new Dialog("Confirm node deletion"); + dialog.add(new Paragraph("Confirm that you want to delete node. Enter '" + node.identifier() + "' to field below and confirm.")); + + var inputField = new TextField(); + dialog.add(inputField); + + { // confirm + var button = new Button("Confirm"); + button.addThemeVariants(ButtonVariant.LUMO_PRIMARY, ButtonVariant.LUMO_ERROR); + button.addClickListener(event -> { + if (!node.identifier().equals(inputField.getValue())) { + Notification.show("Invalid input", 3000, Notification.Position.TOP_END) + .addThemeVariants(NotificationVariant.LUMO_ERROR); + return; + } + + removeNode(node); + Notification.show("Node '" + node.identifier() + "' was successfully removed!", 3000, Notification.Position.TOP_END) + .addThemeVariants(NotificationVariant.LUMO_SUCCESS); + dialog.close(); + }); + + dialog.getFooter().add(button); + } + + { // cancel + var button = new Button("Cancel", event -> dialog.close()); + button.addThemeVariants(ButtonVariant.LUMO_TERTIARY); + dialog.getFooter().add(button); + } + + dialog.open(); + } + public void update(List nodes) { cachedNodes = nodes; applySearch(searchField.getValue()); } - private void registerNode(Node node) { - // TODO: send request for register node and get all nodes - - update(List.of(node)); + private void removeNode(Node node) { + // TODO: send remove request and getting nodes list } }