/** Populate the stored JGraph 5 facade from the mxGraph data model. */ public void syncJGraphFacade() { if (facade == null || graph == null) { return; } mxIGraphModel model = graph.getModel(); if (vertices != null) { Set<Object> vertexKeys = vertices.keySet(); for (Object vertex : vertexKeys) { Object mxVertex = vertices.get(vertex); mxGeometry geo = model.getGeometry(mxVertex); facade.setLocation(vertex, geo.getX(), geo.getY()); } } if (edges != null) { Set<Object> edgeKeys = edges.keySet(); for (Object edge : edgeKeys) { Object mxEdge = edges.get(edge); List<mxPoint> points = model.getGeometry(mxEdge).getPoints(); if (points != null) { facade.setIntermediatePoints(edge, points); } } } }
/** * Default Constructor * * @param parentModel the parent model * @param source the source * @param target the target * @param link the link * @param containsSource is the source in selection */ public Broken( mxGraphModel parentModel, BasicPort source, BasicPort target, BasicLink link, boolean containsSource) { super(); this.parentModel = parentModel; this.source = source; this.target = target; this.parentLink = link; this.containsSource = containsSource; final BasicPort terminal; if (containsSource) { terminal = target; } else { terminal = source; } /* * Update position */ final mxGeometry pos = parentModel.getGeometry(terminal); final mxGeometry parent = parentModel.getGeometry(parentModel.getParent(terminal)); if (pos != null && parent != null) { this.x = pos.getX() + parent.getX() + (pos.getWidth() / 2) - (parent.getWidth() / 2); this.y = pos.getY() + parent.getY() + (pos.getHeight() / 2) - (parent.getHeight() / 2); } else { this.x = 0.0; this.y = 0.0; } }
/** * Inserts a given Relation in a graph * * @param graph the graph as target for insertion * @param relation the relation that should be inserted * @param verticalOffset the vertical offset of the relation * @return the mxCell representing the Relation */ private Object insertRelation(mxGraph graph, RelationSchema relation, int verticalOffset) { int attributeOffset; int horizontalOffset = 0; ArrayList<mxCell> attributeCells = new ArrayList<>(); mxCell relationVertex = (mxCell) graph.insertVertex( parentPane, relation.getName(), relation, horizontalOffset, verticalOffset, 15 * relation.getName().length(), 25, "RELATION_HEADER"); attributeOffset = (int) (relationVertex.getGeometry().getY() + 25); // Add attributes for (Attribute attr : relation.getAttributes()) { attributeCells.add( (mxCell) graph.insertVertex( parentPane, attr.getName(), attr, horizontalOffset, attributeOffset, 30, 25, super.getAttributeStyle(attr, getImageSizeClass(attr)))); graph.updateCellSize(attributeCells.get(attributeCells.size() - 1)); horizontalOffset += 30; } double currentXPos = 0; for (int i = 0; i < attributeCells.size(); i++) { mxGeometry geo = attributeCells.get(i).getGeometry(); if (i > 0) { currentXPos += attributeCells.get(i - 1).getGeometry().getWidth(); geo.setX(currentXPos); } geo.setHeight(25); geo.setWidth(geo.getWidth() + 5); } drawFunctionalDependencies( relation.getAttributes(), attributeCells, relation.getFunctionalDependencies()); return relationVertex; }
/** * Set the geometry of the block * * @param geometry change split block geometry */ @Override public void setGeometry(mxGeometry geometry) { if (geometry != null) { geometry.setWidth(DEFAULT_SIZE); geometry.setHeight(DEFAULT_SIZE); /* * Align the geometry on the grid */ if (getParentDiagram() != null && getParentDiagram().isGridEnabled()) { final double cx = getParentDiagram().snap(geometry.getCenterX()); final double cy = getParentDiagram().snap(geometry.getCenterY()); geometry.setX(cx - (DEFAULT_SIZE / 2)); geometry.setY(cy - (DEFAULT_SIZE / 2)); } } super.setGeometry(geometry); }
public Port() { super("Hello, World!"); mxGraph graph = new mxGraph() { // Ports are not used as terminals for edges, they are // only used to compute the graphical connection point public boolean isPort(Object cell) { mxGeometry geo = getCellGeometry(cell); return (geo != null) ? geo.isRelative() : false; } // Implements a tooltip that shows the actual // source and target of an edge public String getToolTipForCell(Object cell) { if (model.isEdge(cell)) { return convertValueToString(model.getTerminal(cell, true)) + " -> " + convertValueToString(model.getTerminal(cell, false)); } return super.getToolTipForCell(cell); } // Removes the folding icon and disables any folding public boolean isCellFoldable(Object cell, boolean collapse) { return false; } }; // Sets the default edge style Map<String, Object> style = graph.getStylesheet().getDefaultEdgeStyle(); style.put(mxConstants.STYLE_EDGE, mxEdgeStyle.ElbowConnector); Object parent = graph.getDefaultParent(); graph.getModel().beginUpdate(); try { mxCell v1 = (mxCell) graph.insertVertex(parent, null, "Hello", 20, 20, 100, 100, ""); v1.setConnectable(false); mxGeometry geo = graph.getModel().getGeometry(v1); // The size of the rectangle when the minus sign is clicked geo.setAlternateBounds(new mxRectangle(20, 20, 100, 50)); mxGeometry geo1 = new mxGeometry(0, 0.5, PORT_DIAMETER, PORT_DIAMETER); // Because the origin is at upper left corner, need to translate to // position the center of port correctly geo1.setOffset(new mxPoint(-PORT_RADIUS, -PORT_RADIUS)); geo1.setRelative(true); mxCell port1 = new mxCell(null, geo1, "shape=ellipse;perimter=ellipsePerimeter"); port1.setVertex(true); mxGeometry geo2 = new mxGeometry(1.0, 0.5, PORT_DIAMETER, PORT_DIAMETER); geo2.setOffset(new mxPoint(-PORT_RADIUS, -PORT_RADIUS)); geo2.setRelative(true); mxCell port2 = new mxCell(null, geo2, "shape=ellipse;perimter=ellipsePerimeter"); port2.setVertex(true); graph.addCell(port1, v1); graph.addCell(port2, v1); Object v2 = graph.insertVertex(parent, null, "World!", 240, 150, 80, 30); graph.insertEdge(parent, null, "Edge", port2, v2); } finally { graph.getModel().endUpdate(); } mxGraphComponent graphComponent = new mxGraphComponent(graph); getContentPane().add(graphComponent); graphComponent.setToolTips(true); }
/** * Implements <mxGraphLayout.execute>. * * <p>If the parent has any connected edges, then it is used as the root of the tree. Else, * <mxGraph.findTreeRoots> will be used to find a suitable root node within the set of children of * the given parent. */ public void execute(Object parent, Object root) { mxIGraphModel model = graph.getModel(); if (root == null) { // Takes the parent as the root if it has outgoing edges if (graph.getEdges(parent, model.getParent(parent), invert, !invert, false).length > 0) { root = parent; } // Tries to find a suitable root in the parent's // children else { List<Object> roots = graph.findTreeRoots(parent, true, invert); if (roots != null && roots.size() > 0) { for (int i = 0; i < roots.size(); i++) { if (!isVertexIgnored(roots.get(i)) && graph.getEdges(roots.get(i), null, invert, !invert, false).length > 0) { root = roots.get(i); break; } } } } } if (root != null) { parent = model.getParent(root); model.beginUpdate(); try { TreeNode node = dfs(root, parent, null); if (node != null) { layout(node); double x0 = graph.getGridSize(); double y0 = x0; if (!moveTree || model.getParent(parent) == model.getRoot()) { mxGeometry g = model.getGeometry(root); if (g != null) { x0 = g.getX(); y0 = g.getY(); } } mxRectangle bounds = null; if (horizontal) { bounds = horizontalLayout(node, x0, y0, null); } else { bounds = verticalLayout(node, null, x0, y0, null); } if (bounds != null) { double dx = 0; double dy = 0; if (bounds.getX() < 0) { dx = Math.abs(x0 - bounds.getX()); } if (bounds.getY() < 0) { dy = Math.abs(y0 - bounds.getY()); } if (parent != null) { mxRectangle size = graph.getStartSize(parent); dx += size.getWidth(); dy += size.getHeight(); // Resize parent swimlane if (resizeParent && !graph.isCellCollapsed(parent)) { mxGeometry g = model.getGeometry(parent); if (g != null) { double width = bounds.getWidth() + size.getWidth() - bounds.getX() + 2 * x0; double height = bounds.getHeight() + size.getHeight() - bounds.getY() + 2 * y0; g = (mxGeometry) g.clone(); if (g.getWidth() > width) { dx += (g.getWidth() - width) / 2; } else { g.setWidth(width); } if (g.getHeight() > height) { if (horizontal) { dy += (g.getHeight() - height) / 2; } } else { g.setHeight(height); } model.setGeometry(parent, g); } } } moveNode(node, dx, dy); } } } finally { model.endUpdate(); } } }