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.core.role.spi.SimpleCompositePresentable;
  37. import it.tidalwave.ui.example.model.FileEntity;
  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.     private static final String RIGHT_ALIGNED = "right-aligned";

  59.     @Nonnull
  60.     private final FileEntity owner;

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

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