TOP

Extend the ‚Source‘ context menu of the JDT

In this post I describe how you can contribute a context menu entry to the ‚Source‘ menu of the JDT.

  • First of all you have to create a new command. Therefore you must use the extension point org.eclipse.ui.commands. See the following example from the CQTP plugin:

    <extension
    point="org.eclipse.ui.commands">
    <command
    	id="de.dainel.cleanqualifiedtypes.command"
    	name="%action.label">
    </command>
    </extension>
  • In the next step you must add a menu contribution to integrate your command into the context menu. Use the extension point org.eclipse.ui.menus as in the following example. As you can see, it is visible when the ‚Source‘ context menu is visible and one of the registred types is selected. It is also visible within the editor of a Java file.

    <extension
    point="org.eclipse.ui.menus">
    <menuContribution
    	allPopups="true"
    	locationURI="popup:org.eclipse.jdt.ui.source.menu?after=additions">
    	<separator
    		name="cleanQualifiedTypes"
    		visible="true">
    	</separator>
    	<command
    		commandId="de.dainel.cleanqualifiedtypes.command"
    		style="push">
    		<visibleWhen>
    		<or>
    		<with variable="activeMenuSelection">
    		<iterate>
    		<or>
    		<adapt type="org.eclipse.jdt.core.IJavaProject"/>
    		<adapt type="org.eclipse.jdt.core.IPackageFragment"/>
    		<adapt type="org.eclipse.jdt.core.IPackageFragmentRoot"/>
    		<adapt type="org.eclipse.jdt.core.ICompilationUnit"/>
    		</or>
    		</iterate>
    		</with>
    		<with variable="activeEditorId">
    		<equals value="org.eclipse.jdt.ui.CompilationUnitEditor" />
    		</with>
    		</or>
    		</visibleWhen>
    	</command>
    	<separator
    		name="additions"
    		visible="false">
    	</separator>
    </menuContribution>
    </extension>
  • Last but not least you must add a handler which is responsible for the behaviour of your command. Use the extension point org.eclipse.ui.handlers and see the following example:

    <extension
    point="org.eclipse.ui.handlers">
    <handler
    	commandId="de.dainel.cleanqualifiedtypes.command"
    	class="de.dainel.cleanqualifiedtypes.handlers.
    		CleanQualifiedTypesHandler">
    </handler>
    </extension>

Leave a Reply

Your email is never published nor shared.

You may use these HTML tags and attributes:<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*