OperatorManager.java 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package de.tu_darmstadt.informatik.tk.scopviz.ui;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import de.tu_darmstadt.informatik.tk.scopviz.main.Main;
  5. import de.tu_darmstadt.informatik.tk.scopviz.metrics.BasicMappingOperator;
  6. import de.tu_darmstadt.informatik.tk.scopviz.metrics.TestOperator;
  7. import de.tu_darmstadt.informatik.tk.scopviz.metrics.interfaces.ScopvizGraphOperator;
  8. import javafx.application.Platform;
  9. import javafx.collections.FXCollections;
  10. import javafx.geometry.Insets;
  11. import javafx.scene.control.ButtonBar.ButtonData;
  12. import javafx.scene.control.ButtonType;
  13. import javafx.scene.control.ChoiceBox;
  14. import javafx.scene.control.Dialog;
  15. import javafx.scene.control.Label;
  16. import javafx.scene.layout.GridPane;
  17. public class OperatorManager {
  18. private static HashMap<String, ScopvizGraphOperator> operators = new HashMap<String, ScopvizGraphOperator>();
  19. /**
  20. * Initializes all GraphOperators for employment
  21. *
  22. * ****Central method to add a new metric***** Add line: addOperator(new
  23. * YourMetric()); for using it in the Operatordialog
  24. * **************************************************************
  25. *
  26. */
  27. private static void initializeGraphOperators() {
  28. addOperator(new TestOperator());
  29. addOperator(new BasicMappingOperator());
  30. }
  31. /**
  32. * opens a dialog allowing the User to choose which GraphOperator he wants to use on the current Graph
  33. */
  34. public static void openOperatorsDialog() {
  35. Dialog<ArrayList<String>> addPropDialog = new Dialog<>();
  36. addPropDialog.setTitle("GraphOperators");
  37. ButtonType addButtonType = new ButtonType("invoke on current graph", ButtonData.OK_DONE);
  38. addPropDialog.getDialogPane().getButtonTypes().addAll(addButtonType, ButtonType.CANCEL);
  39. // create grid
  40. GridPane grid = new GridPane();
  41. grid.setHgap(10);
  42. grid.setVgap(10);
  43. grid.setPadding(new Insets(20, 150, 10, 10));
  44. // set DropDown Menu
  45. ChoiceBox<String> operatorChooser = new ChoiceBox<>();
  46. operatorChooser.setItems(FXCollections.observableArrayList(operators.keySet()));
  47. // adding elements to grid
  48. grid.add(new Label("Please select the operator you want to invoke on the current Gaph"), 0, 0);
  49. grid.add(operatorChooser, 0, 1);
  50. addPropDialog.getDialogPane().setContent(grid);
  51. Platform.runLater(() -> operatorChooser.requestFocus());
  52. // get new property values
  53. addPropDialog.setResultConverter(dialogButton -> {
  54. if (dialogButton == addButtonType) {
  55. operators.get(operatorChooser.getSelectionModel().getSelectedItem())
  56. .calculate(Main.getInstance().getGraphManager());
  57. return null;
  58. } else
  59. return null;
  60. });
  61. addPropDialog.showAndWait();
  62. }
  63. /**
  64. * Adds an Operator to the HashMap
  65. *
  66. * @param op the Operator that will be added
  67. */
  68. public static void addOperator(ScopvizGraphOperator op) {
  69. operators.put(op.getName(), op);
  70. }
  71. /**
  72. * initializes the OperatorManager
  73. *
  74. * @param g the guiController of the Programm
  75. */
  76. public static void initialize(GUIController g) {
  77. initializeGraphOperators();
  78. }
  79. }