Commit eb24344a authored by Stefan Michel's avatar Stefan Michel

ContextMenu fuer den Hamster eingebaut, welche methoden aus der kompilierten...

ContextMenu fuer den Hamster eingebaut, welche methoden aus der kompilierten eigenen Hamster Klasse ausliest und die der hauptfunktionen hinzufuegt.
parent 219eb65d
......@@ -65,6 +65,7 @@ public class Main extends Application {
ioController.saveFile(terrain.getName());
});
activePrograms.add(terrain.getName());
ioController.compile(terrain.getName());
}
}
......@@ -78,6 +78,7 @@ public class IOController implements IInputOutputController {
Class<?> cls = cl.loadClass(name);
terrain.setHamster((Hamster) cls.newInstance());
mainViewCreationFX.buildContextMenu();
} catch (ClassNotFoundException | MalformedURLException | InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
......
......@@ -2,6 +2,7 @@ package model;
import model.exceptions.NoCornException;
import model.exceptions.WallException;
import view.Invisible;
/**
* Created by Stefan Michel on 23.10.17.
......@@ -58,10 +59,12 @@ public class Hamster {
terrain.turnLeft();
}
@Invisible
public Terrain getTerrain() {
return terrain;
}
@Invisible
public void setTerrain(Terrain terrain) {
this.terrain = terrain;
}
......
package view;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Invisible {
}
......@@ -12,13 +12,17 @@ import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import model.Hamster;
import model.Terrain;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
......@@ -39,6 +43,7 @@ public class MainViewCreationFX extends VBox {
private IHamsterController hamsterController;
private Terrain terrain;
private TextArea textArea;
private ContextMenu contextMenu;
public MainViewCreationFX(Terrain terrain, ITerrainController terrainController, IInputOutputController ioController,
IHamsterController hamsterController) {
......@@ -50,13 +55,9 @@ public class MainViewCreationFX extends VBox {
//Territory Pane erstellen
territoriumPanel = new TerritoriumPanelFX(terrain, terrainController);
// hamsterContextMenu
ContextMenu contextMenu = new ContextMenu();
contextMenu.getItems().add(new MenuItem("Moin"));
territoriumPanel.getHamster().setOnContextMenuRequested(event -> {
contextMenu.show(territoriumPanel.getHamster(), Side.RIGHT, 0, 0);
event.consume();
});
contextMenu = new ContextMenu();
buildContextMenu();
ScrollPane terrainPane = new ScrollPane(territoriumPanel);
......@@ -101,6 +102,44 @@ public class MainViewCreationFX extends VBox {
ioController.setMainViewCreationFX(this);
}
public void buildContextMenu() {
contextMenu = new ContextMenu();
setUpContextMenu(Hamster.class);
setUpContextMenu(terrain.getHamster().getClass());
}
private void setUpContextMenu(Class<?> hamsterClass) {
// lade klasse
List<Method> methods = new ArrayList(Arrays.asList(hamsterClass.getDeclaredMethods()));
// hamsterContextMenu
methods.forEach(method -> {
if (method.getAnnotation(Invisible.class) == null) {
String returnType = method.getReturnType().getSimpleName();
String methodName = method.getName();
Class<?>[] parameterTypes = method.getParameterTypes();
StringBuffer result = new StringBuffer();
result.append(returnType + " " + methodName + "(");
for (int i = 0; i < parameterTypes.length; i++) {
result.append(parameterTypes[i].getSimpleName());
if (i < parameterTypes.length - 1) {
result.append(", ");
}
}
result.append(")");
contextMenu.getItems().add(new MenuItem(result.toString()));
}
});
territoriumPanel.getHamster().setOnContextMenuRequested(event -> {
contextMenu.show(territoriumPanel.getHamster(), Side.RIGHT, 0, 0);
});
}
public void setUpTextArea() {
try {
Path file = Paths.get("programme", terrain.getName() + ".java");
......
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