0

我有一个TreeViewer,我创建了一个包含一些操作的上下文菜单。

使用下面的代码片段,我可以获得所有树项的上下文菜单。

protected void createMasterPart(final IManagedForm managedForm, final Composite parent)
{
 Tree t = toolkit.createTree(client, SWT.NULL);
 GridData gd = new GridData(GridData.FILL_BOTH);
 gd.heightHint = 20;
 gd.widthHint = 100;
 t.setLayoutData(gd);
 toolkit.paintBordersFor(client);
 section.setClient(client);
 final SectionPart spart = new SectionPart(section);
 managedForm.addPart(spart);
 viewer = new TreeViewer(t);
 viewer.addSelectionChangedListener(new ISelectionChangedListener() 
 {
 public void selectionChanged(SelectionChangedEvent event) 
 {
    managedForm.fireSelectionChanged(spart, event.getSelection());
 }
 });
 viewer.setContentProvider(new MasterTreeContentProvider());
 viewer.setInput(page.getEditor().getEditorInput());

 m_newKeyAction = new AddNewKeyAction(viewer, parent.getShell());

 m_newValueAction = new AddNewValueAction(viewer, parent.getShell());

 hookContextMenu();
} 
private void hookContextMenu()
{
   MenuManager menuMgr = new MenuManager();
   menuMgr.setRemoveAllWhenShown(true);
   menuMgr.addMenuListener(new IMenuListener() {
   public void menuAboutToShow(IMenuManager manager) 
   {
   fillContextMenu(manager);
   }
   });
   Menu menu = menuMgr.createContextMenu(viewer.getControl());
   viewer.getControl().setMenu(menu);
}

protected void fillContextMenu( IMenuManager manager )
{   
    manager.add( m_newKeyAction );
    manager.add( m_newValueAction );
}

我希望仅针对特定类型的树项显示此上下文菜单。请让我知道如何完成这项工作。

4

0 回答 0