JavaFxAudioRendererPresentationDelegate.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.audio.renderer.impl.javafx;

  28. import javax.annotation.Nonnull;
  29. import javax.inject.Inject;
  30. import java.util.HashMap;
  31. import java.util.Map;
  32. import javafx.fxml.FXML;
  33. import javafx.scene.control.Button;
  34. import javafx.scene.control.Label;
  35. import javafx.scene.control.ProgressBar;
  36. import javafx.scene.input.KeyCodeCombination;
  37. import javafx.scene.input.KeyCombination;
  38. import javafx.scene.input.KeyEvent;
  39. import it.tidalwave.role.ui.UserAction;
  40. import it.tidalwave.role.ui.javafx.JavaFXBinder;
  41. import it.tidalwave.bluemarine2.ui.audio.explorer.AudioExplorerPresentation;
  42. import it.tidalwave.bluemarine2.ui.audio.renderer.AudioRendererPresentation;
  43. import lombok.extern.slf4j.Slf4j;
  44. import static javafx.scene.input.KeyCode.*;

  45. /***********************************************************************************************************************
  46.  *
  47.  * The JavaFX Delegate for {@link AudioExplorerPresentation}.
  48.  *
  49.  * @stereotype  JavaFXDelegate
  50.  *
  51.  * @author  Fabrizio Giudici
  52.  *
  53.  **********************************************************************************************************************/
  54. @Slf4j
  55. public class JavaFxAudioRendererPresentationDelegate implements AudioRendererPresentation
  56.   {
  57.     @FXML
  58.     private Button btPrev;

  59.     @FXML
  60.     private Button btRewind;

  61.     @FXML
  62.     private Button btStop;

  63.     @FXML
  64.     private Button btPause;

  65.     @FXML
  66.     private Button btPlay;

  67.     @FXML
  68.     private Button btFastForward;

  69.     @FXML
  70.     private Button btNext;

  71.     @FXML
  72.     private ProgressBar pbPlayProgress;

  73.     @FXML
  74.     private Label lbTitle;

  75.     @FXML
  76.     private Label lbFolderName;

  77.     @FXML
  78.     private Label lbArtist;

  79.     @FXML
  80.     private Label lbComposer;

  81.     @FXML
  82.     private Label lbDuration;

  83.     @FXML
  84.     private Label lbPlayTime;

  85.     @FXML
  86.     private Label lbNextTrack;

  87.     @Inject
  88.     private JavaFXBinder binder;

  89.     private final Map<KeyCombination, Runnable> accelerators = new HashMap<>();

  90.     @FXML
  91.     private void initialize()
  92.       {
  93. //        final ObservableMap<KeyCombination, Runnable> accelerators = btPlay.getScene().getAccelerators();
  94.         accelerators.put(new KeyCodeCombination(PLAY),     btPlay::fire);
  95.         accelerators.put(new KeyCodeCombination(STOP),     btStop::fire);
  96.         accelerators.put(new KeyCodeCombination(PAUSE),    btPause::fire);
  97.         accelerators.put(new KeyCodeCombination(REWIND),   btRewind::fire);
  98.         accelerators.put(new KeyCodeCombination(FAST_FWD), btFastForward::fire);
  99.       }

  100.     @FXML // TODO: should be useless, but getScene().getAccelerators() doesn't work
  101.     public void onKeyReleased (@Nonnull final KeyEvent event)
  102.       {
  103.         accelerators.getOrDefault(new KeyCodeCombination(event.getCode()), () -> {}).run();
  104.       }

  105.     @Override
  106.     public void bind (@Nonnull final Properties properties,
  107.                       @Nonnull final UserAction prevAction,
  108.                       @Nonnull final UserAction rewindAction,
  109.                       @Nonnull final UserAction stopAction,
  110.                       @Nonnull final UserAction pauseAction,
  111.                       @Nonnull final UserAction playAction,
  112.                       @Nonnull final UserAction fastForwardAction,
  113.                       @Nonnull final UserAction nextAction)
  114.       {
  115.         binder.bind(btPrev,        prevAction);
  116.         binder.bind(btRewind,      rewindAction);
  117.         binder.bind(btStop,        stopAction);
  118.         binder.bind(btPause,       pauseAction);
  119.         binder.bind(btPlay,        playAction);
  120.         binder.bind(btFastForward, fastForwardAction);
  121.         binder.bind(btNext,        nextAction);

  122.         lbTitle.textProperty().bind(properties.titleProperty());
  123.         lbFolderName.textProperty().bind(properties.folderNameProperty());
  124.         lbArtist.textProperty().bind(properties.artistProperty());
  125.         lbComposer.textProperty().bind(properties.composerProperty());
  126.         lbDuration.textProperty().bind(properties.durationProperty());
  127.         lbPlayTime.textProperty().bind(properties.playTimeProperty());
  128.         lbNextTrack.textProperty().bind(properties.nextTrackProperty());
  129.         pbPlayProgress.progressProperty().bind(properties.progressProperty());
  130.       }

  131.     @Override
  132.     public void showUp (@Nonnull final Object control)
  133.       {
  134.       }

  135.     @Override
  136.     public void focusOnPlayButton()
  137.       {
  138.         btPlay.requestFocus();
  139.       }

  140.     @Override
  141.     public void focusOnStopButton()
  142.       {
  143.         btStop.requestFocus();
  144.       }
  145.   }