Archive

Posts Tagged ‘CustomNewActionProvider’

Writing an Eclipse Plug-in (Part 21): Return of the Popup Menu (Displaying Resources)

[In case anyone cares: I have upgraded to Eclipse 3.6 RC1]

Welcome to the second of what will probably be 4 posts on creating popup menus using the Common Navigator Framework.

In the last post we created a two item popup that appears when there are no resources displayed or selected. In this post we will have the popup menu appear even when we right click on a resource.

How (are we doing it?)

Perform the following on the customnavigator plug-in project.

  1. Delete the navigatorContent enablement adapt entries for both CustomNewActionProvider and CustomRefreshActionProvider.
    • plugin.xml –> Extensions
    • org.eclipse.ui.navigator.navigatorContent –> customnavigator.popup.actionprovider.CustomNewActionProvider –> (enablement) –> (or) –> org.eclipse.core.resources.IResource (adapt) –> Delete
    • org.eclipse.ui.navigator.navigatorContent –> customnavigator.popup.actionprovider.CustomRefreshActionProvider –> (enablement) –> (or) –> org.eclipse.core.resources.IResource (adapt) –> Delete
  2. Add two enablement instanceof entries for both CustomNewActionProvider and CustomRefreshActionProvider.
    • org.eclipse.ui.navigator.navigatorContent –> customnavigator.popup.actionprovider.CustomNewActionProvider –> (enablement) –> (or) –> New –> instanceof
      • value: customnavigator.navigator.ICustomProjectElement
    • org.eclipse.ui.navigator.navigatorContent –> customnavigator.popup.actionprovider.CustomRefreshActionProvider –> (enablement) –> (or) –> New –> instanceof
      • value: customnavigator.navigator.ICustomProjectElement
  3. Start the runtime workbench, create a Custom Project, go to the Custom Perspective and right click on the project. You should see the popup menu.

Why (did we do it that way?)

First, let’s do that simplest thing I can think of: have a popup menu appear with one menu item. Once that is in place the rest are mechanical steps.

In order to have a new popup menu appear Eclipse needs to recognize the resource so that it will show just the menus you want and no others. How do we do that? By setting up an enablement with our custom project type.

Perform the following on the customnavigator plug-in project.

Go to plugin.xml –> Extensions.

Remove the entry for IResource (adapt):

  • org.eclipse.ui.navigator.navigatorContent –> customnavigator.popup.actionprovider.CustomNewActionProvider –> (enablement) –> (or) –> org.eclipse.core.resources.IResource (adapt) –> Delete

That entry told Eclipse to open the popup when the selected resource was of type IResource. Well the custom project does not include the IResource. We don’t need it for now. What we do need is for the popup to open when any of our custom types is selected. Since all of the custom nodes implement ICustomProjectElement we use that instead.

  • org.eclipse.ui.navigator.navigatorContent –> customnavigator.popup.actionprovider.CustomNewActionProvider –> (enablement) –> (or) –> New –> instanceof
    • value: customnavigator.navigator.ICustomProjectElement

Why use instanceof instead of adapt? An adapt entry means that the selected object, in this case CustomProjectParent, can be adapted (converted) into the listed type, originally IResource, Since a CustomProjectParent does not implement IResource anywhere in its inheritance hierarchy the adapt will never work. Adapt means that we would have to change the custom node types while instanceof puts the onus on Eclipse. You know where my vote goes.

Perform the same steps for CustomRefreshActionProvider:

  1. org.eclipse.ui.navigator.navigatorContent –> customnavigator.popup.actionprovider.CustomRefreshActionProvider –> (enablement) –> (or) –> org.eclipse.core.resources.IResource (adapt) –> Delete
  2. org.eclipse.ui.navigator.navigatorContent –> customnavigator.popup.actionprovider.CustomRefreshActionProvider –> (enablement) –> (or) –> New –> instanceof
    • value: customnavigator.navigator.ICustomProjectElement
  3. Start the runtime workbench, create a Custom Project, go to the Custom Perspective and right click on the project. You should see the popup menu.

While I was doing this I found that the above did not work. It turned out that my Launch configuration thought I only needed 73 of the existing plug-ins while I needed a few more. It is possible that your Launch Configuration might not have enough dependencies selected. In Eclipse 3.6 RC1 this needed 80 out of 375 plug-ins. The only other plug-in I have installed is EGit.

What Just Happened?

Not a lot just happened. Changing the adapt entry to instanceof specific to our custom project type was enough to get the popup to behave the way we needed. Not a lot of work.

What we need to do next is create New behavior for:

  • New Schema Table
  • New Schema View
  • New Schema Filter
  • New Stored Procedure

That will take commands, handlers and command images.

Our current setup creates new projects, schema files and deployment files. Oops. We’ll have to change that. Next time. Maybe.

After that we’ll add properties pages to each node type fix the New menus in the toolbar and main menu.

The cat is tired. It may be time to do some genetic programming posts.

Code

<?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">
      <viewer
            viewerId="customnavigator.navigator">
         <popupMenu
               id="customnavigator.navigator#PopupMenu">
            <insertionPoint
                  name="group.new">
            </insertionPoint>
            <insertionPoint
                  name="group.build"
                  separator="true">
            </insertionPoint>
         </popupMenu>
      </viewer>
      <viewerContentBinding
            viewerId="customnavigator.navigator">
         <includes>
            <contentExtension
                  pattern="customnavigator.navigatorContent">
            </contentExtension>
         </includes>
      </viewerContentBinding>
      <viewerActionBinding
            viewerId="customnavigator.navigator">
         <includes>
            <actionExtension
                  pattern="customnavigator.popup.actionprovider.CustomNewAction">
            </actionExtension>
            <actionExtension
                  pattern="customnavigator.popup.actionprovider.CustomRefreshAction">
            </actionExtension>
         </includes>
      </viewerActionBinding>
   </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>
      <actionProvider
            class="customnavigator.popup.actionprovider.CustomNewActionProvider"
            id="customnavigator.popup.actionprovider.CustomNewAction">
         <enablement>
            <or>
               <instanceof
                     value="customnavigator.navigator.ICustomProjectElement">
               </instanceof>
               <adapt
                     type="java.util.Collection">
                  <count
                        value="0">
                  </count>
               </adapt>
            </or>
         </enablement>
      </actionProvider>
      <actionProvider
            class="customnavigator.popup.actionprovider.CustomRefreshActionProvider"
            id="customnavigator.popup.actionprovider.CustomRefreshAction">
         <enablement>
            <or>
               <instanceof
                     value="customnavigator.navigator.ICustomProjectElement">
               </instanceof>
               <adapt
                     type="java.util.Collection">
                  <count
                        value="0">
                  </count>
               </adapt>
            </or>
         </enablement>
      </actionProvider>
      <commonWizard
            type="new"
            wizardId="customplugin.wizard.new.custom">
         <enablement></enablement>
      </commonWizard>
   </extension>

</plugin>