把GEF放在ViewPart里

其实GEF可以放在任何Composite上,当然也就可以放在视图里了。关键任务是创建GraphicalViewer、RootEditPart、EditDomain和EditPartFactory这些对象,下面的代码是我从别处拷来的,稍微修改了一下。

public class TestView extends ViewPart {
    ScrollingGraphicalViewer graphicalViewer;
    FigureCanvas canvas;
    Diagram diagram;

    public void createPartControl(Composite parent) {
        graphicalViewer = new ScrollingGraphicalViewer();
        canvas = (FigureCanvas) graphicalViewer.createControl(parent);
        ScalableFreeformRootEditPart root = new ScalableFreeformRootEditPart();
        graphicalViewer.setRootEditPart(root);
        graphicalViewer.setEditDomain(new EditDomain());
        graphicalViewer.setEditPartFactory(new PartFactory());
        graphicalViewer.setContents(diagram);
    }
}

运行结果如下,这个基本上只有视图的功能,也可以增加编辑功能,例如对GraphicalViewer加一个DropTargetListener就可以从调色板里拉对象上来了,等等。这个代码有点问题,就是打开View后要调整一下大小才能显示出图形,该怎么解决呢……

file

搬家前链接:https://www.cnblogs.com/bjzhanghao/archive/2005/10/21/259500.html