MainApp.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. package de.tu_darmstadt.informatik.tk.scopviz.main;
  2. import java.awt.Dimension;
  3. import java.awt.event.MouseListener;
  4. import java.io.IOException;
  5. import java.util.Iterator;
  6. import java.util.Random;
  7. import javax.swing.JPanel;
  8. import org.graphstream.graph.Graph;
  9. import org.graphstream.graph.Node;
  10. import org.graphstream.ui.geom.Vector3;
  11. import org.graphstream.ui.swingViewer.ViewPanel;
  12. import org.graphstream.ui.swingViewer.util.GraphMetrics;
  13. import org.graphstream.ui.view.View;
  14. import javafx.application.Application;
  15. import javafx.beans.value.ChangeListener;
  16. import javafx.beans.value.ObservableValue;
  17. import javafx.embed.swing.SwingNode;
  18. import javafx.event.ActionEvent;
  19. import javafx.event.EventHandler;
  20. import javafx.fxml.FXMLLoader;
  21. import javafx.scene.Scene;
  22. import javafx.scene.control.SplitPane;
  23. import javafx.scene.control.Button;
  24. import javafx.scene.input.MouseEvent;
  25. import javafx.scene.layout.AnchorPane;
  26. import javafx.scene.layout.Pane;
  27. import javafx.scene.layout.VBox;
  28. import javafx.stage.Stage;
  29. import de.tu_darmstadt.informatik.tk.scopviz.debug.Debug;
  30. import de.tu_darmstadt.informatik.tk.scopviz.io.GraphMLImporter;
  31. import de.tu_darmstadt.informatik.tk.scopviz.ui.GUIController;
  32. import de.tu_darmstadt.informatik.tk.scopviz.ui.Visualizer;
  33. /**
  34. * Main Class, initializes Graph, displays UI.
  35. *
  36. * @author Jan Enders (jan.enders@stud.tu-darmstadt.de)
  37. * @version 1.0
  38. *
  39. */
  40. public class MainApp extends Application {
  41. /**
  42. * Primary Stage for the UI Scene.
  43. */
  44. private Stage primaryStage;
  45. /**
  46. * Root Object of the Scene Graph.
  47. */
  48. private VBox rootLayout;
  49. /**
  50. * Graph (Network or Operator Graph) to display.
  51. */
  52. private Graph graph;
  53. /**
  54. * Preferred size for the Graph Viewer.
  55. */
  56. private final Dimension preferredViewerSize = new Dimension(425, 367);
  57. private Visualizer visualizer;
  58. private ViewPanel view;
  59. private static GUIController guiController;
  60. private enum Mod {
  61. NORMAL, CREATE_NODE, CREATE_EDGE, FIRST_NODE_SELECTED
  62. }
  63. private String firstNode;
  64. private Mod modus = Mod.NORMAL;
  65. /**
  66. * Main Method, launches the Application.
  67. *
  68. * @param args
  69. * Optional String arguments (command line flags), none
  70. * implemented
  71. */
  72. public static void main(final String[] args) {
  73. launch(args);
  74. }
  75. /**
  76. * Initializes the Graph by importing it from a GraphML file.
  77. */
  78. @Override
  79. public void init() {
  80. GraphMLImporter importer = new GraphMLImporter();
  81. graph = importer.readGraph(MainApp.class.getResource("/Example.graphml"));
  82. visualizer = new Visualizer(graph);
  83. }
  84. /**
  85. * Starts the Application by initializing the UI Layout.
  86. */
  87. @Override
  88. public void start(final Stage stage) {
  89. this.primaryStage = stage;
  90. initRootLayout();
  91. }
  92. /**
  93. * initializes the UI Layout by loading it from a FXML file.
  94. */
  95. public void initRootLayout() {
  96. // Load root layout from fxml file.
  97. try {
  98. FXMLLoader loader = new FXMLLoader();
  99. loader.setLocation(MainApp.class.getResource("/NewBetterCoolerWindowTest.fxml"));
  100. rootLayout = (VBox) loader.load();
  101. } catch (IOException e) {
  102. System.err.println("FXML File could not be loaded. Could the Path be incorrect?");
  103. e.printStackTrace();
  104. }
  105. // Show the scene containing the root layout.
  106. Scene scene = new Scene(rootLayout);
  107. SwingNode swingNode = guiController.swingNode;
  108. Pane pane = guiController.pane;
  109. Button createNodeButton = guiController.createNode;
  110. Button createEdgeButton = guiController.createEdge;
  111. view = visualizer.getView();
  112. view.setPreferredSize(preferredViewerSize);
  113. swingNode.setContent((JPanel) view);
  114. pane.setMinSize(200, 200);
  115. ChangeListener<Number> resizeListener = new ChangeListener<Number>() {
  116. @Override
  117. public void changed(ObservableValue<? extends Number> arg0, Number arg1, Number arg2) {
  118. view.setPreferredSize(new Dimension((int) pane.getWidth() - 10, (int) pane.getHeight() - 10));
  119. swingNode.setContent(view);
  120. }
  121. };
  122. pane.heightProperty().addListener(resizeListener);
  123. pane.widthProperty().addListener(resizeListener);
  124. guiController.zoomIn.setOnAction(new EventHandler<ActionEvent>() {
  125. public void handle(ActionEvent evt) {
  126. view.getCamera().setViewPercent(view.getCamera().getViewPercent() * 0.95);
  127. }
  128. });
  129. guiController.zoomOut.setOnAction(new EventHandler<ActionEvent>() {
  130. public void handle(ActionEvent evt) {
  131. view.getCamera().setViewPercent(view.getCamera().getViewPercent() * 1.05);
  132. }
  133. });
  134. createNodeButton.setOnAction(new EventHandler<ActionEvent>() {
  135. @Override
  136. public void handle(ActionEvent arg0) {
  137. switch (modus) {
  138. case CREATE_NODE:
  139. modus = Mod.NORMAL;
  140. Debug.out("Modus set to Normal");
  141. createNodeButton.setText("Knoten hinzufügen");
  142. break;
  143. case NORMAL:
  144. modus = Mod.CREATE_NODE;
  145. Debug.out("Modus set to Create Node");
  146. createNodeButton.setText("Ende");
  147. break;
  148. default:
  149. modus = Mod.CREATE_NODE;
  150. Debug.out("Modus set to Create Node");
  151. createNodeButton.setText("Ende");
  152. createEdgeButton.setText("Kante hinzufügen");
  153. firstNode = "";
  154. break;
  155. }
  156. }
  157. });
  158. createEdgeButton.setOnAction(new EventHandler<ActionEvent>() {
  159. @Override
  160. public void handle(ActionEvent arg0) {
  161. firstNode = "";
  162. switch (modus) {
  163. case CREATE_EDGE:
  164. case FIRST_NODE_SELECTED:
  165. modus = Mod.NORMAL;
  166. Debug.out("Modus set to Normal");
  167. createEdgeButton.setText("Kante hinzufügen");
  168. break;
  169. case NORMAL:
  170. modus = Mod.CREATE_EDGE;
  171. Debug.out("Modus set to Create Edge");
  172. createEdgeButton.setText("Ende");
  173. break;
  174. default:
  175. modus = Mod.CREATE_EDGE;
  176. Debug.out("Modus set to Create Edge");
  177. createEdgeButton.setText("Ende");
  178. createNodeButton.setText("Knoten hinzufügen");
  179. break;
  180. }
  181. }
  182. });
  183. swingNode.setOnMouseClicked(new EventHandler<MouseEvent>() {
  184. @Override
  185. public void handle(MouseEvent event) {
  186. double x = event.getX();
  187. double trueX = (x - 45) / 3 + 100;
  188. double y = event.getY();
  189. double trueY = (y - 30) / (-3) +200;
  190. Debug.out("-M (" + trueX + "/" + trueY + ")");
  191. if (modus == Mod.CREATE_NODE) {
  192. Node n = graph.addNode(getUnusedID());
  193. GraphMetrics gm = view.getCamera().getMetrics();
  194. Vector3 vc3 = gm.getSize();
  195. Debug.out("(x/y): " + vc3.x() + "/" + vc3.y());
  196. vc3.x();
  197. vc3.y();
  198. n.setAttribute("x", trueX);
  199. n.setAttribute("y", trueY);
  200. Debug.out("Created a dot on (" + trueX + "/" + trueY + ")");
  201. } else if (modus == Mod.CREATE_EDGE || modus == Mod.FIRST_NODE_SELECTED) {
  202. Iterator<Node> itr = graph.getNodeIterator();
  203. double d = Double.MAX_VALUE;
  204. String id = null;
  205. while (itr.hasNext()) {
  206. Node curN = itr.next();
  207. double nodeX = curN.getAttribute("x");
  208. double nodeY = curN.getAttribute("y");
  209. double curD = Math.sqrt(Math.pow(nodeX - trueX, 2.0) + Math.pow(nodeY - trueY, 2.0));
  210. Debug.out("+" + curN.getId() + " (" + nodeX + "/" + nodeY + ")");
  211. if (curD < d) {
  212. d = curD;
  213. id = curN.getId();
  214. }
  215. }
  216. Debug.out(id + " pressed");
  217. if (id == null) {
  218. Debug.out("nothing selected");
  219. return;
  220. }
  221. switch (modus) {
  222. case CREATE_EDGE:
  223. firstNode = id;
  224. modus = Mod.FIRST_NODE_SELECTED;
  225. break;
  226. case FIRST_NODE_SELECTED:
  227. if (!id.matches(firstNode)) {
  228. graph.addEdge(getUnusedID(), firstNode, id);
  229. Debug.out("Created a edge between " + firstNode + " and " + id);
  230. }
  231. firstNode = "";
  232. modus = Mod.CREATE_EDGE;
  233. break;
  234. default:
  235. break;
  236. }
  237. }
  238. }
  239. });
  240. primaryStage.setMinHeight(400);
  241. primaryStage.setMinWidth(640);
  242. primaryStage.setScene(scene);
  243. primaryStage.show();
  244. }
  245. private String getUnusedID() {
  246. // TODO gescheite implementierung
  247. Random rand = new Random();
  248. return rand.nextInt() + "";
  249. }
  250. public static void setGUIController(GUIController toSet) {
  251. guiController = toSet;
  252. }
  253. }