📄 mxedgehandler.java
字号:
/** * $Id: mxEdgeHandler.java,v 1.12 2009/04/02 13:41:43 gaudenz Exp $ * Copyright (c) 2008, Gaudenz Alder */package com.mxgraph.swing.handler;import java.awt.Color;import java.awt.Cursor;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Point;import java.awt.Rectangle;import java.awt.Stroke;import java.awt.event.MouseEvent;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import javax.swing.JComponent;import javax.swing.JOptionPane;import javax.swing.JPanel;import com.mxgraph.model.mxGeometry;import com.mxgraph.model.mxIGraphModel;import com.mxgraph.swing.mxGraphComponent;import com.mxgraph.util.mxConstants;import com.mxgraph.util.mxPoint;import com.mxgraph.view.mxCellState;import com.mxgraph.view.mxGraph;/** * */public class mxEdgeHandler extends mxCellHandler{ /** * */ protected boolean cloneEnabled = true; /** * */ protected Point[] p; /** * */ protected transient String error; /** * Workaround for alt-key-state not correct in mouseReleased. */ protected transient boolean gridEnabledEvent = false; /** * Workaround for shift-key-state not correct in mouseReleased. */ protected transient boolean constrainedEvent = false; /** * */ protected mxCellMarker marker = new mxCellMarker(graphComponent) { // Only returns edges if they are connectable and never returns // the edge that is currently being modified protected Object getCell(MouseEvent e) { mxGraph graph = graphComponent.getGraph(); mxIGraphModel model = graph.getModel(); Object cell = super.getCell(e); if (cell == mxEdgeHandler.this.state.getCell() || (!graph.isConnectableEdges() && model.isEdge(cell))) { cell = null; } return cell; } // Sets the highlight color according to isValidConnection protected boolean isValidState(mxCellState state) { mxIGraphModel model = graphComponent.getGraph().getModel(); Object edge = mxEdgeHandler.this.state.getCell(); boolean isSource = isSource(index); Object other = model.getTerminal(edge, !isSource); Object source = (isSource) ? state.getCell() : other; Object target = (isSource) ? other : state.getCell(); error = validateConnection(source, target); return error == null; } }; /** * * @param graphComponent * @param state */ public mxEdgeHandler(mxGraphComponent graphComponent, mxCellState state) { super(graphComponent, state); } /** * */ public void setCloneEnabled(boolean cloneEnabled) { this.cloneEnabled = cloneEnabled; } /** * */ public boolean isCloneEnabled() { return cloneEnabled; } /** * No flip event is ignored. */ protected boolean isIgnoredEvent(MouseEvent e) { return !isFlipEvent(e) && super.isIgnoredEvent(e); } /** * */ protected boolean isFlipEvent(MouseEvent e) { return false; } /** * 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) { return graphComponent.getGraph().getEdgeValidationError( state.getCell(), source, target); } /** * Returns true if the current index is 0. */ public boolean isSource(int index) { return index == 0; } /** * Returns true if the current index is the last index. */ public boolean isTarget(int index) { return index == getHandleCount() - 2; } /** * */ protected Rectangle[] createHandles() { p = createPoints(state); Rectangle[] h = new Rectangle[p.length + 1]; for (int i = 0; i < h.length - 1; i++) { h[i] = createHandle(p[i]); } h[p.length] = createHandle(state.getAbsoluteOffset().getPoint(), mxConstants.HANDLE_SIZE - 2); return h; } /** * */ protected Color getHandleFillColor(int index) { boolean source = isSource(index); if (source || isTarget(index)) { mxGraph graph = graphComponent.getGraph(); Object terminal = graph.getModel().getTerminal(state.getCell(), source); if (terminal != null) { return (graphComponent.getGraph().isCellDisconnectable(state .getCell(), terminal, source)) ? mxConstants.CONNECT_HANDLE_FILLCOLOR : mxConstants.LOCKED_HANDLE_FILLCOLOR; } } return super.getHandleFillColor(index); } /** * * @param x * @param y * @return Returns the inde of the handle at the given location. */ protected int getIndexAt(int x, int y) { int index = super.getIndexAt(x, y); // Makes the complete label are a trigger for moving the label if (index < 0 && handles != null && handlesVisible && isLabelMovable() && state.getLabelBounds().getRectangle().contains(x, y)) { index = handles.length - 1; } return index; } /** * */ protected Rectangle createHandle(Point center) { return createHandle(center, mxConstants.HANDLE_SIZE); } /** * */ protected Rectangle createHandle(Point center, int size) { return new Rectangle(center.x - size / 2, center.y - size / 2, size, size); } /** * */ protected Point[] createPoints(mxCellState s) { Point[] pts = new Point[s.getAbsolutePointCount()]; for (int i = 0; i < pts.length; i++) { pts[i] = s.getAbsolutePoint(i).getPoint(); } return pts; } /** * */ protected JComponent createPreview() { JPanel preview = new JPanel() { public void paint(Graphics g) { super.paint(g); if (!isLabel(index) && p != null) { ((Graphics2D) g).setStroke(mxConstants.PREVIEW_STROKE); if (isSource(index) || isTarget(index)) { if (marker.hasValidState() || graphComponent.getGraph() .isAllowDanglingEdges()) { g.setColor(mxConstants.DEFAULT_VALID_COLOR); } else { g.setColor(mxConstants.DEFAULT_INVALID_COLOR); } } else { g.setColor(Color.BLACK); } Point origin = getLocation(); Point last = p[0]; for (int i = 1; i < p.length; i++) { g.drawLine(last.x - origin.x, last.y - origin.y, p[i].x - origin.x, p[i].y - origin.y); last = p[i]; } } } }; if (isLabel(index)) { preview.setBorder(mxConstants.PREVIEW_BORDER); } preview.setOpaque(false); preview.setVisible(false); return preview; } /** * * @param point * @param gridEnabled * @return Returns the scaled, translated and grid-aligned point. */ protected mxPoint convertPoint(mxPoint point, boolean gridEnabled) { mxGraph graph = graphComponent.getGraph(); double scale = graph.getView().getScale(); mxPoint trans = graph.getView().getTranslate(); double x = point.getX() / scale - trans.getX(); double y = point.getY() / scale - trans.getY(); if (gridEnabled) { x = graph.snap(x); y = graph.snap(y); } point.setX(x - state.getOrigin().getX()); point.setY(y - state.getOrigin().getY()); return point; } /** * * @return Returns the bounds of the preview. */ protected Rectangle getPreviewBounds() { Rectangle bounds = null; if (isLabel(index)) { bounds = state.getLabelBounds().getRectangle(); } else { bounds = new Rectangle(p[0]); for (int i = 0; i < p.length; i++) { bounds.add(p[i]); } bounds.height += 1; bounds.width += 1; } return bounds; } /** * */ public void mousePressed(MouseEvent e) { super.mousePressed(e); if (isSource(index) || isTarget(index)) { mxGraph graph = graphComponent.getGraph(); mxIGraphModel model = graph.getModel(); Object terminal = model.getTerminal(state.getCell(), isSource(index));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -