2

I am currently working on a Eclipse Plugin, where I need to make an action, when a person opens a file with certain properties. However I'm not sure on how to set a listener, I have been looking into the IWorkspace and IResource API, but I can't find the simple API call saying "AddListenerToOnOpenFile".

The file is expected to be opened in the package explorer view.

Kai
  • 38,985
  • 14
  • 88
  • 103
StefanE
  • 817
  • 6
  • 20

2 Answers2

3

Use the answer supplied by @MarttiKäärik to find out when editors are open. Then you can use the IEditorInput to see if it is an IResource you care about.

if (part instanceof IEditorPart) {
    IEditorPart editor = (IEditorPart) part;
    IResource resource = editor.getEditorInput().getAdapter(IResource.class);
    // ...
}
Paul Webster
  • 10,614
  • 1
  • 25
  • 32
2

Question already answered, so only to make it a bit more complete...

You don't necessarily have to implement a view or action (as described in the question linked to by Martti Käärik in a comment) to get a window for your listener. Call to PlatformUI.getWorkbench().get...() can be used as well. See the older, probably duplicate, question called just Eclipse Plugin.

BTW Eclipse Wiki FAQ page contains a good description of the ways how to obtain the current workbench window and possible "gotchas".

Moreover, you can even listen for newly opened windows if there is a need:

PlatformUI.getWorkbench().addWindowListener(listener);
Community
  • 1
  • 1
Petr Bodnár
  • 496
  • 3
  • 14