package de.prob2.ui.dotty;


import java.io.File;

import com.google.inject.Inject;

import de.prob.Main;
import de.prob.animator.command.GetSvgForVisualizationCommand;
import de.prob2.ui.internal.StageManager;
import de.prob2.ui.prob2fx.CurrentTrace;

import javafx.fxml.FXML;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseButton;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class DotView extends Stage {
	
	@FXML
	private WebView dotView;
	
	@FXML
	private ChoiceBox<DotChoiceItem> cbChoice;
	
	@FXML
	private TextField tfFormula;
	
	@FXML
	private ScrollPane pane;
	
	private double oldMousePositionX = -1;
	private double oldMousePositionY = -1;
	private double dragFactor = 1;
	
	private final CurrentTrace currentTrace;
	
	private static final File FILE = new File(Main.getProBDirectory()
			+ File.separator + "prob2ui" + File.separator + "out.svg");
	
	@Inject
	public DotView(final StageManager stageManager, final CurrentTrace currentTrace) {
		stageManager.loadFXML(this, "dot_view.fxml");
		this.currentTrace = currentTrace;
	}
	
	@FXML
	public void initialize() {
		dotView.setContextMenuEnabled(false);
		dotView.setOnMouseMoved(e-> {
			oldMousePositionX = e.getSceneX();
			oldMousePositionY = e.getSceneY();
		});
		
		dotView.setOnMouseDragged(e-> {
			pane.setHvalue(pane.getHvalue() + (-e.getSceneX() + oldMousePositionX)/(pane.getWidth() * dragFactor));
			pane.setVvalue(pane.getVvalue() + (-e.getSceneY() + oldMousePositionY)/(pane.getHeight() * dragFactor));
			oldMousePositionX = e.getSceneX();
			oldMousePositionY = e.getSceneY();

			
		});
		dotView.setOnMouseClicked(e-> {
			if(e.getClickCount() < 2) {
				return;
			}
			if(e.getButton() == MouseButton.SECONDARY) {
				dotView.setZoom(dotView.getZoom() * 0.5);
				dragFactor *= 0.8;
			} else {
				dotView.setZoom(dotView.getZoom() * 2);
				dragFactor *= 1.3;
			}
			pane.setHvalue(e.getX() / pane.getWidth());
			pane.setVvalue(e.getY() / pane.getHeight());
		});
		cbChoice.getSelectionModel().selectFirst();
		cbChoice.getSelectionModel().selectedItemProperty().addListener((observable, from, to) -> 
			tfFormula.setVisible(to.hasFormula())
		);
	}
	
	@FXML
	public void visualize() {
		GetSvgForVisualizationCommand cmd = new GetSvgForVisualizationCommand(cbChoice.getValue().geVisualisationType().getOption(), FILE);
		currentTrace.getStateSpace().execute(cmd);
		dotView.getEngine().load(FILE.toURI().toString());
	}

}

@@ -42,16 +42,44 @@
 io.File;
+%0Aimport java.util.ArrayList;
 %0A%0Aimport
@@ -2326,16 +2326,176 @@
 ize() %7B%0A
+%09%09ArrayList%3CString%3E formulas = new ArrayList%3C%3E();%0A%09%09if(cbChoice.getSelectionModel().getSelectedItem().hasFormula()) %7B%0A%09%09%09formulas.add(tfFormula.getText());%0A%09%09%7D%0A
 %09%09GetSvg
@@ -2617,16 +2617,26 @@
 (), FILE
+, formulas
 );%0A%09%09cur

Update Graph Visualization