addMenuBarCustomizer -menu item enable/disable

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

addMenuBarCustomizer -menu item enable/disable

Post by vishwavaranasi »

Hello Team ,
We have our own main menu and added it to Oxygen menus... using pluginWorkspaceAccess.addMenuBarCustomizer
in this, one of the menu item , should be enabled only when the Topic or any DITA type opened in MAIN EDITING AREA as writable file.

means initially if there is file opened , and the file is read-only -> then the meny item should be disabled.
and when user opens a file with writable then the menu item should be enabled to perform action attached to that menu item.

Would you please help me to achieve the same.

Thanks,
vishwa
Thanks,
vishwa
Cosmin Duna
Site Admin
Posts: 120
Joined: Wed Dec 12, 2018 5:33 pm

Re: addMenuBarCustomizer -menu item enable/disable

Post by Cosmin Duna »

Hi Vishwa,
You can add a WSEditorChangeListener on the main editor that will notify you when the editor is changed. Then you can check the editable state and enable or disable your menu item.
Something like this:

Code: Select all

    pluginWorkspaceAccess.addEditorChangeListener(new WSEditorChangeListener() {
      @Override
      public void editorSelected(URL editorLocation) {
        WSEditor editor = pluginWorkspaceAccess.getEditorAccess(editorLocation, PluginWorkspace.MAIN_EDITING_AREA);
        boolean isEditable = editor.getCurrentPage().isEditable();
        menuItem.setEnabled(isEditable);
      }
    }, PluginWorkspace.MAIN_EDITING_AREA);
Best regards,
Cosmin
Cosmin Duna
<oXygen/> XML Editor
http://www.oxygenxml.com
vishwavaranasi
Posts: 140
Joined: Fri Feb 28, 2020 4:02 pm

Re: addMenuBarCustomizer -menu item enable/disable

Post by vishwavaranasi »

Thanks Cosmin,
I have implemented as you mentioned as below.

but this one is not consistent when enabling and disabling the menu Item.

initially the Menu Item - disabled when plugin loaded.
1. I have opened File-Open - toipc file (READ-ONLY) means locally this file has (Read-only attribute checked) , then the menu item showed as disabled.

2. I have opened another file (keeping that already opened) File- open - topic file (WRITABLE) - the menu item showed as enabled.

3. I switched to file(read-only in step1) - this time still the menu item showing me enabled - it supposed to disable menu item since this file is read-only.

would you please help me to achieve the correct behavior.


@Override
public void editorSelected(URL editorLocation) {

// get a reference to the editor


final WSEditor editorAccess = pluginWorkspaceAccess.getEditorAccess(editorLocation,
PluginWorkspace.MAIN_EDITING_AREA);

URL[] allOpenedEditorLocations = pluginWorkspaceAccess
.getAllEditorLocations(pluginWorkspaceAccess.MAIN_EDITING_AREA);
for (int i = 0; i < allOpenedEditorLocations.length; i++) {
WSEditor openedEditor = pluginWorkspaceAccess.getEditorAccess(allOpenedEditorLocations,
pluginWorkspaceAccess.MAIN_EDITING_AREA);

boolean isEditable = openedEditor.getCurrentPage().isEditable();
if(isEditable) {
System.out.println("IF IF IF editorSelected -- isEditable" +isEditable);
menuItem.setEnabled(isEditable);
//install(pluginWorkspaceAccess, editorAccess);
}
else {
System.out.println("ELSE ELSE ELSE editorSelected -- isEditable" +isEditable);
menuItem.setEnabled(isEditable);
//install(pluginWorkspaceAccess, editorAccess);
}
}

}

}, PluginWorkspace.MAIN_EDITING_AREA);
Thanks,
vishwa
Cosmin Duna
Site Admin
Posts: 120
Joined: Wed Dec 12, 2018 5:33 pm

Re: addMenuBarCustomizer -menu item enable/disable

Post by Cosmin Duna »

Hi,
This happens because you iterated over all editors and change the state of the menu item for every one of them. So, using the code that you sent, the menu item will be enabled/disabled according to the editable state of the last editor.

The "editorSelected" method is called every time the editor is changed and the "editorLocation" variable is the URL of the new editor. All you have to do is to check the editable state of the editor that corresponds with the 'editorLocation' URL.

This code should work properly:

Code: Select all

    pluginWorkspaceAccess.addEditorChangeListener(new WSEditorChangeListener() {
      @Override
      public void editorSelected(URL editorLocation) {
        boolean isEditable = false;
        if(editorLocation != null) {
           WSEditor currentEditor = pluginWorkspaceAccess.getEditorAccess(editorLocation, PluginWorkspace.MAIN_EDITING_AREA);
          if(currentEditor != null) {
            WSEditorPage currentPage = currentEditor.getCurrentPage();
            if(currentPage != null) {
              isEditable = currentPage.isEditable();
            }
          }
        }
        menuItem.setEnabled(isEditable);
      }

    }, PluginWorkspace.MAIN_EDITING_AREA);


Best regards,
Cosmin
Cosmin Duna
<oXygen/> XML Editor
http://www.oxygenxml.com
Post Reply