Content of file DefaultAudioExplorerPresentationControl.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>DefaultAudioExplorerPresentationControl.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">blueMarine II :: UI :: JavaFX</a> &gt; <a href="../index.html" class="el_bundle">it-tidalwave-bluemarine2-ui</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.bluemarine2.ui.audio.explorer.impl</a> &gt; <span class="el_source">DefaultAudioExplorerPresentationControl.java</span></div><h1>DefaultAudioExplorerPresentationControl.java</h1><pre class="source lang-java linenums"><span class="nc" id="L1">/*</span>
 * *********************************************************************************************************************
 *
 * blueMarine II: Semantic Media Centre
 * http://tidalwave.it/projects/bluemarine2
 *
 * 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/bluemarine2-src
 * git clone https://github.com/tidalwave-it/bluemarine2-src
 *
 * *********************************************************************************************************************
 */
package it.tidalwave.bluemarine2.ui.audio.explorer.impl;

import javax.annotation.Nonnull;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Stack;
import java.util.concurrent.atomic.AtomicReference;
import java.net.URL;
import javafx.application.Platform;
import it.tidalwave.util.Finder;
import it.tidalwave.util.annotation.VisibleForTesting;
import it.tidalwave.role.ui.Displayable;
import it.tidalwave.role.ui.PresentationModel;
import it.tidalwave.role.ui.UserAction;
import it.tidalwave.dci.annotation.DciContext;
import it.tidalwave.messagebus.MessageBus;
import it.tidalwave.messagebus.annotation.ListensTo;
import it.tidalwave.messagebus.annotation.SimpleMessageSubscriber;
import it.tidalwave.bluemarine2.model.role.EntityBrowser;
import it.tidalwave.bluemarine2.model.spi.Entity;
import it.tidalwave.bluemarine2.downloader.DownloadComplete;
import it.tidalwave.bluemarine2.downloader.DownloadRequest;
import it.tidalwave.bluemarine2.ui.commons.OnActivate;
import it.tidalwave.bluemarine2.ui.commons.OnDeactivate;
import it.tidalwave.bluemarine2.ui.commons.OpenAudioExplorerRequest;
import it.tidalwave.bluemarine2.ui.audio.explorer.AudioExplorerPresentation;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import static java.util.stream.Collectors.*;
import static java.util.stream.Stream.*;
import static it.tidalwave.role.SimpleComposite._SimpleComposite_;
import static it.tidalwave.role.ui.Displayable._Displayable_;
import static it.tidalwave.role.ui.spi.PresentationModelCollectors.toCompositePresentationModel;
import static it.tidalwave.bluemarine2.model.spi.PathAwareEntity._PathAwareEntity_;

/***********************************************************************************************************************
 *
 * The Control of the {@link AudioExplorerPresentation}.
 *
 * @stereotype  Control
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="nc" id="L76">@SimpleMessageSubscriber @DciContext @Slf4j</span>
<span class="nc" id="L77">public class DefaultAudioExplorerPresentationControl implements AudioExplorerPresentationControlSpi</span>
  {
<span class="nc" id="L79">    @AllArgsConstructor @Getter @ToString</span>
    private static class FolderAndMemento
      {
        @Nonnull
<span class="nc" id="L83">        private final Entity folder;</span>

        @Nonnull
<span class="nc" id="L86">        private final Optional&lt;Object&gt; memento;</span>
      }

    @Inject
    private AudioExplorerPresentation presentation;

    @Inject
    private MessageBus messageBus;

    @Inject
    private List&lt;EntityBrowser&gt; browsers;

    private Entity currentFolder;

<span class="nc" id="L100">    private final Stack&lt;FolderAndMemento&gt; navigationStack = new Stack&lt;&gt;();</span>

<span class="nc" id="L102">    private final AudioExplorerPresentation.Properties properties = new AudioExplorerPresentation.Properties();</span>

<span class="nc" id="L104">    private final UserAction navigateUpAction = UserAction.of(this::navigateUp);</span>

<span class="nc" id="L106">    private final AtomicReference&lt;Optional&lt;URL&gt;&gt; currentCoverArtUrl = new AtomicReference&lt;&gt;(Optional.empty());</span>

<span class="nc" id="L108">    @Getter</span>
    private final List&lt;Entity&gt; mediaItems = new ArrayList&lt;&gt;();

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @PostConstruct
    @VisibleForTesting void initialize()
      {
<span class="nc" id="L118">        presentation.bind(properties, navigateUpAction);</span>
<span class="nc" id="L119">      }</span>

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @VisibleForTesting void onOpenAudioExplorerRequest (@ListensTo @Nonnull final OpenAudioExplorerRequest request)
      {
<span class="nc" id="L127">        log.info(&quot;onOpenAudioExplorerRequest({})&quot;, request);</span>
<span class="nc" id="L128">        presentation.showUp(this);</span>
<span class="nc" id="L129">        populateBrowsers();</span>
<span class="nc" id="L130">      }</span>

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @VisibleForTesting void onDownloadComplete (@ListensTo @Nonnull final DownloadComplete notification)
      {
<span class="nc" id="L138">        log.info(&quot;onDownloadComplete({})&quot;, notification);</span>

<span class="nc bnc" id="L140" title="All 2 branches missed.">        if (currentCoverArtUrl.get().map(url -&gt; url.equals(notification.getUrl())).orElse(false))</span>
          {
<span class="nc bnc" id="L142" title="All 2 branches missed.">            if (notification.getStatusCode() == 200) // FIXME</span>
              {
<span class="nc" id="L144">                presentation.setCoverArt(Optional.of(notification.getCachedUri()));</span>
              }
          }
<span class="nc" id="L147">      }</span>

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    @OnActivate
    @VisibleForTesting void onActivate()
      {
<span class="nc" id="L157">        presentation.focusOnMediaItems();</span>
<span class="nc" id="L158">      }</span>

    /*******************************************************************************************************************
     *
     * Deactivation is disabled (and acts as navigateUpAction) when the stack is not empty.
     *
     ******************************************************************************************************************/
    @OnDeactivate
    @VisibleForTesting OnDeactivate.Result onDeactivate()
      {
<span class="nc" id="L168">        log.debug(&quot;onDeactivate()&quot;);</span>

<span class="nc bnc" id="L170" title="All 2 branches missed.">        if (navigationStack.isEmpty())</span>
          {
<span class="nc" id="L172">            return OnDeactivate.Result.PROCEED;</span>
          }
        else
          {
<span class="nc" id="L176">            navigateUp();</span>
<span class="nc" id="L177">            return OnDeactivate.Result.IGNORE;</span>
          }
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public void selectBrowser (@Nonnull final EntityBrowser browser)
      {
<span class="nc" id="L189">        log.info(&quot;selectBrowser({})&quot;, browser);</span>
<span class="nc" id="L190">        navigationStack.clear();</span>
<span class="nc" id="L191">        populateItems(new FolderAndMemento(browser.getRoot(), Optional.empty()));</span>
<span class="nc" id="L192">      }</span>

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public void navigateTo (@Nonnull final Entity newMediaFolder)
      {
<span class="nc" id="L202">        log.debug(&quot;navigateTo({})&quot;, newMediaFolder);</span>
<span class="nc" id="L203">        navigationStack.push(new FolderAndMemento(currentFolder, Optional.of(presentation.getMemento())));</span>
<span class="nc" id="L204">        populateItems(new FolderAndMemento(newMediaFolder, Optional.empty()));</span>
<span class="nc" id="L205">      }</span>

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public void renderDetails (@Nonnull final String details)
      {
<span class="nc" id="L215">        presentation.renderDetails(details);</span>
<span class="nc" id="L216">      }</span>

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public void clearDetails()
      {
<span class="nc" id="L226">        presentation.setCoverArt(Optional.empty());</span>
<span class="nc" id="L227">        presentation.renderDetails(&quot;&quot;);</span>
<span class="nc" id="L228">      }</span>

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public void requestCoverArt (@Nonnull final Optional&lt;URL&gt; optionalCoverArtUrl)
      {
<span class="nc" id="L238">        log.debug(&quot;requestCoverArt({})&quot;, optionalCoverArtUrl);</span>
<span class="nc" id="L239">        currentCoverArtUrl.set(optionalCoverArtUrl);</span>
<span class="nc" id="L240">        optionalCoverArtUrl.ifPresent(url -&gt; messageBus.publish(new DownloadRequest(url)));</span>
<span class="nc" id="L241">      }</span>

    /*******************************************************************************************************************
     *
     * Navigates up to the parent folder.
     *
     ******************************************************************************************************************/
    private void navigateUp()
      {
<span class="nc" id="L250">        log.debug(&quot;navigateUp()&quot;);</span>
<span class="nc" id="L251">        populateItems(navigationStack.pop());</span>
<span class="nc" id="L252">      }</span>

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    private void populateBrowsers()
      {
<span class="nc" id="L261">        log.debug(&quot;populateBrowsers()&quot;);</span>

        // FIXME: in this case role injection doesn't work because browsers are pre-instantiated by Spring and not
in this case role injection doesn't work because browsers are pre-instantiated by Spring and not
// in this context. // contextManager.runWithContext(this, new SimpleTask() // { // @Override // public Void run() // { // final PresentationModel pm = browsers.stream() // natively sorted by @OrderBy // .map(o -&gt; o.as(_Presentable_).createPresentationModel()) // .collect(toCompositePresentationModel()); // presentation.populateBrowsers(pm); // selectBrowser(browsers.get(0)); // return null; // } // }); <span class="nc" id="L279"> final PresentationModel pm = toCompositePresentationModel(browsers, EntityBrowserUserActionProvider::new);</span> <span class="nc" id="L280"> presentation.populateBrowsers(pm);</span> <span class="nc" id="L281"> selectBrowser(browsers.get(0));</span> <span class="nc" id="L282"> }</span> /******************************************************************************************************************* * * Populates the presentation with the contents of a folder and selects an item. * * @param folderAndMemento the folder and the presentation memento * ******************************************************************************************************************/ private void populateItems (@Nonnull final FolderAndMemento folderAndMemento) { <span class="nc" id="L293"> log.debug(&quot;populateItems({})&quot;, folderAndMemento);</span> <span class="nc" id="L294"> this.currentFolder = folderAndMemento.getFolder();</span> // FIXME: shouldn't deal with JavaFX threads here <span class="nc bnc" id="L296" title="All 2 branches missed."> Platform.runLater(() -&gt; navigateUpAction.enabled().set(!navigationStack.isEmpty()));</span> <span class="nc" id="L297"> Platform.runLater(() -&gt; properties.folderNameProperty().setValue(getCurrentPathLabel()));</span> <span class="nc" id="L298"> final Finder&lt;? extends Entity&gt; finder = currentFolder.as(_SimpleComposite_).findChildren().withContext(this);</span> <span class="nc" id="L299"> mediaItems.clear();</span> // mediaItems.addAll(finder.stream().filter(i -&gt; i instanceof MediaItem).map(i -&gt; (MediaItem)i).collect(toList // ())); <span class="nc" id="L302"> mediaItems.addAll(finder.results());</span> // Needs the cast for overloading ambiguity in the method signature <span class="nc" id="L304"> final PresentationModel pm = toCompositePresentationModel(mediaItems);</span> <span class="nc" id="L305"> presentation.populateItems(pm, folderAndMemento.getMemento());</span> <span class="nc" id="L306"> }</span> /******************************************************************************************************************* * * Computes the label describing the current navigation path. * ******************************************************************************************************************/ @Nonnull private String getCurrentPathLabel() { <span class="nc" id="L316"> return concat(navigationStack.stream().map(FolderAndMemento::getFolder), of(currentFolder))</span> <span class="nc" id="L317"> .filter(i -&gt; i.maybeAs(_PathAwareEntity_).map(p -&gt; p.getParent().isPresent()).orElse(true))</span> <span class="nc" id="L318"> .filter(i -&gt; i.maybeAs(_Displayable_).isPresent())</span> <span class="nc" id="L319"> .map(i -&gt; i.maybeAs(_Displayable_).map(Displayable::getDisplayName).orElse(&quot;???&quot;))</span> <span class="nc" id="L320"> .collect(joining(&quot; / &quot;));</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>