Commit af705ee8 authored by Stefan Michel's avatar Stefan Michel

- Serialisierung und Deserialisierung funktioniert jetzt

parent 494edad1
......@@ -39,6 +39,7 @@ public class Main extends Application {
HamsterController hamsterController = new HamsterController(terrain);
ThreadManager threadManager = new ThreadManager(terrain, ioController);
terrain.setHamsterName(name);
ioController.setUpHamster(name);
Stage newStage = new Stage();
Scene scene = new Scene(new MainViewCreationFX(terrain, terrainController, ioController, hamsterController, threadManager), 1000, 500);
......
......@@ -58,7 +58,7 @@ public class IOController {
public void openFile(String name) {
Main.startNewStage(name);
Main.startNewStage(name, terrain);
}
......@@ -71,21 +71,24 @@ public class IOController {
if (!success) {
new Alert(Alert.AlertType.ERROR, err.toString()).showAndWait();
} else {
try {
File path = new File("./programme/");
URL[] urls = new URL[]{path.toURI().toURL()};
ClassLoader cl = new URLClassLoader(urls);
Class<?> cls = cl.loadClass(name);
terrain.setHamster((Hamster) cls.newInstance());
mainViewCreationFX.buildContextMenu();
} catch (ClassNotFoundException | MalformedURLException | InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
setUpHamster(name);
mainViewCreationFX.setFooterText("Kompilieren erfolgreich");
}
}
public void setUpHamster(String name) {
try {
File path = new File("./programme/");
URL[] urls = new URL[]{path.toURI().toURL()};
ClassLoader cl = new URLClassLoader(urls);
Class<?> cls = cl.loadClass(name);
terrain.setHamster((Hamster) cls.newInstance());
} catch (ClassNotFoundException | MalformedURLException | InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
}
// abgeleitet von http://winterbe.com/posts/2015/03/25/java8-examples-string-number-math-files/
private void writeFile(Path file) {
......
......@@ -6,6 +6,7 @@ import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import model.Terrain;
import java.io.File;
import java.util.Optional;
......@@ -14,13 +15,14 @@ public class SetFileNameDialogBox {
private IOController ioController;
public SetFileNameDialogBox(IOController ioController) {
public SetFileNameDialogBox(IOController ioController, Terrain terrain) {
this.ioController = ioController;
setFileNameDialogBox();
setFileNameDialogBox(terrain);
}
/* angepasst von http://code.makery.ch/blog/javafx-dialogs-official/ */
private void setFileNameDialogBox() {
private void setFileNameDialogBox(Terrain terrain) {
Dialog<String> dialog = new Dialog<>();
dialog.setTitle("setze einen Dateinamen");
dialog.setHeaderText("Setzen Sie einen Dateinamen \n (muss mit einem Buchstaben beginnen, \n" +
......@@ -74,7 +76,7 @@ public class SetFileNameDialogBox {
Optional<String> result = dialog.showAndWait();
result.ifPresent(res -> {
Main.startNewStage(res);
Main.startNewStage(res, terrain);
if (ioController != null)
ioController.saveFile(res);
Main.activePrograms.add(res);
......
package controller;
import application.Main;
import model.Terrain;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.*;
/**
* Created by Stefan Michel on 13.11.17.
......@@ -60,8 +58,32 @@ public class TerrainController {
}
}
public void deserializeTerrain() {
public boolean deserializeTerrain(String name) {
if (name == null)
return false;
Terrain terrain = null;
try (FileInputStream fileOutputStream = new FileInputStream(name)) {
ObjectInputStream objectInputStream = new ObjectInputStream(fileOutputStream);
terrain = (Terrain) objectInputStream.readObject();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
if (terrain != null) {
Main.startNewStage(terrain.getHamsterName(), terrain);
return true;
}
return false;
}
}
......@@ -11,6 +11,7 @@ import javafx.scene.input.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Stage;
import model.Hamster;
import model.Invisible;
......@@ -191,7 +192,7 @@ public class MainViewCreationFX extends VBox implements Observer {
//MenuItems werden mit Grafiken und Accelelatoren gebaut
MenuItem neu = new MenuItem("_Neu", new ImageView(new Image("/New16.gif")));
neu.setAccelerator(new KeyCodeCombination(KeyCode.N, KeyCombination.CONTROL_DOWN));
neu.setOnAction(event -> new SetFileNameDialogBox(ioController));
neu.setOnAction(event -> new SetFileNameDialogBox(ioController, terrain));
MenuItem oeffnen = new MenuItem("_Öffnen", new ImageView(new Image("/Open16.gif")));
oeffnen.setAccelerator(new KeyCodeCombination(KeyCode.O, KeyCombination.CONTROL_DOWN));
......@@ -237,6 +238,8 @@ public class MainViewCreationFX extends VBox implements Observer {
MenuItem seriladen = new MenuItem("_Deserialisieren");
laden.getItems().addAll(xmlladen, jaxbladen, seriladen);
seriladen.setOnAction(event -> openTerrainFileChooser());
Menu bildspeichern = new Menu("_Als Bild speichern");
MenuItem jpg = new MenuItem("_JPEG");
MenuItem png = new MenuItem("_PNG");
......@@ -314,6 +317,7 @@ public class MainViewCreationFX extends VBox implements Observer {
*/
public void openFileChooser() {
FileChooser fileChooser = new FileChooser();
fileChooser.getExtensionFilters().add(new ExtensionFilter("Java Files", "*.java"));
File file = fileChooser.showOpenDialog(this.getScene().getWindow());
if (file != null) {
// StringBuilder schneller, da nicht synchronized
......@@ -328,6 +332,22 @@ public class MainViewCreationFX extends VBox implements Observer {
}
}
public void openTerrainFileChooser() {
FileChooser fileChooser = new FileChooser();
fileChooser.getExtensionFilters().add(new ExtensionFilter("Binary Files", "*.bin"));
File file = fileChooser.showOpenDialog(this.getScene().getWindow());
if (file != null) {
// StringBuilder schneller, da nicht synchronized
StringBuilder cutName = new StringBuilder(file.getName());
cutName.delete(cutName.length() - 5, cutName.length());
if (terrainController.deserializeTerrain(file.toString())) {
Stage stage = (Stage) this.getScene().getWindow();
stage.close();
}
}
}
// ------------------------
// ToolBar Build
// ------------------------
......@@ -337,7 +357,7 @@ public class MainViewCreationFX extends VBox implements Observer {
// neu und open
Button neu = new Button(null, new ImageView(new Image("/New24.gif")));
neu.setTooltip(new Tooltip("Neu"));
neu.setOnAction(event -> new SetFileNameDialogBox(ioController));
neu.setOnAction(event -> new SetFileNameDialogBox(ioController, terrain));
Button open = new Button(null, new ImageView(new Image("/Open24.gif")));
open.setTooltip(new Tooltip("Öffnen"));
open.setOnAction(event -> this.openFileChooser());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment