djpresentationMaker.forms.frmMainController.java Source code

Java tutorial

Introduction

Here is the source code for djpresentationMaker.forms.frmMainController.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 djpresentationMaker.forms;

import java.awt.*;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.time.LocalDate;
import java.util.*;
import java.util.List;

import djpresentationMaker.DJPresentationMaker;
import djpresentationMaker.classes.dataModels.SlideModel;
import djpresentationMaker.classes.dataModels.SongModel;
import djpresentationMaker.classes.dataModels.SongbookModel;
import djpresentationMaker.classes.general.*;
import djpresentationMaker.classes.presMak.songbook;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Cursor;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.Button;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import org.apache.poi.xslf.usermodel.Placeholder;
import org.apache.poi.xslf.usermodel.SlideLayout;
import org.apache.poi.xslf.usermodel.TextAlign;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;

/**
 * FXML Controller class
 *
 * @author domjo_000
 */
public class frmMainController implements Initializable {

    private String currentIMGPath, currentPicsPath;
    @FXML
    Button cmdNewSongBook, cmdNewSong, cmdSongSubmit, cmdSongbookReset, cmdSongbookSubmit, cmdSongPartAddToOrder,
            cmdSongPartRemoveFromOrder, cmdSongPartAdd, cmdSongbookCover, cmdSongbookFind, cmdMainExport,
            cmdMainUserdefined;
    @FXML
    TextField txtMainSlideTitle, txtMainTitle, txtSongbookTitle, txtSongbookOriginalTitle, txtSongbookISBN,
            txtSongbookPublisher, txtSongbookCover, txtSongTitle, txtSongInterpret, txtSongAlbum, txtSongPartTitle;
    @FXML
    TextArea txtSongbookDescription, txtSongPartContent, txtMainSlideContent;
    @FXML
    ComboBox<String> cmbMainEffect;
    @FXML
    DatePicker dpSongbookRelease;
    @FXML
    ImageView ivMainSlide, ivSongbookCover;
    @FXML
    ListView<String> lvSongParts, lvSongPartOrder;
    @FXML
    TableView<SongbookModel> tblSongSongbook, tblSongbook;
    @FXML
    TableView<SongModel> tblSongSong, tblSong;
    @FXML
    TableView<SlideModel> tblMainSlides;
    @FXML
    TreeView tvPictures;
    @FXML
    TabPane tpnlMain;
    @FXML
    Tab pnlMain, pnlSong, pnlSongBook;
    @FXML
    MenuItem ctxMainSongbookChange, ctxMainSongbookDelete, ctxSongChange, ctxSongDelete, ctxSongpartChange,
            ctxSongpartDelete, ctxMainSongAdd, ctxSongOpenLyrics, ctxMainSongRemove, ctxMainSongChange,
            ctxMainSongDelete;
    @FXML
    MenuItem menExtrasExportAsWord, menExtrasExportAsODT, menHelpAbout, menProgramClose, menExtrasToViewer,
            menProgramSettings;
    @FXML
    ContextMenu ctxSongs;
    @FXML
    ProgressBar pbMain;
    @FXML
    AnchorPane anchpnlMain;
    @FXML
    Accordion accMain;
    @FXML
    TitledPane tpnlSongAndSongbook;
    clsSQLite sql = new clsSQLite();
    clsLang lang = new clsLang();
    Boolean songbookChange = false;
    int songbookID = 0;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        sql.setDatabase(clsHelper.getPath() + File.separatorChar + lang.getValue("database.key"));
        sql.open();
        accMain.setExpandedPane(tpnlSongAndSongbook);
        clsControlHelper.reloadTable(sql, tblSongbook, true);
        clsHelper.listRoots(tvPictures);
        Image img = new Image(
                DJPresentationMaker.class.getResourceAsStream("style" + File.separatorChar + "songbook.png"));
        cmdNewSongBook.setGraphic(new ImageView(img));
        img = new Image(DJPresentationMaker.class.getResourceAsStream("style" + File.separatorChar + "song.png"));
        cmdNewSong.setGraphic(new ImageView(img));
        clsControlHelper.reloadTable(sql, tblSongSongbook);
        clsControlHelper.loadEffects(sql, cmbMainEffect);
        clsControlHelper.resetObjects(true, false, txtMainSlideTitle, txtSongTitle, txtSongInterpret, txtSongAlbum,
                txtSongPartTitle, cmdSongPartAdd, txtMainSlideContent, txtSongPartContent);

        ctxMainSongbookDelete.setOnAction(event -> {
            sql.deleteRow("songbooks", "id=" + tblSongbook.getSelectionModel().getSelectedItem().getID());
            clsControlHelper.reloadTable(sql, tblSongbook, true);
        });

        menExtrasToViewer.setOnAction(event -> clsHelper.openFXML("forms/frmSongViewer.fxml",
                clsLang.getLangBundle(), "SongViewer", "style" + File.separatorChar + "logo_128.png"));

        ctxMainSongbookChange.setOnAction(event -> {
            DataTable dt = sql.getRows("songbooks",
                    Arrays.asList("id", "title", "originalTitle", "publisher", "isbn", "release", "path",
                            "description"),
                    "id='" + tblSongbook.getSelectionModel().getSelectedItem().getID() + "'");
            for (int i = 0; i <= dt.columnsCount() - 1; i++) {
                txtSongbookCover.setText(dt.get(0, "path").toString());
                txtSongbookTitle.setText(dt.get(0, "title").toString());
                txtSongbookOriginalTitle.setText(dt.get(0, "originalTitle").toString());
                txtSongbookPublisher.setText(dt.get(0, "publisher").toString());
                txtSongbookISBN.setText(dt.get(0, "isbn").toString());
                dpSongbookRelease.setValue(LocalDate.parse(dt.get(0, "release").toString()));
                txtSongbookDescription.setText(dt.get(0, "description").toString());
                songbookChange = true;
                if (txtSongbookCover.getText().contains("http://")) {
                    ivSongbookCover.setImage(new Image(txtSongbookCover.getText()));
                } else {
                    ivSongbookCover.setImage(new Image("file:///" + txtSongbookCover.getText()));
                }
                tpnlMain.getSelectionModel().select(pnlSongBook);
                songbookID = Integer.parseInt(dt.get(0, "id").toString());
                cmdSongbookSubmit.setText("ndern");
            }
        });

        ctxSongChange.setOnAction(event -> {
            if (!tblSongSong.getSelectionModel().isEmpty()) {
                clsControlHelper.resetObjects(false, false, txtSongAlbum, txtSongInterpret, txtSongTitle);
                clsControlHelper.resetObjects(true, false, txtSongPartContent, txtSongPartTitle, lvSongPartOrder,
                        lvSongParts, cmdSongPartAdd, cmdSongPartAddToOrder, cmdSongPartRemoveFromOrder);
                clsControlHelper.resetButtons(false, true, lang.getValue("change.key"), cmdSongSubmit);
            }
        });

        ctxSongpartChange.setOnAction(event -> {
            if (!lvSongParts.getSelectionModel().isEmpty()) {
                String title = lvSongParts.getSelectionModel().getSelectedItem();
                String song_id = tblSongSong.getSelectionModel().getSelectedItems().get(0).getID();
                DataTable dt = sql.getRows("songparts", Arrays.asList("title", "content"),
                        "title='" + title + "' AND song_id=" + song_id);
                txtSongPartTitle.setText(dt.get(0, "title").toString().replace("_", "'"));
                txtSongPartContent.setText(dt.get(0, "content").toString().replace("_", "'"));
                cmdSongPartAdd.setText(lang.getValue("change.key"));
            }
        });

        ctxSongDelete.setOnAction(event -> {
            sql.deleteRow("songs", "id=" + tblSongSong.getSelectionModel().getSelectedItems().get(0).getID());
            clsControlHelper.getSongsFromSongbook(sql, tblSongSongbook, tblSongSong, true);
        });

        ctxSongpartDelete.setOnAction(event -> {
            String id = tblSongSong.getSelectionModel().getSelectedItems().get(0).getID();
            sql.deleteRow("songparts", "title='" + lvSongParts.getSelectionModel().getSelectedItem() + "'");
            Map<Integer, String> list = clsControlHelper.listSongPartsAndInfo(sql, tblSongSong, txtSongTitle,
                    txtSongAlbum, txtSongInterpret);
            lvSongParts.getItems().clear();
            for (String item : list.values()) {
                lvSongParts.getItems().add(item);
            }
            clsControlHelper.getOrderedParts(sql, id, lvSongPartOrder);
        });

        menProgramSettings.setOnAction(event -> clsHelper.openFXML("forms/frmSettings.fxml",
                clsLang.getLangBundle(), lang.getValue("settings.key"), "style/logo_128.png"));

        cmdNewSongBook.setOnAction(event -> {
            if (tpnlMain.getSelectionModel().getSelectedItem().equals(pnlSongBook)) {
                tpnlMain.getSelectionModel().select(pnlMain);
                clearTab(2);
            } else {
                tpnlMain.getSelectionModel().select(pnlSongBook);
                songbookChange = false;
                clsControlHelper.reloadTable(sql, tblSongbook, true);
            }
        });

        cmdNewSong.setOnAction(event -> {
            if (tpnlMain.getSelectionModel().getSelectedItem().equals(pnlSong)) {
                tpnlMain.getSelectionModel().select(pnlMain);
                clearTab(3);
                clsControlHelper.resetObjects(true, false, cmdSongPartAdd, txtSongTitle, txtSongInterpret,
                        txtSongAlbum, txtSongPartTitle, txtSongPartContent);
                clsControlHelper.reloadTable(sql, tblSongSongbook);
            } else {
                tpnlMain.getSelectionModel().select(pnlSong);
                clsControlHelper.reloadTable(sql, tblSongbook, true);
            }
        });

        cmdSongbookFind.setOnAction(event -> {
            clsXISBN service = new clsXISBN(txtSongbookISBN.getText());
            txtSongbookPublisher.setText(service.getPublisher());
            txtSongbookTitle.setText(service.getTitle());
            if (!service.getYear().equals("")) {
                dpSongbookRelease.setValue(LocalDate.parse(service.getYear() + "-01-01"));
            }
            txtSongbookDescription.setText(service.getEdition());
            currentIMGPath = "http://covers.librarything.com/devkey/b07988b337d9fecafa139c0203e87b29/medium/isbn/"
                    + txtSongbookISBN.getText();
            txtSongbookCover.setText(currentIMGPath);
            ivSongbookCover.setImage(new Image(currentIMGPath));
        });

        cmdSongbookReset.setOnAction(event -> clearTab(2));

        cmdSongbookCover.setOnAction(event -> {
            if (!txtSongbookCover.getText().equals("") && new File(txtSongbookCover.getText()).exists()
                    && !currentIMGPath.equals(txtSongbookCover.getText())) {
                currentIMGPath = txtSongbookCover.getText();
            } else if (txtSongbookCover.getText().equals("")) {
                FileChooser fileChooser = new FileChooser();
                FileChooser.ExtensionFilter extFilterJPG = new FileChooser.ExtensionFilter("JPG files (*.jpg)",
                        "*.JPG");
                FileChooser.ExtensionFilter extFilterPNG = new FileChooser.ExtensionFilter("PNG files (*.png)",
                        "*.PNG");
                fileChooser.getExtensionFilters().addAll(extFilterJPG, extFilterPNG);
                File file = fileChooser.showOpenDialog(null);
                currentIMGPath = file.getPath();
                txtSongbookCover.setText(currentIMGPath);
            }
            ivSongbookCover.setImage(new Image("file:///" + currentIMGPath));
        });

        ctxSongs.setOnShowing(event -> {
            if (!tblSongbook.getSelectionModel().isEmpty()) {
                if (tblSongbook.getSelectionModel().getSelectedItem().getID().equals("999999")) {
                    ctxMainSongChange.setDisable(false);
                    ctxMainSongDelete.setDisable(false);
                } else {
                    ctxMainSongChange.setDisable(true);
                    ctxMainSongDelete.setDisable(true);
                }
            }
        });

        ctxMainSongChange.setOnAction(event -> {
            try {
                if (!tblSong.getSelectionModel().isEmpty()) {
                    frmUserdefinedController controller = new frmUserdefinedController(
                            tblSong.getSelectionModel().getSelectedItem().getName());
                    FXMLLoader loader = new FXMLLoader(
                            DJPresentationMaker.class.getResource("forms/frmUserdefined.fxml"));
                    loader.setController(controller);
                    loader.setResources(clsLang.getLangBundle());
                    Parent root = loader.load();
                    Scene scene = new Scene(root);
                    Stage stage = new Stage();
                    stage.setScene(scene);
                    stage.setTitle(lang.getValue("frmMain.userdefined.key"));
                    stage.getIcons()
                            .add(new Image(DJPresentationMaker.class.getResourceAsStream("style/logo_128.png")));
                    stage.showAndWait();
                }
            } catch (Exception ex) {
                clsHelper.printException(ex);
            }
        });

        ctxMainSongDelete.setOnAction(event -> {
            try {
                if (!tblSong.getSelectionModel().isEmpty()) {
                    sql.deleteRow("userdefined",
                            "TITLE='" + tblSong.getSelectionModel().getSelectedItem().getName() + "'");
                }
            } catch (Exception ex) {
                clsHelper.printException(ex);
            }
        });

        cmdSongbookSubmit.setOnAction(event -> {
            songbook book = new songbook(txtSongbookTitle.getText(), txtSongbookPublisher.getText());
            book.setOriginalTitle(txtSongbookOriginalTitle.getText());
            book.setISBN(txtSongbookISBN.getText());
            book.setPath(txtSongbookCover.getText());
            book.setDescription(txtSongbookDescription.getText());
            book.setRelease(dpSongbookRelease.getValue());
            if (book.DataSetIsOkay()) {
                if (book.DataSetDontExist(sql) || songbookChange) {
                    List<String> columns = Arrays.asList("title", "originalTitle", "publisher", "isbn", "release",
                            "path", "description");
                    List<String> values = book.returnSongBook();
                    if (!songbookChange) {
                        sql.addRow("songbooks", columns, values);
                    } else {
                        sql.updateRow("songbooks", columns, values, "id='" + songbookID + "'");
                    }
                    songbookChange = false;
                    clearTab(2);
                    clsControlHelper.reloadTable(sql, tblSongbook, true);
                    clsControlHelper.reloadTable(sql, tblSongSongbook);
                    tpnlMain.getSelectionModel().select(pnlMain);
                } else {
                    clsHelper.createAlert(Alert.AlertType.WARNING, lang.getValue("datasetExists.header.key"),
                            lang.getValue("datasetExists.line.key"), "");
                }
            } else {
                clsHelper.createAlert(Alert.AlertType.WARNING, lang.getValue("datasetIncompatible.header.key"),
                        lang.getValue("datasetIncompatible.line.key"), "");
            }
            cmdSongbookSubmit.setText(lang.getValue("finish.key"));
        });

        tblSongbook.setOnMouseClicked(
                event -> clsControlHelper.getSongsFromSongbook(sql, tblSongbook, tblSong, false));
        tblSongSongbook.setOnMouseClicked(event -> {
            clsControlHelper.getSongsFromSongbook(sql, tblSongSongbook, tblSongSong, true);
            cmdSongPartAdd.setDisable(true);
        });

        tblSongSong.setOnMouseClicked(event1 -> {
            if (!tblSongSong.getSelectionModel().isEmpty()) {
                String id = tblSongSong.getSelectionModel().getSelectedItems().get(0).getID();
                Map<Integer, String> values = clsControlHelper.listSongPartsAndInfo(sql, tblSongSong, txtSongTitle,
                        txtSongAlbum, txtSongInterpret);
                if (!id.equals("0")) {
                    lvSongParts.getItems().clear();
                    for (String item : values.values()) {
                        lvSongParts.getItems().add(item);
                    }
                    clsControlHelper.getOrderedParts(sql, id, lvSongPartOrder);
                    clsControlHelper.resetObjects(true, false, txtSongInterpret, txtSongAlbum, txtSongTitle);
                    clsControlHelper.resetObjects(false, false, cmdSongPartAdd, txtSongPartTitle,
                            txtSongPartContent, lvSongPartOrder, lvSongParts, cmdSongPartAddToOrder,
                            cmdSongPartRemoveFromOrder);
                    clsControlHelper.resetButtons(false, true, lang.getValue("add.key"), cmdSongSubmit);
                } else {
                    clsControlHelper.resetObjects(false, true, lvSongParts, lvSongPartOrder, txtSongPartTitle,
                            txtSongPartContent, txtSongTitle, txtSongInterpret, txtSongAlbum, cmdSongPartAdd);
                }
            } else {
                cmdSongPartAdd.setDisable(true);
            }
        });

        tvPictures.setOnMouseClicked(event -> {
            if (!tvPictures.getSelectionModel().isEmpty()) {
                currentPicsPath = ((TreeItem) tvPictures.getSelectionModel().getSelectedItem()).getValue()
                        .toString();
                currentPicsPath = clsHelper.getPath(((TreeItem) tvPictures.getSelectionModel().getSelectedItem()),
                        currentPicsPath);
                clsHelper.listFileSystem(currentPicsPath,
                        ((TreeItem) tvPictures.getSelectionModel().getSelectedItem()));
            }
        });

        cmdSongPartAdd.setOnMouseClicked(event -> {
            if (!cmdSongPartAdd.getText().equals(lang.getValue("change.key"))) {
                for (String item : lvSongParts.getItems()) {
                    if (item.equals(txtSongPartTitle.getText())) {
                        clsHelper.createAlert(Alert.AlertType.WARNING,
                                lang.getValue("frmMain.SongPartExists.Title.key"),
                                lang.getValue("frmMain.SongPartExists.Content1.key") + " "
                                        + txtSongPartTitle.getText() + " "
                                        + lang.getValue("frmMain.SongPartExists.Content2.key"),
                                "");
                        return;
                    }
                }
                if (lvSongParts.getItems().isEmpty()) {
                    if (txtSongAlbum.getText().equals("") && txtSongTitle.getText().equals("")
                            && txtSongInterpret.getText().equals("")) {
                        clsHelper.createAlert(Alert.AlertType.ERROR, lang.getValue("generalInformation.header.key"),
                                lang.getValue("generalInformation.line.key"), "");
                    } else {
                        if (!tblSongSong.getSelectionModel().getSelectedItem().getID().equals("0")) {
                            ObservableList list = tblSongSong.getItems();
                            for (int i = 0; i <= list.size() - 1; i++) {
                                if (((SongModel) list.get(i)).getName().equals(txtSongTitle.getText())) {
                                    addSongPart();
                                    return;
                                }
                            }
                        }
                        String songbook_id = tblSongSongbook.getSelectionModel().getSelectedItem().getID();
                        long song_id = sql.addRowGetID("songs",
                                Arrays.asList("title", "interpret", "album", "songbook_id"),
                                Arrays.asList("'" + txtSongTitle.getText() + "'",
                                        "'" + txtSongInterpret.getText() + "'", "'" + txtSongAlbum.getText() + "'",
                                        songbook_id));
                        sql.addRow("songparts", Arrays.asList("title", "content", "song_id"),
                                Arrays.asList("'" + txtSongPartTitle.getText().replace("'", "_") + "'",
                                        "'" + txtSongPartContent.getText().replace("'", "_") + "'",
                                        String.valueOf(song_id)));
                        clsControlHelper.getSongsFromSongbook(sql, tblSongSongbook, tblSongSong, true);
                        for (int i = 0; i <= tblSongSong.getItems().size() - 1; i++) {
                            if (tblSongSong.getItems().get(i).getName().equals(txtSongTitle.getText())) {
                                tblSongSong.getSelectionModel().select(i);
                            }
                        }
                        String strSong_id = tblSongSong.getSelectionModel().getSelectedItems().get(0).getID();
                        Map<Integer, String> list = clsControlHelper.listSongPartsAndInfo(sql, tblSongSong,
                                txtSongTitle, txtSongAlbum, txtSongInterpret);
                        lvSongParts.getItems().clear();
                        for (String item : list.values()) {
                            lvSongParts.getItems().add(item);
                        }
                        clsControlHelper.getOrderedParts(sql, strSong_id, lvSongPartOrder);
                    }
                } else {
                    addSongPart();
                }
            } else {
                String song_id = tblSongSong.getSelectionModel().getSelectedItem().getID();
                String title = lvSongParts.getSelectionModel().getSelectedItem();
                sql.updateRow("songparts", Arrays.asList("title", "content"),
                        Arrays.asList("'" + txtSongPartTitle.getText().replace("'", "_") + "'",
                                "'" + txtSongPartContent.getText().replace("'", "_") + "'"),
                        "title='" + title + "' AND song_id=" + song_id);
                cmdSongPartAdd.setText(lang.getValue("add.key"));
                String strSong_id = tblSongSong.getSelectionModel().getSelectedItems().get(0).getID();
                Map<Integer, String> list = clsControlHelper.listSongPartsAndInfo(sql, tblSongSong, txtSongTitle,
                        txtSongAlbum, txtSongInterpret);
                lvSongParts.getItems().clear();
                for (String item : list.values()) {
                    lvSongParts.getItems().add(item);
                }
                clsControlHelper.getOrderedParts(sql, strSong_id, lvSongPartOrder);
            }
            txtSongPartContent.setText("");
            txtSongPartTitle.setText("");
        });

        cmdSongSubmit.setOnMouseClicked(event -> {
            if (!tblSongSong.getSelectionModel().isEmpty()) {
                if (!cmdSongSubmit.getText().equals(lang.getValue("change.key"))) {
                    String song_id = tblSongSong.getSelectionModel().getSelectedItem().getID();
                    sql.deleteRow("position", "song_id=" + song_id);
                    for (int i = 1; i <= lvSongPartOrder.getItems().size(); i++) {
                        String songpart_id = sql
                                .getRows("songparts", Collections.singletonList("id"), "title='"
                                        + lvSongPartOrder.getItems().get(i - 1) + "' AND song_id=" + song_id)
                                .get(0, "id").toString();
                        sql.addRow("position", Arrays.asList("song_id", "songpart_id", "position"),
                                Arrays.asList(song_id, songpart_id, String.valueOf(i)));
                    }
                    Map<Integer, String> list = clsControlHelper.listSongPartsAndInfo(sql, tblSongSong,
                            txtSongTitle, txtSongAlbum, txtSongInterpret);
                    lvSongParts.getItems().clear();
                    for (String item : list.values()) {
                        lvSongParts.getItems().add(item);
                    }
                    clsControlHelper.getOrderedParts(sql, song_id, lvSongPartOrder);
                    txtSongPartContent.setText("");
                    txtSongPartTitle.setText("");
                    tpnlMain.getSelectionModel().select(pnlMain);
                } else {
                    String song_id = tblSongSong.getSelectionModel().getSelectedItem().getID();
                    sql.updateRow("songs", Arrays.asList("title", "album", "interpret"),
                            Arrays.asList("'" + txtSongTitle.getText() + "'", "'" + txtSongAlbum.getText() + "'",
                                    "'" + txtSongInterpret.getText() + "'"),
                            "id=" + song_id);
                    cmdSongSubmit.setText(lang.getValue("finish.key"));
                    Map<Integer, String> list = clsControlHelper.listSongPartsAndInfo(sql, tblSongSong,
                            txtSongTitle, txtSongAlbum, txtSongInterpret);
                    lvSongParts.getItems().clear();
                    for (String item : list.values()) {
                        lvSongParts.getItems().add(item);
                    }
                    clsControlHelper.getOrderedParts(sql, song_id, lvSongPartOrder);
                    txtSongPartContent.setText("");
                    txtSongPartTitle.setText("");
                    tpnlMain.getSelectionModel().select(pnlMain);
                    txtSongPartContent.setDisable(false);
                    txtSongPartTitle.setDisable(false);
                    lvSongPartOrder.setDisable(false);
                    lvSongParts.setDisable(false);
                    cmdSongPartAdd.setDisable(false);
                    cmdSongPartAddToOrder.setDisable(false);
                    cmdSongPartRemoveFromOrder.setDisable(false);
                }
                clearTab(3);
                clearTab(1);
                clsControlHelper.reloadTable(sql, tblSongbook, true);
                clsControlHelper.reloadTable(sql, tblSongSongbook);
            }
        });

        cmdSongPartAddToOrder.setOnMouseClicked(event -> {
            if (!lvSongParts.getSelectionModel().isEmpty()) {
                lvSongPartOrder.getItems().add(lvSongParts.getSelectionModel().getSelectedItem());
            }
        });

        cmdSongPartRemoveFromOrder.setOnMouseClicked(event -> {
            if (!lvSongPartOrder.getSelectionModel().isEmpty()) {
                lvSongPartOrder.getItems().remove(lvSongPartOrder.getSelectionModel().getSelectedIndex());
            }
        });

        ctxMainSongAdd.setOnAction(event -> {
            if (!tblSong.getSelectionModel().isEmpty()) {
                String songbook_id = tblSongbook.getSelectionModel().getSelectedItem().getID();
                String song_id = tblSong.getSelectionModel().getSelectedItem().getID();
                String name = tblSong.getSelectionModel().getSelectedItem().getName();
                clsControlHelper.listSlides(sql, tblMainSlides, name, song_id, songbook_id);
            }
        });

        tblMainSlides.setOnMouseClicked(event -> {
            if (!tblMainSlides.getSelectionModel().isEmpty()) {
                if (!tblMainSlides.getSelectionModel().getSelectedItem().getSongBookID().equals("999999")) {
                    String songbook_id = tblSongbook.getSelectionModel().getSelectedItem().getID();
                    SlideModel slide = tblMainSlides.getSelectionModel().getSelectedItem();
                    String id = sql
                            .getRows("songs", Collections.singletonList("id"),
                                    "songbook_id=" + songbook_id + " AND title='" + slide.getSongTitle() + "'")
                            .get(0, "id").toString();
                    DataTable dt = sql.getRows("songparts", Arrays.asList("title", "content"),
                            "song_id=" + id + " AND title='" + slide.getSongPart() + "'");
                    txtMainSlideTitle.setText(dt.get(0, "title").toString());
                    txtMainSlideContent.setText(dt.get(0, "content").toString());
                    if (currentPicsPath != null) {
                        ObservableList<SlideModel> model = tblMainSlides.getItems();
                        List<String> ls = new ArrayList<>();
                        String item = "";
                        for (SlideModel s : model) {
                            if (!item.equals(s.getSongTitle())) {
                                item = s.getSongTitle();
                                ls.add(s.getSongTitle());
                            }
                        }
                        SlideModel selected = tblMainSlides.getSelectionModel().getSelectedItem();
                        ls.stream().filter(strItem -> strItem.equals(selected.getSongTitle())).forEach(strItem -> {
                            int index = ls.indexOf(strItem);
                            List<String> existingPath = new ArrayList<>();
                            for (String current : new File(currentPicsPath).list()) {
                                if (current.endsWith("jpg") || current.endsWith("JPG") || current.endsWith("png")) {
                                    existingPath.add(current);
                                }
                            }
                            ivMainSlide.setImage(
                                    new Image("file:///" + currentPicsPath + "\\" + existingPath.get(index)));
                        });
                    }
                    cmdMainUserdefined.setDisable(false);
                }
            }
        });

        cmdMainExport.setOnAction(event -> {
            try {
                if (!txtMainTitle.getText().equals("")) {
                    anchpnlMain.setCursor(Cursor.WAIT);
                    pbMain.setProgress(0.0);
                    if (clsSettings.getMSOffice()) {
                        clsPowerPoint ppt;
                        if (cmbMainEffect.getSelectionModel().isEmpty()) {
                            ppt = new clsPowerPoint(clsSettings.getPathToExport() + File.separatorChar
                                    + txtMainTitle.getText() + ".pptx");
                        } else {
                            ppt = new clsPowerPoint(
                                    clsSettings.getPathToExport() + File.separatorChar + txtMainTitle.getText()
                                            + ".pptx",
                                    sql.getFile("pptFiles", "CONTENT",
                                            clsSettings.getPathToExport() + File.separatorChar
                                                    + txtMainTitle.getText() + ".pptx",
                                            "effect='" + cmbMainEffect.getSelectionModel().getSelectedItem()
                                                    + "'"));
                        }
                        pbMain.setProgress(0.1);
                        List<Integer> picIDs = new ArrayList<>();
                        if (currentPicsPath != null) {
                            if (new File(currentPicsPath).exists()) {
                                for (String path : new File(currentPicsPath).list()) {
                                    if (path.endsWith("jpg") || path.endsWith("JPG") || path.endsWith("png")) {
                                        picIDs.add(ppt.addImage(currentPicsPath + File.separatorChar + path));
                                    }
                                }
                            }
                        }
                        String currentSong = "";
                        int counter = 0;
                        pbMain.setProgress(0.2);
                        double sum = tblMainSlides.getItems().size() / 0.8;
                        ObservableList<SlideModel> model = tblMainSlides.getItems();
                        for (SlideModel item : model) {
                            if (!item.getSongBookID().equals("999999")) {
                                String song_id = sql
                                        .getRows("songs", Collections.singletonList("id"), "songbook_id="
                                                + item.getSongBookID() + " AND title='" + item.getSongTitle() + "'")
                                        .get(0, "id").toString();
                                DataTable dt = sql.getRows("songparts", Arrays.asList("title", "content"),
                                        "song_id=" + song_id + " and title='" + item.getSongPart() + "'");
                                ppt.createNewSlide(SlideLayout.BLANK);
                                if (!currentSong.equals(item.getSongTitle())) {
                                    currentSong = item.getSongTitle();
                                    counter++;
                                }
                                if (currentPicsPath != null) {
                                    try {
                                        ppt.addBackgroundImage(counter - 1);
                                    } catch (Exception ex) {
                                        System.out.println(ex.toString());
                                    }
                                }
                                ppt.addPlaceholder(item.getSongTitle().replace("_", "'"), Placeholder.TITLE, false,
                                        TextAlign.CENTER);
                                ppt.addPlaceholder(dt.get(0, "content").toString().replace("_", "'"),
                                        Placeholder.CONTENT, false, TextAlign.CENTER);
                                pbMain.setProgress(pbMain.getProgress() + sum);
                            } else {
                                DataTable dt = sql.getRows("userdefined",
                                        Arrays.asList("PH_TITLE", "PH_CONTENT", "PH_POSITION", "MEDIA"),
                                        "TITLE='" + item.getSongTitle() + "'");
                                ppt.createNewSlide(SlideLayout.BLANK);
                                for (int i = 0; i <= dt.rowsCount() - 1; i++) {
                                    switch (dt.get(i, "PH_POSITION").toString()) {
                                    case "0":
                                        ppt.addPlaceholder(dt.get(i, "PH_CONTENT").toString(), Placeholder.TITLE);
                                        break;
                                    case "1":
                                        ppt.addPlaceholder(dt.get(i, "PH_CONTENT").toString(), Placeholder.BODY);
                                        break;
                                    case "2":
                                        ppt.addPlaceholder(dt.get(i, "PH_CONTENT").toString(), Placeholder.CONTENT);
                                        break;
                                    case "3":
                                        ppt.addPlaceholder(dt.get(i, "PH_CONTENT").toString(), 80, 200,
                                                (ppt.getWidth() / 2) - 120, ppt.getHeight() - 280);
                                        break;
                                    case "4":
                                        ppt.addPlaceholder(dt.get(i, "PH_CONTENT").toString(),
                                                (ppt.getWidth() / 2) + 40, 200, (ppt.getWidth() / 2) - 120,
                                                ppt.getHeight() - 280);
                                        break;
                                    case "5":
                                        ppt.addBackgroundImage(ppt.addImage(sql.getFile("userdefined", "MEDIA",
                                                "TITLE='" + item.getSongTitle() + "' AND PH_TITLE='"
                                                        + dt.get(i, "PH_TITLE").toString() + "'")));
                                        break;
                                    case "6":
                                        ppt.addPlaceholder(
                                                ppt.addImage(sql.getFile("userdefined", "MEDIA",
                                                        "TITLE='" + item.getSongTitle() + "' AND PH_TITLE='"
                                                                + dt.get(i, "PH_TITLE").toString() + "'")),
                                                80, 200, ppt.getWidth() - 160, ppt.getHeight() - 280);
                                        break;
                                    default:
                                        System.out.println("Not supported yet!");
                                    }
                                }
                            }
                        }
                        ppt.save();
                    }
                    if (clsSettings.getOpenDocument()) {
                        try {
                            clsODP.Effect effect = null;
                            switch (cmbMainEffect.getSelectionModel().getSelectedIndex()) {
                            case 0:
                                effect = clsODP.Effect.Aufloesen;
                                break;
                            case 1:
                                effect = clsODP.Effect.Karofoermig;
                                break;
                            case 2:
                                effect = clsODP.Effect.Kreisfoermig;
                                break;
                            case 3:
                                effect = clsODP.Effect.Nach_links_aufdecken;
                                break;
                            case 4:
                                effect = clsODP.Effect.Von_links_rollen;
                                break;
                            case 5:
                                effect = clsODP.Effect.Ueber_schwarz_blenden;
                                break;
                            default:
                                effect = clsODP.Effect.Von_unten_rollen;
                            }
                            InputStream in = sql.getFile("templates", "CONTENT", "template.fodp",
                                    "FileName='template.fodp'");
                            in.close();
                            clsODP odp = new clsODP("template.fodp", clsSettings.getPathToExport()
                                    + File.separatorChar + txtMainTitle.getText().replace(" ", "_") + ".odp");
                            List<String> picIDs = new ArrayList<>();
                            if (currentPicsPath != null) {
                                if (new File(currentPicsPath).exists()) {
                                    for (String path : new File(currentPicsPath).list()) {
                                        if (path.endsWith("jpg") || path.endsWith("JPG") || path.endsWith("png")) {
                                            picIDs.add(odp.addImage(currentPicsPath + File.separatorChar + path,
                                                    path));
                                        }
                                    }
                                }
                            }
                            String currentSong = "";
                            int counter = 0;
                            pbMain.setProgress(0.2);
                            double sum = tblMainSlides.getItems().size() / 0.8;
                            ObservableList<SlideModel> model = tblMainSlides.getItems();
                            int i = 1;
                            for (SlideModel item : model) {
                                if (!item.getSongBookID().equals("999999")) {
                                    if (!currentSong.equals(item.getSongTitle())) {
                                        currentSong = item.getSongTitle();
                                        counter++;
                                    }
                                    String song_id = sql.getRows("songs", Collections.singletonList("id"),
                                            "songbook_id=" + item.getSongBookID() + " AND title='"
                                                    + item.getSongTitle() + "'")
                                            .get(0, "id").toString();
                                    DataTable dt = sql.getRows("songparts", Arrays.asList("title", "content"),
                                            "song_id=" + song_id + " and title='" + item.getSongPart() + "'");
                                    if (currentPicsPath != null) {
                                        try {
                                            if (picIDs.size() == 0) {
                                                odp.addSlide(i, i, effect, item.getSongTitle().replace("_", "'"),
                                                        dt.get(0, "content").toString().replace("_", "'"));
                                            } else {
                                                odp.addSlide(i, i, effect, item.getSongTitle().replace("_", "'"),
                                                        dt.get(0, "content").toString().replace("_", "'"),
                                                        picIDs.get(counter));
                                            }
                                        } catch (Exception ex) {
                                            System.out.println(ex.toString());
                                        }
                                    } else {
                                        odp.addSlide(i, i, clsODP.Effect.Aufloesen,
                                                item.getSongTitle().replace("_", "'"),
                                                dt.get(0, "content").toString().replace("_", "'"));
                                    }
                                    pbMain.setProgress(pbMain.getProgress() + sum);
                                    i++;
                                } else {
                                    DataTable dt = sql.getRows("userdefined",
                                            Arrays.asList("PH_TITLE", "PH_CONTENT", "PH_POSITION", "MEDIA"),
                                            "TITLE='" + item.getSongTitle() + "'");
                                    String title = "", image = "";
                                    List<String> content = new ArrayList<>();
                                    for (int j = 0; j <= dt.rowsCount() - 1; j++) {
                                        switch (dt.get(j, "PH_POSITION").toString()) {
                                        case "0":
                                            title = odp.buildHeader(dt.get(j, "PH_CONTENT").toString());
                                            break;
                                        case "1":
                                            content.add(odp.buildcentralText(dt.get(j, "PH_CONTENT").toString(),
                                                    25.199, 3.506, 1.4, 5.751));
                                            break;
                                        case "2":
                                            content.add(odp.buildcentralText(dt.get(j, "PH_CONTENT").toString(),
                                                    25.199, 12.179, 1.4, 4.914));
                                            break;
                                        case "3":
                                            content.add(odp.buildcentralText(dt.get(j, "PH_CONTENT").toString(),
                                                    12.599, 12.179, 1.4, 4.914));
                                            break;
                                        case "4":
                                            content.add(odp.buildcentralText(dt.get(j, "PH_CONTENT").toString(),
                                                    12.599, 12.179, 14, 4.914));
                                            break;
                                        case "5":
                                            image = odp.addImage(
                                                    sql.getFile("userdefined", "MEDIA",
                                                            "TITLE='" + item.getSongTitle() + "' AND PH_TITLE='"
                                                                    + dt.get(j, "PH_TITLE").toString() + "'"),
                                                    String.valueOf(new Date().getTime()));
                                            break;
                                        default:
                                            System.out.println("Not supported yet!");
                                        }
                                    }
                                    if (image.equals("")) {
                                        odp.addSlide(i, i, effect, !title.equals(""), title, content);
                                    } else {
                                        odp.addSlide(i, i, effect, !title.equals(""), title, content, image);
                                    }
                                }
                            }
                            odp.isLast();
                        } catch (Exception ex) {
                            clsHelper.printException(ex);
                        }
                    }
                    tblMainSlides.getColumns().clear();
                    txtMainTitle.setText("");
                    cmbMainEffect.getSelectionModel().select(null);
                    tblSong.getItems().clear();
                    tblSongbook.getSelectionModel().clearSelection();
                    clearTab(1);
                    clsControlHelper.reloadTable(sql, tblSongbook, true);
                    anchpnlMain.setCursor(Cursor.DEFAULT);
                    pbMain.setProgress(1);
                } else {
                    clsHelper.createAlert(Alert.AlertType.WARNING, lang.getValue("frmMain.NoTitle.Title.key"),
                            lang.getValue("frmMain.NoTitle.Content.key"), "");
                }
            } catch (Exception ex) {
                clsHelper.printException(ex);
            } finally {
                anchpnlMain.setCursor(Cursor.DEFAULT);
            }
        });

        tblSong.setOnMouseClicked(event -> {
            if (!tblSong.getSelectionModel().isEmpty()) {
                menExtrasExportAsWord.setDisable(false);
                menExtrasExportAsODT.setDisable(false);
            }
        });

        menExtrasExportAsWord.setOnAction(event -> {
            clsXML xml = new clsXML(new File(clsHelper.getPath() + File.separatorChar + "config.xml"));
            clsWord doc = new clsWord(xml.getElementValue("PathToExport").replace(";", "") + File.separatorChar
                    + tblSong.getSelectionModel().getSelectedItem().getName().replace(" ", "_") + ".doc");
            doc.addHeader(tblSong.getSelectionModel().getSelectedItem().getName().replace("_", "'"));
            ObservableList<SlideModel> model = tblMainSlides.getItems();
            List<String> paragraph = new ArrayList<>();
            String interpret = "";
            for (SlideModel item : model) {
                if (tblSong.getSelectionModel().getSelectedItem().getName().equals(item.getSongTitle())) {
                    DataTable dt = sql.getRows("songs", Arrays.asList("id", "interpret"),
                            "songbook_id=" + item.getSongBookID() + " AND title='" + item.getSongTitle() + "'");
                    DataTable dt2 = sql.getRows("songparts", Arrays.asList("title", "content"),
                            "song_id=" + dt.get(0, "id").toString() + " and title='" + item.getSongPart() + "'");
                    paragraph.add(dt2.get(0, "content").toString().replace("_", "'"));
                    interpret = dt.get(0, "interpret").toString();
                }
            }
            doc.addSubTitle(interpret);
            for (String par : paragraph) {
                doc.addContent(par, ParagraphAlignment.CENTER);
            }
            doc.save();
        });

        menExtrasExportAsODT.setOnAction(event1 -> {
            try {
                FileInputStream in = sql.getFile("templates", "Content",
                        clsSettings.getPathToExport() + File.separatorChar
                                + tblSong.getSelectionModel().getSelectedItem().getName().replace(" ", "_")
                                + ".odt",
                        "FileName='template.fodt'");
                Reader r = new InputStreamReader(in, "UTF8");
                StringBuilder builder = new StringBuilder();
                final int[] ch = new int[1];
                while ((ch[0] = r.read()) != -1) {
                    builder.append((char) ch[0]);
                }
                in.close();
                String doccontent = builder.toString().replace("###Titel###",
                        tblSong.getSelectionModel().getSelectedItem().getName().replace("_", "'"));
                String part = "", interpret = "";
                ObservableList<SlideModel> model = tblMainSlides.getItems();
                for (SlideModel item : model) {
                    if (tblSong.getSelectionModel().getSelectedItem().getName().equals(item.getSongTitle())) {
                        DataTable song = sql.getRows("songs", Arrays.asList("id", "interpret"),
                                "songbook_id=" + item.getSongBookID() + " AND title='" + item.getSongTitle() + "'");
                        interpret = song.get(0, "interpret").toString();
                        DataTable dt = sql.getRows("songparts", Arrays.asList("title", "content"), "song_id="
                                + song.get(0, "id").toString() + " and title='" + item.getSongPart() + "'");
                        for (String line : dt.get(0, "content").toString().split("\n")) {
                            part += "<text:p text:style-name=\"P2\">" + line + "</text:p>\n";
                        }
                        part += "<text:p text:style-name=\"P1\"/>\n";
                    }
                }
                doccontent = doccontent.replace("###Content###", part);
                doccontent = doccontent.replace("###SubTitle###", interpret);
                Writer out = new BufferedWriter(new OutputStreamWriter(
                        new FileOutputStream(clsSettings.getPathToExport() + File.separatorChar
                                + tblSong.getSelectionModel().getSelectedItem().getName().replace(" ", "_")
                                + ".odt"),
                        "UTF8"));
                out.append(doccontent);
                out.flush();
                out.close();
            } catch (Exception ex) {
                clsHelper.printException(ex);
            }
        });

        ctxSongOpenLyrics.setOnAction(event -> {
            if (!txtSongTitle.getText().equals("") && !txtSongInterpret.getText().equals("")) {
                clsLyricWiki lyric = new clsLyricWiki(txtSongInterpret.getText().replace(" ", "%20"),
                        txtSongTitle.getText().replace(" ", "%20"));
                try {
                    String ur = new URI(lyric.getURL()).toString();
                    if (!ur.equals("")) {
                        Desktop.getDesktop().browse(new URI(lyric.getURL()));
                    } else {
                        clsHelper.createAlert(Alert.AlertType.INFORMATION,
                                lang.getValue("frmMain.NothingFount.title.key"),
                                lang.getValue("frmMain.NothingFount.content.key"), "");
                    }
                } catch (URISyntaxException | IOException e) {
                    clsHelper.printException(e);
                }
            }
        });

        ctxMainSongRemove.setOnAction(event -> {
            if (!tblSong.getSelectionModel().isEmpty()) {
                SongModel song = tblSong.getSelectionModel().getSelectedItem();
                SongbookModel book = tblSongbook.getSelectionModel().getSelectedItem();
                ObservableList<SlideModel> slides = tblMainSlides.getItems();
                for (int i = 0; i <= slides.size() - 1; i++) {
                    if (song.getName().equals(slides.get(i).getSongTitle())
                            && book.getID().equals(slides.get(i).getSongBookID())) {
                        slides.remove(i);
                    }
                }
                tblMainSlides.getItems().clear();
                tblMainSlides.setItems(slides);
            }
        });

        cmdMainUserdefined.setOnAction(event -> {
            try {
                frmUserdefinedController controller = new frmUserdefinedController();
                FXMLLoader loader = new FXMLLoader(
                        DJPresentationMaker.class.getResource("forms/frmUserdefined.fxml"));
                loader.setController(controller);
                loader.setResources(clsLang.getLangBundle());
                Parent root = loader.load();
                Scene scene = new Scene(root);
                Stage stage = new Stage();
                stage.setScene(scene);
                stage.setTitle(lang.getValue("frmMain.userdefined.key"));
                stage.getIcons()
                        .add(new Image(DJPresentationMaker.class.getResourceAsStream("style/logo_128.png")));
                stage.showAndWait();
                clsControlHelper.reloadTable(sql, tblSongbook, true);
            } catch (Exception ex) {
                clsHelper.printException(ex);
            }
        });
    }

    private void clearTab(int tab) {
        switch (tab) {
        case 1:
            clsControlHelper.resetTables(false, true, tblMainSlides, tblSong, tblSongbook);
            clsControlHelper.resetObjects(false, true, txtMainSlideContent, txtMainSlideTitle, txtMainTitle);
            break;
        case 2:
            clsControlHelper.resetObjects(false, true, txtSongbookDescription, txtSongbookTitle,
                    txtSongbookOriginalTitle, txtSongbookCover, txtSongbookISBN, txtSongbookPublisher,
                    dpSongbookRelease);
            clsControlHelper.resetButtons(false, true, lang.getValue("finish.key"), cmdSongbookSubmit);
            ivSongbookCover.setImage(null);
            break;
        case 3:
            clsControlHelper.resetObjects(false, true, txtSongTitle, txtSongAlbum, txtSongInterpret,
                    txtSongPartContent, txtSongPartTitle, lvSongParts, lvSongPartOrder);
            clsControlHelper.resetTables(false, true, tblSongSong, tblSongSongbook);
            clsControlHelper.resetButtons(false, true, lang.getValue("finish.key"), cmdSongSubmit);
            clsControlHelper.resetButtons(false, true, lang.getValue("add.key"), cmdSongPartAdd);
            break;
        default:
            break;
        }
    }

    private void addSongPart() {
        String song_id = tblSongSong.getSelectionModel().getSelectedItems().get(0).getID();
        sql.addRow("songparts", Arrays.asList("title", "content", "song_id"),
                Arrays.asList("'" + txtSongPartTitle.getText().replace("'", "_") + "'",
                        "'" + txtSongPartContent.getText().replace("'", "_") + "'", song_id));
        Map<Integer, String> list = clsControlHelper.listSongPartsAndInfo(sql, tblSongSong, txtSongTitle,
                txtSongAlbum, txtSongInterpret);
        lvSongParts.getItems().clear();
        for (String item : list.values()) {
            lvSongParts.getItems().add(item);
        }
        clsControlHelper.getOrderedParts(sql, song_id, lvSongPartOrder);
        txtSongPartContent.setText("");
        txtSongPartTitle.setText("");
    }
}