GraphManager.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. package de.tu_darmstadt.informatik.tk.scopviz.graphs;
  2. import java.util.Collection;
  3. import java.util.HashMap;
  4. import java.util.LinkedList;
  5. import org.graphstream.graph.Edge;
  6. import org.graphstream.graph.Element;
  7. import org.graphstream.graph.Node;
  8. import org.graphstream.ui.swingViewer.ViewPanel;
  9. import org.graphstream.ui.view.Viewer;
  10. import org.graphstream.ui.view.ViewerPipe;
  11. import de.tu_darmstadt.informatik.tk.scopviz.debug.Debug;
  12. import de.tu_darmstadt.informatik.tk.scopviz.main.CreationMode;
  13. import de.tu_darmstadt.informatik.tk.scopviz.main.Layer;
  14. import de.tu_darmstadt.informatik.tk.scopviz.main.Main;
  15. import de.tu_darmstadt.informatik.tk.scopviz.ui.GraphDisplayManager;
  16. import de.tu_darmstadt.informatik.tk.scopviz.ui.PropertiesManager;
  17. import de.tu_darmstadt.informatik.tk.scopviz.ui.StylesheetManager;
  18. import de.tu_darmstadt.informatik.tk.scopviz.ui.handlers.MyMouseManager;
  19. /**
  20. * Interface between GUI and internal Graph representation. Manages internal
  21. * representation of the Graph to accommodate creation and deletion of nodes and
  22. * edges.
  23. *
  24. * @author Jascha Bohne
  25. * @version 3.0.0.0
  26. *
  27. */
  28. public class GraphManager {
  29. /** String for the processing enabled type of node. */
  30. public static final String UI_CLASS_PROCESSING_ENABLED = "procEn";
  31. /** The Graph this instance of GraphManager manages. */
  32. protected MyGraph g;
  33. protected MyGraph activeSubGraph;
  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. protected 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. GraphHelper.propagateElementDeletion(g, deletedEdges);
  132. }
  133. /**
  134. * Undoes the last deleting operation on the given Graph. Deleting
  135. * operations are: deleteNode, deleteEdge and deleteEdgesOfNode. Only undoes
  136. * the last deleting operation even if that operation didn't change the
  137. * Graph
  138. */
  139. public void undelete() {
  140. String newId = "";
  141. HashMap<String, Object> attributes = new HashMap<String, Object>();
  142. if (deletedNode != null) {
  143. for (String s : deletedNode.getAttributeKeySet()) {
  144. attributes.put(s, deletedNode.getAttribute(s));
  145. }
  146. newId = Main.getInstance().getUnusedID();
  147. g.addNode(newId);
  148. g.getNode(newId).addAttributes(attributes);
  149. String origElement = GraphHelper.propagateElementUndeletion(g, deletedNode, null);
  150. if (origElement != null) {
  151. g.getNode(newId).addAttribute("originalElement", origElement);
  152. }
  153. }
  154. for (Edge e : deletedEdges) {
  155. String sourceId = null;
  156. String targetId = null;
  157. attributes = new HashMap<String, Object>();
  158. for (String s : e.getAttributeKeySet()) {
  159. attributes.put(s, e.getAttribute(s));
  160. }
  161. String id = Main.getInstance().getUnusedID();
  162. if (deletedNode != null) {
  163. sourceId = (e.getSourceNode().getId().equals(deletedNode.getId())) ? newId : e.getSourceNode().getId();
  164. targetId = (e.getTargetNode().getId().equals(deletedNode.getId())) ? newId : e.getTargetNode().getId();
  165. } else {
  166. sourceId = e.getSourceNode().getId();
  167. targetId = e.getTargetNode().getId();
  168. }
  169. g.addEdge(id, sourceId, targetId, e.isDirected());
  170. g.getEdge(id).addAttributes(attributes);
  171. String origElement = GraphHelper.propagateElementUndeletion(g, e,
  172. g.getNode(newId).getAttribute("originalElement"));
  173. if (origElement != null) {
  174. g.getEdge(id).addAttribute("originalElement", origElement);
  175. }
  176. }
  177. deletedEdges = new LinkedList<>();
  178. deletedNode = null;
  179. }
  180. /**
  181. * returns a View of the Graph. The View lives in the Swing Thread and the
  182. * Graph in the Main thread.
  183. *
  184. *
  185. * @return a View of the Graph, inheriting from JPanel
  186. */
  187. public ViewPanel getView() {
  188. return view;
  189. }
  190. /**
  191. * Returns the ID of the currently selected Node.
  192. *
  193. * @return the node's ID
  194. */
  195. public String getSelectedNodeID() {
  196. return selectedNodeID;
  197. }
  198. /**
  199. * Returns the ID of the currently selected Edge.
  200. *
  201. * @return the edge's ID
  202. */
  203. public String getSelectedEdgeID() {
  204. return selectedEdgeID;
  205. }
  206. /**
  207. * Selects the Node with the given ID, resets Edge selection.
  208. *
  209. * @param nodeID
  210. * the ID of the Node to select
  211. */
  212. public void selectNode(String nodeID) {
  213. if (nodeID != null && g.getNode(nodeID) != null) {
  214. deselect();
  215. this.selectedNodeID = nodeID;
  216. Node n = g.getNode(nodeID);
  217. // set selected node color to red
  218. if (!hasClass(n, UI_CLASS_PROCESSING_ENABLED)
  219. || !GraphDisplayManager.getCurrentLayer().equals(Layer.MAPPING)) {
  220. n.changeAttribute("ui.style", "fill-color : #F00; size: 15px;");
  221. PropertiesManager.setItemsProperties();
  222. }
  223. }
  224. }
  225. /**
  226. * Selects the Edge with the given ID, resets Node selection.
  227. *
  228. * @param edgeID
  229. * the ID of the Edge to select
  230. */
  231. public void selectEdge(String edgeID) {
  232. if (edgeID != null && g.getEdge(edgeID) != null) {
  233. deselect();
  234. this.selectedEdgeID = edgeID;
  235. addClass(edgeID, "selected");
  236. PropertiesManager.setItemsProperties();
  237. }
  238. }
  239. /**
  240. * Deselect any currently selected nodes or edges.
  241. */
  242. // TODO call this before save
  243. protected void deselect() {
  244. // Set last selected Edge Color to Black
  245. if (getSelectedEdgeID() != null && g.getEdge(getSelectedEdgeID()) != null) {
  246. removeClass(getSelectedEdgeID(), "selected");
  247. }
  248. // Set last selected Node color to black
  249. else if (getSelectedNodeID() != null && g.getNode(getSelectedNodeID()) != null) {
  250. Node n = g.getNode(getSelectedNodeID());
  251. if (!hasClass(n, UI_CLASS_PROCESSING_ENABLED)
  252. || !GraphDisplayManager.getCurrentLayer().equals(Layer.MAPPING)) {
  253. String nodeType = n.getAttribute("ui.class");
  254. n.removeAttribute("ui.style");
  255. n.changeAttribute("ui.style", "fill-color: #000000; size: 15px;");
  256. n.changeAttribute("ui.class", nodeType.split("_")[0]);
  257. }
  258. }
  259. this.selectedNodeID = null;
  260. this.selectedEdgeID = null;
  261. }
  262. /**
  263. * Returns a reference to the Graph object managed by this visualizer.
  264. *
  265. * @return the graph
  266. */
  267. public MyGraph getGraph() {
  268. return g;
  269. }
  270. /**
  271. * Zooms in the view of the graph by 5 percent.
  272. */
  273. public void zoomIn() {
  274. zoom(-0.05);
  275. }
  276. /**
  277. * Zooms out the view of the graph by 5 percent.
  278. */
  279. public void zoomOut() {
  280. zoom(0.05);
  281. }
  282. /**
  283. * Zooms the view by the given Amount, positive values zoom out, negative
  284. * values zoom in.
  285. *
  286. * @param amount
  287. * the amount of zoom, should usually be between -0.2 and 0.2 for
  288. * reasonable zoom.
  289. */
  290. public void zoom(double amount) {
  291. view.getCamera().setViewPercent(view.getCamera().getViewPercent() * (1 + amount));
  292. }
  293. /**
  294. * Pumps the Pipe from the graphical Representation to the underlying Graph,
  295. * propagating all Changes made.
  296. */
  297. public void pumpIt() {
  298. fromViewer.pump();
  299. }
  300. @Override
  301. public String toString() {
  302. return "Visualizer for Graph \"" + g.getId() + "\"";
  303. }
  304. /**
  305. * Returns the current Save Path on Disk for the Graph.
  306. *
  307. * @return the current Path
  308. */
  309. public String getCurrentPath() {
  310. return currentPath;
  311. }
  312. /**
  313. * Sets the Save Path.
  314. *
  315. * @param currentPath
  316. * the new Path to set
  317. */
  318. public void setCurrentPath(String currentPath) {
  319. this.currentPath = currentPath;
  320. }
  321. /**
  322. * Adds a <b>Copy</b> of the given Edge to the graph. The Copy retains the
  323. * ID and all attributes.
  324. *
  325. * @param e
  326. * the Edge to be added to the graph
  327. */
  328. public void addEdge(Edge e) {
  329. HashMap<String, Object> attributes = new HashMap<>();
  330. for (String s : e.getAttributeKeySet()) {
  331. attributes.put(s, e.getAttribute(s));
  332. }
  333. if (activeSubGraph != null && !activeSubGraph.equals(g)) {
  334. if (e.getSourceNode().getAttribute("originalGraph").equals(activeSubGraph.getId())
  335. && e.getTargetNode().getAttribute("originalGraph").equals(activeSubGraph.getId())) {
  336. g.addEdge(e.getId(), (Node) e.getSourceNode(), (Node) e.getTargetNode(), e.isDirected());
  337. g.getEdge(e.getId()).addAttributes(attributes);
  338. activeSubGraph.addEdge(e.getId(), (Node) e.getSourceNode(), (Node) e.getTargetNode(), e.isDirected());
  339. activeSubGraph.getEdge(e.getId()).addAttributes(attributes);
  340. g.getEdge(e.getId()).addAttribute("originalElement", activeSubGraph.getId() + "+#" + e.getId());
  341. } else {
  342. Debug.out("Can not create Edge between Nodes of different Subgraphs!", 2);
  343. }
  344. } else {
  345. g.addEdge(e.getId(), (Node) e.getSourceNode(), (Node) e.getTargetNode(), e.isDirected());
  346. g.getEdge(e.getId()).addAttributes(attributes);
  347. }
  348. }
  349. /**
  350. * Adds a <b>Copy</b> of the given Node to the graph. The Copy retains the
  351. * ID and all attributes.
  352. *
  353. * @param n
  354. * the Node to be added to the graph
  355. */
  356. public void addNode(Node n) {
  357. HashMap<String, Object> attributes = new HashMap<>();
  358. for (String s : n.getAttributeKeySet()) {
  359. attributes.put(s, n.getAttribute(s));
  360. }
  361. g.addNode(n.getId());
  362. g.getNode(n.getId()).addAttributes(attributes);
  363. if (activeSubGraph != null && !activeSubGraph.equals(g)) {
  364. activeSubGraph.addNode(n.getId());
  365. activeSubGraph.getNode(n.getId()).addAttributes(attributes);
  366. g.getNode(n.getId()).addAttribute("originalElement", activeSubGraph.getId() + "+#" + n.getId());
  367. }
  368. }
  369. /**
  370. * Returns the smallest X Coordinate of any Node in the Graph.
  371. *
  372. * @return the smallest X Coordinate in the Graph
  373. */
  374. public double getMinX() {
  375. return g.getMinX();
  376. }
  377. /**
  378. * Returns the biggest X Coordinate of any Node in the Graph.
  379. *
  380. * @return the biggest X Coordinate in the Graph
  381. */
  382. public double getMaxX() {
  383. return g.getMaxX();
  384. }
  385. /**
  386. * Returns the smallest Y Coordinate of any Node in the Graph.
  387. *
  388. * @return the smallest Y Coordinate in the Graph
  389. */
  390. public double getMinY() {
  391. return g.getMinY();
  392. }
  393. /**
  394. * Returns the biggest Y Coordinate of any Node in the Graph.
  395. *
  396. * @return the biggest Y Coordinate in the Graph
  397. */
  398. public double getMaxY() {
  399. return g.getMaxY();
  400. }
  401. /**
  402. * Returns the Stylesheet used by the Graph.
  403. *
  404. * @return the Stylesheet in use
  405. */
  406. public String getStylesheet() {
  407. return stylesheet;
  408. }
  409. /**
  410. * Sets the Stylesheet to be used by the Graph.
  411. *
  412. * @param stylesheet
  413. * the new stylesheet to use
  414. */
  415. public void setStylesheet(String stylesheet) {
  416. this.stylesheet = stylesheet;
  417. g.removeAttribute("ui.stylesheet");
  418. String completeStylesheet = stylesheet;
  419. completeStylesheet = completeStylesheet.concat(StylesheetManager.getNodeStylesheet());
  420. completeStylesheet = completeStylesheet
  421. .concat(StylesheetManager.getLayerStyle((Layer) g.getAttribute("layer")));
  422. g.addAttribute("ui.stylesheet", completeStylesheet);
  423. }
  424. /**
  425. * adds the given listener to the underlying graph the listener will be
  426. * notified, when an Edge is added.
  427. *
  428. * @param e
  429. * the EdgeCreatedListener
  430. */
  431. public void addEdgeCreatedListener(EdgeCreatedListener e) {
  432. ((MyGraph) g).addEdgeCreatedListener(e);
  433. }
  434. /**
  435. * adds the given listener to the underlying graph the listener will be
  436. * notified, when a Node is added.
  437. *
  438. * @param n
  439. * the NodeCreatedListener
  440. */
  441. public void addNodeCreatedListener(NodeCreatedListener n) {
  442. ((MyGraph) g).addNodeCreatedListener(n);
  443. }
  444. /**
  445. * Updates the Stylesheet, causing any changes to it to come into effect.
  446. */
  447. public void updateStylesheet() {
  448. setStylesheet(this.stylesheet);
  449. }
  450. /**
  451. * Sets typeofNode as the ui.class of all Nodes.
  452. *
  453. */
  454. public void convertUiClass() {
  455. Collection<Node> allNodes = g.getNodeSet();
  456. for (Node n : allNodes) {
  457. if (n.hasAttribute("typeofNode")) {
  458. n.addAttribute("ui.class", n.getAttribute("typeofNode").toString());
  459. }
  460. }
  461. }
  462. /**
  463. * Create Edges based on CreateMode.
  464. *
  465. * @param id
  466. * The ID for the newly created Edge
  467. */
  468. public void createEdges(String id) {
  469. switch (Main.getInstance().getCreationMode()) {
  470. case CREATE_DIRECTED_EDGE:
  471. case CREATE_UNDIRECTED_EDGE:
  472. if (lastClickedID == null) {
  473. lastClickedID = id;
  474. if (!selectNodeForEdgeCreation(lastClickedID)) {
  475. lastClickedID = null;
  476. }
  477. } else if (id.equals(lastClickedID) || createEdge(id, lastClickedID)) {
  478. deselectNodesAfterEdgeCreation(lastClickedID);
  479. lastClickedID = null;
  480. }
  481. break;
  482. default:
  483. break;
  484. }
  485. PropertiesManager.setItemsProperties();
  486. }
  487. /**
  488. * creates a edge between two nodes.
  489. *
  490. * @author MW
  491. * @param to
  492. * ID of the destination node
  493. * @param from
  494. * ID of the origin node
  495. * @return true if the edge was created. false otherwise
  496. */
  497. protected boolean createEdge(String to, String from) {
  498. if (getGraph().getNode(from).hasEdgeBetween(to))
  499. return false;
  500. String newID = Main.getInstance().getUnusedID();
  501. boolean isDirected = (Main.getInstance().getCreationMode() == CreationMode.CREATE_DIRECTED_EDGE);
  502. Node sourceNode = g.getNode(from);
  503. Node targetNode = g.getNode(to);
  504. if (activeSubGraph != null && !activeSubGraph.equals(g)) {
  505. if (sourceNode.getAttribute("originalGraph").equals(activeSubGraph.getId())
  506. && targetNode.getAttribute("originalGraph").equals(activeSubGraph.getId())) {
  507. g.addEdge(newID, from, to, isDirected);
  508. activeSubGraph.addEdge(newID, from.substring(activeSubGraph.getId().length()), to.substring(activeSubGraph.getId().length()), isDirected);
  509. g.getEdge(newID).addAttribute("originalElement", activeSubGraph.getId() + "+#" + newID);
  510. } else {
  511. if (sourceNode.getAttribute("originalGraph").equals(targetNode.getAttribute("originalGraph"))){
  512. Debug.out("Can Only add edges to currently active Subgraph!", 2);
  513. } else {
  514. Debug.out("Can not create Edge between Nodes of different Subgraphs!", 2);
  515. }
  516. }
  517. } else {
  518. g.addEdge(newID, from, to, isDirected);
  519. }
  520. /*
  521. if (Main.getInstance().getCreationMode() == CreationMode.CREATE_DIRECTED_EDGE) {
  522. getGraph().addEdge(newID, from, to, true);
  523. Debug.out("Created an directed edge with Id " + newID + " between " + from + " and " + to);
  524. } else {
  525. getGraph().addEdge(newID, from, to);
  526. Debug.out("Created an undirected edge with Id " + newID + " between " + from + " and " + to);
  527. }
  528. */
  529. selectEdge(newID);
  530. return true;
  531. }
  532. /**
  533. * Selects a Node as the starting point for creating a new Edge.
  534. *
  535. * @param nodeID
  536. * the ID of the Node to select
  537. */
  538. protected boolean selectNodeForEdgeCreation(String nodeID) {
  539. deselect();
  540. Node n = getGraph().getNode(nodeID);
  541. if (!hasClass(n, UI_CLASS_PROCESSING_ENABLED) || !GraphDisplayManager.getCurrentLayer().equals(Layer.MAPPING)) {
  542. n.changeAttribute("ui.style", "fill-color:green; size: 15px;");
  543. }
  544. return true;
  545. }
  546. /**
  547. * Reset the Selection of the Node after Edge has been successfully created.
  548. *
  549. * @param nodeID
  550. * the Id of the node to deselect.
  551. */
  552. protected void deselectNodesAfterEdgeCreation(String nodeID) {
  553. Node n = getGraph().getNode(nodeID);
  554. if (n == null) {
  555. return;
  556. }
  557. if (!hasClass(n, UI_CLASS_PROCESSING_ENABLED) || !GraphDisplayManager.getCurrentLayer().equals(Layer.MAPPING)) {
  558. n.removeAttribute("ui.style");
  559. n.changeAttribute("ui.style", "fill-color: #000000; size: 15px;");
  560. }
  561. }
  562. /**
  563. * Resets the selction of the Node for Edge selection
  564. */
  565. public void deselectEdgeCreationNodes() {
  566. if (lastClickedID != null)
  567. deselectNodesAfterEdgeCreation(lastClickedID);
  568. lastClickedID = null;
  569. }
  570. public void setActiveSubGraph(String id) {
  571. for (MyGraph subGraph : g.getAllSubGraphs()) {
  572. if (subGraph.getId().equals(id)) {
  573. activeSubGraph = subGraph;
  574. return;
  575. }
  576. }
  577. }
  578. protected boolean addClass(String id, String className) {
  579. Element e = getGraph().getEdge(id);
  580. if (e == null)
  581. e = getGraph().getNode(id);
  582. if (e == null)
  583. return false;
  584. String eClass = e.getAttribute("ui.class");
  585. if (eClass == null || eClass.equals(""))
  586. eClass = className;
  587. else if (!(eClass.equals(className) || eClass.startsWith(className.concat(", "))
  588. || eClass.contains(", ".concat(className))))
  589. eClass = className.concat(", ").concat(eClass);
  590. e.addAttribute("ui.class", eClass);
  591. Debug.out("added " + className + ": " + eClass);
  592. return true;
  593. }
  594. protected boolean removeClass(String id, String className) {
  595. Element e = getGraph().getEdge(id);
  596. if (e == null)
  597. e = getGraph().getNode(id);
  598. if (e == null)
  599. return false;
  600. String eClass = e.getAttribute("ui.class");
  601. if (eClass == null || eClass.equals(className))
  602. eClass = "";
  603. else
  604. eClass = eClass.replace(className.concat(", "), "").replace(", ".concat(className), "");
  605. e.addAttribute("ui.class", eClass);
  606. Debug.out("removed " + className + ": " + eClass);
  607. return true;
  608. }
  609. protected boolean toggleClass(String id, String className) {
  610. Element e = getGraph().getEdge(id);
  611. if (e == null)
  612. e = getGraph().getNode(id);
  613. if (e == null)
  614. return false;
  615. String eClass = e.getAttribute("ui.class");
  616. if (eClass == null || !(eClass.equals(className) || eClass.startsWith(className.concat(", "))
  617. || eClass.contains(", ".concat(className))))
  618. return addClass(id, className);
  619. return removeClass(id, className);
  620. }
  621. protected boolean hasClass(String id, String className) {
  622. Element e = getGraph().getEdge(id);
  623. if (e == null)
  624. e = getGraph().getNode(id);
  625. if (e == null)
  626. return false;
  627. String eClass = e.getAttribute("ui.class");
  628. return (eClass != null && (eClass.equals(className) || eClass.startsWith(className.concat(", "))
  629. || eClass.contains(", ".concat(className))));
  630. }
  631. protected boolean hasClass(Edge e, String className) {
  632. if (e == null)
  633. return false;
  634. String eClass = e.getAttribute("ui.class");
  635. return (eClass != null && (eClass.equals(className) || eClass.startsWith(className.concat(", "))
  636. || eClass.contains(", ".concat(className))));
  637. }
  638. protected boolean hasClass(Node n, String className) {
  639. if (n == null)
  640. return false;
  641. String nClass = n.getAttribute("ui.class");
  642. /*
  643. * TODO: nochmal angucken, wenn CSS Manager steht, ist gerade gehackt
  644. * damit, Vorführung läuft. return (nClass != null &&
  645. * (nClass.equals(className) ||
  646. * nClass.startsWith(className.concat(", ")) ||
  647. * nClass.contains(", ".concat(className))));
  648. */
  649. return (nClass != null && nClass.contains(className));
  650. }
  651. }