PropertiesManager.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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.Element;
  8. import de.tu_darmstadt.informatik.tk.scopviz.debug.Debug;
  9. import de.tu_darmstadt.informatik.tk.scopviz.graphs.GraphHelper;
  10. import de.tu_darmstadt.informatik.tk.scopviz.graphs.GraphManager;
  11. import de.tu_darmstadt.informatik.tk.scopviz.graphs.MyEdge;
  12. import de.tu_darmstadt.informatik.tk.scopviz.graphs.MyNode;
  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. MyNode selectedNode = Main.getInstance().getGraphManager().getGraph().getNode(nid);
  222. showNewDataSet(selectedNode);
  223. } else if (eid != null) {
  224. MyEdge 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 MyNode) {
  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 MyEdge
  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. Debug.out(key);
  271. if (selected instanceof MyNode
  272. && Layer.UNDERLAY == Main.getInstance().getGraphManager().getGraph().getAttribute("layer")) {
  273. break;
  274. }
  275. actualAttribute = selected.getAttribute(key);
  276. if (actualAttribute != null) {
  277. newData.add(new KeyValuePair(key, String.valueOf(actualAttribute), actualAttribute.getClass()));
  278. }
  279. break;
  280. case "process-max":
  281. if (selected instanceof MyNode
  282. && Layer.OPERATOR == Main.getInstance().getGraphManager().getGraph().getAttribute("layer")) {
  283. break;
  284. }
  285. case "typeOfDevice":
  286. if (selected instanceof MyNode
  287. && Layer.OPERATOR == Main.getInstance().getGraphManager().getGraph().getAttribute("layer")) {
  288. break;
  289. }
  290. default:
  291. actualAttribute = selected.getAttribute(key);
  292. if (actualAttribute != null) {
  293. newData.add(new KeyValuePair(key, String.valueOf(actualAttribute), actualAttribute.getClass()));
  294. }
  295. break;
  296. }
  297. }
  298. properties.setItems(groupProperties(newData));
  299. }
  300. /**
  301. * Get the selected node or edge from the GraphManager.
  302. *
  303. * @return selected node or egde
  304. */
  305. private static Element getSelected() {
  306. GraphManager viz = Main.getInstance().getGraphManager();
  307. String nid = viz.getSelectedNodeID();
  308. String eid = viz.getSelectedEdgeID();
  309. if (nid != null) {
  310. return viz.getGraph().getNode(nid);
  311. } else if (eid != null) {
  312. return viz.getGraph().getEdge(eid);
  313. } else {
  314. return null;
  315. }
  316. }
  317. /**
  318. * Delete a given Pair from the current Node or Edge.
  319. *
  320. * @param pair
  321. * selectedProperty
  322. */
  323. private static void removeProperty(KeyValuePair pair) {
  324. Element selected = getSelected();
  325. selected.removeAttribute(pair.getKey());
  326. GraphHelper.propagateAttribute(Main.getInstance().getGraphManager().getGraph(), selected, pair.getKey(), null);
  327. }
  328. /**
  329. * groups and filters a list of items according to the order and visibility
  330. * rules
  331. *
  332. * @param data
  333. * a list of property items
  334. * @return the data with the rules applied
  335. */
  336. private static ObservableList<KeyValuePair> groupProperties(ObservableList<KeyValuePair> data) {
  337. ObservableList<KeyValuePair> newData = FXCollections.observableArrayList();
  338. ;
  339. // adds all items in the order of the rules. Ordered items as an extra
  340. // list, removed from data
  341. for (String s : itemOrderRules) {
  342. for (int i = 0; i < data.size(); i++) {
  343. KeyValuePair kvp = data.get(i);
  344. if (kvp.getKey().equals(s)) {
  345. newData.add(kvp);
  346. data.remove(kvp);
  347. }
  348. }
  349. }
  350. // filters items according to the rules. Filters on the data without the
  351. // ordered items
  352. for (String key : itemVisibilityRules.keySet()) {
  353. for (int i = 0; i < data.size(); i++) {
  354. KeyValuePair kvp = data.get(i);
  355. if (kvp.getKey().equals(key)) {
  356. if (itemVisibilityRules.get(kvp.getKey()) == -1) {
  357. data.remove(kvp);
  358. }
  359. else if (itemVisibilityRules.get(kvp.getKey()) == -2) {
  360. if (!Debug.DEBUG_ENABLED) {
  361. data.remove(kvp);
  362. }
  363. }
  364. break;
  365. }
  366. }
  367. }
  368. // adds the filtered data without the ordered items behind the ordered
  369. // items
  370. newData.addAll(data);
  371. return newData;
  372. }
  373. /**
  374. * TODO Auslagern contextMenu add button functionality.
  375. */
  376. private static void addPropFunctionality(String preConfigPropName) {
  377. Debug.out("Add Element");
  378. // Create new Dialog
  379. Dialog<ArrayList<String>> addPropDialog = new Dialog<>();
  380. addPropDialog.setTitle("Add Property");
  381. addPropDialog.setHeaderText("Choose your Property Details");
  382. // Alert window -> when problems with input
  383. Alert alert = new Alert(AlertType.WARNING);
  384. alert.setTitle("Property-Type Alert");
  385. alert.setHeaderText("The selected Type doesnt fit the Input");
  386. alert.setContentText(null);
  387. ButtonType addButtonType = new ButtonType("Confirm", ButtonData.OK_DONE);
  388. addPropDialog.getDialogPane().getButtonTypes().addAll(addButtonType, ButtonType.CANCEL);
  389. // create grid
  390. GridPane grid = new GridPane();
  391. grid.setHgap(10);
  392. grid.setVgap(10);
  393. grid.setPadding(new Insets(20, 150, 10, 10));
  394. // create dialog elements
  395. TextField name = new TextField();
  396. name.setPromptText("Name");
  397. TextField value = new TextField();
  398. value.setPromptText("Value");
  399. ChoiceBox<String> type = new ChoiceBox<String>();
  400. type.setItems(FXCollections.observableArrayList("Integer", "Float", "String", "Boolean"));
  401. type.getSelectionModel().selectFirst();
  402. // position elements on grid
  403. grid.add(new Label("Property Name:"), 0, 0);
  404. grid.add(name, 1, 0);
  405. grid.add(new Label("Property Value:"), 0, 1);
  406. grid.add(value, 1, 1);
  407. grid.add(new Label("Property Type:"), 0, 2);
  408. grid.add(type, 1, 2);
  409. javafx.scene.Node confirmButton = addPropDialog.getDialogPane().lookupButton(addButtonType);
  410. confirmButton.setDisable(true);
  411. nameSet = false;
  412. valueSet = false;
  413. // show pre defined property name
  414. if (preConfigPropName != null) {
  415. name.setText(preConfigPropName);
  416. PropertiesManager.nameSet = true;
  417. }
  418. // hide confirm button, when textfields empty
  419. name.textProperty().addListener((observable, oldValue, newValue) -> {
  420. PropertiesManager.nameSet = true;
  421. if (newValue.trim().isEmpty()) {
  422. PropertiesManager.nameSet = false;
  423. confirmButton.setDisable(true);
  424. } else if (PropertiesManager.valueSet) {
  425. confirmButton.setDisable(false);
  426. }
  427. });
  428. value.textProperty().addListener((observable, oldValue, newValue) -> {
  429. PropertiesManager.valueSet = true;
  430. if (newValue.trim().isEmpty()) {
  431. PropertiesManager.valueSet = false;
  432. confirmButton.setDisable(true);
  433. } else if (PropertiesManager.nameSet) {
  434. confirmButton.setDisable(false);
  435. }
  436. });
  437. // set dialog
  438. addPropDialog.getDialogPane().setContent(grid);
  439. Platform.runLater(() -> name.requestFocus());
  440. // get new property values
  441. addPropDialog.setResultConverter(dialogButton -> {
  442. if (dialogButton == addButtonType) {
  443. ArrayList<String> tmp = new ArrayList<String>();
  444. tmp.add(name.getText());
  445. tmp.add(value.getText());
  446. tmp.add(type.getValue());
  447. return tmp;
  448. } else {
  449. return null;
  450. }
  451. });
  452. Optional<ArrayList<String>> result = addPropDialog.showAndWait();
  453. // create new Property
  454. result.ifPresent(t -> {
  455. System.out.println("Name: " + t.get(0) + ", Value: " + t.get(1) + ", Type: " + t.get(2));
  456. Element selected = getSelected();
  457. if (t.get(2).equals("Integer") && t.get(1).matches(IS_INT)) {
  458. selected.addAttribute(t.get(0), Integer.valueOf(t.get(1)));
  459. GraphHelper.propagateAttribute(Main.getInstance().getGraphManager().getGraph(), selected, t.get(0),
  460. Integer.valueOf(t.get(1)));
  461. } else if (t.get(2).equals("Float") && t.get(1).matches(IS_FLOAT)) {
  462. selected.addAttribute(t.get(0), Float.valueOf(t.get(1)));
  463. GraphHelper.propagateAttribute(Main.getInstance().getGraphManager().getGraph(), selected, t.get(0),
  464. Float.valueOf(t.get(1)));
  465. } else if (t.get(2).equals("String")) {
  466. selected.addAttribute(t.get(0), String.valueOf(t.get(1)));
  467. GraphHelper.propagateAttribute(Main.getInstance().getGraphManager().getGraph(), selected, t.get(0),
  468. String.valueOf(t.get(1)));
  469. } else if (t.get(2).equals("Boolean") && t.get(1).matches(IS_BOOL)) {
  470. selected.addAttribute(t.get(0), Boolean.valueOf(t.get(1)));
  471. GraphHelper.propagateAttribute(Main.getInstance().getGraphManager().getGraph(), selected, t.get(0),
  472. Boolean.valueOf(t.get(1)));
  473. } else {
  474. // type doesnt fit input -> show alert and re-open property
  475. // creation window
  476. alert.showAndWait();
  477. addPropFunctionality(t.get(0));
  478. }
  479. showNewDataSet(selected);
  480. });
  481. }
  482. }