PropertiesManager.java 18 KB

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