0

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?

Alex
  • 2,126
  • 3
  • 25
  • 47
  • 3
    You're registering an early startup plugin - there's no guarantee that your `earlyStartup()` method will be called when the workbench is open or has an active page. You could schedule a new `Job` - the `JobManager` will be started as soon as the workbench has opened, and you can do your work in there, which should guarantee that the UI is running when you are called. – Edward Thomson Mar 05 '12 at 22:51
  • I am doing the same thing but with a `job` and now I get this error: `An internal error occurred during: "Visits: Populate menu". No context available outside of the request service lifecycle.` – Alex Mar 06 '12 at 09:59
  • 1
    See the RAP FAQ -> http://wiki.eclipse.org/RAP/FAQ#No_context_available_outside_of_the_request_service_lifecycle – Tom Seidel Mar 06 '12 at 10:07
  • can you give me an example? I can't find any... – Alex Mar 06 '12 at 11:08
  • @Tom Seidel not working even with that solution – Alex Mar 06 '12 at 11:31
  • Alex: if you've got a solution, you should write this as an answer then accept it for future visitors. – Edward Thomson Mar 06 '12 at 15:53

1 Answers1

2

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

            }

        });

    }

}
Alex
  • 2,126
  • 3
  • 25
  • 47