Skip to content
Robert Silverton edited this page Jul 3, 2014 · 4 revisions

View this tutorial's changes on GitHub.

If you're working with the tutorial repo, open a Git Shell in the CoreEditor-HelloWorld-Example-as folder and run the following command:

git checkout -f step-4

Otherwise, follow the instructions below.


Adding Actions To The MenuBar

In the previous tutorial we saw how to add an action to a ToolBar, and mentioned that the same Action can appear on the ToolBar, MenuBar or both. In this tutorial we will see how to configure an ActionFactory to perform all of this.

Currently our ActionFactory looks like this:

new ActionFactory( HelloWorldContext, Commands.MY_COMMAND, "My Action", "myActions" );

To recap the parameters are:

  • target : The type of Context whose view you want the action to appear on.
  • command : The Command linking your Action to a CommandHandler.
  • label : The label of the ToolBarButton and -as we will see- the label of the action on the MenuBar.
  • toolbarPath : The group of buttons you want your Action to appear next to on the ToolBar.

The next parameter you can specify is 'menuPath'. This parameter is a string that takes the form: "path/to/menu/item/groupName"

Menus can be nested, which is why we have a path syntax being used here. The final part of the path works much like the toolbarPath, in that it determines which group of Actions you want your Action to be next to.

Some examples using menuPaths are:

new ActionFactory( IGlobalViewContainer, Commands.NEW_FILE, "New...", "", "File/fileActions" );
new ActionFactory( IGlobalViewContainer, Commands.OPEN_FILE, "Open..", "", "File/fileActions" );
new ActionFactory( IGlobalViewContainer, Commands.PRINT, "Print..", "", "File/printActions" );
new ActionFactory( IGlobalViewContainer, Commands.UNDO, "Undo", "", "Edit/undoActions" );
new ActionFactory( IGlobalViewContainer, Commands.REDO, "Redo", "", "Edit/undoActions" );

You can see that we are using grouping to separate fileActions from printActions, even though both groups appear under File Menu paths can be as nested as you like, although typically they go no deeper than 2 levels.

For our example, change the following line:

CoreApp.resourceManager.addResource( new ActionFactory( HelloWorldContext, Commands.MY_COMMAND, "My Action", "myActions" ) );

to:

CoreApp.resourceManager.addResource( new ActionFactory( IGlobalViewContainer, Commands.MY_COMMAND, "My Action", "myActions", "Actions/myActions" ) );

Notice that we’ve switched HelloWorldContext for IGlobalViewContainer here. Build your extension and run the application to see your Action now appears in the application’s main menu bar (under “Actions/My Action”), and as an icon in the actions bar just below it.


Adding Icons To Actions

You may have noticed that your Action on the ToolBar has a dashed square as its icon. This is the default icon if no other is specified.

We’ll now see how you can supply your own icon to this Action. Replace the line of code again so it uses CoreEditorIcons to pass a reference to an embedded image to your ActionFactory's constructor like so:

CoreApp.resourceManager.addResource( new ActionFactory( IGlobalViewContainer, Commands.MY_COMMAND, "My Action", "myActions", "Actions/myActions", CoreEditorIcons.Resource ) );

Alternatively, you could create an image embed local to your HelloWorldExtension and pass a reference to that image instead.

Note how the new icon appears both in the main menu bar’s dropdown and in the action bar’s action icon.


< Previous | Next >