ButtonManager.java 15 KB

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