How to create a menu action with an icon that toggles?
Posted: Fri Oct 10, 2025 6:53 am
I'm working on a combination workspace access plugin + custom document framework in the desktop version of Oxygen.
I have a custom action I'd like to put in the framework menu, and I want it to toggle an option on/off to control some behavior in my plugin (enable/disable event handlers that respond to DOM changes and try to keep various numbered elements up-to-date). Some users may prefer to only run the numbering updates on-demand, instead of automatically as they make changes to the document. So I want to store the state of this button in options using PluginWorkspace.getOptionsStorage().setOption(key, value).
I have the action defined in a java class, and it is added to the menu using the the my_doctype.framework XML script. In the action's doOperation method, I can read the current option state, flip it, store it back to options, and then set the action button icon accordingly (OptionsManager and ImageResolver are some utility classes I've created):
This part works AFTER clicking on the menu action one or more times. My problem is that I can't set the icon for the first time whenever a new editor window is opened. I have similar code in my main WorkspaceAccessPluginExtension, inside a WSEditorChangeListener for the editorPageChanged and editorOpened events, and while it doesn't throw any errors, it also doesn't change the icon on the framework menu item:
Can you help me figure out where I can initialize the action icon on editorOpen?
I have a custom action I'd like to put in the framework menu, and I want it to toggle an option on/off to control some behavior in my plugin (enable/disable event handlers that respond to DOM changes and try to keep various numbered elements up-to-date). Some users may prefer to only run the numbering updates on-demand, instead of automatically as they make changes to the document. So I want to store the state of this button in options using PluginWorkspace.getOptionsStorage().setOption(key, value).
I have the action defined in a java class, and it is added to the menu using the the my_doctype.framework XML script. In the action's doOperation method, I can read the current option state, flip it, store it back to options, and then set the action button icon accordingly (OptionsManager and ImageResolver are some utility classes I've created):
Code: Select all
public void doOperation(final AuthorAccess authorAccess, final ArgumentsMap args) throws IllegalArgumentException, AuthorOperationException {
boolean newAutonumberState = !OptionsManager.getBooleanValue(OptionsManager.AUTONUMBERING_ENABLED_KEY);
AuthorActionsProvider actionsProvider = authorAccess.getEditorAccess().getActionsProvider();
Map<String, Object> actionMap = actionsProvider.getAuthorExtensionActions();
Action action = (Action) actionMap.get(AUTONUMBER_ACTION_ID);
if (action != null) {
String iconPath = (newAutonumberState ? ImageResolver.AUTONUMBER_ENABLED_ICON : ImageResolver.AUTONUMBER_DISABLED_ICON);
action.putValue(Action.SMALL_ICON, ImageResolver.getImage(iconPath));
}
}
Code: Select all
public void editorOpened(URL url) {
WSEditor editorAccess = pluginWorkspaceAccess.getCurrentEditorAccess(StandalonePluginWorkspace.MAIN_EDITING_AREA);
if (editorAccess.getCurrentPageID().equals(EditorPageConstants.PAGE_AUTHOR)) {
WSEditorPage editorPage = editorAccess.getCurrentPage();
if (editorPage instanceof WSAuthorEditorPage authorPage) {
boolean autonumberState = !OptionsManager.getBooleanValue(OptionsManager.AUTONUMBERING_ENABLED_KEY);
AuthorActionsProvider actionsProvider = authorPage.getActionsProvider();
Map<String, Object> actionMap = actionsProvider.getAuthorExtensionActions();
Action action = (Action) actionMap.get(AUTONUMBER_ACTION_ID);
if (action != null) {
String iconPath = (autonumberState ? ImageResolver.AUTONUMBER_ENABLED_ICON : ImageResolver.AUTONUMBER_DISABLED_ICON);
action.putValue(Action.SMALL_ICON, ImageResolver.getImage(iconPath));
}
}
}
}