views:

122

answers:

2

I am attempting to create a simple application using Eclipse GEF that displays a diagram inside a ScrollingGraphicalViewer. On start-up I want the diagram to be centered inside the viewer (imagine a star layout where the center of the star is in the center of the view).

Here are what I think are the relevant code sections:

My view:

public class View extends ViewPart {
    public static final String ID = "GEFProofOfConcept.view";

...         

    public void createPartControl(Composite parent) {
        viewer.createControl(parent);

        viewer.setRootEditPart(rootEditPart);
        viewer.setEditPartFactory(editPartFactory);

        viewer.setContents(DummyModelCreator.generateModel());          
    }

The edit part code:

@Override
protected void refreshVisuals() {
    Project project = (Project) getModel();

    // This is where the actual drawing is done,
    // Simply a rectangle with text
    Rectangle bounds = new Rectangle(50, 50, 75, 50);
    getFigure().setBounds(bounds);
    Label label = new Label(project.getName());
    projectFont = new Font(null, "Arial", 6, SWT.NORMAL);
    label.setFont(projectFont);
    label.setTextAlignment(PositionConstants.CENTER);
    label.setBounds(bounds.crop(IFigure.NO_INSETS));

    getFigure().add(label);

    setLocation();
}

private void setLocation() {
    Project project = (Project) getModel();

    if (project.isRoot()) {
        // Place in centre of the layout
        Point centrePoint = new Point(0, 0); // This is where I need the center of the view
        getFigure().setLocation(centrePoint);
    } else {
        ...
    }       
}

And the parent of the above edit part:

public class ProjectDependencyModelEditPart extends AbstractGraphicalEditPart {

@Override
protected IFigure createFigure() {
    Figure f = new FreeformLayer();
    f.setLayoutManager(new XYLayout());

    return f;
}
...

Alternative solutions to the problem also welcome, I am most certainly a GEF (and Eclipse in general) newbie.

A: 

Worked it out, for anyone who's interested:

        Dimension viewSize = (((FigureCanvas) getViewer().getControl()).getViewport().getSize());
Simon
A: 

I tried your solution but get "Dimension(0, 0)". Any idea why?

Punit
I believe that will happen if the viewer hasn't been drawn yet. My code sits inside a resize method that is called by the layout manager. I think it should probably work inside refreshVisuals() as well but I haven't tested this.
Simon