Page 1 of 1

Set filename and save folder via plugin or framework

Posted: Fri Jan 11, 2019 5:09 pm
by AndreasM
Hello again,
is there a way, when saving a file, to auto generate the filename (based on an ID value within the document) and set the save location to a predetermined folder on server filesystem.
I figured out how to inject code using the saveListener but have not found ways to manipulate the filename or savelocation.
Thank you!

Re: Set filename and save folder via plugin or framework

Posted: Mon Jan 14, 2019 9:17 am
by Radu
Hi Andreas,

How about if on this callback:

Code: Select all

ro.sync.exml.workspace.api.listeners.WSEditorListener.editorAboutToBeSavedVeto(int)
you sometimes return "false" to cancel the save operation and then maybe call:

Code: Select all

ro.sync.exml.workspace.api.editor.WSEditorBase.saveAs(URL)
? Calling "saveAs" might also call the "editorAboutToBeSavedVeto" again so you need to make sure you do not end up in a loop.

Regards,
Radu

Re: Set filename and save folder via plugin or framework

Posted: Thu Jan 17, 2019 4:55 pm
by AndreasM
Hi Radu
thanks, yes that was the direction in which I was thinking.
I forgot to mention that I'm using the a JS plugin to achieve this.

Here's a snippet of my current code, the message "in save event" is displayed as expected but I get the error "Cannot find function saveAs in object 0"

Code: Select all


 editor = pluginWorkspaceAccess.getEditorAccess(editorLocation, Packages.ro.sync.exml.workspace.api.PluginWorkspace.MAIN_EDITING_AREA);
preSaveListener = {
editorAboutToBeSavedVeto: function(editor) {
javax.swing.JOptionPane.showMessageDialog(null, "in save event");
chosenURL = "[some url]";
editor.saveAs(chosenURL);
return false
I have the feeling that I'm overlooking something really obvious here. "editor" as a WSEditor should have inherited the "saveAs" method from WSEditorBase (https://www.oxygenxml.com/InstData/Edit ... ditor.html)

Re: Set filename and save folder via plugin or framework

Posted: Thu Jan 17, 2019 5:03 pm
by Radu
Hi Andreas,

Please give me more sample code from your side, especially about how you add the "preSaveListener" listener to the WSEditor using the API "ro.sync.exml.workspace.api.editor.WSEditor.addEditorListener(WSEditorListener)".
Tomorrow I can try to create a sample minimal plugin on my side to see if this works.

Regards,
Radu

Re: Set filename and save folder via plugin or framework

Posted: Thu Jan 17, 2019 5:27 pm
by AndreasM
This is the entire wsAccess.js right now.
When I return "true" in the preSaveListener, both event listeners work as expected showing my debug messages just before/after the save.

Code: Select all


function applicationStarted(pluginWorkspaceAccess) {
Packages.java.lang.System.err.println("Application started " + pluginWorkspaceAccess);
edChangedListener = {
/*Called when a document is Opened*/
editorOpened: function (editorLocation) {
Packages.java.lang.System.err.println("\nrunning " + editorLocation);
/*Get the opened editor*/
editor = pluginWorkspaceAccess.getEditorAccess(editorLocation, Packages.ro.sync.exml.workspace.api.PluginWorkspace.MAIN_EDITING_AREA);

preSaveListener = {
/* Called when a document is about to be Saved */
editorAboutToBeSavedVeto: function(editor) {
javax.swing.JOptionPane.showMessageDialog(null, "in save event");
chosenURL = "[some url]";
editor.saveAs(chosenURL);
return false



}

}

postSaveListener = {
/* Called when a document is Saved */
editorSaved: function(editor) {

javax.swing.JOptionPane.showMessageDialog(null, "gespeichert")

}

}
preSaveListener = new JavaAdapter(Packages.ro.sync.exml.workspace.api.listeners.WSEditorListener, preSaveListener);
postSaveListener = new JavaAdapter(Packages.ro.sync.exml.workspace.api.listeners.WSEditorListener, postSaveListener);

if (editor !=0) {
editor.addEditorListener(preSaveListener)
editor.addEditorListener(postSaveListener)
}


}
}
edChangedListener = new JavaAdapter(Packages.ro.sync.exml.workspace.api.listeners.WSEditorChangeListener, edChangedListener);

pluginWorkspaceAccess.addEditorChangeListener(
edChangedListener,
Packages.ro.sync.exml.workspace.api.PluginWorkspace.MAIN_EDITING_AREA);
}


function applicationClosing(pluginWorkspaceAccess) {
Packages.java.lang.System.err.println("Application closing " + pluginWorkspaceAccess);
}

Re: Set filename and save folder via plugin or framework

Posted: Fri Jan 18, 2019 1:46 pm
by Radu
Hi Andreas,

The "saveAs" takes an URL as a parameter:

https://www.oxygenxml.com/InstData/Edit ... rBase.html

So you may need to rewrite the code to:

Code: Select all

chosenURL =  new Packages.java.net.URL("[some url]");
Regards,
Radu

Re: Set filename and save folder via plugin or framework

Posted: Fri Jan 18, 2019 4:11 pm
by AndreasM
Hi Radu
That's a good hint. Thanks.
Even with that, my code returned "TypeError: Cannot find function saveAs in object 0." in the above configuration.

Today I rewrote the preSaveListener and made some progress I think...

Code: Select all


 preSaveListener = {
/* Called when a document is about to be Saved */

editorAboutToBeSavedVeto: function(editor) {
javax.swing.JOptionPane.showMessageDialog(null, "in save event");
currentEditor = new JavaAdapter( Packages.ro.sync.exml.workspace.api.editor.WSEditor, editorLocation)
chosenURL = new Packages.java.net.URL("file:/C/testsave/test.xml");
currentEditor.saveAs(chosenURL);
return false
}
This fixes the TypeError. "saveAs" gets called now, but it does not actually save the file. (also tested with other URL without success)

Re: Set filename and save folder via plugin or framework

Posted: Fri Jan 18, 2019 4:45 pm
by Radu
Hi Andreas,

I'm attaching a test code which works on my side:

Code: Select all

function applicationStarted(pluginWorkspaceAccess) {
Packages.java.lang.System.err.println("Application started " + pluginWorkspaceAccess);
var edChangedListener = {
/*Called when a document is opened*/
/*See: https://www.oxygenxml.com/InstData/Editor/SDK/javadoc/ro/sync/exml/plugin/workspace/WorkspaceAccessPluginExtension.html */
editorOpened: function (editorLocation) {
Packages.java.lang.System.err.println("\nrunning " + editorLocation);
var editor = pluginWorkspaceAccess.getEditorAccess(editorLocation, Packages.ro.sync.exml.workspace.api.PluginWorkspace.MAIN_EDITING_AREA);
preSaveListener = {
/* Called when a document is about to be Saved */
editorAboutToBeSavedVeto: function(operationType) {
if(operationType == Packages.ro.sync.exml.workspace.api.listeners.WSEditorListener.SAVE_OPERATION){
chosenURL = new Packages.java.net.URL("file:/D:/testsave/test.xml");
javax.swing.JOptionPane.showMessageDialog(null, "in save event");
editor.saveAs(chosenURL);
return false;
}
return true;
}
}
preSaveListener = new JavaAdapter(Packages.ro.sync.exml.workspace.api.listeners.WSEditorListener, preSaveListener);
if (editor !=0) {
editor.addEditorListener(preSaveListener)
}
}
}
var edChangedListenerAdapter = new JavaAdapter(Packages.ro.sync.exml.workspace.api.listeners.WSEditorChangeListener, edChangedListener);
/* Add the editor changed listener */
pluginWorkspaceAccess.addEditorChangeListener(
edChangedListenerAdapter,
Packages.ro.sync.exml.workspace.api.PluginWorkspace.MAIN_EDITING_AREA);
}
as a difference from what you did, if you look at the Java code for the method:

https://www.oxygenxml.com/InstData/Edit ... dVeto-int-

The parameter is "operationType" and I used it to perform the save-as only on the callback for the save operation.

Regards,
Radu