JavaFXWorker.java

  1. /*
  2.  * #%L
  3.  * *********************************************************************************************************************
  4.  *
  5.  * SteelBlue
  6.  * http://steelblue.tidalwave.it - git clone git@bitbucket.org:tidalwave/steelblue-src.git
  7.  * %%
  8.  * Copyright (C) 2015 - 2021 Tidalwave s.a.s. (http://tidalwave.it)
  9.  * %%
  10.  *
  11.  * *********************************************************************************************************************
  12.  *
  13.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  14.  * the License. You may obtain a copy of the License at
  15.  *
  16.  *     http://www.apache.org/licenses/LICENSE-2.0
  17.  *
  18.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  19.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  20.  * specific language governing permissions and limitations under the License.
  21.  *
  22.  * *********************************************************************************************************************
  23.  *
  24.  *
  25.  *
  26.  * *********************************************************************************************************************
  27.  * #L%
  28.  */
  29. package it.tidalwave.role.ui.javafx.impl.common;

  30. import javax.annotation.Nonnegative;
  31. import javax.annotation.Nonnull;
  32. import java.util.ArrayList;
  33. import java.util.List;
  34. import java.util.Optional;
  35. import java.util.concurrent.Executor;
  36. import java.util.concurrent.RejectedExecutionException;
  37. import java.util.function.Consumer;
  38. import java.util.function.Supplier;
  39. import javafx.collections.ObservableList;
  40. import javafx.application.Platform;
  41. import it.tidalwave.util.annotation.VisibleForTesting;
  42. import it.tidalwave.role.SimpleComposite;
  43. import it.tidalwave.role.ui.PresentationModel;
  44. import it.tidalwave.role.ui.javafx.impl.util.Logging;
  45. import lombok.experimental.UtilityClass;
  46. import lombok.extern.slf4j.Slf4j;
  47. import static java.util.Collections.emptyList;
  48. import static javafx.collections.FXCollections.*;
  49. import static it.tidalwave.role.SimpleComposite._SimpleComposite_;
  50. import static it.tidalwave.role.ui.javafx.impl.util.Logging.INDENT;

  51. /***********************************************************************************************************************
  52.  *
  53.  * @author  Fabrizio Giudici
  54.  *
  55.  **********************************************************************************************************************/
  56. @UtilityClass @Slf4j
  57. public class JavaFXWorker
  58.   {
  59.     public static <T> void run (@Nonnull final Executor executor,
  60.                                 @Nonnull Supplier<T> backgroundSupplier,
  61.                                 @Nonnull Consumer<T> javaFxFinalizer)
  62.       {
  63.         try
  64.           {
  65.             executor.execute(() ->
  66.               {
  67.                 final T value = backgroundSupplier.get();
  68.                 Platform.runLater(() -> javaFxFinalizer.accept(value));
  69.               });
  70.           }
  71.         catch (RejectedExecutionException e)
  72.           {
  73.             log.error("Background task failed: {}", e.getMessage());
  74.           }
  75.       }

  76.     @Nonnull
  77.     public static ObservableList<PresentationModel> childrenPm (@Nonnull final PresentationModel pm)
  78.       {
  79.         return childrenPm(pm, 0);
  80.       }

  81.     @Nonnull
  82.     public static ObservableList<PresentationModel> childrenPm (@Nonnull final PresentationModel pm,
  83.                                                                 @Nonnegative int depth)
  84.       {
  85.         final String indent = INDENT.substring(0, depth * 8);
  86.         final Optional<SimpleComposite> composite = pm.maybeAs(_SimpleComposite_);
  87.         composite.ifPresent(c -> Logging.logObject(indent, composite));
  88.         final List<PresentationModel> items = composite.map(c -> c.findChildren().results()).orElse(emptyList());
  89.         final List<Object> badItems = extractBadItems(items);

  90.         if (!badItems.isEmpty()) // defensive
  91.           {
  92.             log.error("Child object are not PresentationModel: (only 10 are shown)");
  93.             log.error("This happens when the PresentationModel doesn't have its own Composite role that decorates the" +
  94.                       " owner entity Composite - see SimpleCompositePresentable for instance.");
  95.             badItems.stream().limit(10).forEach(item -> log.error("    {}", item));
  96.             return emptyObservableList();
  97.           }

  98.         Logging.logObjects(indent, items);
  99.         return observableArrayList(items);
  100.       }

  101.     @Nonnull
  102.     @VisibleForTesting static List<Object> extractBadItems (@Nonnull final List<PresentationModel> items)
  103.       {
  104.         final List<Object> badItems = new ArrayList<>(items);
  105.         badItems.removeIf(item -> item instanceof PresentationModel);
  106.         return badItems;
  107. //        return items.stream()
  108. //                    .map(item -> (Object)item)
  109. //                    .filter(item -> !(item instanceof PresentationModel))
  110. //                    .peek(item -> log.error(">>>> {}", item))
  111. //                    .collect(toList());
  112.       }
  113.   }