代码之家  ›  专栏  ›  技术社区  ›  Simon

在Eclipse中确定滚动图形浏览器可见区域的大小

  •  3
  • Simon  · 技术社区  · 16 年前

    我正在尝试使用eclipsegef创建一个简单的应用程序,它在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)新手。

    1 回复  |  直到 12 年前
        1
  •  4
  •   Simon    16 年前

    为所有感兴趣的人解决了这个问题:

            Dimension viewSize = (((FigureCanvas) getViewer().getControl()).getViewport().getSize());
    
    推荐文章