分类:
2009-10-13 00:01:53
-----------------------------------------------------------
本文系本站原创,欢迎转载!
转载请注明出处:http://sjj0412.cublog.cn/
-----------------------------------------------------------
当单击节点EditPart的拖动区域时:因为正好在Viewer的热点:
故会返回ResizeTracker。
protected boolean handleButtonDown(int button) {
if (!stateTransition(STATE_INITIAL, STATE_DRAG)) {
resetHover();
return true;
}
resetHover();
EditPartViewer viewer = getCurrentViewer();
Point p = getLocation();
if (getDragTracker() != null)
getDragTracker().deactivate();
if (viewer instanceof GraphicalViewer) {
Handle
handle = ((GraphicalViewer) viewer).findHandleAt(p);
if
(handle != null) {
setDragTracker(handle.getDragTracker());
return
true;
}
}
//跟MOVE时不一样,在上面就setDragTracker ResizeTracer,而MOVE是通过下面的
// setDragTracker(editpart.getDragTracker(getTargetRequest()))设置的
updateTargetRequest();
((SelectionRequest)getTargetRequest()).setLastButtonPressed(button);
updateTargetUnderMouse();
EditPart editpart = getTargetEditPart();
if (editpart != null) {
setDragTracker(editpart.getDragTracker(getTargetRequest()));
lockTargetEditPart(editpart);
return true;
}
return false;
}
ResizeTracer和DrageEditPartTracker的处理差别很大,首先是REQ的type.
protected String getCommandName() {
return REQ_RESIZE;
}
不一样,导致request type是REQ_RESIZE而以,且request都是changeBoundrequest,
同时处理过程也不一样,主要体现在handleDragInProgress中.
protected boolean handleDragInProgress() {
if (isInDragInProgress()) {
updateSourceRequest();
//这个是Source,前面的MOVE是target
showSourceFeedback();
showTargetFeedback();
setCurrentCommand(getCommand());
}
return true;
}
更主要的区别体现在如下3点:
protected void showSourceFeedback()
{
List editParts = getOperationSet();
for (int i = 0; i < editParts.size(); i++) {
EditPart editPart = (EditPart) editParts.get(i);
editPart.showSourceFeedback(getSourceRequest());
//这个是用getSourceRequest
}
setFlag(FLAG_SOURCE_FEEDBACK, true);
}
protected void showTargetFeedback()
{
setFlag(FLAG_TARGET_FEEDBACK, true);
if (getTargetEditPart() != null)
getTargetEditPart().showTargetFeedback(getSourceRequest());
}
同时showTargetFeedback也用的是getSourceRequest().
updateSourceRequest的函数更复杂,其由8中情况的变化操作,SOUTH_EAST, NORTH_WEST, NORTH_EAST , SOUTH_WEST,SOUTH,EAST,NORTH,WEST:
protected void updateSourceRequest()
{
ChangeBoundsRequest request = (ChangeBoundsRequest)
getSourceRequest();
Dimension d = getDragMoveDelta();
Point location = new Point(getLocation());
Point moveDelta = new Point(0, 0);
Dimension resizeDelta = new Dimension(0, 0);
if (getCurrentInput().isShiftKeyDown() && owner != null) {
request.setConstrainedResize(true);
int origHeight = owner.getFigure().getBounds().height;
int origWidth = owner.getFigure().getBounds().width;
float ratio = 1;
if (origWidth != 0 && origHeight != 0)
ratio = ((float)origHeight / (float)origWidth);
//东南边方向改变
if (getResizeDirection() == PositionConstants.SOUTH_EAST) {
if (d.height > (d.width * ratio))
d.width = (int)(d.height / ratio);
else
d.height = (int)(d.width * ratio);
//西北边方向改变
} else if (getResizeDirection() == PositionConstants.NORTH_WEST) {
if (d.height < (d.width * ratio))
d.width = (int)(d.height / ratio);
else
d.height = (int)(d.width * ratio);
} else if (getResizeDirection() == PositionConstants.NORTH_EAST) {
if (-(d.height) > (d.width * ratio))
d.width = -(int)(d.height / ratio);
else
d.height = -(int)(d.width * ratio);
} else if (getResizeDirection() == PositionConstants.SOUTH_WEST) {
if (-(d.height) < (d.width * ratio))
d.width = -(int)(d.height / ratio);
else
d.height = -(int)(d.width * ratio);
}
} else
request.setConstrainedResize(false);
request.setCenteredResize(getCurrentInput().isModKeyDown(SWT.MOD1));
if ((getResizeDirection() & PositionConstants.NORTH) != 0) {
if (getCurrentInput().isControlKeyDown()) {
resizeDelta.height -= d.height;
}
moveDelta.y += d.height;
resizeDelta.height -= d.height;
}
if ((getResizeDirection() & PositionConstants.SOUTH) != 0) {
if (getCurrentInput().isControlKeyDown()) {
moveDelta.y -= d.height;
resizeDelta.height += d.height;
}
resizeDelta.height += d.height;
}
if ((getResizeDirection() & PositionConstants.WEST) != 0) {
if (getCurrentInput().isControlKeyDown()) {
resizeDelta.width -= d.width;
}
moveDelta.x += d.width;
resizeDelta.width -= d.width;
}
if ((getResizeDirection() & PositionConstants.EAST) != 0) {
if (getCurrentInput().isControlKeyDown()) {
moveDelta.x -= d.width;
resizeDelta.width += d.width;
}
resizeDelta.width += d.width;
}
request.setMoveDelta(moveDelta);
request.setSizeDelta(resizeDelta);
request.setLocation(location);
request.setEditParts(getOperationSet());
request.getExtendedData().clear();
if (!getCurrentInput().isAltKeyDown() && snapToHelper != null) {
PrecisionRectangle rect = sourceRect.getPreciseCopy();
rect.translate(moveDelta);
rect.resize(resizeDelta);
PrecisionRectangle result = new PrecisionRectangle();
snapToHelper.snapRectangle(request, request.getResizeDirection(),
rect, result);
if (request.isCenteredResize()) {
if (result.preciseX != 0.0)
result.preciseWidth += -result.preciseX;
else if (result.preciseWidth != 0.0) {
result.preciseX = -result.preciseWidth;
result.preciseWidth *= 2.0;
}
if (result.preciseY != 0.0)
result.preciseHeight += -result.preciseY;
else if (result.preciseHeight != 0.0) {
result.preciseY = -result.preciseHeight;
result.preciseHeight *= 2.0;
}
result.updateInts();
}
PrecisionPoint preciseMove = new PrecisionPoint(
result.x + moveDelta.x,
result.y + moveDelta.y);
PrecisionDimension preciseResize = new PrecisionDimension(
result.width + resizeDelta.width,
result.height + resizeDelta.height);
request.setMoveDelta(preciseMove);
request.setSizeDelta(preciseResize);
}
}
ResizeTracer的getCommand是调用节点EditParT的getCommand,然后在间接调用节点EditParT的parent-ContentEditPart的getCommand,同时中间requst类型发生了变化,由REQ_RESIZE变成REQ_RESIZE_CHILDREN,而DragEditPartTracer是直接调用TargetEditPart即ContentEditPart的getCommand。
protected Command getCommand() {
List editparts = getOperationSet();
//被选择的节点。
EditPart part;
CompoundCommand command = new CompoundCommand();
command.setDebugLabel("Resize
Handle Tracker");//$NON-NLS-1$
for (int i = 0; i < editparts.size(); i++) {
part = (EditPart)editparts.get(i);
command.add(part.getCommand(getSourceRequest()));
}
return command.unwrap();
}
这个是支持拖放的节点的resizablePolicy的函数:
public Command getCommand(Request request) {
if (REQ_RESIZE.equals(request.getType()))
//注意,这里是REQ_RESIZE
return getResizeCommand((ChangeBoundsRequest)request);
return super.getCommand(request);
}
protected Command getResizeCommand(ChangeBoundsRequest request) {
ChangeBoundsRequest req = new ChangeBoundsRequest(REQ_RESIZE_CHILDREN);
//到这里变成REQ_RESIZE_CHILDREN了,因为要发给节点父节点contentEditpart
req.setEditParts(getHost());
req.setMoveDelta(request.getMoveDelta());
req.setSizeDelta(request.getSizeDelta());
req.setLocation(request.getLocation());
req.setExtendedData(request.getExtendedData());
req.setResizeDirection(request.getResizeDirection());
return getHost().getParent().getCommand(req);
//调用父亲节点处理。
}
当在空白处点击时:Target是rootEditpart,而rootEditPart的getDragTracer就是MarqueeDragTracker,这样这个事件的DragTracer就是MarqueeDragTracker。 Class MarqueeDragTracker 鼠标按下时: protected boolean handleButtonDown(int button) { if (!isGraphicalViewer()) return true; if (button != 1) { setState(STATE_INVALID); handleInvalidInput(); } if (stateTransition(STATE_INITIAL, STATE_DRAG_IN_PROGRESS)) { if (getCurrentInput().isModKeyDown(SWT.MOD1)) setSelectionMode(TOGGLE_MODE); //这个是修改选择效果 else if (getCurrentInput().isShiftKeyDown()) setSelectionMode(APPEND_MODE); //这个是加了shift建的选择效果,就是增加选项 else setSelectionMode(DEFAULT_MODE); } return true; } 拖曳过程中: protected boolean handleDragInProgress() { if (isInState(STATE_DRAG | STATE_DRAG_IN_PROGRESS)) { showMarqueeFeedback(); //显示选择虚线框。 eraseTargetFeedback(); calculateNewSelection(selectedEditParts = new ArrayList(), newArrayList()); //重新计算被选择的节点。。 showTargetFeedback(); } return true; }在空白处拖曳事件的DragTracer—MarqueeDragTracker
1.选择,平移,改变大小这些操作的source,target的editpart分别是什么?
2.选择,平移,改变大小操作出来的虚框是怎么出来的,这个是feedback产生的?如果是feedback产生的,由于平移,改变会执行showTargetFeedback和showSourceFeedback,那么这个虚框是Target产生,还是Source产生。
3.当我们拖曳时,尽管鼠标在source editpart上,为什么target editpart始终是content editpart呢?