package de.fhswf.in.inf.se.notepadMinusMinus; import java.io.File; import java.util.prefs.Preferences; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import de.fhswf.in.inf.se.notepadMinusMinus.model.Grades; import de.fhswf.in.inf.se.notepadMinusMinus.view.MainViewController; 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.layout.BorderPane; import javafx.stage.Stage; /** * The main {@link File} and {@link Stage} handling class. * * @author Stefan Suhren * @version 1.0 */ public class Main extends Application { private Grades grades = new Grades(); private Stage primaryStage; private File openFile; /* * (non-Javadoc) * * @see javafx.application.Application#start(javafx.stage.Stage) */ @Override public void start(Stage primaryStage) { this.primaryStage = primaryStage; try { FXMLLoader loader = new FXMLLoader( getClass().getResource("view/MainView.fxml")); BorderPane root = (BorderPane) loader.load(); Scene scene = new Scene(root); primaryStage.setScene(scene); MainViewController controller = loader.getController(); controller.setMain(this); primaryStage.setMaximized(true); primaryStage.show(); } catch (Exception e) { e.printStackTrace(); } } /** * Start the JavaFX {@link Application}. * * @param args * The command line arguments. */ public static void main(String[] args) { launch(args); } /** * Get the global data class. * * @return The global {@link Grades} object. */ public Grades getGradesObject() { return grades; } /** * Get primary {@link Stage} for various things. * * @return The primary {@link Stage} of the {@link Application}. */ public Stage getPrimaryStage() { return primaryStage; } /** * Get the {@link File} that is opened right now. * * @return The open {@link File} or null if no {@link File} is open. */ public File getOpenFile() { return openFile; } /** * Set the {@link File} that is open right now. * * @param openFile * The open {@link File} or null if the {@link File} is not * opened. */ public void setOpenFile(File openFile) { this.openFile = openFile; } /** * Get the last opened {@link File}. * * @return The last location as a {@link File}. */ public File getGradesFilePath() { Preferences prefs = Preferences.userNodeForPackage(Main.class); String filePath = prefs.get("filePath", null); if (filePath != null) { return new File(filePath); } else { return null; } } /** * Set the last opened {@link File} location. * * @param file * The last location as a {@link File}. */ public void setGradesFilePath(File file) { Preferences prefs = Preferences.userNodeForPackage(Main.class); if (file != null) { prefs.put("filePath", file.getPath()); // Update the stage title. primaryStage.setTitle("Notepad-- - " + file.getName()); } else { prefs.remove("filePath"); // Update the stage title. primaryStage.setTitle("Notepad--"); } } /** * Load data from {@link File}. * * @param file * The {@link File} to open and read. */ public void loadGradesFromFile(File file) { try { JAXBContext context = JAXBContext.newInstance(Grades.class); Unmarshaller um = context.createUnmarshaller(); // Reading XML from the file and unmarshalling. Grades wrapper = (Grades) um.unmarshal(file); grades.setGrades(wrapper.getGrades()); grades.setColloquium(wrapper.getColloquium()); grades.setThesis(wrapper.getThesis()); // Save the file path to the registry. setGradesFilePath(file); // Save file in use. setOpenFile(file); } catch (Exception e) { // catches ANY exception Alert alert = new Alert(AlertType.ERROR); alert.initOwner(primaryStage); alert.setTitle("Fehler"); alert.setHeaderText("Konnte Noten nicht laden."); alert.setContentText( "Konnte Noten nicht aus folgender Datei laden:\n" + file.getPath()); alert.showAndWait(); } } /** * Save data to {@link File}. * * @param file * The {@link File} to open and write to. */ public void saveGradesToFile(File file) { try { JAXBContext context = JAXBContext.newInstance(Grades.class); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // Marshalling and saving XML to the file. m.marshal(grades, file); // Save the file path to the registry. setGradesFilePath(file); // Save file in use. setOpenFile(file); } catch (Exception e) { // catches ANY exception Alert alert = new Alert(AlertType.ERROR); alert.initOwner(primaryStage); alert.setTitle("Fehler"); alert.setHeaderText("Konnte Noten nicht speichern."); alert.setContentText( "Konnte Noten nicht in folgende Datei speichern:\n" + file.getPath()); alert.showAndWait(); } } }