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

Java tutorial

Introduction

Here is the source code for benedict.zhang.addon.soundmanager.controller.SoundManagerController.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.controller.ModeViewUIController;
import benedict.zhang.addon.persistence.PersistenceManager;
import benedict.zhang.addon.proxy.SoundDataProxy;
import benedict.zhang.addon.soundmanager.model.Sound;
import benedict.zhang.addon.soundmanager.task.SoundQueryTask;
import benedict.zhang.addon.soundmanager.view.SoundManagerConfigureDialog;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.collections.FXCollections;
import javafx.collections.MapChangeListener;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
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.TableView;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import org.apache.commons.beanutils.BeanUtils;

/**
 * FXML Controller class
 *
 * @author bzhang097
 */
public class SoundManagerController implements Initializable, ModeViewUIController {

    @FXML
    private TextField soundName;

    @FXML
    private TextField actor;

    @FXML
    private Button btnSearch;

    private ObservableList<Sound> soundList;

    @FXML
    private TableView searchResultTableView;

    public UIViewMode displayMode;

    private Stage display;

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        soundList = FXCollections.observableArrayList();
        initDataBinding();
        initListeners();

    }

    private void initDataBinding() {
        this.searchResultTableView.setItems(soundList);
    }

    private void initListeners() {

    }

    public void doSearchAction(ActionEvent e) {
        soundList.clear();
        this.btnSearch.setDisable(true);
        Task<List> queryTask = new SoundQueryTask(this.soundName.getText(), this.actor.getText());
        queryTask.setOnSucceeded((WorkerStateEvent event) -> {
            List<Sound> resultList = queryTask.getValue();
            for (int i = 0; i < resultList.size(); i++) {
                resultList.get(i).setNo(new Integer(i + 1));
            }
            this.btnSearch.setDisable(false);
            soundList.setAll(resultList);
        });
        queryTask.run();
    }

    public void onSoundSelected(MouseEvent e) {
        PersistenceManager manager = PersistenceManager.getInstance();
        Sound selectItem = (Sound) (this.searchResultTableView.getSelectionModel().getSelectedItem());
        if (selectItem == null) {
            return;
        }
        Sound selectSound = manager.loadSound(selectItem.getSoundName(), selectItem.getActor());
        if (this.getMode().equals(UIViewMode.EDIT)) {
            if (e.getClickCount() >= 2) {
                try {

                    SoundDataProxy.getInstance().storeSound(ApplicationUIConstants.SOUND_INFO, selectSound);
                    MapChangeListener<String, Sound> listener = (
                            MapChangeListener.Change<? extends String, ? extends Sound> change) -> {
                        if (change.wasAdded()) {
                            Sound sound = SoundDataProxy.getInstance()
                                    .getDataSound(ApplicationUIConstants.SOUND_INFO, Boolean.TRUE);
                            try {
                                BeanUtils.copyProperties(
                                        (Sound) (this.searchResultTableView.getSelectionModel().getSelectedItem()),
                                        sound);
                            } catch (IllegalAccessException ex) {
                                Logger.getLogger(SoundManagerController.class.getName()).log(Level.SEVERE, null,
                                        ex);
                            } catch (InvocationTargetException ex) {
                                Logger.getLogger(SoundManagerController.class.getName()).log(Level.SEVERE, null,
                                        ex);
                            }
                        }
                    };
                    SoundDataProxy.getInstance().addSoundConfigListener(listener);
                    Stage soundManagerConfigure = SoundManagerConfigureDialog.getSoundManagerDialog(this, display,
                            UIViewMode.READONLY);
                    soundManagerConfigure.showAndWait();
                    SoundDataProxy.getInstance().removeSoundConfigListener(listener);
                } catch (IOException ex) {
                    Logger.getLogger(SoundManagerController.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } else {
            if (e.getClickCount() >= 2) {
                SoundDataProxy.getInstance().storeSound(ApplicationUIConstants.SOUND_INFO, selectSound);
                display.hide();
            }
        }

    }

    @Override
    public void setMode(UIViewMode mode) {
        this.displayMode = mode;
        if (UIViewMode.CREATE.equals(mode)) {
            this.displayMode = UIViewMode.EDIT;
        }
    }

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

    @Override
    public void editMode() {

    }

    @Override
    public void readonlyMode() {

    }

    @Override
    public void createMode() {

    }

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

}