Project-HomeFlix/src/main/java/kellerkinder/HomeFlix/application/Main.java

195 lines
6.6 KiB
Java

/**
* Project-HomeFlix
*
* Copyright 2016-2019 <@Seil0>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
package kellerkinder.HomeFlix.application;
import java.io.File;
import java.io.IOException;
import java.util.Locale;
import java.util.ResourceBundle;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.kellerkinder.Alerts.JFX2BtnCancelAlert;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import kellerkinder.HomeFlix.controller.XMLController;
public class Main extends Application {
private Stage primaryStage;
private Scene scene;
private AnchorPane pane;
private MainWindowController mainWindowController;
private static XMLController xmlController;
private ResourceBundle bundle;
private static Logger LOGGER;
@Override
public void start(Stage primaryStage) throws IOException {
LOGGER.info("OS: " + XMLController.getOsName() + " " + XMLController.getOsVers() + " " + XMLController.getOsVers());
LOGGER.info("Java: " + XMLController.getJavaVend() + " " + XMLController.getJavaVers());
LOGGER.info("User: " + XMLController.getUserName() + " " + XMLController.getUserHome());
this.primaryStage = primaryStage;
mainWindow();
}
/**
* initialize the mainWindowController, GUI and load the saved settings or call addFirstSource
* initialize the primaryStage and set the file/directory paths
*/
private void mainWindow(){
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/fxml/MainWindow.fxml"));
pane = (AnchorPane) loader.load();
primaryStage.setMinHeight(600.00 + 34); // 34 -> window decoration
primaryStage.setMinWidth(1130.00);
//primaryStage.setResizable(false);
primaryStage.setTitle("Project HomeFlix");
primaryStage.getIcons().add(new Image(Main.class.getResourceAsStream("/icons/Homeflix_Icon_64x64.png"))); //adds application icon
primaryStage.setOnCloseRequest(event -> System.exit(0));
// generate window
scene = new Scene(pane); // create new scene, append pane to scene
scene.getStylesheets().add(getClass().getResource("/css/MainWindow.css").toExternalForm());
primaryStage.setScene(scene); // append scene to stage
primaryStage.show(); // show stage
mainWindowController = loader.getController(); //Link of FXMLController and controller class
mainWindowController.init();
// startup checks
if (!XMLController.getConfigFile().exists()) {
XMLController.getDirHomeFlix().mkdir();
System.out.println("config not found");
addFirstSource();
xmlController.saveSettings();
}
if (!XMLController.getPosterCache().exists()) {
XMLController.getPosterCache().mkdir();
}
} catch (IOException e) {
LOGGER.error(e);
}
}
/**
* set the log file location and initialize the logger launch the GUI
* @param args arguments given at the start
*/
public static void main(String[] args) {
// Logger initialization
String logPath = "";
if (System.getProperty("os.name").contains("Windows")) {
logPath = System.getProperty("user.home") + "/Documents/HomeFlix/app.log";
} else {
logPath = System.getProperty("user.home") + "/HomeFlix/app.log";
}
System.setProperty("logFilename", logPath);
File logFile = new File(logPath);
logFile.delete();
LOGGER = LogManager.getLogger(Main.class.getName());
xmlController = new XMLController();
launch(args);
}
/** TODO this should move
* we need to get the path for the first source from the user and add it to
* sources.json, if the user ends the file-/directory-chooser the program will exit
*/
private void addFirstSource() {
switch (XMLController.getSysLocal()) {
case "en_US":
bundle = ResourceBundle.getBundle("locals.HomeFlix-Local", Locale.US); // us_english
break;
case "de_DE":
bundle = ResourceBundle.getBundle("locals.HomeFlix-Local", Locale.GERMAN); // de_german
break;
default:
bundle = ResourceBundle.getBundle("locals.HomeFlix-Local", Locale.US); // default local
break;
}
JFX2BtnCancelAlert selectFirstSource = new JFX2BtnCancelAlert(bundle.getString("addSourceHeader"),
bundle.getString("addSourceBody"),
"-fx-button-type: RAISED; -fx-background-color: #ee3523; -fx-text-fill: BLACK;",
bundle.getString("addDirectory"), bundle.getString("addStreamSource"),
bundle.getString("cancelBtnText"), primaryStage);
// directory action
EventHandler<ActionEvent> btn1Action = new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
DirectoryChooser directoryChooser = new DirectoryChooser();
directoryChooser.setTitle(bundle.getString("addDirectory"));
File selectedFolder = directoryChooser.showDialog(primaryStage);
if (selectedFolder != null && selectedFolder.exists()) {
mainWindowController.addSource(selectedFolder.getPath(), "local");
selectFirstSource.getAlert().close();
} else {
LOGGER.error("The selected folder dosen't exist!");
System.exit(1);
}
}
};
// streaming action
EventHandler<ActionEvent> btn2Action = new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("addStreamSource");
File selectedFile = fileChooser.showOpenDialog(primaryStage);
if (selectedFile != null && selectedFile.exists()) {
mainWindowController.addSource(selectedFile.getPath(), "stream");
selectFirstSource.getAlert().close();
} else {
LOGGER.error("The selected file dosen't exist!");
System.exit(1);
}
}
};
selectFirstSource.setBtn1Action(btn1Action);
selectFirstSource.setBtn2Action(btn2Action);
selectFirstSource.showAndWait();
}
}