八进制

少年壮志无烟抽

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  233 随笔 :: 0 文章 :: 3036 评论 :: 11 引用

从直线连接转换到可以任意增减转折点的折线连接,因为模型里要增加新的元素,所以模型、editpart和图形部分都要有所修改,显得稍微有些烦琐,但其实很多代码是通用的。这个过程主要分为以下几个部分:

1、在模型里增加转折点对应的类(这些转折点在GEF里称作Bendpoint),在类里要具有两个Dimension类型用来记录Bendpoint相对连接线起止点的位置。在连接类里要维护一个Bendpoint列表,并提供访问方法,由于篇幅关系这里只列出连接类中的这几个方法。

public void addBendpoint(int index, ConnectionBendpoint point) {
    getBendpoints().add(index, point);
    firePropertyChange(PROP_BENDPOINT, 
nullnull);
}

/**
 * zhanghao: 为了在更新两个dimension后能发送事件,在MoveBendpointCommand要在用这个方法设置新坐标,
 * 而不是直接用BendPoint里的方法。
 
*/
public void setBendpointRelativeDimensions(int index, Dimension d1, Dimension d2){
    ConnectionBendpoint cbp
=(ConnectionBendpoint)getBendpoints().get(index);
    cbp.setRelativeDimensions(d1,d2);
    firePropertyChange(PROP_BENDPOINT, 
nullnull);
}

public void removeBendpoint(int index) {
    getBendpoints().remove(index);
    firePropertyChange(PROP_BENDPOINT, 
nullnull);
}

2、在原来的连接方式里,由于连接线本身不需要刷新,所以现在要确保这个editpart实现了PropertyChangeListener接口,并像其他editpart一样覆盖了activate()和deactivate()这两个方法,以便接收Bendpoint发生改变的事件。

public void activate() {
    
super.activate();
    ((Connection)getModel()).addPropertyChangeListener(
this);
}

public void deactivate() {
    
super.deactivate();
    ((Connection)getModel()).removePropertyChangeListener(
this);
}

public void propertyChange(PropertyChangeEvent event) {
    String property 
= event.getPropertyName();
    
if(Connection.PROP_BENDPOINT.equals(property)){
        refreshBendpoints();
    }
}

为模型连接类对应的editpart里增加一个继承自BendpointEditPolicy的子类ConnectionBendPointEditPolicy,这个类的内容后面会说到。

protected void createEditPolicies() {
    
    installEditPolicy(EditPolicy.CONNECTION_BENDPOINTS_ROLE, 
new ConnectionBendPointEditPolicy());
}

直线连接的情况下,连接的刷新不需要我们负责,但增加了Bendpoint以后,必须在Bendpoint发生改变时刷新连接线的显示。所以在上面这个editpart的refreshVisuals()方法里需要增加一些代码,以便把模型里的Bendpoint转换为图形上的relativeBendpoint。

protected void refreshVisuals() {
    Connection conn 
= (Connection) getModel();
    List modelConstraint 
= conn.getBendpoints();
    List figureConstraint 
= new ArrayList();
    
for (int i = 0; i < modelConstraint.size(); i++) {
        ConnectionBendpoint cbp 
= (ConnectionBendpoint) modelConstraint
                .get(i);
        RelativeBendpoint rbp 
= new RelativeBendpoint(getConnectionFigure());
        rbp.setRelativeDimensions(cbp.getFirstRelativeDimension(), cbp
                .getSecondRelativeDimension());
        rbp.setWeight((i 
+ 1/ ((float) modelConstraint.size() + 1));
        figureConstraint.add(rbp);
    }
    getConnectionFigure().setRoutingConstraint(figureConstraint);
}

3、创建CreateBendpointCommand、MoveBendpointCommand和DeleteBendpointCommand这三个类,可以像Logic例子那样创建一个基类BendPointCommand让它们来继承。作为例子,BendpointCommand的内容如下。

public class BendpointCommand extends Command {

    
protected int index;
    
protected Connection connection;
    
protected Dimension d1, d2;

    
public void setConnection(Connection connection) {
        
this.connection = connection;
    }

    
public void redo() {
        execute();
    }

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

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

}

4、在ConnectionBendPointEditPolicy里实现BendpointEditPolicy定义的创建、移动和删除Bendpoint的三个方法。

public class ConnectionBendPointEditPolicy extends BendpointEditPolicy {

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

        conn.translateToRelative(p);

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

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

        cmd.setRelativeDimensions(p.getDifference(ref1), p.getDifference(ref2));
        cmd.setConnection((com.example.model.Connection) request.getSource()
                .getModel());
        cmd.setIndex(request.getIndex());
        
return cmd;
    }

    
protected Command getDeleteBendpointCommand(BendpointRequest request) {
        BendpointCommand cmd 
= new DeleteBendpointCommand();
        Point p 
= request.getLocation();
        cmd.setConnection((com.example.model.Connection) request.getSource().getModel());
        cmd.setIndex(request.getIndex());
        
return cmd;
    }

    
protected Command getMoveBendpointCommand(BendpointRequest request) {
        MoveBendpointCommand cmd 
= new MoveBendpointCommand();
        Point p 
= request.getLocation();
        Connection conn 
= getConnection();

        conn.translateToRelative(p);

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

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

        cmd.setRelativeDimensions(p.getDifference(ref1), p.getDifference(ref2));
        cmd.setConnection((com.example.model.Connection) request.getSource()
                .getModel());
        cmd.setIndex(request.getIndex());
        
return cmd;
    }
}

修改完成后的编辑器如下图所示。


编辑器中的转折连接线

点此下载工程,此工程修改自GEF应用实例中的GefPractice,目标文件的扩展名改为.gefpracticebp。

posted on 2006-06-22 00:47 八进制 阅读(3059) 评论(13)  编辑 收藏 所属分类: EclipseGEF

评论

我已经建立了节点之间的连接,但是我在打开文件时,不想显示连接弧,只有在选中节点时,才显示该节点上的连接弧,请问这如何实现.
huang_wei@cvicse.com
  回复  引用    

#2楼  2007-07-09 15:33 jiezi [未注册用户]
请问连接线正交(就是只在横和竖两个方向折)相关的例子有么
bit506@163.com
  回复  引用    

#3楼 [楼主] 2007-07-10 15:35 八进制      
to jiezi: 用gef提供的ManhattonConnectionRouter
to jacky9881: 最直接的方法是给连接线增加一个visible属性,并在它的editpart里的refreshVisuals()(也许是refreshConnection())方法里根据这个属性设置connectionFigure的visible属性。当选中节点时把相关连接线的visible设置为true其他设置为false。
  回复  引用  查看    

#4楼  2007-07-26 17:27 任我行 [未注册用户]
请指示如何在连线的中间给连线加上注释
  回复  引用    

#5楼 [楼主] 2007-07-27 11:16 八进制      
注释用Label,用MidpointLocator把它定位在连线上。
  回复  引用  查看    

#6楼  2007-07-31 17:48 jiezi [未注册用户]
用gef提供的ManhattonConnectionRouter可实现正交,但要实现可以拖动的正交该怎么实现啊
  回复  引用    

#7楼 [楼主] 2007-07-31 22:42 八进制      
没试过,应该也是用bendpoint实现。
  回复  引用  查看    

#8楼  2007-08-13 16:15 jacky [未注册用户]
给连接弧增加拐点,同时实现了网格吸附功能,能够实现让拐点自动吸附到网格的功能?
  回复  引用    

#9楼  2007-08-14 08:32 jiezi [未注册用户]
恩同问下,让图figure或线自动吸附到网格怎么实现
  回复  引用    

#10楼  2007-08-15 15:01 SSCarrier [未注册用户]
八进制您好,请问有什么办法让整个Diagram有两类children:Node和Connection,谢谢!
  回复  引用    

下载的工程的xml文件内容缺失这些内容
<plugin
id="GefPractice"
name="GefPractice Plug-in"
version="1.0.0"
provider-name=""
class="com.example.GefPracticePlugin">

<runtime>
<library name="GefPractice.jar">
<export name="*"/>
</library>
</runtime>

<requires>
<import plugin="org.eclipse.core.resources"/>
<import plugin="org.eclipse.gef"/>
<import plugin="org.eclipse.ui"/>
<import plugin="org.eclipse.core.runtime"/>
<import plugin="org.eclipse.core.runtime.compatibility"/>
<import plugin="org.eclipse.ui.views"/>
</requires>
  回复  引用    

#12楼  2008-09-19 19:43 zhangqu [未注册用户]
请问搂主,如果要求连接为弧线,最好弧上有一控制点,通过鼠标拖动控制点来调整弧线的形状,请问该怎么做

email:chj4144_cn@hotmail.com
qq34087032
  回复  引用    

#13楼 [楼主] 2008-09-27 16:05 八进制      
我没有实现过,但GMF里已经实现了这个功能,你可以参考/利用它的PolylineConnectionEx类。
  回复  引用  查看    


标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      


相关链接: