天天看点

GEF图形显示机制(一)

GEF是eclipse中图形绘制插件,它是基于draw2d,下面来看下它的机制

先看一个draw2d程序,

要有一个LightweightSystem,然后给这个LightweightSystem的setcontents函数传递一个figure,这个figure就是LightweightSystem的根figure了。

public static void main(String args[])

  {

    final Label label = new Label("Press a button!");

    Shell shell = new Shell();

    LightweightSystem lws = new LightweightSystem(shell);

    Figure parent = new Figure();

    parent.setLayoutManager(new XYLayout());

    lws.setContents(parent);

    Clickable above = new CheckBox("I'm above!");

    parent.add(above, new Rectangle(10,10,80,20));

    ButtonModel aModel = new ToggleModel();

    aModel.addChangeListener(new ChangeListener()

    {

      public void handleStateChanged(ChangeEvent e)

      {

        label.setText("Above");

      }

    });

    above.setModel(aModel);

    Clickable below = new CheckBox("I'm below!");

    parent.add(below, new Rectangle(10,40,80,20));

    ButtonModel bModel = new ToggleModel();

    bModel.addChangeListener(new ChangeListener()

    {

      public void handleStateChanged(ChangeEvent e)

      {

        label.setText("Below");

      }

    });

    below.setModel(bModel);

    ButtonGroup bGroup = new ButtonGroup();

    bGroup.add(aModel);

    bGroup.add(bModel);

    bGroup.setDefault(bModel);

    parent.add(label, new Rectangle(10,70,80,20));

    shell.setSize(130,120);

    shell.open();

    shell.setText("Example");

    Display display = Display.getDefault();

    while (!shell.isDisposed())

    {

      if (!display.readAndDispatch())

      display.sleep ();

}

那在GEF中如何实现上面这一个过程的呢。

public abstract class GraphicalEditor

    extends EditorPart

    implements CommandStackListener, ISelectionListener

{

public void createPartControl(Composite parent) {

    createGraphicalViewer(parent);

}

protected void createGraphicalViewer(Composite parent) {

    GraphicalViewer viewer = new ScrollingGraphicalViewer();

    viewer.createControl(parent);

    setGraphicalViewer(viewer);

    configureGraphicalViewer();

    hookGraphicalViewer();

    initializeGraphicalViewer();

}

ScrollingGraphicalViewer的createControl,完成lws和composite的关联。

public final Control createControl(Composite parent) {

    FigureCanvas canvas = new FigureCanvas(parent, getLightweightSystem());

    setControl(canvas);

    installRootFigure();

    return canvas;

}

protected LightweightSystem getLightweightSystem() {

    return lws;

}

private final LightweightSystem lws = createLightweightSystem();

这样就创建一个LightweightSystem了,并和composite关联了。

那LightweightSystem得setcontents在哪呢:

在installRootFigure这个函数:

private void installRootFigure() {

    if (getFigureCanvas() == null)

        return;

    if (rootFigure instanceof Viewport)

        getFigureCanvas().setViewport((Viewport)rootFigure);

    else

        getFigureCanvas().setContents(rootFigure);

}

public void setViewport(Viewport vp) {

    if (viewport != null)

        unhookViewport();

    viewport = vp;

    lws.setContents(viewport);

    hookViewport();

}

也就是说viewport就是rootifigure.

那这个rootfigure是哪来的呢:

这个是在GraphicalViewer创建时就创建了;

Rootfigure是RootEditPart的createfigure创建的。

GraphicalViewer继承GraphicalViewerImpl

public GraphicalViewerImpl () {

    createDefaultRoot();

    setProperty(MouseWheelHandler.KeyGenerator.getKey(SWT.NONE),

            MouseWheelDelegateHandler.SINGLETON);

}

protected void createDefaultRoot() {

    setRootEditPart(new ScalableRootEditPart());

}

public void setRootEditPart(RootEditPart editpart) {

    super.setRootEditPart(editpart);

    setRootFigure(((GraphicalEditPart)editpart).getFigure());

}

protected void setRootFigure(IFigure figure) {

    rootFigure = figure;

    getLightweightSystem().setContents(rootFigure);

}

当然GraphicalViewer的rooteditpart可以通过setrooteditpart函数改。

public void setRootEditPart(RootEditPart editpart) {

    super.setRootEditPart(editpart);

    setRootFigure(((GraphicalEditPart)editpart).getFigure());

}

public void setRootEditPart(RootEditPart editpart) {

    if (rootEditPart != null) {

        if (rootEditPart.isActive())

            rootEditPart.deactivate();

        rootEditPart.setViewer(null);

    }

    rootEditPart = editpart;

    rootEditPart.setViewer(this);

    if (getControl() != null)

        rootEditPart.activate();

}

protected void setRootFigure(IFigure figure) {

    rootFigure = figure;

    getLightweightSystem().setContents(rootFigure);

}

当然设置后会覆盖原来的。

转载自http://sjj0412.cublog.cn/

继续阅读