/** * Returns the edges between two specified ports or two specified vertices. * If directed is true then <code>cell1</code> must be the source of the * returned edges. This method never returns null. If there are no edges * between the specified cells, then an array of length 0 is returned. */ public static Object[] getEdgesBetween(GraphModel model, Object cell1, Object cell2, boolean directed) { boolean isPort1 = model.isPort(cell1); boolean isPort2 = model.isPort(cell2); ArrayList result = new ArrayList(); Set edges = DefaultGraphModel.getEdges(model, new Object[] { cell1 }); Iterator it = edges.iterator(); while (it.hasNext()) { Object edge = it.next(); // TODO: Handle edge groups Object source = (isPort1) ? model.getSource(edge) : getSourceVertex(model, edge); Object target = (isPort2) ? model.getTarget(edge) : getTargetVertex(model, edge); if ((source == cell1 && target == cell2) || (!directed && source == cell2 && target == cell1)) result.add(edge); } return result.toArray(); }
/** * @return Returns the opposite port or vertex in <code>edge</code>. */ public static Object getOpposite(GraphModel model, Object edge, Object cell) { boolean isPort = model.isPort(cell); Object source = (isPort) ? model.getSource(edge) : getSourceVertex( model, edge); if (cell == source) return (isPort) ? model.getTarget(edge) : getTargetVertex(model, edge); else return source; }
/** * Returns the vertex that is connected to the target end of the specified * edge * * @param edge * the reference edge * @return any vertex connected as the target the specified edge */ public Object getTarget(Object edge) { Object cell = null; cell = DefaultGraphModel.getTargetVertex(model, edge); if (cell != null && !isVertex(cell)) { // Check to see if the edge has been promoted if (edgePromotion) { while (model.getParent(cell) != null && !isVertex(cell)) { cell = model.getParent(cell); } } else { return null; } if (isVertex(cell)) { return cell; } else { return null; } } return cell; }
Object edge = it.next(); Object source = DefaultGraphModel.getSourceVertex(model, edge); Object target = DefaultGraphModel.getTargetVertex(model, edge); JGraphUnionFind.Node setA = uf.find(uf.getNode(source)); JGraphUnionFind.Node setB = uf.find(uf.getNode(target));
/** * Returns a union find structure representing the connection components of * G=(E,V). * * @param model * the model that describes the graph * @param v * the vertices of the graph * @param e * the edges of the graph * * @return Returns the connection components in G=(E,V) * * @see #createUnionFind(Object[]) */ public JGraphUnionFind getConnectionComponents(GraphModel model, Object[] v, Object[] e) { JGraphUnionFind uf = createUnionFind(v); for (int i = 0; i < e.length; i++) { Object source = DefaultGraphModel.getSourceVertex(model, e[i]); Object target = DefaultGraphModel.getTargetVertex(model, e[i]); uf.union(uf.find(uf.getNode(source)), uf.find(uf.getNode(target))); } return uf; }
/** * Hook for subclassers to return the port to be used for edges that have * been connected to the group. This is called from expand. This returns the * first port of the first or last vertex depending on <code>source</code>. */ protected Object getChildPort(Object edge, boolean source) { GraphModel model = getModel(); // Contains the parent of the port, eg. the group Object parent = (source) ? DefaultGraphModel.getSourceVertex(model, edge) : DefaultGraphModel.getTargetVertex(model, edge); // Finds a vertex in the group int c = model.getChildCount(parent); for (int i = (source) ? c - 1 : 0; i < c && i >= 0; i += (source) ? -1 : +1) { Object child = model.getChild(parent, i); if (!model.isEdge(child) && !model.isPort(child)) { // Finds a port in the vertex for (int j = 0; j < model.getChildCount(child); j++) { Object port = model.getChild(child, j); if (model.isPort(port)) { return port; } } } } return null; }
/** * Hook for subclassers to return the first or last visible port to replace * the current source or target port of the edge. This is called when groups * are collapsed for the edges that cross the group, ie. go from a child * cell to a cell which is outside the group. This implementation returns * the first port of the parent group if source is true, otherwise it * returns the last port of the parent group. */ protected Object getParentPort(Object edge, boolean source) { // Contains the parent of the parent vertex, eg. the group Object parent = getModel().getParent( (source) ? DefaultGraphModel.getSourceVertex(getModel(), edge) : DefaultGraphModel.getTargetVertex(getModel(), edge)); // Finds a port in the group int c = getModel().getChildCount(parent); for (int i = (source) ? c - 1 : 0; i < getModel().getChildCount(parent) && i >= 0; i += (source) ? -1 : +1) { Object child = getModel().getChild(parent, i); if (getModel().isPort(child)) { return child; } } return null; }