Home > Eclipse development, Software Development > Writing an Eclipse Plug-in (Part 18 – Take 2): Common Navigator: Adding submenus (Presentation/Behavior)

Writing an Eclipse Plug-in (Part 18 – Take 2): Common Navigator: Adding submenus (Presentation/Behavior)


Well, don’t I feel silly.

During the writing of Part 19, Adding Behavior to the menu items, I discovered something both heartening and disturbing: the addition of behavior to the New Wizards is much easier than I thought. Where I used the org.eclipse.ui.menus extension point to add the menu items to the New menu of the default popup menu I could have used the org.eclipse.ui.navigator.navigatorContent –> commonWizard extension instead. This would take care of both presentation and behavior.

Since using the org.eclipse.ui.menus to accomplish this task is wrong (yes, wrong*) I thought I would correct this obvious affront to civilization and pleasant company before the wrong kind of meme invades too many impressionable minds and causes an aggregation of incorrect behavior leading to that worst of all possible behaviors: rudeness.

Consider this a refactoring of Part 18 as the things discovered in it are still valid and will help in the controlling of the popup menu in other contexts…just not this one.

Okay! Places everyone! This time with feeling!

[Pretend that you just finished reading the brilliant advice that was Part 17 and are looking for the enlightenment that only comes from believing in God, unicorns or the Na’vi.]

And now, the moment you’ve all been waiting for: adding commands to the popup menu.

I know, you are just aquiver in anticipation.

What menus do we need to put in? Just a few. The Custom Navigator popup menu should contain:

  • New – Sub-menu with choices (Custom Project, Schema, and Deployment. Eventually we will change Schema to Tables, Views, Filters)
  • Copy – Ctrl+C (an item in a category or an entire category)
  • Paste – Ctrl+V (an item in a category or an entire category)
  • Remove – Del or Ctrl+X (an item in a category or an entire category)

We will not worry about Import/Export behavior as that will be added to the Import/Export Wizard with the associated additions to the main menu, toolbar, etc. done as appropriate (meaning later, when I care).

The thing to remember is: presentation first, behavior second. Actually, by the time we’re done we will have done both for the price of one (well, at least for the New wizards).

Where should the popup menu configuration/code go: into the customplugin or customnavigator project? Because we are directly affecting the navigator popup menu the configuration will go into the customnavigator plug-in. I promise not to regret it later when someone points out why it would really be in the customplugin project.

How (are we doing it?)

First: let’s add one menu item to the New menu.

  1. customnavigator –> plugin.xml –> Extensions –> org.eclipse.ui.navigator.navigatorContent –> New –> commonWizard
    • type: new
    • wizardId: customplugin.wizard.new.custom
  2. As a sanity check start the runtime workbench, go to the Custom Perspective and right click in the empty Custom Navigator.

    Back to the grindstone.

  3. org.eclipse.ui.navigator.navigatorContent –> New –> commonWizard
    • type: new
    • wizardId: customplugin.wizard.file.schema
  4. org.eclipse.ui.navigator.navigatorContent –> New –> commonWizard
    • type: new
    • wizardId: customplugin.wizard.file.deployment
  5. I know: you probably think that the above was too easy. You’re right. It was. In addition to the presentation we got the behavior for free. And no code so we’re ahead.

Next: let’s add three standard menus: Copy/Paste/Delete. Again, for now we will assign default commands that we will change later.

Let’s use the following icons for the copy/paste/delete menu items (yes, I copied them from Eclipse):

  1. customnavigator –> plugin.xml –> Extensions –> All Extensions –> Add –> org.eclipse.ui.menus
  2. org.eclipse.ui.menus –> New –> menuContribution
    • locationURI: popup:customnavigator.navigator?before=import
  3. popup:customnavigator.navigator?before=import (menuContribution) –> New –> command
    • commandId: org.eclipse.ui.edit.copy
    • label: Copy
    • icon: icons/copy_16x16.png
  4. popup:customnavigator.navigator?before=import (menuContribution) –> New –> command
    • commandId: org.eclipse.ui.edit.paste
    • label: Paste
    • icon: icons/paste_16x16.png
  5. popup:customnavigator.navigator?before=import (menuContribution) –> New –> command
    • commandId: org.eclipse.ui.edit.delete
    • label: Delete
    • icon: icons/delete_16x16.png
  6. popup:customnavigator.navigator?before=import (menuContribution) –> New –> separator
    • name: customnavigator.separator
    • visible: true

The above is a lot to do all at once so feel free to add one section at a time and check the runtime workbench. Once you add the org.eclipse.ui.menus extension along with the menuContribution and one of the commands you should be able to open the Custom Perspective, right click in the Custom Navigator and see a lovely popup menu.

The final result is beautifully displayed in the screen capture (don’t sweat the fact that Copy is not disabled. That is under the control of Eclipse and the clipboard. You will probably have a different item enabled or none).

But first: time to reveal the magic.

Why (did we do it that way?)

If you want to add a menu item to the popup menu the easiest way is to use the org.eclipse.ui.menus extension and add a menuContribution to it (hmm. That would imply there is another way to do this. Yes, using actionSets…which have been deprecated, so don’t). The menuContribution‘s locationURI is the path to the location within the popup where your new menu item should go. If we were adding our menu item at the top level popup we would simply have used a locationURI of popup:customplugin.customnavigator?after=additions; that’s right: simply using the id of the navigator places the menu item into its popup. Using the after=additions piece puts the menu items directly in the popup right after the last item…which is not where we want it.

So the $54,000 question is: what locationURI do we use to put our menu items in the correct spot?

That turns out to be the wrong question (I am starting to dislike that word).

The real question is: if there is a shortcut to adding New Wizards to the main menu, why isn’t there a way to do that with the navigator popup menu? If you recall from Part 15 we added our 3 New Wizards to the main menu by using the org.eclipse.ui.perspectiveExtensions –> perspectiveExtension –> newWizardShortcut. By simply supplying the New Wizard id Eclipse took care of adding it to the main menu File –> New and to the toolbar New button.

The real answer is: there is a way to do it and it involves the org.eclipse.ui.navigator.navigatorContent extension point. It has a child extension named commonWizards that does the equivalent behavior of the newWizardShortcut but for popup menus. On one hand, this is the greatest thing since sliced bread since we don’t have to configure as much (and did I mention no code?), but on the other hand we still have no idea how to add to a popup menu submenu such as New when the behavior we want to add has nothing to do with a wizard.

Let’s speak to that later. [Peek at the end of the post after the configuration files; I speak to the madness needed to implement the method of adding to the default popup menu supplied with the Common Navigator Framework.]

The configuration of the remaining menus now make sense. Add a new menuContribution to the org.eclipse.ui.menus extension and, using the high-level custom navigator path location URI (popup:customnavigator.navigator?before=import), we tell Eclipse to put the menus before the import section. Again, just to supply the required information we assign the internal copy, paste, and delete commandIds to the various menus.

Here are the instructions again:

  • org.eclipse.ui.menus –> New –> menuContribution
    • locationURI: popup:customnavigator.navigator?before=import
  • popup:customnavigator.navigator?before=import (menuContribution) –> New –> command
    • commandId: org.eclipse.ui.edit.copy
    • label: Copy
    • icon: icons/copy_16x16.png
  • popup:customnavigator.navigator?before=import (menuContribution) –> New –> command
    • commandId: org.eclipse.ui.edit.paste
    • label: Paste
    • icon: icons/paste_16x16.png
  • popup:customnavigator.navigator?before=import (menuContribution) –> New –> command
    • commandId: org.eclipse.ui.edit.delete
    • label: Delete
    • icon: icons/delete_16x16.png
  • popup:customnavigator.navigator?before=import (menuContribution) –> New –> separator
    • name: customnavigator.separator
    • visible: true

Doesn’t it look great? It just makes you proud to be a plug-in developer.

The next big question: why did we do it that way? Or more accurately: where is the documentation for this magic?

Part of the answer is easy: instructions on adding to a popup menu can be found here. Part of the answer is weak: I stumbled on the help documentation for commonWizard while I was looking up something else.

Time to wash up:

  • Go to the Overview tab, click on Externalize Strings Wizard and externalize the strings
  • In plugin.xml click on the MANIFEST.MF tab, click on the light bulb in the left hand margin and select Add Missing Packages

Who’s better than you?

What Just Happened?

The popup menu items have been added! With and without behavior! Who says you can’t have your cake and eat it too?

The cat is not only alive, but curious, which leads one to worry about the cat’s future.

(I hope this was a better answer to your question, Augusto. It’s always better the second time…)

* I know, I know: it still works. So what? you may say. So what?! So everything. If I had known about the commonWizard extension sooner I would have used it first as it would have eliminated the need for the Plug-in Spy and the search from the Common Navigator popup menu path to the New menu. While it is still just configuration, it is needless configuration. Needless. Need less. Compare what commonWizard needs:


to what org.eclipse.ui.menus –> menuContribution is looking for:

Use the more extensive extension when extensiveness is called for. Otherwise, strive to be simple. Like a new born baby. Or the brain of a politician.

Code

No code…again.

bundle.properties

#Properties file for customnavigator_1.0.0.7
Bundle-Name = Custom Navigator Plug-in
view.name = Custom Plug-in Navigator
category.name = Custom Projects
navigatorContent.name = Custom Navigator Content
copy.command.label = Copy
paste.command.label = Paste
delete.command.label = Delete

plugin.xml (customnavigator)

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
   <extension
         point="org.eclipse.ui.views">
      <category
            id="customnavigator.category"
            name="%category.name">
      </category>
      <view
            allowMultiple="false"
            category="customnavigator.category"
            class="org.eclipse.ui.navigator.CommonNavigator"
            icon="icons/navigator.png"
            id="customnavigator.navigator"
            name="%view.name">
      </view>
   </extension>
   <extension
         point="org.eclipse.ui.navigator.viewer">
      <viewerActionBinding
            viewerId="customnavigator.navigator">
         <includes>
            <actionExtension
                  pattern="org.eclipse.ui.navigator.resources.*">
            </actionExtension>
         </includes>
      </viewerActionBinding>
      <viewerContentBinding
            viewerId="customnavigator.navigator">
         <includes>
            <contentExtension
                  pattern="customnavigator.navigatorContent">
            </contentExtension>
         </includes>
      </viewerContentBinding>
   </extension>
   <extension
         point="org.eclipse.ui.navigator.navigatorContent">
      <navigatorContent
            activeByDefault="true"
            contentProvider="customnavigator.navigator.ContentProvider"
            id="customnavigator.navigatorContent"
            labelProvider="customnavigator.navigator.LabelProvider"
            name="%navigatorContent.name">
         <triggerPoints>
            <instanceof
                  value="org.eclipse.core.resources.IWorkspaceRoot">
            </instanceof>
         </triggerPoints>
         <commonSorter
               class="customnavigator.sorter.SchemaCategorySorter"
               id="customnavigator.sorter.schemacategorysorter">
            <parentExpression>
               <or>
                  <instanceof
                        value="customnavigator.navigator.CustomProjectSchema">
                  </instanceof>
               </or>
            </parentExpression>
         </commonSorter>
      </navigatorContent>
      <commonWizard
            type="new"
            wizardId="customplugin.wizard.new.custom">
         <enablement></enablement>
      </commonWizard>
      <commonWizard
            type="new"
            wizardId="customplugin.wizard.file.schema">
         <enablement></enablement>
      </commonWizard>
      <commonWizard
            type="new"
            wizardId="customplugin.wizard.file.deployment">
         <enablement></enablement>
      </commonWizard>
   </extension>
   <extension
         point="org.eclipse.ui.menus">
      <menuContribution
            locationURI="popup:customnavigator.navigator?before=import">
         <command
               commandId="org.eclipse.ui.edit.copy"
               icon="icons/copy_16x16.png"
               label="%copy.command.label"
               style="push">
         </command>
         <command
               commandId="org.eclipse.ui.edit.paste"
               icon="icons/paste_16x16.png"
               label="%paste.command.label"
               style="push">
         </command>
         <command
               commandId="org.eclipse.ui.edit.delete"
               icon="icons/delete_16x16.png"
               label="%delete.command.label"
               style="push">
         </command>
         <separator
               name="customnavigator.separator"
               visible="true">
         </separator>
      </menuContribution>
   </extension>

</plugin>

Extra Credit

So you want to add non-Wizard behavior to the default popup menu and aren’t sure how? You’ve come to the right place. Let’s look at what it means to add a new menu item to the New menu item of the default popup menu of our Custom Navigator.

But before I can answer that we have to fix something. Well, some of us have to fix something.

For those of you doing this on Windows: shield your eyes and move on to the section titled CONTINUE HERE (while goto may be considered harmful, continue here isn’t).

Now that we have all of those miscreants out of the way: those of you who need to see the answer with your own eyes, but are using Kubuntu, need to change the Eclipse key binding for the runtime workbench from Shift+Alt+F2 to Shift+Alt+F3. Why? Because in order to discover the correct path to the New menu in the popup we need the Plug-in Spy to work. The Plug-in Spy, at least on Kubuntu, does not work properly because Shift+Alt+F2 doesn’t work properly; changing the key binding from Shift+Alt+F2 to Shift+Alt+F3 works.

How do we change the key binding? Start and do the following from the runtime workbench:

  1. Window –> Preferences –> General –> Keys
  2. In the text field below the Scheme drop down type: shift+alt+f
  3. Press Delete in the Binding field to remove the current key binding
  4. With the cursor in the Binding field press the shift key, the alt key and the F3 key all at the same time.
  5. Click OK to close the Preferences window

Now the bad news: you will have to do this every time you start the runtime workbench if you want to use the Plug-in Spy unless you unset the Launch configuration to clear the workspace; by not clearing the workspace you may find weird behavior that isn’t weird but appears to be weird because the runtime workbench is not cleaning up after itself. I recommend leaving the Launch configuration alone…meaning with the Clear selection checked.

The good news: you don’t need to use the Plug-in spy that often so this manual step is something you will not do too often. Grow up or start taking stronger meds.

CONTINUE HERE

Those of you on Windows: every time I say press Shift+Alt+F3 you must press Shift+Alt+F2. Got it? Shift+Alt+F3 really mean Shift+Alt+F2 (just in Windows). Think you can handle that?

  1. Start the runtime workbench and open the Custom Perspective
  2. Press Shift+Alt+F3. Notice the cursor changes appearance
  3. Right click in the Custom Navigator and select New –> Project
  4. Oh look! A window with wonderful information!

And there it is: the path used by the active contribution location URI: common.new.menu. If you change the current path for the Copy/Paste/Delete command from popup:customplugin.customnavigator?after=additions to popup:common.new.menu?after=additions and start the runtime workbench again you will find that the menus are part of the submenus associated with New.

So where can you find all this great information on inserting a menu item in the popup menu’s New menu?

My answer is going to be very unsatisfying: I don’t know. I went through a number of plug-ins, web sites and help files and found nothing. When I remembered the Plug-in Spy, and fixed the Shift+Alt+F2 problem (Kubuntu, remember?), I was able to discover the path I was looking for.

The cat remained quiet and was of no value whatsoever.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

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: