Content of file DefaultMainScreenPresentationControl.java.html

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>DefaultMainScreenPresentationControl.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">SteelBlue - Examples - Large</a> &gt; <a href="../index.html" class="el_bundle">it-tidalwave-steelblue-example-backend</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.role.ui.javafx.example.large.mainscreen.impl</a> &gt; <span class="el_source">DefaultMainScreenPresentationControl.java</span></div><h1>DefaultMainScreenPresentationControl.java</h1><pre class="source lang-java linenums">/*
 * *********************************************************************************************************************
 *
 * SteelBlue: DCI User Interfaces
 * http://tidalwave.it/projects/steelblue
 *
 * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
 *
 * *********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an &quot;AS IS&quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * *********************************************************************************************************************
 *
 * git clone https://bitbucket.org/tidalwave/steelblue-src
 * git clone https://github.com/tidalwave-it/steelblue-src
 *
 * *********************************************************************************************************************
 */
package it.tidalwave.role.ui.javafx.example.large.mainscreen.impl;

import javax.annotation.Nonnull;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.util.Collection;
import java.nio.file.Path;
import java.nio.file.Paths;
import it.tidalwave.role.Aggregate;
import it.tidalwave.role.ui.BoundProperty;
import it.tidalwave.role.ui.Displayable;
import it.tidalwave.role.ui.PresentationModel;
import it.tidalwave.role.ui.PresentationModelAggregate;
import it.tidalwave.role.ui.Selectable;
import it.tidalwave.role.ui.UserAction;
import it.tidalwave.role.ui.UserActionProvider;
import it.tidalwave.role.ui.Visible;
import it.tidalwave.role.ui.javafx.example.large.data.Dao;
import it.tidalwave.role.ui.javafx.example.large.data.SimpleEntity;
import it.tidalwave.role.ui.javafx.example.large.data.SimpleDciEntity;
import it.tidalwave.role.ui.javafx.example.large.data.impl.FileEntity;
import it.tidalwave.role.ui.javafx.example.large.mainscreen.MainScreenPresentation;
import it.tidalwave.role.ui.javafx.example.large.mainscreen.MainScreenPresentation.Bindings;
import it.tidalwave.role.ui.javafx.example.large.mainscreen.MainScreenPresentationControl;
import static it.tidalwave.role.ui.Presentable._Presentable_;
import static it.tidalwave.util.Parameters.r;
import static it.tidalwave.util.ui.UserNotificationWithFeedback.*;
import static it.tidalwave.role.ui.spi.PresentationModelCollectors.toCompositePresentationModel;

/***********************************************************************************************************************
 *
 * @stereotype  Control
 *
 * @author  Fabrizio Giudici (Fabrizio.Giudici@tidalwave.it)
 *
 **********************************************************************************************************************/
<span class="nc" id="L63">public class DefaultMainScreenPresentationControl implements MainScreenPresentationControl</span>
  {
<span class="nc" id="L65">    private static final Path USER_HOME = Paths.get(System.getProperty(&quot;user.home&quot;));</span>

    // The presentation is injected by the infrastructure in form of its interface.
    @Inject
    private MainScreenPresentation presentation;

    // As usual, also other required objects can be injected.
    @Inject
    private Dao dao;

    // For each button on the presentation that can do something, a UserAction is provided.
<span class="nc" id="L76">    private final UserAction buttonAction = UserAction.of(this::onButtonPressed,</span>
<span class="nc" id="L77">                                                          Displayable.of(&quot;Press me&quot;));</span>

<span class="nc" id="L79">    private final UserAction actionDialogOk = UserAction.of(this::onButtonDialogOkPressed,</span>
<span class="nc" id="L80">                                                            Displayable.of(&quot;Dialog with ok&quot;));</span>

<span class="nc" id="L82">    private final UserAction actionDialogCancelOk = UserAction.of(this::onButtonDialogOkCancelPressed,</span>
<span class="nc" id="L83">                                                                  Displayable.of(&quot;Dialog with ok/cancel&quot;));</span>

<span class="nc" id="L85">    private final UserAction actionPickFile = UserAction.of(this::onButtonPickFilePressed,</span>
<span class="nc" id="L86">                                                            Displayable.of(&quot;Pick file&quot;));</span>

<span class="nc" id="L88">    private final UserAction actionPickDirectory = UserAction.of(this::onButtonPickDirectoryPressed,</span>
<span class="nc" id="L89">                                                                 Displayable.of(&quot;Pick directory&quot;));</span>

<span class="nc" id="L91">    private final Bindings bindings = Bindings.builder()</span>
<span class="nc" id="L92">                                              .buttonAction(buttonAction)</span>
<span class="nc" id="L93">                                              .actionDialogOk(actionDialogOk)</span>
<span class="nc" id="L94">                                              .actionDialogCancelOk(actionDialogCancelOk)</span>
<span class="nc" id="L95">                                              .actionPickFile(actionPickFile)</span>
<span class="nc" id="L96">                                              .actionPickDirectory(actionPickDirectory)</span>
<span class="nc" id="L97">                                              .build();</span>

    // Then there can be a set of variables that represent the internal state of the control.
<span class="nc" id="L100">    private int status = 1;</span>

    /*******************************************************************************************************************
     *
     * At {@link PostConstruct} time the control just performs the binding to the presentation.
     *
     ******************************************************************************************************************/
    @PostConstruct
    private void initialize()
      {
<span class="nc" id="L110">        presentation.bind(bindings);</span>
<span class="nc" id="L111">      }</span>

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     * This method demonstrates the typical idiom for populating data:
     *
     * 1. A dao is called to provide raw data - let's say in form of collections;
     * 2. Objects in the collection are transformed into PresentationModels.
     * 3. The PresentationModels are then passed to the presentation.
     *
     ******************************************************************************************************************/
    @Override
    public void start()
      {
<span class="nc" id="L127">        presentation.showUp();</span>
<span class="nc" id="L128">        final Collection&lt;SimpleEntity&gt; entities1 = dao.getSimpleEntities();</span>
<span class="nc" id="L129">        final Collection&lt;SimpleDciEntity&gt; entities2 = dao.getDciEntities();</span>
<span class="nc" id="L130">        final Collection&lt;FileEntity&gt; files = dao.getFiles();</span>
<span class="nc" id="L131">        final PresentationModel pm1 = entities1.stream().map(this::pmFor)</span>
<span class="nc" id="L132">                                                        .collect(toCompositePresentationModel());</span>
<span class="nc" id="L133">        final PresentationModel pm2 = entities2.stream().map(this::pmFor)</span>
<span class="nc" id="L134">                                                        .collect(toCompositePresentationModel());</span>
<span class="nc" id="L135">        final PresentationModel pm3 = files.stream().map(item -&gt; item.as(_Presentable_).createPresentationModel())</span>
<span class="nc" id="L136">                                               .collect(toCompositePresentationModel(r(Visible.INVISIBLE)));</span>
<span class="nc" id="L137">        presentation.populate(pm1, pm2, pm3);</span>
<span class="nc" id="L138">      }</span>

    /*******************************************************************************************************************
     *
     * Factory method for the PresentationModel of SimpleEntity instances.
     *
     * It aggregates a few extra roles into the PresentationModel that are used by the control, such as callbacks
     * for action associated to the context menu. Also a Displayable role is usually injected to control the rendering
     * of entities.
     *
     ******************************************************************************************************************/
    @Nonnull
    private PresentationModel pmFor (@Nonnull final SimpleEntity entity)
      {
<span class="nc" id="L152">        final Selectable selectable = () -&gt; onSelected(entity);</span>
<span class="nc" id="L153">        final UserAction action1 = UserAction.of(() -&gt; action1(entity), Displayable.of(&quot;Action 1&quot;));</span>
<span class="nc" id="L154">        final UserAction action2 = UserAction.of(() -&gt; action2(entity), Displayable.of(&quot;Action 2&quot;));</span>
<span class="nc" id="L155">        final UserAction action3 = UserAction.of(() -&gt; action3(entity), Displayable.of(&quot;Action 3&quot;));</span>
<span class="nc" id="L156">        return PresentationModel.of(entity, r(Displayable.of(&quot;Item #&quot; + entity.getName()),</span>
                                              selectable,
<span class="nc" id="L158">                                              UserActionProvider.of(action1, action2, action3)));</span>
      }

    /*******************************************************************************************************************
     *
     * Factory method for the PresentationModel of SimpleDciEntity instances.
     *
     ******************************************************************************************************************/
    @Nonnull
    private PresentationModel pmFor (@Nonnull final SimpleDciEntity entity)
      {
        // FIXME: column names
column names
<span class="nc" id="L170"> final Aggregate&lt;PresentationModel&gt; aggregate = PresentationModelAggregate.newInstance()</span> <span class="nc" id="L171"> .withPmOf(&quot;C1&quot;, r(Displayable.of(entity.getName())))</span> <span class="nc" id="L172"> .withPmOf(&quot;C2&quot;, r(Displayable.of(&quot;&quot; + entity.getAttribute1())))</span> <span class="nc" id="L173"> .withPmOf(&quot;C3&quot;, r(Displayable.of(&quot;&quot; + entity.getAttribute2())));</span> <span class="nc" id="L174"> final Selectable selectable = () -&gt; onSelected(entity);</span> <span class="nc" id="L175"> final UserAction action1 = UserAction.of(() -&gt; action1(entity), Displayable.of(&quot;Action 1&quot;));</span> <span class="nc" id="L176"> final UserAction action2 = UserAction.of(() -&gt; action2(entity), Displayable.of(&quot;Action 2&quot;));</span> <span class="nc" id="L177"> final UserAction action3 = UserAction.of(() -&gt; action3(entity), Displayable.of(&quot;Action 3&quot;));</span> // No explicit Displayable here, as the one inside SimpleDciEntity is used. <span class="nc" id="L179"> return PresentationModel.of(entity, r(aggregate, selectable, UserActionProvider.of(action1, action2, action3)));</span> } // Below simple business methonds, as per usual business. /******************************************************************************************************************* * ******************************************************************************************************************/ private void onButtonPressed() { <span class="nc" id="L189"> presentation.notify(&quot;Button pressed&quot;);</span> <span class="nc" id="L190"> status++;</span> <span class="nc" id="L191"> bindings.textProperty.set(Integer.toString(status));</span> <span class="nc" id="L192"> }</span> /******************************************************************************************************************* * ******************************************************************************************************************/ private void onButtonDialogOkPressed() { <span class="nc" id="L199"> presentation.notify(notificationWithFeedback()</span> <span class="nc" id="L200"> .withCaption(&quot;Notification&quot;)</span> <span class="nc" id="L201"> .withText(&quot;Now press the button&quot;)</span> <span class="nc" id="L202"> .withFeedback(feedback().withOnConfirm(() -&gt; presentation.notify(&quot;Pressed ok&quot;))));</span> <span class="nc" id="L203"> }</span> /******************************************************************************************************************* * ******************************************************************************************************************/ private void onButtonDialogOkCancelPressed() { <span class="nc" id="L210"> presentation.notify(notificationWithFeedback()</span> <span class="nc" id="L211"> .withCaption(&quot;Notification&quot;)</span> <span class="nc" id="L212"> .withText(&quot;Now press the button&quot;)</span> <span class="nc" id="L213"> .withFeedback(feedback().withOnConfirm(() -&gt; presentation.notify(&quot;Pressed ok&quot;))</span> <span class="nc" id="L214"> .withOnCancel(() -&gt; presentation.notify(&quot;Pressed cancel&quot;))));</span> <span class="nc" id="L215"> }</span> /******************************************************************************************************************* * * This method demonstrates how to pick a file name by using the proper UI dialog. * ******************************************************************************************************************/ private void onButtonPickFilePressed() { <span class="nc" id="L224"> final BoundProperty&lt;Path&gt; selectedFile = new BoundProperty&lt;&gt;(USER_HOME);</span> <span class="nc" id="L225"> presentation.pickFile(selectedFile,</span> <span class="nc" id="L226"> notificationWithFeedback()</span> <span class="nc" id="L227"> .withCaption(&quot;Pick a file&quot;)</span> <span class="nc" id="L228"> .withFeedback(feedback().withOnConfirm(() -&gt; presentation.notify(&quot;Selected file: &quot; + selectedFile.get()))</span> <span class="nc" id="L229"> .withOnCancel(() -&gt; presentation.notify(&quot;Selection cancelled&quot;))));</span> <span class="nc" id="L230"> }</span> /******************************************************************************************************************* * * This method demonstrates how to pick a directory name by using the proper UI dialog. * ******************************************************************************************************************/ private void onButtonPickDirectoryPressed() { <span class="nc" id="L239"> final BoundProperty&lt;Path&gt; selectedFolder = new BoundProperty&lt;&gt;(USER_HOME);</span> <span class="nc" id="L240"> presentation.pickDirectory(selectedFolder,</span> <span class="nc" id="L241"> notificationWithFeedback()</span> <span class="nc" id="L242"> .withCaption(&quot;Pick a directory&quot;)</span> <span class="nc" id="L243"> .withFeedback(feedback().withOnConfirm(() -&gt; presentation.notify(&quot;Selected folder: &quot; + selectedFolder.get()))</span> <span class="nc" id="L244"> .withOnCancel(() -&gt; presentation.notify(&quot;Selection cancelled&quot;))));</span> <span class="nc" id="L245"> }</span> /******************************************************************************************************************* * ******************************************************************************************************************/ private void onSelected (@Nonnull final Object object) { <span class="nc" id="L252"> presentation.notify(&quot;Selected &quot; + object);</span> <span class="nc" id="L253"> }</span> /******************************************************************************************************************* * ******************************************************************************************************************/ private void action1 (@Nonnull final Object object) { <span class="nc" id="L260"> presentation.notify(&quot;Action 1 on &quot; + object);</span> <span class="nc" id="L261"> }</span> /******************************************************************************************************************* * ******************************************************************************************************************/ private void action2 (@Nonnull final Object object) { <span class="nc" id="L268"> presentation.notify(&quot;Action 2 on &quot; + object);</span> <span class="nc" id="L269"> }</span> /******************************************************************************************************************* * ******************************************************************************************************************/ private void action3 (@Nonnull final Object object) { <span class="nc" id="L276"> presentation.notify(&quot;Action 3 on &quot; + object);</span> <span class="nc" id="L277"> }</span> } </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.7.202105040129</span></div></body></html>