benedict.zhang.addon.soundmanager.controller.SoundManagerConfigureController.java Source code

Java tutorial

Introduction

Here is the source code for benedict.zhang.addon.soundmanager.controller.SoundManagerConfigureController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package benedict.zhang.addon.soundmanager.controller;

import benedict.zhang.addon.common.ApplicationUIConstants;
import benedict.zhang.addon.common.UIViewMode;
import benedict.zhang.addon.common.UserPreferenceConstants;
import benedict.zhang.addon.common.utils.FileUtils;
import benedict.zhang.addon.controller.ModeViewUIController;
import benedict.zhang.addon.listeners.ViewModeChangeListener;
import benedict.zhang.addon.persistence.PersistenceManager;
import benedict.zhang.addon.preferences.UserPreference;
import benedict.zhang.addon.proxy.SoundDataProxy;
import benedict.zhang.addon.soundmanager.model.Sound;
import benedict.zhang.addon.soundmanager.task.SoundSaveTask;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.media.Media;
import javafx.scene.media.MediaException;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import org.apache.commons.beanutils.BeanUtils;

/**
 * FXML Controller class
 *
 * @author bzhang097
 */
public class SoundManagerConfigureController
        implements Initializable, EventHandler<WindowEvent>, ModeViewUIController {

    @FXML
    private MediaView configMediaView;

    @FXML
    private Label soundFileName;

    @FXML
    private Button btnSoundPlayPause;

    @FXML
    private Button btnEditAndUpdate;

    @FXML
    private Button btnCancel;

    @FXML
    private TextField soundName;

    @FXML
    private TextField actor;

    @FXML
    private TextArea soundDescription;

    private MediaPlayer mediaPlayer;

    public UIViewMode displayMode;

    private SimpleStringProperty currentMode;

    private Sound sound;

    private Sound oriSound;

    private Stage dialog;

    private SimpleBooleanProperty isUpdatingOrSaving;

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        currentMode = new SimpleStringProperty();
        sound = new Sound();
        oriSound = new Sound();
        isUpdatingOrSaving = new SimpleBooleanProperty(false);
        this.setMode(null);
        initDataBinding();
        currentMode.addListener(new ViewModeChangeListener(this));

    }

    private void initDataBinding() {
        this.soundFileName.textProperty().bindBidirectional(sound.soundPath());
        this.soundName.textProperty().bindBidirectional(sound.SoundName());
        this.actor.textProperty().bindBidirectional(sound.Actor());
        this.soundDescription.textProperty().bindBidirectional(sound.Description());
    }

    private void initListeners() {
        this.isUpdatingOrSaving.addListener(new ChangeListener<Boolean>() {

            @Override
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                if (newValue) {
                    btnEditAndUpdate.setDisable(true);
                } else {
                    btnEditAndUpdate.setDisable(false);
                }
            }
        });
    }

    public void closeAction(ActionEvent e) {
        this.dialog.hide();

    }

    @Override
    public void handle(WindowEvent event) {
        Sound dbSound = null;
        if (WindowEvent.WINDOW_SHOWN.equals(event.getEventType())) {
            try {
                dbSound = SoundDataProxy.getInstance().getDataSound(ApplicationUIConstants.SOUND_INFO,
                        Boolean.TRUE);
                BeanUtils.copyProperties(sound, dbSound);
                Media media = new Media(new File(sound.getSoundPath()).toURI().toURL().toString());
                if (media != null) {
                    mediaPlayer = new MediaPlayer(media);
                    mediaPlayer.setOnPlaying(() -> {
                        btnSoundPlayPause.setText("Pause");
                        this.sound.setTotalDuration(mediaPlayer.getTotalDuration());
                    });
                    mediaPlayer.setOnPaused(() -> {
                        btnSoundPlayPause.setText("Play");
                    });
                    mediaPlayer.setOnStopped(() -> {
                        btnSoundPlayPause.setText("Play");
                    });
                    mediaPlayer.setOnEndOfMedia(() -> {
                        btnSoundPlayPause.setText("Play");
                        mediaPlayer.stop();
                    });
                    configMediaView.setMediaPlayer(mediaPlayer);
                    String mediaInfo = sound.getSoundPath();
                    soundFileName.setText(mediaInfo);
                    mediaPlayer.play();
                }
            } catch (MalformedURLException ex) {
                this.dialog.hide();
            } catch (IllegalAccessException ex) {
                this.dialog.hide();
            } catch (InvocationTargetException ex) {
                this.dialog.hide();
            } catch (MediaException me) {
                if (dbSound != null) {
                    PersistenceManager.getInstance().removeSound(dbSound);
                }
                this.dialog.hide();

            }
        }

        if (WindowEvent.WINDOW_HIDING.equals(event.getEventType())) {
            if (this.mediaPlayer != null) {
                this.mediaPlayer.stop();
            }
            SoundDataProxy.getInstance().storeSound(ApplicationUIConstants.SOUND_INFO, oriSound);
        }
    }

    public void onPlayPauseButtonClick(ActionEvent e) {
        Button btn = (Button) e.getSource();
        if ("Play".equals(btn.getText())) {
            this.mediaPlayer.play();
        } else if ("Pause".equals(btn.getText())) {
            this.mediaPlayer.pause();
        }
    }

    public void onStopButtonClick(ActionEvent event) {
        this.mediaPlayer.stop();
    }

    public void onEditUpdateButtonClick(ActionEvent e) {
        Button btn = (Button) e.getSource();
        if ("Edit".equals(btn.getText())) {
            this.setMode(UIViewMode.EDIT);
        } else {//update button clicked
            this.setMode(UIViewMode.READONLY);
            doUpdate();
        }
        if ("Save".equals(btn.getText())) {
            doUpdate();
        }
    }

    private void doUpdate() {
        this.isUpdatingOrSaving.set(true);
        // copyFile to repository
        String srcFile = this.sound.getSoundPath();
        String destFolder = UserPreference.getUserPreference().getSetting(UserPreferenceConstants.SOUND_LIBRARY)
                + sound.getActor();
        String destFileName = this.sound.getSoundName() + ".mp3";
        String toSoundFilePath = FileUtils.copyFileToFolder(srcFile, destFolder, destFileName);
        // reset sound file path
        sound.setSoundPath(toSoundFilePath);
        // save sound task
        SoundSaveTask task = new SoundSaveTask(sound);
        task.setOnSucceeded((WorkerStateEvent event) -> {
            this.isUpdatingOrSaving.set(false);
            try {
                BeanUtils.copyProperties(oriSound, sound);
            } catch (IllegalAccessException ex1) {

            } catch (InvocationTargetException ex2) {

            }
            if ("Save".equals(this.btnEditAndUpdate.getText())) {
                this.setMode(UIViewMode.READONLY);
            }
        });
        //        task.setOnFailed((WorkerStateEvent event)->{
        //            event.getSource().getException().printStackTrace();
        //        });
        task.run();
    }

    public void onCancelButtonClick(ActionEvent e) {
        try {
            BeanUtils.copyProperties(sound, oriSound);
            this.setMode(UIViewMode.READONLY);
        } catch (IllegalAccessException ex) {

        } catch (InvocationTargetException ex) {

        }
    }

    @Override
    public void editMode() {
        this.soundName.setDisable(false);
        this.actor.setDisable(false);
        this.soundDescription.setDisable(false);
        this.btnEditAndUpdate.setText("Update");
        this.btnCancel.setVisible(true);
    }

    @Override
    public void readonlyMode() {
        this.soundName.setDisable(true);
        this.actor.setDisable(true);
        this.soundDescription.setDisable(true);
        this.btnEditAndUpdate.setText("Edit");
        this.btnCancel.setVisible(false);
    }

    @Override
    public void createMode() {
        this.btnEditAndUpdate.setDisable(false);
        this.soundName.setDisable(false);
        this.soundName.setText("");
        this.actor.setDisable(false);
        this.actor.setText("");
        this.soundDescription.setDisable(false);
        this.soundDescription.setText("");
        this.btnEditAndUpdate.setText("Save");
        this.btnCancel.setVisible(false);
    }

    @Override
    public void setMode(UIViewMode openMode) {
        this.displayMode = openMode;
        if (openMode != null) {
            this.currentMode.set(ApplicationUIConstants.getUIViewModeString(openMode));
        }
    }

    @Override
    public UIViewMode getMode() {
        return this.displayMode;
    }

    public void setDialog(Stage dialog) {
        this.dialog = dialog;
    }
}