diff --git a/control-panel/src/main/java/ru/dragonestia/picker/cp/component/NodeList.java b/control-panel/src/main/java/ru/dragonestia/picker/cp/component/NodeList.java index c0e928d..ac3fa8b 100644 --- a/control-panel/src/main/java/ru/dragonestia/picker/cp/component/NodeList.java +++ b/control-panel/src/main/java/ru/dragonestia/picker/cp/component/NodeList.java @@ -4,6 +4,7 @@ import com.vaadin.flow.component.Html; 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.ColumnTextAlign; import com.vaadin.flow.component.grid.Grid; import com.vaadin.flow.component.html.H2; import com.vaadin.flow.component.icon.Icon; @@ -13,28 +14,29 @@ import com.vaadin.flow.component.orderedlayout.VerticalLayout; import com.vaadin.flow.component.textfield.TextField; import com.vaadin.flow.data.value.ValueChangeMode; import lombok.Setter; +import ru.dragonestia.picker.api.repository.NodeRepository; import ru.dragonestia.picker.api.repository.response.type.RNode; import java.util.List; import java.util.function.Consumer; -public class NodeList extends VerticalLayout { +public class NodeList extends VerticalLayout implements RefreshableTable { + private final NodeRepository nodeRepository; private final Grid nodesGrid; private final TextField searchField; private List cachedNodes; @Setter private Consumer removeMethod; - public NodeList(List nodes) { + public NodeList(NodeRepository nodeRepository) { super(); - - cachedNodes = nodes; + this.nodeRepository = nodeRepository; add(new H2("Nodes")); add(searchField = createSearchField()); add(nodesGrid = createGrid()); - update(nodes); + refresh(); } private TextField createSearchField() { @@ -60,7 +62,8 @@ public class NodeList extends VerticalLayout { grid.addColumn(RNode::getId).setHeader("Identifier").setSortable(true); grid.addColumn(node -> node.getMode().getName()).setHeader("Mode").setSortable(true); - grid.addComponentColumn(this::createManageButtons).setFrozenToEnd(true); + grid.addComponentColumn(this::createManageButtons).setFrozenToEnd(true) + .setTextAlign(ColumnTextAlign.END).setHeader(createRefreshButton()); grid.setMultiSort(true, Grid.MultiSortPriority.APPEND); return grid; @@ -124,14 +127,15 @@ public class NodeList extends VerticalLayout { dialog.open(); } - public void update(List nodes) { - cachedNodes = nodes; - applySearch(searchField.getValue()); - } - private void removeNode(RNode node) { if (removeMethod != null) { removeMethod.accept(node.getId()); } } + + @Override + public void refresh() { + cachedNodes = nodeRepository.all(NodeRepository.ALL_DETAILS); + applySearch(searchField.getValue()); + } } diff --git a/control-panel/src/main/java/ru/dragonestia/picker/cp/component/RefreshableTable.java b/control-panel/src/main/java/ru/dragonestia/picker/cp/component/RefreshableTable.java new file mode 100644 index 0000000..dd151b7 --- /dev/null +++ b/control-panel/src/main/java/ru/dragonestia/picker/cp/component/RefreshableTable.java @@ -0,0 +1,26 @@ +package ru.dragonestia.picker.cp.component; + +import com.vaadin.flow.component.button.Button; +import com.vaadin.flow.component.button.ButtonVariant; +import com.vaadin.flow.component.icon.Icon; +import com.vaadin.flow.component.icon.VaadinIcon; +import com.vaadin.flow.component.notification.Notification; + +public interface RefreshableTable { + + default Button createRefreshButton() { + var button = new Button("Refresh", new Icon(VaadinIcon.REFRESH)); + button.addThemeVariants(ButtonVariant.LUMO_CONTRAST); + button.addClickListener(event -> { + refresh(); + + var notification = new Notification("Refreshed!"); + notification.setDuration(1000); + notification.setPosition(Notification.Position.BOTTOM_END); + notification.open(); + }); + return button; + } + + void refresh(); +} diff --git a/control-panel/src/main/java/ru/dragonestia/picker/cp/page/NodesPage.java b/control-panel/src/main/java/ru/dragonestia/picker/cp/page/NodesPage.java index 6b6bc27..3a03af5 100644 --- a/control-panel/src/main/java/ru/dragonestia/picker/cp/page/NodesPage.java +++ b/control-panel/src/main/java/ru/dragonestia/picker/cp/page/NodesPage.java @@ -33,7 +33,7 @@ public class NodesPage extends VerticalLayout { add(nodeList = createNodeListElement()); nodeList.setRemoveMethod(nodeIdentifier -> { nodeRepository.remove(nodeIdentifier); - nodeList.update(nodeRepository.all(NodeRepository.ALL_DETAILS)); + nodeList.refresh(); }); } @@ -45,12 +45,12 @@ public class NodesPage extends VerticalLayout { } catch (ApiException ex) { return new RegisterNode.Response(true, ex.getMessage()); } finally { - nodeList.update(nodeRepository.all(NodeRepository.ALL_DETAILS)); + nodeList.refresh(); } }); } protected NodeList createNodeListElement() { - return new NodeList(nodeRepository.all(NodeRepository.ALL_DETAILS)); + return new NodeList(nodeRepository); } }