Archive

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.

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.

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.

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

(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.

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:

Window –> Preferences –> General –> Keys

In the text field below the Scheme drop down type: shift+alt+f

Press Delete in the Binding field to remove the current key binding

With the cursor in the Binding field press the shift key, the alt key and the F3 key all at the same time.

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?

Start the runtime workbench and open the Custom Perspective

Press Shift+Alt+F3. Notice the cursor changes appearance

Right click in the Custom Navigator and select New –> Project

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.

[Before you get too engaged in this post: you might want to skip it. Yes, I realize that the writing is engaging, witty, poetic, philosophical, daring, edgy, and technically astute, but what is all that to being wrong? Just jump forward to Part 18 – Take 2 for a refactoring of this post with many of the wrong parts taken out. However, the writing is still what it is and for that I offer my apologies.]

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

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.

For now, we are going to set up the menus with bogus behavior. Since I don’t usually test the presentation this works out well for me. We’ll get around to behavior in the next post. Let’s get the menus in place and then we will snap the commands (next time).

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.

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 Delete is not disabled. That is under the control of Eclipse and the clipboard. You will probably have a different item enabled or none).

Don’t let the gratuitous use of Ctrl+N upset you. We will take care of messing those up later.

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?

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:

Window –> Preferences –> General –> Keys

In the text field below the Scheme drop down type: shift+alt+f

Press Delete in the Binding field to remove the current key binding

With the cursor in the Binding field press the shift key, the alt key and the F3 key all at the same time.

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?

Start the runtime workbench and open the Custom Perspective

Press Shift+Alt+F3. Notice the cursor changes appearance

Right click in the Custom Navigator and select New –> Project

Oh look! A window with wonderful information!

And there it is: the path used by the active contribution location URI: common.new.menu. Change the current path from popup:customplugin.customnavigator?after=additions to popup:common.new.menu?after=additions and start the runtime workbench again. The menus are exactly where we want them.

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.

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

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.

However, that is not the reason why this post has taken so long to appear; I have had a lot on my mind and this wasn’t it. In the next post we will add command objects to the New menu items and ignore the Copy/Paste/Delete stuff (those commands need to be configured to recognize our custom types so they copy things properly. They might work our of the box, but since I haven’t thought through all of the implication I will assume that they will have to be changed).

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