How to create an Eclipse plugin extension that displays various context menu items when a user clicks a marker?

This is a question about developing plugins for the Eclipse platform:

I want to add a menu item to the default menu that appears when you right-click on the IMarker view (all markers will be a good start).

I had some success with implementing IMarkerResolution and linking to it in my plugin.xml

<extension point="org.eclipse.ui.ide.markerResolution">
<markerResolutionGenerator
  markerType="my.stuff.mymarker" 
  class="my.stuff.MyResolutionGenerator">
</markerResolutionGenerator>
</extension>

but instead of accessing my code through eclipse quick fix functionality, I want to add my own menu text instead of “quick fixes” and not show the action along with quick fix options. Being able to perform an action (by double) clicking on a marker is also very useful.

I am using eclipse 3.5.2 for my current project.

Thanks in advance!

:

<extension point="org.eclipse.ui.menus">
<menuContribution
    class="my.stuff.MarkerContributionFactory"
    locationURI="popup:#AbstractTextEditorRulerContext?after=additions">
  <dynamic
         class="my.stuff.MarkerMenuContribution"
         id="my.stuff.MarkerMenuContribution">
  </dynamic>
</menuContribution>
</extension>

public class MarkerContributionFactory extends ExtensionContributionFactory{

@Override
public void createContributionItems(IServiceLocator serviceLocator, IContributionRoot additions){
    ITextEditor editor = (ITextEditor) 
        PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart();

    additions.addContributionItem(new MarkerMenuContribution(editor), null);
}
}

public class MarkerMenuContribution extends ContributionItem{

private ITextEditor editor;
private IVerticalRulerInfo rulerInfo;
private List<IMarker> markers;

public MarkerMenuContribution(ITextEditor editor){
    this.editor = editor;
    this.rulerInfo = getRulerInfo();
    this.markers = getMarkers();
}

private IVerticalRulerInfo getRulerInfo(){
    return (IVerticalRulerInfo) editor.getAdapter(IVerticalRulerInfo.class);
}

private List<IMarker> getMarkers(){
    List<IMarker> clickedOnMarkers = new ArrayList<IMarker>();
    for (IMarker marker : getAllMarkers()){
        if (markerHasBeenClicked(marker)){
            clickedOnMarkers.add(marker);
        }
    }

    return clickedOnMarkers;
}

//Determine whether the marker has been clicked using the ruler mouse listener
private boolean markerHasBeenClicked(IMarker marker){
    return (marker.getAttribute(IMarker.LINE_NUMBER, 0)) == (rulerInfo.getLineOfLastMouseButtonActivity() + 1);
}

//Get all My Markers for this source file
private IMarker[] getAllMarkers(){
    return ((FileEditorInput) editor.getEditorInput()).getFile()
        .findMarkers("defined.in.plugin.xml.mymarker", true, IResource.DEPTH_ZERO);
}

@Override
//Create a menu item for each marker on the line clicked on
public void fill(Menu menu, int index){
    for (final IMarker marker : markers){
        MenuItem menuItem = new MenuItem(menu, SWT.CHECK, index);
        menuItem.setText(marker.getAttribute(IMarker.MESSAGE, ""));
        menuItem.addSelectionListener(createDynamicSelectionListener(marker));
    }
}

//Action to be performed when clicking on the menu item is defined here
private SelectionAdapter createDynamicSelectionListener(final IMarker marker){
    return new SelectionAdapter(){
        public void widgetSelected(SelectionEvent e){
            System.out.println(marker.getAttribute(IMarker.MESSAGE, ""));
        }
    };
}
}
+5
1

http://www.eclipse.org/articles/article.php?file=Article-action-contribution/index.html. .

, :

   <extension
         point="org.eclipse.ui.popupMenus">
      <viewerContribution
            targetID="#CompilationUnitRulerContext"
            id="Q4098270.contribution1">
                     <menu
               label="New Submenu"
               path="additions"
               id="Q4098270.menu1">
            <separator
                  name="group1">
            </separator>
         </menu>
         <action
               label="New Action"
               class="q4098270.popup.actions.NewAction"
               menubarPath="Q4098270.menu1/group1"
               id="Q4098270.newAction">
         </action>
      </viewerContribution>
   </extension>

todo, , - , , .

: . , , , , .

Eclipse , , . . JADclipse . , " " . . , plugin.xml, . . , , Google:)

+4

All Articles