PresentationModelObservable.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.Nonnull;
  31. import java.util.Optional;
  32. import java.util.function.Supplier;
  33. import it.tidalwave.role.Aggregate;
  34. import it.tidalwave.role.ui.PresentationModel;
  35. import javafx.beans.value.ObservableValueBase;
  36. import javafx.scene.control.TableColumn;
  37. import javafx.scene.control.TreeTableColumn;
  38. import lombok.RequiredArgsConstructor;

  39. /***********************************************************************************************************************
  40.  *
  41.  * @author  Fabrizio Giudici
  42.  *
  43.  **********************************************************************************************************************/
  44. @RequiredArgsConstructor
  45. public final class PresentationModelObservable extends ObservableValueBase
  46.   {
  47.     @Nonnull
  48.     private final Supplier<PresentationModel> rowPresentationModelSupplier;

  49.     @Nonnull
  50.     private final Supplier<String> columnKeySupplier;

  51.     @Override @Nonnull
  52.     public final PresentationModel getValue()
  53.       {
  54.         final PresentationModel rowPm = rowPresentationModelSupplier.get();
  55.         final Optional<Aggregate<PresentationModel>> aggregate =
  56.                 (Optional<Aggregate<PresentationModel>>)(Object)rowPm.maybeAs(Aggregate.class);
  57.         // FIXME: uses the column header names, should be an internal id instead
  58.         return aggregate.flatMap(a -> a.getByName(columnKeySupplier.get()))
  59.                         .map(columnPm -> PresentationModelAsDelegateDecorator.decorating(columnPm, rowPm))
  60.                         .orElse(PresentationModel.empty());
  61.       }

  62.     @Nonnull
  63.     public static PresentationModelObservable of (
  64.             @Nonnull final TableColumn.CellDataFeatures<PresentationModel, PresentationModel> cell)
  65.       {
  66.         return new PresentationModelObservable(
  67.                 () -> cell.getValue(),
  68.                 () -> cell.getTableColumn().getText());
  69.       }

  70.     @Nonnull
  71.     public static PresentationModelObservable of (
  72.             @Nonnull final TreeTableColumn.CellDataFeatures<PresentationModel, PresentationModel> cell)
  73.       {
  74.         return new PresentationModelObservable(
  75.                 () -> cell.getValue().getValue(),
  76.                 () -> cell.getTreeTableColumn().getText());
  77.       }
  78.   }