0

在 Eclipse RCP 应用程序中共享轻量级视图模型或数据上下文对象的最佳实践是什么,因为我无法控制视图创建?对于这种轻量级的事情,我宁愿不使用 OSGi 服务或静态变量。

假设我有一个 MapView,它将在其构造函数中创建其 MapViewController 的实例。

public class MapView extends ViewPart  {

    public static final String ID = "myapp.mapview";

    MapViewController controller = null;        

    public MapView() {      

          LegendContainer legends = new LegendContainer();
          MapViewModel viewModel = new MapViewModel();
          controller = new MapViewController(null, legends, viewModel);         
    }

    @Override
    public void createPartControl(Composite parent) {
           // create main map 


        }
}

MapViewModel有一个名为 Legends 的属性,需要通过TableOfContentsView( ) 访问,如果创建为in的子级TocView,这很简单TocViewMapViewMapView.createPartControl()

@Override
public void createPartControl(Composite parent) {
    // pass model to child here when creating it
}                       

如果我想在透视图中作为独立视图存在TocViewMapView添加到平台中,MapPerspective.createInitialLayout如下所示:

public class MapPerspective implements IPerspectiveFactory {

@Override
    public void createInitialLayout(IPageLayout layout) {           

        String editorArea = layout.getEditorArea();             
        layout.setEditorAreaVisible(false);        

        IFolderLayout main = layout.createFolder("right", IPageLayout.RIGHT, 0.2f, editorArea); 
        IFolderLayout top = layout.createFolder("top", IPageLayout.TOP, 0.5f, editorArea); 
        IFolderLayout bottom = layout.createFolder("bottom", IPageLayout.BOTTOM, 0.5f, editorArea); 



        // how do I share stuff???              
        main.addView(MapView.ID);              
        layout.getViewLayout(MapView.ID).setCloseable(false);

        top.addView(TableOfContentsView.ID);
        layout.getViewLayout(TableOfContentsView.ID).setCloseable(false);

        bottom.addView(PropertiesView.ID);
        layout.getViewLayout(PropertiesView.ID).setCloseable(false);

如何传递需要共享的视图模型或数据上下文?

4

0 回答 0