📄 backgrounddraggingmanipulator.java
字号:
package net.sourceforge.jpowergraph.manipulator.dragging;
import java.awt.Cursor;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import net.sourceforge.jpowergraph.lens.CursorLens;
import net.sourceforge.jpowergraph.manipulator.AbstractManipulator;
/**
* A manipulator enabling the dragging of the graph's background.
*/
public class BackgroundDraggingManipulator extends AbstractManipulator {
/** The name of this manipulator. */
public static final String NAME="BackgroundDraggingManipulator";
/** The position where the mouse was grabbed. */
protected Point m_grabPosition;
private int mouseEventKeyMask = -1;
private CursorLens cursorLens;
/**
* Creates an instance of this class.
*
* Dragging the background can be done by holding down the shift key
* and left clicking.
*/
public BackgroundDraggingManipulator(CursorLens theCursorLens) {
this(theCursorLens, MouseEvent.SHIFT_MASK);
}
/**
* Creates an instance of this class.
*
* Dragging the background can be done by holding down the key specified in
* theMouseKeyAssist and left clicking. The key mask can be one of:
*
* MouseEvent.SHIFT_MASK
* MouseEvent.CTRL_MASK
* MouseEvent.ALT_MASK
* -1 (for no key mask)
*
* @param theMouseEventKeyMask The Mouse MASK
*/
public BackgroundDraggingManipulator(CursorLens theCursorLens, int theMouseEventKeyMask) {
this.cursorLens = theCursorLens;
mouseEventKeyMask = theMouseEventKeyMask;
}
public String getName() {
return NAME;
}
public boolean isDragging() {
return m_grabPosition!=null;
}
public void mousePressed(MouseEvent e) {
if (m_graphPane.isEnabled() && (e.getModifiers() & MouseEvent.BUTTON1_MASK)!=0 && isStartBackgroundDraggingEvent(e)) {
Point point=e.getPoint();
if (m_graphPane.getNodeAtPoint(point)==null && m_graphPane.getNearestEdge(point)==null) {
m_grabPosition=point;
cursorLens.setCursor(CursorLens.hand);
e.consume();
}
}
}
public void mouseReleased(MouseEvent e) {
if (isDragging()) {
performDrag(e.getPoint());
cursorLens.setCursor(null);
m_grabPosition=null;
e.consume();
}
}
public void mouseDragged(MouseEvent e) {
if (isDragging()) {
performDrag(e.getPoint());
e.consume();
}
}
protected void performDrag(Point position) {
Rectangle rectangle=m_graphPane.getVisibleRect();
rectangle.x+=m_grabPosition.x-position.x;
rectangle.y+=m_grabPosition.y-position.y;
m_graphPane.scrollRectToVisible(rectangle);
m_grabPosition=position;
}
protected boolean isStartBackgroundDraggingEvent(MouseEvent e) {
if (mouseEventKeyMask == -1){
return true;
}
return (e.getModifiers() & mouseEventKeyMask) != 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -