Skip to content

Commit

Permalink
#5 add direct links to each board in the listbox
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Delporte committed May 7, 2024
1 parent de83623 commit b8d31f8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
10 changes: 8 additions & 2 deletions src/main/java/com/pi4j/boardinfoservice/views/BaseLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,19 @@ public MenuItemComponent(String menuTitle, Component icon, Class<? extends Compo
this.view = view;
RouterLink link = new RouterLink();
// Use Lumo classnames for various styling
link.addClassNames(LumoUtility.Display.FLEX, LumoUtility.Gap.XSMALL, LumoUtility.Height.MEDIUM, LumoUtility.AlignItems.CENTER, LumoUtility.Padding.Horizontal.SMALL,
link.addClassNames(LumoUtility.Display.FLEX,
LumoUtility.Gap.XSMALL,
LumoUtility.Height.MEDIUM,
LumoUtility.AlignItems.CENTER,
LumoUtility.Padding.Horizontal.SMALL,
LumoUtility.TextColor.BODY);
link.setRoute(view);

Span text = new Span(menuTitle);
// Use Lumo classnames for various styling
text.addClassNames(LumoUtility.FontWeight.MEDIUM, LumoUtility.FontSize.MEDIUM, LumoUtility.Whitespace.NOWRAP);
text.addClassNames(LumoUtility.FontWeight.MEDIUM,
LumoUtility.FontSize.MEDIUM,
LumoUtility.Whitespace.NOWRAP);

if (icon != null) {
link.add(icon);
Expand Down
29 changes: 21 additions & 8 deletions src/main/java/com/pi4j/boardinfoservice/views/BoardInfoView.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.splitlayout.SplitLayout;
import com.vaadin.flow.data.renderer.ComponentRenderer;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.router.RouteAlias;
import com.vaadin.flow.router.*;
import com.vaadin.flow.theme.lumo.LumoUtility;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand All @@ -27,12 +26,13 @@
@PageTitle("Raspberry Pi Board Information")
@Route(value = "board-information", layout = BaseLayout.class)
@RouteAlias(value = "", layout = BaseLayout.class)
public class BoardInfoView extends VerticalLayout {
public class BoardInfoView extends VerticalLayout implements HasUrlParameter<String> {

private static final Logger logger = LogManager.getLogger(BoardInfoView.class);

private final VerticalLayout holder = new VerticalLayout();
private final ListBox<BoardModel> listBox = new ListBox<>();
private BoardModel selectedBoard;

public BoardInfoView() {
setSpacing(false);
Expand All @@ -41,11 +41,11 @@ public BoardInfoView() {
setJustifyContentMode(JustifyContentMode.START);

listBox.addValueChangeListener(e -> showBoard(e.getValue()));
listBox.setMinWidth(250, Unit.PIXELS);
listBox.setMinWidth(300, Unit.PIXELS);
listBox.setHeightFull();
listBox.setRenderer(new ComponentRenderer<>(board -> {
var lbl = new Span(board.getLabel());
lbl.setWidthFull();
var lbl = new Anchor("/board-information/" + board.getName(), board.getLabel());
lbl.addClassNames(LumoUtility.Gap.XSMALL, LumoUtility.TextColor.BODY);
return lbl;
}));

Expand All @@ -60,13 +60,26 @@ public BoardInfoView() {
add(split);
}

@Override
public void setParameter(BeforeEvent event, @OptionalParameter String parameter) {
selectedBoard = Arrays.stream(BoardModel.values())
.filter(bm -> bm.name().equalsIgnoreCase(parameter == null ? "" : parameter))
.findFirst()
.orElse(BoardModel.UNKNOWN);
}

@Override
public void onAttach(AttachEvent event) {
var listWithoutUnknown = Arrays.stream(BoardModel.values())
.filter(bm -> bm != BoardModel.UNKNOWN)
.sorted(Comparator.comparing(BoardModel::getLabel))
.toList();
UI.getCurrent().access(() -> listBox.setItems(listWithoutUnknown));
UI.getCurrent().access(() -> {
listBox.setItems(listWithoutUnknown);
if (selectedBoard != BoardModel.UNKNOWN) {
listBox.setValue(selectedBoard);
}
});
}

private void showBoard(BoardModel boardModel) {
Expand Down

0 comments on commit b8d31f8

Please sign in to comment.