Sunday 26 October 2008

Geoff on GEF pt. IV

Wow! Things are now really falling into place.

Most of my effort recently has been spent trying to get my GEF ViewPart to display in a seperate window, as a nice touch I wanted it to display in fullscreen on a second monitor if one is available. Amazingly, I've done it.


@SuppressWarnings("restriction")
@Override
public void run() {
Display display = Display.getDefault();
Monitor[] monitors = display.getMonitors();

Monitor lastMon = monitors[monitors.length - 1];
Rectangle bounds = lastMon.getBounds();

try {
IWorkbenchWindow window = Workbench.getInstance().openWorkbenchWindow("org.gamingclubnetwork.tournament.perspective.display", null);

Shell shell = window.getShell();
shell.setMenuBar(null);
shell.setLocation(bounds.x, bounds.y);
shell.setFullScreen(monitors.length > 1);
}
catch (WorkbenchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


First of all we look for all available monitors and grab the last one (I assumed the secondary display will be used for projectors or large monitors).
With that done, the trick is to open a new workbench window specifying a new perspective that contains just my GEF view. I then grab the shell of this workbench, remove the menu bar, set it's location to the top corner of the monitor to show it on and then set to full screen if not on the primary display.

Friday 24 October 2008

Geoff on GEF pt. III

I tracked down the reason for my figures not appearing to the layout used in my root figure. I was using the XYLayout from the example, changing it to the ToolbarLayout immediately produced the desired results. e.g.

@Override
protected IFigure createFigure() {
Figure f = new Figure();
f.setOpaque(true);
f.setLayoutManager(new ToolbarLayout());
return f;
}


I can now plough on and create edit parts and figures for the remaining components of my model.

One of the problems I encountered when using the ttolbar layout, is that shapes I have defined are not appearing as expected, is the short term I have replaced them with Labels containing just icons, but I think I will have to revisit this in the future.

Thursday 23 October 2008

Geoff on GEF pt. II

So, carrying straight on from last night, I fixed the null pointer and now have an empty view displaying. The trouble is, as soon as I plugged in the listeners and updated the children of the EditPart when the model changed, I still get an empty view.

After some investigation I discovered that the following code can be used in a ViewPart to display a GEF diagram.


@Override
public void createPartControl(Composite parent) {
DefaultEditDomain defaultEditDomain = new DefaultEditDomain(null);

sgv = new ScrollingGraphicalViewer();

defaultEditDomain.addViewer(sgv);
sgv.createControl(parent);
ScalableRootEditPart sffrep = new ScalableRootEditPart();
sgv.setRootEditPart(sffrep);
sgv.setEditPartFactory(new TournamentEditPartFactory());

sgv.getControl().setBackground(ColorConstants.listBackground);

initView();
}

private void initView() {
sgv.setContents(TournamentManager.get());
}


However, my diagram is still not displaying as expected...

Wednesday 22 October 2008

Geoff on GEF pt. I

It's been about 4 years since I last worked with the Graphical Editing Framework, or GEF, in eclipse. I have recently been working on a new application using eclipse RCP to manage tournaments (specifically wargaming events).
Anyway, I have a basic application up and running now and intend to add an additional feature to allow for real-time information to be displayed on a seperate screen (or projector). For this feature I've decided to use GEF once more.
As I said, it has been about 4 years since I last used GEF and I recall that it was a little tricky to get started with. This time around I have decided to document my experience as I develop this new functionality.

To get things started, I paid a visit to the GEF homepage and jumped straight into the tutorial referenced there. The tutorial starts by stating that an updated version was posted in March 2007, not a good sign if the homepage does not point to the more up-to-date documentation.
Skipping quickly over the first section, that explains the MVC architechture, I wrote a couple of EditParts as described, then decided to create a view to display them. Now, the tutorial only deals with creating an editor, not a view. So after some googling I found this post on creating a view for GEF.

One of the methods called is setEditPartFactory(), I know from experience that the Edit Part Factory is a core piece of functionality that creates controllers (EditParts) from your model. I was stunned to find that the Edit PartFactory was not described in the tutorial. I threw one together from memory (see below) to confirm that I could get a view to display.

On launching the app, the view displayed, but there was a null pointer when getting the children of my root object, not really a surprise as I had yet to populate it. Anyway, that was enough for one night. I shall continue to work on it tomorrow.

The content of my EditPartFactory (simply implements EditPartFactory):

public EditPart createEditPart(EditPart context, Object model) {
EditPart part = null;
if(model instanceof TournamentData)
{
part = new FloorplanEditPart();
}
else if(model instanceof Game)
{
part = new GameEditPart();
}

if(part != null)
{
part.setModel(model);
part.setParent(context);
}
return part;
}