Automatically generate UUID filename when saving

Post here questions and problems related to oXygen frameworks/document types.
Rodrik
Posts: 30
Joined: Mon Jan 27, 2014 11:19 am

Automatically generate UUID filename when saving

Post by Rodrik »

Is it possible using the SDK to get Oxygen Author to automatically generate a filename comprising a UUID plus file extension?

Regards

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

Re: Automatically generate UUID filename when saving

Post by Radu »

Hi Rodrik,

This could be done implementing a plugin for Oxygen using our Plugins SDK:

http://www.oxygenxml.com/oxygen_sdk.htm ... er_Plugins

We have a type of plugin called "Workspace Access", there is a sample implementation + Java sources in the Plugins SDK.

Such a Java-based plugin would basically intercept the save events when a newly created document is untitled and show an alternative chooser dialog to the user, then save the topic with the proper name, the Java code for something like this would be like:

Code: Select all

  private static class CustomEdListener extends WSEditorListener{
private final WSEditor editor;
private final StandalonePluginWorkspace pluginWorkspaceAccess;
private boolean saving = false;
public CustomEdListener(StandalonePluginWorkspace pluginWorkspaceAccess, WSEditor editor) {
this.pluginWorkspaceAccess = pluginWorkspaceAccess;
this.editor = editor;
}
/**
* @see ro.sync.exml.workspace.api.listeners.WSEditorListener#editorAboutToBeSavedVeto(int)
*/
@Override
public boolean editorAboutToBeSavedVeto(int operationType) {
if(! saving && editor.getEditorLocation().toString().contains("Untitled")) {
File chosenDir = pluginWorkspaceAccess.chooseDirectory();
if(chosenDir != null) {
final File chosenFile = new File(chosenDir, UUID.randomUUID().toString() + ".dita");
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
saving = true;
editor.saveAs(new URL(chosenFile.toURI().toASCIIString()));
} catch (MalformedURLException e) {
e.printStackTrace();
} finally {
saving = false;
}
}
});
}
//Reject the original save request.
return false;
}
return true;
}
}

/**
* @see ro.sync.exml.plugin.workspace.WorkspaceAccessPluginExtension#applicationStarted(ro.sync.exml.workspace.api.standalone.StandalonePluginWorkspace)
*/
@Override
public void applicationStarted(final StandalonePluginWorkspace pluginWorkspaceAccess) {
pluginWorkspaceAccess.addEditorChangeListener(new WSEditorChangeListener() {
/**
* @see ro.sync.exml.workspace.api.listeners.WSEditorChangeListener#editorOpened(java.net.URL)
*/
@Override
public void editorOpened(URL editorLocation) {
final WSEditor editor = pluginWorkspaceAccess.getEditorAccess(editorLocation, PluginWorkspace.MAIN_EDITING_AREA);
if(editor != null && editor.getEditorLocation().toString().contains("Untitled")) {
//Untitled editor
editor.addEditorListener(new CustomEdListener(pluginWorkspaceAccess, editor));
}
}
}, PluginWorkspace.MAIN_EDITING_AREA);
................................................
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Rodrik
Posts: 30
Joined: Mon Jan 27, 2014 11:19 am

Re: Automatically generate UUID filename when saving

Post by Rodrik »

Hi Radu

Thank you for your detailed reply. I've passed your response on to one of my developer colleagues.

Regards

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

Re: Automatically generate UUID filename when saving

Post by Radu »

Hi,

Just to update this thread, the API ro.sync.exml.workspace.api.editor.WSEditorBase.isNewDocument() is a more elegant approach that can be used to check if a document is a newly created document or not.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Post Reply