为什么我没有弹出菜单出现在Eclipse中?(Why am I not getting a popupmenu to appear in Eclipse?)

编程入门 行业动态 更新时间:2024-10-14 18:17:05
为什么我没有弹出菜单出现在Eclipse中?(Why am I not getting a popupmenu to appear in Eclipse?)

我正在阅读Eclipse插件书。 我之前有一个简单的SWT / JFace练习问题无法找到处理程序 。

我现在正在进行弹出菜单练习。 我有一个树视图和一个表视图,我正在尝试在两个视图上呈现相同的弹出菜单(这完全来自本书中的说明)。

在每个视图的createPartControl方法中,我添加了如下代码:

MenuManager manager = new MenuManager("#PopupMenu"); Menu menu = manager.createContextMenu(viewer.getControl()); viewer.getControl().setMenu(menu); getSite().registerContextMenu(manager, viewer);

其中“viewer”是TableViewer或TreeViewer 。

我添加了以下menuContribution :

 <menuContribution allPopups="false" locationURI="popup:org.eclipse.ui.popup.any">
     <command commandId="com.packtpub.e4.clock.ui.command.showTheTime"
           label="Show the Time" style="push">
        <visibleWhen checkEnabled="false">
           <with variable="selection">
              <iterate ifEmpty="false">
                 <adapt type="java.util.TimeZone">
                 </adapt>
              </iterate>
           </with>
        </visibleWhen>
     </command>
 </menuContribution>
 

以及此命令:

 <command description="Shows the Time"
        id="com.packtpub.e4.clock.ui.command.showTheTime"
        name="Show the Time">
 </command>
 

和这个处理程序:

 <handler class="com.packtpub.e4.clock.ui.handlers.ShowTheTime"
        commandId="com.packtpub.e4.clock.ui.command.showTheTime">
 </handler>
 

以下是处理程序类:

public class ShowTheTime extends AbstractHandler { public Object execute(ExecutionEvent event) { ISelection sel = HandlerUtil.getActiveWorkbenchWindow(event).getSelectionService().getSelection(); if (sel instanceof IStructuredSelection && !sel.isEmpty()) { Object value = ((IStructuredSelection)sel).getFirstElement(); if (value instanceof TimeZone) { SimpleDateFormat sdf = new SimpleDateFormat(); sdf.setTimeZone((TimeZone) value); MessageDialog.openInformation(null, "The time is", sdf.format(new Date())); } } return null; } }

当我运行实例并右键单击树或表时,我得到......什么都没有。 什么都没发生。

带有“adapt”的“iterate”是否有可能找不到匹配该类型的选择( java.util.TimeZone )?

I'm working through an Eclipse plugins book. I had an earlier question of Simple SWT/JFace exercise fails to find handler.

I'm now working on a popupmenu exercise. I have a tree view and a table view, and I'm attempting to render the same popup menu on both views (this is all from the instructions in the book).

In the createPartControl method of each view, I've added code like the following:

MenuManager manager = new MenuManager("#PopupMenu"); Menu menu = manager.createContextMenu(viewer.getControl()); viewer.getControl().setMenu(menu); getSite().registerContextMenu(manager, viewer);

where the "viewer" is either a TableViewer or a TreeViewer.

I added the following menuContribution:

 <menuContribution allPopups="false" locationURI="popup:org.eclipse.ui.popup.any">
     <command commandId="com.packtpub.e4.clock.ui.command.showTheTime"
           label="Show the Time" style="push">
        <visibleWhen checkEnabled="false">
           <with variable="selection">
              <iterate ifEmpty="false">
                 <adapt type="java.util.TimeZone">
                 </adapt>
              </iterate>
           </with>
        </visibleWhen>
     </command>
 </menuContribution>
 

along with this command:

 <command description="Shows the Time"
        id="com.packtpub.e4.clock.ui.command.showTheTime"
        name="Show the Time">
 </command>
 

and this handler:

 <handler class="com.packtpub.e4.clock.ui.handlers.ShowTheTime"
        commandId="com.packtpub.e4.clock.ui.command.showTheTime">
 </handler>
 

The following is the handler class:

public class ShowTheTime extends AbstractHandler { public Object execute(ExecutionEvent event) { ISelection sel = HandlerUtil.getActiveWorkbenchWindow(event).getSelectionService().getSelection(); if (sel instanceof IStructuredSelection && !sel.isEmpty()) { Object value = ((IStructuredSelection)sel).getFirstElement(); if (value instanceof TimeZone) { SimpleDateFormat sdf = new SimpleDateFormat(); sdf.setTimeZone((TimeZone) value); MessageDialog.openInformation(null, "The time is", sdf.format(new Date())); } } return null; } }

When I run the instance and right-click on the tree or table, I get ... nothing. Nothing happens.

Is it possible that the "iterate" with "adapt" isn't finding a selection matching that type (java.util.TimeZone)?

最满意答案

您必须在上下文菜单中添加一个菜单项,告诉Eclipse将其他菜单项放在何处:

manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));

menuContribution将仅添加到其中一个条目具有MB_ADDITIONS id的菜单中(除非您指定allPopus="true"或指定特定位置)。

您还必须告诉视图站点您的表/树是该部件的选择提供者:

getSite().setSelectionProvider(viewer);

选择服务仅在确定所选内容时查看视图/编辑器站点的选择提供程序。

表/树内容提供程序中的对象必须实现java.util.TimeZone或使用适配器管理器来适应该类。

You must add a menu item to the context menu telling Eclipse where to put the additional menu items:

manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));

The menuContribution will only add to a menu which has the MB_ADDITIONS id for one of its entries (unless you specify allPopus="true" or specify a specific position).

You must also tell the view site that your table/tree is the selection provider for the part:

getSite().setSelectionProvider(viewer);

The selection service only looks at the selection provider for a view / editor site when determining what is selected.

The objects you have in your table/tree content provider must implement java.util.TimeZone or use the adapter manager to adapt to that class.

更多推荐

本文发布于:2023-07-15 17:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1116660.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:出现在   弹出   菜单   popupmenu   Eclipse

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!