2016-08-14 15:17:14 +02:00
/ * *
2018-03-01 15:42:47 +01:00
* Project - HomeFlix
*
2019-01-08 17:10:33 +01:00
* Copyright 2016 - 2019 < @Seil0 >
2016-08-14 15:17:14 +02:00
*
* 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 .
*
* /
2018-03-01 15:42:47 +01:00
package kellerkinder.HomeFlix.application ;
2017-02-09 16:27:52 +01:00
2016-08-14 15:17:14 +02:00
import java.io.File ;
import java.io.IOException ;
2017-02-09 16:27:52 +01:00
import java.util.Locale ;
import java.util.ResourceBundle ;
2017-09-16 18:22:46 +02:00
import org.apache.logging.log4j.LogManager ;
import org.apache.logging.log4j.Logger ;
2018-03-29 18:15:57 +02:00
import org.kellerkinder.Alerts.JFX2BtnCancelAlert ;
2018-03-29 12:27:44 +02:00
2016-08-14 15:17:14 +02:00
import javafx.application.Application ;
2018-03-29 12:27:44 +02:00
import javafx.event.ActionEvent ;
import javafx.event.EventHandler ;
2016-08-14 15:17:14 +02:00
import javafx.fxml.FXMLLoader ;
import javafx.scene.Scene ;
import javafx.scene.image.Image ;
import javafx.scene.layout.AnchorPane ;
import javafx.stage.DirectoryChooser ;
2018-03-29 11:26:20 +02:00
import javafx.stage.FileChooser ;
2016-08-14 15:17:14 +02:00
import javafx.stage.Stage ;
2019-01-08 17:10:33 +01:00
import kellerkinder.HomeFlix.controller.XMLController ;
2016-08-14 15:17:14 +02:00
public class Main extends Application {
2018-03-29 18:15:57 +02:00
2018-03-01 15:42:47 +01:00
private Stage primaryStage ;
private Scene scene ;
private AnchorPane pane ;
private MainWindowController mainWindowController ;
2019-01-08 17:10:33 +01:00
private static XMLController xmlController ;
2017-02-09 20:39:43 +01:00
private ResourceBundle bundle ;
2017-09-16 18:22:46 +02:00
private static Logger LOGGER ;
2018-03-29 18:15:57 +02:00
2016-08-14 15:17:14 +02:00
@Override
2017-04-25 23:35:20 +02:00
public void start ( Stage primaryStage ) throws IOException {
2019-01-08 17:10:33 +01:00
LOGGER . info ( " OS: " + XMLController . getOsName ( ) + " " + XMLController . getOsVers ( ) + " " + XMLController . getOsVers ( ) ) ;
LOGGER . info ( " Java: " + XMLController . getJavaVend ( ) + " " + XMLController . getJavaVers ( ) ) ;
LOGGER . info ( " User: " + XMLController . getUserName ( ) + " " + XMLController . getUserHome ( ) ) ;
2018-03-29 18:15:57 +02:00
this . primaryStage = primaryStage ;
2018-05-17 18:58:54 +02:00
2016-08-14 15:17:14 +02:00
mainWindow ( ) ;
}
2018-03-29 18:15:57 +02:00
2018-04-02 18:29:59 +02:00
/ * *
* initialize the mainWindowController , GUI and load the saved settings or call addFirstSource
* initialize the primaryStage and set the file / directory paths
* /
2017-03-05 18:29:24 +01:00
private void mainWindow ( ) {
2016-08-14 15:17:14 +02:00
try {
2017-09-16 18:22:46 +02:00
FXMLLoader loader = new FXMLLoader ( ) ;
2018-09-23 14:33:16 +02:00
loader . setLocation ( getClass ( ) . getResource ( " /fxml/MainWindow.fxml " ) ) ;
2018-03-01 15:42:47 +01:00
pane = ( AnchorPane ) loader . load ( ) ;
2017-09-16 18:22:46 +02:00
primaryStage . setMinHeight ( 600 . 00 ) ;
2019-06-15 18:44:35 +02:00
primaryStage . setMinWidth ( 1130 . 00 ) ;
//primaryStage.setResizable(false);
2017-09-16 18:22:46 +02:00
primaryStage . setTitle ( " Project HomeFlix " ) ;
2018-03-01 15:42:47 +01:00
primaryStage . getIcons ( ) . add ( new Image ( Main . class . getResourceAsStream ( " /icons/Homeflix_Icon_64x64.png " ) ) ) ; //adds application icon
2019-06-15 11:09:59 +02:00
primaryStage . setOnCloseRequest ( event - > System . exit ( 0 ) ) ;
2018-03-01 15:42:47 +01:00
2018-03-29 12:27:44 +02:00
// 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
2019-01-09 22:36:50 +01:00
mainWindowController = loader . getController ( ) ; //Link of FXMLController and controller class
mainWindowController . init ( ) ;
2018-10-13 00:32:29 +02:00
// startup checks
2019-01-08 17:10:33 +01:00
if ( ! XMLController . getConfigFile ( ) . exists ( ) ) {
XMLController . getDirHomeFlix ( ) . mkdir ( ) ;
2018-10-11 12:29:26 +02:00
System . out . println ( " config not found " ) ;
2018-04-28 18:02:02 +02:00
2018-04-02 18:29:59 +02:00
addFirstSource ( ) ;
2019-01-08 17:10:33 +01:00
xmlController . saveSettings ( ) ;
2017-09-16 18:22:46 +02:00
}
2018-03-01 15:42:47 +01:00
2019-01-08 17:10:33 +01:00
if ( ! XMLController . getPosterCache ( ) . exists ( ) ) {
XMLController . getPosterCache ( ) . mkdir ( ) ;
2017-09-16 18:22:46 +02:00
}
2018-10-11 12:29:26 +02:00
2016-08-14 15:17:14 +02:00
} catch ( IOException e ) {
2018-03-01 15:42:47 +01:00
LOGGER . error ( e ) ;
2016-08-14 15:17:14 +02:00
}
}
2018-03-29 18:15:57 +02:00
2018-05-17 18:58:54 +02:00
/ * *
* 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 ) {
2019-01-08 17:10:33 +01:00
// Logger initialization
String logPath = " " ;
if ( System . getProperty ( " os.name " ) . contains ( " Windows " ) ) {
logPath = System . getProperty ( " user.home " ) + " /Documents/HomeFlix/app.log " ;
2018-05-17 18:58:54 +02:00
} else {
2019-01-08 17:10:33 +01:00
logPath = System . getProperty ( " user.home " ) + " /HomeFlix/app.log " ;
2018-05-17 18:58:54 +02:00
}
2019-01-08 17:10:33 +01:00
System . setProperty ( " logFilename " , logPath ) ;
File logFile = new File ( logPath ) ;
2018-05-17 18:58:54 +02:00
logFile . delete ( ) ;
LOGGER = LogManager . getLogger ( Main . class . getName ( ) ) ;
2019-01-08 17:10:33 +01:00
xmlController = new XMLController ( ) ;
2018-05-17 18:58:54 +02:00
launch ( args ) ;
}
2019-01-08 17:10:33 +01:00
/ * * TODO this should move
2018-04-02 18:29:59 +02:00
* 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
2018-03-29 11:26:20 +02:00
* /
2019-01-09 22:36:50 +01:00
private void addFirstSource ( ) {
2019-01-08 17:10:33 +01:00
switch ( XMLController . getSysLocal ( ) ) {
2018-03-04 20:07:43 +01:00
case " en_US " :
bundle = ResourceBundle . getBundle ( " locals.HomeFlix-Local " , Locale . US ) ; // us_english
break ;
case " de_DE " :
2018-04-28 18:02:02 +02:00
bundle = ResourceBundle . getBundle ( " locals.HomeFlix-Local " , Locale . GERMAN ) ; // de_german
2018-03-04 20:07:43 +01:00
break ;
default :
bundle = ResourceBundle . getBundle ( " locals.HomeFlix-Local " , Locale . US ) ; // default local
break ;
}
2017-02-09 16:27:52 +01:00
2018-03-29 18:15:57 +02:00
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 ) ;
}
2018-03-29 11:26:20 +02:00
}
2018-03-29 18:15:57 +02:00
} ;
// streaming action
EventHandler < ActionEvent > btn2Action = new EventHandler < ActionEvent > ( ) {
@Override
public void handle ( ActionEvent event ) {
FileChooser fileChooser = new FileChooser ( ) ;
fileChooser . setTitle ( " addStreamSource " ) ;
2019-01-09 22:36:50 +01:00
File selectedFile = fileChooser . showOpenDialog ( primaryStage ) ;
2018-03-29 18:15:57 +02:00
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 ) ;
}
2018-03-29 11:26:20 +02:00
}
2018-03-29 18:15:57 +02:00
} ;
2018-10-11 12:29:26 +02:00
2018-03-29 18:15:57 +02:00
selectFirstSource . setBtn1Action ( btn1Action ) ;
selectFirstSource . setBtn2Action ( btn2Action ) ;
selectFirstSource . showAndWait ( ) ;
2016-08-14 15:17:14 +02:00
}
2019-01-09 22:36:50 +01:00
2018-09-21 18:57:39 +02:00
}