'Eclipse Plugin Development - Detect/Listen File open in editor

I need to detect when a file (of any type) is opened in Eclipse and run some code when that happens.

I've tried with the following code but it seems to be calling the function multiple times:

Display.getDefault().asyncExec(new Runnable() {
    @Override
    public void run() {
        PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService().addPartListener(new IPartListener2() {
            @Override
            public void partOpened(IWorkbenchPartReference partRef) {
                System.out.println("File opened");
            }
            
        });
    }
});

Is there a way to do this in Eclipse RCP?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source