|
1 | 1 | package nl.tudelft.ti2806.pl1.DGraph; |
2 | 2 |
|
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.HashSet; |
| 5 | +import java.util.Set; |
| 6 | + |
| 7 | +import nl.tudelft.ti2806.pl1.gui.contentpane.ViewArea; |
| 8 | + |
3 | 9 | import org.graphstream.graph.Graph; |
| 10 | +import org.graphstream.graph.Node; |
4 | 11 | import org.graphstream.graph.implementations.SingleGraph; |
5 | 12 |
|
6 | 13 | /** |
7 | | - * Converts a DGraph into a graphstream graph. |
| 14 | + * Contains methods to create a visual graph from a data graph. |
8 | 15 | * |
9 | | - * @author mark |
| 16 | + * @author Mark, Maarten, Justin |
10 | 17 | * |
| 18 | + * @see Graph |
| 19 | + * @see DGraph |
11 | 20 | */ |
12 | 21 | public final class ConvertDGraph { |
13 | 22 |
|
14 | 23 | /** |
15 | | - * When this threshold is surpassed, the length instead of the label will be |
16 | | - * shown. |
| 24 | + * When this threshold is surpassed, the length instead of the content of a |
| 25 | + * node will be displayed on or next to the node. |
17 | 26 | */ |
18 | 27 | private static final int LABEL_LENGTH_THRESHOLD = 10; |
19 | 28 |
|
20 | 29 | /** |
21 | | - * Converts a DGraph into a graphstream graph. |
| 30 | + */ |
| 31 | + private ConvertDGraph() { |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Converts a data graph into a visual graph. |
| 36 | + * |
22 | 37 | * |
23 | 38 | * @param dgraph |
24 | | - * The dgraph to be converted |
25 | | - * @return Graphstream graph |
| 39 | + * The data graph to convert. |
| 40 | + * @return The visual graph containing all the nodes from the data graph. |
26 | 41 | */ |
27 | 42 | public static Graph convert(final DGraph dgraph) { |
28 | | - Graph graph = new SingleGraph("DNApp"); |
29 | | - for (DNode n : dgraph.getNodes().values()) { |
| 43 | + return convert(dgraph, new ViewArea(Integer.MIN_VALUE, |
| 44 | + Integer.MAX_VALUE)); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Converts a part of a data graph into a visual graph. |
| 49 | + * |
| 50 | + * @param dgraph |
| 51 | + * The data graph to convert. |
| 52 | + * @param va |
| 53 | + * The area of the graph to convert. |
| 54 | + * @return The visual graph containing the nodes positioned in the given |
| 55 | + * view area of the data graph. |
| 56 | + */ |
| 57 | + public static Graph convert(final DGraph dgraph, final ViewArea va) { |
| 58 | + Graph graph = new SingleGraph(""); |
| 59 | + Set<DEdge> edges = new HashSet<DEdge>(); |
| 60 | + for (DNode n : dgraph.getDNodes(va)) { |
| 61 | + edges.addAll(n.getAllEdges()); |
30 | 62 | String id = String.valueOf(n.getId()); |
31 | | - graph.addNode(id); |
32 | | - graph.getNode(id).addAttribute("x", n.getX()); |
33 | | - graph.getNode(id).addAttribute("y", n.getY()); |
34 | | - graph.getNode(id).addAttribute("ui.label", |
35 | | - checkLabelLength(n.getContent())); |
36 | | - graph.getNode(id).addAttribute("ui.class", "common"); |
37 | | - graph.getNode(id).addAttribute("ui.color", 1 - n.getPercUnknown()); |
| 63 | + Node gn = graph.addNode(id); |
| 64 | + gn.addAttribute("x", n.getX()); |
| 65 | + gn.addAttribute("y", n.getY()); |
| 66 | + gn.addAttribute("ui.label", checkLabelLength(n.getContent())); |
| 67 | + gn.addAttribute("ui.class", "common"); |
| 68 | + gn.addAttribute("ui.color", 1 - n.getPercUnknown()); |
| 69 | + gn.addAttribute("contentsize", n.getContent().length()); |
| 70 | + gn.addAttribute("collapsed", |
| 71 | + new HashSet<Integer>(Arrays.asList(n.getId()))); |
38 | 72 | } |
39 | | - for (DEdge edge : dgraph.getEdges()) { |
40 | | - String src = String.valueOf(edge.getStartNode().getId()); |
41 | | - String target = String.valueOf(edge.getEndNode().getId()); |
42 | | - graph.addEdge(src + target, src, target, true); |
| 73 | + for (DEdge edge : edges) { |
| 74 | + String from = String.valueOf(edge.getStartNode().getId()); |
| 75 | + String to = String.valueOf(edge.getEndNode().getId()); |
| 76 | + if (graph.getNode(from) == null) { |
| 77 | + addNodeToGraph(graph, from, dgraph); |
| 78 | + } else if (graph.getNode(to) == null) { |
| 79 | + addNodeToGraph(graph, to, dgraph); |
| 80 | + } |
| 81 | + graph.addEdge(from + to, from, to, true); |
43 | 82 | } |
44 | 83 | return graph; |
45 | 84 | } |
46 | 85 |
|
| 86 | + /** |
| 87 | + * Creates a visual node object, extracts the needed data from a data graph |
| 88 | + * and adds it to a visual graph. |
| 89 | + * |
| 90 | + * @param g |
| 91 | + * The graph to add the new node to. |
| 92 | + * @param id |
| 93 | + * The id for the new node. |
| 94 | + * @param dg |
| 95 | + * The data graph where the node is defined. |
| 96 | + * @return |
| 97 | + */ |
| 98 | + private static void addNodeToGraph(final Graph g, final String id, |
| 99 | + final DGraph dg) { |
| 100 | + Node gn = g.addNode(id); |
| 101 | + DNode n = dg.getDNode(Integer.parseInt(id)); |
| 102 | + gn.addAttribute("x", n.getX()); |
| 103 | + gn.addAttribute("y", n.getY()); |
| 104 | + gn.addAttribute("ui.label", checkLabelLength(n.getContent())); |
| 105 | + gn.addAttribute("ui.class", "common"); |
| 106 | + gn.addAttribute("ui.color", 1 - n.getPercUnknown()); |
| 107 | + } |
| 108 | + |
47 | 109 | /** |
48 | 110 | * Compares the label with a threshold length and returns the label if it's |
49 | 111 | * smaller than the threshold, otherwise it returns the length. |
|
0 commit comments