Howto: Register Selection Converters

From Eclipsepedia

Revision as of 14:18, 22 April 2008 by Yuri.strot.xored.com (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

< To: Tigerstripe_APIs

Annotation Framework provides some UI features to allow user easily navigate through annotations. Generally there are opening annotated object in the some specific location (for example, resources will be opened in the Projects Explorer and java elements will be opened in the Package Explorer or Java Editor) and showing currently selected annotated object. To provide this features for new annotation source (like resources and java elements) we need to register Selection Converter. It can be done with the "org.eclipse.tigerstripe.annotation.ui.workbenchAnnotationProvider" extension point:

   <extension
           point="org.eclipse.tigerstripe.annotation.ui.workbenchAnnotationProvider">
       <selectionConverter
           class="org.eclipse.tigerstripe.annotation.resource.ui.ResourceSelectionConverter"/>
   </extension>

This extension point provide selection converter class implements ISelectionConverter interface:

 public class ResourceSelectionConverter implements ISelectionConverter {
   public ISelection convert(IWorkbenchPart part, ISelection selection) {
       return null;
   }
   public void open(ISelection selection) {
       if (isResourceSelection(selection)) {
           IWorkbenchPage activePage = WorkbenchUtil.getPage();
           if (activePage != null) {
               IViewPart view;
               try {
                   view = activePage.showView(ProjectExplorer.VIEW_ID);
                   if (view instanceof ISetSelectionTarget) {
                       ((ISetSelectionTarget) view).selectReveal(selection);
                   }
               }
               catch (PartInitException e) {
                   e.printStackTrace();
               }
           }
       }
   }
   ...
 }

With the open(ISelection) method this class open resource in the ProjectExplorer.

Usually, feature with currently selected annotated object will be supported automatically, because Annotation Framework uses ISelectionService to listen all selection changing in the framework and map current selection to the annotations if possible. But some times selection provided with an ISelectionService can be used directly. For example, Java Editor provide ITextSelection (that can not be annotated) which can be converter to the IJavaElement (that can be annotated). So, we can listen java element selection directly from the Java Editor. To map one selection to another we can use convert(IWorkbenchPart, ISelection) of the selection converter.

See org.eclipse.tigerstripe.annotation.java.ui plug-in as a example of the selection converter.