Content Sensitive Object Contributions

Last modified June 14, 2004

Context menus for some applications have previously only been able to exclude some menu options based on peripheral information known about the resources selected (the number of resources selected, the physical name of the file, the type of the resource, etc.). There are some cases where a restricted amount of information known about the contents of the resource would significantly reduce unusable options from the menus. Consider XML files as a good example of where such information would be useful. There are numerous situations where an action is applicable for one type of XML file but not another. For example, some XML files contain Ant scripts. The action "Run Ant..." makes sense in its context menu. But this action is not applicable to an XML file used to define a plug-in.

The notion of a content type has been added to Eclipse. A new extension point, org.eclipse.core.runtime.contentTypes allows plug-ins to contribute to the Platform content type catalog. Further, classes called Content Type Describers have been added.

For developers providing their own content types, two customizable, built-in content type describers are available: BinarySignatureDescriber (for binary content types) and XMLRootElementContentDescriber (for text, XML based content types). Plug-in providers may create their own content describers. The Platform Plug-in Developer Guide (Programmer's Guide -> Runtime overview -> Content types) describes this in more detail.

XMLRootElementContentDescriber

It will now be possible to define object contributions which are specific to an XML file with a given top level tag or which specify a given DTD. To do this, define an extension to the org.eclipse.core.runtime.contentTypes extension point with a describer class of XMLRootElementContentDescriber and parameters indicating the top level tag name or the dtd name as follows:

   <extension
       point="org.eclipse.core.runtime.contentTypes">
       <content-type
           id=<id>
           name=<name> 	
           base-type="org.eclipse.core.runtime.xml">
           <describer class="org.eclipse.core.runtime.content.XMLRootElementContentDescriber">
               <parameter name="element" value=tagValue />
           </describer>
       </content-type>
   </extension>
or
   <extension
       point="org.eclipse.core.runtime.contentTypes">
       <content-type
           id=<id>
           name=<name> 	
           base-type="org.eclipse.core.runtime.xml">
           <describer class="org.eclipse.core.runtime.content.XMLRootElementContentDescriber">
               <parameter name="dtd" value=dtdValue />
           </describer>
       </content-type>
    </extension>

where tagValue represents the name of the top level tag to match and

dtdValue represents the name of the DTD as seen in the XML file.

Consider the following object contribution in a plugin.xml file:

   <extension
       point="org.eclipse.core.runtime.contentTypes">
       <content-type 
           id="topElementContentType"
           name="Tests top-level element recognition" 	
           base-type="org.eclipse.core.runtime.xml"
           priority="high">
           <describer class="org.eclipse.core.runtime.content.XMLRootElementContentDescriber">
               <parameter name="element" value="myTag" />
           </describer>
       </content-type>
   </extension>

   <extension
       point="org.eclipse.core.runtime.contentTypes">
       <content-type 
           id="dtdContentType"
           name="Tests dtd element recognition" 	
           base-type="org.eclipse.core.runtime.xml"
           priority="high">
           <describer class="org.eclipse.core.runtime.content.XMLRootElementContentDescriber">
               <parameter name="dtd" value="myDTD.xml" />
           </describer>
       </content-type>
   </extension>
   <extension point="org.eclipse.ui.popupMenus">
       <objectContribution
           id="org.eclipse.ui.examples.objectContributions"
           objectClass="org.eclipse.core.resources.IFile"
           nameFilter="*.xml">
           <visibility>
               <or>
                   <objectState
                       name="contentTypeId"
                       value="org.eclipse.ui.examples.topElementContentType"/>
                   <objectState
                       name="contentTypeId"
                       value="org.eclipse.ui.examples.dtdContentType"/>
               </or>
           </visibility>
           <action id="org.eclipse.ui.examples.objectContributions.action1"
               label="%PopupMenus.action"
               icon="icons/ctool16/openbrwsr.gif"
               menubarPath="additions"
               class="org.eclipse.ui.examples.objectContributions.PopupMenuActionDelegate"
               enablesFor="1">
           </action>
       </objectContribution>
   </extension>

This will make action1 visible for any IFile with a name matching *.xml provided it contains myTag as the top level XML tag or it uses the DTD called myDTD.xml. So the following XML files will match:

<?xml version="1.0" encoding="UTF-8"?>
<myTag
  id="org.eclipse.ui.workbench"
  name="%pluginName"
  version="3.0.0"
  provider-name="%providerName"
  class="org.eclipse.ui.internal.WorkbenchPlugin">
</myTag>

Or

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Book SYSTEM "myDTD.xml">
<fragment
   id="org.eclipse.ui.workbench"
   name="%pluginName"
	version="3.0.0"
	provider-name="%providerName"
	class="org.eclipse.ui.internal.WorkbenchPlugin">
	<runtime>
	   <library name="workbench.jar">
	      <export name="*"/>
		  <packages prefixes="org.eclipse.ui, org.eclipse.jface"/>
	   </library>
	</runtime>
</fragment>

BinarySignatureDescriber

The BinarySignatureDescriber is a content describer to detect a specified binary 'signature' at a given offset within a file. This describer is used in the same fashion as the XMLRootElementContentDescriber with the exception that it takes parameters "signature", "offset" and "required" instead of "element" or "dtd". The Javadoc for BinarySignatureDescriber gives complete details on this content describer's class usage.