Kirill Kulakov
Kirill Kulakov

Reputation: 10255

Using the ActionProvider of a File - Eclipse CNF

I'm writing a plugin which creates a custom Package Explorer that represent the underlying file system hierarchy in a different hierarchy (some sort of virtual folders).

I've managed to create the hierarchy by implementing ITreeContentProvider.The final node should represent a file with all its actions (open, copy, rename...), it works great when I return an Instance of org.eclipse.core.internal.resources.File but if I create a delegate/proxy class which implements the IFile interface I see the action but when I click on one of them Eclipse freezes.

Anyway, I guess I'll need to implement my own CommonActionProvider. my question is: How can I add the same action to a node, that will represent actions on file, is there some FileActionProvider?

enter image description here

I'm following the following tutorial: tutorial

UPDATE

I've tried messing with the adapters as @greg-449, suggested.However none of them gave me the full menu, the biggest menu I got was but implementing the IAdaptable interface and delegating the getAdapter() to the instance of my IFile. but doing so the double click to open file is not working, and most important the rename,delete,move are not here

menu I got by delegating the getAdapter

menu I got by delegating the getAdapter()

Upvotes: 1

Views: 161

Answers (1)

greg-449
greg-449

Reputation: 111216

Rather than trying to implement IFile you should use an IAdapterFactory to provide an adapter from your UI class to the underlying IFile.

Declare the adapter factory using the org.eclipse.core.runtime.adapters extension point:

 <extension point="org.eclipse.core.runtime.adapters">
     <factory 
        class="com.xyz.MyFileAdapterFactory" 
        adaptableType="com.xyz.MyFile">
        <adapter type="org.eclipse.core.resources.IFile"/>
     </factory>
  </extension>

The adapter factory class methods would look something like:

@Override
public Object getAdapter(Object adaptableObject, Class adapterType)
{
  if (adaptableObject instanceof MyFile)
   {
     // TODO get the IFile from MyFile
     return ifile;
   }

  return null;
}

@Override
public Class<?> [] getAdapterList()
{
  return new  Class<?> [] {IFile.class};
}

You might also need to register for IResource to get all menu items working.

You can also register the factory using the IAdapterManager interface.

Upvotes: 1

Related Questions