如何捕获在 Eclipse e4 中激活上下文菜单的 TableViewer 单元格的值?

How to capture the value of a cell of a TableViewer where a contextual menu has been activated in eclipse e4?

在我的一个 eclipse e4 应用程序 JMSToolBox 中,一些数据显示在 TableViewer
在 e4 模型文件 (e4xmi) 中定义了一个上下文菜单,并链接到 TableViewer,就像这样

menuService.registerContextMenu(tableViwere.getTable(), <name of the e4 part menu>);

附加到 e4 模型中的上下文菜单,"menu item" 链接到动态添加菜单项到菜单的 "Dynamic Menu Contribution" class:

public class VisualizerShowPayloadAsMenu {
   @Inject private EModelService       modelService;
   @AboutToShow
   public void aboutToShow(EModelService modelService, List<MMenuElement> items) {
      // Not the real code..., illustrate adding a dynamic menu item to the contextual menu
      MDirectMenuItem dynamicItem = modelService.createModelElement(MDirectMenuItem.class);
      dynamicItem.setLabel(<name..>);
      dynamicItem.setContributorURI(Constants.BASE_CORE_PLUGIN);// "platform:/plugin/org.titou10.jtb.core");
      dynamicItem.setContributionURI(Constants.VISUALIZER_MENU_URI);// "bundleclass://org.titou10.jtb.core/org.titou10.jtb.visualizer.ui.VisualizerShowPayloadAsHandler");
      items.add(dynamicItem);
   }

现在,我要做的是捕获已激活上下文菜单的底层单元格中的数据,并按顺序在 "@AboutToShow" 注释的方法中取回该值 将MDirectMenuItem条目添加到上下文菜单,标签包含该值
问:如何使用 eclipse rcp e4 做到这一点?

在附图中,右击发生在content="ID:414d5120514d41414544202020202020ee4bb25612666920"的单元格中。我想在 @AboutToShow 方法中取回此值,并根据该值将菜单项添加到 "Open Payload as..." 菜单 谢谢

我找到方法了!
我不确定这是最好的方法,但至少它有效并且非常简单

下面的代码是为了说明这个想法,它是无效的Java。 在管理TableViewer的部分:

TableViewer tableViewer = new TableViewer(composite, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
{...}
new TableViewerFocusCellManager(tableViewer, new JTBFocusCellHighlighter(tableViewer, windowContext));

JTBFocusCellHighlighterclass:

public class JTBFocusCellHighlighter extends FocusCellHighlighter {
   private IEclipseContext windowContext;
   private Table           table;
   public JTBFocusCellHighlighter(ColumnViewer viewer, IEclipseContext windowContext) {
      super(viewer);
      this.windowContext = windowContext;
      this.table = ((TableViewer) viewer).getTable();
   }
   @Override
   protected void focusCellChanged(ViewerCell newCell, ViewerCell oldCell) {
      super.focusCellChanged(newCell, oldCell);
      // Capture the content of the cell (or other info..) and store it in Eclipse Context
      windowContext.set("key", newCell.getText());
      TableColumn tableColumn = table.getColumn(newCell.getColumnIndex());
   }
}

实际代码实现:JTBSessionContentViewPart , JTBFocusCellHighlighter and FilterMenu