简体   繁体   中英

Eclipse RCP, RAP, how to populate tree from another view on plugin loaded

I want to make a dynamic application and I want to add TreeItem 's in a Tree when another plugin is loaded.

For example:

  1. I have plugin: com.project.startup and this plugin has a View with a Tree inside it. This
  2. I have plugin: com.project.populator . I want to populate the Tree from com.project.startup when com.project.populator is starting

I know how to add items but I don't know when and where I have to write my code.

So far I did this using org.eclipse.ui.IStartup but I get this error:

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

My code looks like this:

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

I think the TreeViewer is not initialized.. but it is in createPartControl of ViewMenuOffers .

How can I make this work? How can I add something to a View from another plugin onStartup?

I found an answer. It can be done like this:

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");

            }

        });

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM