Guest User

Increase label visibility in GraphStream

a guest
Jul 8th, 2013
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.69 KB | None | 0 0
  1. import java.io.IOException;
  2.  
  3. import org.graphstream.algorithm.layout.openord.OpenOrdLayout;
  4. import org.graphstream.graph.Edge;
  5. import org.graphstream.graph.Graph;
  6. import org.graphstream.graph.Node;
  7. import org.graphstream.graph.implementations.MultiGraph;
  8. import org.graphstream.stream.PipeBase;
  9. import org.graphstream.stream.file.FileSource;
  10. import org.graphstream.stream.file.FileSourceFactory;
  11. import org.graphstream.ui.layout.springbox.implementations.SpringBox;
  12. import org.graphstream.ui.swingViewer.Viewer;
  13.  
  14. public class LabelVisibility {
  15.     public static class VisibleLink extends PipeBase {
  16.         public void edgeAdded(String sourceId, long timeId, String edgeId,
  17.                 String source, String target, boolean directed) {
  18.             sendNodeAdded(this.sourceId, edgeId);
  19.             sendNodeAttributeAdded(this.sourceId, edgeId, "ui.class", "link");
  20.  
  21.             sendEdgeAdded(this.sourceId, edgeId + "_A", source, edgeId,
  22.                     directed);
  23.             sendEdgeAdded(this.sourceId, edgeId + "_B", edgeId, target,
  24.                     directed);
  25.         }
  26.  
  27.         public void edgeRemoved(String sourceId, long timeId, String edgeId) {
  28.             sendNodeRemoved(this.sourceId, edgeId);
  29.         }
  30.  
  31.         public void edgeAttributeAdded(String sourceId, long timeId,
  32.                 String edgeId, String attributeId, Object value) {
  33.             sendNodeAttributeAdded(this.sourceId, edgeId, attributeId, value);
  34.  
  35.             if (attributeId.equals("ui.class")) {
  36.                 sendEdgeAttributeAdded(this.sourceId, edgeId + "_A",
  37.                         attributeId, value);
  38.                 sendEdgeAttributeAdded(this.sourceId, edgeId + "_B",
  39.                         attributeId, value);
  40.             }
  41.         }
  42.  
  43.         public void edgeAttributeChanged(String sourceId, long timeId,
  44.                 String edgeId, String attributeId, Object oldValue, Object value) {
  45.             sendNodeAttributeChanged(this.sourceId, edgeId, attributeId,
  46.                     oldValue, value);
  47.  
  48.             if (attributeId.equals("ui.class")) {
  49.                 sendEdgeAttributeChanged(this.sourceId, edgeId + "_A",
  50.                         attributeId, oldValue, value);
  51.                 sendEdgeAttributeChanged(this.sourceId, edgeId + "_B",
  52.                         attributeId, oldValue, value);
  53.             }
  54.         }
  55.  
  56.         public void edgeAttributeRemoved(String sourceId, long timeId,
  57.                 String edgeId, String attributeId) {
  58.             sendNodeAttributeRemoved(this.sourceId, edgeId, attributeId);
  59.         }
  60.     }
  61.  
  62.     public static void main(String[] args) {
  63.         System.setProperty("org.graphstream.ui.renderer",
  64.                 "org.graphstream.ui.j2dviewer.J2DGraphRenderer");
  65.  
  66.         Graph graph = new MultiGraph("__test__");
  67.         Graph displayGraph = new MultiGraph("__test_display__");
  68.         FileSource fs = null;
  69.  
  70.         VisibleLink vl = new VisibleLink();
  71.         graph.addSink(vl);
  72.         vl.addSink(displayGraph);
  73.  
  74.         try {
  75.             fs = FileSourceFactory.sourceFor(___source___);
  76.             fs.addSink(graph);
  77.             fs.readAll(___source___);
  78.         } catch (IOException e) {
  79.             System.err.println(e.getMessage());
  80.         } finally {
  81.             fs.removeSink(graph);
  82.         }
  83.  
  84.         for (Node node : graph)
  85.             node.addAttribute("ui.label", node.getId());
  86.  
  87.         for (Edge edge : graph.getEachEdge()) {
  88.             edge.addAttribute("ui.label", edge.getAttribute("arcvalue"));
  89.  
  90.             if (edge.getNumber("arcvalue") > 100)
  91.                 edge.addAttribute("ui.class", "biglink");
  92.         }
  93.  
  94.         graph.addAttribute("ui.quality");
  95.         graph.addAttribute("ui.antialias");
  96.         graph.addAttribute("layout.gravity", 0.02);
  97.         graph.addAttribute(
  98.                 "ui.stylesheet",
  99.                 "node {text-background-mode:plain;text-background-color:yellow;text-size:14px;text-padding:2px;} "
  100.                         + "node.link {text-background-mode:plain;text-background-color:orange;text-size:11px;} "
  101.                         + "node.biglink {text-background-color:red;text-size:11px;} "
  102.                         + "edge {fill-color:gray;}"
  103.                         + "edge.biglink {size:4px; fill-color:black;}");
  104.  
  105.         Viewer v = displayGraph.display(false);
  106.         SpringBox l = new SpringBox();
  107.         v.enableAutoLayout(l);
  108.         l.shake();
  109.     }
  110. }
Add Comment
Please, Sign In to add comment