Package com.mxgraph.view

Examples of com.mxgraph.view.mxGraph


    boolean source = isSource(index);

    if (source || isTarget(index))
    {
      mxGraph graph = graphComponent.getGraph();
      mxIGraphModel model = graph.getModel();
      Object terminal = model.getTerminal(state.getCell(), source);

      if ((terminal == null && !graph.isTerminalPointMovable(
          state.getCell(), source))
          || (terminal != null && !graph.isCellDisconnectable(
              state.getCell(), terminal, source)))
      {
        first = null;
      }
    }
View Full Code Here


    person2.setAttribute("lastName", "Bunny");

    Element relation = doc.createElement("Knows");
    relation.setAttribute("since", "1985");

    mxGraph graph = new mxGraph()
    {
      // Overrides method to disallow edge label editing
      public boolean isCellEditable(Object cell)
      {
        return !getModel().isEdge(cell);
      }

      // Overrides method to provide a cell label in the display
      public String convertValueToString(Object cell)
      {
        if (cell instanceof mxCell)
        {
          Object value = ((mxCell) cell).getValue();

          if (value instanceof Element)
          {
            Element elt = (Element) value;

            if (elt.getTagName().equalsIgnoreCase("person"))
            {
              String firstName = elt.getAttribute("firstName");
              String lastName = elt.getAttribute("lastName");

              if (lastName != null && lastName.length() > 0)
              {
                return lastName + ", " + firstName;
              }

              return firstName;
            }
            else if (elt.getTagName().equalsIgnoreCase("knows"))
            {
              return elt.getTagName() + " (Since "
                  + elt.getAttribute("since") + ")";
            }

          }
        }

        return super.convertValueToString(cell);
      }

      // Overrides method to store a cell label in the model
      public void cellLabelChanged(Object cell, Object newValue,
          boolean autoSize)
      {
        if (cell instanceof mxCell && newValue != null)
        {
          Object value = ((mxCell) cell).getValue();

          if (value instanceof Node)
          {
            String label = newValue.toString();
            Element elt = (Element) value;

            if (elt.getTagName().equalsIgnoreCase("person"))
            {
              int pos = label.indexOf(' ');

              String firstName = (pos > 0) ? label.substring(0,
                  pos).trim() : label;
              String lastName = (pos > 0) ? label.substring(
                  pos + 1, label.length()).trim() : "";

              // Clones the value for correct undo/redo
              elt = (Element) elt.cloneNode(true);

              elt.setAttribute("firstName", firstName);
              elt.setAttribute("lastName", lastName);

              newValue = elt;
            }
          }
        }

        super.cellLabelChanged(cell, newValue, autoSize);
      }
    };

    Object parent = graph.getDefaultParent();

    graph.getModel().beginUpdate();
    try
    {
      Object v1 = graph.insertVertex(parent, null, person1, 20, 20, 80,
          30);
      Object v2 = graph.insertVertex(parent, null, person2, 240, 150, 80,
          30);
      graph.insertEdge(parent, null, relation, v1, v2);
    }
    finally
    {
      graph.getModel().endUpdate();
    }

    // Overrides method to create the editing value
    mxGraphComponent graphComponent = new mxGraphComponent(graph)
    {
View Full Code Here

   *
   */
  public GraphEditor(String appTitle, mxGraphComponent component)
  {
    super(appTitle, component);
    final mxGraph graph = graphComponent.getGraph();

    // Creates the shapes palette
    EditorPalette shapesPalette = insertPalette(mxResources.get("shapes"));
    EditorPalette imagesPalette = insertPalette(mxResources.get("images"));
    EditorPalette symbolsPalette = insertPalette(mxResources.get("symbols"));

    // Sets the edge template to be used for creating new edges if an edge
    // is clicked in the shape palette
    shapesPalette.addListener(mxEvent.SELECT, new mxIEventListener()
    {
      public void invoke(Object sender, mxEventObject evt)
      {
        Object tmp = evt.getProperty("transferable");

        if (tmp instanceof mxGraphTransferable)
        {
          mxGraphTransferable t = (mxGraphTransferable) tmp;
          Object cell = t.getCells()[0];

          if (graph.getModel().isEdge(cell))
          {
            ((CustomGraph) graph).setEdgeTemplate(cell);
          }
        }
      }
View Full Code Here

    Document xmlDocument = mxUtils.createDocument();
    Element sourceNode = xmlDocument.createElement("Source");
    Element targetNode = xmlDocument.createElement("Target");
    Element subtargetNode = xmlDocument.createElement("Subtarget");

    mxGraph graph = new mxGraph();
    Object parent = graph.getDefaultParent();

    graph.getModel().beginUpdate();
    try
    {
      Object v1 = graph.insertVertex(parent, null, sourceNode, 20, 20,
          80, 30);
      Object v2 = graph.insertVertex(parent, null, targetNode, 200, 20,
          80, 30);
      Object v3 = graph.insertVertex(parent, null, targetNode
          .cloneNode(true), 200, 80, 80, 30);
      Object v4 = graph.insertVertex(parent, null, targetNode
          .cloneNode(true), 200, 140, 80, 30);
      graph.insertVertex(parent, null, subtargetNode, 200,
          200, 80, 30);
      Object v6 = graph.insertVertex(parent, null, sourceNode
          .cloneNode(true), 20, 140, 80, 30);
      graph.insertEdge(parent, null, "", v1, v2);
      graph.insertEdge(parent, null, "", v1, v3);
      graph.insertEdge(parent, null, "", v6, v4);
      //Object e4 = graph.insertEdge(parent, null, "", v1, v4);
    }
    finally
    {
      graph.getModel().endUpdate();
    }

    mxMultiplicity[] multiplicities = new mxMultiplicity[3];

    // Source nodes needs 1..2 connected Targets
    multiplicities[0] = new mxMultiplicity(true, "Source", null, null, 1,
        "2", Arrays.asList(new String[] { "Target" }),
        "Source Must Have 1 or 2 Targets",
        "Source Must Connect to Target", true);

    // Source node does not want any incoming connections
    multiplicities[1] = new mxMultiplicity(false, "Source", null, null, 0,
        "0", null, "Source Must Have No Incoming Edge", null, true); // Type does not matter

    // Target needs exactly one incoming connection from Source
    multiplicities[2] = new mxMultiplicity(false, "Target", null, null, 1,
        "1", Arrays.asList(new String[] { "Source" }),
        "Target Must Have 1 Source", "Target Must Connect From Source",
        true);

    graph.setMultiplicities(multiplicities);

    final mxGraphComponent graphComponent = new mxGraphComponent(graph);
    graph.setMultigraph(false);
    graph.setAllowDanglingEdges(false);
    graphComponent.setConnectable(true);
    graphComponent.setToolTips(true);

    // Enables rubberband selection
    new mxRubberband(graphComponent);
    new mxKeyboardHandler(graphComponent);

    // Installs automatic validation (use editor.validation = true
    // if you are using an mxEditor instance)
    graph.getModel().addListener(mxEvent.CHANGE, new mxIEventListener()
    {
      public void invoke(Object sender, mxEventObject evt)
      {
        graphComponent.validateGraph();
      }
View Full Code Here

  @SuppressWarnings("serial")
  public EditorMenuBar(final BasicGraphEditor editor)
  {
    final mxGraphComponent graphComponent = editor.getGraphComponent();
    final mxGraph graph = graphComponent.getGraph();
    JMenu menu = null;
    JMenu submenu = null;

    // Creates the file menu
    menu = add(new JMenu(mxResources.get("file")));
View Full Code Here

  /**
   *
   */
  public void setGraph(mxGraph value)
  {
    mxGraph oldValue = graph;

    // Uninstalls listeners for existing graph
    if (graph != null)
    {
      graph.removeListener(repaintHandler);
View Full Code Here

  /**
   *
   */
  public Object createTargetVertex(MouseEvent e, Object source)
  {
    mxGraph graph = graphComponent.getGraph();
    Object clone = graph.cloneCells(new Object[] { source })[0];
    mxIGraphModel model = graph.getModel();
    mxGeometry geo = model.getGeometry(clone);

    if (geo != null)
    {
      mxPoint point = graphComponent.getPointForEvent(e);
      geo.setX(graph.snap(point.getX() - geo.getWidth() / 2));
      geo.setY(graph.snap(point.getY() - geo.getHeight() / 2));
    }

    return clone;
  }
View Full Code Here

        JOptionPane.showMessageDialog(graphComponent, error);
      }
    }
    else if (first != null)
    {
      mxGraph graph = graphComponent.getGraph();
      double dx = first.getX() - e.getX();
      double dy = first.getY() - e.getY();

      if (connectPreview.isActive()
          && (marker.hasValidState() || isCreateTarget() || graph
              .isAllowDanglingEdges()))
      {
        graph.getModel().beginUpdate();

        try
        {
          Object dropTarget = null;

          if (!marker.hasValidState() && isCreateTarget())
          {
            Object vertex = createTargetVertex(e, source.getCell());
            dropTarget = graph.getDropTarget(
                new Object[] { vertex }, e.getPoint(),
                graphComponent.getCellAt(e.getX(), e.getY()));

            if (vertex != null)
            {
              // Disables edges as drop targets if the target cell was created
              if (dropTarget == null
                  || !graph.getModel().isEdge(dropTarget))
              {
                mxCellState pstate = graph.getView().getState(
                    dropTarget);

                if (pstate != null)
                {
                  mxGeometry geo = graph.getModel()
                      .getGeometry(vertex);

                  mxPoint origin = pstate.getOrigin();
                  geo.setX(geo.getX() - origin.getX());
                  geo.setY(geo.getY() - origin.getY());
                }
              }
              else
              {
                dropTarget = graph.getDefaultParent();
              }

              graph.addCells(new Object[] { vertex }, dropTarget);
            }

            // FIXME: Here we pre-create the state for the vertex to be
            // inserted in order to invoke update in the connectPreview.
            // This means we have a cell state which should be created
            // after the model.update, so this should be fixed.
            mxCellState targetState = graph.getView().getState(
                vertex, true);
            connectPreview.update(e, targetState, e.getX(), e.getY());
          }

          Object cell = connectPreview.stop(
              graphComponent.isSignificant(dx, dy), e);

          if (cell != null)
          {
            graphComponent.getGraph().setSelectionCell(cell);
            eventSource.fireEvent(new mxEventObject(mxEvent.CONNECT,
                "cell", cell, "event", e, "target", dropTarget));
          }

          e.consume();
        }
        finally
        {
          graph.getModel().endUpdate();
        }
      }
    }

    reset();
View Full Code Here

  /**
   * Returns a dirty rectangle to be repainted in mxGraphControl.
   */
  public mxRectangle show()
  {
    mxGraph graph = graphComponent.getGraph();
    mxIGraphModel model = graph.getModel();

    // Stores a copy of the cell states
    List<mxCellState> previousStates = null;

    if (isCloned())
    {
      previousStates = new LinkedList<mxCellState>();
      Iterator<mxCellState> it = deltas.keySet().iterator();

      while (it.hasNext())
      {
        mxCellState state = it.next();
        previousStates.addAll(snapshot(state));
      }
    }

    // Translates the states in step
    Iterator<mxCellState> it = deltas.keySet().iterator();

    while (it.hasNext())
    {
      mxCellState state = it.next();
      mxPoint delta = deltas.get(state);
      mxCellState parentState = graph.getView().getState(
          model.getParent(state.getCell()));
      translateState(parentState, state, delta.getX(), delta.getY());
    }

    // Revalidates the states in step
    mxRectangle dirty = null;
    it = deltas.keySet().iterator();

    while (it.hasNext())
    {
      mxCellState state = it.next();
      mxPoint delta = deltas.get(state);
      mxCellState parentState = graph.getView().getState(
          model.getParent(state.getCell()));
      mxRectangle tmp = revalidateState(parentState, state, delta.getX(),
          delta.getY());

      if (dirty != null)
View Full Code Here

  /**
   *
   */
  public void restore(List<mxCellState> snapshot)
  {
    mxGraph graph = graphComponent.getGraph();
    Iterator<mxCellState> it = snapshot.iterator();

    while (it.hasNext())
    {
      mxCellState state = it.next();
      mxCellState orig = graph.getView().getState(state.getCell());

      if (orig != null && orig != state)
      {
        restoreState(orig, state);
      }
View Full Code Here

TOP

Related Classes of com.mxgraph.view.mxGraph

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.