DefaultCellBinder.java

  1. /*
  2.  * *********************************************************************************************************************
  3.  *
  4.  * SteelBlue: DCI User Interfaces
  5.  * http://tidalwave.it/projects/steelblue
  6.  *
  7.  * Copyright (C) 2015 - 2023 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
  12.  * the License. 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
  17.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  18.  * specific language governing permissions and limitations under the License.
  19.  *
  20.  * *********************************************************************************************************************
  21.  *
  22.  * git clone https://bitbucket.org/tidalwave/steelblue-src
  23.  * git clone https://github.com/tidalwave-it/steelblue-src
  24.  *
  25.  * *********************************************************************************************************************
  26.  */
  27. package it.tidalwave.role.ui.javafx.impl.common;

  28. import javax.annotation.Nonnull;
  29. import javax.annotation.Nullable;
  30. import java.util.List;
  31. import java.util.concurrent.Executor;
  32. import java.util.stream.Collectors;
  33. import javafx.collections.ObservableList;
  34. import javafx.scene.control.Cell;
  35. import javafx.scene.control.ContextMenu;
  36. import javafx.scene.control.MenuItem;
  37. import javafx.scene.input.KeyCode;
  38. import it.tidalwave.ui.role.javafx.CustomGraphicProvider;
  39. import it.tidalwave.util.As;
  40. import it.tidalwave.util.annotation.VisibleForTesting;
  41. import it.tidalwave.role.ui.Displayable;
  42. import it.tidalwave.role.ui.UserAction;
  43. import it.tidalwave.role.ui.UserActionProvider;
  44. import lombok.RequiredArgsConstructor;
  45. import lombok.extern.slf4j.Slf4j;
  46. import static it.tidalwave.ui.role.javafx.CustomGraphicProvider._CustomGraphicProvider_;
  47. import static java.util.stream.Collectors.*;
  48. import static it.tidalwave.role.ui.Displayable._Displayable_;
  49. import static it.tidalwave.role.ui.Styleable._Styleable_;
  50. import static it.tidalwave.role.ui.UserActionProvider._UserActionProvider_;

  51. /***********************************************************************************************************************
  52.  *
  53.  * An implementation of {@link CellBinder} that extracts information from a {@link UserActionProvider}.
  54.  *
  55.  * @author  Fabrizio Giudici
  56.  *
  57.  **********************************************************************************************************************/
  58. @RequiredArgsConstructor @Slf4j
  59. public class DefaultCellBinder implements CellBinder
  60.   {
  61.     /** Roles to preload, so they are computed in the background thread. */
  62.     private static final List<Class<?>> PRELOADING_ROLE_TYPES = List.of(
  63.             _Displayable_, _UserActionProvider_, _Styleable_, _CustomGraphicProvider_);

  64.     private static final String ROLE_STYLE_PREFIX = "-rs-";

  65.     @Nonnull
  66.     private final Executor executor;

  67.     /*******************************************************************************************************************
  68.      *
  69.      * {@inheritDoc}
  70.      *
  71.      ******************************************************************************************************************/
  72.     @Override
  73.     public void bind (@Nonnull final Cell<?> cell, @Nullable final As item, final boolean empty)
  74.       {
  75.         log.trace("bind({}, {}, {})", cell, item, empty);
  76.         clearBindings(cell);

  77.         if (!empty && (item != null))
  78.           {
  79.             JavaFXWorker.run(executor,
  80.                              () -> new RoleBag(item, PRELOADING_ROLE_TYPES),
  81.                              roles -> bindAll(cell, roles));
  82.           }
  83.       }

  84.     /*******************************************************************************************************************
  85.      *
  86.      * Binds everything provided by the given {@link RoleBag} to the given {@link Cell}.
  87.      *
  88.      * @param     cell            the {@code Cell}
  89.      * @param     roles           the role bag
  90.      *
  91.      ******************************************************************************************************************/
  92.     private void bindAll (@Nonnull final Cell<?> cell, @Nonnull final RoleBag roles)
  93.       {
  94.         bindTextAndGraphic(cell, roles);
  95.         bindDefaultAction(cell, roles);
  96.         bindContextMenu(cell, roles);
  97.         bindStyles(cell.getStyleClass(), roles);
  98.       }

  99.     /*******************************************************************************************************************
  100.      *
  101.      * Binds the text and eventual custom {@link javafx.scene.Node} provided by the given {@link RoleBag} to the given
  102.      * {@link Cell}.
  103.      *
  104.      * @param     cell            the {@code Cell}
  105.      * @param     roles           the role bag
  106.      *
  107.      ******************************************************************************************************************/
  108.     private void bindTextAndGraphic (@Nonnull final Cell<?> cell, @Nonnull final RoleBag roles)
  109.       {
  110.         final var cgp = roles.get(_CustomGraphicProvider_);
  111.         cell.setGraphic(cgp.map(CustomGraphicProvider::getGraphic).orElse(null));
  112.         cell.setText(cgp.map(c -> "").orElse(roles.get(_Displayable_).map(Displayable::getDisplayName).orElse("")));
  113.       }

  114.     /*******************************************************************************************************************
  115.      *
  116.      * Binds the default {@link UserAction}s provided by the given {@link RoleBag} as the default action of the given
  117.      * {@link Cell} (activated by double click or key pressure).
  118.      *
  119.      * @param     cell            the {@code Cell}
  120.      * @param     roles           the role bag
  121.      *
  122.      ******************************************************************************************************************/
  123.     private void bindDefaultAction (@Nonnull final Cell<?> cell, @Nonnull final RoleBag roles)
  124.       {
  125.         roles.getDefaultUserAction().ifPresent(defaultAction ->
  126.           {
  127.             // FIXME: doesn't work - keyevents are probably handled by ListView
  128.             cell.setOnKeyPressed(event ->
  129.               {
  130.                 log.debug("onKeyPressed: {}", event);

  131.                 if (event.getCode().equals(KeyCode.SPACE))
  132.                   {
  133.                     executor.execute(defaultAction::actionPerformed);
  134.                   }
  135.               });

  136.             // FIXME: depends on mouse click, won't handle keyboard
  137.             cell.setOnMouseClicked(event ->
  138.               {
  139.                 if (event.getClickCount() == 2)
  140.                   {
  141.                     executor.execute(defaultAction::actionPerformed);
  142.                   }
  143.               });
  144.           });
  145.       }

  146.     /*******************************************************************************************************************
  147.      *
  148.      * Binds the {@link UserAction}s provided by the given {@link RoleBag} as items of the contextual menu of a
  149.      * {@link Cell}.
  150.      *
  151.      * @param     cell            the {@code Cell}
  152.      * @param     roles           the role bag
  153.      *
  154.      ******************************************************************************************************************/
  155.     private void bindContextMenu (@Nonnull final Cell<?> cell, @Nonnull final RoleBag roles)
  156.       {
  157.         final var menuItems = createMenuItems(roles);
  158.         cell.setContextMenu(menuItems.isEmpty() ? null : new ContextMenu(menuItems.toArray(new MenuItem[0])));
  159.       }

  160.     /*******************************************************************************************************************
  161.      *
  162.      * Adds all the styles provided by the given {@link RoleBag} to a {@link ObservableList} of styles.
  163.      *
  164.      * @param     styleClasses    the destination where to add styles
  165.      * @param     roles           the role bag
  166.      *
  167.      ******************************************************************************************************************/
  168.     @Nonnull
  169.     private void bindStyles (@Nonnull final ObservableList<String> styleClasses, @Nonnull final RoleBag roles)
  170.       {
  171.         final var styles = styleClasses.stream()
  172.                                        .filter(s -> !s.startsWith(ROLE_STYLE_PREFIX))
  173.                                        .collect(toList());
  174.         // FIXME: shouldn't reset them? In case of cell reuse, they get accumulated
  175.         styles.addAll(roles.getMany(_Styleable_)
  176.                            .stream()
  177.                            .flatMap(styleable -> styleable.getStyles().stream())
  178.                            .map(s -> ROLE_STYLE_PREFIX + s)
  179.                            .collect(toList()));
  180.         styleClasses.setAll(styles);
  181.       }

  182.     /*******************************************************************************************************************
  183.      *
  184.      * Create a list of {@link MenuItem}s for each action provided by the given {@link RoleBag}.
  185.      * Don't directly return a ContextMenu otherwise it will be untestable.
  186.      *
  187.      * @param     roles           the role bag
  188.      * @return                    the list of {@MenuItem}s
  189.      *
  190.      ******************************************************************************************************************/
  191.     @Nonnull
  192.     @VisibleForTesting public List<MenuItem> createMenuItems (@Nonnull final RoleBag roles)
  193.       {
  194.         return roles.getMany(_UserActionProvider_).stream()
  195.                     .flatMap(uap -> uap.getActions().stream())
  196.                     .map(this::createMenuItem)
  197.                     .collect(Collectors.toList());
  198.       }

  199.     /*******************************************************************************************************************
  200.      *
  201.      *
  202.      *
  203.      ******************************************************************************************************************/
  204.     private void clearBindings (@Nonnull final Cell<?> cell)
  205.       {
  206.         cell.setText("");
  207.         cell.setGraphic(null);
  208.         cell.setContextMenu(null);
  209.         cell.setOnKeyPressed(null);
  210.         cell.setOnMouseClicked(null);
  211.       }

  212.     /*******************************************************************************************************************
  213.      *
  214.      * Creates a {@link MenuItem} bound to the given action.
  215.      *
  216.      * @param     action          the action
  217.      * @return                    the bound {@code MenuItem}
  218.      *
  219.      ******************************************************************************************************************/
  220.     @Nonnull
  221.     private MenuItem createMenuItem (@Nonnull final UserAction action)
  222.       {
  223.         final var menuItem = new MenuItem(action.as(_Displayable_).getDisplayName());
  224.         menuItem.setOnAction(new EventHandlerUserActionAdapter(executor, action));
  225.         return menuItem;
  226.       }
  227.   }