private Object getPort(Object node, Object edge) { if (!model.isPort(node)) { for (int index = 0; index < model.getChildCount(node); index++) { Object result = getPort(model.getChild(node, index), edge); if (result != null) { return result; } } return null; } Iterator edges = model.edges(node); while (edges.hasNext()) { if (edge.equals(edges.next())) { return node; } } return null; }
/** * Helper methods that connects the source of <code>edge</code> to * <code>port</code> in <code>model</model>. */ public static void setTargetPort(GraphModel model, Object edge, Object port) { model.edit(null, new ConnectionSet(edge, port, false), null, null); }
/** * Returns the incoming or outgoing edges for cell. Cell should be a port or * a vertex. */ public static Object[] getEdges(GraphModel model, Object cell, boolean incoming) { Set edges = DefaultGraphModel.getEdges(model, new Object[] { cell }); // Base initial capacity on size of set, it can't be any larger ArrayList result = new ArrayList(edges.size()); Iterator it = edges.iterator(); while (it.hasNext()) { Object edge = it.next(); // TODO: Handle edge groups Object port = (incoming) ? model.getTarget(edge) : model .getSource(edge); Object parent = model.getParent(port); if (port == cell || parent == cell) result.add(edge); } return result.toArray(); }
private static boolean detectEdgeIntersections(Rectangle rect1, JGraph graph, boolean intersectsline, Object port) { Iterator edges = graph.getModel().edges(port); while (edges.hasNext()){ Object edge=edges.next(); Object targetport = graph.getModel().getTarget(edge); Object sourceport = graph.getModel().getSource(edge); AttributeMap attargetport = graph.getModel().getAttributes(graph.getModel().getParent(targetport)); AttributeMap atsourceport = graph.getModel().getAttributes(graph.getModel().getParent(sourceport)); Rectangle b1 = GraphConstants.getBounds(attargetport).getBounds(); Rectangle b2 = GraphConstants.getBounds(atsourceport).getBounds(); Float line = new Line2D.Float(new Point((int)b1.getCenterX(),(int)b1.getCenterY()),new Point((int)b2.getCenterX(),(int)b2.getCenterY())); intersectsline=intersectsline||line.intersects(rect1); if (!(graph.getModel().getParent(targetport) instanceof NAryEdge) && !ListenerContainer.isContainer((DefaultGraphCell) graph.getModel().getParent(targetport), graph)){ intersectsline=intersectsline || b1.intersects(rect1); // System.err.println("overlapping1 with "+targetport+","+graph.getModel().getParent(targetport)+" "+rect1+":"+b1); } else if (!(graph.getModel().getParent(sourceport) instanceof NAryEdge) && !ListenerContainer.isContainer((DefaultGraphCell) graph.getModel().getParent(sourceport), graph)){ intersectsline=intersectsline || b2.intersects(rect1); // System.err.println("overlapping2 with "+sourceport+","+graph.getModel().getParent(sourceport)+" "+rect1+":"+b2); } } return intersectsline; }
/** * Returns the roots of the specified model as an array. This implementation * uses the GraphModel interface in the general case, but if the model is a * <code>DefaultGraphModel</code> the performance can be improved to * linear time. */ public static Object[] getRoots(GraphModel model) { Object[] cells = null; if (model != null) { // If model is DefaultGraphModel, we can do a linear time getRoots if (model instanceof DefaultGraphModel) { cells = ((DefaultGraphModel) model).getRoots().toArray(); } else { cells = new Object[model.getRootCount()]; for (int i = 0; i < cells.length; i++) { cells[i] = model.getRootAt(i); } } } return cells; }
public static Port[] getPorts(Object[] vertexList, ModelJGraph graph) { // Ports of argument vertexs. Port[] ports = new Port[vertexList.length]; // Obtain the model. GraphModel model = graph.getModel(); // Iterate over all Objects. for (int i = 0; i < vertexList.length; i++) { Port objectPort = null; // Iterate over all Children for (int j = 0; j < model.getChildCount(vertexList[i]); j++) { // Fetch the Child of Vertex at Index i Object child = model.getChild(vertexList[i], j); // Check if Child is a Port if (child instanceof Port) { // Return the Child as a Port objectPort = (Port) child; } } ports[i] = objectPort; } return ports; }
/** * 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; }
/** * Returns a connection set that represents the connection or disconnection * of <code>cells</code> in <code>model</code> based on * <code>disconnect</code>. */ public static ConnectionSet create(GraphModel m, Object[] cells, boolean disconnect) { ConnectionSet cs = new ConnectionSet(); for (int i = 0; i < cells.length; i++) { Object cell = cells[i]; // Edge if (m.isEdge(cell)) { if (disconnect) cs.disconnect(cell); else cs.connect(cell, m.getSource(cell), m.getTarget(cell)); } // Port Iterator it = m.edges(cell); while (it.hasNext()) { // Edge At Port Object edge = it.next(); if (m.getSource(edge) == cell) connect(cs, edge, cell, true, disconnect); else if (m.getTarget(edge) == cell) connect(cs, edge, cell, false, disconnect); } } return cs; }
/** * Orders cells so that they reflect the model order. */ public static Object[] order(GraphModel model, Object[] cells) { if (cells != null) { Set cellSet = new HashSet(); for (int i = 0; i < cells.length; i++) cellSet.add(cells[i]); Stack stack = new Stack(); for (int i = model.getRootCount() - 1; i >= 0; i--) stack.add(model.getRootAt(i)); LinkedList result = new LinkedList(); while (!stack.isEmpty()) { Object tmp = stack.pop(); for (int i = model.getChildCount(tmp) - 1; i >= 0; i--) stack.add(model.getChild(tmp, i)); if (cellSet.remove(tmp)) result.add(tmp); } return result.toArray(); } return null; }
public void actionPerformed(ActionEvent e) { graph.getListenerContainer().storeContraints(cell); graph.getModel().getAttributes(cell).put("view", "INGENIAS"); ent.getPrefs(graph.getModel().getAttributes(cell)).setView(ViewPreferences.ViewType.INGENIAS); graph.getListenerContainer().restoreContraints(cell); graph.invalidate(); graph.refresh(); } });
public void resizeCurrentDiagram(ActionEvent e) { GraphModel gm=editor.getGraph().getModel(); for (int k=0;k<gm.getRootCount();k++){ DefaultGraphCell dgc = (DefaultGraphCell) gm.getRootAt(k); if (editor.getGraph().getListenerContainer().parentHasVisibleContainers(dgc).isEmpty()){ Object userobject=dgc.getUserObject(); CellView currentview=editor.getGraph().getGraphLayoutCache().getMapping(dgc,false); Entity ent=(Entity)dgc.getUserObject(); Map dgcMap=gm.getAttributes(dgc); if (ent!=null && RenderComponentManager.retrievePanel(ent.getType(),ent.getPrefs(dgcMap).getView())!=null){ Dimension dim=RenderComponentManager.getSize(ent,ent.getType(),ent.getPrefs(dgcMap).getView()); if (dim!=null){ Map attributes=dgc.getAttributes(); Rectangle2D loc=GraphConstants.getBounds(attributes); loc.setRect(loc.getX(),loc.getY(),dim.getWidth(),dim.getHeight()); GraphConstants.setBounds(attributes,loc); //attributes.put("view", ent.getPrefs(null).getView().toString()); Map nmap=new Hashtable(); nmap.put(dgc,attributes); editor.getGraph().getModel().edit(nmap,null,null,null); editor.getGraph().repaint(); } } } } } }
/** * Checks whether the cell has at least one child which is not a port. This * implementation operates on the model, not taking into account visibility * of cells. It returns true for groups regardless of their folded state. * * @param cell * the cell to check for being a group * @return Returns true if the cell contains at least one cell which is not * a port */ public static boolean isGroup(GraphModel model, Object cell) { for (int i = 0; i < model.getChildCount(cell); i++) { if (!model.isPort(model.getChild(cell, i))) return true; } return false; }
/** * @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; }
private static DefaultGraphCell[] getEdgeExtremes(DefaultEdge edge, GraphModel m) { DefaultEdge de = edge; DefaultGraphCell target = (DefaultGraphCell) ( (DefaultPort) m.getTarget(de)). getParent(); DefaultGraphCell source = (DefaultGraphCell) ( (DefaultPort) m.getSource(de)). getParent(); return new DefaultGraphCell[]{target,source}; }
public void actionPerformed(ActionEvent e) { re.hide(); AttributeMap am = graph.getModel().getAttributes(defaultEdge); GraphConstants.setLabelEnabled(am,false); GraphConstants.setLabelPosition(am, new Point2D.Double(GraphConstants.PERMILLE/2, -20)); Hashtable attributes=new Hashtable(); attributes.put(defaultEdge,am); graph.getModel().edit(attributes,null,null,null); // hack to prevent duplicating attributes /*attributes=new Hashtable(); attributes.put(defaultEdge,am); graph.getModel().edit(attributes,null,null,null);*/ SwingUtilities.invokeLater(new Runnable(){ public void run(){ graph.invalidate(); graph.repaint(); resources.getMainFrame().invalidate(); resources.getMainFrame().repaint(); } }); } });
/** * Returns the default portview for the specified cell. The default * implementation returns the first floating port (ie. the first port that * does not define an offset) or <b>the </b> port, if there is only one * port. * * @param cell * the cell whose port is to be obtained * @return the port view of the specified cell */ public PortView getDefaultPortForCell(Object cell) { if (cell != null && !getModel().isEdge(cell)) { int childCount = getModel().getChildCount(cell); for (int i = 0; i < childCount; i++) { Object childCell = getModel().getChild(cell, i); CellView child = getGraphLayoutCache().getMapping(childCell, false); if (child instanceof PortView) { Point2D offset = GraphConstants.getOffset(child .getAllAttributes()); if (offset == null || childCount == 1) return (PortView) child; } } } return null; }
/** * @param model * @param node * @return root node of object */ public static Object getRoot(GraphModel model, Object node) { Object result = node; Object parent = null; while ((parent = model.getParent(result)) != null) { result = parent; } return result; }