我正在尝试使用Eclipse GEF创建一个简单的应用程序,该应用程序在ScrollingGraphicalViewer中显示一个图表。在启动时,我希望图表在查看器中居中(想象一下星形布局,其中恒星的中心位于视图的中心)。
以下是我认为的相关代码部分:
我的观点:
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());
}
编辑部分代码:
@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 {
...
}
}
以上编辑部分的父级:
public class ProjectDependencyModelEditPart extends AbstractGraphicalEditPart {
@Override
protected IFigure createFigure() {
Figure f = new FreeformLayer();
f.setLayoutManager(new XYLayout());
return f;
}
...
该问题的替代解决方案也受到欢迎,我肯定是GEF(和Eclipse一般)新手。
答案 0 :(得分:4)
为所有感兴趣的人做了一件事:
Dimension viewSize = (((FigureCanvas) getViewer().getControl()).getViewport().getSize());