Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make an Eclipse plugin extension which displays different context menu items when the user clicks a marker?

This is a question specifically about plugin development for the Eclipse platform:

I want to add a menu item to the default menu which appears when right clicking a kind of IMarker (all markers would be a good start).

I have had some success with implementing IMarkerResolution and referring 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 the eclipse quick fix functionality, I want to add my own menu text instead of 'quick fixes' and not have to display the action alongside the quick fix options. Being able to run an action by (double) clicking on a marker would be very useful as well.

I am using eclipse 3.5.2 for my current project.

Thanks in advance!

Update I have resolved this:

<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's 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, ""));
        }
    };
}
}
like image 676
Matt Avatar asked Oct 13 '22 21:10

Matt


1 Answers

you should take the tutorial at http://www.eclipse.org/articles/article.php?file=Article-action-contribution/index.html. It is a fine article about basic UI stuff.

The following let you define a popup menu action if you click on ruler:

   <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>

Problem and todo which i have no time currently for is that it is not displayed only for markers, but all stuff on ruler.

Explanation: In eclipse menu contributions themselves are extension points. So if you right click to see popup menu, the system will check all of the implementors of the extension point, and will check that wich are applicable for the given object or view.

Some advice for future

Since Eclipse platform is far from user friendly documented one, hunting proper extension points is the most complex as far as I experienced. I always start from existing solutions in platform itself. So install JADclipse, and start to look for texts displayed. For example look for the text "Quick fix" in plugins folder. You will find a property file. You see the key of the property, then look for the class or plugin.xml it is in. Then you wil see a living example for your problem. This always work, and even better than Google :)

like image 182
Gábor Lipták Avatar answered Oct 31 '22 18:10

Gábor Lipták