ButtonManager.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. package de.tu_darmstadt.informatik.tk.scopviz.ui;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.HashSet;
  5. import java.util.stream.Collectors;
  6. import org.jxmapviewer.viewer.GeoPosition;
  7. import org.jxmapviewer.viewer.WaypointPainter;
  8. import de.tu_darmstadt.informatik.tk.scopviz.graphs.GraphManager;
  9. import de.tu_darmstadt.informatik.tk.scopviz.graphs.MyGraph;
  10. import de.tu_darmstadt.informatik.tk.scopviz.main.Layer;
  11. import de.tu_darmstadt.informatik.tk.scopviz.main.Main;
  12. import de.tu_darmstadt.informatik.tk.scopviz.ui.mapView.CustomMapClickListener;
  13. import de.tu_darmstadt.informatik.tk.scopviz.ui.mapView.CustomWaypoint;
  14. import de.tu_darmstadt.informatik.tk.scopviz.ui.mapView.CustomWaypointRenderer;
  15. import de.tu_darmstadt.informatik.tk.scopviz.ui.mapView.MapViewFunctions;
  16. import de.tu_darmstadt.informatik.tk.scopviz.ui.mapView.WorldView;
  17. import javafx.application.Platform;
  18. import javafx.beans.value.ObservableValue;
  19. import javafx.event.ActionEvent;
  20. import javafx.scene.control.Alert;
  21. import javafx.scene.control.Alert.AlertType;
  22. import javafx.scene.control.Button;
  23. /**
  24. * Manager to contain the various handlers for the buttons of the UI.
  25. *
  26. * @author Jan Enders (jan.enders@stud.tu-darmstadt.de), Julian Ohl, Dominik
  27. * Renkel
  28. * @version 1.2
  29. *
  30. */
  31. public final class ButtonManager {
  32. /** List of the Buttons for Layer switching. */
  33. private static ArrayList<Button> layerButtons;
  34. /**
  35. * Reference to the GUI Controller for Access to various GUI Elements.
  36. */
  37. private static GUIController controller;
  38. /**
  39. * Private Constructor to prevent Instantiation.
  40. */
  41. private ButtonManager() {
  42. }
  43. /**
  44. * Initializes the ButtonManager with a List of Buttons for Layer switching.
  45. *
  46. * @param nList
  47. * the Layer switching Buttons
  48. * @param guiController
  49. * a Reference to the GUI Controller for Access
  50. * @param uButton
  51. * a Reference to the Underlay switch Button for marking it as
  52. * active on startup
  53. *
  54. */
  55. public static void initialize(ArrayList<Button> nList, GUIController guiController, Button uButton) {
  56. layerButtons = nList;
  57. controller = guiController;
  58. setBorderStyle(uButton);
  59. }
  60. /**
  61. * Handler for zoom in Button.
  62. */
  63. public static void zoomInAction(ActionEvent event) {
  64. if (GraphDisplayManager.getCurrentLayer().equals(Layer.SYMBOL)) {
  65. WorldView.internMapViewer.setZoom(WorldView.internMapViewer.getZoom() - 1);
  66. } else {
  67. Main.getInstance().getGraphManager().zoomIn();
  68. }
  69. }
  70. /**
  71. * Handler for zoom out Button.
  72. */
  73. public static void zoomOutAction(ActionEvent event) {
  74. if (GraphDisplayManager.getCurrentLayer().equals(Layer.SYMBOL)) {
  75. WorldView.internMapViewer.setZoom(WorldView.internMapViewer.getZoom() + 1);
  76. } else {
  77. Main.getInstance().getGraphManager().zoomOut();
  78. }
  79. }
  80. /**
  81. * Handler for center map Button
  82. *
  83. * @param event
  84. */
  85. public static void centerMapAction(ActionEvent event) {
  86. HashSet<GeoPosition> positions = new HashSet<GeoPosition>(WorldView.waypoints.size());
  87. WorldView.waypoints.forEach((w) -> positions.add(w.getPosition()));
  88. WorldView.showAllWaypoints(positions);
  89. }
  90. /**
  91. * After switching from symbol-layer to other layer show toolbox and make
  92. * properties editable again.
  93. */
  94. private static void switchfromSymbolLayer() {
  95. if (GraphDisplayManager.getCurrentLayer().equals(Layer.SYMBOL)) {
  96. // show toolbox and hide VBox
  97. controller.toolbox.setVisible(true);
  98. controller.topLeftAPane.getChildren().remove(controller.symbolToolVBox);
  99. controller.symbolToolVBox.setVisible(false);
  100. // make properties editable again
  101. controller.propertiesObjectColumn.setEditable(true);
  102. // enabel context menu
  103. controller.properties.setRowFactory(PropertiesManager.rightClickCallback);
  104. // show graph instead of map view
  105. controller.swingNodeWorldView.setVisible(false);
  106. controller.swingNode.setVisible(true);
  107. // make map view mouse transparent
  108. controller.stackPane.setMouseTransparent(true);
  109. controller.swingNodeWorldView.setMouseTransparent(true);
  110. // make graph non mouse transparent
  111. controller.pane.setMouseTransparent(false);
  112. controller.swingNode.setMouseTransparent(false);
  113. // dont show symbol layer Button
  114. controller.centerMap.setVisible(false);
  115. controller.defaultMapView.setVisible(false);
  116. controller.roadMapView.setVisible(false);
  117. controller.satelliteMapView.setVisible(false);
  118. controller.hybridMapView.setVisible(false);
  119. controller.previousWaypoint.setVisible(false);
  120. controller.nextWaypoint.setVisible(false);
  121. // dont show properties of selected node or edge
  122. PropertiesManager.showNewDataSet(null);
  123. // deselect current selected node or edge
  124. CustomMapClickListener.deselectAll();
  125. // reset loaded images
  126. MapViewFunctions.resetImageMap();
  127. }
  128. }
  129. /**
  130. * Handler for the Underlay Layer switch Button.
  131. */
  132. public static void underlayAction(ActionEvent arg0) {
  133. Main.getInstance().getGraphManager().deselectEdgeCreationNodes();
  134. switchfromSymbolLayer();
  135. GraphDisplayManager.setCurrentLayer(Layer.UNDERLAY);
  136. GraphDisplayManager.switchActiveGraph();
  137. ToolboxManager.setUnderlayItems();
  138. setBorderStyle((Button) arg0.getSource());
  139. controller.open.setText("Open...");
  140. controller.newItem.disableProperty().set(false);
  141. controller.open.disableProperty().set(false);
  142. controller.add.disableProperty().set(true);
  143. controller.save.disableProperty().set(false);
  144. controller.saveAs.disableProperty().set(false);
  145. controller.delete.disableProperty().set(false);
  146. controller.undelete.disableProperty().set(false);
  147. controller.updateMetricMI.disableProperty().set(true);
  148. controller.resetMapping.disableProperty().set(true);
  149. // hide metricbox/update button/reset mapping button
  150. controller.rightSide.getChildren().remove(controller.updateButtonAPane);
  151. controller.metricbox.setVisible(false);
  152. controller.leftSide.getChildren().remove(controller.resetMappingButtonAPane);
  153. // Hide operator graph selection box
  154. controller.opGraphSelectionBox.setVisible(false);
  155. }
  156. /**
  157. * Handler for the Operator Layer switch Button.
  158. */
  159. public static void operatorAction(ActionEvent arg0) {
  160. Main.getInstance().getGraphManager().deselectEdgeCreationNodes();
  161. switchfromSymbolLayer();
  162. GraphDisplayManager.setCurrentLayer(Layer.OPERATOR);
  163. GraphDisplayManager.switchActiveGraph();
  164. ToolboxManager.setOperatorItems();
  165. setBorderStyle((Button) arg0.getSource());
  166. controller.open.setText("Open...");
  167. controller.newItem.disableProperty().set(false);
  168. controller.open.disableProperty().set(false);
  169. controller.add.disableProperty().set(false);
  170. controller.save.disableProperty().set(false);
  171. controller.saveAs.disableProperty().set(false);
  172. controller.delete.disableProperty().set(false);
  173. controller.undelete.disableProperty().set(false);
  174. controller.updateMetricMI.disableProperty().set(true);
  175. controller.resetMapping.disableProperty().set(true);
  176. // hide metricbox/update button/reset mapping button
  177. controller.rightSide.getChildren().remove(controller.updateButtonAPane);
  178. controller.metricbox.setVisible(false);
  179. controller.leftSide.getChildren().remove(controller.resetMappingButtonAPane);
  180. // show operator graph selection box
  181. controller.opGraphSelectionBox.setVisible(true);
  182. }
  183. /**
  184. * Handler for the Mapping Layer switch Button.
  185. */
  186. public static void mappingAction(ActionEvent arg0) {
  187. Main.getInstance().getGraphManager().deselectEdgeCreationNodes();
  188. // show metricbox/update button/reset mapping button
  189. if (!(GraphDisplayManager.getCurrentLayer().equals(Layer.MAPPING))) {
  190. controller.rightSide.getChildren().add(2, controller.updateButtonAPane);
  191. controller.metricbox.setVisible(true);
  192. controller.leftSide.getChildren().add(1, controller.resetMappingButtonAPane);
  193. }
  194. switchfromSymbolLayer();
  195. GraphDisplayManager.setCurrentLayer(Layer.MAPPING);
  196. GraphDisplayManager.switchActiveGraph();
  197. ToolboxManager.setMappingItems();
  198. setBorderStyle((Button) arg0.getSource());
  199. controller.open.setText("Open Mapping...");
  200. controller.newItem.disableProperty().set(true);
  201. controller.open.disableProperty().set(false);
  202. controller.add.disableProperty().set(true);
  203. controller.save.disableProperty().set(false);
  204. controller.saveAs.disableProperty().set(false);
  205. controller.delete.disableProperty().set(false);
  206. controller.undelete.disableProperty().set(false);
  207. controller.updateMetricMI.disableProperty().set(false);
  208. controller.resetMapping.disableProperty().set(false);
  209. // Hide operator graph selection box
  210. controller.opGraphSelectionBox.setVisible(false);
  211. }
  212. /**
  213. * Handler for the Symbol Representation Layer switch Button.
  214. */
  215. public static void symbolRepAction(ActionEvent arg0) {
  216. Main.getInstance().getGraphManager().deselectEdgeCreationNodes();
  217. PropertiesManager.showNewDataSet(null);
  218. if (!(GraphDisplayManager.getCurrentLayer().equals(Layer.SYMBOL))) {
  219. GraphDisplayManager.setCurrentLayer(Layer.SYMBOL);
  220. controller.topLeftAPane.getChildren().add(controller.symbolToolVBox);
  221. }
  222. // load world view
  223. if (!activateWorldView()) {
  224. // show "Connection Error" message, because of problems during
  225. // connecting attempt to server
  226. showConnectionErrorMsg();
  227. }
  228. // hide metricbox/update button/reset mapping button
  229. controller.rightSide.getChildren().remove(controller.updateButtonAPane);
  230. controller.metricbox.setVisible(false);
  231. controller.leftSide.getChildren().remove(controller.resetMappingButtonAPane);
  232. // Hide operator graph selection box
  233. controller.opGraphSelectionBox.setVisible(false);
  234. GraphDisplayManager.switchActiveGraph();
  235. setBorderStyle((Button) arg0.getSource());
  236. controller.open.setText("Open...");
  237. controller.newItem.disableProperty().set(true);
  238. controller.open.disableProperty().set(true);
  239. controller.add.disableProperty().set(true);
  240. controller.save.disableProperty().set(true);
  241. controller.saveAs.disableProperty().set(true);
  242. controller.delete.disableProperty().set(true);
  243. controller.undelete.disableProperty().set(true);
  244. controller.updateMetricMI.disableProperty().set(true);
  245. controller.resetMapping.disableProperty().set(true);
  246. }
  247. /**
  248. * show an Alert dialog when OpenStreetMap could not be loaded
  249. */
  250. public static void showConnectionErrorMsg() {
  251. Alert alert = new Alert(AlertType.WARNING);
  252. alert.setTitle("Connection Error");
  253. alert.setHeaderText("Could not reach OpenStreetMap server");
  254. alert.setContentText(null);
  255. alert.showAndWait();
  256. }
  257. /**
  258. * Initializes the WorldView, sets data and paints them.
  259. *
  260. * @throws IOException
  261. */
  262. private static boolean activateWorldView() {
  263. // dont show graph and toolbox
  264. controller.toolbox.setVisible(false);
  265. controller.swingNode.setVisible(false);
  266. // make properties uneditable
  267. controller.propertiesObjectColumn.setEditable(false);
  268. // make map view non mouse transparent
  269. controller.stackPane.setMouseTransparent(false);
  270. controller.swingNodeWorldView.setMouseTransparent(false);
  271. // make graph mouse transparent
  272. controller.pane.setMouseTransparent(true);
  273. controller.swingNode.setMouseTransparent(true);
  274. // show VBox for map options
  275. controller.symbolToolVBox.setVisible(true);
  276. // show symbol layer Button
  277. controller.centerMap.setVisible(true);
  278. controller.defaultMapView.setVisible(true);
  279. controller.roadMapView.setVisible(true);
  280. controller.satelliteMapView.setVisible(true);
  281. controller.hybridMapView.setVisible(true);
  282. controller.previousWaypoint.setVisible(true);
  283. controller.nextWaypoint.setVisible(true);
  284. // standard server connection status is true
  285. Boolean serverConnection = true;
  286. try {
  287. WorldView.loadWorldView();
  288. } catch (IOException e) {
  289. // problems with server connection -> show error message
  290. serverConnection = false;
  291. }
  292. MapViewFunctions.checkVBoxChanged();
  293. WorldView.internMapViewer.repaint();
  294. // set content to UI Element
  295. controller.swingNodeWorldView.setContent(WorldView.internMapViewer);
  296. controller.swingNodeWorldView.setVisible(true);
  297. return serverConnection;
  298. }
  299. /**
  300. * Functionality for "edge visible" Checkbox.
  301. *
  302. * @param ov
  303. * @param oldVal
  304. * Checkbox previous state (Checked or unchecked)
  305. * @param newVal
  306. * Checkbox current state (Checked or unchecked)
  307. */
  308. public static void edgeVisibilitySwitcher(ObservableValue<? extends Boolean> ov, Boolean oldVal, Boolean newVal) {
  309. // Show edges
  310. if (newVal) {
  311. WorldView.edgePainter.setShowEdges(true);
  312. WorldView.internMapViewer.repaint();
  313. } else {
  314. // Hide edges
  315. WorldView.edgePainter.setShowEdges(false);
  316. WorldView.internMapViewer.repaint();
  317. }
  318. }
  319. /**
  320. * Functionality for "label visible" Checkbox.
  321. *
  322. * @param ov
  323. * @param oldVal
  324. * Checkbox previous state (Checked or unchecked)
  325. * @param newVal
  326. * Checkbox current state (Checked or unchecked)
  327. */
  328. public static void labelVisibilitySwitcher(ObservableValue<? extends Boolean> ov, Boolean oldVal, Boolean newVal) {
  329. WaypointPainter<CustomWaypoint> waypointPainter = WorldView.waypointPainter;
  330. CustomWaypointRenderer renderer = new CustomWaypointRenderer();
  331. if (newVal) {
  332. // Show node labels
  333. renderer.setShowLabels(true);
  334. waypointPainter.clearCache();
  335. waypointPainter.setRenderer(renderer);
  336. WorldView.internMapViewer.repaint();
  337. } else {
  338. // Hide node labels
  339. renderer.setShowLabels(false);
  340. waypointPainter.clearCache();
  341. waypointPainter.setRenderer(renderer);
  342. WorldView.internMapViewer.repaint();
  343. }
  344. }
  345. /**
  346. * Functionality for "edge weights visible" Checkbox.
  347. *
  348. * @param ov
  349. * @param oldVal
  350. * Checkbox previous state (Checked or unchecked)
  351. * @param newVal
  352. * Checkbox current state (Checked or unchecked)
  353. */
  354. public static void edgeWeightVisibilitySwitcher(ObservableValue<? extends Boolean> ov, Boolean oldVal,
  355. Boolean newVal) {
  356. if (newVal) {
  357. // Show Edges weights
  358. WorldView.edgePainter.setShowWeights(true);
  359. WorldView.internMapViewer.repaint();
  360. } else {
  361. // Hide Edges weights
  362. WorldView.edgePainter.setShowWeights(false);
  363. WorldView.internMapViewer.repaint();
  364. }
  365. }
  366. /**
  367. * Changes the border of the button that was pressed to red.
  368. *
  369. * @param currentButton
  370. * the button that was pressed
  371. */
  372. private static void setBorderStyle(Button currentButton) {
  373. for (Button j : layerButtons) {
  374. if (j.equals(currentButton)) {
  375. j.setStyle(
  376. "-fx-background-color: #039ED3, #039ED3, #039ED3, -fx-faint-focus-color, -fx-body-color; -fx-background-insets: -0.2, 1, 2, -1.4, 2.6; -fx-background-radius: 3, 2, 1, 4, 1;");
  377. } else {
  378. j.setStyle("-fx-border-width: 0;");
  379. }
  380. }
  381. }
  382. /**
  383. * update mapViewer if choiceBox item was changed.
  384. *
  385. * @param ov
  386. * The observed Value
  387. * @param oldVal
  388. * Its old Value
  389. * @param newVal
  390. * Its new Value
  391. */
  392. /*
  393. * public static void mapViewChoiceChange(ObservableValue<? extends String>
  394. * ov, String oldVal, String newVal) { MapViewFunctions.changeMapView(); }
  395. *
  396. * /** select the given MapType in the ChoiceBox and change Map View
  397. *
  398. * @param mapType
  399. */
  400. /*
  401. * public static void switchToMap(String mapType) {
  402. * controller.mapViewChoiceBox.getSelectionModel().select(mapType);
  403. * MapViewFunctions.changeMapView(); }
  404. */
  405. public static void setupOpGraphComboBox() {
  406. Platform.runLater(() -> {
  407. controller.opGraphSelectionBox.getItems().clear();
  408. GraphManager operatorManager = GraphDisplayManager.getGraphManager(Layer.OPERATOR);
  409. for (MyGraph g : operatorManager.getGraph().getAllSubGraphs().stream().filter((g) -> !g.isComposite())
  410. .collect(Collectors.toList())) {
  411. controller.opGraphSelectionBox.getItems().add(g.getId());
  412. }
  413. controller.opGraphSelectionBox.getItems().add("Add...");
  414. controller.opGraphSelectionBox.setValue(controller.opGraphSelectionBox.getItems().get(0));
  415. });
  416. }
  417. public static void opGraphSelectedAction(ActionEvent v) {
  418. if (controller.opGraphSelectionBox.getValue() == null || controller.opGraphSelectionBox.getValue().equals("")) {
  419. return;
  420. }
  421. if (controller.opGraphSelectionBox.getValue().equals("Add...")) {
  422. MenuBarManager.addAction(v);
  423. Platform.runLater(() -> controller.opGraphSelectionBox.setValue(controller.opGraphSelectionBox.getItems()
  424. .get(controller.opGraphSelectionBox.getItems().size() - 2)));
  425. } else {
  426. GraphDisplayManager.getGraphManager(Layer.OPERATOR)
  427. .setActiveSubGraph(controller.opGraphSelectionBox.getValue());
  428. }
  429. }
  430. }