Skip to content

HOWTO: Setup an Editor Specific Top-Level Menu

May 14, 2010

The Platform Command Framework is one of the most powerful features of RCP. It’s also one of the most confusing. In truth, the command framework is a kind of mini-language that’s embedded inside RCP. Unfortunately it’s syntax consists of XML extension points and it’s semantics aren’t formally specified anywhere. This can make it difficult to get started with the command framework and even the simplest tasks can be somewhat daunting. Fortunately, once you begin to grok the necessary concepts and learn the language behind the language you’ll reach a point where every little task doesn’t require oodles of googling.

But until then here’s another HOWTO that addresses a common object: defining a top-level menu that only shows up when a specific type of editor is active.

  1. Define your own top-level menu. This is done by making a menuContribution whose locationURI is something like menu:org.eclipse.ui.main.menu?after=additions. In this menuContribution you’ll define your own top-level menu like:


    <menu
    id="com.example.app.menu.test"
    label="Test">
    </menu>

  2. Contribute a command to your menu. You’ll need to define your own command and then add another menuContribution and this time the locationURI will be your menu-id. The content of this contribution will be a command element whose commandId references your new command.

  3. Use visibleWhen to restrict when the command is active. At this point you’ve simply created a new top-level menu that has your command in it. If you only want the command to be available when a certain editor-type is active you’ll need to use a Command Expression. (This is another mini-language!). To achieve the required effect you want something like (note that checkEnabled is
    false!):


    <command
    commandId="com.example.command.testCommand"
    label="Test">
    <visibleWhen
    checkEnabled="false">
    <with
    variable="activeEditorId">
    <equals value="com.example.editor.myEditor" />
    </with>
    </visibleWhen>
    </command>

And that’s pretty much all there is to it. This general three step approach works for most command stuff. First, just get the command item to show up in the appropriate menu and the like. Then try to figure out how to further restrict its visibility using a core expression.

Advertisements

From → links

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: