📄 mxconnectionhandler.java
字号:
{ newTarget = createTargetVertex(e, source); target = newTarget; } if (target != null || graph.isAllowDanglingEdges()) { model.beginUpdate(); try { Object dropTarget = graph.getDropTarget( new Object[] { newTarget }, e.getPoint(), graphComponent.getCellAt(e.getX(), e.getY())); if (newTarget != null) { // Disables edges as drop targets if the target cell was created if (dropTarget == null || !graph.getModel().isEdge(dropTarget)) { mxCellState pstate = graph.getView().getState( dropTarget); if (pstate != null) { mxGeometry geo = model.getGeometry(newTarget); mxPoint origin = pstate.getOrigin(); geo.setX(geo.getX() - origin.getX()); geo.setY(geo.getY() - origin.getY()); } } else { dropTarget = graph.getDefaultParent(); } graph.addCells(new Object[] { newTarget }, dropTarget); } Object parent = graph.getDefaultParent(); if (model.getParent(source) == model.getParent(target)) { parent = model.getParent(source); } edge = insertEdge(parent, null, "", source, target); if (edge != null) { // Makes sure the edge has a non-null, relative geometry mxGeometry geo = model.getGeometry(edge); if (geo == null) { geo = new mxGeometry(); geo.setRelative(true); model.setGeometry(edge, geo); } if (target == null) { mxPoint pt = graphComponent.getPointForEvent(e); geo.setTerminalPoint(pt, false); } } } finally { model.endUpdate(); } } if (select) { graph.setSelectionCell(edge); } } /** * Creates, inserts and returns a new edge using mxGraph.insertEdge. */ protected Object insertEdge(Object parent, String id, Object value, Object source, Object target) { return graphComponent.getGraph().insertEdge(parent, id, value, source, target); } /** * */ public Object createTargetVertex(MouseEvent e, Object source) { mxGraph graph = graphComponent.getGraph(); Object clone = graph.cloneCells(new Object[] { source })[0]; mxIGraphModel model = graph.getModel(); mxGeometry geo = model.getGeometry(clone); if (geo != null) { mxPoint point = graphComponent.getPointForEvent(e); geo.setX(graph.snap(point.getX() - geo.getWidth() / 2)); geo.setY(graph.snap(point.getY() - geo.getHeight() / 2)); } return clone; } /** * */ public boolean isValidSource(Object cell) { return graphComponent.getGraph().isValidSource(cell); } /** * Returns true. The call to mxGraph.isValidTarget is implicit by calling * mxGraph.getEdgeValidationError in validateConnection. This is an * additional hook for disabling certain targets in this specific handler. */ public boolean isValidTarget(Object cell) { return true; } /** * Returns the error message or an empty string if the connection for the * given source target pair is not valid. Otherwise it returns null. */ public String validateConnection(Object source, Object target) { if (target == null && createTarget) { return null; } if (!isValidTarget(target)) { return ""; } return graphComponent.getGraph().getEdgeValidationError(null, source, target); } /** * */ public void mousePressed(MouseEvent e) { if (!graphComponent.isForceMarqueeEvent(e) && !graphComponent.isPanningEvent(e)) { // Subhandles in the graph handler have precedence over this // if keepOnTop is false, otherwise this has precendence if (!keepOnTop) { graphComponent.getGraphHandler().dispatchMousePressed(e); } if (!e.isConsumed() && source != null && !e.isPopupTrigger()) { if ((isHighlighting() && marker.hasValidState()) || (!isHighlighting() && getBounds().contains( e.getPoint()))) { start = e.getPoint(); preview.setOpaque(false); preview.setVisible(false); graphComponent.getGraphControl().add(preview, 0); getParent().setComponentZOrder(this, 0); graphComponent.getGraphControl().setCursor(DEFAULT_CURSOR); marker.reset(); e.consume(); } } if (keepOnTop) { graphComponent.getGraphHandler().dispatchMousePressed(e); } } } /** * */ public void mouseDragged(MouseEvent e) { if (!e.isConsumed() && source != null && start != null) { int dx = e.getX() - start.x; int dy = e.getY() - start.y; if (!preview.isVisible() && graphComponent.isSignificant(dx, dy)) { preview.setVisible(true); marker.reset(); } current = e.getPoint(); mxGraph graph = graphComponent.getGraph(); mxGraphView view = graph.getView(); double scale = view.getScale(); mxPoint trans = view.getTranslate(); current.x = (int) Math.round((graph.snap(current.x / scale - trans.getX()) + trans.getX()) * scale); current.y = (int) Math.round((graph.snap(current.y / scale - trans.getY()) + trans.getY()) * scale); marker.process(e); // Checks if a color was used to highlight the state mxCellState state = marker.getValidState(); if (state != null) { current.x = (int) state.getCenterX(); current.y = (int) state.getCenterY(); // Computes the target perimeter point mxPerimeterFunction targetPerimeter = view .getPerimeterFunction(state); if (targetPerimeter != null) { mxPoint next = new mxPoint(source.getCenterX(), source .getCenterY()); mxPoint tmp = targetPerimeter.apply(view .getPerimeterBounds(state, null, false), null, state, false, next); if (tmp != null) { current = tmp.getPoint(); } } } // Computes the source perimeter point mxPerimeterFunction sourcePerimeter = view .getPerimeterFunction(source); if (sourcePerimeter != null) { mxPoint pt = sourcePerimeter.apply(view.getPerimeterBounds( source, null, true), null, source, true, new mxPoint( current)); if (pt != null) { start = pt.getPoint(); } } else { start = new Point((int) Math.round(source.getCenterX()), (int) Math.round(source.getCenterY())); } // Hides the connect icon or handle setVisible(false); // Updates the bounds of the previewed line Rectangle bounds = new Rectangle(current); bounds.add(start); bounds.grow(1, 1); preview.setBounds(bounds); e.consume(); } } /** * */ public void mouseReleased(MouseEvent e) { if (!e.isConsumed() && isConnecting()) { // Does not connect if there is an error if (error != null) { if (error.length() > 0) { JOptionPane.showMessageDialog(graphComponent, error); } } else if (source != null) { Object src = source.getCell(); Object trg = (marker.hasValidState()) ? marker.getValidState() .getCell() : null; connect(src, trg, e); } e.consume(); } else if (source != null && !e.isPopupTrigger()) { graphComponent.selectCellForEvent(source.getCell(), e); } reset(); } /** * */ public void mouseMoved(MouseEvent e) { if (!e.isConsumed() && graphComponent.isEnabled() && isEnabled()) { source = marker.process(e); if (isHighlighting() && !marker.hasValidState()) { source = null; } if (source != null) { if (isHighlighting()) { // Displays the connect icon on the complete highlighted area setBounds(source.getRectangle()); } else { int imgWidth = handleSize; int imgHeight = handleSize; if (connectIcon != null) { imgWidth = connectIcon.getIconWidth(); imgHeight = connectIcon.getIconHeight(); } int x = (int) source.getCenterX() - imgWidth / 2; int y = (int) source.getCenterY() - imgHeight / 2; if (graphComponent.getGraph().isSwimlane(source.getCell())) { mxRectangle size = graphComponent.getGraph() .getStartSize(source.getCell()); if (size.getWidth() > 0) { x = (int) (source.getX() + size.getWidth() / 2 - imgWidth / 2); } else { y = (int) (source.getY() + size.getHeight() / 2 - imgHeight / 2); } } setBounds(x, y, imgWidth, imgHeight); } if (keepOnTop) { getParent().setComponentZOrder(this, 0); } } setVisible(source != null); } } /** * */ public void paint(Graphics g) { if (start == null) { if (connectIcon != null) { g.drawImage(connectIcon.getImage(), 0, 0, getWidth(), getHeight(), this); } else if (handleEnabled) { g.setColor(Color.BLACK); g.draw3DRect(0, 0, getWidth() - 1, getHeight() - 1, true); g.setColor(Color.GREEN); g.fill3DRect(1, 1, getWidth() - 2, getHeight() - 2, true); g.setColor(Color.BLUE); g.drawRect(getWidth() / 2 - 1, getHeight() / 2 - 1, 1, 1); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -