Archive

Archive for February, 2010

Writing an Eclipse Plug-in (Part 19): A Quick Display Fix

February 28, 2010 2 comments

[I am deep into solving a CNF issue, but since I haven’t solved it yet you will have to settle for a bug fix.]

Random bug: when the cursor hovers over the custom navigator title bar a tooltip opens letting us know that the navigator can’t find a label for the root node. The full error message is Error: no label provider for R/. Tells you everything you need to know. Except what the problem is. Or how to fix it. Or, for Eclipse novices, what R/ means.

Luckily this is something that we are not afflicted with here at Hidden Clause. The message did tell us everything we needed to know. The Custom Navigator label provider is ignoring the root node used by the navigator (the R/ referred to in the error) and returning an empty string. The code for LabelProvider.getText() is:

   public String getText(Object element) {
        String text = ""; //$NON-NLS-1$
        if (ICustomProjectElement.class.isInstance(element)) {
            text = ((ICustomProjectElement)element).getText();
        }
        // else ignore the element

        return text;
    }

(Notice how it so brilliantly ignores everything except elements of type ICustomProjectElement.)

What the message also tells us, by not telling us, is that our zero-length string appears to be causing consternation in the navigator. It is causing so much consternation that the navigator thinks no label provider is available to supply it with a default label for the root node.

That something is easily fixed in the LabelProvider. I’m not sure why Eclipse does not default to no string for the root (damn, those double negatives!), but it does not so we have to assign something to it. Since the standard behavior for other navigator views is to use the name of the view, in this case Custom Plug-in Navigator, that is what we will do.

  1. Open LabelProvider.java
  2. Change getText() to include an else if:
       public String getText(Object element) {
            String text = ""; //$NON-NLS-1$
            if (ICustomProjectElement.class.isInstance(element)) {
                text = ((ICustomProjectElement)element).getText();
            } else if (IWorkspaceRoot.class.isInstance(element)) {
                text = "Custom Plug-in Navigator";
            }
            // else ignore the element
    
            return text;
        }
    
  3. Start the runtime workbench and behold the beauty of our new string.

For Those of You Who Care

I discovered the solution to the above by putting a breakpoint in LabelProvider.getText() and walking the call tree. NavigatorContentServiceLabelProvider.findStyledText() quite explicitly changed the original empty string returned by LabelProvider.getText() into a null which caused NavigatorContentServiceLabelProvider.getStyledText() to assign the error message to the navigator view. It makes sense, it just wasn’t what I wanted.

Extra credit: Run the Externalize String Wizard on LabelProvider.java and add the new string to the message.properties file.

What Just Happened?

Fixed a bug. Contain your excitement.

The cat was not impressed and refused to come out.

Code

messages.properties

CustomProjectParent_Project_Folder=icons/project-folder.png
CustomProjectSchema_Project_Schema=icons/project-schema.png
CustomProjectSchemaFilters_Project_Schema_Filters=icons/project-schema-filters.png
CustomProjectSchemaTables_Project_Schema_Tables=icons/project-schema-tables.png
CustomProjectSchemaViews_Project_Schema_Views=icons/project-schema-views.png
CustomProjectStoredProcedures_Project_Stored_Procedures=icons/project-stored-procedures.png
LabelProvider_Custom_Plugin_Navigator=Custom Plug-in Navigator

LabelProvider.java

/**
 * Coder beware: this code is not warranted to do anything.
 *
 * Copyright Oct 17, 2009 Carlos Valcarcel
 */
package customnavigator.navigator;

import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.swt.graphics.Image;

/**
 * @author carlos
 *
 */
public class LabelProvider implements ILabelProvider {

    /* (non-Javadoc)
     * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
     */
    @Override
    public Image getImage(Object element) {
        Image image = null;
        
        if (ICustomProjectElement.class.isInstance(element)) {
            image = ((ICustomProjectElement)element).getImage();
        }
        // else ignore the element
        
        return image;
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
     */
    @Override
    public String getText(Object element) {
        String text = ""; //$NON-NLS-1$
        if (ICustomProjectElement.class.isInstance(element)) {
            text = ((ICustomProjectElement)element).getText();
        } else if (IWorkspaceRoot.class.isInstance(element)) {
            text = Messages.LabelProvider_Custom_Plugin_Navigator;
        }
        // else ignore the element
        
        return text;
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.viewers.IBaseLabelProvider#addListener(org.eclipse.jface.viewers.ILabelProviderListener)
     */
    @Override
    public void addListener(ILabelProviderListener listener) {
        // TODO Auto-generated method stub

    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
     */
    @Override
    public void dispose() {
        // TODO Auto-generated method stub

    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.viewers.IBaseLabelProvider#isLabelProperty(java.lang.Object, java.lang.String)
     */
    @Override
    public boolean isLabelProperty(Object element, String property) {
        // TODO Auto-generated method stub
        return false;
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.viewers.IBaseLabelProvider#removeListener(org.eclipse.jface.viewers.ILabelProviderListener)
     */
    @Override
    public void removeListener(ILabelProviderListener listener) {
        // TODO Auto-generated method stub

    }

}

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

February 21, 2010 1 comment

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.

Three 9s

February 14, 2010 Leave a comment

Plan 9 from Outer Space

Universal questions.

How did people fill their time before the Internet?

Does anyone remember what it was like to use a phone without having to reboot it?

When I was younger (and by younger I mean right after I graduated from college) I remember playing working with a Vic 20, and then an Atari 800, and then an Amiga 1000 and then an Amiga 2000 before I finally learned enough about programming to get a job. As it turns out my fear of not finding work in the computer science field 20 years ago was somewhat overblown. Lack of qualified people in a field can do that (think early 80’s).

I don’t remember rebooting my phone, but back then the princess phone was still pretty recent. I don’t miss it, but I do remember how cool touch tone phones were.

Deep Space 9

Will we be rebooting our televisions any time soon?

It is easy, in hindsight, to look back and realize what a geek I was. If only I’d known back then; I might have invested better.

I have a new definition of geek: someone who is so obsessed with bending technology to their will they instead bend space time using just the power of their mind. This allows them to jump forward in time minutes or, more often, hours in the blink of an eye. Unfortunately, going back in time does not currently appear possible though there are rumors that Bill Gates has in fact accomplished this feat; this would explain how Microsoft has changed direction so many times and still succeeded (of course, being friends with Anthony Stark doesn’t hurt).

District 9

You take chicken, for example: maybe [the machines] couldn’t figure out what to make chicken taste like, which is why chicken tastes like everything.
Mouse from the Matrix

Where the religious, or those aspiring to be religious, or maybe those who think they are religious, or maybe those who would like to be religious, or perhaps those trying to understand why anyone is religious, find solace in watching movies like The Ten Commandments or The Passion of the Christ, geeks (or at least I) find solace watching movies like 12 Monkeys or District 9.

The thing about growing up as a geek is that it didn’t start until after I graduated from college. I have to admit, with my head bowed low, that I was a liberal arts major. I always enjoyed math and science, but the voices in my head always said that those were hard subjects and that I wouldn’t do well with them. Oddly enough my SAT scores disagreed, but who listens to SAT scores when you have voices? I graduated with a B.A. in Communications, worked in television for about 3 years and left. I just didn’t get the satisfaction I was looking for as a union engineer loading up video tape and working the Chyron.

After twenty years in the software field I now know what my mind craved all those years: spending life in a cube writing software that would never make it into production.

Perhaps Christopher Johnson will return in three years and transform us all back into humans. Or not.

I am afraid to look in the box…

Categories: Incoherent

Writing an Eclipse Plug-in (Part 18): Common Navigator: Adding Submenus (Presentation)

February 7, 2010 2 comments

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

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.

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.

How (are we doing it?)

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

  1. customnavigator –> plugin.xml –> Extensions –> All Extensions –> Add –> org.eclipse.ui.menus
  2. org.eclipse.ui.menus –> New –> menuContribution
    • locationURI: popup:common.new.menu?after=additions
  3. popup:common.new.menu?after=additions (menuContribution) –> New –> command
    • commandId: org.eclipse.ui.newWizard
    • label: Custom Project
    • icon: icons/project-folder.png
  4. As a sanity check start the runtime workbench.

    Back to the grindstone.

  5. popup:common.new.menu?after=additions (menuContribution) –> New –> command
    • commandId: org.eclipse.ui.newWizard
    • label: Schema File
    • icon: icons/schema-file_16x16.png (copy this from the customplugin icon folder)
  6. popup:common.new.menu?after=additions (menuContribution) –> New –> command
    • commandId: org.eclipse.ui.newWizard
    • label: Deployment File
    • icon: icons/deployment-file_16x16.png (copy this from the customplugin icon folder)

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. org.eclipse.ui.menus –> New –> menuContribution
    • locationURI: popup:customnavigator.navigator?before=import
  2. popup:customnavigator.navigator?before=import (menuContribution) –> New –> command
    • commandId: org.eclipse.ui.edit.copy
    • label: Copy
    • icon: icons/copy_16x16.png
  3. popup:customnavigator.navigator?before=import (menuContribution) –> New –> command
    • commandId: org.eclipse.ui.edit.paste
    • label: Paste
    • icon: icons/paste_16x16.png
  4. popup:customnavigator.navigator?before=import (menuContribution) –> New –> command
    • commandId: org.eclipse.ui.edit.delete
    • label: Delete
    • icon: icons/delete_16x16.png
  5. 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 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:

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

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

Who’s better than you?

What Just Happened?

The popup menu items have been added! Without behavior!

In addition it was the Plug-in Spy that made it possible for me to create the proper locationURI I needed to insert the new menus in the correct spot.

Don’t underestimate the power of Plug-in Spy.

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

(I hope this answered your question, Augusto.)

Code

No code…again.

bundle.properties

#Properties file for customnavigator
Bundle-Name = Custom Navigator Plug-in
view.name = Custom Plug-in Navigator
category.name = Custom Projects
navigatorContent.name = Custom Navigator Content
customProject.command.label = Custom Project
schemaFile.command.label = Schema File
deploymentFile.command.label = Deployment File
copy.command.label = Copy
paste.command.label = Paste
delete.command.label = Delete

plugin.xml

<?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>
   </extension>
   <extension
         point="org.eclipse.ui.menus">
      <menuContribution
            locationURI="popup:common.new.menu?after=additions">
         <command
               commandId="org.eclipse.ui.newWizard"
               icon="icons/project-folder.png"
               label="%customProject.command.label"
               style="push">
         </command>
         <command
               commandId="org.eclipse.ui.newWizard"
               icon="icons/schema-file_16x16.png"
               label="%schemaFile.command.label"
               style="push">
         </command>
         <command
               commandId="org.eclipse.ui.newWizard"
               icon="icons/deployment-file_16x16.png"
               label="%deploymentFile.command.label"
               style="push">
         </command>
      </menuContribution>
      <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>