📄 mxgraph.java
字号:
cellsAdded(cells, group, index, null, null, false); cellsMoved(cells, -bounds.getX(), -bounds.getY(), false, true); // Adds the group into the parent and resizes index = model.getChildCount(parent); cellsAdded(new Object[] { group }, parent, index, null, null, false); cellsResized(new Object[] { group }, new mxRectangle[] { bounds }); fireEvent(mxEvent.GROUP_CELLS, new mxEventObject(new Object[] { group, border, cells })); } finally { model.endUpdate(); } } return group; } /** * Returns the cells with the same parent as the first cell * in the given array. This method always returns more than * one cell in the array or an empty array. */ public Object[] getCellsForGroup(Object[] cells) { List result = new ArrayList(cells.length); if (cells != null && cells.length > 1) { Object parent = model.getParent(cells[0]); result.add(cells[0]); // Filters selection cells with the same parent for (int i = 1; i < cells.length; i++) { if (model.getParent(cells[i]) == parent) { result.add(cells[i]); } } } return result.toArray(); } /** * Returns the bounds to be used for the given group and children. */ public mxRectangle getBoundsForGroup(Object group, Object[] children, double border) { mxRectangle bounds = view.getBounds(children); if (children != null && children.length > 0 && bounds != null) { Object parent = model.getParent(children[0]); mxCellState pstate = view.getState(parent); double scale = view.getScale(); mxPoint tr = view.getTranslate(); double x = bounds.getX() - pstate.getOrigin().getX() * scale; double y = bounds.getY() - pstate.getOrigin().getY() * scale; double width = bounds.getWidth(); double height = bounds.getHeight(); // Adds the startsize to the dimension if (isSwimlane(group)) { mxRectangle size = getStartSize(group); x -= size.getWidth(); width += size.getWidth(); y -= size.getHeight(); height += size.getHeight(); } bounds = new mxRectangle(x / scale - border - tr.getX(), y / scale - border - tr.getY(), width / scale + 2 * border, height / scale + 2 * border); } return bounds; } /** * Hook for creating the group cell to hold the given array of <mxCells> if * no group cell was given to the <group> function. The children are just * for informational purpose, they will be added to the returned group * later. Note that the returned group should have a geometry. The * coordinates of which are later overridden. * * @param cells * @return Returns a new group cell. */ public Object createGroupCell(Object[] cells) { mxCell group = new mxCell("", new mxGeometry(), null); group.setVertex(true); group.setConnectable(false); return group; } /** * Ungroups the selection cells. This is a shortcut method. */ public Object[] ungroupCells() { return ungroupCells(null); } /** * Ungroups the given cells by moving the children the children to their * parents parent and removing the empty groups. * * @param cells Array of cells to be ungrouped. If null is specified then * the selection cells are used. * @return Returns the children that have been removed from the groups. */ public Object[] ungroupCells(Object[] cells) { List result = new ArrayList(); if (cells == null) { cells = getSelectionCells(); } if (cells != null) { // Finds the cells with children List tmp = new ArrayList(cells.length); for (int i = 0; i < cells.length; i++) { if (model.getChildCount(cells[i]) > 0) { tmp.add(cells[i]); } } cells = tmp.toArray(); if (cells.length > 0) { model.beginUpdate(); try { for (int i = 0; i < cells.length; i++) { Object[] children = mxGraphModel.getChildren(model, cells[i]); if (children != null && children.length > 0) { Object parent = model.getParent(cells[i]); Integer index = model.getChildCount(parent); cellsAdded(children, parent, index, null, null, true); result.addAll(Arrays.asList(children)); } } cellsRemoved(addAllEdges(cells)); fireEvent(mxEvent.UNGROUP_CELLS, new mxEventObject( new Object[] { cells })); } finally { model.endUpdate(); } } } return result.toArray(); } /** * Removes the selection cells from their parents and adds them to the * default parent returned by getDefaultParent. */ public Object[] removeCellsFromParent() { return removeCellsFromParent(null); } /** * Removes the specified cells from their parents and adds them to the * default parent. * * @param cells Array of cells to be removed from their parents. * @return Returns the cells that were removed from their parents. */ public Object[] removeCellsFromParent(Object[] cells) { if (cells == null) { cells = getSelectionCells(); } model.beginUpdate(); try { Object parent = getDefaultParent(); Integer index = model.getChildCount(parent); cellsAdded(cells, parent, index, null, null, true); fireEvent(mxEvent.REMOVE_CELLS_FROM_PARENT, new mxEventObject( new Object[] { cells })); } finally { model.endUpdate(); } return cells; } // // Cell cloning, insertion and removal // /** * Clones all cells in the given array. To clone all children in a cell and * add them to another graph: * * <code> * graph2.addCells(graph.cloneCells(new Object[] { parent })); * </code> */ public Object[] cloneCells(Object[] cells) { return cloneCells(cells, true); } /** * Returns the clones for the given cells. If the terminal of an edge is * not in the given array, then the respective end is assigned a terminal * point and the terminal is removed. If a cloned edge is invalid and * allowInvalidEdges is false, then a null pointer will be at this position * in the returned array. Use getCloneableCells on the input array to only * clone the cells where isCellCloneable returns true. * * @param cells Array of mxCells to be cloned. * @return Returns the clones of the given cells. */ public Object[] cloneCells(Object[] cells, boolean allowInvalidEdges) { Object[] clones = null; if (cells != null) { Collection tmp = new LinkedHashSet(cells.length); tmp.addAll(Arrays.asList(cells)); if (!tmp.isEmpty()) { double scale = view.getScale(); mxPoint trans = view.getTranslate(); clones = model.cloneCells(cells, true); for (int i = 0; i < cells.length; i++) { if (!allowInvalidEdges && model.isEdge(clones[i]) && getEdgeValidationError(clones[i], model .getTerminal(clones[i], true), model .getTerminal(clones[i], false)) != null) { clones[i] = null; } else { mxGeometry g = model.getGeometry(clones[i]); if (g != null) { mxCellState state = view.getState(cells[i]); mxCellState pstate = view.getState(model .getParent(cells[i])); if (state != null && pstate != null) { double dx = pstate.getOrigin().getX(); double dy = pstate.getOrigin().getY(); if (model.isEdge(clones[i])) { // Checks if the source is cloned or sets the terminal point Object src = model.getTerminal(cells[i], true); while (src != null && !tmp.contains(src)) { src = model.getParent(src); } if (src == null) { mxPoint pt = state.getAbsolutePoint(0); g.setTerminalPoint(new mxPoint(pt .getX() / scale - trans.getX(), pt .getY() / scale - trans.getY()), true); } // Checks if the target is cloned or sets the terminal point Object trg = model.getTerminal(cells[i], false); while (trg != null && !tmp.contains(trg)) { trg = model.getParent(trg); } if (trg == null) { mxPoint pt = state .getAbsolutePoint(state .getAbsolutePointCount() - 1); g.setTerminalPoint(new mxPoint(pt .getX() / scale - trans.getX(), pt .getY() / scale - trans.getY()), false); } // Translates the control points List points = g.getPoints(); if (points != null) { Iterator it = points.iterator(); while (it.hasNext()) { mxPoint pt = (mxPoint) it.next(); pt.setX(pt.getX() + dx); pt.setY(pt.getY() + dy); } } } else { g.setX(g.getX() + dx); g.setY(g.getY() + dy); } } } } } } } return clones; } /** * Creates and adds a new vertex with an empty style. */ public Object insertVertex(Object parent, String id, Object value, double x, double y, double width, double height) { return insertVertex(parent, id, value, x, y, width, height, null); } /** * Adds a new vertex into the given parent using value as the user object * and the given coordinates as the geometry of the new vertex. The id and * style are used for the respective properties of the new cell, which is * returned. * * @param parent Cell that specifies the parent of the new vertex. * @param id Optional string that defines the Id of the new vertex. * @param value Object to be used as the user object. * @param x Integer that defines the x coordinate of the vertex. * @param y Integer that defines the y coordinate of the vertex. * @param width Integer that defines the width of the vertex. * @param height Integer that defines the height of the vertex. * @param style Optional string that defines the cell style. * @return Returns the new vertex that has been inserted. */ public Object insertVertex(Object parent, String id, Object value, double x, double y, double width, double height, String style) { Object vertex = createVertex(parent, id, value, x, y, width, height, style); return addCell(vertex, parent); } /** * Hook method that creates the new vertex for insertVertex. * * @param parent Cell that specifies the parent of the new vertex. * @param id Optional string that defines the Id of the new vertex. * @param value Object to be used as the user object. * @param x Integer that defines the x coordinate of the vertex. * @param y Integer that defines the y coordinate of the vertex. * @param width Integer that defines the width of the vertex. * @param height Integer that defines the height of the vertex. * @param style Optional string that defines the cell style. * @return Returns the new vertex to be inserted. */ public Object createVertex(Object parent, String id, Object value, double x, double y, double width, double height, String style) { mxGeometry geometry = new mxGeometry(x, y, width, height); mxCell vertex = new mxCell(value, geometry, style); vertex.setId(id); vertex.setVertex(true); vertex.setConnectable(true); return vertex; } /** * Creates and adds a new edge with an empty style. */ public Object insertEdge(Object parent, String id, Object value, Object source, Object target) { return insertEdge(parent, id, value, source, target, null); } /** * Adds a new edge into the given parent using value as the user object and * the given source and target as the terminals of the new edge. The Id and * style are used for the respective properties of the new cell, which is * returned. * * @param parent Cell that specifies the parent of the new edge. * @param id Optional string that defines the Id of the new edge. * @param value Object to be used as the user object. * @param source Cell that defines the source of the edge. * @param target Cell that defines the target of the edge. * @param style Optional string that defines the cell style. * @return Returns the new edge that has been inserted. */ public Object insertEdge(Object parent, String id, Object value, Object source, Object target, String style) { Object edge = createEdge(parent, id, value, source, target, style); return addEdge(edge, parent, source, target, null); } /** * Hook method that creates the new edge for insertEdge. * * @param parent Cell that specifies the parent of the new edge. * @param id Optional string that defines the Id of the new edge. * @param value Object to be used as the user object. * @param source Cell that defines the source of the edge. * @param target Cell that defines the target of the edge. * @param style Optional string that defines the cell style. * @return Returns the new edge to be inserted. */ public Object createEdge(Object parent, String id, Object value, Object source, Object target, String style) { mxCell edge = new mxCell(value, new mxGeometry(), style); edge.setId(id); edge.setEdge(true); edge.getGeometry().setRelative(true); return edge; } /** * Adds the edge to the parent and connects it to the given source and * target terminals. This is a shortcut method. * * @param cell Edge to be inserted into the given parent. * @param parent Object that represents the new parent. If no parent is * given then the default parent is used. * @param source Optional cell that represents the source terminal. * @param target Optional cell that represents the target terminal. * @param index Optional index to insert the cells at. Default is to append. * @return Returns the edge that was added. */ public Object addEdge(Object edge, Object parent, Object source, Object target, Integer index) { return addCell(edge, parent, index, source, target); } /** * Adds the cell to the default parent. This is a shortcut method. * * @param cells Array of cells to be inserted. * @return Returns the cell that was added. */ public Object addCell(Object cell) { return addCell(cell, null); } /** * Adds the cell to the parent. This is a shortcut method.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -