@Test
 public void layoutRadial() {
   Graph graph =
       interpreter.create(new Shell(), SWT.NONE, "graph Sample{graph[layout=radial];1;}");
   Assert.assertNotNull("Created graph must not be null", graph);
   Assert.assertEquals(RadialLayoutAlgorithm.class, graph.getLayoutAlgorithm().getClass());
 }
 @Test
 public void globalEdgeLabel() {
   Graph graph =
       interpreter.create(new Shell(), SWT.NONE, "graph Sample{edge[label=\"Edge1\"];1;2;1->2}");
   Assert.assertNotNull("Created graph must not be null", graph);
   Assert.assertEquals("Edge1", ((GraphConnection) graph.getConnections().get(0)).getText());
 }
 @Test
 public void globalNodeLabel() {
   Graph graph =
       interpreter.create(new Shell(), SWT.NONE, "graph Sample{node[label=\"Node1\"];1;}");
   Assert.assertNotNull("Created graph must not be null", graph);
   Assert.assertEquals("Node1", ((GraphNode) graph.getNodes().get(0)).getText());
 }
  public static void main(String[] args) {
    // Create the shell
    Display d = new Display();
    Shell shell = new Shell(d);
    shell.setText("GraphSnippet1");
    shell.setLayout(new FillLayout());
    shell.setSize(500, 500);

    final Graph g = new Graph(shell, SWT.NONE);
    g.setSize(500, 500);
    GraphNode root = new GraphNode(g, SWT.NONE, "Root");
    for (int i = 0; i < 3; i++) {
      GraphNode n = new GraphNode(g, SWT.NONE, "1 - " + i);
      for (int j = 0; j < 3; j++) {
        GraphNode n2 = new GraphNode(g, SWT.NONE, "2 - " + j);
        new GraphConnection(g, SWT.NONE, n, n2).setWeight(-1);
      }
      new GraphConnection(g, SWT.NONE, root, n);
    }

    final LayoutAlgorithm layoutAlgorithm = new RadialLayoutAlgorithm();

    g.setLayoutAlgorithm(layoutAlgorithm, true);
    shell.open();
    while (!shell.isDisposed()) {
      while (!d.readAndDispatch()) {
        d.sleep();
      }
    }
  }
 @Test
 public void digraphType() {
   Shell shell = new Shell();
   Graph graph = interpreter.create(shell, SWT.NONE, "digraph Sample{1;2;1->2}");
   Assert.assertNotNull("Created graph must not be null", graph);
   Assert.assertEquals(ZestStyles.CONNECTIONS_DIRECTED, graph.getConnectionStyle());
   // open(shell);
 }
 @Test
 public void dotImport() {
   Shell shell = new Shell();
   DotImport importer = new DotImport("digraph Sample{1;2;1->2}");
   Graph graph = importer.newGraphInstance(shell, SWT.NONE);
   Assert.assertNotNull("Created graph must not be null", graph);
   Assert.assertEquals(ZestStyles.CONNECTIONS_DIRECTED, graph.getConnectionStyle());
   // open(shell);
 }
 @Test
 public void globalEdgeStyle() {
   Shell parent = new Shell();
   Graph graph = interpreter.create(parent, SWT.NONE, "graph Sample{edge[style=dashed];1;2;1->2}");
   Assert.assertNotNull("Created graph must not be null", graph);
   Assert.assertEquals(
       SWT.LINE_DASH, ((GraphConnection) graph.getConnections().get(0)).getLineStyle());
   // open(parent);
 }
  /*
   * (non-Javadoc)
   *
   * @see org.eclipse.jface.viewers.Viewer#inputChanged(java.lang.Object,
   *      java.lang.Object)
   */
  protected void inputChanged(Object input, Object oldInput) {
    IStylingGraphModelFactory factory = getFactory();
    factory.setConnectionStyle(getConnectionStyle());
    factory.setNodeStyle(getNodeStyle());

    // Save the old map so we can set the size and position of any nodes
    // that are the same
    Map oldNodesMap = nodesMap;
    Graph graph = (Graph) getControl();
    graph.setSelection(new GraphNode[0]);

    Iterator iterator = nodesMap.values().iterator();
    while (iterator.hasNext()) {
      GraphNode node = (GraphNode) iterator.next();
      if (!node.isDisposed()) {
        node.dispose();
      }
    }

    iterator = connectionsMap.values().iterator();
    while (iterator.hasNext()) {
      GraphConnection connection = (GraphConnection) iterator.next();
      if (!connection.isDisposed()) {
        connection.dispose();
      }
    }

    nodesMap = new HashMap();
    connectionsMap = new HashMap();

    graph = factory.createGraphModel(graph);

    ((Graph) getControl()).setNodeStyle(getNodeStyle());
    ((Graph) getControl()).setConnectionStyle(getConnectionStyle());

    // check if any of the pre-existing nodes are still present
    // in this case we want them to keep the same location & size
    for (Iterator iter = oldNodesMap.keySet().iterator(); iter.hasNext(); ) {
      Object data = iter.next();
      GraphNode newNode = (GraphNode) nodesMap.get(data);
      if (newNode != null) {
        GraphNode oldNode = (GraphNode) oldNodesMap.get(data);
        newNode.setLocation(oldNode.getLocation().x, oldNode.getLocation().y);
        if (oldNode.isSizeFixed()) {
          newNode.setSize(oldNode.getSize().width, oldNode.getSize().height);
        }
      }
    }

    applyLayout();
  }
  /**
   * Test importing a DOT graph to a Zest graph instance directly. Internally, this compiles the
   * generated Zest class and loads it using Reflection.
   */
  static void test(final IGraphCreator converter) {

    /*
     * This is not really working, it only appears to be, as the generated file will be compiled by the
     * IDE and will then be available to the classloader. A symptom of this is that it will only work
     * starting with the second run.
     */
    Shell shell = new Shell();
    String dot1 = "digraph TestingGraph {1;2;3;4; 1->2;2->3;2->4}";
    Graph graph = converter.create(shell, SWT.NONE, dot1);
    Assert.assertNotNull("Created graph must exist!", graph);
    // open(shell); // blocks UI when running tests
    Assert.assertEquals(4, graph.getNodes().size());
    Assert.assertEquals(3, graph.getConnections().size());
    System.out.println(
        String.format(
            "Imported '%s' to Graph '%s' of type '%s'",
            dot1, graph, graph.getClass().getSimpleName()));

    /*
     * Check a unique name works, i.e. no existing classes on the classpath could be used instead of the
     * new one:
     */
    String dot2 = "digraph TestingGraph" + System.currentTimeMillis() + "{1;2;3;4; 1->2;2->3;2->4}";
    graph = converter.create(shell, SWT.NONE, dot2);
    Assert.assertNotNull("Created graph must exist!", graph);
    // open(shell); // blocks UI when running tests
    Assert.assertEquals(4, graph.getNodes().size());
    Assert.assertEquals(3, graph.getConnections().size());
    System.out.println(
        String.format(
            "Imported '%s' to Graph '%s' of type '%s'",
            dot2, graph, graph.getClass().getSimpleName()));

    /*
     * Check if a DOT graph with the same name is changed when the generated class is loaded:
     */
    String dot3 = "digraph TestingGraph{1;2;3 1->2;2->3}";
    graph = converter.create(shell, SWT.NONE, dot3);
    Assert.assertNotNull("Created graph must exist!", graph);
    Assert.assertEquals(3, graph.getNodes().size());
    Assert.assertEquals(2, graph.getConnections().size());
    System.out.println(
        String.format(
            "Imported '%s' to Graph '%s' of type '%s'",
            dot3, graph, graph.getClass().getSimpleName()));
    // open(shell); // blocks UI when running tests
  }
 /*
  * (non-Javadoc)
  *
  * @see org.eclipse.jface.viewers.StructuredViewer#setSelectionToWidget(java.util.List,
  *      boolean)
  */
 protected void setSelectionToWidget(List l, boolean reveal) {
   Graph control = (Graph) getControl();
   List selection = new LinkedList();
   for (Iterator i = l.iterator(); i.hasNext(); ) {
     Object obj = i.next();
     GraphNode node = (GraphNode) nodesMap.get(obj);
     GraphConnection conn = (GraphConnection) connectionsMap.get(obj);
     if (node != null) {
       selection.add(node);
     }
     if (conn != null) {
       selection.add(conn);
     }
   }
   control.setSelection((GraphNode[]) selection.toArray(new GraphNode[selection.size()]));
 }
Exemple #11
0
  private void clearGraph(Graph g) {

    // remove all the connections
    Object[] objects = g.getConnections().toArray();

    for (int x = 0; x < objects.length; x++) {
      ((GraphConnection) objects[x]).dispose();
    }

    // remove all the nodes

    objects = g.getNodes().toArray();

    for (int x = 0; x < objects.length; x++) {
      ((GraphNode) objects[x]).dispose();
    }
  }
 @Test
 public void nodeDefaultLabel() {
   Graph graph = interpreter.create(new Shell(), SWT.NONE, "graph Sample{1}");
   Assert.assertNotNull("Created graph must not be null", graph);
   Assert.assertEquals("1", ((GraphNode) graph.getNodes().get(0)).getText());
 }
 /**
  * Converts the list of GraphModelNode objects into an array an returns it.
  *
  * @return GraphModelNode[]
  */
 protected GraphNode[] getNodesArray(Graph graph) {
   GraphNode[] nodesArray = new GraphNode[graph.getNodes().size()];
   nodesArray = (GraphNode[]) graph.getNodes().toArray(nodesArray);
   return nodesArray;
 }
 /**
  * Converts the list of GraphModelConnection objects into an array and returns it.
  *
  * @return GraphModelConnection[]
  */
 protected GraphConnection[] getConnectionsArray(Graph graph) {
   GraphConnection[] connsArray = new GraphConnection[graph.getConnections().size()];
   connsArray = (GraphConnection[]) graph.getConnections().toArray(connsArray);
   return connsArray;
 }
 /**
  * Gets the internal model elements that are selected.
  *
  * @return
  */
 protected List getWidgetSelection() {
   Graph control = (Graph) getControl();
   return control.getSelection();
 }
 @Test
 public void nodeCount() {
   Graph graph = interpreter.create(new Shell(), SWT.NONE, "graph Sample{1;2}");
   Assert.assertNotNull("Created graph must not be null", graph);
   Assert.assertEquals(2, graph.getNodes().size());
 }
 @Test
 public void edgeCount() {
   Graph graph = interpreter.create(new Shell(), SWT.NONE, "graph Sample{1;2;1->2;2->2;1->1}");
   Assert.assertNotNull("Created graph must not be null", graph);
   Assert.assertEquals(3, graph.getConnections().size());
 }
  @Override
  protected Control createDialogArea(Composite parent) {
    //		toolkit = new FormToolkit(parent.getDisplay());
    //		form = toolkit.createScrolledForm(parent);
    //		form.setText("Dependency Graph UI Legend");
    //		form.getToolBarManager().add(new Action("Close Dialog", MavenEditorImages.CLEAR) {
    //		  public void run() {
    //	      close();
    //		  }
    //		});
    //		form.getToolBarManager().update(true);
    //		form.getBody().setLayout(new TableWrapLayout());
    //		toolkit.decorateFormHeading(form.getForm());

    Graph g =
        new Graph(parent, SWT.NONE) {
          public org.eclipse.swt.graphics.Point computeSize(int wHint, int hHint, boolean changed) {
            return new org.eclipse.swt.graphics.Point(260, 300);
          }
        };
    g.setConnectionStyle(ZestStyles.CONNECTIONS_DIRECTED);
    g.setEnabled(false);

    {
      GraphNode n1 = new GraphNode(g, SWT.NONE, " compile scope dependency ");
      n1.setLocation(10, 10);
      n1.setSize(240, 25);
    }

    {
      GraphNode n1 = new GraphNode(g, SWT.NONE, " non-compile scope dependency ");
      n1.setLocation(10, 40);
      n1.setSize(240, 25);
      n1.setBackgroundColor(colorTestBackground);
    }

    {
      GraphNode n1 = new GraphNode(g, SWT.NONE, " selected dependency ");
      n1.setLocation(10, 70);
      n1.setSize(240, 25);
      n1.setBackgroundColor(colorSelectedBackground);
    }

    {
      GraphNode n1 = new GraphNode(g, SWT.NONE, " selected non-compile ");
      n1.setLocation(10, 100);
      n1.setSize(240, 25);
      n1.setBackgroundColor(colorSelectedTestBackground);
    }

    {
      GraphNode n1 = new GraphNode(g, SWT.NONE, "    ");
      GraphNode n2 = new GraphNode(g, SWT.NONE, "    ");

      DependencyConnection c1 = new DependencyConnection(g, SWT.NONE, n1, n2);
      c1.setText("compile scope");
      c1.setConnectionStyle(ZestStyles.CONNECTIONS_DIRECTED | ZestStyles.CONNECTIONS_SOLID);

      n1.setLocation(10, 140);
      n2.setLocation(220, 140);
    }

    {
      GraphNode n1 = new GraphNode(g, SWT.NONE, "    ");
      GraphNode n2 = new GraphNode(g, SWT.NONE, "    ");
      n2.setBackgroundColor(colorTestBackground);

      GraphConnection c1 = new DependencyConnection(g, SWT.NONE, n1, n2);
      c1.setText("non-compile scope");
      c1.setConnectionStyle(ZestStyles.CONNECTIONS_DIRECTED | ZestStyles.CONNECTIONS_DOT);
      c1.setLineColor(colorTestRel);

      n1.setLocation(10, 170);
      n2.setLocation(220, 170);
    }

    {
      GraphNode n1 = new GraphNode(g, SWT.NONE, "    ");
      GraphNode n2 = new GraphNode(g, SWT.NONE, "    ");

      GraphConnection c1 = new DependencyConnection(g, SWT.NONE, n1, n2);
      c1.setText("resolved conflict");
      c1.setConnectionStyle(ZestStyles.CONNECTIONS_DIRECTED | ZestStyles.CONNECTIONS_SOLID);
      c1.setLineColor(colorRelResolved);

      n1.setLocation(10, 200);
      n2.setLocation(220, 200);
    }

    {
      GraphNode n1 = new GraphNode(g, SWT.NONE, "    ");
      GraphNode n2 = new GraphNode(g, SWT.NONE, "    ");
      n2.setBackgroundColor(colorSelectedBackground);

      GraphConnection c1 = new DependencyConnection(g, SWT.NONE, n1, n2);
      c1.setText("referenced from selected");
      c1.setConnectionStyle(ZestStyles.CONNECTIONS_DIRECTED | ZestStyles.CONNECTIONS_SOLID);
      c1.setLineColor(highlighted);
      c1.setLineWidth(3);

      n1.setLocation(10, 230);
      n2.setLocation(220, 230);
    }

    {
      GraphNode n1 = new GraphNode(g, SWT.NONE, "    ");
      GraphNode n2 = new GraphNode(g, SWT.NONE, "    ");
      n2.setBackgroundColor(colorSelectedTestBackground);

      GraphConnection c1 = new DependencyConnection(g, SWT.NONE, n1, n2);
      c1.setText("referenced from non-compile");
      c1.setConnectionStyle(ZestStyles.CONNECTIONS_DIRECTED | ZestStyles.CONNECTIONS_DOT);
      c1.setLineColor(highlighted);
      c1.setLineWidth(3);

      n1.setLocation(10, 260);
      n2.setLocation(220, 260);
    }

    g.addFocusListener(
        new FocusAdapter() {
          public void focusLost(FocusEvent e) {
            close();
          }
        });

    return parent;
  }