OptionsManager.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package de.tu_darmstadt.informatik.tk.scopviz.ui;
  2. import java.util.ArrayList;
  3. import de.tu_darmstadt.informatik.tk.scopviz.debug.Debug;
  4. import de.tu_darmstadt.informatik.tk.scopviz.main.Layer;
  5. import de.tu_darmstadt.informatik.tk.scopviz.main.Main;
  6. import javafx.application.Platform;
  7. import javafx.collections.FXCollections;
  8. import javafx.geometry.Insets;
  9. import javafx.scene.control.ButtonBar.ButtonData;
  10. import javafx.scene.control.ButtonType;
  11. import javafx.scene.control.ChoiceBox;
  12. import javafx.scene.control.Dialog;
  13. import javafx.scene.control.Label;
  14. import javafx.scene.control.RadioButton;
  15. import javafx.scene.control.TextField;
  16. import javafx.scene.layout.GridPane;
  17. /**
  18. * manages the settings of the program also stores the constants
  19. *
  20. * @author Jascha Bohne
  21. * @version 1.0.0.0
  22. */
  23. public final class OptionsManager {
  24. // SETTINGS
  25. /** The Default Weight for all new Edges. */
  26. private static int defaultWeight = 0;
  27. /** Flag whether to show the weight labels on Edges. */
  28. private static boolean showWeight = true;
  29. // Layer stylesheets
  30. /**
  31. * Private Constructor to prevent Instantiation.
  32. */
  33. private OptionsManager() {
  34. }
  35. /**
  36. * opens a dialog that can be used to edit options
  37. */
  38. public static void openOptionsDialog() {
  39. // Create new Dialog
  40. Dialog<ArrayList<String>> addPropDialog = new Dialog<>();
  41. addPropDialog.setTitle("Preferences");
  42. ButtonType addButtonType = new ButtonType("save & exit", ButtonData.OK_DONE);
  43. addPropDialog.getDialogPane().getButtonTypes().addAll(addButtonType, ButtonType.CANCEL);
  44. // create grid
  45. GridPane grid = new GridPane();
  46. grid.setHgap(10);
  47. grid.setVgap(10);
  48. grid.setPadding(new Insets(20, 150, 10, 10));
  49. // create dialog elements
  50. TextField defaultWeightField = new TextField();
  51. defaultWeightField.setPromptText(Integer.toString(defaultWeight));
  52. RadioButton showWeightButton = new RadioButton();
  53. showWeightButton.setSelected(showWeight);
  54. ChoiceBox<String> nodeGraphicsSelector = new ChoiceBox<String>();
  55. nodeGraphicsSelector.setItems(FXCollections.observableArrayList(StylesheetManager.getAllNodeGraphics()[0],
  56. StylesheetManager.getAllNodeGraphics()[1]));
  57. nodeGraphicsSelector.getSelectionModel().select(StylesheetManager.getNodeGraphics());
  58. ;
  59. // position elements on grid
  60. grid.add(new Label("Default weight of edges:"), 0, 0);
  61. grid.add(defaultWeightField, 1, 0);
  62. grid.add(new Label("Show weight of edges in the eraph viewer"), 0, 1);
  63. grid.add(showWeightButton, 1, 1);
  64. grid.add(new Label("Node display:"), 0, 2);
  65. grid.add(nodeGraphicsSelector, 1, 2);
  66. // set dialog
  67. addPropDialog.getDialogPane().setContent(grid);
  68. Platform.runLater(() -> defaultWeightField.requestFocus());
  69. // get new property values
  70. addPropDialog.setResultConverter(dialogButton -> {
  71. if (dialogButton == addButtonType) {
  72. try {
  73. defaultWeight = Integer.parseInt(defaultWeightField.getText());
  74. } catch (NumberFormatException e) {
  75. }
  76. showWeight = showWeightButton.isSelected();
  77. StylesheetManager.adjustNodeGraphics(nodeGraphicsSelector.getValue());
  78. return null;
  79. } else
  80. return null;
  81. });
  82. addPropDialog.showAndWait();
  83. }
  84. /**
  85. * Returns the default weight for new Edges.
  86. *
  87. * @return the default weight
  88. */
  89. public static int getDefaultWeight() {
  90. return defaultWeight;
  91. }
  92. /**
  93. * Sets the default weight for new Edges.
  94. *
  95. * @param defaultWeight
  96. * the defaultWeight to set
  97. */
  98. public static void setDefaultWeight(int defaultWeight) {
  99. OptionsManager.defaultWeight = defaultWeight;
  100. }
  101. /**
  102. * Returns whether Edge weight should be displayed as labels.
  103. *
  104. * @return true if weight should be shown, false otherwise
  105. */
  106. public static boolean isWeightShown() {
  107. return showWeight;
  108. }
  109. /**
  110. * Sets the Flag whether Edge weight should be displayed as labels.
  111. *
  112. * @param showWeight
  113. * the showWeight to set
  114. */
  115. public static void setShowWeight(boolean showWeight) {
  116. OptionsManager.showWeight = showWeight;
  117. }
  118. }