ToolBarControlSupport.java

  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. import jakarta.annotation.Nonnull;
  28. import java.util.Collection;
  29. import java.util.List;
  30. import java.util.function.Supplier;
  31. import it.tidalwave.ui.core.ToolBarControl;
  32. import it.tidalwave.ui.core.role.UserAction;
  33. import it.tidalwave.ui.core.role.UserActionProvider;
  34. import it.tidalwave.util.As;
  35. import lombok.AccessLevel;
  36. import lombok.RequiredArgsConstructor;
  37. import lombok.experimental.Delegate;
  38. import lombok.extern.slf4j.Slf4j;
  39. import static it.tidalwave.ui.core.role.UserActionProvider._UserActionProvider_;
  40. import static java.util.Collections.emptyList;
  41. import static java.util.stream.Collectors.*;

  42. /***************************************************************************************************************************************************************
  43.  *
  44.  * A support implementation for {@link ToolBarControl}.
  45.  *
  46.  * @param   <B>               the concrete type of the binder
  47.  * @param   <T>               the concrete type of the toolbar
  48.  * @param   <BT>              the concrete type of the button
  49.  * @since   1.1-ALPHA-6
  50.  * @author  Fabrizio Giudici
  51.  *
  52.  **************************************************************************************************************************************************************/
  53. @RequiredArgsConstructor(access = AccessLevel.PROTECTED) @Slf4j @SuppressWarnings("this-escape")
  54. public abstract class ToolBarControlSupport<B, T, BT> implements ToolBarControl<B, T>
  55.   {
  56.     @Delegate
  57.     private final As as = As.forObject(this);

  58.     /** The default supplier of {@link UserAction}s, can be injected for testing. */
  59.     @Nonnull
  60.     protected final Supplier<Collection<? extends UserAction>> userActionsSupplier;

  61.     /***********************************************************************************************************************************************************
  62.      * Default constructor.
  63.      **********************************************************************************************************************************************************/
  64.     protected ToolBarControlSupport ()
  65.       {
  66.         userActionsSupplier = () -> maybeAs(_UserActionProvider_).map(UserActionProvider::getActions).orElse(emptyList());
  67.       }

  68.     /***********************************************************************************************************************************************************
  69.      * Populates the menu bar with menus.
  70.      * @param   binder    the binder
  71.      * @param   toolBar   the toolbar
  72.      **********************************************************************************************************************************************************/
  73.     public void populate (@Nonnull final B binder, @Nonnull final T toolBar)
  74.       {
  75.         final var actions = userActionsSupplier.get();
  76.         log.info("Toolbar user actions: {}", actions);
  77.         final var buttons = actions.stream()
  78.                                    .map(action -> createButton(binder, action))
  79.                                    .collect(toList());
  80.         addButtonsToToolBar(toolBar, buttons);
  81.       }

  82.     /***********************************************************************************************************************************************************
  83.      * Creates a button bound to the given {@link UserAction}.
  84.      *
  85.      * @param   binder    the binder
  86.      * @param   action    the user action
  87.      * @return            the button
  88.      **********************************************************************************************************************************************************/
  89.     @Nonnull
  90.     protected abstract BT createButton (@Nonnull B binder, @Nonnull UserAction action);

  91.     /***********************************************************************************************************************************************************
  92.      * Adds buttons to the toolbar.
  93.      * @param   toolBar   the toolbar
  94.      * @param   buttons   the button
  95.      **********************************************************************************************************************************************************/
  96.     protected abstract void addButtonsToToolBar (@Nonnull T toolBar, @Nonnull List<BT> buttons);
  97.   }