mxgraphhandler.java

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

JAVA
1,511
字号
						super.paint(g);					}				}			};			preview.setOpaque(false);			preview.setVisible(false);			preview.setBorder(mxConstants.PREVIEW_BORDER);			graphComponent.getGraphControl().add(preview, 0);		}	}	/**	 * 	 */	protected void updatePreviewBounds()	{		if (preview != null)		{			Rectangle bounds = getBounds();			bounds.grow(-5, -5);			preview.setBounds(bounds);		}	}	/**	 * 	 */	public void updateDragImage(Object[] cells)	{		Image img = mxCellRenderer.createBufferedImage(graphComponent				.getGraph(), cells, graphComponent.getGraph().getView()				.getScale(), null, graphComponent.isAntiAlias(), null,				graphComponent.getCanvas());		if (img != null)		{			dragImage = new ImageIcon(img);			preview					.setSize(dragImage.getIconWidth(), dragImage							.getIconHeight());			preview.getParent().setComponentZOrder(preview, 0);		}	}	/**	 * 	 */	protected void destroyPreview()	{		if (preview != null)		{			preview.setVisible(false);			preview.getParent().remove(preview);			dragImage = null;			preview = null;		}		marker.reset();	}	/**	 * 	 */	public void mouseMoved(MouseEvent e)	{		if (graphComponent.isEnabled() && isEnabled())		{			Iterator it = handlers.values().iterator();			while (it.hasNext() && !e.isConsumed())			{				((mxCellHandler) it.next()).mouseMoved(e);			}			if (!e.isConsumed())			{				Cursor cursor = getCursor(e);								if (e != null)				{					graphComponent.getGraphControl().setCursor(getCursor(e));					e.consume();				}			}		}	}	/**	 * 	 */	protected Cursor getCursor(MouseEvent e)	{		Object cell = graphComponent.getCellAt(e.getX(), e.getY(), false);		Cursor cursor = null;		if (cell != null)		{			if (graphComponent.isFoldingEnabled()					&& graphComponent.hitFoldingIcon(cell, e.getX(), e.getY()))			{				cursor = new Cursor(Cursor.HAND_CURSOR);			}			else if (graphComponent.getGraph().isCellMovable(cell))			{				cursor = MOVE_CURSOR;			}		}		return cursor;	}	/**	 * 	 */	public void dragEnter(DropTargetDragEvent e)	{		JComponent component = getDropTarget(e);		TransferHandler th = component.getTransferHandler();		boolean isLocal = th instanceof mxGraphTransferHandler				&& ((mxGraphTransferHandler) th).isLocalDrag();		if (isLocal)		{			canImport = true;		}		else		{			canImport = graphComponent.isImportEnabled()					&& th.canImport(component, e.getCurrentDataFlavors());		}		if (canImport)		{			e.acceptDrag(TransferHandler.COPY_OR_MOVE);			transferBounds = null;			createPreview();			try			{				Transferable t = e.getTransferable();				if (t.isDataFlavorSupported(mxGraphTransferable.dataFlavor))				{					mxGraphTransferable gt = (mxGraphTransferable) t							.getTransferData(mxGraphTransferable.dataFlavor);					dragCells = gt.getCells();					if (gt.getBounds() != null)					{						mxGraph graph = graphComponent.getGraph();						double scale = graph.getView().getScale();						transferBounds = gt.getBounds();						mxRectangle bounds = new mxRectangle(transferBounds);						bounds.setWidth(bounds.getWidth() * scale);						bounds.setHeight(bounds.getHeight() * scale);						preview.setBounds(bounds.getRectangle());						if (imagePreview)						{							// Does not render fixed cells for local preview							// but ignores movable state for non-local previews							if (isLocal)							{								updateDragImage(graph										.getMovableCells(dragCells));							}							else							{								updateDragImage(graphComponent										.getImportableCells(dragCells));							}						}						preview.setVisible(true);					}				}			}			catch (Exception ex)			{				// do nothing				ex.printStackTrace();			}		}		else		{			e.rejectDrag();		}	}	/**	 * Redirects the tooltip handling of the JComponent to the graph	 * component, which in turn may use getHandleToolTipText in this class to	 * find a tooltip associated with a handle.	 */	public String getToolTipText(MouseEvent e)	{		return graphComponent.getGraphControl().getToolTipText(				SwingUtilities.convertMouseEvent(e.getComponent(), e,						graphComponent.getGraphControl()));	}	/**	 * Redirects a tooltip request from the graph component to the handles.	 * This is called from the graph component in getToolTipText which in turn	 * may be called from getToolTipText in this class.	 */	public String getHandleToolTipText(MouseEvent e)	{		String toolTip = null;		Iterator it = handlers.values().iterator();		while (it.hasNext() && toolTip == null)		{			toolTip = ((mxCellHandler) it.next()).getToolTipText(e);		}		return toolTip;	}	/**	 * Dispatches the mousepressed event to the subhandles. This is	 * called from the connection handler as subhandles have precedence	 * over the connection handler.	 */	public void dispatchMousePressed(MouseEvent e)	{		if (!graphComponent.isForceMarqueeEvent(e))		{			Iterator it = handlers.values().iterator();			while (it.hasNext() && !e.isConsumed())			{				((mxCellHandler) it.next()).mousePressed(e);			}		}	}	/**	 * 	 */	public void mousePressed(MouseEvent e)	{		if (graphComponent.isEnabled() && isEnabled() && !e.isConsumed()				&& !graphComponent.isForceMarqueeEvent(e))		{			cell = graphComponent.getCellAt(e.getX(), e.getY(), false);			initialCell = cell;			if (cell != null)			{				if (!graphComponent.getGraph().isSelected(cell)						&& !graphComponent.isToggleEvent(e))				{					graphComponent.selectCellForEvent(cell, e);					cell = null;				}				// Starts move if the cell under the mouse is movable and/or any				// cells of the selection are movable				if (!e.isPopupTrigger())				{					start(e);					e.consume();				}			}			else if (e.isPopupTrigger())			{				graphComponent.getGraph().clearSelection();			}		}	}	/**	 * 	 */	public void start(MouseEvent e)	{		first = e.getPoint();		createPreview();		updatePreviewBounds();	}	/**	 * 	 */	public void dropActionChanged(DropTargetDragEvent e)	{		// do nothing	}	/**	 * 	 * @param e	 */	public void dragOver(DropTargetDragEvent e)	{		if (canImport)		{			mouseDragged(createEvent(e));			mxGraphTransferHandler handler = getGraphTransferHandler(e);			if (handler != null)			{				mxGraph graph = graphComponent.getGraph();				double scale = graph.getView().getScale();				Point pt = SwingUtilities.convertPoint(graphComponent, e						.getLocation(), graphComponent.getGraphControl());				pt = graphComponent.snapScaledPoint(new mxPoint(pt)).getPoint();				handler.setLocation(new Point(pt));				int dx = 0;				int dy = 0;				// Centers the preview image				if (centerPreview && transferBounds != null)				{					dx -= Math.round((double) transferBounds.getWidth() * scale							/ 2);					dy -= Math.round((double) transferBounds.getHeight()							* scale / 2);				}				// Sets the drop offset so that the location in the transfer				// handler reflects the actual mouse position				handler.setOffset(new Point((int) graph.snap(dx / scale),						(int) graph.snap(dy / scale)));				pt.translate(dx, dy);				// Shifts the preview so that overlapping parts do not				// affect the centering				if (transferBounds != null && dragImage != null)				{					dx = (int) Math							.round((dragImage.getIconWidth() - 2 - transferBounds									.getWidth()									* scale) / 2);					dy = (int) Math							.round((dragImage.getIconHeight() - 2 - transferBounds									.getHeight()									* scale) / 2);					pt.translate(-dx, -dy);				}				if (!handler.isLocalDrag())				{					preview.setLocation(pt.x, pt.y);				}			}		}		else		{			e.rejectDrag();		}	}	/**	 * 	 */	public Point convertPoint(Point pt)	{		pt = SwingUtilities.convertPoint(graphComponent, pt, graphComponent				.getGraphControl());		pt.x -= graphComponent.getHorizontalScrollBar().getValue();		pt.y -= graphComponent.getVerticalScrollBar().getValue();		return pt;	}	/**	 * 	 */	public void mouseDragged(MouseEvent e)	{		// LATER: Check scrollborder, use scroll-increments, do not		// scroll when over ruler dragging from library		graphComponent.getGraphControl().scrollRectToVisible(				new Rectangle(e.getPoint()));		Iterator it = handlers.values().iterator();		while (it.hasNext() && !e.isConsumed())		{			((mxCellHandler) it.next()).mouseDragged(e);		}		if (!e.isConsumed() && preview != null)		{			gridEnabledEvent = graphComponent.isGridEnabledEvent(e);			constrainedEvent = graphComponent.isConstrainedEvent(e);			if (constrainedEvent && first != null)			{				int x = e.getX();				int y = e.getY();				if (Math.abs(e.getX() - first.x) > Math.abs(e.getY() - first.y))				{					y = first.y;				}				else				{					x = first.x;				}				e = new MouseEvent(e.getComponent(), e.getID(), e.getWhen(), e						.getModifiers(), x, y, e.getClickCount(), e						.isPopupTrigger(), e.getButton());			}			if (preview.isVisible())			{				marker.process(e);			}			else if (cell != null					&& !graphComponent.getGraph().isSelected(cell)					&& graphComponent.isToggleEvent(e))			{				graphComponent.selectCellForEvent(cell, e);				cell = null;			}			if (first != null)			{				double dx = e.getX() - first.x;				double dy = e.getY() - first.y;				preview.setLocation(getPreviewLocation(e, gridEnabledEvent));				if (!preview.isVisible()						&& graphComponent.isSignificant(dx, dy))				{					if (imagePreview && dragImage == null							&& !graphComponent.isDragEnabled())					{						updateDragImage(cells);					}					preview.setVisible(true);				}				e.consume();			}		}	}	/**	 * 	 */	protected Point getPreviewLocation(MouseEvent e, boolean gridEnabled)	{		int x = 0;		int y = 0;		if (first != null && previewBounds != null)		{			mxGraph graph = graphComponent.getGraph();			double scale = graph.getView().getScale();			mxPoint trans = graph.getView().getTranslate();			// LATER: 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.			// Only fix is a full repaint of the drag cells at each new mouse location.			double dx = e.getX() - first.x;			double dy = e.getY() - first.y;			double dxg = ((previewBounds.getX() + dx) / scale) - trans.getX();			double dyg = ((previewBounds.getY() + dy) / scale) - trans.getY();			if (gridEnabled)			{				dxg = graph.snap(dxg);				dyg = graph.snap(dyg);			}			x = (int) Math.round((dxg + trans.getX()) * scale)					+ (int) Math.round(previewBbox.getX())					- (int) Math.round(previewBounds.getX());			y = (int) Math.round((dyg + trans.getY()) * scale)					+ (int) Math.round(previewBbox.getY())					- (int) Math.round(previewBounds.getY());		}		return new Point(x, y);	}	/**	 * 	 * @param e	 */	public void dragExit(DropTargetEvent e)	{		mxGraphTransferHandler handler = getGraphTransferHandler(e);		if (handler != null)		{			handler.setLocation(null);

⌨️ 快捷键说明

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