mirror of
https://codeberg.org/qg-info-unterricht/zpg-graphentester.git
synced 2026-03-25 04:58:24 +01:00
First Commit (Fobi)
This commit is contained in:
commit
2bff291a51
336 changed files with 88781 additions and 0 deletions
280
control/SimulationTabMitController.java
Normal file
280
control/SimulationTabMitController.java
Normal file
|
|
@ -0,0 +1,280 @@
|
|||
package control;
|
||||
|
||||
import imp.*;
|
||||
import graph.*;
|
||||
import algorithmen.*;
|
||||
|
||||
import javafx.fxml.*;
|
||||
import javafx.scene.control.*;
|
||||
import javafx.event.*;
|
||||
import javafx.scene.input.MouseEvent;
|
||||
import javafx.scene.layout.*;
|
||||
import javafx.scene.Node;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.text.*;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.image.Image;
|
||||
import javafx.stage.*; // Dateiöffnen / Speichern-Dialog
|
||||
import java.io.File;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.file.*;
|
||||
import java.util.Collections;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.Iterator;
|
||||
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javafx.collections.ObservableList;
|
||||
|
||||
/**
|
||||
* Die Klasse SimulationTabMitController stellt einen Tab inclusive ihres Controllers
|
||||
* zur Simulation eines Algorithmus dar. Der Algorithmus kann ausgewählt und schrittweise
|
||||
* durchgeführt werden.
|
||||
*
|
||||
* @author Thomas Schaller
|
||||
* @version v6.7 (9.12.2020)
|
||||
*/
|
||||
public class SimulationTabMitController extends TabMitController {
|
||||
|
||||
@FXML
|
||||
private ComboBox<String> cbAlgorithmen;
|
||||
|
||||
@FXML
|
||||
private Slider sSpeed;
|
||||
|
||||
@FXML
|
||||
private Button bStep;
|
||||
|
||||
@FXML
|
||||
private Button bStart;
|
||||
|
||||
@FXML
|
||||
private Button bBreak;
|
||||
|
||||
@FXML
|
||||
private Button bReset;
|
||||
|
||||
private GraphAlgo aktAlgo = null;
|
||||
private ArrayList<String> algoNamen;
|
||||
private Hilfefenster hilfe;
|
||||
|
||||
public SimulationTabMitController(Graph graph, GraphOptions options) {
|
||||
this.graph = graph;
|
||||
this.options = options;
|
||||
this.setOnClosed((ev)->afterClosing());
|
||||
}
|
||||
|
||||
public void initialize() {
|
||||
this.algoNamen = new ArrayList<String>();
|
||||
try {
|
||||
File verzeichnis = new File("algorithmen");
|
||||
if(verzeichnis != null) {
|
||||
String[] entries = verzeichnis.list();
|
||||
for (String name : entries) {
|
||||
if (name.startsWith("GraphAlgo_") && name.endsWith(".class")){
|
||||
Class c = Class.forName("algorithmen."+name.split(Pattern.quote("."))[0]);
|
||||
GraphAlgo a = ((GraphAlgo)(c).getDeclaredConstructor().newInstance());
|
||||
int i = 0;
|
||||
while(i < cbAlgorithmen.getItems().size() && cbAlgorithmen.getItems().get(i).compareTo(a.getBezeichnung())<0)
|
||||
i++;
|
||||
//System.out.println("Algorithmus: "+a.getBezeichnung()+" geladen.");
|
||||
cbAlgorithmen.getItems().add(i, a.getBezeichnung());
|
||||
|
||||
algoNamen.add(i, "algorithmen."+name.split(Pattern.quote("."))[0]);
|
||||
}
|
||||
} // end of for
|
||||
}
|
||||
verzeichnis = new File( "eigeneAlgorithmen" );
|
||||
if(verzeichnis != null) {
|
||||
String[] entries = verzeichnis.list();
|
||||
for (String name : entries) {
|
||||
if (name.startsWith("GraphAlgo_") && name.endsWith(".class")){
|
||||
Class c = Class.forName("eigeneAlgorithmen."+name.split(Pattern.quote("."))[0]);
|
||||
GraphAlgo a = ((GraphAlgo)(c).getDeclaredConstructor().newInstance());
|
||||
int i = 0;
|
||||
while(i < cbAlgorithmen.getItems().size() && cbAlgorithmen.getItems().get(i).compareTo(a.getBezeichnung())<0)
|
||||
i++;
|
||||
//System.out.println("Algorithmus: "+a.getBezeichnung()+" geladen.");
|
||||
cbAlgorithmen.getItems().add(i, a.getBezeichnung());
|
||||
|
||||
algoNamen.add(i, "eigeneAlgorithmen."+name.split(Pattern.quote("."))[0]);
|
||||
}
|
||||
} // end of for
|
||||
}
|
||||
} catch(Exception e)
|
||||
{
|
||||
System.out.println("Exception " + e);
|
||||
}
|
||||
|
||||
viewer.setGraph(graph,options);
|
||||
this.hilfe = null;
|
||||
this.aktAlgo = null;
|
||||
super.initialize();
|
||||
|
||||
sSpeed.valueProperty().addListener(e -> { if (aktAlgo != null) aktAlgo.setSpeed(910-(int) (sSpeed.getValue()));});
|
||||
bStart.managedProperty().bind(bStart.visibleProperty());
|
||||
bBreak.managedProperty().bind(bBreak.visibleProperty());
|
||||
bBreak.setVisible(false);
|
||||
}
|
||||
|
||||
private void afterClosing() {
|
||||
if (hilfe != null) hilfe.close();
|
||||
}
|
||||
|
||||
private void hilfefensterErzeugen() {
|
||||
try { // try-catch ist notwendig, um Fehler durch fehlende Dateien abzufangen.
|
||||
if(hilfe != null) hilfe.close();
|
||||
|
||||
hilfe = new Hilfefenster();
|
||||
FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/hilfefenster.fxml"));
|
||||
loader.setController(hilfe);
|
||||
Scene scene = new Scene((VBox) loader.load());
|
||||
Image icon = new Image("/view/icon.png");
|
||||
hilfe.getIcons().add(icon);
|
||||
hilfe.setTitle("Hilfefenster");
|
||||
hilfe.setScene(scene);
|
||||
hilfe.setX(0);
|
||||
hilfe.setY(0);
|
||||
}
|
||||
catch(Exception e) {
|
||||
System.out.println(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void showHilfe(boolean b) {
|
||||
if(b) {
|
||||
hilfefensterErzeugen();
|
||||
hilfe.show();
|
||||
if(aktAlgo != null && aktAlgo.isAlive()) {
|
||||
aktAlgo.setGUIElemente(viewer,hilfe);
|
||||
hilfe.append("Unvollständiger Ablauf");
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (hilfe != null) hilfe.close();
|
||||
hilfe = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void setGraph(Graph graph, GraphOptions options) {
|
||||
super.setGraph(graph,options);
|
||||
mReset(null);
|
||||
}
|
||||
|
||||
@FXML
|
||||
void mReset(ActionEvent event) {
|
||||
if(aktAlgo != null && aktAlgo.isAlive()) aktAlgo.stop();
|
||||
graph.initialisiereAlleKnoten();
|
||||
graph.initialisiereAlleKanten();
|
||||
update();
|
||||
//gp.setInfoText(gp.getGraph().toString());
|
||||
bStep.setDisable(false);
|
||||
bStart.setDisable(false);
|
||||
bStart.setVisible(true);
|
||||
bBreak.setVisible(false);
|
||||
if (hilfe != null) hilfe.loescheAlles();
|
||||
this.aktAlgo = null;
|
||||
}
|
||||
|
||||
@FXML
|
||||
void mStep(ActionEvent event) {
|
||||
|
||||
if (aktAlgo == null ) {
|
||||
if(cbAlgorithmen.getSelectionModel().getSelectedIndex() >= 0) {
|
||||
try{
|
||||
ClassLoader parentClassLoader = MyClassLoader.class.getClassLoader();
|
||||
MyClassLoader classLoader = new MyClassLoader(parentClassLoader);
|
||||
Class c = classLoader.loadClass(algoNamen.get(cbAlgorithmen.getSelectionModel().getSelectedIndex()));
|
||||
|
||||
aktAlgo = ((GraphAlgo)(c).getDeclaredConstructor().newInstance());
|
||||
aktAlgo.setStartKnoten(viewer.getSelectedKnoten());
|
||||
aktAlgo.setGUIElemente(viewer, hilfe);
|
||||
aktAlgo.setSpeed(910-(int) (sSpeed.getValue()));
|
||||
} catch( Exception e) {
|
||||
System.out.println(e);
|
||||
}
|
||||
aktAlgo.start();
|
||||
}
|
||||
} else {
|
||||
if(aktAlgo.isAlive()) {
|
||||
aktAlgo.setSpeed(910-(int) (sSpeed.getValue()));
|
||||
aktAlgo.setWaitforclick(false);
|
||||
//gp.setInfoText(aktAlgo.getInfo());
|
||||
|
||||
} else {
|
||||
//gp.setInfoText("Algorithmus ist beendet. "+aktAlgo.getInfo());
|
||||
bStep.setDisable(true);
|
||||
bStart.setDisable(true);
|
||||
bBreak.setDisable(true);
|
||||
} // end of if-else
|
||||
} // end of if-else
|
||||
try{
|
||||
Thread.sleep(100);
|
||||
} catch(Exception e) {}
|
||||
|
||||
if (!aktAlgo.isAlive()) {
|
||||
//gp.setInfoText("Algorithmus ist beendet"+aktAlgo.getInfo());
|
||||
bStep.setDisable(true);
|
||||
bStart.setDisable(true);
|
||||
bBreak.setDisable(true);
|
||||
}
|
||||
}
|
||||
|
||||
@FXML
|
||||
void mStart(ActionEvent event) {
|
||||
|
||||
if (aktAlgo == null ) {
|
||||
if(cbAlgorithmen.getSelectionModel().getSelectedIndex() >= 0) {
|
||||
try{
|
||||
ClassLoader parentClassLoader = MyClassLoader.class.getClassLoader();
|
||||
MyClassLoader classLoader = new MyClassLoader(parentClassLoader);
|
||||
Class c = classLoader.loadClass(algoNamen.get(cbAlgorithmen.getSelectionModel().getSelectedIndex()));
|
||||
|
||||
aktAlgo = ((GraphAlgo)(c).getDeclaredConstructor().newInstance());
|
||||
|
||||
aktAlgo.setStartKnoten(viewer.getSelectedKnoten());
|
||||
aktAlgo.setGUIElemente(viewer,hilfe);
|
||||
aktAlgo.setSpeed(910-(int) (sSpeed.getValue()));
|
||||
|
||||
} catch( Exception e) {
|
||||
System.out.println(e);
|
||||
}
|
||||
|
||||
aktAlgo.setStepping(false);
|
||||
aktAlgo.start();
|
||||
}
|
||||
} else {
|
||||
if(aktAlgo.isAlive()) {
|
||||
aktAlgo.setSpeed(910-(int) (sSpeed.getValue()));
|
||||
aktAlgo.setStepping(false);
|
||||
aktAlgo.setWaitforclick(false);
|
||||
//gp.setInfoText(aktAlgo.getInfo());
|
||||
|
||||
} else {
|
||||
//gp.setInfoText("Algorithmus ist beendet. "+aktAlgo.getInfo());
|
||||
|
||||
} // end of if-else
|
||||
} // end of if-else
|
||||
|
||||
// gp.setInfoText(aktAlgo.getInfo());
|
||||
bStep.setDisable(true);
|
||||
bStart.setVisible(false);
|
||||
bBreak.setVisible(true);
|
||||
bBreak.setDisable(false);
|
||||
|
||||
}
|
||||
|
||||
public void mBreak(ActionEvent event) {
|
||||
if(aktAlgo != null && aktAlgo.isAlive()) {
|
||||
aktAlgo.setStepping(true);
|
||||
bStart.setVisible(true);
|
||||
bBreak.setVisible(false);
|
||||
bStep.setDisable(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue