为什么我没有弹出菜单出现在Eclipse中?

时间:2015-10-17 22:09:46

标签: java eclipse eclipse-plugin

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

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

在每个视图的ArrayAdapter<CharSequence> location_adapter = ArrayAdapter.createFromResource(this, R.array.locations, android.R.layout.simple_spinner_item); lang_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(location_adapter); 方法中,我添加了以下代码:

createPartControl

其中“查看者”是 MenuManager manager = new MenuManager("#PopupMenu"); Menu menu = manager.createContextMenu(viewer.getControl()); viewer.getControl().setMenu(menu); getSite().registerContextMenu(manager, 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>

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

带有“adapt”的“iterate”是否有可能找不到与该类型匹配的选择(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; } } )?

1 个答案:

答案 0 :(得分:0)

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

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

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

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

getSite().setSelectionProvider(viewer);

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

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