Search code examples
swteclipse-rcpe4

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


In one of my eclipse e4 application JMSToolBox, some data is displayed in aTableViewer
A contextual menu is defined in the e4 model file (e4xmi) and linked to theTableViewer like this

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

Attached to the contextual menu in the e4 model, a "menu item" is linked to a"Dynamic Menu Contribution" class that dynamically add the menu items to the menu:

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

Now, what I want to do is to capture the data in the underlying cell where the contextual menu has been activated, and get that value back in the method annotated by"@AboutToShow" in order to addMDirectMenuItementries to the contextual menu with a label containing that value
Q: how to do that with eclipse rcp e4?

In the attached picture, the right click happened in the cell with content="ID:414d5120514d41414544202020202020ee4bb25612666920". I would like to get this value back in the @AboutToShowmethod and add menu items to the"Open Payload as..."menu based on that value Thanks

enter image description here


Solution

  • I found a way to do it!
    I'm not sure it is the best way, but at least it works and it is quite simple

    The following code is here to illustrate the idea, it is not valid Java. In the part that manage theTableViewer:

    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());
       }
    }
    

    Real code implementation: JTBSessionContentViewPart , JTBFocusCellHighlighter and FilterMenu