GEF In-Depth Tutorial

I love GEF (Graphical Editing Framework). Maybe secretly I’m homesick for Swing. I mean, you can draw anything you want on an SWT Canvas (like you can with JComponent). You should use JFace and SWT whenever you can, but when you need a two dimensional, spatial user interface like the XSD editor in WTP (Web Tools Platform), GEF is what you need.

Now let’s see some other ways in how GEF is similar to Swing:

You call revalidate (when you change the layout) and repaint (when a model has changed and needs to show that change in its figure.)

You don’t pass in the parent when you construct Figures. Instead, you add Figures to their parent later as in:

parentFigure.add(childFigure);

This is because GEF is reparentable, unlike SWT.

It sits on top of Draw2D (like how Swing sits on top of Java2D). Note that Draw2D is more focused though. The team warns that Draw2D was not built for general usage like Java2D was. Instead, it was built to support GEF, which is probably a good thing since it does its job well.

OK, enough Swing reminiscing.

The only problem is that GEF is a bit complicated and is a little light on doc. There’s no big Eclipse book for it like there is for EMF or JFace/SWT.

The GEF In-Depth tutorial was a good step in helping more people understand it.

The tutorial was packed. It had probably 50 people. Randy Hudson and Pratik Shah, the two committers on GEF, presented. Note that the PDF that is up currently is not up to date. They handed out a newer presentation that was more extensive and should be up on the main EclipseCon site by the end of the week.

I think I had seen most of the material before, but hearing Randy and Pratik explain it in person was different. I learned which things to pay attention to, useful tips, and got it in context.

The key to GEF, I think, is to understand how its design is similar to Swing and then recognizing what it adds on top of that. I’m talking specifically about things like connections, edit parts, edit policies, zooming. There’s really so much to talk about GEF, I’d have to dedicate a portion of my website to explain all that I’ve learned. Now I didn’t learn all of this from the tutorial. I’ve implemented a few GEF projects and have experimented with even more small experimental ones. So I’ll just offer what I consider to be the philosophy of GEF along with some useful tips:

The philosophy:

1. Don’t dictate any specific model. Instead, let the implementor use any model.

2. Allow the user to interact with the model graphically, mostly with direct editing metaphors like drag and drop (to create, to move, and to connect), double-clicking (to edit), and single-clicking / marquee dragging (to select).

3. Provide seamless integration with the Workbench/RCP.

It is simple philosophy, but powerful. The most important is #2 and this is where most of the complexity lies.

Tips:

1. Do not modify objects returned by reference. Ex: bounds.

2. Use revalidate() when you need to reflect layout changes and repaint() when you need to reflect changes in a piece of the model.

3. Use LineBorder during debugging to help highlight where each figure is. You can also set the border or background color to something different. Note that if you are using a Figure, you need to setOpaque(true) to see the background color.

4. If you need help or have questions, try the newsgroup: eclipse.tools.gef on news.eclipse.org.

5. Don’t share layouts.

6. Call super.activate() in activate() and super.deactivate() in deactivate().

7. Use requests to encapsulate interaction.

8. Use commands consistently, so that you don’t have duplicate code and modify your models in a uniform way.

One Reply to “GEF In-Depth Tutorial”

  1. Hello, can you help me with a problem?

    I want to pass by all elements in my diagram and while i’m passing for them
    I want to change the background color to make a simulation of a workflow
    execution.

    My problem is that if the figures already have a color even if I change to
    another color with the setbackground(new Color(null, 0, 44, 0)) doesn’t work
    :(

    But if the figures don’t have any color it works fine!!

    Can anyone help me to change the color of a figure that have a previous
    color?

    This is my code:

    public static void ExecutionSimulation(IFigure figure){

    oldColor = figure.getBackgroundColor();

    try {

    // figure.setBackgroundColor(new Color(null, 220, 220, 250));

    figure.setBackgroundColor(new Color(null, 220, 220, 0));

    Thread.sleep(1000);

    } catch (InterruptedException e) {

    e.printStackTrace();

    }

    figure.setBackgroundColor(oldColor);

    }

    Thanks in advance and sorry for my bad english…

Comments are closed.