The Eclipse command framework allows you to create a generic command and handler. However, when you create an extension point that targets ‘copy’ the runtime complains:
<extension point="org.eclipse.ui.handlers">
<handler class="example.Handler" commandId="org.eclipse.ui.edit.copy"/>
</extension>
!MESSAGE Conflicting handlers for org.eclipse.ui.edit.copy: {org.eclipse.ui.internal.handlers.WidgetMethodHandler:copy} vs {example.Handler}
If you add an activeWhen clause, so that the special handler is only invoked when an object of a particular type is selected in a viewer, then you get an exception when you try and copy:
<extension point="org.eclipse.ui.handlers">
<handler class="example.Handler" commandId="org.eclipse.ui.edit.copy">
<activeWhen>
<with
variable="selection">
<iterate
ifEmpty="false"
operator="or">
<instanceof
value="sample.Class">
</instanceof>
</iterate>
</with>
</activeWhen>
</handler>
</extension>
Caused by: org.eclipse.core.commands.NotHandledException: There is no handler to execute for command org.eclipse.ui.edit.copy
What’s the right way of using the commands framework to provide a specific copy operation for objects of a particular type?
Your
activeWhenclause looks acceptable. What will matter is what selection you are going for. ex:I originally tried
instanceofin my activeWhen expression to get access to the IMarkers in the markers view, but that didn’t work (the objects don’t implement IMarker). I had toadaptthem to get my handler to work correctly. This would apply to .java files in the Package Explorer as well, they adapt to anIResourcebut they’re actually IJavaElements or something.