GraphManager.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. package de.tu_darmstadt.informatik.tk.scopviz.main;
  2. import java.util.Collection;
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.LinkedList;
  6. import org.graphstream.algorithm.Toolkit;
  7. import org.graphstream.graph.Edge;
  8. import org.graphstream.graph.Element;
  9. import org.graphstream.graph.Graph;
  10. import org.graphstream.graph.Node;
  11. import org.graphstream.ui.geom.Point3;
  12. import org.graphstream.ui.swingViewer.ViewPanel;
  13. import org.graphstream.ui.view.Viewer;
  14. import org.graphstream.ui.view.ViewerPipe;
  15. import de.tu_darmstadt.informatik.tk.scopviz.debug.Debug;
  16. import de.tu_darmstadt.informatik.tk.scopviz.ui.GraphDisplayManager;
  17. import de.tu_darmstadt.informatik.tk.scopviz.ui.OptionsManager;
  18. import de.tu_darmstadt.informatik.tk.scopviz.ui.PropertiesManager;
  19. import de.tu_darmstadt.informatik.tk.scopviz.ui.StylesheetManager;
  20. import de.tu_darmstadt.informatik.tk.scopviz.ui.handlers.MyMouseManager;
  21. /**
  22. * Interface between GUI and internal Graph representation. Manages internal
  23. * representation of the Graph to accommodate creation and deletion of nodes and
  24. * edges.
  25. *
  26. * @author Jascha Bohne
  27. * @version 3.0.0.0
  28. *
  29. */
  30. public class GraphManager {
  31. public static final String UI_CLASS_PROCESSING_ENABLED = "procEn";
  32. /** The Graph this instance of GraphManager manages. */
  33. protected MyGraph g;
  34. /**
  35. * The Stylesheet for this Graph, excluding parts that can be set by
  36. * NodeGraphics.
  37. */
  38. protected String stylesheet = "";
  39. /** The last Node that was deleted. */
  40. protected Node deletedNode;
  41. /** The last Edge that was deleted. */
  42. protected LinkedList<Edge> deletedEdges = new LinkedList<>();
  43. /** The currently selected Node, mutually exclusive with selectedEdgeID. */
  44. protected String selectedNodeID = null;
  45. /** The currently selected Edge, mutually exclusive with selectedNodeID. */
  46. protected String selectedEdgeID = null;
  47. /** The ViewPanel the Graph is drawn in. */
  48. protected ViewPanel view;
  49. /** The Path on Disk the Graph will be saved to. */
  50. protected String currentPath;
  51. /** The Viewer the Graph provides, grants Access to Camera Manipulation. */
  52. protected Viewer viewer;
  53. /**
  54. * The Pipe that notifies the underlying Graph of any Changes within the
  55. * graphic Representation.
  56. */
  57. protected ViewerPipe fromViewer;
  58. /**
  59. * The Id of the Node that was last clicked.
  60. */
  61. protected String lastClickedID;
  62. /**
  63. * Creates a new Manager for the given graph.
  64. *
  65. * @param graph
  66. * the graph this visualizer should handle
  67. */
  68. public GraphManager(MyGraph graph) {
  69. g = graph;
  70. /* Viewer */ viewer = new Viewer(g, Viewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
  71. view = viewer.addDefaultView(false);
  72. viewer.setCloseFramePolicy(Viewer.CloseFramePolicy.EXIT);
  73. /* ViewerPipe */fromViewer = viewer.newViewerPipe();
  74. view.setMouseManager(new MyMouseManager(this));
  75. fromViewer.addSink(graph);
  76. fromViewer.removeElementSink(graph);
  77. }
  78. /**
  79. * Deletes the Node corresponding to the given ID from the Graph. The
  80. * referenced Graph is modified directly. Will throw an
  81. * ElementNotFoundException, when the Node is not Found Will also remove all
  82. * Edges connected to the given Node
  83. *
  84. * @param id
  85. * the ID of the node that will be removed
  86. */
  87. public void deleteNode(final String id) {
  88. deletedEdges.removeAll(deletedEdges);
  89. deletedNode = null;
  90. // Edges have to be deleted first because they clear deletedNode
  91. // and need the Node to still be in the Graph
  92. deleteEdgesOfNode(id);
  93. deletedNode = g.removeNode(id);
  94. }
  95. /**
  96. * Deletes the Edge corresponding to the given ID from the Graph. The
  97. * referenced Graph is modified directly. Will throw an
  98. * ElementNotFoundException, when the Edge is not Found
  99. *
  100. * @param id
  101. * the ID of the Edge that will be removed
  102. */
  103. public void deleteEdge(final String id) {
  104. deselect();
  105. deletedEdges.removeAll(deletedEdges);
  106. deletedNode = null;
  107. deletedEdges.add(g.removeEdge(id));
  108. }
  109. /**
  110. * Deletes all Edges connected to the given Node. The referenced Graph is
  111. * modified directly. Will throw an ElementNotFoundException if the Node is
  112. * not Found
  113. *
  114. * @param id
  115. * the Id of the Node, whose Edges shall be removed
  116. */
  117. public void deleteEdgesOfNode(final String id) {
  118. deselect();
  119. Node node = g.getNode(id);
  120. deletedEdges.removeAll(deletedEdges);
  121. deletedNode = null;
  122. Edge[] temp = new Edge[0];
  123. temp = g.getEdgeSet().toArray(temp);
  124. for (Edge e : temp) {
  125. if (e.getSourceNode().equals(node) || e.getTargetNode().equals(node)) {
  126. // adds the Edge to the list of deleted Edges and remove sit
  127. // from the Graph
  128. deletedEdges.add(g.removeEdge(e));
  129. }
  130. }
  131. }
  132. /**
  133. * Undoes the last deleting operation on the given Graph. Deleting
  134. * operations are: deleteNode, deleteEdge and deleteEdgesOfNode. Only undoes
  135. * the last deleting operation even if that operation didn't change the
  136. * Graph
  137. */
  138. public void undelete() {
  139. // System.out.println("test-undel");
  140. HashMap<String, Object> attributes = new HashMap<String, Object>();
  141. if (deletedNode != null) {
  142. for (String s : deletedNode.getAttributeKeySet()) {
  143. attributes.put(s, deletedNode.getAttribute(s));
  144. }
  145. g.addNode(deletedNode.getId());
  146. g.getNode(deletedNode.getId()).addAttributes(attributes);
  147. }
  148. for (Edge e : deletedEdges) {
  149. attributes = new HashMap<String, Object>();
  150. for (String s : e.getAttributeKeySet()) {
  151. attributes.put(s, e.getAttribute(s));
  152. }
  153. g.addEdge(e.getId(), (Node) e.getSourceNode(), (Node) e.getTargetNode());
  154. g.getEdge(e.getId()).addAttributes(attributes);
  155. }
  156. }
  157. /**
  158. * returns a View of the Graph. The View lives in the Swing Thread and the
  159. * Graph in the Main thread.
  160. *
  161. *
  162. * @return a View of the Graph, inheriting from JPanel
  163. */
  164. public ViewPanel getView() {
  165. return view;
  166. }
  167. /**
  168. * Returns the ID of the currently selected Node.
  169. *
  170. * @return the node's ID
  171. */
  172. public String getSelectedNodeID() {
  173. return selectedNodeID;
  174. }
  175. /**
  176. * Returns the ID of the currently selected Edge.
  177. *
  178. * @return the edge's ID
  179. */
  180. public String getSelectedEdgeID() {
  181. return selectedEdgeID;
  182. }
  183. /**
  184. * Selects the Node with the given ID, resets Edge selection.
  185. *
  186. * @param nodeID
  187. * the ID of the Node to select
  188. */
  189. public void selectNode(String nodeID) {
  190. if (nodeID != null && g.getNode(nodeID) != null) {
  191. deselect();
  192. this.selectedNodeID = nodeID;
  193. Node n = g.getNode(nodeID);
  194. // set selected node color to red
  195. if (!hasClass(n, UI_CLASS_PROCESSING_ENABLED)
  196. || !GraphDisplayManager.getCurrentLayer().equals(Layer.MAPPING)) {
  197. String nodeType = n.getAttribute("ui.class");
  198. n.changeAttribute("ui.style", "fill-mode: image-scaled; fill-image: url('src/main/resources/png/"
  199. + nodeType + "_red.png'); size: 15px;");
  200. n.changeAttribute("ui.class", nodeType + "_red");
  201. }
  202. PropertiesManager.setItemsProperties();
  203. }
  204. }
  205. /**
  206. * Selects the Edge with the given ID, resets Node selection.
  207. *
  208. * @param edgeID
  209. * the ID of the Edge to select
  210. */
  211. public void selectEdge(String edgeID) {
  212. if (edgeID != null && g.getEdge(edgeID) != null) {
  213. deselect();
  214. this.selectedEdgeID = edgeID;
  215. addClass(edgeID, "selected");
  216. // set selected edge color to red
  217. // g.getEdge(getSelectedEdgeID()).changeAttribute("ui.style",
  218. // "fill-color: #FF0000;");
  219. PropertiesManager.setItemsProperties();
  220. }
  221. }
  222. /**
  223. * Deselect any currently selected nodes or edges.
  224. */
  225. // TODO call this before save
  226. public void deselect() {
  227. // Set last selected Edge Color to Black
  228. if (getSelectedEdgeID() != null) {
  229. removeClass(getSelectedEdgeID(), "selected");
  230. }
  231. // Set last selected Node color to black
  232. else if (getSelectedNodeID() != null) {
  233. Node n = g.getNode(getSelectedNodeID());
  234. if (!hasClass(n, UI_CLASS_PROCESSING_ENABLED)
  235. || !GraphDisplayManager.getCurrentLayer().equals(Layer.MAPPING)) {
  236. String nodeType = n.getAttribute("ui.class");
  237. n.removeAttribute("ui.style");
  238. n.changeAttribute("ui.style", "fill-color: #000000; size: 10px;");
  239. n.changeAttribute("ui.class", nodeType.split("_")[0]);
  240. }
  241. }
  242. this.selectedNodeID = null;
  243. this.selectedEdgeID = null;
  244. }
  245. /**
  246. * Returns a reference to the Graph object managed by this visualizer.
  247. *
  248. * @return the graph
  249. */
  250. public Graph getGraph() {
  251. return g;
  252. }
  253. /**
  254. * Zooms in the view of the graph by 5 percent.
  255. */
  256. public void zoomIn() {
  257. zoom(-0.05);
  258. }
  259. /**
  260. * Zooms out the view of the graph by 5 percent.
  261. */
  262. public void zoomOut() {
  263. zoom(0.05);
  264. }
  265. /**
  266. * Zooms the view by the given Amount, positive values zoom out, negative
  267. * values zoom in.
  268. *
  269. * @param amount
  270. * the amount of zoom, should usually be between -0.2 and 0.2 for
  271. * reasonable zoom.
  272. */
  273. public void zoom(double amount) {
  274. view.getCamera().setViewPercent(view.getCamera().getViewPercent() * (1 + amount));
  275. }
  276. /**
  277. * Pumps the Pipe from the graphical Representation to the underlying Graph,
  278. * propagating all Changes made.
  279. */
  280. public void pumpIt() {
  281. fromViewer.pump();
  282. }
  283. @Override
  284. public String toString() {
  285. return "Visualizer for Graph \"" + g.getId() + "\"";
  286. }
  287. /**
  288. * Returns the current Save Path on Disk for the Graph.
  289. *
  290. * @return the current Path
  291. */
  292. public String getCurrentPath() {
  293. return currentPath;
  294. }
  295. /**
  296. * Sets the Save Path.
  297. *
  298. * @param currentPath
  299. * the new Path to set
  300. */
  301. public void setCurrentPath(String currentPath) {
  302. this.currentPath = currentPath;
  303. }
  304. /**
  305. * Adds a <b>Copy</b> of the given Edge to the graph. The Copy retains the
  306. * ID and all attributes.
  307. *
  308. * @param e
  309. * the Edge to be added to the graph
  310. */
  311. public void addEdge(Edge e) {
  312. HashMap<String, Object> attributes = new HashMap<>();
  313. for (String s : e.getAttributeKeySet()) {
  314. attributes.put(s, e.getAttribute(s));
  315. }
  316. g.addEdge(e.getId(), (Node) e.getSourceNode(), (Node) e.getTargetNode());
  317. g.getEdge(e.getId()).addAttributes(attributes);
  318. }
  319. /**
  320. * Adds a <b>Copy</b> of the given Node to the graph. The Copy retains the
  321. * ID and all attributes.
  322. *
  323. * @param n
  324. * the Node to be added to the graph
  325. */
  326. public void addNode(Node n) {
  327. HashMap<String, Object> attributes = new HashMap<>();
  328. for (String s : n.getAttributeKeySet()) {
  329. attributes.put(s, deletedNode.getAttribute(s));
  330. }
  331. g.addNode(n.getId());
  332. g.getNode(n.getId()).addAttributes(attributes);
  333. }
  334. /**
  335. * Returns the smallest X Coordinate of any Node in the Graph.
  336. *
  337. * @return the smallest X Coordinate in the Graph
  338. */
  339. public double getMinX() {
  340. double currentMin = Double.MAX_VALUE;
  341. Node n = null;
  342. Iterator<Node> allNodes = g.getNodeIterator();
  343. while (allNodes.hasNext()) {
  344. n = allNodes.next();
  345. if (n.hasAttribute("x") && currentMin > (Double) n.getAttribute("x")) {
  346. currentMin = (Double) n.getAttribute("x");
  347. }
  348. }
  349. return currentMin;
  350. }
  351. /**
  352. * Returns the biggest X Coordinate of any Node in the Graph.
  353. *
  354. * @return the biggest X Coordinate in the Graph
  355. */
  356. public double getMaxX() {
  357. double currentMax = Double.MIN_VALUE;
  358. Node n = null;
  359. Iterator<Node> allNodes = g.getNodeIterator();
  360. while (allNodes.hasNext()) {
  361. n = allNodes.next();
  362. if (n.hasAttribute("x") && currentMax < (Double) n.getAttribute("x")) {
  363. currentMax = (Double) n.getAttribute("x");
  364. }
  365. }
  366. return currentMax;
  367. }
  368. /**
  369. * Returns the smallest Y Coordinate of any Node in the Graph.
  370. *
  371. * @return the smallest Y Coordinate in the Graph
  372. */
  373. public double getMinY() {
  374. double currentMin = Double.MAX_VALUE;
  375. Node n = null;
  376. Iterator<Node> allNodes = g.getNodeIterator();
  377. while (allNodes.hasNext()) {
  378. n = allNodes.next();
  379. if (n.hasAttribute("y") && currentMin > (Double) n.getAttribute("y")) {
  380. currentMin = (Double) n.getAttribute("y");
  381. }
  382. }
  383. return currentMin;
  384. }
  385. /**
  386. * Returns the biggest Y Coordinate of any Node in the Graph.
  387. *
  388. * @return the biggest Y Coordinate in the Graph
  389. */
  390. public double getMaxY() {
  391. double currentMax = Double.MIN_VALUE;
  392. Node n = null;
  393. Iterator<Node> allNodes = g.getNodeIterator();
  394. while (allNodes.hasNext()) {
  395. n = allNodes.next();
  396. if (n.hasAttribute("y") && currentMax < (Double) n.getAttribute("y")) {
  397. currentMax = (Double) n.getAttribute("y");
  398. }
  399. }
  400. return currentMax;
  401. }
  402. /**
  403. * Converts the Coordinates of all Nodes into a saveable and uniform Format.
  404. */
  405. public void correctCoordinates() {
  406. Point3 coords;
  407. Node n = null;
  408. Iterator<Node> allNodes = g.getNodeIterator();
  409. while (allNodes.hasNext()) {
  410. n = allNodes.next();
  411. if (n.hasAttribute("xyz")) {
  412. coords = Toolkit.nodePointPosition(n);
  413. n.setAttribute("x", coords.x);
  414. n.setAttribute("y", coords.y);
  415. n.removeAttribute("xyz");
  416. }
  417. }
  418. }
  419. /**
  420. * Converts the weight property into a label to display on the Graph.
  421. * Removes all labels if that option is set
  422. */
  423. public void handleEdgeWeight() {
  424. Edge e = null;
  425. Iterator<Edge> allEdges = g.getEdgeIterator();
  426. while (allEdges.hasNext()) {
  427. e = allEdges.next();
  428. if (!e.hasAttribute("weight")) {
  429. e.addAttribute("weight", OptionsManager.getDefaultWeight());
  430. }
  431. if (OptionsManager.isWeightShown()) {
  432. e.setAttribute("ui.label", e.getAttribute("weight").toString());
  433. } else {
  434. e.removeAttribute("ui.label");
  435. }
  436. }
  437. }
  438. /**
  439. * Returns the Stylesheet used by the Graph.
  440. *
  441. * @return the Stylesheet in use
  442. */
  443. public String getStylesheet() {
  444. return stylesheet;
  445. }
  446. /**
  447. * Sets the Stylesheet to be used by the Graph.
  448. *
  449. * @param stylesheet
  450. * the new stylesheet to use
  451. */
  452. public void setStylesheet(String stylesheet) {
  453. this.stylesheet = stylesheet;
  454. g.removeAttribute("ui.stylesheet");
  455. String completeStylesheet = stylesheet;
  456. completeStylesheet = completeStylesheet.concat(StylesheetManager.getNodeGraphics());
  457. completeStylesheet = completeStylesheet
  458. .concat(StylesheetManager.getLayerStyle((Layer) g.getAttribute("layer")));
  459. g.addAttribute("ui.stylesheet", completeStylesheet);
  460. }
  461. /**
  462. * adds the given listener to the underlying graph the listener will be
  463. * notified, when an Edge is added.
  464. *
  465. * @param e
  466. * the EdgeCreatedListener
  467. */
  468. public void addEdgeCreatedListener(EdgeCreatedListener e) {
  469. ((MyGraph) g).addEdgeCreatedListener(e);
  470. }
  471. /**
  472. * adds the given listener to the underlying graph the listener will be
  473. * notified, when a Node is added.
  474. *
  475. * @param n
  476. * the NodeCreatedListener
  477. */
  478. public void addNodeCreatedListener(NodeCreatedListener n) {
  479. ((MyGraph) g).addNodeCreatedListener(n);
  480. }
  481. /**
  482. * Updates the Stylesheet, causing any changes to it to come into effect.
  483. */
  484. public void updateStylesheet() {
  485. setStylesheet(this.stylesheet);
  486. }
  487. /**
  488. * Sets typeofNode as the ui.class of all Nodes.
  489. *
  490. */
  491. public void convertUiClass() {
  492. Collection<Node> allNodes = g.getNodeSet();
  493. for (Node n : allNodes) {
  494. if (n.hasAttribute("typeofNode")) {
  495. n.addAttribute("ui.class", n.getAttribute("typeofNode").toString());
  496. }
  497. }
  498. }
  499. /**
  500. * Create Edges based on CreateMode
  501. *
  502. * @param id
  503. */
  504. public void createEdges(String id) {
  505. switch (Main.getInstance().getCreationMode()) {
  506. case CREATE_DIRECTED_EDGE:
  507. case CREATE_UNDIRECTED_EDGE:
  508. if (lastClickedID == null) {
  509. lastClickedID = id;
  510. if (!selectNodeForEdgeCreation(lastClickedID))
  511. lastClickedID = null;
  512. } else if (id.equals(lastClickedID) || createEdge(id, lastClickedID)) {
  513. deselectNodesAfterEdgeCreation(lastClickedID);
  514. lastClickedID = null;
  515. }
  516. break;
  517. default:
  518. break;
  519. }
  520. PropertiesManager.setItemsProperties();
  521. // controller.createModusText.setText(Main.getInstance().getCreationMode().toString());
  522. }
  523. /**
  524. * creates a edge between to nodes
  525. *
  526. * @author MW
  527. * @param to
  528. * ID of the destination node
  529. * @param from
  530. * ID of the origin node
  531. * @return true if the edge was created. false otherwise
  532. */
  533. public boolean createEdge(String to, String from) {
  534. if (getGraph().getNode(from).hasEdgeBetween(to))
  535. return false;
  536. String newID = Main.getInstance().getUnusedID();
  537. if (Main.getInstance().getCreationMode() == CreationMode.CREATE_DIRECTED_EDGE) {
  538. getGraph().addEdge(newID, from, to, true);
  539. Debug.out("Created an directed edge with Id " + newID + " between " + from + " and " + to);
  540. } else {
  541. getGraph().addEdge(newID, from, to);
  542. Debug.out("Created an undirected edge with Id " + newID + " between " + from + " and " + to);
  543. }
  544. selectEdge(newID);
  545. return true;
  546. }
  547. /**
  548. * Selects a Node as the starting point for creating a new Edge.
  549. *
  550. * @param nodeID
  551. * the ID of the Node to select
  552. */
  553. public boolean selectNodeForEdgeCreation(String nodeID) {
  554. deselect();
  555. Node n = getGraph().getNode(nodeID);
  556. String nodeType = n.getAttribute("ui.class");
  557. if (!hasClass(n, UI_CLASS_PROCESSING_ENABLED) || !GraphDisplayManager.getCurrentLayer().equals(Layer.MAPPING)) {
  558. nodeType = nodeType.split("_")[0];
  559. n.changeAttribute("ui.style", "fill-mode: image-scaled; fill-image: url('src/main/resources/png/" + nodeType
  560. + "_green.png'); size: 15px;");
  561. n.changeAttribute("ui.class", nodeType + "_green");
  562. }
  563. return true;
  564. }
  565. /**
  566. * Reset the Selection of the Node after Edge has been successfully created.
  567. *
  568. * @param nodeID
  569. * the Id of the node to deselect.
  570. */
  571. public void deselectNodesAfterEdgeCreation(String nodeID) {
  572. Node n = getGraph().getNode(nodeID);
  573. if (!hasClass(n, UI_CLASS_PROCESSING_ENABLED) || !GraphDisplayManager.getCurrentLayer().equals(Layer.MAPPING)) {
  574. String nodeType = n.getAttribute("ui.class");
  575. n.changeAttribute("ui.style", "fill-color: #000000; size: 10px;");
  576. n.changeAttribute("ui.class", nodeType.split("_")[0]);
  577. }
  578. }
  579. public boolean addClass(String id, String className) {
  580. Element e = getGraph().getEdge(id);
  581. if (e == null)
  582. e = getGraph().getNode(id);
  583. if (e == null)
  584. return false;
  585. String eClass = e.getAttribute("ui.class");
  586. if (eClass == null || eClass.equals(""))
  587. eClass = className;
  588. else if (!(eClass.equals(className) || eClass.startsWith(className.concat(", "))
  589. || eClass.contains(", ".concat(className))))
  590. eClass = className.concat(", ").concat(eClass);
  591. e.addAttribute("ui.class", eClass);
  592. Debug.out("added " + className + ": " + eClass);
  593. return true;
  594. }
  595. public boolean removeClass(String id, String className) {
  596. Element e = getGraph().getEdge(id);
  597. if (e == null)
  598. e = getGraph().getNode(id);
  599. if (e == null)
  600. return false;
  601. String eClass = e.getAttribute("ui.class");
  602. if (eClass == null || eClass.equals(className))
  603. eClass = "";
  604. else
  605. eClass = eClass.replace(className.concat(", "), "").replace(", ".concat(className), "");
  606. e.addAttribute("ui.class", eClass);
  607. Debug.out("removed " + className + ": " + eClass);
  608. return true;
  609. }
  610. public boolean toggleClass(String id, String className) {
  611. Element e = getGraph().getEdge(id);
  612. if (e == null)
  613. e = getGraph().getNode(id);
  614. if (e == null)
  615. return false;
  616. String eClass = e.getAttribute("ui.class");
  617. if (eClass == null || !(eClass.equals(className) || eClass.startsWith(className.concat(", "))
  618. || eClass.contains(", ".concat(className))))
  619. return addClass(id, className);
  620. return removeClass(id, className);
  621. }
  622. public boolean hasClass(String id, String className) {
  623. Element e = getGraph().getEdge(id);
  624. if (e == null)
  625. e = getGraph().getNode(id);
  626. if (e == null)
  627. return false;
  628. String eClass = e.getAttribute("ui.class");
  629. return (eClass != null && (eClass.equals(className) || eClass.startsWith(className.concat(", "))
  630. || eClass.contains(", ".concat(className))));
  631. }
  632. public boolean hasClass(Edge e, String className) {
  633. if (e == null)
  634. return false;
  635. String eClass = e.getAttribute("ui.class");
  636. return (eClass != null && (eClass.equals(className) || eClass.startsWith(className.concat(", "))
  637. || eClass.contains(", ".concat(className))));
  638. }
  639. public boolean hasClass(Node n, String className) {
  640. if (n == null)
  641. return false;
  642. String nClass = n.getAttribute("ui.class");
  643. /*
  644. * TODO: nochmal angucken, wenn CSS Manager steht, ist gerade gehackt
  645. * damit, Vorführung läuft. return (nClass != null &&
  646. * (nClass.equals(className) ||
  647. * nClass.startsWith(className.concat(", ")) ||
  648. * nClass.contains(", ".concat(className))));
  649. */
  650. return (nClass != null && nClass.contains(className));
  651. }
  652. }