slideshow.client.ui.ClientUIController.java Source code

Java tutorial

Introduction

Here is the source code for slideshow.client.ui.ClientUIController.java

Source

/*
 * The MIT License
 *
 * Copyright 2016 Patrick Dallarosa and Stephen Brikiatis.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package slideshow.client.ui;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;
import org.json.JSONObject;
import slideshow.client.*;

/**
 *<h1>FXML Controller class </h1>
 * Initializes FXML and contains Events.
 *
 * @author Patrick Dallarosa
 * @version 1
 * @since 19 April 2016
 */
public class ClientUIController implements Initializable {

    /**
     * Initializes FXML panel to default
     * 
     * @param url
     * @param rb 
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        System.out.println("slideshow.client.ui.ClientUIController.initialize");

        //Initializes members
        assert rootVBox != null : "fx:id=\"rootVBox\" was not injected: check your FXML file 'clientUI.fxml'.";
        assert songListView != null : "fx:id=\"songListView\" was not injected: check your FXML file 'clientUI.fxml'.";
        assert spotifyTextField != null : "fx:id=\"spotifyTextField\" was not injected: check your FXML file 'clientUI.fxml'.";
        assert spotifyLabel != null : "fx:id=\"spotifyLabel\" was not injected: check your FXML file 'clientUI.fxml'.";
        assert prevSongButton != null : "fx:id=\"prevSongButton\" was not injected: check your FXML file 'clientUI.fxml'.";
        assert playSongButton != null : "fx:id=\"playSongButton\" was not injected: check your FXML file 'clientUI.fxml'.";
        assert nextSongButton != null : "fx:id=\"nextSongButton\" was not injected: check your FXML file 'clientUI.fxml'.";
        assert fullscreenButton != null : "fx:id=\"fullScreenButton\" was not injected: check your FXML file 'clientUI.fxml'.";
        assert prevPhotoButton != null : "fx:id=\"prevPhotoButton\" was not injected: check your FXML file 'clientUI.fxml'.";
        assert nextPhotoButton != null : "fx:id=\"nextPhotoButton\" was not injected: check your FXML file 'clientUI.fxml'.";
        assert photoImageView != null : "fx:id=\"photoImageView\" was not injected: check your FXML file 'clientUI.fxml'.";
        assert imagePane != null : "fx:id=\"imagePane\" was not injected: check your FXML file 'clientUI.fxml'.";

        this.mMusic = new Music();

        //load information
        try {
            System.out.println("Loading Local Directories");
            this.mSongsLocal = loadDir(new File(this.getClass().getResource(this.DIRECTORY_MUSIC).toURI()));
            this.mPhotosLocal = loadDir(new File(this.getClass().getResource(this.DIRECTORY_PHOTOS).toURI()));

            ObservableList<String> songs = FXCollections.observableArrayList(this.mSongsLocal.keySet());

            Collections.sort(songs);

            this.songListView.setItems(songs);

            //this.listProperty.set(FXCollections.observableArrayList(this.songsLocal.keySet()));
        } catch (URISyntaxException e) {
            System.out.println(e.getMessage());
        }

        this.photoImageView.fitWidthProperty().bind(this.imagePane.widthProperty());
        this.photoImageView.fitHeightProperty().bind(this.imagePane.heightProperty());

        this.mConnectServer = new Task<Integer>() {
            @Override
            protected Integer call() throws Exception {
                ClientUIController.this.mConnect = new Connect(InetAddress.getLocalHost(), 6001);
                ClientUIController.this.mBufferedReader = new BufferedReader(
                        new InputStreamReader(ClientUIController.this.mConnect.getSocket().getInputStream()));
                ClientUIController.this.mPhotoAddress = ClientUIController.this.mBufferedReader.readLine();

                ClientUIController.this.photoImageView.setImage(new Image(ClientUIController.this.mPhotoAddress));

                int iterations = 0;

                do {
                    System.out.println("Fetching photo");

                    ClientUIController.this.mPhotoAddress = ClientUIController.this.mBufferedReader.readLine();

                    ClientUIController.this.photoImageView
                            .setImage(new Image(ClientUIController.this.mPhotoAddress));

                    Thread.sleep(10000);

                } while (ClientUIController.this.mPhotoAddress != null);

                return 1;

            }
        };

        Thread th = new Thread(this.mConnectServer);
        th.setDaemon(true);
        th.start();

    }//end initialize

    /**
     * Fill a HashMap with Strings and URI from a directory
     * @param dir 
     * @return HashMap<String, URI> containing the files in the directory
     */
    public HashMap<String, URI> loadDir(File dir) {
        HashMap<String, URI> map = new HashMap<>();
        String[] names = dir.list();
        File[] files = dir.listFiles();

        if (names != null && files != null) {
            for (int i = 0; i < files.length && i < names.length; i++) {
                if (names[i] != null && files[i] != null) {
                    map.put(names[i], files[i].toURI());
                    System.out.println(names[i]);
                }
            }
        }

        return map;

    }//end loadDir

    /**
     * Set ImageView to fullscreen
     * @param event
     */
    @FXML
    void fullscreenClicked(ActionEvent event) {
        System.out.println("slideshow.client.ui.ClientUIController.fullscreenClicked");
        Stage stage = new Stage();
        stage.setTitle("Fullscreen");

        ImageView iv = this.photoImageView;
        Pane p = new Pane(iv);

        Scene scene = new Scene(p);

        stage.setScene(scene);

        stage.setWidth(100);
        stage.setHeight(200);
        stage.setFullScreen(true);

        stage.show();

    }//end method fullscreenClicked

    /**
     * ListView item clicked
     * @param event 
     */
    @FXML
    void listClicked(MouseEvent event) {

        //Accepts Double Clicks
        if (event.getClickCount() == this.LIST_VIEW_CLICKS) {
            System.out.println("slideshow.client.ui.ClientUIController.listClicked.doubleClick");

            //Gets Selected Item
            String selected = this.songListView.getSelectionModel().getSelectedItem().toString();

            URI uri = this.mSongsLocal.get(selected);

            if (uri != null) {
                this.mMusic.pause();
                this.mMusic = new Music(uri);

                this.playSongButton.setGraphic(new ImageView(new Image("/slideshow/client/ui/icons/pause.png")));

                this.mMusic.play();
            }
        }
    }//end method listClicked

    /**
     * Go to next photo
     * @param event 
     */
    @FXML
    void nextPhotoClicked(ActionEvent event) {
        System.out.println("slideshow.client.ui.ClientUIController.nextPhotoClicked");

    }//end nextPhotoClicked

    /**
     * Reset or play previous song
     * @param event 
     */
    @FXML
    void nextSongClicked(ActionEvent event) {
        System.out.println("slideshow.client.ui.ClientUIController.nextSongClicked");

    }//end method nextSongClicked

    /**
     * Toggle Music on and off
     * @param event 
     */
    @FXML
    void playSongClicked(ActionEvent event) {
        System.out.println("slideshow.client.ui.ClientUIController.playSongClicked");

        if (this.mMusic.getMediaPlayer() != null) {
            System.out.println("slideshow.client.Music." + this.mMusic.getMediaPlayer().getStatus());

            if (this.mMusic.getMediaPlayer().getStatus().equals(MediaPlayer.Status.PLAYING)) {
                this.mMusic.pause();
                this.playSongButton.setGraphic(new ImageView(new Image("/slideshow/client/ui/icons/play.png")));
            }

            if (this.mMusic.getMediaPlayer().getStatus().equals(MediaPlayer.Status.PAUSED)) {
                this.mMusic.play();
                this.playSongButton.setGraphic(new ImageView(new Image("/slideshow/client/ui/icons/pause.png")));
            }
        }

    }//end method playSongClicked

    /**
     * Go to previous photo
     */
    @FXML
    void previousPhotoClicked(ActionEvent event) {
        System.out.println("slideshow.client.ui.ClientUIController.previousPhotoClicked");

    }//end previousPhotoClicked

    /**
     * Play Next Song
     * @param event 
     */
    @FXML
    void previousSongClicked(ActionEvent event) {
        System.out.println("slideshow.client.ui.ClientUIController.previousSongClicked");

    }//end method previousSongClicked

    /**
     * Search Spotify
     * @param event
     */
    @FXML
    void searchSpotify(ActionEvent event) {
        System.out.println("slideshow.client.ui.ClientUIController.searchSpotify");

    }//end searchSpotify

    /**
     * Authorize Spotify
     * @param event
     */
    @FXML
    void spotifyClicked(MouseEvent event) {
        System.out.println("slideshow.client.ui.ClientUIController.spotifyClicked");

    }//end spotifyClicked

    /**
     * Members
     */
    private HashMap<String, URI> mSongsLocal;
    private HashMap<String, JSONObject> mSongsSpotify;

    private HashMap<String, URI> mPhotosLocal;
    private HashMap<String, JSONObject> mPhotosNasa;

    private String mCurrentSongKey;
    private String mCurrentPhotoKey;

    private Music mMusic;
    private Photos mPhotos;

    private Connect mConnect;
    private BufferedReader mBufferedReader;
    private String mPhotoAddress;

    private Task<Integer> mConnectServer;
    private Task<Integer> mUpdateImageView;

    /**
     * Components
     */
    @FXML
    private VBox rootVBox;
    @FXML
    private ListView songListView;
    @FXML
    private TextField spotifyTextField;
    @FXML
    private Label spotifyLabel;
    @FXML
    private Button prevSongButton;
    @FXML
    private Button playSongButton;
    @FXML
    private Button nextSongButton;
    @FXML
    private Button fullscreenButton;
    @FXML
    private Button prevPhotoButton;
    @FXML
    private Button nextPhotoButton;
    @FXML
    private ImageView photoImageView;
    @FXML
    private MediaView songMediaView;
    @FXML
    private Pane imagePane;

    /**
     * Constants
     */
    private final String DIRECTORY_MUSIC = "../music";
    private final String DIRECTORY_PHOTOS = "../images";

    private final byte LIST_VIEW_CLICKS = 2;

    private final String PLAY_BUTTON_STYLE = "play";
    private final String PAUSE_BUTTON_STYLE = "pause";

}//end ClientUIController