📄 draggingmanipulator.java
字号:
package net.sourceforge.jpowergraph.manipulator.dragging;
import java.awt.geom.Point2D;
import net.sourceforge.jpowergraph.Edge;
import net.sourceforge.jpowergraph.Node;
import net.sourceforge.jpowergraph.lens.CursorLens;
import net.sourceforge.jpowergraph.manipulator.AbstractManipulator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Point;
/**
* A manipulator enabling the dragging of the graph
*/
public class DraggingManipulator extends AbstractManipulator {
/** The name of this manipulator. */
public static final String NAME="DraggingManipulator";
/** The position of the last mouse event. */
protected Point m_lastPosition;
/** The node that is being dragged. */
protected Node m_draggedNode;
/** The edge that is being dragged. */
protected Edge m_draggedEdge;
/** Point of the original grab. */
protected Point m_grabPoint1;
/** Point of the original grab. */
protected Point m_grabPoint2;
/** The old 'fixed' state of the node. */
protected boolean m_oldFixedState1;
/** The old 'fixed' state of the node. */
protected boolean m_oldFixedState2;
private CursorLens cursorLens;
private int mouseEventKeyMask = SWT.NONE;
private boolean disposed = false;
/**
* Creates an instance of this class.
*/
public DraggingManipulator(CursorLens theDraggingLens) {
this(theDraggingLens, SWT.SHIFT);
}
/**
* Creates an instance of this class.
*
* Dragging the tree can be done by holding down the key specified in
* theMouseKeyAssist and left clicking. The key mask can be one of:
*
* SWT.SHIFT
* SWT.CTRL
* SWT.ALT
* SWT.NONE (for no key mask)
*
* @param theMouseEventKeyMask The Mouse MASK
*/
public DraggingManipulator(CursorLens theCursorLens, int theMouseEventKeyMask) {
this.cursorLens = theCursorLens;
mouseEventKeyMask = theMouseEventKeyMask;
}
public String getName() {
return NAME;
}
public boolean isDragging() {
return m_draggedNode!=null || m_draggedEdge!=null;
}
public Node getDraggedNode() {
return m_draggedNode;
}
public Edge getDraggedEdge() {
return m_draggedEdge;
}
public void mouseDown(MouseEvent e) {
if (m_graphPane.isEnabled() && cursorLens.isMoveGraph() && e.button == 1 && isStartDraggingEvent(e)) {
Point point = new Point(e.x, e.y);
m_draggedNode=m_graphPane.getNodeAtPoint(point);
if (m_draggedNode!=null) {
m_lastPosition=new Point(e.x, e.y);
Point nodeScreenPoint=m_graphPane.getScreenPointForNode(m_draggedNode);
m_grabPoint1=new Point(point.x-nodeScreenPoint.x,point.y-nodeScreenPoint.y);
m_oldFixedState1=m_draggedNode.isFixed();
m_draggedNode.setFixed(true);
cursorLens.setCursor(CursorLens.hand);
}
else {
m_draggedEdge=m_graphPane.getNearestEdge(point);
if (m_draggedEdge!=null) {
m_lastPosition = new Point(e.x, e.y);
Point fromPoint=m_graphPane.getScreenPointForNode(m_draggedEdge.getFrom());
m_grabPoint1=new Point(point.x-fromPoint.x,point.y-fromPoint.y);
Point toPoint=m_graphPane.getScreenPointForNode(m_draggedEdge.getTo());
m_grabPoint2=new Point(point.x-toPoint.x,point.y-toPoint.y);
m_oldFixedState1=m_draggedEdge.getFrom().isFixed();
m_oldFixedState1=m_draggedEdge.getTo().isFixed();
m_draggedEdge.getFrom().setFixed(true);
m_draggedEdge.getTo().setFixed(true);
cursorLens.setCursor(CursorLens.hand);
}
}
}
}
public void mouseUp(MouseEvent e) {
if (m_draggedNode!=null) {
moveDraggedNode(new Point(e.x, e.y), false);
m_draggedNode.setFixed(m_oldFixedState1);
cursorLens.setCursor(null);
m_grabPoint1=null;
m_draggedNode=null;
m_lastPosition=null;
}
if (m_draggedEdge!=null) {
moveDraggedEdge(new Point(e.x, e.y), false);
m_draggedEdge.getFrom().setFixed(m_oldFixedState1);
m_draggedEdge.getTo().setFixed(m_oldFixedState2);
cursorLens.setCursor(null);
m_grabPoint1=null;
m_grabPoint2=null;
m_draggedEdge=null;
m_lastPosition=null;
}
}
public void mouseMove(MouseEvent e) {
if (m_draggedNode!=null) {
autoscroll(e);
moveDraggedNode(new Point(e.x, e.y), true);
}
if (m_draggedEdge!=null) {
autoscroll(e);
moveDraggedEdge(new Point(e.x, e.y), true);
}
}
protected boolean isStartDraggingEvent(MouseEvent e) {
if (mouseEventKeyMask == SWT.NONE){
return true;
}
return (e.stateMask & mouseEventKeyMask) != 0;
}
/**
* Moves the dragged node to given point.
*
* @param point the screen point where the node should be dragged
*/
protected void moveDraggedNode(Point point, boolean stillMoving) {
if (!point.equals(m_lastPosition)) {
Point tempLastPoint = new Point(point.x, point.y);
point.x-=m_grabPoint1.x;
point.y-=m_grabPoint1.y;
Point2D graphPoint=new Point2D.Double();
m_graphPane.screenToGraphPoint(point,graphPoint);
double deltax = graphPoint.getX() - m_draggedNode.getX();
if (deltax < 0){
deltax *= -1;
}
double deltay = graphPoint.getY() - m_draggedNode.getY();
if (deltay < 0){
deltay *= -1;
}
if (deltax >= 10 || deltay >= 10 || !stillMoving){
m_lastPosition.x = tempLastPoint.x;
m_lastPosition.y = tempLastPoint.y;
m_draggedNode.setLocation(graphPoint.getX(),graphPoint.getY());
m_graphPane.getGraph().notifyLayoutUpdated();
}
}
}
/**
* Moves the dragged edge to given point.
*
* @param point the screen point where the edge should be dragged
*/
protected void moveDraggedEdge(Point point, boolean stillMoving) {
if (!point.equals(m_lastPosition)) {
Point tempLastPoint = new Point(point.x, point.y);
Point p1 = new Point(point.x-m_grabPoint1.x,point.y-m_grabPoint1.y);
Point p2 = new Point(point.x-m_grabPoint2.x,point.y-m_grabPoint2.y);
Point2D graphPoint1=new Point2D.Double();
Point2D graphPoint2=new Point2D.Double();
m_graphPane.screenToGraphPoint(p1, graphPoint1);
m_graphPane.screenToGraphPoint(p2, graphPoint2);
double deltax1 = graphPoint1.getX() - m_draggedEdge.getFrom().getX();
if (deltax1 < 0){
deltax1 *= -1;
}
double deltay1 = graphPoint1.getY() - m_draggedEdge.getFrom().getY();
if (deltay1 < 0){
deltay1 *= -1;
}
double deltax2 = graphPoint2.getX() - m_draggedEdge.getTo().getX();
if (deltax2 < 0){
deltax2 *= -1;
}
double deltay2 = graphPoint2.getY() - m_draggedEdge.getFrom().getY();
if (deltay2 < 0){
deltay2 *= -1;
}
if (deltax1 >= 10 || deltay1 >= 10 || deltax2 >= 10 || deltay2 >= 10 || !stillMoving){
m_lastPosition.x = tempLastPoint.x;
m_lastPosition.y = tempLastPoint.y;
m_draggedEdge.getFrom().setLocation(graphPoint1.getX(),graphPoint1.getY());
m_draggedEdge.getTo().setLocation(graphPoint2.getX(),graphPoint2.getY());
m_graphPane.getGraph().notifyLayoutUpdated();
}
}
}
public void dispose(){
disposed = true;
}
public boolean isDisposed(){
return disposed ;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -