ButtonManager.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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.getWaypoints().size());
  87. WorldView.getWaypoints().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. // hide metricbox/update button/reset mapping button
  149. controller.rightSide.getChildren().remove(controller.updateButtonAPane);
  150. controller.metricbox.setVisible(false);
  151. controller.leftSide.getChildren().remove(controller.resetMappingButtonAPane);
  152. // Hide operator graph selection box
  153. controller.opGraphSelectionBox.setVisible(false);
  154. }
  155. /**
  156. * Handler for the Operator Layer switch Button.
  157. */
  158. public static void operatorAction(ActionEvent arg0) {
  159. Main.getInstance().getGraphManager().deselectEdgeCreationNodes();
  160. switchfromSymbolLayer();
  161. GraphDisplayManager.setCurrentLayer(Layer.OPERATOR);
  162. GraphDisplayManager.switchActiveGraph();
  163. ToolboxManager.setOperatorItems();
  164. setBorderStyle((Button) arg0.getSource());
  165. controller.open.setText("Open...");
  166. controller.newItem.disableProperty().set(false);
  167. controller.open.disableProperty().set(false);
  168. controller.add.disableProperty().set(false);
  169. controller.save.disableProperty().set(false);
  170. controller.saveAs.disableProperty().set(false);
  171. controller.delete.disableProperty().set(false);
  172. controller.undelete.disableProperty().set(false);
  173. controller.updateMetricMI.disableProperty().set(true);
  174. // hide metricbox/update button/reset mapping button
  175. controller.rightSide.getChildren().remove(controller.updateButtonAPane);
  176. controller.metricbox.setVisible(false);
  177. controller.leftSide.getChildren().remove(controller.resetMappingButtonAPane);
  178. // show operator graph selection box
  179. controller.opGraphSelectionBox.setVisible(true);
  180. }
  181. /**
  182. * Handler for the Mapping Layer switch Button.
  183. */
  184. public static void mappingAction(ActionEvent arg0) {
  185. Main.getInstance().getGraphManager().deselectEdgeCreationNodes();
  186. // show metricbox/update button/reset mapping button
  187. if (!(GraphDisplayManager.getCurrentLayer().equals(Layer.MAPPING))) {
  188. controller.rightSide.getChildren().add(2, controller.updateButtonAPane);
  189. controller.metricbox.setVisible(true);
  190. controller.leftSide.getChildren().add(1, controller.resetMappingButtonAPane);
  191. }
  192. switchfromSymbolLayer();
  193. GraphDisplayManager.setCurrentLayer(Layer.MAPPING);
  194. GraphDisplayManager.switchActiveGraph();
  195. ToolboxManager.setMappingItems();
  196. setBorderStyle((Button) arg0.getSource());
  197. controller.open.setText("Open Mapping...");
  198. controller.newItem.disableProperty().set(true);
  199. controller.open.disableProperty().set(false);
  200. controller.add.disableProperty().set(true);
  201. controller.save.disableProperty().set(false);
  202. controller.saveAs.disableProperty().set(false);
  203. controller.delete.disableProperty().set(false);
  204. controller.undelete.disableProperty().set(false);
  205. controller.updateMetricMI.disableProperty().set(false);
  206. // Hide operator graph selection box
  207. controller.opGraphSelectionBox.setVisible(false);
  208. }
  209. /**
  210. * Handler for the Symbol Representation Layer switch Button.
  211. */
  212. public static void symbolRepAction(ActionEvent arg0) {
  213. Main.getInstance().getGraphManager().deselectEdgeCreationNodes();
  214. PropertiesManager.showNewDataSet(null);
  215. if (!(GraphDisplayManager.getCurrentLayer().equals(Layer.SYMBOL))) {
  216. GraphDisplayManager.setCurrentLayer(Layer.SYMBOL);
  217. controller.topLeftAPane.getChildren().add(controller.symbolToolVBox);
  218. }
  219. // load world view
  220. if (!activateWorldView()) {
  221. // show "Connection Error" message, because of problems during
  222. // connecting attempt to server
  223. showConnectionErrorMsg();
  224. }
  225. // hide metricbox/update button/reset mapping button
  226. controller.rightSide.getChildren().remove(controller.updateButtonAPane);
  227. controller.metricbox.setVisible(false);
  228. controller.leftSide.getChildren().remove(controller.resetMappingButtonAPane);
  229. // Hide operator graph selection box
  230. controller.opGraphSelectionBox.setVisible(false);
  231. GraphDisplayManager.switchActiveGraph();
  232. setBorderStyle((Button) arg0.getSource());
  233. controller.open.setText("Open...");
  234. controller.newItem.disableProperty().set(true);
  235. controller.open.disableProperty().set(true);
  236. controller.add.disableProperty().set(true);
  237. controller.save.disableProperty().set(true);
  238. controller.saveAs.disableProperty().set(true);
  239. controller.delete.disableProperty().set(true);
  240. controller.undelete.disableProperty().set(true);
  241. controller.updateMetricMI.disableProperty().set(true);
  242. }
  243. /**
  244. * show an Alert dialog when OpenStreetMap could not be loaded
  245. */
  246. public static void showConnectionErrorMsg() {
  247. Alert alert = new Alert(AlertType.WARNING);
  248. alert.setTitle("Warning");
  249. alert.setHeaderText("Connection Error");
  250. alert.setContentText("Could not reach OpenStreetMap server");
  251. alert.showAndWait();
  252. }
  253. /**
  254. * Initializes the WorldView, sets data and paints them.
  255. *
  256. * @throws IOException
  257. */
  258. private static boolean activateWorldView() {
  259. // dont show graph and toolbox
  260. controller.toolbox.setVisible(false);
  261. controller.swingNode.setVisible(false);
  262. // make properties uneditable
  263. controller.propertiesObjectColumn.setEditable(false);
  264. // make map view non mouse transparent
  265. controller.stackPane.setMouseTransparent(false);
  266. controller.swingNodeWorldView.setMouseTransparent(false);
  267. // make graph mouse transparent
  268. controller.pane.setMouseTransparent(true);
  269. controller.swingNode.setMouseTransparent(true);
  270. // show VBox for map options
  271. controller.symbolToolVBox.setVisible(true);
  272. // show symbol layer Button
  273. controller.centerMap.setVisible(true);
  274. controller.defaultMapView.setVisible(true);
  275. controller.roadMapView.setVisible(true);
  276. controller.satelliteMapView.setVisible(true);
  277. controller.hybridMapView.setVisible(true);
  278. controller.previousWaypoint.setVisible(true);
  279. controller.nextWaypoint.setVisible(true);
  280. // standard server connection status is true
  281. Boolean serverConnection = true;
  282. try {
  283. WorldView.loadWorldView();
  284. } catch (IOException e) {
  285. // problems with server connection -> show error message
  286. serverConnection = false;
  287. }
  288. MapViewFunctions.checkVBoxChanged();
  289. WorldView.internMapViewer.repaint();
  290. // set content to UI Element
  291. controller.swingNodeWorldView.setContent(WorldView.internMapViewer);
  292. controller.swingNodeWorldView.setVisible(true);
  293. return serverConnection;
  294. }
  295. /**
  296. * Functionality for "edge visible" Checkbox.
  297. *
  298. * @param ov
  299. * @param oldVal
  300. * Checkbox previous state (Checked or unchecked)
  301. * @param newVal
  302. * Checkbox current state (Checked or unchecked)
  303. */
  304. public static void edgeVisibilitySwitcher(ObservableValue<? extends Boolean> ov, Boolean oldVal, Boolean newVal) {
  305. // Show edges
  306. if (newVal) {
  307. WorldView.edgePainter.setShowEdges(true);
  308. WorldView.internMapViewer.repaint();
  309. } else {
  310. // Hide edges
  311. WorldView.edgePainter.setShowEdges(false);
  312. WorldView.internMapViewer.repaint();
  313. }
  314. }
  315. /**
  316. * Functionality for "label visible" Checkbox.
  317. *
  318. * @param ov
  319. * @param oldVal
  320. * Checkbox previous state (Checked or unchecked)
  321. * @param newVal
  322. * Checkbox current state (Checked or unchecked)
  323. */
  324. public static void labelVisibilitySwitcher(ObservableValue<? extends Boolean> ov, Boolean oldVal, Boolean newVal) {
  325. WaypointPainter<CustomWaypoint> waypointPainter = WorldView.waypointPainter;
  326. CustomWaypointRenderer renderer = new CustomWaypointRenderer();
  327. if (newVal) {
  328. // Show node labels
  329. renderer.setShowLabels(true);
  330. waypointPainter.clearCache();
  331. waypointPainter.setRenderer(renderer);
  332. WorldView.internMapViewer.repaint();
  333. } else {
  334. // Hide node labels
  335. renderer.setShowLabels(false);
  336. waypointPainter.clearCache();
  337. waypointPainter.setRenderer(renderer);
  338. WorldView.internMapViewer.repaint();
  339. }
  340. }
  341. /**
  342. * Functionality for "edge weights visible" Checkbox.
  343. *
  344. * @param ov
  345. * @param oldVal
  346. * Checkbox previous state (Checked or unchecked)
  347. * @param newVal
  348. * Checkbox current state (Checked or unchecked)
  349. */
  350. public static void edgeWeightVisibilitySwitcher(ObservableValue<? extends Boolean> ov, Boolean oldVal,
  351. Boolean newVal) {
  352. if (newVal) {
  353. // Show Edges weights
  354. WorldView.edgePainter.setShowWeights(true);
  355. WorldView.internMapViewer.repaint();
  356. } else {
  357. // Hide Edges weights
  358. WorldView.edgePainter.setShowWeights(false);
  359. WorldView.internMapViewer.repaint();
  360. }
  361. }
  362. /**
  363. * Changes the border of the button that was pressed to red.
  364. *
  365. * @param currentButton
  366. * the button that was pressed
  367. */
  368. private static void setBorderStyle(Button currentButton) {
  369. for (Button j : layerButtons) {
  370. if (j.equals(currentButton)) {
  371. j.setStyle(
  372. "-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;");
  373. } else {
  374. j.setStyle("-fx-border-width: 0;");
  375. }
  376. }
  377. }
  378. /**
  379. * update mapViewer if choiceBox item was changed.
  380. *
  381. * @param ov
  382. * The observed Value
  383. * @param oldVal
  384. * Its old Value
  385. * @param newVal
  386. * Its new Value
  387. */
  388. /*
  389. * public static void mapViewChoiceChange(ObservableValue<? extends String>
  390. * ov, String oldVal, String newVal) { MapViewFunctions.changeMapView(); }
  391. *
  392. * /** select the given MapType in the ChoiceBox and change Map View
  393. *
  394. * @param mapType
  395. */
  396. /*
  397. * public static void switchToMap(String mapType) {
  398. * controller.mapViewChoiceBox.getSelectionModel().select(mapType);
  399. * MapViewFunctions.changeMapView(); }
  400. */
  401. public static void setupOpGraphComboBox() {
  402. Platform.runLater(() -> {
  403. controller.opGraphSelectionBox.getItems().clear();
  404. GraphManager operatorManager = GraphDisplayManager.getGraphManager(Layer.OPERATOR);
  405. for (MyGraph g : operatorManager.getGraph().getAllSubGraphs().stream().filter((g) -> !g.isComposite())
  406. .collect(Collectors.toList())) {
  407. controller.opGraphSelectionBox.getItems().add(g.getId());
  408. }
  409. controller.opGraphSelectionBox.getItems().add("Add...");
  410. controller.opGraphSelectionBox.setValue(controller.opGraphSelectionBox.getItems().get(0));
  411. });
  412. }
  413. public static void opGraphSelectedAction(ActionEvent v) {
  414. if (controller.opGraphSelectionBox.getValue() == null || controller.opGraphSelectionBox.getValue().equals("")) {
  415. return;
  416. }
  417. if (controller.opGraphSelectionBox.getValue().equals("Add...")) {
  418. MenuBarManager.addAction(v);
  419. Platform.runLater(() -> controller.opGraphSelectionBox.setValue(controller.opGraphSelectionBox.getItems()
  420. .get(controller.opGraphSelectionBox.getItems().size() - 2)));
  421. } else {
  422. GraphDisplayManager.getGraphManager(Layer.OPERATOR)
  423. .setActiveSubGraph(controller.opGraphSelectionBox.getValue());
  424. }
  425. }
  426. }