JavaFXWorker.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.javafx.impl.common;

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

  48. /***************************************************************************************************************************************************************
  49.  *
  50.  * @author  Fabrizio Giudici
  51.  *
  52.  **************************************************************************************************************************************************************/
  53. @UtilityClass @Slf4j
  54. public class JavaFXWorker
  55.   {
  56.     private static final As.Type<SimpleComposite<PresentationModel>> _PresentationModel_Composite_ = As.type(Composite.class);

  57.     public static <T> void run (@Nonnull final Executor executor,  @Nonnull final Supplier<T> backgroundSupplier, @Nonnull final Consumer<T> javaFxFinalizer)
  58.       {
  59.         try
  60.           {
  61.             executor.execute(() ->
  62.               {
  63.                 final var value = backgroundSupplier.get();
  64.                 Platform.runLater(() -> javaFxFinalizer.accept(value));
  65.                 log.trace("JavaFX finalizer submitted");
  66.               });
  67.           }
  68.         catch (RejectedExecutionException e)
  69.           {
  70.             log.error("Background task failed: {}", e.getMessage());
  71.           }
  72.       }

  73.     @Nonnull
  74.     public static ObservableList<PresentationModel> childrenPm (@Nonnull final PresentationModel pm)
  75.       {
  76.         return childrenPm(pm, 0);
  77.       }

  78.     @Nonnull
  79.     public static ObservableList<PresentationModel> childrenPm (@Nonnull final PresentationModel pm, @Nonnegative final int depth)
  80.       {
  81.         final var indent = INDENT.substring(0, depth * 8);
  82.         final var composite = pm.maybeAs(_PresentationModel_Composite_);
  83.         composite.ifPresent(c -> Logging.logObject(indent, composite));
  84.         final var items = composite.map(c -> c.findChildren().results()).orElse(emptyList());
  85.         final var badItems = extractBadItems(items);

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

  94.         Logging.logObjects(indent, items);
  95.         return observableArrayList(items);
  96.       }

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