2016-08-14 15:17:14 +02:00
/ * *
2018-03-01 15:42:47 +01:00
* Project - HomeFlix
*
* Copyright 2016 - 2018 < @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 .
*
* /
2017-04-15 17:54:47 +02:00
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 ;
2016-08-14 15:17:14 +02:00
import java.util.Optional ;
2017-02-09 16:27:52 +01:00
import java.util.ResourceBundle ;
2017-09-16 18:22:46 +02:00
import org.apache.logging.log4j.LogManager ;
import org.apache.logging.log4j.Logger ;
2016-08-14 15:17:14 +02:00
import javafx.application.Application ;
import javafx.fxml.FXMLLoader ;
import javafx.scene.Scene ;
import javafx.scene.control.Alert ;
import javafx.scene.control.Alert.AlertType ;
import javafx.scene.control.ButtonType ;
import javafx.scene.image.Image ;
import javafx.scene.layout.AnchorPane ;
import javafx.stage.DirectoryChooser ;
import javafx.stage.Stage ;
public class Main extends Application {
2018-03-01 15:42:47 +01:00
private Stage primaryStage ;
private Scene scene ;
private AnchorPane pane ;
private MainWindowController mainWindowController ;
private static String userHome = System . getProperty ( " user.home " ) ;
private static String userName = System . getProperty ( " user.name " ) ;
private static String osName = System . getProperty ( " os.name " ) ;
private static String osArch = System . getProperty ( " os.arch " ) ;
private static String osVers = System . getProperty ( " os.version " ) ;
private static String javaVers = System . getProperty ( " java.version " ) ;
private static String javaVend = System . getProperty ( " java.vendor " ) ;
2018-03-01 16:10:37 +01:00
private String dirWin = userHome + " /Documents/HomeFlix " ; //Windows: C:/Users/"User"/Documents/HomeFlix
private String dirLinux = userHome + " /HomeFlix " ; //Linux: /home/"User"/HomeFlix
2018-03-01 15:42:47 +01:00
private File directory ;
private File configFile ;
private File posterCache ;
2016-08-14 15:17:14 +02:00
private String path ;
2017-04-06 12:07:34 +02:00
private String FONT_FAMILY = " System " ;
private String mode = " local " ; //local or streaming TODO
2017-02-09 20:39:43 +01:00
private String local = System . getProperty ( " user.language " ) + " _ " + System . getProperty ( " user.country " ) ;
2017-04-06 12:07:34 +02:00
private double FONT_SIZE = 17 ;
2017-02-09 20:39:43 +01:00
private ResourceBundle bundle ;
2017-09-16 18:22:46 +02:00
private static Logger LOGGER ;
2016-08-14 15:17:14 +02:00
@Override
2017-04-25 23:35:20 +02:00
public void start ( Stage primaryStage ) throws IOException {
2018-03-01 15:42:47 +01:00
LOGGER . info ( " OS: " + osName + " " + osVers + " " + osArch ) ;
LOGGER . info ( " Java: " + javaVend + " " + javaVers ) ;
LOGGER . info ( " User: " + userName + " " + userHome ) ;
2017-06-04 20:13:04 +02:00
this . primaryStage = primaryStage ;
2016-08-14 15:17:14 +02:00
mainWindow ( ) ;
}
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 ( ) ;
loader . setLocation ( ClassLoader . getSystemResource ( " 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 ) ;
2018-02-03 12:06:30 +01:00
primaryStage . setMinWidth ( 950 . 00 ) ;
2017-09-16 18:22:46 +02:00
primaryStage . setResizable ( false ) ;
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
2017-09-16 18:22:46 +02:00
mainWindowController = loader . getController ( ) ; //Link of FXMLController and controller class
mainWindowController . setMain ( this ) ; //call setMain
2018-03-01 15:42:47 +01:00
2017-09-16 18:22:46 +02:00
2018-03-01 15:42:47 +01:00
// get OS and the specific paths
2018-03-01 16:10:37 +01:00
if ( osName . equals ( " Windows " ) ) {
2017-09-16 18:22:46 +02:00
directory = new File ( dirWin ) ;
2018-03-01 15:42:47 +01:00
configFile = new File ( dirWin + " /config.xml " ) ;
posterCache = new File ( dirWin + " /posterCache " ) ;
2018-03-01 16:10:37 +01:00
} else {
directory = new File ( dirLinux ) ;
configFile = new File ( dirLinux + " /config.xml " ) ;
posterCache = new File ( dirLinux + " /posterCache " ) ;
2017-09-16 18:22:46 +02:00
}
2018-03-01 15:42:47 +01:00
// startup checks
if ( ! configFile . exists ( ) ) {
2017-09-16 18:22:46 +02:00
directory . mkdir ( ) ;
2018-03-04 20:07:43 +01:00
mainWindowController . addSource ( firstStart ( ) , " local " ) ;
2018-03-01 15:42:47 +01:00
mainWindowController . setColor ( " ee3523 " ) ;
2017-09-16 18:22:46 +02:00
mainWindowController . setSize ( FONT_SIZE ) ;
2018-03-01 15:42:47 +01:00
mainWindowController . setAutoUpdate ( false ) ;
2017-09-16 18:22:46 +02:00
mainWindowController . setLocal ( local ) ;
mainWindowController . setMode ( mode ) ;
mainWindowController . saveSettings ( ) ;
try {
2018-03-01 15:42:47 +01:00
Runtime . getRuntime ( ) . exec ( " java -jar ProjectHomeFlix.jar " ) ; // start again (preventing Bugs) TODO is this really needed
System . exit ( 0 ) ; // finishes it self
2017-09-16 18:22:46 +02:00
} catch ( Exception e ) {
LOGGER . error ( " error while restarting HomeFlix " , e ) ;
}
}
2018-03-01 15:42:47 +01:00
if ( ! posterCache . exists ( ) ) {
2017-09-16 18:22:46 +02:00
posterCache . mkdir ( ) ;
}
2018-03-01 15:42:47 +01:00
// generate window
scene = new Scene ( pane ) ; // create new scene, append pane to scene
2017-09-16 18:22:46 +02:00
scene . getStylesheets ( ) . add ( getClass ( ) . getResource ( " /css/MainWindow.css " ) . toExternalForm ( ) ) ;
2018-03-01 15:42:47 +01:00
primaryStage . setScene ( scene ) ; // append scene to stage
primaryStage . show ( ) ; // show stage
// init here as it loads the games to the mwc and the gui, therefore the window must exist
mainWindowController . init ( ) ;
mainWindowController . dbController . init ( ) ;
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-01 15:42:47 +01:00
// Method for first Start
2016-08-14 15:17:14 +02:00
private String firstStart ( ) {
2018-03-04 20:07:43 +01:00
switch ( System . getProperty ( " user.language " ) + " _ " + System . getProperty ( " user.country " ) ) {
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 ) ; // German
break ;
default :
bundle = ResourceBundle . getBundle ( " locals.HomeFlix-Local " , Locale . US ) ; // default local
break ;
}
2017-02-09 16:27:52 +01:00
Alert alert = new Alert ( AlertType . CONFIRMATION ) ; //new alert with file-chooser
2016-08-14 15:17:14 +02:00
alert . setTitle ( " Project HomeFlix " ) ;
2017-02-09 16:27:52 +01:00
alert . setHeaderText ( bundle . getString ( " firstStartHeader " ) ) ;
alert . setContentText ( bundle . getString ( " firstStartContent " ) ) ;
2016-08-14 15:17:14 +02:00
Optional < ButtonType > result = alert . showAndWait ( ) ;
if ( result . get ( ) = = ButtonType . OK ) {
DirectoryChooser directoryChooser = new DirectoryChooser ( ) ;
File selectedDirectory =
directoryChooser . showDialog ( primaryStage ) ;
path = selectedDirectory . getAbsolutePath ( ) ;
} else {
path = " " ;
}
return path ;
}
2017-02-09 20:39:43 +01:00
2016-08-14 15:17:14 +02:00
public static void main ( String [ ] args ) {
2018-03-01 16:10:37 +01:00
if ( System . getProperty ( " os.name " ) . equals ( " Windows " ) ) {
System . setProperty ( " logFilename " , userHome + " /Documents/HomeFlix/app.log " ) ;
File logFile = new File ( userHome + " /Documents/HomeFlix/app.log " ) ;
2017-09-16 18:22:46 +02:00
logFile . delete ( ) ;
} else {
2018-03-01 16:10:37 +01:00
System . setProperty ( " logFilename " , userHome + " /HomeFlix/app.log " ) ;
File logFile = new File ( userHome + " /HomeFlix/app.log " ) ;
2017-09-16 18:22:46 +02:00
logFile . delete ( ) ;
}
LOGGER = LogManager . getLogger ( Main . class . getName ( ) ) ;
2016-08-14 15:17:14 +02:00
launch ( args ) ;
}
2017-04-06 12:07:34 +02:00
2018-03-01 15:42:47 +01:00
public Stage getPrimaryStage ( ) {
return primaryStage ;
}
public void setPrimaryStage ( Stage primaryStage ) {
this . primaryStage = primaryStage ;
}
2018-03-02 13:50:21 +01:00
public AnchorPane getPane ( ) {
return pane ;
}
2018-03-01 15:42:47 +01:00
2017-04-06 12:07:34 +02:00
public String getFONT_FAMILY ( ) {
return FONT_FAMILY ;
}
public void setFONT_FAMILY ( String FONT_FAMILY ) {
this . FONT_FAMILY = FONT_FAMILY ;
}
2018-03-01 16:10:37 +01:00
public File getDirectory ( ) {
return directory ;
}
2017-04-09 15:07:56 +02:00
2018-03-01 16:10:37 +01:00
public File getConfigFile ( ) {
return configFile ;
2017-04-09 15:07:56 +02:00
}
2018-03-01 16:10:37 +01:00
public File getPosterCache ( ) {
return posterCache ;
2017-04-09 15:07:56 +02:00
}
2016-08-14 15:17:14 +02:00
}