Sizing the ScrollingGraphicalViewer Visible Area in the Eclipse GEF

I am trying to create a simple application using the Eclipse GEF that displays a chart inside the ScrollingGraphicalViewer. At startup, I want the chart to be centered inside the viewer (imagine a star model where the center of the star is in the center of the view).

Here's what I think of the relevant sections of code:

My opinion:

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());          
    }

Editing 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 part of the specified editing 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 are also welcomed; I am certainly a newbie to GEF (and Eclipse in general).

+3
source share
1 answer

Designed this for anyone interested:

        Dimension viewSize = (((FigureCanvas) getViewer().getControl()).getViewport().getSize());
+4
source

Source: https://habr.com/ru/post/1743938/


All Articles