示例#1
0
  @SuppressWarnings("unchecked")
  protected void setDummyEdges() {
    int nodeCount = nodeList.size();

    // if node count is only one then we don't have to worry about whether
    // the nodes are connected
    if (nodeCount > 1) {
      NodeList candidateList = new NodeList();
      for (Iterator<Node> iter = nodeList.iterator(); iter.hasNext(); ) {
        Node sourceNode = iter.next();

        // we will need to set up a dummy relationship for any node not
        // in one already
        if (sourceNode.outgoing.size() == 0 && sourceNode.incoming.size() == 0) {
          candidateList.add(sourceNode);
          sourceNode.setRowConstraint(2);
        } else {
          sourceNode.setRowConstraint(1);
        }
      }
      if (candidateList.size() > 1) {
        int index = 0;
        while (index < candidateList.size() - 1) {
          Node sourceNode = candidateList.getNode(index++);
          Node targetNode = candidateList.getNode(index);
          newDummyEdge(targetNode, sourceNode);
        }
      }
    }
  }
  @Override
  public void run() {
    try {
      CompoundCommand commands = new CompoundCommand();
      List models = getViewer().getContents().getChildren();
      NodeList graphNodes = new NodeList();
      EdgeList graphEdges = new EdgeList();

      // nodes
      for (int i = 0; i < models.size(); i++) {
        Object obj = models.get(i);
        if (obj instanceof TableEditPart) {
          TableEditPart editPart = (TableEditPart) obj;
          Table model = (Table) editPart.getModel();
          EntityNode node = new EntityNode();
          node.model = model;
          node.width = editPart.getFigure().getSize().width;
          node.height = editPart.getFigure().getSize().height + 40;
          graphNodes.add(node);
        }
      }

      // edge
      for (int i = 0; i < models.size(); i++) {
        Object obj = models.get(i);
        if (obj instanceof TableEditPart) {
          TableEditPart tableEditpart = (TableEditPart) obj;

          List outgoing = tableEditpart.getSourceConnections();
          for (int j = 0; j < outgoing.size(); j++) {
            RelationEditPart conn = (RelationEditPart) outgoing.get(j);
            EntityNode source =
                (EntityNode) getNode(graphNodes, (Table) conn.getSource().getModel());
            EntityNode target =
                (EntityNode) getNode(graphNodes, (Table) conn.getTarget().getModel());

            if (source != null && target != null) {
              ConnectionEdge edge = new ConnectionEdge(source, target);
              Relation relation = (Relation) conn.getModel();
              edge.model = relation.getSource();
              graphEdges.add(edge);
            }
          }
        }
      }

      DirectedGraph graph = new DirectedGraph();
      graph.nodes = graphNodes;
      graph.edges = graphEdges;
      new DirectedGraphLayout().visit(graph);
      for (int i = 0; i < graph.nodes.size(); i++) {
        EntityNode node = (EntityNode) graph.nodes.getNode(i);
        commands.add(new LayoutCommand(node.model, node.x, node.y));
      }

      getViewer().getEditDomain().getCommandStack().execute(commands);
    } catch (Exception e) {
      logger.error(Messages.AutoLayoutAction_2, e);

      Status errStatus =
          new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e); // $NON-NLS-1$
      ExceptionDetailsErrorDialog.openError(
          PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
          "Error",
          Messages.AutoLayoutAction_3,
          errStatus); //$NON-NLS-1$
    }
  }