FileEntityPresentable.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.example.presentation.impl;

  27. import jakarta.annotation.Nonnull;
  28. import java.time.format.DateTimeFormatter;
  29. import java.time.format.FormatStyle;
  30. import java.util.Collection;
  31. import java.util.Locale;
  32. import it.tidalwave.ui.core.role.Displayable;
  33. import it.tidalwave.ui.core.role.PresentationModel;
  34. import it.tidalwave.ui.core.role.PresentationModelAggregate;
  35. import it.tidalwave.ui.core.role.Styleable;
  36. import it.tidalwave.ui.example.model.FileEntity;
  37. import it.tidalwave.ui.core.role.spi.SimpleCompositePresentable;
  38. import it.tidalwave.dci.annotation.DciRole;
  39. import lombok.extern.slf4j.Slf4j;
  40. import static it.tidalwave.util.FunctionalCheckedExceptionWrappers.*;
  41. import static it.tidalwave.util.LocalizedDateTimeFormatters.getDateTimeFormatterFor;
  42. import static it.tidalwave.util.Parameters.r;

  43. /***************************************************************************************************************************************************************
  44.  *
  45.  * A {@link it.tidalwave.ui.core.role.Presentable} implementation for {@link FileEntity}, creates a {@link PresentationModel}
  46.  * for a tabular rendering with four columns: name, size, creationDate and latestModificationDate.
  47.  *
  48.  * @author  Fabrizio Giudici
  49.  *
  50.  **************************************************************************************************************************************************************/
  51. // TODO: add roles such as Removable, present only if permissions allow
  52. // TODO: iconprovider
  53. // START SNIPPET: all
  54. @DciRole(datumType = FileEntity.class) @Slf4j
  55. public class FileEntityPresentable extends SimpleCompositePresentable
  56.   {
  57.     private static final DateTimeFormatter FORMATTER = getDateTimeFormatterFor(FormatStyle.SHORT, Locale.getDefault());

  58.     @Nonnull
  59.     private final FileEntity owner;

  60.     public FileEntityPresentable (@Nonnull final FileEntity owner)
  61.       {
  62.         super(owner);
  63.         this.owner = owner;
  64.       }

  65.     @Override @Nonnull
  66.     public PresentationModel createPresentationModel (@Nonnull final Collection<Object> roles)
  67.       {
  68.         final var aggregate = PresentationModelAggregate.newInstance()
  69.            .withPmOf("name",
  70.                      r(owner))  // owner is a Displayable itself
  71.            .withPmOf("size",
  72.                      r(Displayable.of(_s(() -> "" + owner.getSize())),
  73.                        Styleable.of("right-aligned")))
  74.            .withPmOf("creationDate",
  75.                      r(Displayable.of(_s(() -> FORMATTER.format(owner.getCreationDateTime()))),
  76.                        Styleable.of("right-aligned")))
  77.            .withPmOf("latestModificationDate",
  78.                      r(Displayable.of(_s(() -> FORMATTER.format(owner.getLastModifiedDateTime()))),
  79.                        Styleable.of("right-aligned")));
  80.         return super.createPresentationModel(r(aggregate, roles));
  81.       }
  82.   }
  83. // END SNIPPET: all