DefaultMainScreenPresentationControl.java

  1. /*
  2.  * *********************************************************************************************************************
  3.  *
  4.  * blueMarine II: Semantic Media Centre
  5.  * http://tidalwave.it/projects/bluemarine2
  6.  *
  7.  * Copyright (C) 2015 - 2021 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
  12.  * the License. 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
  17.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  18.  * specific language governing permissions and limitations under the License.
  19.  *
  20.  * *********************************************************************************************************************
  21.  *
  22.  * git clone https://bitbucket.org/tidalwave/bluemarine2-src
  23.  * git clone https://github.com/tidalwave-it/bluemarine2-src
  24.  *
  25.  * *********************************************************************************************************************
  26.  */
  27. package it.tidalwave.bluemarine2.ui.mainscreen.impl;

  28. import javax.inject.Inject;
  29. import javax.annotation.Nonnull;
  30. import javax.annotation.PostConstruct;
  31. import java.util.Collection;
  32. import it.tidalwave.util.annotation.VisibleForTesting;
  33. import org.springframework.beans.factory.ListableBeanFactory;
  34. import it.tidalwave.role.ui.Displayable;
  35. import it.tidalwave.role.ui.UserAction;
  36. import it.tidalwave.messagebus.annotation.ListensTo;
  37. import it.tidalwave.messagebus.annotation.SimpleMessageSubscriber;
  38. import it.tidalwave.bluemarine2.message.PowerOnNotification;
  39. import it.tidalwave.bluemarine2.ui.commons.flowcontroller.FlowController;
  40. import it.tidalwave.bluemarine2.ui.mainscreen.MainScreenPresentation;
  41. import lombok.extern.slf4j.Slf4j;
  42. import static java.util.Comparator.comparing;
  43. import static java.util.stream.Collectors.*;

  44. /***********************************************************************************************************************
  45.  *
  46.  * The control of the {@link MainScreenPresentation}.
  47.  *
  48.  * @stereotype  Control
  49.  *
  50.  * @author  Fabrizio Giudici
  51.  *
  52.  **********************************************************************************************************************/
  53. @SimpleMessageSubscriber @Slf4j
  54. public class DefaultMainScreenPresentationControl
  55.   {
  56.     @Inject
  57.     private MainScreenPresentation presentation;

  58.     @Inject
  59.     private FlowController flowController;

  60.     @Inject
  61.     private ListableBeanFactory beanFactory;

  62.     /*******************************************************************************************************************
  63.      *
  64.      * The action that powers off the application.
  65.      *
  66.      ******************************************************************************************************************/
  67.     // FIXME: this fails because flowController is not injected yet.
  68. //    private final UserAction powerOffAction = UserAction.of(flowController::powerOff,
  69. //                                                            LocalizedDisplayable.fromBundle(getClass(), "powerOff"));
  70.     private final UserAction powerOffAction = UserAction.of(() -> flowController.powerOff(),
  71.                                                             Displayable.fromBundle(getClass(), "powerOff"));

  72.     /*******************************************************************************************************************
  73.      *
  74.      * Populates the Presentation with the main menu.
  75.      *
  76.      ******************************************************************************************************************/
  77.     @PostConstruct
  78.     @VisibleForTesting void initialize()
  79.       {
  80.         final Collection<UserAction> mainMenuActions =  beanFactory.getBeansOfType(MainMenuItem.class)
  81.                                                                    .values()
  82.                                                                    .stream()
  83.                                                                    .sorted(comparing(MainMenuItem::getPriority))
  84.                                                                    .map(MainMenuItem::getAction)
  85.                                                                    .collect(toList());
  86.         presentation.bind(mainMenuActions, powerOffAction);
  87.       }

  88.     /*******************************************************************************************************************
  89.      *
  90.      *
  91.      ******************************************************************************************************************/
  92.     @VisibleForTesting void onPowerOnNotification (@Nonnull @ListensTo final PowerOnNotification notification)
  93.       {
  94.         presentation.showUp();
  95.       }
  96.   }