Set filename and save folder via plugin or framework

Post here questions and problems related to oXygen frameworks/document types.
AndreasM
Posts: 9
Joined: Fri Nov 02, 2018 12:31 pm

Set filename and save folder via plugin or framework

Post by AndreasM » Fri Jan 11, 2019 5:09 pm

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!

Radu
Posts: 6141
Joined: Fri Jul 09, 2004 5:18 pm

Re: Set filename and save folder via plugin or framework

Post by Radu » Mon Jan 14, 2019 9:17 am

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
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com

AndreasM
Posts: 9
Joined: Fri Nov 02, 2018 12:31 pm

Re: Set filename and save folder via plugin or framework

Post by AndreasM » Thu Jan 17, 2019 4:55 pm

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)

Radu
Posts: 6141
Joined: Fri Jul 09, 2004 5:18 pm

Re: Set filename and save folder via plugin or framework

Post by Radu » Thu Jan 17, 2019 5:03 pm

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
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com

AndreasM
Posts: 9
Joined: Fri Nov 02, 2018 12:31 pm

Re: Set filename and save folder via plugin or framework

Post by AndreasM » Thu Jan 17, 2019 5:27 pm

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);
}

Radu
Posts: 6141
Joined: Fri Jul 09, 2004 5:18 pm

Re: Set filename and save folder via plugin or framework

Post by Radu » Fri Jan 18, 2019 1:46 pm

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
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com

AndreasM
Posts: 9
Joined: Fri Nov 02, 2018 12:31 pm

Re: Set filename and save folder via plugin or framework

Post by AndreasM » Fri Jan 18, 2019 4:11 pm

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)

Radu
Posts: 6141
Joined: Fri Jul 09, 2004 5:18 pm

Re: Set filename and save folder via plugin or framework

Post by Radu » Fri Jan 18, 2019 4:45 pm

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
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com

Post Reply