📄 mxgraphmodel.java
字号:
for (int i = 0; i < childCount; i++) { Object child = from.getChildAt(i); if (child instanceof mxICell) { mxICell cell = (mxICell) child; String id = cell.getId(); mxICell target = (mxICell) ((id != null && (!isEdge(cell) || !cloneAllEdges)) ? getCell(id) : null); // Clones and adds the child if no cell exists for the id if (target == null) { mxCell clone = (mxCell) cell.clone(); clone.setId(id); // Do *NOT* use model.add as this will move the edge away // from the parent in updateEdgeParent if maintainEdgeParent // is enabled in the target model target = (mxICell) to.insert(clone); cellAdded(target); } // Stores the mapping for later reconnecting edges mapping.put(cell, target); // Recurses mergeChildrenImpl(cell, target, cloneAllEdges, mapping); } } } finally { endUpdate(); } } /** * Returns the number of incoming or outgoing edges. * * @param model Graph model that contains the connection data. * @param cell Cell whose edges should be counted. * @param outgoing Boolean that specifies if the number of outgoing or * incoming edges should be returned. * @return Returns the number of incoming or outgoing edges. */ public static int getDirectedEdgeCount(mxIGraphModel model, Object cell, boolean outgoing) { return getDirectedEdgeCount(model, cell, outgoing, null); } /** * Returns the number of incoming or outgoing edges, ignoring the given * edge. * * @param model Graph model that contains the connection data. * @param cell Cell whose edges should be counted. * @param outgoing Boolean that specifies if the number of outgoing or * incoming edges should be returned. * @param ignoredEdge Object that represents an edge to be ignored. * @return Returns the number of incoming or outgoing edges. */ public static int getDirectedEdgeCount(mxIGraphModel model, Object cell, boolean outgoing, Object ignoredEdge) { int count = 0; int edgeCount = model.getEdgeCount(cell); for (int i = 0; i < edgeCount; i++) { Object edge = model.getEdgeAt(cell, i); if (edge != ignoredEdge && model.getTerminal(edge, outgoing) == cell) { count++; } } return count; } /** * Returns all edges connected to this cell including loops. * * @param model Model that contains the connection information. * @param cell Cell whose connections should be returned. * @return Returns the array of connected edges for the given cell. */ public static Object[] getEdges(mxIGraphModel model, Object cell) { return getEdges(model, cell, true, true, true); } /** * Returns all edges connected to this cell without loops. * * @param model Model that contains the connection information. * @param cell Cell whose connections should be returned. * @return Returns the connected edges for the given cell. */ public static Object[] getConnections(mxIGraphModel model, Object cell) { return getEdges(model, cell, true, true, false); } /** * Returns the incoming edges of the given cell without loops. * * @param model Graphmodel that contains the edges. * @param cell Cell whose incoming edges should be returned. * @return Returns the incoming edges for the given cell. */ public static Object[] getIncomingEdges(mxIGraphModel model, Object cell) { return getEdges(model, cell, true, false, false); } /** * Returns the outgoing edges of the given cell without loops. * * @param model Graphmodel that contains the edges. * @param cell Cell whose outgoing edges should be returned. * @return Returns the outgoing edges for the given cell. */ public static Object[] getOutgoingEdges(mxIGraphModel model, Object cell) { return getEdges(model, cell, false, true, false); } /** * Returns all distinct edges connected to this cell. * * @param model Model that contains the connection information. * @param cell Cell whose connections should be returned. * @param incoming Specifies if incoming edges should be returned. * @param outgoing Specifies if outgoing edges should be returned. * @param includeLoops Specifies if loops should be returned. * @return Returns the array of connected edges for the given cell. */ public static Object[] getEdges(mxIGraphModel model, Object cell, boolean incoming, boolean outgoing, boolean includeLoops) { int edgeCount = model.getEdgeCount(cell); List result = new ArrayList(edgeCount); for (int i = 0; i < edgeCount; i++) { Object edge = model.getEdgeAt(cell, i); Object source = model.getTerminal(edge, true); Object target = model.getTerminal(edge, false); if (includeLoops || ((source != target) && ((incoming && target == cell) || (outgoing && source == cell)))) { result.add(edge); } } return result.toArray(); } /** * Returns all edges from the given source to the given target. * * @param model The graph model that contains the graph. * @param source Object that defines the source cell. * @param target Object that defines the target cell. * @return Returns all edges from source to target. */ public static Object[] getEdgesBetween(mxIGraphModel model, Object source, Object target) { return getEdgesBetween(model, source, target, false); } /** * Returns all edges between the given source and target pair. If directed * is true, then only edges from the source to the target are returned, * otherwise, all edges between the two cells are returned. * * @param model The graph model that contains the graph. * @param source Object that defines the source cell. * @param target Object that defines the target cell. * @param directed Boolean that specifies if the direction of the edge * should be taken into account. * @return Returns all edges between the given source and target. */ public static Object[] getEdgesBetween(mxIGraphModel model, Object source, Object target, boolean directed) { int tmp1 = model.getEdgeCount(source); int tmp2 = model.getEdgeCount(target); // Assumes the source has less connected edges Object terminal = source; int edgeCount = tmp1; // Uses the smaller array of connected edges // for searching the edge if (tmp2 < tmp1) { edgeCount = tmp2; terminal = target; } List result = new ArrayList(edgeCount); // Checks if the edge is connected to the correct // cell and returns the first match for (int i = 0; i < edgeCount; i++) { Object edge = model.getEdgeAt(terminal, i); Object src = model.getTerminal(edge, true); Object trg = model.getTerminal(edge, false); boolean isSource = src == source; if (isSource && trg == target || (!directed && model.getTerminal(edge, !isSource) == target)) { result.add(edge); } } return result.toArray(); } /** * Returns all opposite cells of terminal for the given edges. * * @param model Model that contains the connection information. * @param edges Array of edges to be examined. * @param terminal Cell that specifies the known end of the edges. * @return Returns the opposite cells of the given terminal. */ public static Object[] getOpposites(mxIGraphModel model, Object[] edges, Object terminal) { return getOpposites(model, edges, terminal, true, true); } /** * Returns all opposite vertices wrt terminal for the given edges, only * returning sources and/or targets as specified. The result is returned as * an array of mxCells. * * @param model Model that contains the connection information. * @param edges Array of edges to be examined. * @param terminal Cell that specifies the known end of the edges. * @param sources Boolean that specifies if source terminals should * be contained in the result. Default is true. * @param targets Boolean that specifies if target terminals should * be contained in the result. Default is true. * @return Returns the array of opposite terminals for the given edges. */ public static Object[] getOpposites(mxIGraphModel model, Object[] edges, Object terminal, boolean sources, boolean targets) { List terminals = new ArrayList(); if (edges != null) { for (int i = 0; i < edges.length; i++) { Object source = model.getTerminal(edges[i], true); Object target = model.getTerminal(edges[i], false); // Checks if the terminal is the source of // the edge and if the target should be // stored in the result if (targets && source == terminal && target != null && target != terminal) { terminals.add(target); } // Checks if the terminal is the taget of // the edge and if the source should be // stored in the result else if (sources && target == terminal && source != null && source != terminal) { terminals.add(source); } } } return terminals.toArray(); } /** * Sets the source and target of the given edge in a single atomic change. * * @param edge Cell that specifies the edge. * @param source Cell that specifies the new source terminal. * @param target Cell that specifies the new target terminal. */ public static void setTerminals(mxIGraphModel model, Object edge, Object source, Object target) { model.beginUpdate(); try { model.setTerminal(edge, source, true); model.setTerminal(edge, target, false); } finally { model.endUpdate(); } } /** * Returns all children of the given cell regardless of their type. * * @param model Model that contains the hierarchical information. * @param parent Cell whose child vertices or edges should be returned. * @return Returns the child vertices and/or edges of the given parent. */ public static Object[] getChildren(mxIGraphModel model, Object parent) { return getChildCells(model, parent, false, false); } /** * Returns the child vertices of the given parent. * * @param model Model that contains the hierarchical information. * @param parent Cell whose child vertices should be returned. * @return Returns the child vertices of the given parent. */ public static Object[] getChildVertices(mxIGraphModel model, Object parent) { return getChildCells(model, parent, true, false); } /** * Returns the child edges of the given parent. * * @param model Model that contains the hierarchical information. * @param parent Cell whose child edges should be returned. * @return Returns the child edges of the given parent. */ public static Object[] getChildEdges(mxIGraphModel model, Object parent) { return getChildCells(model, parent, false, true); } /** * Returns the children of the given cell that are vertices and/or edges * depending on the arguments. If both arguments are false then all * children are returned regardless of their type. * * @param model Model that contains the hierarchical information. * @param parent Cell whose child vertices or edges should be returned. * @param vertices Boolean indicating if child vertices should be returned. * @param edges Boolean indicating if child edges should be returned. * @return Returns the child vertices and/or edges of the given parent. */ public static Object[] getChildCells(mxIGraphModel model, Object parent, boolean vertices, boolean edges) { int childCount = model.getChildCount(parent); List result = new ArrayList(childCount); for (int i = 0; i < childCount; i++) { Object child = model.getChildAt(parent, i); if ((!edges && !vertices) || (edges && model.isEdge(child)) || (vertices && model.isVertex(child))) { result.add(child); } } return result.toArray(); } /** * */ public static Object[] getParents(mxIGraphModel model, Object[] cells) { HashSet parents = new HashSet(); if (cells != null) { for (int i = 0; i < cells.length; i++) { Object parent = model.getParent(cells[i]); if (parent != null) { parents.add(parent); } } } return parents.toArray(); } /** * */ public static Object[] filterCells(Object[] cells, Filter filter) { ArrayList result = null; if (cells != null) { result = new ArrayList(cells.length); for (int i = 0; i < cells.length; i++) { if (filter.filter(cells[i])) { result.add(cells[i]); } } } return (result != null) ? result.toArray() : null; } /** * Returns a all descendants of the given cell and the cell itself * as a collection. */ public static Collection getDescendants(mxIGraphModel model, Object parent) { return filterDescendants(model, null, parent); } /** * Creates a collection of cells using the visitor pattern. */ public static Collection filterDescendants(mxIGraphModel model, Filter filter) { return filterDescendants(model, filter, model.getRoot()); } /** * Creates a collection of cells using the visitor pattern. */ public static Collection filterDescendants(mxIGraphModel model, Filter filter, Object parent) { List result = new ArrayList(); if (filter == null || filter.filter(parent)) { result.add(parent); } int childCount = model.getChildCount(parent); for (int i = 0; i < childCount; i++) { Object child = model.getChildAt(parent, i); result.addAll(filterDescendants(model, filter, child)); } return result; } /** * Function: getTopmostCells * * Returns the topmost cells of the hierarchy in an array that contains no * desceandants for each <mxCell> that it contains. Duplicates should be * removed in the cells array to improve performance. * * Parameters: * * cells - Array of <mxCells> whose topmost ancestors should be returned. */ public static Object[] getTopmostCells(mxIGraphModel model, Object[] cells) { Set hash = new HashSet(); hash.addAll(Arrays.asList(cells)); List result = new ArrayList(cells.length); for (int i = 0; i < cells.length; i++) { Object cell = cells[i]; boolean topmost = true; Object parent = model.getParent(cell); while (parent != null) { if (hash.contains(parent)) { topmost = false; break; } parent = model.getParent(parent); } if (topmost) { result.add(cell); } } return result.toArray(); } // // Visitor patterns // /** * */ public static interface Filter { /** * */ boolean filter(Object cell); } // // Atomic changes // public static class mxRootChange extends mxAtomicGraphModelChange { /** * Holds the new and previous root cell. */ protected Object root, previous; /** * */ public mxRootChange(mxGraphModel model, Object root) { super(model); this.root = root; previous = root; } /** * @return the root */ public Object getRoot() { return root; } /** * @return the previous */ public Object getPrevious() { return previous; } /** * Changes the root of the model. */ public void execute() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -