package structure; import java.util.*; import org.graphstream.graph.*; import org.graphstream.graph.implementations.SingleGraph; /** * Class for display graph with GraphStream * @author loic, adama * */ public class MyGraph extends SingleGraph{ // Grid associate to the graph private Grid grid; /** * Build a graph with a grid * * @param title * @param grid */ public MyGraph(String title, Grid grid) { super(title); // Allow CSS on view System.setProperty("org.graphstream.ui.renderer", "org.graphstream.ui.j2dviewer.J2DGraphRenderer"); // Set graph CSS this.addAttribute("ui.stylesheet", "url('resources/style.css')"); // Assign grid this.grid=grid; int i=0; int ll=this.grid.getGrid().size(); // Build node for(Router r : this.grid.getGrid()){ if(i==0){ this.addNode(r.name).setAttribute("ui.label", "Source"); } else{ if(i==ll-1){ this.addNode(r.name).setAttribute("ui.label", "Destination"); } else{ this.addNode(r.name); } } i++; } // Build Edges this.buildEdges(); //Build bestLink this.showBestLink(); } /** * Build edges */ public void buildEdges(){ for(Router r : this.grid.getGrid()){ String current=r.name; HashMap relier=r.getLinks(); Set k=relier.keySet(); Iterator i=k.iterator(); while(i.hasNext()){ Router currentRouter=i.next(); String currentRouterName=currentRouter.name; try{ Edge toAdd=this.addEdge(current+currentRouterName, current, currentRouterName); toAdd.setAttribute("ui.label", relier.get(currentRouter)); } catch(Exception e){ } } } } /** * Update best link on screen */ public void showBestLink(){ ArrayList bestLink=this.grid.getLinks().get(this.grid.getBestLinkByProtocol()); for(int i=0;i nodes= this.getNodeIterator(); while(nodes.hasNext()){ Node node=nodes.next(); Iterator edges=node.getEdgeIterator(); while(edges.hasNext()){ Edge edge=edges.next(); if(i<(bestLink.size()-1)){ String src=this.grid.getGrid().get(bestLink.get(i)).name; String dest=this.grid.getGrid().get(bestLink.get(i+1)).name; if((edge.getNode0().getId().equals(src) && edge.getNode1().getId().equals(dest))||(edge.getNode1().getId().equals(src) && edge.getNode0().getId().equals(dest))){ edge.setAttribute("ui.style", "fill-color:red;"); } } } } } } /** * Update all the graph on screen */ public void update(){ // Reset color Iterator edges=this.getEdgeIterator(); while(edges.hasNext()){ Edge edge=edges.next(); edge.setAttribute("ui.style", "fill-color:black;"); } // Update label edges=this.getEdgeIterator(); while(edges.hasNext()){ Edge edge=edges.next(); for(Router r : this.grid.getGrid()){ String current=r.name; HashMap relier=r.getLinks(); Set k=relier.keySet(); Iterator i=k.iterator(); while(i.hasNext()){ Router currentRouter=i.next(); String currentRouterName=currentRouter.name; if(edge.getId().equals(current+currentRouterName)||edge.getId().equals(currentRouterName+current)){ edge.setAttribute("ui.label", relier.get(currentRouter)); } } } } this.showBestLink(); } }