如何制作一个 Eclipse 插件扩展,当用户单击标记时显示不同的上下文菜单项?

发布于 2024-09-30 11:49:30 字数 3317 浏览 7 评论 0原文

这是一个专门关于 Eclipse 平台插件开发的问题:

我想向默认菜单添加一个菜单项,该菜单项在右键单击一种 IMarker 时出现(所有标记都是一个好的开始)。

我在实现 IMarkerResolution 并在我的plugin.xml 中引用它方面取得了一些成功,

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

但我不想通过 eclipse 快速修复功能访问我的代码,而是想添加自己的菜单文本而不是“快速修复”,并且不必显示与快速修复选项一起采取行动。能够通过(双击)单击标记来运行操作也非常有用。

我当前的项目使用 eclipse 3.5.2。

提前致谢!

更新我已经解决了这个问题:

<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, ""));
        }
    };
}
}

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, ""));
        }
    };
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南冥有猫 2024-10-07 11:49:30

您应该在 http://www .eclipse.org/articles/article.php?file=Article-action-contribution/index.html。这是一篇关于基本 UI 内容的好文章。

如果您单击标尺,以下内容可让您定义弹出菜单操作:

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

我目前没有时间处理的问题和待办事项是,它不仅显示标记,还显示标尺上的所有内容。

解释:
在 Eclipse 中,菜单贡献本身就是扩展点。因此,如果您右键单击查看弹出菜单,系统将检查扩展点的所有实现者,并将检查其是否适用于给定的对象或视图。

对未来的一些建议

由于 Eclipse 平台远非用户友好的文档平台,因此据我所知,寻找合适的扩展点是最复杂的。我总是从平台本身的现有解决方案开始。因此,安装JADclipse,并开始查找显示的文本。例如,在插件文件夹中查找文本“快速修复”。您将找到一个属性文件。您会看到属性的键,然后查找它所在的类或plugin.xml。然后您将看到您的问题的活生生的示例。这总是有效的,甚至比谷歌更好:)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文