0

I've got a tree in a view builded with Common Navigation Framework. I've got a custom item MyTreeEntry that contains an IFolder object.

public class MyTreeEntry implements IWorkbenchAdapter {
   private IFolder iFolder;
[...]
}

I want to see for MyTreeEntry item all menu entries as IFolder (New, Open in new window...) and when an action is invoked, it must act on IFolder contained.

Is it possible and how?

Tommaso DS
  • 211
  • 2
  • 14

1 Answers1

1

You should be able to do this using the platform adapter manager to tell Eclipse how to get a folder from your object:

Create an IAdapterFactory for your class:

class MyTreeEntryAdapterFactory implements IAdapterFactory
{
  @Override
  public Object getAdapter(Object adaptableObject, Class adapterType)
  {
    if (!(adaptableObject instanceof MyTreeEntry))
      return null;

    if (adapterType == IResource.class || adapterType == IFolder.class)
      return ((MyTreeEntry)adaptableObject).getFolder();

    return null;
  }

  public Class<?> [] getAdapterList()
  {
    return new Class<?> [] {IResource.class, IFolder.class};
  }
}

register your adapter factory with the adapter manager:

Platform.getAdapterManager().registerAdapters(adapterFactory, MyTreeEntry.class);

You probably need the adapters for IResource as well as IFolder.

Note: The IAdaptable interface provides very similar support but the Eclipse menu system requires the IAdapterFactory.

greg-449
  • 109,219
  • 232
  • 102
  • 145
  • This is good trick. I've tryed it but the only menu that adds is "Properties". New, Go into, Refresh... aren't visible yet. – Tommaso DS Jul 07 '14 at 12:29
  • Exactly which menu items show up depends on how they are declared. Some are only declared for a specific view and for those you will have to add your own declaration and some code to your view. – greg-449 Jul 07 '14 at 13:08
  • You might want to look at the source of the Project Explorer in the `org.eclipse.ui.navigator.resources` plugin which is based on Common Navigator and has the menus you want. – greg-449 Jul 07 '14 at 13:28
  • You're right, but my view extends PackageExplorerPart and has those menus. If I try to right click on Project or Folder I'll see all Eclipse standard menus. If I try to right click on MyTreeEntry I see only Cut, Paste, Import, Export, Show In, Delete, Build Path and, with your code, Properties. – Tommaso DS Jul 07 '14 at 13:57
  • PackageExplorerPart is in an **internal** package and as such [should not be extended](http://www.eclipse.org/articles/Article-API-Use/index.html) You are going to have to look at the source to see what it expects. – greg-449 Jul 07 '14 at 14:05
  • This is not my code, I know it is a very bad practice to use internal object. But I have to do this issue on this code, even though is bad. – Tommaso DS Jul 07 '14 at 14:23