summaryrefslogtreecommitdiffstats
path: root/src/de/fhswf/in/inf/se/notepadMinusMinus/Main.java
blob: da0e06d0f015ca05654d155f017a78469d01566f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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 de.fhswf.in.inf.se.notepadMinusMinus.view.MainViewController;
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;

public class Main extends Application
{
   private ObservableList<Grade> gradeList = FXCollections
         .observableArrayList();

   private Stage primaryStage;

   private File openFile;

   @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();
      }
   }

   public static void main(String[] args)
   {
      launch(args);
   }

   public ObservableList<Grade> getGradeList()
   {
      return gradeList;
   }

   public Stage getPrimaryStage()
   {
      return primaryStage;
   }

   public File getOpenFile()
   {
      return openFile;
   }

   public void setOpenFile(File openFile)
   {
      this.openFile = openFile;
   }

   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);

         // 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();
      }
   }

   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);

         // 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 speichern.");
         alert.setContentText(
               "Konnte Noten nicht in folgende Datei speichern:\n"
                     + file.getPath());

         alert.showAndWait();
      }
   }
}