mxgraphhandler.java

来自「经典的java图像处理程序源码」· Java 代码 · 共 1,511 行 · 第 1/3 页

JAVA
1,511
字号
/** * $Id: mxGraphHandler.java,v 1.41 2009/04/28 16:00:19 gaudenz Exp $ * Copyright (c) 2008, Gaudenz Alder *  * Known issue: Drag image size depends on the initial position and may sometimes * not align with the grid when dragging. This is because the rounding of the width * and height at the initial position may be different than that at the current * position as the left and bottom side of the shape must align to the grid lines. */package com.mxgraph.swing.handler;import java.awt.Color;import java.awt.Cursor;import java.awt.Graphics;import java.awt.Image;import java.awt.Point;import java.awt.Rectangle;import java.awt.datatransfer.Transferable;import java.awt.dnd.DnDConstants;import java.awt.dnd.DragGestureEvent;import java.awt.dnd.DragGestureListener;import java.awt.dnd.DragGestureRecognizer;import java.awt.dnd.DragSource;import java.awt.dnd.DragSourceAdapter;import java.awt.dnd.DragSourceDropEvent;import java.awt.dnd.DropTarget;import java.awt.dnd.DropTargetDragEvent;import java.awt.dnd.DropTargetDropEvent;import java.awt.dnd.DropTargetEvent;import java.awt.dnd.DropTargetListener;import java.awt.event.InputEvent;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import java.util.Iterator;import java.util.LinkedHashMap;import java.util.Map;import java.util.TooManyListenersException;import javax.swing.ImageIcon;import javax.swing.JComponent;import javax.swing.JPanel;import javax.swing.SwingUtilities;import javax.swing.TransferHandler;import com.mxgraph.swing.mxGraphComponent;import com.mxgraph.swing.util.mxGraphTransferable;import com.mxgraph.swing.util.mxMouseControl;import com.mxgraph.util.mxCellRenderer;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;public class mxGraphHandler extends mxMouseControl implements		DropTargetListener{	/**	 * 	 */	public static Cursor MOVE_CURSOR = new Cursor(Cursor.MOVE_CURSOR);	/**	 * Defines the default value for maxHandlers. Default is 100.	 */	public static int DEFAULT_MAX_HANDLERS = 100;	/**	 * Reference to the enclosing graph component.	 */	protected mxGraphComponent graphComponent;	/**	 * Specifies if cloning by control-drag is enabled. Default is true.	 */	protected boolean cloneEnabled = true;	/**	 * Specifies if cells may be moved out of their parents.	 */	protected boolean removeCellsFromParent = true;	/**	 * Specifies if an image should be used for preview. Default is true.	 */	protected boolean imagePreview = true;	/**	 * Defines the maximum number of handlers to paint individually.	 * Default is DEFAULT_MAX_HANDLES.	 */	protected int maxHandlers = DEFAULT_MAX_HANDLERS;	/**	 * Specifies if the preview should be centered around the mouse cursor if there	 * was no mouse click to define the offset within the shape (eg. drag from	 * external source). Default is true.	 */	protected boolean centerPreview = true;	/**	 * Specifies if this handler should be painted on top of all other components.	 * Default is true.	 */	protected boolean keepOnTop = true;	/**	 * Maps from cells to handlers in the order of the selection cells.	 */	protected transient Map handlers = new LinkedHashMap();	/**	 * Holds the cells that are being moved by this handler.	 */	protected transient Object[] cells;	/**	 * Holds the component that is being used as a preview.	 */	protected transient JComponent preview;	/**	 * Holds the image that is being used for the preview.	 */	protected transient ImageIcon dragImage;	/**	 * Holds the start location of the mouse gesture.	 */	protected transient Point first;	/**	 * 	 */	protected transient Object cell;	/**	 * 	 */	protected transient Object initialCell;	/**	 * 	 */	protected transient Object[] dragCells;	/**	 * 	 */	protected transient mxCellMarker marker;	/**	 * 	 */	protected transient boolean canImport;	/**	 * Scaled, translated bounds of the selection cells.	 */	protected transient mxRectangle cellBounds;	/**	 * Scaled, translated bounding box of the selection cells.	 */	protected transient mxRectangle bbox;	/**	 * 	 */	protected transient mxRectangle previewBounds;	/**	 * 	 */	protected transient mxRectangle previewBbox;	/**	 * Unscaled, untranslated bounding box of the selection cells.	 */	protected transient mxRectangle transferBounds;	/**	 * Workaround for alt-key-state not correct in mouseReleased. Note: State	 * of the alt-key is not available during drag-and-drop.	 */	private transient boolean gridEnabledEvent = false;	/**	 * Workaround for shift-key-state not correct in mouseReleased.	 */	protected transient boolean constrainedEvent = false;	/**	 * 	 */	protected transient mxIEventListener refreshHandler = new mxIEventListener()	{		public void invoke(Object source, mxEventObject evt)		{			refresh();		}	};	/**	 * 	 * @param graphComponent	 */	public mxGraphHandler(final mxGraphComponent graphComponent)	{		this.graphComponent = graphComponent;		marker = new mxCellMarker(graphComponent, Color.BLUE)		{			/**			 * 			 */			public boolean isEnabled()			{				return graphComponent.getGraph().isDropEnabled();			}			/**			 * 			 */			public Object getCell(MouseEvent e)			{				TransferHandler th = graphComponent.getTransferHandler();				boolean isLocal = th instanceof mxGraphTransferHandler						&& ((mxGraphTransferHandler) th).isLocalDrag();				Object cell = super.getCell(e);				Object[] cells = (isLocal) ? handlers.keySet().toArray()						: dragCells;				mxGraph graph = graphComponent.getGraph();				cell = graph.getDropTarget(cells, e.getPoint(), cell);				boolean clone = graphComponent.isCloneEvent(e) && cloneEnabled;				if (isLocal						&& cell != null						&& cells.length > 0						&& !clone						&& (handlers.keySet().contains(cell) || graph								.getModel().getParent(cells[0]) == cell))				{					cell = null;				}				return cell;			}		};		// Swimlane content area will not be transparent drop targets		marker.setSwimlaneContentEnabled(true);		// Adds component for rendering the handles (preview is separate)		graphComponent.getGraphControl().add(this, 0);		// Listens to all mouse events on the rendering control		graphComponent.getGraphControl().addMouseListener(this);		graphComponent.getGraphControl().addMouseMotionListener(this);		// Redirects events from component to rendering control so		// that the event handling order is maintained if other controls		// such as overlays are added to the component hierarchy and		// consume events before they reach the rendering control		MouseRedirector redirector = new MouseRedirector();		addMouseMotionListener(redirector);		addMouseListener(redirector);		// Refreshes the handles after any changes		graphComponent.getGraph().getSelectionModel().addListener(				mxEvent.CHANGE, refreshHandler);		graphComponent.getGraph().addListener(mxEvent.REPAINT, refreshHandler);		// Refreshes the handles if moveVertexLabels or moveEdgeLabels changes		graphComponent.getGraph().addPropertyChangeListener(				new PropertyChangeListener()				{					/*					 * (non-Javadoc)					 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)					 */					public void propertyChange(PropertyChangeEvent evt)					{						if (evt.getPropertyName().equals("vertexLabelsMovable")								|| evt.getPropertyName().equals(										"edgeLabelsMovable"))						{							refresh();						}					}				});		// Drag target creates preview image		DragGestureListener dragGestureListener = new DragGestureListener()		{			public void dragGestureRecognized(DragGestureEvent e)			{				if (graphComponent.isDragEnabled() && preview != null						&& first != null)				{					final TransferHandler th = graphComponent							.getTransferHandler();					if (th instanceof mxGraphTransferHandler)					{						final mxGraphTransferable t = (mxGraphTransferable) ((mxGraphTransferHandler) th)								.createTransferable(graphComponent);						if (t != null)						{							e.startDrag(null, mxConstants.EMPTY_IMAGE,									new Point(), t, new DragSourceAdapter()									{										/**										 * 										 */										public void dragDropEnd(												DragSourceDropEvent dsde)										{											((mxGraphTransferHandler) th)													.exportDone(															graphComponent,															t,															TransferHandler.NONE);										}									});						}					}				}			}		};		DragSource dragSource = new DragSource();		DragGestureRecognizer dgr = dragSource				.createDefaultDragGestureRecognizer(graphComponent						.getGraphControl(), DnDConstants.ACTION_COPY_OR_MOVE,						dragGestureListener);		// Listens to dropped graph cells		DropTarget dropTarget = graphComponent.getDropTarget();		try		{			if (dropTarget != null)			{				dropTarget.addDropTargetListener(this);			}		}		catch (TooManyListenersException tmle)		{			// should not happen... swing drop target is multicast		}		setVisible(false);	}	/**	 * 	 */	public mxGraphComponent getGraphComponent()	{		return graphComponent;	}	/**	 * Returns the component that is used for drawing the preview.	 */	public JComponent getPreview()	{		return preview;	}	/**	 * 	 */	public boolean isKeepOnTop()	{		return keepOnTop;	}	/**	 * 	 */	public void setKeepOnTop(boolean value)	{		keepOnTop = value;	}	/**	 * 	 */	public boolean isCloneEnabled()	{		return cloneEnabled;	}	/**	 * 	 */	public void setCloneEnabled(boolean value)	{		cloneEnabled = value;	}	/**	 * 	 */	public boolean isRemoveCellsFromParent()	{		return removeCellsFromParent;	}	/**	 * 	 */	public void setRemoveCellsFromParent(boolean value)	{		removeCellsFromParent = value;	}	/**	 * 	 */	public boolean isImagePreview()	{		return imagePreview;	}	/**	 * 	 */	public void setImagePreview(boolean value)	{		imagePreview = value;	}	/**	 * 	 */	public boolean isCenterPreview()	{		return centerPreview;	}	/**	 * 	 */	public void setCenterPreview(boolean value)	{		centerPreview = value;	}	/**	 * 	 */	public int getMaxHandlers()	{		return maxHandlers;	}	/**	 * 	 */	public void setMaxHandlers(int value)	{		maxHandlers = value;	}	/**	 * 	 */	protected void createPreview()	{		if (preview == null)		{			preview = new JPanel()			{				/**				 * Paints the preview.				 */				public void paint(Graphics g)				{					if (dragImage != null)					{						// LATER: Clipping with mxUtils doesnt fix the problem						// of the drawImage being painted over the scrollbars						g.drawImage(dragImage.getImage(), 0, 0, dragImage								.getIconWidth(), dragImage.getIconHeight(),								this);					}					else if (!imagePreview)					{

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?