首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

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

2012-09-05 
Bendpoint随着图元位置的变化而变化我这里以gef.tutorial.step为例,在此工程基础修改代码实现该功能首先将

Bendpoint随着图元位置的变化而变化
我这里以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;}}

热点排行