Commit 494edad1 authored by Stefan Michel's avatar Stefan Michel

- man kann Terrains serialisieren und unter verschiedenen namen speichern

parent 112c3f8f
......@@ -30,21 +30,24 @@ public class Main extends Application {
}
public static void startNewStage(String name) {
Terrain terrain = new Terrain();
startNewStage(name, new Terrain());
}
public static void startNewStage(String name, Terrain terrain) {
TerrainController terrainController = new TerrainController(terrain);
IOController ioController = new IOController(terrain);
HamsterController hamsterController = new HamsterController(terrain);
ThreadManager threadManager = new ThreadManager(terrain, ioController);
terrain.setName(name);
terrain.setHamsterName(name);
Stage newStage = new Stage();
Scene scene = new Scene(new MainViewCreationFX(terrain, terrainController, ioController, hamsterController, threadManager), 1000, 500);
newStage.setScene(scene);
newStage.setTitle(terrain.getName());
newStage.setTitle(terrain.getHamsterName());
newStage.show();
newStage.setOnCloseRequest(event -> {
activePrograms.remove(terrain.getName());
ioController.saveFile(terrain.getName());
activePrograms.remove(terrain.getHamsterName());
ioController.saveFile(terrain.getHamsterName());
});
}
......@@ -67,14 +70,14 @@ public class Main extends Application {
public void start(Stage primaryStage) {
Scene scene = new Scene(new MainViewCreationFX(terrain, terrainController, ioController, hamsterController, threadManager), 1000, 500);
primaryStage.setScene(scene);
primaryStage.setTitle(terrain.getName());
primaryStage.setTitle(terrain.getHamsterName());
primaryStage.show();
primaryStage.setOnCloseRequest(event -> {
activePrograms.remove(terrain.getName());
ioController.saveFile(terrain.getName());
activePrograms.remove(terrain.getHamsterName());
ioController.saveFile(terrain.getHamsterName());
});
activePrograms.add(terrain.getName());
ioController.compile(terrain.getName());
activePrograms.add(terrain.getHamsterName());
ioController.compile(terrain.getHamsterName());
}
// von https://stackoverflow.com/questions/26305/how-can-i-play-sound-in-java
......
......@@ -6,7 +6,6 @@ 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;
......@@ -15,7 +14,7 @@ public class SetFileNameDialogBox {
private IOController ioController;
public SetFileNameDialogBox(Terrain terrain, IOController ioController) {
public SetFileNameDialogBox(IOController ioController) {
this.ioController = ioController;
setFileNameDialogBox();
}
......@@ -76,7 +75,8 @@ public class SetFileNameDialogBox {
result.ifPresent(res -> {
Main.startNewStage(res);
ioController.saveFile(res);
if (ioController != null)
ioController.saveFile(res);
Main.activePrograms.add(res);
});
......
package controller;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import java.io.File;
import java.util.NoSuchElementException;
import java.util.Optional;
public class SetTerrainNameDialogBox {
/* angepasst von http://code.makery.ch/blog/javafx-dialogs-official/ */
public String setTerrainNameDialogBox() {
Dialog<String> dialog = new Dialog<>();
dialog.setTitle("setze einen Dateinamen");
dialog.setHeaderText("Setzen Sie einen Dateinamen fuer das Terrain");
// Set the button types.
ButtonType okayButtonType = new ButtonType("Bestaetigen", ButtonBar.ButtonData.OK_DONE);
ButtonType cancelButtonType = new ButtonType("Abbrechen", ButtonBar.ButtonData.CANCEL_CLOSE);
dialog.getDialogPane().getButtonTypes().addAll(okayButtonType, cancelButtonType);
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(20, 150, 10, 10));
TextField filename = new TextField();
filename.setPromptText("Dateiname");
grid.add(new Label("Dateiname: "), 0, 0);
grid.add(filename, 1, 0);
Label error = new Label("");
grid.add(error, 0, 1);
// Enable/Disable login button depending on whether a username was entered.
Node okayButton = dialog.getDialogPane().lookupButton(okayButtonType);
okayButton.setDisable(true);
filename.textProperty().addListener((observable, oldValue, newValue) -> {
File file = new File("terrains/" + newValue + ".bin");
if (file.exists()) {
okayButton.setDisable(true);
error.setText("existiert bereits!");
} else {
okayButton.setDisable(newValue.trim().isEmpty());
error.setText("");
}
});
dialog.getDialogPane().setContent(grid);
// Request focus on the username field by default.
Platform.runLater(filename::requestFocus);
dialog.setResultConverter(dialogButton -> {
if (dialogButton == okayButtonType) {
return filename.getText();
}
return null;
});
Optional<String> result = null;
try {
result = dialog.showAndWait();
return result.get();
} catch (NoSuchElementException e) {
return null;
}
}
}
......@@ -2,8 +2,15 @@ package controller;
import model.Terrain;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
/**
* Created by Stefan Michel on 13.11.17.
* <p>
* behandelt die veraenderung, serialisierung, speicherung und laden des Terrains
*/
public class TerrainController {
......@@ -32,4 +39,29 @@ public class TerrainController {
public void deleteTile(int col, int row) {
terrain.deleteTile(col, row);
}
public void serializeTerrain() {
String terrainName = new SetTerrainNameDialogBox().setTerrainNameDialogBox();
if (terrainName == null)
return;
try (FileOutputStream fileOutputStream = new FileOutputStream("terrains/" + terrainName + ".bin")) {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(terrain);
objectOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void deserializeTerrain() {
}
}
......@@ -5,12 +5,13 @@ import model.exceptions.BorderException;
import model.exceptions.NoCornException;
import model.exceptions.WallException;
import java.io.Serializable;
import java.util.Observable;
/**
* Created by Stefan Michel on 23.10.17.
*/
public class Terrain extends Observable {
public class Terrain extends Observable implements Serializable {
public final static int NORTH = 0;
public final static int EAST = 1;
......@@ -19,9 +20,10 @@ public class Terrain extends Observable {
final static int WALL = -1;
public final static int MAXCORNS = 9;
private final static int MINTILES = 5;
private String name = "DefaultTerrain";
private Hamster hamster;
private String name = "DefaultHamster";
private transient Hamster hamster;
private String hamsterName = "DefaultHamster";
int maxCols = 10;
int maxRows = 10;
......@@ -32,7 +34,7 @@ public class Terrain extends Observable {
int hamsterCorns = 0;
int[][] map;
private double speed;
private double speed = 450;
public Terrain() {
......@@ -237,12 +239,12 @@ public class Terrain extends Observable {
return this.hamsterDirection;
}
public String getName() {
return name;
public String getHamsterName() {
return hamsterName;
}
public void setName(String name) {
this.name = name;
public void setHamsterName(String hamsterName) {
this.hamsterName = hamsterName;
}
public Hamster getHamster() {
......@@ -276,4 +278,8 @@ public class Terrain extends Observable {
public void setSpeed(double speed) {
this.speed = speed;
}
public String getName() {
return name;
}
}
......@@ -15,7 +15,7 @@ public class ThreadManager {
}
public void startThread() {
ioController.compile(terrain.getName());
ioController.compile(terrain.getHamsterName());
terrain.getHamster().setDaemon(true);
terrain.getHamster().start();
......
......@@ -35,10 +35,7 @@ public class MainViewCreationFX extends VBox implements Observer {
private final MenuBar menuBar = new MenuBar();
private final ToolBar toolBar = new ToolBar();
RadioMenuItem hamsterMenuItem;
RadioMenuItem cornMenuItem;
RadioMenuItem wallMenuItem;
RadioMenuItem deleteMenuItem;
private RadioMenuItem hamsterMenuItem, cornMenuItem, wallMenuItem, deleteMenuItem;
private TerritoriumPanelFX territoriumPanel;
private TerrainController terrainController;
private IOController ioController;
......@@ -48,9 +45,7 @@ public class MainViewCreationFX extends VBox implements Observer {
private TextArea textArea;
private ContextMenu contextMenu;
private ThreadManager threadManager;
private Button start;
private Button pause;
private Button stopp;
private MenuItem start, pause, stop;
private Slider slider;
public MainViewCreationFX(Terrain terrain, TerrainController terrainController, IOController ioController,
......@@ -156,7 +151,7 @@ public class MainViewCreationFX extends VBox implements Observer {
public void setUpTextArea() {
try {
Path file = Paths.get("programme", terrain.getName() + ".java");
Path file = Paths.get("programme", terrain.getHamsterName() + ".java");
if (file != null) {
List<String> strings = Files.readAllLines(file);
strings.remove(0);
......@@ -196,7 +191,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(terrain, ioController));
neu.setOnAction(event -> new SetFileNameDialogBox(ioController));
MenuItem oeffnen = new MenuItem("_Öffnen", new ImageView(new Image("/Open16.gif")));
oeffnen.setAccelerator(new KeyCodeCombination(KeyCode.O, KeyCombination.CONTROL_DOWN));
......@@ -205,7 +200,7 @@ public class MainViewCreationFX extends VBox implements Observer {
MenuItem kompilieren = new MenuItem("_Kompilieren");
kompilieren.setAccelerator(new KeyCodeCombination(KeyCode.K, KeyCombination.CONTROL_DOWN));
kompilieren.setOnAction(event -> ioController.compile(terrain.getName()));
kompilieren.setOnAction(event -> ioController.compile(terrain.getHamsterName()));
MenuItem drucken = new MenuItem("_Drucken", new ImageView(new Image("/Print16.gif")));
drucken.setAccelerator(new KeyCodeCombination(KeyCode.P, KeyCombination.CONTROL_DOWN));
......@@ -213,7 +208,7 @@ public class MainViewCreationFX extends VBox implements Observer {
MenuItem beenden = new MenuItem("_Beenden");
beenden.setAccelerator(new KeyCodeCombination(KeyCode.P, KeyCombination.CONTROL_DOWN));
beenden.setOnAction(event -> {
ioController.saveFile(terrain.getName());
ioController.saveFile(terrain.getHamsterName());
Stage stage = (Stage) getScene().getWindow();
stage.close();
});
......@@ -234,6 +229,7 @@ public class MainViewCreationFX extends VBox implements Observer {
speichern.getItems().addAll(xml, jaxb, seri, java);
java.setOnAction(event -> ioController.saveFile("name"));
seri.setOnAction(event -> terrainController.serializeTerrain());
Menu laden = new Menu("_Laden");
MenuItem xmlladen = new MenuItem("_XML");
......@@ -296,13 +292,19 @@ public class MainViewCreationFX extends VBox implements Observer {
private Menu buildSimulationMenu() {
MenuItem start = new MenuItem("_Start/Fortsetzen", new ImageView(new Image("/Play16.gif")));
start = new MenuItem("_Start/Fortsetzen", new ImageView(new Image("/Play16.gif")));
start.setAccelerator(new KeyCodeCombination(KeyCode.F11, KeyCombination.CONTROL_DOWN));
MenuItem pause = new MenuItem("_Pause", new ImageView(new Image("/Pause16.gif")));
pause = new MenuItem("_Pause", new ImageView(new Image("/Pause16.gif")));
pause.setDisable(true);
MenuItem stop = new MenuItem("_Stop", new ImageView(new Image("/Stop16.gif")));
stop = new MenuItem("_Stop", new ImageView(new Image("/Stop16.gif")));
stop.setAccelerator(new KeyCodeCombination(KeyCode.F12, KeyCombination.CONTROL_DOWN));
stop.setDisable(true);
start.setOnAction(event -> threadManager.startThread());
pause.setOnAction(event -> threadManager.pauseThread());
stop.setOnAction(event -> threadManager.stopThread());
return new Menu("_Simulation", null, start, pause, stop);
}
......@@ -335,16 +337,16 @@ 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(terrain, ioController));
neu.setOnAction(event -> new SetFileNameDialogBox(ioController));
Button open = new Button(null, new ImageView(new Image("/Open24.gif")));
open.setTooltip(new Tooltip("Öffnen"));
open.setOnAction(event -> this.openFileChooser());
// speichern und kompilieren
Button speichern = new Button(null, new ImageView(new Image("/Save24.gif")));
speichern.setOnAction(event -> ioController.saveFile(terrain.getName()));
speichern.setOnAction(event -> ioController.saveFile(terrain.getHamsterName()));
Button kompilieren = new Button(null, new ImageView(new Image("/Compile24.gif")));
kompilieren.setOnAction(event -> ioController.compile(terrain.getName()));
kompilieren.setOnAction(event -> ioController.compile(terrain.getHamsterName()));
// Territorium
ToggleGroup territoriumToggle = new ToggleGroup();
......@@ -387,17 +389,21 @@ public class MainViewCreationFX extends VBox implements Observer {
gib.setOnAction(event -> this.hamsterController.putDown());
// Simulation
start = new Button(null, new ImageView(new Image("/Play24.gif")));
pause = new Button(null, new ImageView(new Image("/Pause24.gif")));
stopp = new Button(null, new ImageView(new Image("/Stop24.gif")));
Button start = new Button(null, new ImageView(new Image("/Play24.gif")));
Button pause = new Button(null, new ImageView(new Image("/Pause24.gif")));
Button stop = new Button(null, new ImageView(new Image("/Stop24.gif")));
pause.setDisable(true);
stopp.setDisable(true);
stop.setDisable(true);
start.setOnAction(event -> threadManager.startThread());
pause.setOnAction(event -> threadManager.pauseThread());
stopp.setOnAction(event -> threadManager.stopThread());
stop.setOnAction(event -> threadManager.stopThread());
start.disableProperty().bindBidirectional(this.start.disableProperty());
pause.disableProperty().bindBidirectional(this.pause.disableProperty());
stop.disableProperty().bindBidirectional(this.stop.disableProperty());
// Slider
slider = new Slider(0, 900, 450);
......@@ -406,7 +412,7 @@ public class MainViewCreationFX extends VBox implements Observer {
// zusammensetzen und returnen
toolBar.getItems().addAll(neu, open, new Separator(), speichern, kompilieren,
new Separator(), territorium, hamster, corn, wall, delete, new Separator(),
kornImMaul, linksUm, vor, nimm, gib, new Separator(), start, pause, stopp,
kornImMaul, linksUm, vor, nimm, gib, new Separator(), start, pause, stop,
new Separator(), slider);
}
......@@ -437,11 +443,15 @@ public class MainViewCreationFX extends VBox implements Observer {
if (terrain.isRunningThread()) {
start.setDisable(true);
slider.setDisable(true);
stopp.setDisable(false);
stop.setDisable(false);
} else {
start.setDisable(false);
slider.setDisable(false);
stopp.setDisable(true);
stop.setDisable(true);
}
}
public void setTerrain(Terrain terrain) {
this.terrain = terrain;
}
}
......@@ -113,6 +113,10 @@ public class TerritoriumPanelFX extends Region implements Observer {
}
}
public void setTerrain(Terrain terrain) {
this.terrain = terrain;
}
public ImageView getHamster() {
return hamster;
}
......
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