JavaFxCecNavigationAdapter.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.cec.javafx.impl;

  28. import javax.annotation.Nonnull;
  29. import javax.inject.Inject;
  30. import java.util.HashMap;
  31. import java.util.Map;
  32. import it.tidalwave.util.annotation.VisibleForTesting;
  33. import javafx.application.Platform;
  34. import javafx.scene.input.KeyCode;
  35. import javafx.scene.robot.Robot;
  36. import it.tidalwave.messagebus.annotation.ListensTo;
  37. import it.tidalwave.messagebus.annotation.SimpleMessageSubscriber;
  38. import it.tidalwave.cec.CecEvent;
  39. import it.tidalwave.cec.CecUserControlEvent;
  40. import it.tidalwave.bluemarine2.ui.commons.flowcontroller.impl.javafx.JavaFxFlowController;
  41. import lombok.extern.slf4j.Slf4j;
  42. import static it.tidalwave.cec.CecEvent.EventType.*;
  43. import static it.tidalwave.cec.CecUserControlEvent.UserControlCode.*;

  44. /***********************************************************************************************************************
  45.  *
  46.  * @author  Fabrizio Giudici
  47.  *
  48.  **********************************************************************************************************************/
  49. @SimpleMessageSubscriber @Slf4j
  50. public class JavaFxCecNavigationAdapter
  51.   {
  52.     private final Map<CecEvent, Runnable> actionMap = new HashMap<>();
  53.    
  54.     public JavaFxCecNavigationAdapter()
  55.       {
  56.         actionMap.put(new CecUserControlEvent(USER_CONTROL_PRESSED,  SELECT),       () -> keyPress(KeyCode.SPACE)     );
  57.         actionMap.put(new CecUserControlEvent(USER_CONTROL_RELEASED, SELECT),       () -> keyRelease(KeyCode.SPACE)   );
  58.         actionMap.put(new CecUserControlEvent(USER_CONTROL_PRESSED,  LEFT),         () -> keyPress(KeyCode.LEFT)      );
  59.         actionMap.put(new CecUserControlEvent(USER_CONTROL_RELEASED, LEFT),         () -> keyRelease(KeyCode.LEFT)    );
  60.         actionMap.put(new CecUserControlEvent(USER_CONTROL_PRESSED,  RIGHT),        () -> keyPress(KeyCode.RIGHT)     );
  61.         actionMap.put(new CecUserControlEvent(USER_CONTROL_RELEASED, RIGHT),        () -> keyRelease(KeyCode.RIGHT)   );
  62.         actionMap.put(new CecUserControlEvent(USER_CONTROL_PRESSED,  UP),           () -> keyPress(KeyCode.UP)        );
  63.         actionMap.put(new CecUserControlEvent(USER_CONTROL_RELEASED, UP),           () -> keyRelease(KeyCode.UP)      );
  64.         actionMap.put(new CecUserControlEvent(USER_CONTROL_PRESSED,  DOWN),         () -> keyPress(KeyCode.DOWN)      );
  65.         actionMap.put(new CecUserControlEvent(USER_CONTROL_RELEASED, DOWN),         () -> keyRelease(KeyCode.DOWN)    );
  66.         actionMap.put(new CecUserControlEvent(USER_CONTROL_PRESSED,  PLAY),         () -> keyPress(KeyCode.PLAY)      );
  67.         actionMap.put(new CecUserControlEvent(USER_CONTROL_RELEASED, PLAY),         () -> keyRelease(KeyCode.PLAY)    );
  68.         actionMap.put(new CecUserControlEvent(USER_CONTROL_PRESSED,  STOP),         () -> keyPress(KeyCode.STOP)      );
  69.         actionMap.put(new CecUserControlEvent(USER_CONTROL_RELEASED, STOP),         () -> keyRelease(KeyCode.STOP)    );
  70.         actionMap.put(new CecUserControlEvent(USER_CONTROL_PRESSED,  PAUSE),        () -> keyPress(KeyCode.PAUSE)     );
  71.         actionMap.put(new CecUserControlEvent(USER_CONTROL_RELEASED, PAUSE),        () -> keyRelease(KeyCode.PAUSE)   );
  72.         actionMap.put(new CecUserControlEvent(USER_CONTROL_PRESSED,  REWIND),       () -> keyPress(KeyCode.REWIND)    );
  73.         actionMap.put(new CecUserControlEvent(USER_CONTROL_RELEASED, REWIND),       () -> keyRelease(KeyCode.REWIND)  );
  74.         actionMap.put(new CecUserControlEvent(USER_CONTROL_PRESSED,  FAST_FORWARD), () -> keyPress(KeyCode.FAST_FWD)  );
  75.         actionMap.put(new CecUserControlEvent(USER_CONTROL_RELEASED, FAST_FORWARD), () -> keyRelease(KeyCode.FAST_FWD));
  76.        
  77.         actionMap.put(new CecUserControlEvent(USER_CONTROL_PRESSED,  EXIT),        
  78.                                                                 () -> flowController.tryToDismissCurrentPresentation());
  79.       }
  80.    
  81.     @Inject
  82.     private JavaFxFlowController flowController;
  83.    
  84.     @VisibleForTesting void onCecUserControlEventReceived (@Nonnull @ListensTo final CecUserControlEvent event)
  85.       {
  86.         log.debug("onCecUserControlEventReceived({})", event);
  87.         Platform.runLater(() -> actionMap.getOrDefault(event, () -> log.warn("unmapped event: {}", event)).run());
  88.       }
  89.    
  90.     private void keyPress (@Nonnull final KeyCode code)
  91.       {
  92.         log.debug("keyPress({})", code);
  93.         final Robot robot = new Robot();
  94.         robot.keyPress(code);              
  95.       }
  96.    
  97.     private void keyRelease (@Nonnull final KeyCode code)
  98.       {
  99.         log.debug("keyRelease({})", code);
  100.         final Robot robot = new Robot();
  101.         robot.keyRelease(code);              
  102.       }
  103.   }