📄 mxconnectionhandler.java
字号:
/** * $Id: mxConnectionHandler.java,v 1.15 2009/03/30 08:47:26 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.event.MouseEvent;import javax.swing.ImageIcon;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.swing.mxGraphComponent.mxGraphControl;import com.mxgraph.swing.util.mxMouseControl;import com.mxgraph.util.mxConstants;import com.mxgraph.util.mxEvent;import com.mxgraph.util.mxEventObject;import com.mxgraph.util.mxPoint;import com.mxgraph.util.mxRectangle;import com.mxgraph.util.mxEventSource.mxIEventListener;import com.mxgraph.view.mxCellState;import com.mxgraph.view.mxGraph;import com.mxgraph.view.mxGraphView;import com.mxgraph.view.mxPerimeter.mxPerimeterFunction;/** * Connection handler creates new connections between cells. This control is used to display the connector * icon, while the preview is used to draw the line. */public class mxConnectionHandler extends mxMouseControl{ /** * */ public static Cursor DEFAULT_CURSOR = new Cursor(Cursor.HAND_CURSOR); /** * */ protected mxGraphComponent graphComponent; /** * Specifies the icon to be used for creating new connections. If this is * specified then it is used instead of the handle. Default is null. */ protected ImageIcon connectIcon = null; /** * Specifies the size of the handle to be used for creating new * connections. Default is mxConstants.CONNECT_HANDLE_SIZE. */ protected int handleSize = mxConstants.CONNECT_HANDLE_SIZE; /** * Specifies if a handle should be used for creating new connections. This * is only used if no connectIcon is specified. If this is false, then the * source cell will be highlighted when the mouse is over the hotspot given * in the marker. Default is mxConstants.CONNECT_HANDLE_ENABLED. */ protected boolean handleEnabled = mxConstants.CONNECT_HANDLE_ENABLED; /** * */ protected boolean select = true; /** * */ protected boolean createTarget = false; /** * Appearance and event handling order wrt subhandles. */ protected boolean keepOnTop = true; /** * */ protected transient Point start, current; /** * */ protected transient mxCellState source; /** * */ protected transient mxCellMarker marker; /** * */ protected transient String error; /** * */ protected transient JPanel preview = new JPanel() { public void paint(Graphics g) { super.paint(g); ((Graphics2D) g).setStroke(mxConstants.PREVIEW_STROKE); if (start != null && current != null) { if (marker.hasValidState() || createTarget || graphComponent.getGraph().isAllowDanglingEdges()) { g.setColor(mxConstants.DEFAULT_VALID_COLOR); } else { g.setColor(mxConstants.DEFAULT_INVALID_COLOR); } g.drawLine(start.x - getX(), start.y - getY(), current.x - getX(), current.y - getY()); } } }; /** * */ protected transient mxIEventListener resetHandler = new mxIEventListener() { public void invoke(Object source, mxEventObject evt) { reset(); } }; /** * * @param graphComponent */ public mxConnectionHandler(mxGraphComponent graphComponent) { this.graphComponent = graphComponent; mxGraphControl graphControl = graphComponent.getGraphControl(); graphControl.add(this, 0); graphControl.addMouseListener(this); graphControl.addMouseMotionListener(this); mxGraphView view = graphComponent.getGraph().getView(); view.addListener(mxEvent.SCALE, resetHandler); view.addListener(mxEvent.TRANSLATE, resetHandler); view.addListener(mxEvent.SCALE_AND_TRANSLATE, resetHandler); mxIGraphModel model = graphComponent.getGraph().getModel(); model.addListener(mxEvent.CHANGE, resetHandler); marker = new mxCellMarker(graphComponent) { // Overrides to return cell at location only if valid (so that // there is no highlight for invalid cells that have no error // message when the mouse is released) protected Object getCell(MouseEvent e) { Object cell = super.getCell(e); if (isConnecting()) { if (source != null) { error = validateConnection(source.getCell(), cell); if (error != null && error.length() == 0) { cell = null; // Enables create target inside groups if (createTarget) { error = null; } } } } else if (!isValidSource(cell)) { cell = null; } return cell; } // Sets the highlight color according to isValidConnection protected boolean isValidState(mxCellState state) { if (isConnecting()) { return error == null; } else { return super.isValidState(state); } } // Overrides to use marker color only in highlight mode or for // target selection protected Color getMarkerColor(MouseEvent e, mxCellState state, boolean isValid) { return (isHighlighting() || isConnecting()) ? super .getMarkerColor(e, state, isValid) : null; } // Overrides to use hotspot only for source selection otherwise // intersects always returns true when over a cell protected boolean intersects(mxCellState state, MouseEvent e) { if (!isHighlighting() || isConnecting()) { return true; } return super.intersects(state, e); } }; marker.setHotspotEnabled(true); setCursor(DEFAULT_CURSOR); } /** * Returns true if the source terminal has been clicked and a new * connection is currently being previewed. */ public boolean isConnecting() { return start != null && preview != null && preview.isVisible(); } /** * Returns true if no connectIcon is specified and handleEnabled is false. */ public boolean isHighlighting() { return connectIcon == null && !handleEnabled; } /** * */ public boolean isKeepOnTop() { return keepOnTop; } /** * */ public void setKeepOnTop(boolean keepOnTop) { this.keepOnTop = keepOnTop; } /** * */ public void setConnectIcon(ImageIcon connectIcon) { this.connectIcon = connectIcon; } /** * */ public ImageIcon getConnecIcon() { return connectIcon; } /** * */ public void setHandleEnabled(boolean handleEnabled) { this.handleEnabled = handleEnabled; } /** * */ public boolean isHandleEnabled() { return handleEnabled; } /** * */ public void setHandleSize(int size) { this.handleSize = size; } /** * */ public int getHandleSize() { return handleSize; } /** * */ public mxCellMarker getMarker() { return marker; } /** * */ public void setMarker(mxCellMarker marker) { this.marker = marker; } /** * */ public void setCreateTarget(boolean createTarget) { this.createTarget = createTarget; } /** * */ public boolean isCreateTarget() { return createTarget; } /** * */ public void setSelect(boolean select) { this.select = select; } /** * */ public boolean isSelect() { return select; } /** * */ public void reset() { if (preview.getParent() != null) { preview.setVisible(false); preview.getParent().remove(preview); } setVisible(false); marker.reset(); source = null; start = null; error = null; } /** * * @param source * @param target * @param e */ protected void connect(Object source, Object target, MouseEvent e) { mxGraph graph = graphComponent.getGraph(); mxIGraphModel model = graph.getModel(); Object newTarget = null; Object edge = null; if (target == null && createTarget)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -