Skip to content

Method: lambda$new$0()

1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * SteelBlue: DCI User Interfaces
5: * http://tidalwave.it/projects/steelblue
6: *
7: * Copyright (C) 2015 - 2025 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *************************************************************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12: * You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
17: * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18: *
19: * *************************************************************************************************************************************************************
20: *
21: * git clone https://bitbucket.org/tidalwave/steelblue-src
22: * git clone https://github.com/tidalwave-it/steelblue-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.ui.core.spi;
27:
28: import jakarta.annotation.Nonnull;
29: import java.util.Collection;
30: import java.util.List;
31: import java.util.function.Supplier;
32: import it.tidalwave.util.As;
33: import it.tidalwave.ui.core.ToolBarControl;
34: import it.tidalwave.ui.core.role.UserAction;
35: import it.tidalwave.ui.core.role.UserActionProvider;
36: import lombok.AccessLevel;
37: import lombok.RequiredArgsConstructor;
38: import lombok.experimental.Delegate;
39: import lombok.extern.slf4j.Slf4j;
40: import static java.util.Collections.emptyList;
41: import static java.util.stream.Collectors.*;
42: import static it.tidalwave.ui.core.role.UserActionProvider._UserActionProvider_;
43:
44: /***************************************************************************************************************************************************************
45: *
46: * A support implementation for {@link ToolBarControl}.
47: *
48: * @param <B> the concrete type of the binder
49: * @param <T> the concrete type of the toolbar
50: * @param <BT> the concrete type of the button
51: * @since 1.1-ALPHA-6
52: * @author Fabrizio Giudici
53: *
54: **************************************************************************************************************************************************************/
55: @RequiredArgsConstructor(access = AccessLevel.PROTECTED) @Slf4j @SuppressWarnings("this-escape")
56: public abstract class ToolBarControlSupport<B, T, BT> implements ToolBarControl<B, T>
57: {
58: @Delegate
59: private final As as = As.forObject(this);
60:
61: /** The default supplier of {@link UserAction}s, can be injected for testing. */
62: @Nonnull
63: protected final Supplier<Collection<? extends UserAction>> userActionsSupplier;
64:
65: /***********************************************************************************************************************************************************
66: * Default constructor.
67: **********************************************************************************************************************************************************/
68: protected ToolBarControlSupport ()
69: {
70: userActionsSupplier = () -> maybeAs(_UserActionProvider_).map(UserActionProvider::getActions).orElse(emptyList());
71: }
72:
73: /***********************************************************************************************************************************************************
74: * Populates the menu bar with menus.
75: * @param binder the binder
76: * @param toolBar the toolbar
77: **********************************************************************************************************************************************************/
78: public void populate (@Nonnull final B binder, @Nonnull final T toolBar)
79: {
80: final var actions = userActionsSupplier.get();
81: log.info("Toolbar user actions: {}", actions);
82: final var buttons = actions.stream()
83: .map(action -> createButton(binder, action))
84: .collect(toList());
85: addButtonsToToolBar(toolBar, buttons);
86: }
87:
88: /***********************************************************************************************************************************************************
89: * Creates a button bound to the given {@link UserAction}.
90: *
91: * @param binder the binder
92: * @param action the user action
93: * @return the button
94: **********************************************************************************************************************************************************/
95: @Nonnull
96: protected abstract BT createButton (@Nonnull B binder, @Nonnull UserAction action);
97:
98: /***********************************************************************************************************************************************************
99: * Adds buttons to the toolbar.
100: * @param toolBar the toolbar
101: * @param buttons the button
102: **********************************************************************************************************************************************************/
103: protected abstract void addButtonsToToolBar (@Nonnull T toolBar, @Nonnull List<BT> buttons);
104: }