Varun Raval
Varun Raval

Reputation: 470

Creating decorators, how to get info of file in eclipse plugin?

I am trying to make a plugin such that whenever a new specific file is created, having certain properties, such as derived from file contents, then that type of decorator is shown.

I know how to make a basic decorator by extending org.eclipse.ui.decorator extension.

But, how to know the info regarding file such as file name and extension before applying a descriptor? I know little that it can be done using Object element parameter in decorate() method of file implementing ILightWeightLabelDecorator interface. But I don't know how to use this element object?

Whenever a new file is added to package explorer, then will the decorators be shown by itself or the workbench needs to be refreshed?

I am using Eclipse 4.5.2

Upvotes: 0

Views: 113

Answers (1)

greg-449
greg-449

Reputation: 111142

The enablement element of your decorator declaration in the plugin.xml says what sort of objects the decorator is enabled for and determines the type of object passed to the decorator.

So if you have:

    <enablement>
        <objectClass name="org.eclipse.core.resources.IFile"/> 
    </enablement>

The element object on the

@Override
public void decorate(final Object element, final IDecoration decoration)

call will be an IFile. So you just need to do:

IFile file = (IFile)element;

or to be absolutely sure you deal with adaptble objects use:

IFile file = (IFile)Platform.getAdapterManager().getAdapter(element, IFile.class);

IFile has methods to tell you all about the file.

Upvotes: 1

Related Questions