Package org.livesub.ui

Source Code of org.livesub.ui.MainUIController

package org.livesub.ui;

import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.net.URL;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ResourceBundle;
import javafx.animation.Animation;
import javafx.animation.Transition;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ToolBar;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;
import org.apache.log4j.Logger;
import org.livesub.downloader.DownloaderUI;
import org.livesub.player.Player;
import org.livesub.player.PlayerStateEnum;
import org.livesub.player.StateChangeListener;
import org.livesub.utils.Utils;

/**
* Controller of the main UI
* add functionality to the buttons
* @author vasilis
*/
public class MainUIController implements Initializable, StateChangeListener {
   
    private static final Logger LOG = Logger.getLogger(MainUIController.class);
   
    //x and y coordinates of the window
    double xPos, yPos;
   
    private DownloaderUI downloader;
    private GoToDialog goToDialog;
    private Player player;
   
    //FXML components
    @FXML private BorderPane mainPanel;
    @FXML private Text textPanel;
    @FXML private ToolBar toolbarContainer;
    @FXML private Button playButton;
   
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        addDraggableNode(mainPanel);
        setPlayerStateListener();
        Utils.setOptimalSize(mainPanel);
       
        player = new Player(this);
        downloader = new DownloaderUI(player);
        goToDialog = new GoToDialog(player);
    }
   
    @FXML
    private void loadButtonListener(ActionEvent event) {
        if(Utils.playerState.getState() == PlayerStateEnum.PLAYING) {
            player.pause();
        }
        player.loadSubtitles();
    }
   
    @FXML
    private void downloadButtonListener(ActionEvent event) {
        downloader.start(new Stage());
    }
   
    @FXML
    private void playButtonListener(ActionEvent event) {
        updatePlayButton();
    }
   
    @FXML
    private void jumpScrollHandler(ScrollEvent event){
        if(event.getDeltaY() > 0) {
            player.jumpForward();
        } else {
            player.jumpBackward();
        }
    }
   
    @FXML
    private void jumpButtonMouseListener(MouseEvent event) {
        switch(event.getButton()) {
            case PRIMARY:
                player.jumpForward();
                break;
            case SECONDARY:
                player.jumpBackward();
                break;
        }
    }
   
    @FXML
    private void goToButtonListener(ActionEvent event) {
        goToDialog.start();
    }
   
    /**
     * Temp: Adjust the window size
     */
    @FXML
    private void howToUseButtonListener(ActionEvent event) {
        checkScreenSize();
    }
   
    @FXML
    private void settingsButtonListener(ActionEvent event) {
    }
   
    @FXML
    private void quitButtonListener(ActionEvent event) {
        System.exit(0);
    }
   
    @FXML
    private void onMouseEnteredHandler(){
        fadeOutEffect.stop();
        toolbarContainer.setOpacity(1);
        mainPanel.setStyle("-fx-background-color: rgba(.1, .1, .1, 1)");
    }
   
    @FXML
    private void onMouseExitedHandler(){
        fadeOutEffect.play();
    }
   
    private void addDraggableNode(final Node node) {

        node.setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent me) {
                if (me.getButton() != MouseButton.MIDDLE) {
                    xPos = me.getSceneX();
                    yPos = me.getSceneY();
                }
            }
        });

        node.setOnMouseDragged(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent me) {
                if (me.getButton() != MouseButton.MIDDLE) {
                    node.getScene().getWindow().setX(me.getScreenX() - xPos);
                    node.getScene().getWindow().setY(me.getScreenY() - yPos);
                }
            }
        });
    }
   
    private void setPlayerStateListener() {
        Utils.playerState.addListener(this);
    }
   
    final Animation fadeOutEffect = new Transition() {
        {
            setCycleDuration(Duration.millis(500));
        }

        @Override
        protected void interpolate(double frac) {
            double n = 1 - frac;
            NumberFormat formatter = new DecimalFormat("#0.00");
            String s = formatter.format(n);
            mainPanel.setStyle("-fx-background-color: rgba(.1, .1, .1, " + Double.parseDouble(s) + ")");
            toolbarContainer.setOpacity(Double.parseDouble(s));
        }
    };
   
    @Override
    public synchronized void stateChanged() {
        switch (Utils.playerState.getState()) {
            case LOADED:
                LOG.trace("Player state changed to LOADED");
                playButton.setText("Play");
                break;
            case IDLE:
                LOG.trace("Player state changed to IDLE");
                playButton.setText("Play");
                break;
            case PLAYING:
                LOG.trace("Player state changed to PLAYING");
                playButton.setText("Pause");
                break;
            case PAUSED:
                LOG.trace("Player state changed to PAUSED");
                playButton.setText("Resume");
                break;
        }
    }
   
    public void updatePlayButton() {
        switch (Utils.playerState.getState()) {
            case LOADED:
                player.play();
                break;
            case PLAYING:
                player.pause();
                break;
            case PAUSED:
                player.play();
                break;
            case IDLE:
                player.loadSubtitles();
                break;
        }
    }
   
    public String getDisplayedText() {
        return textPanel.getText();
    }
   
    public void setDisplayedText(String text){
        textPanel.setText(text);
    }
   
    /**
     * Check the screen Dimensions
     */
    public void checkScreenSize() {
        double width = 0;
        double height = 0;
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] gs = ge.getScreenDevices();
        for (GraphicsDevice curGs : gs) {
            GraphicsConfiguration[] gc = curGs.getConfigurations();
            for (GraphicsConfiguration curGc : gc) {
                Rectangle bounds = curGc.getBounds();
                width = bounds.getWidth();
                height = bounds.getHeight();
            }
        }
        Utils.setOptimalSize(mainPanel, width, height);
    }
}
TOP

Related Classes of org.livesub.ui.MainUIController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.