Bendpoint随着图元位置的变化而变化

2 篇文章 0 订阅
我这里以gef.tutorial.step为例,在此工程基础修改代码实现该功能

首先将ContentsEditPart的布局做如下调整
	protected IFigure createFigure() {
Layer figure = new Layer() {
public void paint(Graphics graphics) {
graphics.setAntialias(SWT.ON);
graphics.setTextAntialias(SWT.ON);
super.paint(graphics);
}
};
figure.setLayoutManager(new XYLayout());
ConnectionLayer layer = (ConnectionLayer) getLayer(LayerConstants.CONNECTION_LAYER);
layer.setConnectionRouter(ConnectionRouter.NULL);
return figure;
}


然后实现连线转折点模型:
/*******************************************************************************
* Copyright (c) 2005-2011, Chinese Eclipse Community(CEC) All rights reserved.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.ceclipse.org
*
* Contributors:
* Ming.He <heming@ceclipse.com> - initial API and implementation
*******************************************************************************/

package gef.tutorial.step.parts;

import java.io.Serializable;

import org.eclipse.draw2d.Bendpoint;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;

/**
* TODO 此处填写 class 信息
*
* @author Ming.He
* @date 2011-4-15
*/
public class ConnectionBendpoint implements Serializable, Bendpoint {

/**
*
*/
private static final long serialVersionUID = 1L;

private float weight = 0.5f;

private Dimension d1 = null;

private Dimension d2 = null;

public ConnectionBendpoint() {
// ignore
}

public ConnectionBendpoint(Dimension dim1, Dimension dim2) {
d1 = dim1;
d2 = dim2;
}

public Dimension getFirstRelativeDimension() {
return d1;
}

public Point getLocation() {
return null;
}

public Dimension getSecondRelativeDimension() {
return d2;
}

public float getWeight() {
return weight;
}

public void setRelativeDimensions(Dimension dim1, Dimension dim2) {
d1 = dim1;
d2 = dim2;
}

public void setWeight(float w) {
weight = w;
}
}


在来实现Command:
关于转折点的命令这里做了个基类以便添加删除移动实现的方便BendpointCommand:
/*******************************************************************************
* Copyright (c) 2005-2011, Chinese Eclipse Community(CEC) All rights reserved.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.ceclipse.org
*
* Contributors:
* Ming.He <heming@ceclipse.com> - initial API and implementation
*******************************************************************************/
package gef.tutorial.step.commands;

import gef.tutorial.step.model.AbstractConnectionModel;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.gef.commands.Command;

/**
* TODO 此处填写 class 信息
*
* @author Ming.He
* @date 2011-4-15
*/
public class BendpointCommand extends Command {
protected int index = 0;
protected Point location = null;
protected AbstractConnectionModel connectionModel = null;
private Dimension d1 = null;
private Dimension d2 = null;

protected Dimension getFirstRelativeDimension() {
return d1;
}

protected Dimension getSecondRelativeDimension() {
return d2;
}

protected int getIndex() {
return index;
}

@SuppressWarnings("unused")
protected Point getLocation() {
return location;
}

protected AbstractConnectionModel getConnectionModel() {
return connectionModel;
}

public void redo() {
execute();
}

public void setRelativeDimensions(Dimension dim1, Dimension dim2) {
d1 = dim1;
d2 = dim2;
}

public void setIndex(int i) {
index = i;
}

public void setLocation(Point p) {
location = p;
}

public void setConnectionModel(AbstractConnectionModel connection) {
connectionModel = connection;
}
}


CreateBendpointCommand:
/*******************************************************************************
* Copyright (c) 2005-2011, Chinese Eclipse Community(CEC) All rights reserved.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.ceclipse.org
*
* Contributors:
* Ming.He <heming@ceclipse.com> - initial API and implementation
*******************************************************************************/

package gef.tutorial.step.commands;

import gef.tutorial.step.parts.ConnectionBendpoint;

/**
* TODO 此处填写 class 信息
*
* @author Ming.He
* @date 2011-4-15
*/
public class CreateBendpointCommand extends BendpointCommand {
public void execute() {
ConnectionBendpoint rbp = new ConnectionBendpoint(getFirstRelativeDimension(), getSecondRelativeDimension());
getConnectionModel().addBendpoint(getIndex(), rbp);
super.execute();
}

public void undo() {
super.undo();
getConnectionModel().removeBendpoint(getIndex());
}
}

MoveBendpointCommand:
/*******************************************************************************
* Copyright (c) 2005-2011, Chinese Eclipse Community(CEC) All rights reserved.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.ceclipse.org
*
* Contributors:
* Ming.He <heming@ceclipse.com> - initial API and implementation
*******************************************************************************/

package gef.tutorial.step.commands;

import gef.tutorial.step.parts.ConnectionBendpoint;

/**
* TODO 此处填写 class 信息
*
* @author Ming.He
* @date 2011-4-15
*/
public class MoveBendpointCommand extends BendpointCommand {
private ConnectionBendpoint bendpoint = null;

public void execute() {
bendpoint = (ConnectionBendpoint) getConnectionModel().getBendpoints().get(getIndex());
getConnectionModel().removeBendpoint(getIndex());
super.execute();
}

public void undo() {
super.undo();
getConnectionModel().addBendpoint(getIndex(), bendpoint);
}
}

DeleteBendpointCommand:
/*******************************************************************************
* Copyright (c) 2005-2011, Chinese Eclipse Community(CEC) All rights reserved.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.ceclipse.org
*
* Contributors:
* Ming.He <heming@ceclipse.com> - initial API and implementation
*******************************************************************************/

package gef.tutorial.step.commands;

import gef.tutorial.step.parts.ConnectionBendpoint;

/**
* TODO 此处填写 class 信息
*
* @author Ming.He
* @date 2011-4-15
*/
public class DeleteBendpointCommand extends BendpointCommand {
private ConnectionBendpoint bendpoint = null;

public void execute() {
bendpoint = (ConnectionBendpoint) getConnectionModel().getBendpoints().get(getIndex());
getConnectionModel().removeBendpoint(getIndex());
super.execute();
}

public void undo() {
super.undo();
getConnectionModel().addBendpoint(getIndex(), bendpoint);
}

}


对应的Plicy做如下修改:
/*******************************************************************************
* Copyright (c) 2005-2011, Chinese Eclipse Community(CEC) All rights reserved.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.ceclipse.org
*
* Contributors:
* Ming.He <heming@ceclipse.com> - initial API and implementation
*******************************************************************************/

package gef.tutorial.step.policies;

import gef.tutorial.step.commands.BendpointCommand;
import gef.tutorial.step.commands.CreateBendpointCommand;
import gef.tutorial.step.commands.DeleteBendpointCommand;
import gef.tutorial.step.commands.MoveBendpointCommand;
import gef.tutorial.step.model.AbstractConnectionModel;

import org.eclipse.draw2d.Connection;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.BendpointEditPolicy;
import org.eclipse.gef.requests.BendpointRequest;

/**
* TODO 此处填写 class 信息
*
* @author Ming.He
* @date 2011-4-15
*/
public class CustomBendpointEditPolicy extends BendpointEditPolicy {


protected Command getCreateBendpointCommand(BendpointRequest request) {
CreateBendpointCommand command = new CreateBendpointCommand();
Point p = request.getLocation();
Connection conn = getConnection();

conn.translateToRelative(p);

command.setLocation(p);
Point ref1 = getConnection().getSourceAnchor().getReferencePoint();
Point ref2 = getConnection().getTargetAnchor().getReferencePoint();

conn.translateToRelative(ref1);
conn.translateToRelative(ref2);

command.setRelativeDimensions(p.getDifference(ref1), p.getDifference(ref2));
command.setConnectionModel((AbstractConnectionModel) request.getSource().getModel());
command.setIndex(request.getIndex());
return command;
}


protected Command getDeleteBendpointCommand(BendpointRequest request) {
BendpointCommand command = new DeleteBendpointCommand();
Point p = request.getLocation();
command.setLocation(p);
command.setConnectionModel((AbstractConnectionModel) request.getSource().getModel());
command.setIndex(request.getIndex());
return command;
}


protected Command getMoveBendpointCommand(BendpointRequest request) {
// ������bend����
MoveBendpointCommand command = new MoveBendpointCommand();
Point p = request.getLocation();
Connection conn = getConnection();

conn.translateToRelative(p);

command.setLocation(p);

Point ref1 = getConnection().getSourceAnchor().getReferencePoint();
Point ref2 = getConnection().getTargetAnchor().getReferencePoint();

conn.translateToRelative(ref1);
conn.translateToRelative(ref2);

command.setRelativeDimensions(p.getDifference(ref1), p.getDifference(ref2));
command.setConnectionModel((AbstractConnectionModel) request.getSource().getModel());
command.setIndex(request.getIndex());
return command;
}

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值