GMF学习系列(二) 一些知识点(续)

4、使用ConnectionHandler连接到canvas上已存在的图形或创建新的图形

覆盖XXXModelingAssistantProvider里的几个get方法,要连接到已存在的图形覆盖getRelTypesOnSourceAndTarget()方法,创建新的作为源的图形覆盖getRelTypesOnSource()和getTypesForTarget()方法,创建新的作为目标的图形应覆盖getRelTypesOnTarget()和getTypesForSource()方法。具体代码可参考LogicModelingAssistantProvider里的实现。

5 如何设置Compartment为自动填充父窗口

以工程中的Layer为例:

在****EditPart类的内部类****Figure的构造函数改为:

public BlueprintLayerFigure() {

ConstrainedToolbarLayout layout = new ConstrainedToolbarLayout();

layout.setStretchMajorAxis(true);

layout.setStretchMinorAxis(true);

layout.setMinorAlignment(ConstrainedToolbarLayout.ALIGN_TOPLEFT);

//optional

layout.setVertical(true);

layout.setSpacing(5);

// add layout to make compartment rectangle can extend automatically while resizing parent node.

this.setLayoutManager(layout);

this.setBackgroundColor(THIS_BACK);

this.setPreferredSize(new Dimension(getMapMode().DPtoLP(850), getMapMode().DPtoLP(200)));

createContents();

}

但是这样的话,Name Label也会同时自动扩充,可以通过设定Name Label的最大高度来限制

****LayerNameFigure.setMaximumSize(new Dimension(1850,30));

6.确定连线中连线的锚点

找到相应的EditPart文件,添加四个方法,

    /**

     * @generated NOT

     */

    @Override

    public ConnectionAnchor getSourceConnectionAnchor(

           ConnectionEditPart connEditPart) {

       return new LinkAnchor(getFigure());

    }

 

    /**

     * @generated NOT

     */

    @Override

    public ConnectionAnchor getSourceConnectionAnchor(Request request) {

       return new LinkAnchor(getFigure());

    }

 

    /**

     * @generated NOT

     */

    @Override

    public ConnectionAnchor getTargetConnectionAnchor(

           ConnectionEditPart connEditPart) {

       return new LinkAnchor(getFigure(), false);

    }

 

    /**

     * @generated NOT

     */

    @Override

    public ConnectionAnchor getTargetConnectionAnchor(Request request) {

       return new LinkAnchor(getFigure(), false);

    }

可以使用系统提供的一些Anchor,也可以自定义Anchor。

自定义Anchor需要继承自AbstractConnectionAnchor,并实现getLocation方法。

本文中的是

import org.eclipse.draw2d.*;

import org.eclipse.draw2d.geometry.*;

 

public class LinkAnchor extends AbstractConnectionAnchor {

 

    private boolean IsSource=true;

    public LinkAnchor(IFigure figure)

    {

        super(figure);

       

    }

   

    public LinkAnchor(IFigure figure,boolean issource)

    {

        this(figure);

        IsSource=issource;

       

    }

   

    public Point getLocation(Point loc)

     {

        Rectangle r = getOwner().getBounds();

        int x = r.x+r.width/2;

        int y = r.y+r.height;

        if(!IsSource)

        {

            x = r.x+r.width/2;

            y = r.y;

        }

      

     

        Point p = new Point(x,y);

        getOwner().translateToAbsolute(p);

      

        return p;

     }

 

}

 

7.为了测试,在VariableContentProvider中添加了一些数据。在DesignProcessPropertySection中的createControl中添加了initialize()方法。

posted on 2011-10-26 17:14  缠中说禅  阅读(436)  评论(0编辑  收藏  举报