diff options
| author | Stefan Suhren <suhren.stefan@fh-swf.de> | 2015-11-23 23:13:23 +0100 |
|---|---|---|
| committer | Stefan Suhren <suhren.stefan@fh-swf.de> | 2015-11-23 23:25:38 +0100 |
| commit | 83f14ed743a7dc7de1c072195da58aa38fc12905 (patch) | |
| tree | 457dd702bd5b3e98f6457295237fb29ea16ca2a9 /src/de/fhswf/in/inf | |
| parent | 90c302a97bdcafe71a1245f854bd4b6d36194a1d (diff) | |
| download | Notepad---83f14ed743a7dc7de1c072195da58aa38fc12905.tar.gz Notepad---83f14ed743a7dc7de1c072195da58aa38fc12905.zip | |
Add xml saving and loading
Diffstat (limited to 'src/de/fhswf/in/inf')
3 files changed, 163 insertions, 6 deletions
diff --git a/src/de/fhswf/in/inf/se/notepadMinusMinus/Main.java b/src/de/fhswf/in/inf/se/notepadMinusMinus/Main.java index 9481abe..55e550a 100644 --- a/src/de/fhswf/in/inf/se/notepadMinusMinus/Main.java +++ b/src/de/fhswf/in/inf/se/notepadMinusMinus/Main.java @@ -1,11 +1,21 @@ 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.Grade; +import de.fhswf.in.inf.se.notepadMinusMinus.model.Grades; import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; 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; @@ -14,13 +24,19 @@ public class Main extends Application private ObservableList<Grade> gradeList = FXCollections .observableArrayList(); + private Stage primaryStage; + @Override public void start(Stage primaryStage) { + this.primaryStage = primaryStage; + try { - BorderPane root = (BorderPane) FXMLLoader - .load(getClass().getResource("view/mainView.fxml")); + FXMLLoader loader = new FXMLLoader( + getClass().getResource("view/MainView.fxml")); + BorderPane root = (BorderPane) loader.load(); + Scene scene = new Scene(root); primaryStage.setScene(scene); primaryStage.setMaximized(true); @@ -41,4 +57,102 @@ public class Main extends Application { return gradeList; } + + 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; + } + } + + 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--"); + } + } + + 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); + + gradeList.clear(); + gradeList.addAll(wrapper.getGrades()); + + // Save the file path to the registry. + setGradesFilePath(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(); + } + } + + public void saveGradesToFile(File file) + { + try + { + JAXBContext context = JAXBContext.newInstance(Grades.class); + Marshaller m = context.createMarshaller(); + m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); + + // Wrapping our grades data. + Grades wrapper = new Grades(); + wrapper.setGrades(gradeList); + + // Marshalling and saving XML to the file. + m.marshal(wrapper, 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(); + } + } + + public void saveGradesToFile() + { + saveGradesToFile(getGradesFilePath()); + } } diff --git a/src/de/fhswf/in/inf/se/notepadMinusMinus/model/Grade.java b/src/de/fhswf/in/inf/se/notepadMinusMinus/model/Grade.java index 0b1528f..3e87d5c 100644 --- a/src/de/fhswf/in/inf/se/notepadMinusMinus/model/Grade.java +++ b/src/de/fhswf/in/inf/se/notepadMinusMinus/model/Grade.java @@ -3,8 +3,16 @@ */ package de.fhswf.in.inf.se.notepadMinusMinus.model; +import java.math.BigDecimal; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlValue; + import javafx.beans.property.IntegerProperty; +import javafx.beans.property.ObjectProperty; import javafx.beans.property.SimpleIntegerProperty; +import javafx.beans.property.SimpleObjectProperty; import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.StringProperty; @@ -14,6 +22,7 @@ import javafx.beans.property.StringProperty; * @author $Author: $ * @version $Revision: $, $Date: $ UTC */ +@XmlRootElement(name = "grade") public class Grade { private IntegerProperty semester = new SimpleIntegerProperty(); @@ -22,7 +31,7 @@ public class Grade private IntegerProperty ects = new SimpleIntegerProperty(); - private IntegerProperty grade = new SimpleIntegerProperty(); + private ObjectProperty<BigDecimal> grade = new SimpleObjectProperty<>(); private IntegerProperty attempt = new SimpleIntegerProperty(); @@ -31,6 +40,7 @@ public class Grade return this.semester; } + @XmlAttribute(name = "semester") public final int getSemester() { return this.semesterProperty().get(); @@ -46,6 +56,7 @@ public class Grade return this.modulename; } + @XmlAttribute(name = "modulename", required = true) public final String getModulename() { return this.modulenameProperty().get(); @@ -61,6 +72,7 @@ public class Grade return this.ects; } + @XmlAttribute(name = "ects") public final int getEcts() { return this.ectsProperty().get(); @@ -71,17 +83,18 @@ public class Grade this.ectsProperty().set(ects); } - public final IntegerProperty gradeProperty() + public final ObjectProperty<BigDecimal> gradeProperty() { return this.grade; } - public final int getGrade() + @XmlValue + public final BigDecimal getGrade() { return this.gradeProperty().get(); } - public final void setGrade(final int grade) + public final void setGrade(final BigDecimal grade) { this.gradeProperty().set(grade); } @@ -91,6 +104,7 @@ public class Grade return this.attempt; } + @XmlAttribute(name = "attempt") public final int getAttempt() { return this.attemptProperty().get(); diff --git a/src/de/fhswf/in/inf/se/notepadMinusMinus/model/Grades.java b/src/de/fhswf/in/inf/se/notepadMinusMinus/model/Grades.java new file mode 100644 index 0000000..9518aee --- /dev/null +++ b/src/de/fhswf/in/inf/se/notepadMinusMinus/model/Grades.java @@ -0,0 +1,29 @@ +package de.fhswf.in.inf.se.notepadMinusMinus.model; + +import java.util.ArrayList; +import java.util.List; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "grades") +public class Grades +{ + protected List<Grade> grades; + + @XmlElement(name = "grade", required = true) + public List<Grade> getGrades() + { + if (grades == null) + { + grades = new ArrayList<Grade>(); + } + return this.grades; + } + + public void setGrades(List<Grade> consumption) + { + this.grades = consumption; + } + +} |
