DITAMAPS Manager custom insert action showing prompt to discard or load changes

Post here questions and problems related to oXygen frameworks/document types.
vishwavaranasi
Posts: 149
Joined: Fri Feb 28, 2020 4:02 pm

DITAMAPS Manager custom insert action showing prompt to discard or load changes

Post by vishwavaranasi »

Hello Team , with our custom insert actions we are inserting a topicref from DITAMAPS Manager -> Insert Before ->{custom insert action} to ditamap opened inside the DITA Maps Manager
this action works fine , inside the insert action method we also calling the the following method() to save the ditamap and reload the map.

Code: Select all

WSEditor mainEditor = null;
		if(DITAAccess.getRootMapURL()==null) {
			logger.info("Selected RootMap is null, no map will be updated");
			return;
		}
		String rootMapPath = DITAAccess.getRootMapURL().getPath();
		File rootMap = new File(rootMapPath);
		boolean isRootmapLocked = !rootMap.canWrite();
		rootMap.setWritable(true);
		URL[] editorLocations = PluginWorkspaceProvider.getPluginWorkspace().getAllEditorLocations(DITA_MAPS_EDITING_AREA);

       	mainEditor = PluginWorkspaceProvider.getPluginWorkspace().getCurrentEditorAccess(MAIN_EDITING_AREA);
       	
        logger.info(">>>>>Saving file: {}", mainEditor.getEditorLocation().getPath());
        mainEditor.save();
		URL enabledRootmap = null;
		// Update only the references in the .ditamap file since it's not needed to save it at this point
        for (URL location : editorLocations) {
        	WSEditor ditMapsEditor = PluginWorkspaceProvider.getPluginWorkspace().getEditorAccess(location, DITA_MAPS_EDITING_AREA);
            logger.info("11>>>>>Saving and reloading ditamap: {}", mainEditor.getEditorLocation().getPath());
			String path=mainEditor.getEditorLocation().getPath();
			String finalName = path.substring(path. lastIndexOf('/'));
			if(DITAAccess.getRootMapURL().toString().contains(finalName) && !mainEditor.getCurrentPage().isEditable()){
				mainEditor.getCurrentPage().setEditable(true);
				enabledRootmap=location;
			}
			WSDITAMapEditorPage page = (WSDITAMapEditorPage) ditMapsEditor.getCurrentPage();
           
            ditMapsEditor.save();
            page.refreshReferences();
        }
		ActionsProvider actionsProvider = ((StandalonePluginWorkspace)PluginWorkspaceProvider.getPluginWorkspace()).getActionsProvider();
		Map<String, Object> globalActions = actionsProvider.getGlobalActions();
		AbstractAction reloadAction = (AbstractAction)globalActions.get("File/Reload");
		SwingUtilities.invokeLater(() -> actionsProvider.invokeAction(reloadAction));
		//actionsProvider.invokeAction(reloadAction);
		if(enabledRootmap!=null){
			PluginWorkspaceProvider.getPluginWorkspace().getEditorAccess(enabledRootmap, DITA_MAPS_EDITING_AREA).getCurrentPage().setEditable(false);;
		}
		if(isRootmapLocked) {
			rootMap.setWritable(false);
		}
    }
but after this method executed , we keep getting the prompt as below
image.png
image.png (16.63 KiB) Viewed 453 times
when we click "NO" then only the newly added topicref showsup , if we click "YES" then it not loads newly added topicref
would you please help us here , how do we avoid the prompt
Thanks,
vishwa
Radu
Posts: 9088
Joined: Fri Jul 09, 2004 5:18 pm

Re: DITAMAPS Manager custom insert action showing prompt to discard or load changes

Post by Radu »

Hi,
I looked a number of times over the code, I do not quite understand your purpose here.
1) DITA Maps opened from the local disk in the DITA Maps Manager view auto-save by default. In the Oxygen Preferences->"DITA / Maps" page there is an "Automatically save local DITA Maps after each modification" checkbox which should be checked by default.

Are you working with files from a content management system that you have brought locally to a cached folder?
Have you considered writing a custom URL protocol to make this local cache not visible to the code?
https://www.oxygenxml.com/doc/ug-editor ... lugin.html

2)

Code: Select all

		String rootMapPath = DITAAccess.getRootMapURL().getPath();
		File rootMap = new File(rootMapPath);
I would recommend you obtain a File from an URL using instead the method:

Code: Select all

PluginWorkspaceProvider.getPluginWorkspace().getUtilAccess().locateFile(DITAAccess.getRootMapURL());
If the method returns null, it means the URL is remote.

3)

Code: Select all

		boolean isRootmapLocked = !rootMap.canWrite();
		rootMap.setWritable(true);
Is the File locked on disk by some other code you have? Is this maybe the reason that Oxygen's auto save for the DITA maps manager view does not work?

4) I assume that your code is called after actions are performed in the DITA Maps Manager view.

Code: Select all

       	mainEditor = PluginWorkspaceProvider.getPluginWorkspace().getCurrentEditorAccess(MAIN_EDITING_AREA);
       	
        logger.info(">>>>>Saving file: {}", mainEditor.getEditorLocation().getPath());
        mainEditor.save();
Why do you attempt to save the currently selected file from the main editor area?

5)

Code: Select all

    for (URL location : editorLocations) {
      WSEditor ditMapsEditor = PluginWorkspaceProvider.getPluginWorkspace().getEditorAccess(location, DITA_MAPS_EDITING_AREA);
      logger.info("11>>>>>Saving and reloading ditamap: {}", mainEditor.getEditorLocation().getPath());
      String path=mainEditor.getEditorLocation().getPath();
      String finalName = path.substring(path. lastIndexOf('/'));
     ......
      WSDITAMapEditorPage page = (WSDITAMapEditorPage) ditMapsEditor.getCurrentPage();
      ditMapsEditor.save();
      page.refreshReferences();
    }
So you also call save on all other opened DITA Maps from the DITA Maps Manager view? Why?

6)

Code: Select all

    ActionsProvider actionsProvider = ((StandalonePluginWorkspace)PluginWorkspaceProvider.getPluginWorkspace()).getActionsProvider();
    Map<String, Object> globalActions = actionsProvider.getGlobalActions();
    AbstractAction reloadAction = (AbstractAction)globalActions.get("File/Reload");
    SwingUtilities.invokeLater(() -> actionsProvider.invokeAction(reloadAction));
And now you take the global "File->Reload" action from the main menu and call it? I think this would call the "File/Reload" action for the current editor opened in the main editor area. Is this what you want?

7)

Code: Select all

    if(isRootmapLocked) {
      rootMap.setWritable(false);
    }
I assume that calling rootMap.setWritable might change the timestamp of the file on disk and when focus is received in the editor, Oxygen detects the timestamp has changed for a local loaded file and asks you to reload.
Maybe something like this might work:

Code: Select all

      long lastModified = rootMap.lastModified();
      rootMap.setWritable(false);
      rootMap.setLastModified(lastModified);    
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
vishwavaranasi
Posts: 149
Joined: Fri Feb 28, 2020 4:02 pm

Re: DITAMAPS Manager custom insert action showing prompt to discard or load changes

Post by vishwavaranasi »

Thanks Radu for your reply.

for the first point , am using oxygen 24 editor and i don't see the checkbox option to save automatically
image.png
image.png (59.3 KiB) Viewed 436 times
is this can be achieved programmatically using any oxygen API.

Thanks for other comments , we will consider to implement the same.

all the code we are attempting to save and File/Reload to make sure that the inserted image when we insert image inside topic or insert reference to ditamap will be resolved automatically with out user to reload the ditamap.
Thanks,
vishwa
Radu
Posts: 9088
Joined: Fri Jul 09, 2004 5:18 pm

Re: DITAMAPS Manager custom insert action showing prompt to discard or load changes

Post by Radu »

Hi,
I think that particular checkbox which auto saves a DITA Map (of course if the file on disk is not read-only) was added around Oxygen 25.
About Oxygen 24 officially we no longer maintain it in any way and we'll drop support for it at the end of this year:
https://www.oxygenxml.com/eol.html
About your remark:
all the code we are attempting to save and File/Reload to make sure that the inserted image when we insert image inside topic or insert reference to ditamap will be resolved automatically with out user to reload the ditamap.
Could you elaborate? Maybe give me a small set of steps that you took which did not work until you reloaded the XML document in the main editor.
Why would the image reference depend on the DITA Map contents? Is it an image with a keyref to a key that you define in the DITA Map?
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
vishwavaranasi
Posts: 149
Joined: Fri Feb 28, 2020 4:02 pm

Re: DITAMAPS Manager custom insert action showing prompt to discard or load changes

Post by vishwavaranasi »

Hi Radu ,
Here is what we are doing , we have our custom inserts for images and topic refs ,
The custom inserts for topic refs works both from DITA menu (from oxygen) and DITA Maps Manager.
The custom inserts for images works from DITA menu only.
within in the rootmap file we have a mapref where we maintain all keydefs to all references of the topics ,images , etc... this file is always there at backmatter
image.png
image.png (10.99 KiB) Viewed 275 times
the file having the <keydef> entries as below

Code: Select all

 <keydef format="dita" href="file_11011=GUID-6887E6FC-DE36-4B57-8763-EDA7EE11011=2.2.xml" keys="GUID-6887E6FC-DE36-4B57-8763-EDA7EE11011">
      <topicmeta>
         <navtitle>file 11011</navtitle>
      </topicmeta>
   </keydef>
   <keydef format="dita" href="file_title_31=GUID-A1CB2D1A-DFA0-461E-7EF7-AF89F572C99D=5.xml" keys="GUID-A1CB2D1A-DFA0-461E-7EF7-AF89F572C99D">
      <topicmeta>
         <navtitle>file title 31</navtitle>
      </topicmeta>
   </keydef>
so here , when we insert any topicref and any image inserted to a topic will be created one <keydef> entry to this file , the save and reload of the rootmap what we are doing is to auto resolve the entry without user relaods the rootmap since the keydef file is there at ditamaps manager , we try to relaod the entrie rootmap.

coming to the problem , as you mentioned using last modified time , we could able to get rid-off the oxygen prompt to discard or load dialog. but the problem is the ditamaps manager still not saved. showing like this below (asterisk) even though we try to save both the ditamaps editing area as well the main editing area

image.png
image.png (2.03 KiB) Viewed 275 times
Thanks,
vishwa
Radu
Posts: 9088
Joined: Fri Jul 09, 2004 5:18 pm

Re: DITAMAPS Manager custom insert action showing prompt to discard or load changes

Post by Radu »

Hi,
As your code looks like this:

Code: Select all

            ditMapsEditor.save();
            page.refreshReferencesrefreshReferences();
The "refreshReferences()" changes again the modified flag of the editor. So maybe you can reverse the lines:

Code: Select all

            
             page.refreshReferencesrefreshReferences();
            ditMapsEditor.save();
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
vishwavaranasi
Posts: 149
Joined: Fri Feb 28, 2020 4:02 pm

Re: DITAMAPS Manager custom insert action showing prompt to discard or load changes

Post by vishwavaranasi »

Okay Radu , we will try the same.

otherwise do you suggest us anything to be added or to be deleted what we are doing reload/ refreshReferencesrefreshReferences() to resolve the inserted references to inline with the <keydef> file?

Thanks,
vishwa
Thanks,
vishwa
vishwavaranasi
Posts: 149
Joined: Fri Feb 28, 2020 4:02 pm

Re: DITAMAPS Manager custom insert action showing prompt to discard or load changes

Post by vishwavaranasi »

Thanks Radu , the reversing the lines of code worked
page.refreshReferencesrefreshReferences();
ditMapsEditor.save();

what we are doing now

Code: Select all

WSDITAMapEditorPage page = (WSDITAMapEditorPage) ditaMapsEditor.getCurrentPage();
            page.refreshReferences();
            rootDitaMap.setLastModified(lastModified);  
            ditaMapsEditor.save();
and do we really required the invoke action what we are doing as below after we did the page.refreshReferences() and saved main editor and saved dita maps manager area?

Code: Select all

ActionsProvider actionsProvider = ((StandalonePluginWorkspace)PluginWorkspaceProvider.getPluginWorkspace()).getActionsProvider();
		Map<String, Object> globalActions = actionsProvider.getGlobalActions();
		AbstractAction reloadAction = (AbstractAction)globalActions.get("File/Reload");
		actionsProvider.invokeAction(reloadAction);
Thanks,
vishwa
Radu
Posts: 9088
Joined: Fri Jul 09, 2004 5:18 pm

Re: DITAMAPS Manager custom insert action showing prompt to discard or load changes

Post by Radu »

Hi,
So:
and do we really required the invoke action what we are doing as below after we did the page.refreshReferences() and saved main editor and saved dita maps manager area?
I don't know, I do not understand your entire flow here. Why did you add that piece of code? Is it because in the main editor you added a keyref to a specific key and then you defined the key in the DITA Map? How about if you have defined the key first in the DITA Map and then used it in the main editor?
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Post Reply