Skip to content

Method: createPresentationModel(Collection)

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:
28: import jakarta.annotation.Nonnull;
29: import java.time.format.DateTimeFormatter;
30: import java.time.format.FormatStyle;
31: import java.util.Collection;
32: import java.util.Locale;
33: import it.tidalwave.ui.core.role.Displayable;
34: import it.tidalwave.ui.core.role.PresentationModel;
35: import it.tidalwave.ui.core.role.PresentationModelAggregate;
36: import it.tidalwave.ui.core.role.Styleable;
37: import it.tidalwave.ui.example.model.FileEntity;
38: import it.tidalwave.ui.core.role.spi.SimpleCompositePresentable;
39: import it.tidalwave.dci.annotation.DciRole;
40: import lombok.extern.slf4j.Slf4j;
41: import static it.tidalwave.util.FunctionalCheckedExceptionWrappers.*;
42: import static it.tidalwave.util.LocalizedDateTimeFormatters.getDateTimeFormatterFor;
43: import static it.tidalwave.util.Parameters.r;
44:
45: /***************************************************************************************************************************************************************
46: *
47: * A {@link it.tidalwave.ui.core.role.Presentable} implementation for {@link FileEntity}, creates a {@link PresentationModel}
48: * for a tabular rendering with four columns: name, size, creationDate and latestModificationDate.
49: *
50: * @author Fabrizio Giudici
51: *
52: **************************************************************************************************************************************************************/
53: // TODO: add roles such as Removable, present only if permissions allow
54: // TODO: iconprovider
55: // START SNIPPET: all
56: @DciRole(datumType = FileEntity.class) @Slf4j
57: public class FileEntityPresentable extends SimpleCompositePresentable
58: {
59: private static final DateTimeFormatter FORMATTER = getDateTimeFormatterFor(FormatStyle.SHORT, Locale.getDefault());
60: private static final String RIGHT_ALIGNED = "right-aligned";
61:
62: @Nonnull
63: private final FileEntity owner;
64:
65: public FileEntityPresentable (@Nonnull final FileEntity owner)
66: {
67: super(owner);
68: this.owner = owner;
69: }
70:
71: @Override @Nonnull
72: public PresentationModel createPresentationModel (@Nonnull final Collection<Object> roles)
73: {
74: final var aggregate = PresentationModelAggregate.newInstance()
75: .withPmOf("name",
76: r(owner)) // owner is a Displayable itself
77: .withPmOf("size",
78: r(Displayable.of(_s(() -> "" + owner.getSize())),
79: Styleable.of(RIGHT_ALIGNED)))
80: .withPmOf("creationDate",
81: r(Displayable.of(_s(() -> FORMATTER.format(owner.getCreationDateTime()))),
82: Styleable.of(RIGHT_ALIGNED)))
83: .withPmOf("latestModificationDate",
84: r(Displayable.of(_s(() -> FORMATTER.format(owner.getLastModifiedDateTime()))),
85: Styleable.of(RIGHT_ALIGNED)));
86: return super.createPresentationModel(r(aggregate, roles));
87: }
88: }
89: // END SNIPPET: all