PropertiesManager.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. package de.tu_darmstadt.informatik.tk.scopviz.ui;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.HashSet;
  5. import java.util.LinkedList;
  6. import java.util.Optional;
  7. import org.graphstream.graph.Edge;
  8. import org.graphstream.graph.Element;
  9. import org.graphstream.graph.Node;
  10. import de.tu_darmstadt.informatik.tk.scopviz.debug.Debug;
  11. import de.tu_darmstadt.informatik.tk.scopviz.graphs.GraphHelper;
  12. import de.tu_darmstadt.informatik.tk.scopviz.graphs.GraphManager;
  13. import de.tu_darmstadt.informatik.tk.scopviz.main.Layer;
  14. import de.tu_darmstadt.informatik.tk.scopviz.main.Main;
  15. import javafx.application.Platform;
  16. import javafx.beans.binding.Bindings;
  17. import javafx.collections.FXCollections;
  18. import javafx.collections.ObservableList;
  19. import javafx.event.EventHandler;
  20. import javafx.geometry.Insets;
  21. import javafx.scene.control.Alert;
  22. import javafx.scene.control.Alert.AlertType;
  23. import javafx.scene.control.ButtonBar.ButtonData;
  24. import javafx.scene.control.ButtonType;
  25. import javafx.scene.control.ChoiceBox;
  26. import javafx.scene.control.ContextMenu;
  27. import javafx.scene.control.Dialog;
  28. import javafx.scene.control.Label;
  29. import javafx.scene.control.MenuItem;
  30. import javafx.scene.control.TableColumn.CellEditEvent;
  31. import javafx.scene.control.TableRow;
  32. import javafx.scene.control.TableView;
  33. import javafx.scene.control.TextField;
  34. import javafx.scene.layout.GridPane;
  35. import javafx.util.Callback;
  36. /**
  37. * Manager for the Properties pane and its contents.
  38. *
  39. * @author Julian Ohl, Dominik Renkel
  40. * @version 1.6
  41. *
  42. */
  43. public final class PropertiesManager {
  44. /** Regex for detecting whether a String represent an Integer. */
  45. public static final String IS_INT = "^(-)?\\d+$";
  46. /** Regex for detecting whether a String represents a Boolean. */
  47. public static final String IS_BOOL = "^true$|^false$";
  48. /**
  49. * Regex for detecting whether a String represents a floating point number.
  50. */
  51. public static final String IS_FLOAT = "^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?$";
  52. /** The Table of Attributes. */
  53. private static TableView<KeyValuePair> properties;
  54. /** Flag whether the name has been set. */
  55. public static boolean nameSet;
  56. /** Flag whether the value has been set. */
  57. public static boolean valueSet;
  58. public static HashSet<TableRow<KeyValuePair>> tableRows = new HashSet<TableRow<KeyValuePair>>();
  59. /**
  60. * list for organizing items in the properties window in a specific order
  61. */
  62. private static LinkedList<String> itemOrderRules = new LinkedList<String>();
  63. /** hashmap for filtering items out of the properties window */
  64. private static HashMap<String, Integer> itemVisibilityRules = new HashMap<String, Integer>();
  65. /**
  66. * Private Constructor to prevent Instantiation.
  67. */
  68. private PropertiesManager() {
  69. }
  70. /**
  71. * Initializes the Manager by adding the list of properties to display into
  72. * the properties pane.
  73. *
  74. * @param propertiesInput
  75. * The list of properties to display
  76. */
  77. public static void initializeItems(TableView<KeyValuePair> propertiesInput) {
  78. properties = propertiesInput;
  79. setItemRules();
  80. }
  81. /**
  82. * setting up the rules for the items displayed in the properties window
  83. *
  84. * ****************************************************** add properties
  85. * here for grouping or filtering them out
  86. * ******************************************************
  87. */
  88. private static void setItemRules() {
  89. // setting the order for specific properties
  90. itemOrderRules.add("weight");
  91. itemOrderRules.add("ID");
  92. itemOrderRules.add("typeofNode");
  93. itemOrderRules.add("typeofDevice");
  94. itemOrderRules.add("x");
  95. itemOrderRules.add("y");
  96. itemOrderRules.add("lat");
  97. itemOrderRules.add("long");
  98. // properties, which shall be filtered out of the properties window
  99. itemVisibilityRules.put("layout.frozen", -1);
  100. itemVisibilityRules.put("ui.style", -1);
  101. itemVisibilityRules.put("ui.j2dsk", -1);
  102. itemVisibilityRules.put("ui.clicked", -1);
  103. itemVisibilityRules.put("ui.map.selected", -1);
  104. itemVisibilityRules.put("xyz", -1);
  105. // properties, which shall be filtered out of the properties window ,
  106. // only if debug is disabled
  107. itemVisibilityRules.put("mapping", -2);
  108. itemVisibilityRules.put("mapping-parent", -2);
  109. itemVisibilityRules.put("mapping-parent-id", -2);
  110. itemVisibilityRules.put("ui.class", -2);
  111. itemVisibilityRules.put("originalElement", -2);
  112. }
  113. /**
  114. * Update Properties of selected Node/Edge, if a any Property was changed.
  115. */
  116. public static final EventHandler<CellEditEvent<KeyValuePair, String>> setOnEditCommitHandler = new EventHandler<CellEditEvent<KeyValuePair, String>>() {
  117. @Override
  118. public void handle(CellEditEvent<KeyValuePair, String> t) {
  119. KeyValuePair editedPair = t.getTableView().getItems().get(t.getTablePosition().getRow());
  120. Object classType = editedPair.getClassType();
  121. String key = editedPair.getKey();
  122. // handling the problem when using his own names for properties
  123. // needed by graphstream
  124. // e.g. "ui.label" as "ID", might need an extra function/structure
  125. // if more of these are added
  126. if (key.equals("ID")) {
  127. key = "ui.label";
  128. }
  129. String oldValue = t.getOldValue();
  130. String newValue = t.getNewValue();
  131. Element selected = getSelected();
  132. // Type-Check the input
  133. if (classType.equals(Integer.class) && newValue.matches(IS_INT)) {
  134. GraphHelper.propagateAttribute(Main.getInstance().getGraphManager().getGraph(), selected, key,
  135. newValue);
  136. selected.changeAttribute(key, Integer.valueOf(newValue));
  137. editedPair.setValue(newValue);
  138. Debug.out("Edited integer Attribute " + key);
  139. } else if (classType.equals(Boolean.class) && newValue.matches(IS_BOOL)) {
  140. GraphHelper.propagateAttribute(Main.getInstance().getGraphManager().getGraph(), selected, key,
  141. newValue);
  142. selected.changeAttribute(key, Boolean.valueOf(newValue));
  143. editedPair.setValue(newValue);
  144. Debug.out("Edited boolean Attribute " + key);
  145. } else if (classType.equals(Float.class) && newValue.matches(IS_FLOAT)) {
  146. GraphHelper.propagateAttribute(Main.getInstance().getGraphManager().getGraph(), selected, key,
  147. newValue);
  148. selected.changeAttribute(key, Float.valueOf(newValue));
  149. editedPair.setValue(newValue);
  150. Debug.out("Edited float Attribute " + key);
  151. } else if (classType.equals(Double.class) && newValue.matches(IS_FLOAT)) {
  152. GraphHelper.propagateAttribute(Main.getInstance().getGraphManager().getGraph(), selected, key,
  153. newValue);
  154. selected.changeAttribute(key, Double.valueOf(newValue));
  155. editedPair.setValue(newValue);
  156. Debug.out("Edited double Attribute " + key);
  157. } else if (classType.equals(String.class)) {
  158. GraphHelper.propagateAttribute(Main.getInstance().getGraphManager().getGraph(), selected, key,
  159. newValue);
  160. selected.changeAttribute(key, newValue);
  161. editedPair.setValue(newValue);
  162. Debug.out("Edited String Attribute " + key);
  163. if (key.equals("typeofNode")) {
  164. selected.changeAttribute("ui.class", newValue);
  165. }
  166. } else {
  167. editedPair.setValue(oldValue);
  168. t.getTableView().getItems().get(t.getTablePosition().getRow()).setKey(oldValue);
  169. setItemsProperties();
  170. Debug.out("WARNING: invalid input for this attribute type", 2);
  171. }
  172. // Unselect row after updating Property
  173. properties.getSelectionModel().clearSelection();
  174. }
  175. };
  176. /**
  177. * Callback to be executed when a right click occurs on the table.
  178. */
  179. public static Callback<TableView<KeyValuePair>, TableRow<KeyValuePair>> rightClickCallback = new Callback<TableView<KeyValuePair>, TableRow<KeyValuePair>>() {
  180. @Override
  181. public TableRow<KeyValuePair> call(TableView<KeyValuePair> tableView) {
  182. final TableRow<KeyValuePair> row = new TableRow<>();
  183. // ContextMenu on non empty rows (add & delete)
  184. final ContextMenu menuOnNonEmptyRows = new ContextMenu();
  185. final MenuItem addPropMenuItem = new MenuItem("Add..");
  186. final MenuItem deletePropMenuItem = new MenuItem("Delete");
  187. // ContextMenu on empty rows (only add)
  188. final ContextMenu menuOnEmptyRows = new ContextMenu();
  189. final MenuItem onlyAddPropMenuItem = new MenuItem("Add..");
  190. // add functionality
  191. onlyAddPropMenuItem.setOnAction((event) -> addPropFunctionality(null));
  192. addPropMenuItem.setOnAction((event) -> addPropFunctionality(null));
  193. // delete functionality
  194. deletePropMenuItem.setOnAction((event) -> {
  195. Debug.out("Remove Element");
  196. removeProperty(row.getItem());
  197. properties.getItems().remove(row.getItem());
  198. });
  199. // Disable MenuItem in symbol layer
  200. onlyAddPropMenuItem.disableProperty().bind(GraphDisplayManager.inSymbolLayerProperty());
  201. addPropMenuItem.disableProperty().bind(GraphDisplayManager.inSymbolLayerProperty());
  202. deletePropMenuItem.disableProperty().bind(GraphDisplayManager.inSymbolLayerProperty());
  203. // add MenuItem to ContextMenu
  204. menuOnEmptyRows.getItems().add(onlyAddPropMenuItem);
  205. menuOnNonEmptyRows.getItems().addAll(addPropMenuItem, deletePropMenuItem);
  206. // when empty row right-clicked open special menu (only add),
  207. // otherwise normal menu (add & delete)
  208. row.contextMenuProperty().bind(Bindings.when(Bindings.isNotNull(row.itemProperty()))
  209. .then(menuOnNonEmptyRows).otherwise(menuOnEmptyRows));
  210. tableRows.add(row);
  211. return row;
  212. }
  213. };
  214. /**
  215. * Sets Property-TableView Elements to selected Node or Edge Properties.
  216. */
  217. public static void setItemsProperties() {
  218. String nid = Main.getInstance().getGraphManager().getSelectedNodeID();
  219. String eid = Main.getInstance().getGraphManager().getSelectedEdgeID();
  220. if (nid != null) {
  221. Node selectedNode = Main.getInstance().getGraphManager().getGraph().getNode(nid);
  222. showNewDataSet(selectedNode);
  223. } else if (eid != null) {
  224. Edge selectedEdge = Main.getInstance().getGraphManager().getGraph().getEdge(eid);
  225. showNewDataSet(selectedEdge);
  226. } else {
  227. return;
  228. }
  229. }
  230. /**
  231. * Add properties of selected Node or Edge to Properties TableView.
  232. *
  233. * @param selected
  234. * selected Node or Edge
  235. * @param newData
  236. */
  237. public static void showNewDataSet(Element selected) {
  238. ObservableList<KeyValuePair> newData = FXCollections.observableArrayList();
  239. if (selected == null) {
  240. properties.setItems(newData);
  241. return;
  242. }
  243. // fix for concurrentModification exception
  244. String[] temp = new String[0];
  245. temp = selected.getAttributeKeySet().toArray(temp);
  246. for (int i = 0; i < temp.length; i++) {
  247. String key = temp[i];
  248. switch (key) {
  249. // filter out or change attributes added by graphstream that are of
  250. // no use to the user
  251. case "ui.label":
  252. if (selected instanceof Node) {
  253. Object actualAttribute = selected.getAttribute(key);
  254. // replace UI Label with ID"
  255. key = "ID";
  256. newData.add(0, new KeyValuePair(key, String.valueOf(actualAttribute), actualAttribute.getClass()));
  257. }
  258. break;
  259. case "weight":
  260. if (selected instanceof Edge
  261. && Layer.OPERATOR == Main.getInstance().getGraphManager().getGraph().getAttribute("layer")) {
  262. break;
  263. }
  264. Object actualAttribute = selected.getAttribute(key);
  265. if (actualAttribute != null) {
  266. newData.add(new KeyValuePair(key, String.valueOf(actualAttribute), actualAttribute.getClass()));
  267. }
  268. break;
  269. case "process-need":
  270. if (selected instanceof Node
  271. && Layer.UNDERLAY == Main.getInstance().getGraphManager().getGraph().getAttribute("layer")) {
  272. break;
  273. }
  274. actualAttribute = selected.getAttribute(key);
  275. if (actualAttribute != null) {
  276. newData.add(new KeyValuePair(key, String.valueOf(actualAttribute), actualAttribute.getClass()));
  277. }
  278. break;
  279. case "process-max":
  280. if (selected instanceof Node
  281. && Layer.OPERATOR == Main.getInstance().getGraphManager().getGraph().getAttribute("layer")) {
  282. break;
  283. }
  284. case "typeOfDevice":
  285. if (selected instanceof Node
  286. && Layer.OPERATOR == Main.getInstance().getGraphManager().getGraph().getAttribute("layer")) {
  287. break;
  288. }
  289. default:
  290. actualAttribute = selected.getAttribute(key);
  291. if (actualAttribute != null) {
  292. newData.add(new KeyValuePair(key, String.valueOf(actualAttribute), actualAttribute.getClass()));
  293. }
  294. break;
  295. }
  296. }
  297. properties.setItems(groupProperties(newData));
  298. }
  299. /**
  300. * Get the selected node or edge from the GraphManager.
  301. *
  302. * @return selected node or egde
  303. */
  304. private static Element getSelected() {
  305. GraphManager viz = Main.getInstance().getGraphManager();
  306. String nid = viz.getSelectedNodeID();
  307. String eid = viz.getSelectedEdgeID();
  308. if (nid != null) {
  309. return viz.getGraph().getNode(nid);
  310. } else if (eid != null) {
  311. return viz.getGraph().getEdge(eid);
  312. } else {
  313. return null;
  314. }
  315. }
  316. /**
  317. * Delete a given Pair from the current Node or Edge.
  318. *
  319. * @param pair
  320. * selectedProperty
  321. */
  322. private static void removeProperty(KeyValuePair pair) {
  323. Element selected = getSelected();
  324. selected.removeAttribute(pair.getKey());
  325. GraphHelper.propagateAttribute(Main.getInstance().getGraphManager().getGraph(), selected, pair.getKey(), null);
  326. }
  327. /**
  328. * groups and filters a list of items according to the order and visibility
  329. * rules
  330. *
  331. * @param data
  332. * a list of property items
  333. * @return the data with the rules applied
  334. */
  335. private static ObservableList<KeyValuePair> groupProperties(ObservableList<KeyValuePair> data) {
  336. ObservableList<KeyValuePair> newData = FXCollections.observableArrayList();
  337. ;
  338. // adds all items in the order of the rules. Ordered items as an extra
  339. // list, removed from data
  340. for (String s : itemOrderRules) {
  341. for (int i = 0; i < data.size(); i++) {
  342. KeyValuePair kvp = data.get(i);
  343. if (kvp.getKey().equals(s)) {
  344. newData.add(kvp);
  345. data.remove(kvp);
  346. }
  347. }
  348. }
  349. // filters items according to the rules. Filters on the data without the
  350. // ordered items
  351. for (String key : itemVisibilityRules.keySet()) {
  352. for (int i = 0; i < data.size(); i++) {
  353. KeyValuePair kvp = data.get(i);
  354. if (kvp.getKey().equals(key)) {
  355. if (itemVisibilityRules.get(kvp.getKey()) == -1) {
  356. data.remove(kvp);
  357. }
  358. else if (itemVisibilityRules.get(kvp.getKey()) == -2) {
  359. if (!Debug.DEBUG_ENABLED) {
  360. data.remove(kvp);
  361. }
  362. }
  363. break;
  364. }
  365. }
  366. }
  367. // adds the filtered data without the ordered items behind the ordered
  368. // items
  369. newData.addAll(data);
  370. return newData;
  371. }
  372. /**
  373. * TODO Auslagern contextMenu add button functionality.
  374. */
  375. private static void addPropFunctionality(String preConfigPropName) {
  376. Debug.out("Add Element");
  377. // Create new Dialog
  378. Dialog<ArrayList<String>> addPropDialog = new Dialog<>();
  379. addPropDialog.setTitle("Add Property");
  380. addPropDialog.setHeaderText("Choose your Property Details");
  381. // Alert window -> when problems with input
  382. Alert alert = new Alert(AlertType.WARNING);
  383. alert.setTitle("Warning");
  384. alert.setHeaderText("Property-Type Alert");
  385. alert.setContentText("The selected Type doesnt fit the Input");
  386. ButtonType addButtonType = new ButtonType("Confirm", ButtonData.OK_DONE);
  387. addPropDialog.getDialogPane().getButtonTypes().addAll(addButtonType, ButtonType.CANCEL);
  388. // create grid
  389. GridPane grid = new GridPane();
  390. grid.setHgap(10);
  391. grid.setVgap(10);
  392. grid.setPadding(new Insets(20, 150, 10, 10));
  393. // create dialog elements
  394. TextField name = new TextField();
  395. name.setPromptText("Name");
  396. TextField value = new TextField();
  397. value.setPromptText("Value");
  398. ChoiceBox<String> type = new ChoiceBox<String>();
  399. type.setItems(FXCollections.observableArrayList("Integer", "Float", "String", "Boolean"));
  400. type.getSelectionModel().selectFirst();
  401. // position elements on grid
  402. grid.add(new Label("Property Name:"), 0, 0);
  403. grid.add(name, 1, 0);
  404. grid.add(new Label("Property Value:"), 0, 1);
  405. grid.add(value, 1, 1);
  406. grid.add(new Label("Property Type:"), 0, 2);
  407. grid.add(type, 1, 2);
  408. javafx.scene.Node confirmButton = addPropDialog.getDialogPane().lookupButton(addButtonType);
  409. confirmButton.setDisable(true);
  410. nameSet = false;
  411. valueSet = false;
  412. // show pre defined property name
  413. if (preConfigPropName != null) {
  414. name.setText(preConfigPropName);
  415. PropertiesManager.nameSet = true;
  416. }
  417. // hide confirm button, when textfields empty
  418. name.textProperty().addListener((observable, oldValue, newValue) -> {
  419. PropertiesManager.nameSet = true;
  420. if (newValue.trim().isEmpty()) {
  421. PropertiesManager.nameSet = false;
  422. confirmButton.setDisable(true);
  423. } else if (PropertiesManager.valueSet) {
  424. confirmButton.setDisable(false);
  425. }
  426. });
  427. value.textProperty().addListener((observable, oldValue, newValue) -> {
  428. PropertiesManager.valueSet = true;
  429. if (newValue.trim().isEmpty()) {
  430. PropertiesManager.valueSet = false;
  431. confirmButton.setDisable(true);
  432. } else if (PropertiesManager.nameSet) {
  433. confirmButton.setDisable(false);
  434. }
  435. });
  436. // set dialog
  437. addPropDialog.getDialogPane().setContent(grid);
  438. Platform.runLater(() -> name.requestFocus());
  439. // get new property values
  440. addPropDialog.setResultConverter(dialogButton -> {
  441. if (dialogButton == addButtonType) {
  442. ArrayList<String> tmp = new ArrayList<String>();
  443. tmp.add(name.getText());
  444. tmp.add(value.getText());
  445. tmp.add(type.getValue());
  446. return tmp;
  447. } else {
  448. return null;
  449. }
  450. });
  451. Optional<ArrayList<String>> result = addPropDialog.showAndWait();
  452. // create new Property
  453. result.ifPresent(t -> {
  454. System.out.println("Name: " + t.get(0) + ", Value: " + t.get(1) + ", Type: " + t.get(2));
  455. Element selected = getSelected();
  456. if (t.get(2).equals("Integer") && t.get(1).matches(IS_INT)) {
  457. selected.addAttribute(t.get(0), Integer.valueOf(t.get(1)));
  458. GraphHelper.propagateAttribute(Main.getInstance().getGraphManager().getGraph(), selected, t.get(0),
  459. Integer.valueOf(t.get(1)));
  460. } else if (t.get(2).equals("Float") && t.get(1).matches(IS_FLOAT)) {
  461. selected.addAttribute(t.get(0), Float.valueOf(t.get(1)));
  462. GraphHelper.propagateAttribute(Main.getInstance().getGraphManager().getGraph(), selected, t.get(0),
  463. Float.valueOf(t.get(1)));
  464. } else if (t.get(2).equals("String")) {
  465. selected.addAttribute(t.get(0), String.valueOf(t.get(1)));
  466. GraphHelper.propagateAttribute(Main.getInstance().getGraphManager().getGraph(), selected, t.get(0),
  467. String.valueOf(t.get(1)));
  468. } else if (t.get(2).equals("Boolean") && t.get(1).matches(IS_BOOL)) {
  469. selected.addAttribute(t.get(0), Boolean.valueOf(t.get(1)));
  470. GraphHelper.propagateAttribute(Main.getInstance().getGraphManager().getGraph(), selected, t.get(0),
  471. Boolean.valueOf(t.get(1)));
  472. } else {
  473. // type doesnt fit input -> show alert and re-open property
  474. // creation window
  475. alert.showAndWait();
  476. addPropFunctionality(t.get(0));
  477. }
  478. showNewDataSet(selected);
  479. });
  480. }
  481. }