Beispiel #1
0
        /// <summary>
        /// Recursively creates the cell state for the given cell if visible is true and
        /// the given cell is visible. If the cell is not visible but the state exists
        /// then it is removed using removeState.
        /// </summary>
        /// <param name="cell">Cell whose cell state should be created.</param>
        /// <param name="visible">Boolean indicating if the cell should be visible.</param>
        public Object ValidateCell(Object cell, Boolean visible)
        {
            if (cell != null)
            {
                visible = visible && graph.IsCellVisible(cell);
                mxCellState state = GetState(cell, visible);

                if (state != null && !visible)
                {
                    RemoveState(cell);
                }
                else
                {
                    mxIGraphModel model      = graph.Model;
                    int           childCount = model.GetChildCount(cell);

                    for (int i = 0; i < childCount; i++)
                    {
                        ValidateCell(
                            model.GetChildAt(cell, i),
                            visible && !graph.IsCellCollapsed(cell));
                    }
                }
            }

            return(cell);
        }
Beispiel #2
0
        /// <summary>
        /// Invoked when a child state was moved as a result of late evaluation
        /// of its position. This is invoked for relative edge children whose
        /// position can only be determined after the points of the parent edge
        /// are updated in validatePoints, and validates the bounds of all
        /// descendants of the child using validateBounds.
        /// </summary>
        /// <param name="parent">State that represents the parent.</param>
        /// <param name="child">State that represents the child.</param>
        public void childMoved(mxCellState parent, mxCellState child)
        {
            Object cell = child.Cell;

            // Children of relative edge children need to validate
            // their bounds after their parent state was updated
            if (!graph.IsCellCollapsed(cell))
            {
                mxIGraphModel model      = graph.Model;
                int           childCount = model.GetChildCount(cell);

                for (int i = 0; i < childCount; i++)
                {
                    ValidateBounds(child, model.GetChildAt(cell, i));
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Returns the children of the given cell that are vertices and/or edges
        /// depending on the arguments.
        /// </summary>
        /// <param name="model">Model that contains the hierarchical information.</param>
        /// <param name="parent">Cell whose child vertices or edges should be returned.</param>
        /// <param name="vertices">Boolean indicating if child vertices should be returned.</param>
        /// <param name="edges">Boolean indicating if child edges should be returned.</param>
        /// <returns>Returns the child vertices and/or edges of the given parent.</returns>
        public static Object[] getChildCells(mxIGraphModel model, Object parent,
                                             bool vertices, bool edges)
        {
            int           childCount = model.GetChildCount(parent);
            List <Object> result     = new List <Object>(childCount);

            for (int i = 0; i < childCount; i++)
            {
                Object child = model.GetChildAt(parent, i);

                if ((edges && model.IsEdge(child)) ||
                    (vertices && model.IsVertex(child)))
                {
                    result.Add(child);
                }
            }

            return(result.ToArray());
        }
Beispiel #4
0
        /// <summary>
        /// Validates the cell state for the given cell.
        /// </summary>
        /// <param name="cell">Cell whose cell state should be validated.</param>
        /// <param name="recurse">Boolean indicating if the children of the cell should be
        /// validated.</param>
        /// <returns></returns>
        public mxCellState ValidateCellState(Object cell, Boolean recurse)
        {
            mxCellState state = null;

            if (cell != null)
            {
                state = GetState(cell);

                if (state != null)
                {
                    mxIGraphModel model = graph.Model;

                    if (state.Invalid)
                    {
                        state.Invalid = false;

                        ValidateCellState(model.GetParent(cell), false);
                        mxCellState source = ValidateCellState(GetVisibleTerminal(cell, true), false);
                        mxCellState target = ValidateCellState(GetVisibleTerminal(cell, false), false);

                        UpdateCellState(state, source, target);

                        if (model.IsEdge(cell) || model.IsVertex(cell))
                        {
                            UpdateLabelBounds(state);
                            UpdateBoundingBox(state);
                        }
                    }

                    if (recurse)
                    {
                        int childCount = model.GetChildCount(cell);

                        for (int i = 0; i < childCount; i++)
                        {
                            ValidateCellState(model.GetChildAt(cell, i));
                        }
                    }
                }
            }

            return(state);
        }
Beispiel #5
0
        /// <summary>
        /// Returns the bounding box of the shape and the label for the given
        /// cell state and its children if recurse is true.
        /// </summary>
        /// <param name="state">Cell state whose bounding box should be returned.</param>
        /// <param name="recurse">Boolean indicating if the children should be included.</param>
        public mxRectangle GetBoundingBox(mxCellState state, Boolean recurse)
        {
            mxRectangle bbox = null;

            if (state != null)
            {
                if (state.BoundingBox != null)
                {
                    bbox = (mxRectangle)state.BoundingBox.Clone();
                }

                if (recurse)
                {
                    mxIGraphModel model      = graph.Model;
                    int           childCount = model.GetChildCount(state.Cell);

                    for (int i = 0; i < childCount; i++)
                    {
                        mxRectangle bounds = GetBoundingBox(
                            GetState(model.GetChildAt(state.Cell, i)), true);

                        if (bounds != null)
                        {
                            if (bbox == null)
                            {
                                bbox = bounds;
                            }
                            else
                            {
                                bbox.Add(bounds);
                            }
                        }
                    }
                }
            }

            return(bbox);
        }
Beispiel #6
0
        /// <summary>
        /// Removes and returns the mxCellState for the given cell.
        /// </summary>
        /// <param name="cell">mxCell for which the mxCellState should be removed.</param>
        /// <returns>Returns the mxCellState that has been removed.</returns>
        public mxCellState RemoveState(Object cell, Boolean recurse)
        {
            if (recurse)
            {
                mxIGraphModel model      = graph.Model;
                int           childCount = model.GetChildCount(cell);

                for (int i = 0; i < childCount; i++)
                {
                    RemoveState(model.GetChildAt(cell, i), true);
                }
            }

            mxCellState state = null;

            if (states.ContainsKey(cell))
            {
                state = states[cell];
                states.Remove(cell);
            }

            return(state);
        }
Beispiel #7
0
        /// <summary>
        /// Executes the fast organic layout.
        /// </summary>
        /// <param name="parent"></param>
        public void execute(Object parent)
        {
            mxIGraphModel model = graph.Model;

            // Finds the relevant vertices for the layout
            int           childCount = model.GetChildCount(parent);
            List <Object> tmp        = new List <Object>(childCount);

            for (int i = 0; i < childCount; i++)
            {
                Object child = model.GetChildAt(parent, i);

                if (!IsCellIgnored(child))
                {
                    tmp.Add(child);
                }
            }

            vertexArray = tmp.ToArray();
            int n = vertexArray.Length;

            dispX         = new double[n];
            dispY         = new double[n];
            cellLocation  = new double[n][];
            isMoveable    = new bool[n];
            neighbours    = new int[n][];
            radius        = new double[n];
            radiusSquared = new double[n];

            minDistanceLimitSquared = minDistanceLimit * minDistanceLimit;

            if (forceConstant < 0.001)
            {
                forceConstant = 0.001;
            }

            forceConstantSquared = forceConstant * forceConstant;

            // Create a map of vertices first. This is required for the array of
            // arrays called neighbours which holds, for each vertex, a list of
            // ints which represents the neighbours cells to that vertex as
            // the indices into vertexArray
            for (int i = 0; i < vertexArray.Length; i++)
            {
                Object vertex = vertexArray[i];
                cellLocation[i] = new double[2];

                // Set up the mapping from array indices to cells
                indices[vertex] = i;
                mxGeometry bounds = model.GetGeometry(vertex);

                // Set the X,Y value of the internal version of the cell to
                // the center point of the vertex for better positioning
                double width  = bounds.Width;
                double height = bounds.Height;

                // Randomize (0, 0) locations
                double x = bounds.X;
                double y = bounds.Y;

                cellLocation[i][0] = x + width / 2.0;
                cellLocation[i][1] = y + height / 2.0;

                radius[i]        = Math.Min(width, height);
                radiusSquared[i] = radius[i] * radius[i];
            }

            for (int i = 0; i < n; i++)
            {
                dispX[i]      = 0;
                dispY[i]      = 0;
                isMoveable[i] = graph.IsCellMovable(vertexArray[i]);

                // Get lists of neighbours to all vertices, translate the cells
                // obtained in indices into vertexArray and store as an array
                // against the orginial cell index
                Object[] edges = mxGraphModel.GetEdges(model, vertexArray[i]);
                Object[] cells = mxGraphModel.GetOpposites(model, edges,
                                                           vertexArray[i], true, true);

                neighbours[i] = new int[cells.Length];

                for (int j = 0; j < cells.Length; j++)
                {
                    int?index = indices[cells[j]];

                    // Check the connected cell in part of the vertex list to be
                    // acted on by this layout
                    if (index != null)
                    {
                        neighbours[i][j] = (int)index;
                    }

                    // Else if index of the other cell doesn't correspond to
                    // any cell listed to be acted upon in this layout. Set
                    // the index to the value of this vertex (a dummy self-loop)
                    // so the attraction force of the edge is not calculated
                    else
                    {
                        neighbours[i][j] = i;
                    }
                }
            }
            temperature = initialTemp;

            // If max number of iterations has not been set, guess it
            if (maxIterations == 0)
            {
                maxIterations = (int)(20 * Math.Sqrt(n));
            }

            // Main iteration loop
            for (iteration = 0; iteration < maxIterations; iteration++)
            {
                if (!allowedToRun)
                {
                    return;
                }

                // Calculate repulsive forces on all vertices
                calcRepulsion();

                // Calculate attractive forces through edges
                calcAttraction();

                calcPositions();
                reduceTemperature();
            }

            // Moved cell location back to top-left from center locations used in
            // algorithm
            model.BeginUpdate();
            try
            {
                double?minx = null;
                double?miny = null;

                for (int i = 0; i < vertexArray.Length; i++)
                {
                    Object     vertex = vertexArray[i];
                    mxGeometry geo    = model.GetGeometry(vertex);

                    if (geo != null)
                    {
                        cellLocation[i][0] -= geo.Width / 2.0;
                        cellLocation[i][1] -= geo.Height / 2.0;

                        geo = geo.Clone();

                        geo.X = graph.Snap(cellLocation[i][0]);
                        geo.Y = graph.Snap(cellLocation[i][1]);

                        model.SetGeometry(vertex, geo);

                        if (minx == null)
                        {
                            minx = geo.X;
                        }
                        else
                        {
                            minx = Math.Min((double)minx, geo.X);
                        }

                        if (miny == null)
                        {
                            miny = geo.Y;
                        }
                        else
                        {
                            miny = Math.Min((double)miny, geo.Y);
                        }
                    }
                }

                // Modifies the cloned geometries in-place. Not needed
                // to clone the geometries again as we're in the same
                // undoable change.
                if (minx != null || miny != null)
                {
                    for (int i = 0; i < vertexArray.Length; i++)
                    {
                        Object     vertex = vertexArray[i];
                        mxGeometry geo    = model.GetGeometry(vertex);

                        if (geo != null)
                        {
                            if (minx != null)
                            {
                                geo.X -= ((double)minx) - 1;
                            }

                            if (miny != null)
                            {
                                geo.Y -= ((double)miny) - 1;
                            }
                        }
                    }
                }
            }
            finally
            {
                model.EndUpdate();
            }
        }
Beispiel #8
0
        /// <summary>
        /// Returns the children of the given cell that are vertices and/or edges
        /// depending on the arguments.
        /// </summary>
        /// <param name="model">Model that contains the hierarchical information.</param>
        /// <param name="parent">Cell whose child vertices or edges should be returned.</param>
        /// <param name="vertices">Boolean indicating if child vertices should be returned.</param>
        /// <param name="edges">Boolean indicating if child edges should be returned.</param>
        /// <returns>Returns the child vertices and/or edges of the given parent.</returns>
        public static Object[] getChildCells(mxIGraphModel model, Object parent,
                bool vertices, bool edges)
        {
            int childCount = model.GetChildCount(parent);
            List<Object> result = new List<Object>(childCount);

            for (int i = 0; i < childCount; i++)
            {
                Object child = model.GetChildAt(parent, i);

                if ((edges && model.IsEdge(child))
                        || (vertices && model.IsVertex(child)))
                {
                    result.Add(child);
                }
            }

            return result.ToArray();
        }
Beispiel #9
0
        /// <summary>
        /// Validates the points for the state of the given cell recursively if the
        /// cell is not collapsed and returns the bounding box of all visited states
        /// as a rectangle.
        /// </summary>
        public mxRectangle ValidatePoints(mxCellState parentState, Object cell)
        {
            mxIGraphModel model = graph.Model;
            mxCellState   state = GetState(cell);
            mxRectangle   bbox  = null;

            if (state != null)
            {
                mxGeometry geo = graph.GetCellGeometry(cell);

                if (geo != null && model.IsEdge(cell))
                {
                    // Updates the points on the source terminal if its an edge
                    mxCellState source = GetState(GetVisibleTerminal(cell, true));

                    if (source != null && model.IsEdge(source.Cell) &&
                        !model.IsAncestor(source, cell))
                    {
                        mxCellState tmp = GetState(model.GetParent(source.Cell));
                        ValidatePoints(tmp, source.Cell);
                    }

                    // Updates the points on the target terminal if its an edge
                    mxCellState target = GetState(GetVisibleTerminal(cell, false));

                    if (target != null && model.IsEdge(target.Cell) &&
                        !model.IsAncestor(target.Cell, cell))
                    {
                        mxCellState tmp = GetState(model.GetParent(target.Cell));
                        ValidatePoints(tmp, target.Cell);
                    }

                    UpdateFixedTerminalPoints(state, source, target);
                    UpdatePoints(state, geo.Points, source, target);
                    UpdateFloatingTerminalPoints(state, source, target);
                    UpdateEdgeBounds(state);
                    state.AbsoluteOffset = GetPoint(state, geo);
                }
                else if (geo != null &&
                         geo.Relative &&
                         parentState != null &&
                         model.IsEdge(parentState.Cell))
                {
                    mxPoint origin = GetPoint(parentState, geo);

                    if (origin != null)
                    {
                        state.X = origin.X;
                        state.Y = origin.Y;

                        origin.X     = (origin.X / scale) - translate.X;
                        origin.Y     = (origin.Y / scale) - translate.Y;
                        state.Origin = origin;

                        childMoved(parentState, state);
                    }
                }

                if (model.IsEdge(cell) || model.IsVertex(cell))
                {
                    UpdateLabelBounds(state);
                    bbox = new mxRectangle(UpdateBoundingBox(state));
                }
            }

            if (state != null && !graph.IsCellCollapsed(cell))
            {
                int childCount = model.GetChildCount(cell);

                for (int i = 0; i < childCount; i++)
                {
                    Object      child  = model.GetChildAt(cell, i);
                    mxRectangle bounds = ValidatePoints(state, child);

                    if (bounds != null)
                    {
                        if (bbox == null)
                        {
                            bbox = bounds;
                        }
                        else
                        {
                            bbox.Add(bounds);
                        }
                    }
                }
            }

            return(bbox);
        }
Beispiel #10
0
        /// <summary>
        /// Validates the bounds of the given parent's child using the given parent
        /// state as the origin for the child. The validation is carried out
        /// recursively for all non-collapsed descendants.
        /// </summary>
        /// <param name="parentState">Cell state for the given parent.</param>
        /// <param name="cell">Cell for which the bounds in the state should be updated.</param>
        public void ValidateBounds(mxCellState parentState, Object cell)
        {
            mxIGraphModel model = graph.Model;
            mxCellState   state = GetState(cell, true);

            if (state != null)
            {
                if (!graph.IsCellVisible(cell))
                {
                    RemoveState(cell);
                }
                else if (parentState != null)
                {
                    state.AbsoluteOffset.X = 0;
                    state.AbsoluteOffset.Y = 0;
                    state.Origin           = new mxPoint(parentState.Origin.X,
                                                         parentState.Origin.Y);
                    mxGeometry geo = graph.GetCellGeometry(cell);

                    if (geo != null)
                    {
                        if (!model.IsEdge(cell))
                        {
                            mxPoint origin = state.Origin;
                            mxPoint offset = geo.Offset;

                            if (offset == null)
                            {
                                offset = EMPTY_POINT;
                            }

                            if (geo.Relative)
                            {
                                origin.X += geo.X * parentState.Width /
                                            Scale + offset.X;
                                origin.Y += geo.Y * parentState.Height /
                                            Scale + offset.Y;
                            }
                            else
                            {
                                state.AbsoluteOffset = new mxPoint(
                                    scale * offset.X,
                                    scale * offset.Y);
                                origin.X += geo.X;
                                origin.Y += geo.Y;
                            }
                        }

                        // Updates the cell state's bounds
                        state.X      = scale * (translate.X + state.Origin.X);
                        state.Y      = scale * (translate.Y + state.Origin.Y);
                        state.Width  = scale * geo.Width;
                        state.Height = scale * geo.Height;

                        if (model.IsVertex(cell))
                        {
                            UpdateVertexLabelOffset(state);
                        }
                    }
                }

                // Applies child offset to origin
                mxPoint childOffset = graph.GetChildOffsetForCell(cell);

                if (childOffset != null)
                {
                    state.Origin.X += childOffset.X;
                    state.Origin.Y += childOffset.Y;
                }
            }

            // Recursively validates the child bounds
            if (state != null &&
                !graph.IsCellCollapsed(cell))
            {
                int childCount = model.GetChildCount(cell);

                for (int i = 0; i < childCount; i++)
                {
                    ValidateBounds(state, model.GetChildAt(cell, i));
                }
            }
        }
        public void execute(Object parent)
        {
            mxIGraphModel model = graph.Model; //.GetModel();

            // Moves the vertices to build a circle. Makes sure the
            // radius is large enough for the vertices to not
            // overlap
            model.BeginUpdate();
            try
            {
                // Gets all vertices inside the parent and finds
                // the maximum dimension of the largest vertex
                double        max        = 0;
                Double        top        = 0;
                Double        left       = 0;
                List <Object> vertices   = new List <Object>();
                int           childCount = model.GetChildCount(parent);

                for (int i = 0; i < childCount; i++)
                {
                    Object cell = model.GetChildAt(parent, i);

                    if (!isVertexIgnored(cell))
                    {
                        vertices.add(cell);
                        mxRectangle bounds = getVertexBounds(cell);

                        if (top == null)
                        {
                            top = bounds.getY();
                        }
                        else
                        {
                            top = Math.Min(top, bounds.getY());
                        }

                        if (left == null)
                        {
                            left = bounds.getX();
                        }
                        else
                        {
                            left = Math.Min(left, bounds.getX());
                        }

                        max = Math.Max(max, Math.Max(bounds.getWidth(), bounds
                                                     .getHeight()));
                    }
                    else if (!isEdgeIgnored(cell))
                    {
                        if (isResetEdges())
                        {
                            graph.resetEdge(cell);
                        }

                        if (isDisableEdgeStyle())
                        {
                            setEdgeStyleEnabled(cell, false);
                        }
                    }
                }

                int    vertexCount = vertices.size();
                double r           = Math.Max(vertexCount * max / Math.PI, radius);

                // Moves the circle to the specified origin
                if (moveCircle)
                {
                    left = x0;
                    top  = y0;
                }

                circle(vertices.ToArray(), r, left, top);
            }
            finally
            {
                model.EndUpdate();
            }
        }