Thursday, April 26, 2007

Synchronizing Views with a GEF Editor

View synchronizing is one of the fundamental features for most Eclipse workbench apps. When using a GEF editor, you can use additional views to provide more targeted information about the selection, or even allow editing. There is a lot of good information about this topic - both in the eclipse docs and in the newsgroups. However, I think I can augment that with a concise set of steps for getting this task done quickly.

In this post, I'll talk about how to easily allow views to respond to selection events in an editor.

  1. First, you will need to make sure events are being properly fired from your editor. Since a default selection provider is provided by the GEF Graphical Editor, this is handled for you if you are extending it.

  2. Next, you need to watch for these selection events in your views. This is done by simply adding a postSelectionListener to the page during createPartControl in the view:

  3. getSite().getPage().addPostSelectionListener(yourPageSelectionListener);

    It is important to note here that you can't use the other add method that allows filtering by workbench part. This does not work for editors. I have filed an enhancement request to get this added, but it wouldn't be until 3.4 at the earliest since 3.3 is at API freeze.

  4. If all you wanted was to get selection events of your edit parts, you're done. However, in my designs, I usually like to hide the edit parts from view outside the editor. For this, you will need to use the Eclipse adapter mechanism. In your edit part getAdapter method, you have the opportunity to do any pre-processing.

    Lets say that your edit parts are shapes and you want to find out the color, and size of them without exposing the edit parts to the view:
    1. First, put an interface in a shared plugin called: IShapeInfo with getters for color and size
    2. In Shape.getAdapter, when IShapeInfo is passed in, return an implementation that provides size and color. (You can implement IShapeInfo in Shape, create an anonymous inner class, etc.)
    3. In your view's pageSelectionListener:


//Get the selection as a list
StructuredSelection ss = (StructuredSelection) selection;
List sList = ss.toList();
/**
* Create a list of objects to show
*/
for (Iterator iterator = sList.iterator(); iterator.hasNext();) {
Object name = iterator.next();
//See if it implements IAdaptable
if (name instanceof IAdaptable) {
//See if the IShapeInfo is available as an adapter
Object o = ((IAdaptable)name).getAdapter(IShapeInfo.class);
if(o != null) {
IShapeInfo info = (IShapeInfo)o;
/** Now you can get size and color from the selected Object
}
}
}

Thats it. All you have to do is customize the interface and you can get custom information back from your edit parts - or any other selection - without exposing class details.

Read More...

Thursday, April 12, 2007

Current GEF Activity

Since I have been watching GEF progress closely, I got a nice surprise lately with a lot of Bugzilla activity. Even though the project plan still doesn't really contain any real detail, its nice to see bugs targeted at 3.3 milestone builds, as well as the final release. While I realize that this doesn't mean they will get fixed, its definitely a start. Thanks to the GEF Lead for taking the time to put this in!

For some reason, the GEF project seems to have really dropped in popularity over the last year - at least judging by the newsgroup activity and lack of project activity. As part of the community, I can take some blame for this since I haven't submitted any fixes. My 3.3 resolution is to find at least 1 bug for which I can provide a patch. I strongly encourage anyone interested in GEF to do the same!

Here is the list of bugs currently targeted at 3.3 if you're interested in helping.

Read More...