Eclipse RCP,RAP,如何从插件加载的另一个视图中填充树

时间:2012-03-05 21:56:10

标签: java eclipse-plugin eclipse-rcp eclipse-rap

我想创建一个动态应用程序,并且我希望在加载另一个插件时在TreeItem中添加Tree

例如:

  1. 我有插件:com.project.startup,此插件中有一个View,其中包含Tree。这个
  2. 我有插件:com.project.populator。我想在Tree开始时com.project.startup填充com.project.populator
  3. 我知道如何添加项目,但我不知道何时何地编写代码。

    到目前为止,我使用org.eclipse.ui.IStartup执行此操作,但我收到此错误:

    !MESSAGE Unable to execute early startup code for an extension
    !STACK 0
    java.lang.NullPointerException
        at ro.project.populator.TreePopulator.earlyStartup(TreePopulator.java:18)
    

    我的代码如下所示:

    public class TreePopulator implements IStartup
    {
    
        @Override
        public void earlyStartup()
        {
            ViewMenuOffers viewMenuOffers = (ViewMenuOffers) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(ViewMenuOffers.ID);
            TreeViewer treeViewer = viewMenuOffers.getTreeViewer();
            Tree tree = treeViewer.getTree();
            TreeItem trtmItem = new TreeItem(tree, SWT.NONE);
            trtmItem.setText("Item 1");
        }
    }
    

    我认为TreeViewer未初始化..但它位于createPartControl的{​​{1}}。

    我该如何使这项工作?如何从另一个插件onStartup中向ViewMenuOffers添加内容?

1 个答案:

答案 0 :(得分:2)

我找到了答案。可以这样做:

public class TreePopulator implements IStartup
{

    @Override
    public void earlyStartup()
    {
        PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {

            @Override
            public void run()
            {
                ViewMenuOffers viewMenuOffers = (ViewMenuOffers) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(ViewMenuOffers.ID);
                TreeViewer treeViewer = viewMenuOffers.getTreeViewer();
                Tree tree = treeViewer.getTree();

                TreeItem trtmS = new TreeItem(tree, SWT.NONE);
                trtmS.setText("Test");

            }

        });

    }

}