Page 1 of 1

file close listener with extension bundle

Posted: Tue Jan 24, 2017 3:07 pm
by NissenJ
Hi!

I am trying to implement a file save listener and a file close listener. For the file save listener, i found this solution:

Code: Select all

                  																									authorAccess.getWorkspaceAccess().getEditorAccess(authorAccess.getEditorAccess().getEditorLocation()).addEditorListener(new WSEditorListener() {
@Override
public void editorSaved(int arg0) {
super.editorSaved(arg0);
logger.info("File saved!");
}
});
For the file close listener there seems to be an equivalent solution using an EditorChangeListener, but this can only be added to a PluginWorkspace. Is there a way to do this with an extension bundle without a plugin?

Thanks and Regards,
Julia

Re: file close listener with extension bundle

Posted: Tue Jan 24, 2017 4:25 pm
by Radu
Hi Julia,

Just to see if I understand the use case, you want to do a specific thing when the current opened XML file is closed, right?
Could you elaborate on what you want to do? I could consider adding some more API for this.

Anyway, in any place of the code you can use static access to the entire workspace like:

Code: Select all

PluginWorkspaceProvider.getPluginWorkspace().addEditorChangeListener(....., PluginWorkspace.MAIN_EDITING_AREA);
but ideally if you add a listener from an opened editor, when the XML editor is closed you should remove the listener, otherwise the main plugin workspace will keep a reference to the listener which will keep a reference to the opened editor, so when you will close the editor the Java garbage collector will no longer de-allocate the memory used by it.
Something like:

Code: Select all

  WSEditorChangeListener editorListener = null;

private addTheListener() {
editorListener = new WSEditorChangeListener(){
/**
* @see ro.sync.exml.workspace.api.listeners.WSEditorChangeListener#editorClosed(java.net.URL)
*/
@Override
public void editorClosed(URL editorLocation) {
//Do something here...
//And remove the listener
PluginWorkspaceProvider.getPluginWorkspace().removeEditorChangeListener(editorListener, PluginWorkspace.MAIN_EDITING_AREA);
}
};
PluginWorkspaceProvider.getPluginWorkspace().addEditorChangeListener(editorListener, PluginWorkspace.MAIN_EDITING_AREA);
}
Or you can create a plugin for Oxygen (a Workspace Access plugin) which adds the editor listener and does various things. And the end user would need to install both the plugin and the custom framework.

Regards,
Radu

Re: file close listener with extension bundle

Posted: Tue Jan 24, 2017 5:22 pm
by NissenJ
Hi Radu!

Thanks for your reply, this should work for me. I am caching some information for every open file and I would like to clear the cache if the file is closed, just as I refresh the cache when the file is saved.

Regards,
Julia

Re: file close listener with extension bundle

Posted: Tue Jan 24, 2017 5:34 pm
by Radu
Hi Julia,

Thanks for the details, I added an issue on our side, maybe in a future version we can add an "editorClosed" callback API directly in the listener WSEditorListener that you were originally trying to use for this purpose.

Regards,
Radu

Re: file close listener with extension bundle

Posted: Tue Jan 24, 2017 5:43 pm
by NissenJ
Hi Radu,

that sounds great, thank you!

Regards,
Julia

Re: file close listener with extension bundle

Posted: Mon Jul 31, 2017 4:49 pm
by NissenJ
Hi Radu,

I am currently facing one problem with this file save listener. The EditorListener only works for files opened in author mode, since it needs the AuthorAccess, right? But I often open, edit and then save files in text mode and I would like to have my functionality for that case, too.

How can I achieve this through my extension bundle?

Regards,
Julia

Re: file close listener with extension bundle

Posted: Tue Aug 01, 2017 12:15 pm
by alex_jitianu
Hi Julia,

My colleague Radu has a few days off so I will help you while he's away. The plugin-level API works for the Text page as well. Have you decided to create an WorkspaceAccess plugin or are you still using this API from the framework-level API, meaning the ExtensionsBundle? If you are still using the ExtensionsBundle then I guess you are adding the listener from an AuthorExtensionStateListener? If that's the case then, indeed this AuthorExtensionStateListener is only notified when an editor is opened in the AuthorPage.

An WorkspaceAccess plugin is activated automatically on Oxygen's startup and in it you can listen for any editor open event (in any page it might be):

Code: Select all

  public void applicationStarted(final StandalonePluginWorkspace pluginWorkspaceAccess) {

pluginWorkspaceAccess.addEditorChangeListener(new WSEditorChangeListener() {
@Override
public void editorOpened(URL editorLocation) {
WSEditor editorAccess = pluginWorkspaceAccess.getEditorAccess(editorLocation, PluginWorkspace.MAIN_EDITING_AREA);
editorAccess.addEditorListener(new WSEditorListener() {
@Override
public void editorSaved(int operationType) {
// TODO Save event.
}
});
}

@Override
public void editorClosed(URL editorLocation) {
// TODO Save event.
WSEditor editorAccess = pluginWorkspaceAccess.getEditorAccess(editorLocation, PluginWorkspace.MAIN_EDITING_AREA);
}
}, PluginWorkspace.MAIN_EDITING_AREA);
So the bottom line is that if you create such a plugin you will definitely not miss any events.

Best regards,
Alex

Re: file close listener with extension bundle

Posted: Tue Aug 01, 2017 12:26 pm
by NissenJ
Hi Alex,

thank you for your reply! I am still using only the ExtensionBundle, but it looks like I am going to create a plugin now.

Thanks and Regards,
Julia

Re: file close listener with extension bundle

Posted: Mon Oct 02, 2017 10:32 am
by sorin_carbunaru
Hello there,

Just wanted to announce that oXygen 19.1 is out and our API now has a method that gets notified when an editor in the stand-alone application is about to be closed. See ro.sync.exml.workspace.api.listeners.WSEditorListener.editorAboutToBeClosedVeto(URL).

All the best wishes,
Sorin Carbunaru
oXygen XML