【问题标题】:Why am I not getting a popupmenu to appear in Eclipse?为什么我没有在 Eclipse 中出现弹出菜单?
【发布时间】:2016-04-02 00:50:17
【问题描述】:

我正在阅读一本 Eclipse 插件书。我之前有一个Simple SWT/JFace exercise fails to find handler 的问题。

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

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

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

“查看者”是TableViewerTreeViewer

我添加了以下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”的“迭代”没有找到与该类型匹配的选择 (java.util.TimeZone)?

【问题讨论】:

    标签: java eclipse eclipse-plugin


    【解决方案1】:

    您必须在上下文菜单中添加一个菜单项,告诉 Eclipse 将附加菜单项放在哪里:

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

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

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

    getSite().setSelectionProvider(viewer);
    

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

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

    【讨论】:

    • 当我尝试添加第一行时,它指出菜单上没有“添加”方法。
    • 那行得通。您能否再详细说明一下这些要点(主要是前两个)?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-16
    • 1970-01-01
    • 1970-01-01
    • 2019-12-03
    • 2022-12-10
    • 1970-01-01
    • 2014-12-04
    相关资源
    最近更新 更多