GUIController.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. package de.tu_darmstadt.informatik.tk.scopviz.ui;
  2. import java.awt.Dimension;
  3. import java.net.URL;
  4. import java.util.ArrayList;
  5. import java.util.ResourceBundle;
  6. import javax.swing.JPanel;
  7. import org.jxmapviewer.JXMapViewer;
  8. import org.jxmapviewer.OSMTileFactoryInfo;
  9. import org.jxmapviewer.viewer.DefaultTileFactory;
  10. import org.jxmapviewer.viewer.GeoPosition;
  11. import org.jxmapviewer.viewer.TileFactoryInfo;
  12. import de.tu_darmstadt.informatik.tk.scopviz.main.Main;
  13. import de.tu_darmstadt.informatik.tk.scopviz.ui.handlers.KeyboardShortcuts;
  14. import de.tu_darmstadt.informatik.tk.scopviz.ui.handlers.MyAnimationTimer;
  15. import de.tu_darmstadt.informatik.tk.scopviz.ui.handlers.MyViewerListener;
  16. import de.tu_darmstadt.informatik.tk.scopviz.ui.handlers.ResizeListener;
  17. import javafx.beans.value.ChangeListener;
  18. import javafx.beans.value.ObservableValue;
  19. import javafx.embed.swing.SwingNode;
  20. import javafx.fxml.FXML;
  21. import javafx.fxml.Initializable;
  22. import javafx.scene.control.Button;
  23. import javafx.scene.control.CheckBox;
  24. import javafx.scene.control.Label;
  25. import javafx.scene.control.ListView;
  26. import javafx.scene.control.MenuItem;
  27. import javafx.scene.control.TableCell;
  28. import javafx.scene.control.TableColumn;
  29. import javafx.scene.control.TableRow;
  30. import javafx.scene.control.TableView;
  31. import javafx.scene.control.cell.PropertyValueFactory;
  32. import javafx.scene.control.cell.TextFieldTableCell;
  33. import javafx.scene.layout.Pane;
  34. import javafx.scene.layout.VBox;
  35. import javafx.scene.text.Text;
  36. import javafx.util.Callback;
  37. import javafx.util.Pair;
  38. /**
  39. * Controller class for the various GUI elements, gets instanced and initialized
  40. * by the FXML loading process. Can access GUI elements specified in the FXML
  41. * file through matching variable name here and id attribute in the FXML,
  42. *
  43. * @author Dominik Renkel, Jan Enders
  44. * @version 1.1
  45. *
  46. */
  47. public class GUIController implements Initializable {
  48. // The SwingNode and its containing Pane that house the graph viewer
  49. @FXML
  50. public SwingNode swingNode;
  51. @FXML
  52. public SwingNode swingNodeWorldView;
  53. @FXML
  54. public Pane pane;
  55. // The buttons present in the UI
  56. @FXML
  57. public Button zoomIn;
  58. @FXML
  59. public Button zoomOut;
  60. @FXML
  61. public Button underlayButton;
  62. @FXML
  63. public Button operatorButton;
  64. @FXML
  65. public Button mappingButton;
  66. @FXML
  67. public Button symbolRepButton;
  68. // The Toolbar Items
  69. @FXML
  70. public MenuItem newItem;
  71. @FXML
  72. public MenuItem open;
  73. @FXML
  74. public MenuItem add;
  75. @FXML
  76. public MenuItem save;
  77. @FXML
  78. public MenuItem saveAs;
  79. @FXML
  80. public MenuItem preferences;
  81. @FXML
  82. public MenuItem quit;
  83. @FXML
  84. public MenuItem delete;
  85. @FXML
  86. public MenuItem undelete;
  87. @FXML
  88. public MenuItem selectMode;
  89. @FXML
  90. public MenuItem about;
  91. // The contents of the corresponding ScrollPanes
  92. @FXML
  93. public TableView<Pair<Object, String>> toolbox;
  94. @FXML
  95. public TableView<KeyValuePair> properties;
  96. @FXML
  97. public ListView<String> metricListView;
  98. @FXML
  99. public ListView<String> layerListView;
  100. // The columns of the Toolbox
  101. @FXML
  102. public TableColumn<Pair<Object, String>, String> toolboxStringColumn;
  103. @FXML
  104. public TableColumn<Pair<Object, String>, Object> toolboxObjectColumn;
  105. // The columns of the Properites pane
  106. // TODO: Fix Generic type arguments for propertiesObjectColumn
  107. @FXML
  108. public TableColumn<KeyValuePair, String> propertiesStringColumn;
  109. @FXML
  110. public TableColumn propertiesObjectColumn;
  111. @FXML
  112. public TableColumn<KeyValuePair, String> propertiesTypeColumn;
  113. @FXML
  114. public Text createModusText;
  115. @FXML
  116. public Text selectModusText;
  117. @FXML
  118. public Text actualLayerText;
  119. @FXML
  120. public VBox symbolToolVBox;
  121. @FXML
  122. public CheckBox edgesVisibleCheckbox;
  123. @FXML
  124. public CheckBox nodeLabelCheckbox;
  125. @FXML
  126. public CheckBox edgeWeightCheckbox;
  127. @FXML
  128. public Pane worldViewPane;
  129. /**
  130. * Initializes all the references to the UI elements specified in the FXML
  131. * file. Gets called during FXML loading. Asserts the correct injection of
  132. * all referenced UI elements and initializes them.
  133. */
  134. @Override
  135. public void initialize(URL arg0, ResourceBundle arg1) {
  136. // Assert the correct injection of all references from FXML
  137. assertFXMLInjections();
  138. // Give the AnimationTimer access to UI elements
  139. MyAnimationTimer.setGUIController(this);
  140. initializeToolbox();
  141. initializeProperties();
  142. // Remove Header for the toolbox
  143. removeHeaderTableView(toolbox);
  144. // Initialize the Managers for the various for UI elements
  145. ToolboxManager.initializeItems(toolbox);
  146. PropertiesManager.initializeItems(properties);
  147. GraphDisplayManager.setGuiController(this);
  148. // Bind all the handlers to their corresponding UI elements
  149. initializeZoomButtons();
  150. initializeLayerButton();
  151. //initializeDisplayPane();
  152. initializeMenuBar();
  153. initializeSymbolRepToolbox();
  154. // Initialize the Text Labels for displaying the current state of the
  155. // Application
  156. initializeTextFields();
  157. // Setup the Keyboard Shortcuts
  158. KeyboardShortcuts.initialize(Main.getInstance().getPrimaryStage());
  159. initializeWorldView();
  160. }
  161. // Initialize world view for symbolRep.
  162. private void initializeWorldView() {
  163. JXMapViewer mapViewer = new JXMapViewer();
  164. // Create a TileFactoryInfo for OpenStreetMap
  165. TileFactoryInfo info = new OSMTileFactoryInfo();
  166. DefaultTileFactory tileFactory = new DefaultTileFactory(info);
  167. mapViewer.setTileFactory(tileFactory);
  168. // Use 8 threads in parallel to load the tiles
  169. tileFactory.setThreadPoolSize(8);
  170. // Set the focus
  171. GeoPosition frankfurt = new GeoPosition(50.11, 8.68);
  172. mapViewer.setZoom(7);
  173. mapViewer.setAddressLocation(frankfurt);
  174. JPanel testPane = new JPanel();
  175. testPane.setSize(new Dimension(500, 500));
  176. testPane.add(mapViewer);
  177. swingNodeWorldView.setContent(testPane);
  178. swingNode.setVisible(false);
  179. pane.setVisible(false);
  180. worldViewPane.setMinSize(200, 200);
  181. worldViewPane.setMaxSize(1000, 1000);
  182. }
  183. /**
  184. * Initializes the Menu Bar with all its contents.
  185. */
  186. private void initializeMenuBar() {
  187. MenuBarManager.setGUIController(this);
  188. newItem.setOnAction(MenuBarManager.newHandler);
  189. open.setOnAction(MenuBarManager.openHandler);
  190. add.setOnAction(MenuBarManager.addHandler);
  191. save.setOnAction(MenuBarManager.saveHandler);
  192. saveAs.setOnAction(MenuBarManager.saveAsHandler);
  193. preferences.setOnAction(MenuBarManager.preferencesHandler);
  194. quit.setOnAction(MenuBarManager.quitHandler);
  195. delete.setOnAction(MenuBarManager.deleteHandler);
  196. undelete.setOnAction(MenuBarManager.undeleteHandler);
  197. selectMode.setOnAction(MenuBarManager.selectModeHandler);
  198. about.setOnAction(MenuBarManager.aboutHandler);
  199. }
  200. /**
  201. * Sets the handlers for the zoomin and zoomout buttons.
  202. */
  203. private void initializeZoomButtons() {
  204. zoomIn.setOnAction(ButtonManager.zoomInHandler);
  205. zoomOut.setOnAction(ButtonManager.zoomOutHandler);
  206. }
  207. private void initializeSymbolRepToolbox(){
  208. // Hide SymbolRep Toolbox View
  209. symbolToolVBox.setVisible(false);
  210. edgesVisibleCheckbox.selectedProperty().addListener(ButtonManager.edgeVisibleListener);
  211. nodeLabelCheckbox.selectedProperty().addListener(ButtonManager.nodeLabelListener);
  212. edgeWeightCheckbox.selectedProperty().addListener(ButtonManager.edgeWeightListener);
  213. }
  214. /**
  215. * Set the Handlers for the Layer switch Buttons.
  216. */
  217. private void initializeLayerButton() {
  218. underlayButton.setOnAction(ButtonManager.underlayHandler);
  219. operatorButton.setOnAction(ButtonManager.operatorHandler);
  220. mappingButton.setOnAction(ButtonManager.mappingHandler);
  221. symbolRepButton.setOnAction(ButtonManager.symbolRepHandler);
  222. ArrayList<Button> layerButtons = new ArrayList<Button>();
  223. layerButtons.add(underlayButton);
  224. layerButtons.add(operatorButton);
  225. layerButtons.add(mappingButton);
  226. layerButtons.add(symbolRepButton);
  227. ButtonManager.initialize(layerButtons, this);
  228. }
  229. /**
  230. * Sets the minimum size and adds the handlers to the graph display.
  231. */
  232. private void initializeDisplayPane() {
  233. pane.heightProperty().addListener(new ResizeListener(swingNode, pane));
  234. pane.widthProperty().addListener(new ResizeListener(swingNode, pane));
  235. pane.setOnScroll(GraphDisplayManager.scrollHandler);
  236. swingNode.setContent((JPanel) Main.getInstance().getGraphManager().getView());
  237. swingNode.setOnMouseClicked(ButtonManager.clickedHandler);
  238. swingNode.setOnMousePressed(GraphDisplayManager.rememberLastClickedPosHandler);
  239. swingNode.setOnMouseDragged(GraphDisplayManager.mouseDraggedHandler);
  240. pane.setMinSize(200, 200);
  241. }
  242. /**
  243. * Initialize the Toolbox.
  244. */
  245. private void initializeToolbox() {
  246. ToolboxManager.initialize(this);
  247. MyViewerListener.setGUIController(this);
  248. toolboxStringColumn.setCellValueFactory(new ToolboxManager.PairKeyFactory());
  249. toolboxObjectColumn.setCellValueFactory(new ToolboxManager.PairValueFactory());
  250. toolboxObjectColumn.setCellFactory(
  251. new Callback<TableColumn<Pair<Object, String>, Object>, TableCell<Pair<Object, String>, Object>>() {
  252. @Override
  253. public TableCell<Pair<Object, String>, Object> call(
  254. TableColumn<Pair<Object, String>, Object> column) {
  255. return new ToolboxManager.PairValueCell();
  256. }
  257. });
  258. toolbox.getColumns().setAll(toolboxObjectColumn, toolboxStringColumn);
  259. // Click event for TableView row
  260. toolbox.setRowFactory(tv -> {
  261. TableRow<Pair<Object, String>> row = new TableRow<>();
  262. row.setOnMouseClicked(ToolboxManager.rowClickedHandler);
  263. return row;
  264. });
  265. // nothing is selected at the start
  266. toolbox.getSelectionModel().clearSelection();
  267. }
  268. /**
  269. * Initialize the Properties Window.
  270. */
  271. private void initializeProperties() {
  272. propertiesObjectColumn.setResizable(true);
  273. propertiesStringColumn.setResizable(true);
  274. propertiesStringColumn.setCellValueFactory(new PropertyValueFactory<KeyValuePair, String>("key"));
  275. propertiesObjectColumn.setCellValueFactory(new PropertyValueFactory<KeyValuePair, Object>("value"));
  276. propertiesObjectColumn.setCellFactory(TextFieldTableCell.forTableColumn());
  277. propertiesObjectColumn.setOnEditCommit(PropertiesManager.setOnEditCommitHandler);
  278. propertiesTypeColumn.setCellValueFactory(new PropertyValueFactory<KeyValuePair, String>("classTypeAsString"));
  279. properties.getColumns().setAll(propertiesStringColumn, propertiesObjectColumn, propertiesTypeColumn);
  280. properties.setRowFactory(PropertiesManager.rightClickCallback);
  281. properties.setPlaceholder(new Label("No graph element selected"));
  282. properties.getSelectionModel().clearSelection();
  283. }
  284. /**
  285. * Initialize the Text Labels for displaying the State of the Application.
  286. */
  287. private void initializeTextFields() {
  288. createModusText.setText(Main.getInstance().getCreationMode().toString());
  289. selectModusText.setText(Main.getInstance().getSelectionMode().toString());
  290. actualLayerText.setText(GraphDisplayManager.getCurrentLayer().toString());
  291. }
  292. /**
  293. * Removes the TableView Header for a given TableView
  294. *
  295. * @param tableView
  296. */
  297. private void removeHeaderTableView(TableView<?> tableView) {
  298. tableView.widthProperty().addListener(new ChangeListener<Number>() {
  299. @Override
  300. public void changed(ObservableValue<? extends Number> ov, Number t, Number t1) {
  301. // Get the table header
  302. Pane header = (Pane) tableView.lookup("TableHeaderRow");
  303. if (header != null && header.isVisible()) {
  304. header.setMaxHeight(0);
  305. header.setMinHeight(0);
  306. header.setPrefHeight(0);
  307. header.setVisible(false);
  308. header.setManaged(false);
  309. }
  310. }
  311. });
  312. }
  313. /**
  314. * Asserts the correct Injection of all Elements from the FXML File.
  315. */
  316. private void assertFXMLInjections() {
  317. assert swingNode != null : "fx:id=\"swingNode\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  318. assert pane != null : "fx:id=\"pane\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  319. assert zoomIn != null : "fx:id=\"zoomIn\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  320. assert zoomOut != null : "fx:id=\"zoomOut\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  321. assert underlayButton != null : "fx:id=\"underlayButton\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  322. assert operatorButton != null : "fx:id=\"operatorButton\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  323. assert mappingButton != null : "fx:id=\"mappingButton\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  324. assert symbolRepButton != null : "fx:id=\"symbolRepButton\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  325. assert newItem != null : "fx:id=\"newItem\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  326. assert open != null : "fx:id=\"open\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  327. assert add != null : "fx:id=\"add\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  328. assert save != null : "fx:id=\"save\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  329. assert saveAs != null : "fx:id=\"saveAs\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  330. assert preferences != null : "fx:id=\"preferences\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  331. assert quit != null : "fx:id=\"quit\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  332. assert delete != null : "fx:id=\"delete\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  333. assert undelete != null : "fx:id=\"undelete\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  334. assert selectMode != null : "fx:id=\"selectMode\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  335. assert about != null : "fx:id=\"about\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  336. assert layerListView != null : "fx:id=\"layerListView\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  337. assert toolbox != null : "fx:id=\"toolbox\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  338. assert properties != null : "fx:id=\"properties\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  339. assert metricListView != null : "fx:id=\"metricListView\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  340. assert toolboxStringColumn != null : "fx:id=\"toolboxString\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  341. assert toolboxObjectColumn != null : "fx:id=\"toolboxObject\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  342. assert propertiesStringColumn != null : "fx:id=\"propertiesString\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  343. assert propertiesObjectColumn != null : "fx:id=\"propertiesObject\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  344. assert propertiesTypeColumn != null : "fx:id=\"propertiesType\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  345. assert createModusText != null : "fx:id=\"createModusText\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  346. assert selectModusText != null : "fx:id=\"selectModusText\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  347. assert actualLayerText != null : "fx:id=\"actualLayerText\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  348. assert symbolToolVBox != null : "fx:id=\"symbolToolVBox\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  349. assert edgesVisibleCheckbox != null : "fx:id=\"edgesVisibleCheckbox\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  350. assert nodeLabelCheckbox != null : "fx:id=\"nodeLabelCheckbox\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  351. assert edgeWeightCheckbox != null : "fx:id=\"egdeWeightCheckbox\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  352. assert swingNodeWorldView != null : "fx:id=\"swingNodeWorldView\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  353. assert worldViewPane != null : "fx:id=\"worldViewPane\" was not injected: check your FXML file 'NewBetterCoolerWindowTest.fxml'.";
  354. }
  355. }