Listening to SWT.OpenDocument event from within a plugin - eclipse

I have read lots of differemt documentation about the launcher.openFile feature of eclipse. They contain information about how to add a listener for the SWT.OpenDocument event to react on files that were opened from outside. This always includes adding the listener inside the Application class of the eclipse product.
What i want to do is to add such a listener from within my plugin where i don't have an Application class at all. Is this possible?

Related

Listener for all jface actions similar to IExecutionListener

In eclipse, there is a listener for any org.eclipse.core.command that is executed - org.eclipse.core.commands.IExecutionListener.
Is there such listener for jface action? I know you can add a property change listener to a jface action. But what I am looking for is a listener at framework level where it listens to all actions being executed in the application. It is probably possible to write such a listener, but is anything provided out of the box?
No, there is nothing that does this.
Lots of code in Eclipse calls the IAction run or runWithEvent methods directly without going through any centralised service so it is not possible to do this.

Azure DevOps extension using React: Can't stop navigating away from the page(beforeunload event)

The azure-devops-extension-sdk has events that can be listened to for work items like onLoaded, onRefreshed, onUnloaded etc. But there's no event to listen to while the web page itself is being refreshed(navigating away). I need to stop the navigation action in case there are unsaved changes in my custom UI element. Azure DevOps does this inherently only in those cases where a standard backend field is used. My custom UI element (installed using an extension I developed) doesn't use any backend fields.
window.addEventListener function doesn't seem to work and neither did the window.beforeunload function.
Seems you are using a hub extension. We could not use the window.beforeunload event of the browser which is not working in the case of an extension because it is running in an iframe.
After go through azure-devops-extension-api, didn't find any related interface. Afraid this is not available for a customized extension right now.

How to extend IDocumentListener in eclipse for creating plugin?

I am trying to create a plugin which monitors the change in a document.
I am interested in adding a marker in the text editor when the document is changed.
I observed that for the class - IDocumentListener the method documentChanged is getting called whenever there is a change.
However, I am unable to implement this as plugin as this interface does not have an extension point.
Can you help me with extending IDocumentListener ?
It's instructive to look at an existing open-source plugin to see how it does a similar task. Let's look at Bracketeer as an example.
The starting point is a class that implements org.eclipse.core.runtime.Plugin. For plugins that have a UI, it's useful to implement org.eclipse.ui.plugin.AbstractUIPlugin which provides additional functionality. This class is commonly called the "activator", and indeed in Bracketeer it's called Activator. It's registered as the plugin's activator class in the MANIFEST.MF file using a line like:
Bundle-Activator: com.chookapp.org.bracketeer.Activator
The Activator class overrides Plugin.start(), which will be called by the runtime when the plugin is loaded. The overridden start() method sets up a part listener.
The part listener is a class that implements the IPartListener2 interface. In Bracketeer, it's called PartListener. On setup, it calls PlatformUI.getWorkbench() to get a hold of the IWorkbench, and IWorkbench.getWorkbenchWindows() to get a list of currently open windows (at the time the plugin starts). It then registers itself with each window via IWorkbenchWindow.getPartService().addPartListener().
In addition, to handle new windows being opened after the plugin is loaded, PartListener also implements IWindowListener, and registers itself as a window listener via IWorkbench.addWindowListener(). This allows the PartListener to handle new windows opening by overriding IWindowListener.windowOpened(), and register itself as a part listener for the new windows.
As a part listener, PartListener overrides IPartListener2.partActivated() and partOpened() to handle workbench parts (which include editors) being opened or activated. In those methods, it checks whether the part is an IEditorPart; if so, it gets a hold of the editor part's document (see PartListener.getPartDocument()), which is an IDocument.
Finally, having an IDocument, it can register any IDocumentListener it wants via IDocument.addDocumentListener(IDocumentListener).
(There are some details I'm glossing over, such as manually calling partActivated() for every workbench part that's already open at the time the plugin is started. See the Bracketeer code for the full details.)
All of these are public APIs, and none of this requires any extension point to be implemented.

Eclipse RCP SourceProvider listening for changes

I have a source provider that helps to provide state for enabling buttons and menu items. The enabling part is working correctly. My challenge is how to get the source provider called when various editors / views are activated.
I have implemented IPartListener2 on the source provider but don't see a way to get it registered for all editors and views in a generic way. The getPartService().addPartListerner(this) will register it but only for a specific workbench part. Also the constructor for the Source Provider is called before any part is active so getting a valid part is not working.
What is the best way to register this Source Provider for all editors and views?
Thanks for reading my question and any assistance you can provide.
Use a IWindowListener to listen for workbench windows being activated:
IWorkbench workbench = PlatformUI.getWorkbench();
workbench.addWindowListener(windowListener);
In the windowActivated method of the listener use the window part service to add a part listener for the window:
public void windowActivated(IWorkbenchWindow window)
{
window.getPartService().addPartListener(partListener);
}
As an example see org.eclipse.jdt.internal.debug.ui.actions.ActionDelegateHelper

Plugin and dialog together

If I use a dialog to create an instance of an entity, will it fire the plugin registered to that message (i.e. Create on EntityA)?
If not, how can I link that plugin to that message?
Yes, the plugin will always fire regardless of the source of the message.
You're safe to use the plugin. It will catch the dispatched message as long as you've registered it correctly.
The plugin will, in fact, react to the following events.
Creation of an instance (a record) by the user from the GUI.
Creation of an instance by a workflow.
Creation of an instance by a dialog.
Creation of an instance by a code from an extern program.
Creation of an instance by... anything.