下面列出了怎么用org.eclipse.jface.action.IMenuManager的API类实例代码及写法,或者点击链接到github查看源代码。
public void fillContextMenu(IMenuManager manager) {
MenuManager javaSearchMM = new MenuManager(getName(), IContextMenuConstants.GROUP_SEARCH);
// addAction(fFindReferencesAction, javaSearchMM);
addAction(fFindReferencesInProjectAction, javaSearchMM);
// addAction(fFindReferencesInHierarchyAction, javaSearchMM);
javaSearchMM.add(new Separator());
//
// Iterator iter= SearchUtil.getLRUWorkingSets().sortedIterator();
// while (iter.hasNext()) {
// addWorkingSetAction((IWorkingSet[]) iter.next(), javaSearchMM);
// }
// addAction(fFindReferencesInWorkingSetAction, javaSearchMM);
if (!javaSearchMM.isEmpty())
manager.appendToGroup(fGroupId, javaSearchMM);
}
@Override
public void init(IPageSite site) {
super.init(site);
IMenuManager menuManager = site.getActionBars().getMenuManager();
menuManager.insertBefore(IContextMenuConstants.GROUP_PROPERTIES, new Separator(GROUP_FILTERING));
fActionGroup.fillActionBars(site.getActionBars());
menuManager.appendToGroup(IContextMenuConstants.GROUP_PROPERTIES, new Action(SearchMessages.JavaSearchResultPage_preferences_label) {
@Override
public void run() {
String pageId= "org.eclipse.search.preferences.SearchPreferencePage"; //$NON-NLS-1$
String[] displayedPages= { pageId,
"org.eclipse.ui.editors.preferencePages.Annotations", //$NON-NLS-1$
"org.eclipse.ui.preferencePages.ColorsAndFonts" //$NON-NLS-1$
};
PreferencesUtil.createPreferenceDialogOn(JavaPlugin.getActiveWorkbenchShell(), pageId, displayedPages, null).open();
}
});
}
/**
* Adds the filter actions to the menu
*
* @param mm the menu manager
*/
public void fillViewMenu(IMenuManager mm) {
if (mm.find(IWorkingSetActionGroup.ACTION_GROUP) == null) {
mm.add(new Separator(IWorkingSetActionGroup.ACTION_GROUP));
}
add(mm, fSelectWorkingSetAction);
add(mm, fClearWorkingSetAction);
add(mm, fEditWorkingSetAction);
add(mm, new Separator());
add(mm, new Separator(LRU_GROUP));
fMenuManager= mm;
fMenuListener= new IMenuListener() {
public void menuAboutToShow(IMenuManager manager) {
removePreviousLRUWorkingSetActions(manager);
addLRUWorkingSetActions(manager);
}
};
fMenuManager.addMenuListener(fMenuListener);
}
public void fillContextMenu(IMenuManager menu) {
IStructuredSelection selection = (IStructuredSelection) getContext().getSelection();
boolean anyResourceSelected = !selection.isEmpty()
&& ResourceSelectionUtil.allResourcesAreOfType(selection, IResource.PROJECT | IResource.FOLDER
| IResource.FILE);
copyAction.selectionChanged(selection);
// menu.appendToGroup(ICommonMenuConstants.GROUP_EDIT, copyAction);
pasteAction.selectionChanged(selection);
// menu.insertAfter(copyAction.getId(), pasteAction);
// menu.appendToGroup(ICommonMenuConstants.GROUP_EDIT, pasteAction);
if (anyResourceSelected) {
deleteAction.selectionChanged(selection);
// menu.insertAfter(pasteAction.getId(), deleteAction);
menu.appendToGroup(ICommonMenuConstants.GROUP_EDIT, deleteAction);
}
}
@Override
public void fillContextMenu(final IMenuManager menu) {
// {@link N4JSProjectActionGroup} does enablement-logic
// on its own, thus always invoke it here
projectGroup.fillContextMenu(menu);
// Only delegate to {@link N4JSWorkingSetActionProvider},
// if the current selection contains working sets.
if (selectionContainsWorkingSet) {
workingSetActionProvider.fillContextMenu(menu);
}
if (assignWorkingSetsAction.isEnabled()) {
menu.appendToGroup(IContextMenuConstants.GROUP_BUILD, assignWorkingSetsAction);
}
}
/**
* Creates the context menu for the given object. Gets the action from the
* actionRegistry and adds the action to the menu.
*
* @param menu
* the menu
* @param object
* the object
*/
public void createContextMenu( TreeViewer sourceViewer, Object object,
IMenuManager menu )
{
super.createContextMenu( sourceViewer, object, menu );
if ( ( (LevelHandle) object ).canEdit( ) )
{
menu.insertAfter( IWorkbenchActionConstants.MB_ADDITIONS,
new EditCubeLevelAction( object,
Messages.getString( "CubeLevelNodeProvider.menu.text" ) ) ); //$NON-NLS-1$
}
menu.insertBefore( IWorkbenchActionConstants.MB_ADDITIONS + "-refresh", //$NON-NLS-1$
new ShowPropertyAction( object ) );
menu.insertAfter( IWorkbenchActionConstants.MB_ADDITIONS + "-refresh", new Separator( ) ); //$NON-NLS-1$
IAction action = new RefreshAction( sourceViewer );
if (action.isEnabled( ))
{
menu.insertAfter( IWorkbenchActionConstants.MB_ADDITIONS + "-refresh", action ); //$NON-NLS-1$
}
}
/**
* Contextual menu
*/
private void createContextMenu() {
// Create menu manager.
MenuManager menuMgr = new MenuManager();
menuMgr.setRemoveAllWhenShown(true);
menuMgr.addMenuListener(new IMenuListener() {
public void menuAboutToShow(IMenuManager mgr) {
fillContextMenu(mgr);
}
});
// Create menu.
Menu menu = menuMgr.createContextMenu(viewer.getControl());
viewer.getControl().setMenu(menu);
// Register menu for extension.
getSite().registerContextMenu(menuMgr, viewer);
}
@Override
protected void rulerContextMenuAboutToShow(final IMenuManager menu) {
super.rulerContextMenuAboutToShow(menu);
menu.remove("projection");
final IMenuManager foldingMenu = new MenuManager(XtextUIMessages.Editor_FoldingMenu_name, "projection"); //$NON-NLS-1$
menu.appendToGroup(ITextEditorActionConstants.GROUP_RULERS, foldingMenu);
IAction action = getAction("FoldingToggle"); //$NON-NLS-1$
foldingMenu.add(action);
action = getAction("FoldingExpandAll"); //$NON-NLS-1$
foldingMenu.add(action);
action = getAction("FoldingCollapseAll"); //$NON-NLS-1$
foldingMenu.add(action);
action = getAction("FoldingCollapseStrings"); //$NON-NLS-1$
foldingMenu.add(action);
action = getAction("FoldingRestore"); //$NON-NLS-1$
foldingMenu.add(action);
}
/**
* Creates the context menu for the given object. Gets the action from the
* actionRegistry and adds the action to the menu.
*
* @param menu
* the menu
* @param object
* the object
*/
public void createContextMenu( TreeViewer sourceViewer, Object object,
IMenuManager menu )
{
super.createContextMenu( sourceViewer, object, menu );
menu.insertBefore( IWorkbenchActionConstants.MB_ADDITIONS + "-refresh", //$NON-NLS-1$
new ShowPropertyAction( object ) );
menu.insertAfter( IWorkbenchActionConstants.MB_ADDITIONS + "-refresh", new Separator( ) ); //$NON-NLS-1$
IAction action = new RefreshAction( sourceViewer );
if (action.isEnabled( ))
{
menu.insertAfter( IWorkbenchActionConstants.MB_ADDITIONS + "-refresh", action ); //$NON-NLS-1$
}
}
private void createPopupMenu() {
removeHistoryItemAction = new RemoveHistoryItemAction();
removeHistoryActionContributionItem = new ActionContributionItem(
removeHistoryItemAction);
contextMenuManager = new MenuManager();
contextMenuManager.setRemoveAllWhenShown(true);
contextMenuManager.addMenuListener(new IMenuListener() {
public void menuAboutToShow(IMenuManager manager) {
fillContextMenu(manager);
}
});
final Table table = list.getTable();
Menu menu= contextMenuManager.createContextMenu(table);
table.setMenu(menu);
}
@Override
public void fillContextMenu(IMenuManager menu) {
super.fillContextMenu(menu);
menu.insertBefore(ICommonMenuConstants.GROUP_PORT, new Separator("fb"));
menu.insertBefore(ICommonMenuConstants.GROUP_PORT, new Separator("fb.project"));
menu.insertBefore(ICommonMenuConstants.GROUP_PORT, new Separator("fb.filter"));
}
private void hookContextMenu() {
MenuManager menuMgr = new MenuManager("#PopupMenu");
menuMgr.setRemoveAllWhenShown(true);
menuMgr.addMenuListener(new IMenuListener() {
public void menuAboutToShow(IMenuManager manager) {
SinkView.this.fillContextMenu(manager);
}
});
Menu menu = menuMgr.createContextMenu(viewer.getControl());
viewer.getControl().setMenu(menu);
getSite().registerContextMenu(menuMgr, viewer);
}
@Override
public void fillContextMenu(IMenuManager menu) {
super.fillContextMenu(menu);
ISelection selection= getContext().getSelection();
if (selection instanceof IStructuredSelection) {
if (canEnable((IStructuredSelection)selection)) {
MenuManager newMenu= new MenuManager(ActionMessages.NewWizardsActionGroup_new);
menu.appendToGroup(IContextMenuConstants.GROUP_NEW, newMenu);
newMenu.add(getNewWizardMenu());
}
}
}
private void fillContextMenu(IMenuManager manager) {
manager.add(refreshCurrentFileAction);
manager.add(startTailOnCurrentFile);
manager.add(stopTailOnCurrentFile);
manager.add(fileCloseAction);
manager.add(fileClearAction);
manager.add(new Separator());
manager.add(fileEncodingAction);
manager.add(tabRenameAction);
manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
}
public void init(final XViewer xviewer) {
this.xViewer = xviewer;
setupActions();
xViewer.getTree().addListener(SWT.KeyUp, e-> {
if (e.keyCode == 'c' && e.stateMask == (SWT.CONTROL | SWT.SHIFT)) {
performCopyColumnCells();
} else if (e.keyCode == 'b' && e.stateMask == (SWT.CONTROL | SWT.SHIFT)) {
performViewOrCopyCell(Option.Copy);
} else if (e.keyCode == 'v' && e.stateMask == (SWT.CONTROL | SWT.SHIFT)) {
performViewOrCopyCell(Option.View);
} else if (e.keyCode == 'c' && e.stateMask == SWT.CONTROL) {
performCopy();
}
});
xViewer.getTree().addListener(SWT.Dispose, e-> {
if (clipboard != null) {
clipboard.dispose();
}
});
xViewer.getMenuManager().addMenuListener(new IMenuListener() {
@Override
public void menuAboutToShow(IMenuManager manager) {
if (headerMouseClick) {
setupMenuForHeader();
xviewer.updateMenuActionsForHeader();
} else {
setupMenuForTable();
xViewer.updateMenuActionsForTable();
}
}
});
xViewer.getTree().addListener(SWT.MenuDetect, event -> {
Point pt = Display.getCurrent().map(null, xViewer.getTree(), new Point(event.x, event.y));
Rectangle clientArea = xViewer.getTree().getClientArea();
headerMouseClick = clientArea.y <= pt.y && pt.y < (clientArea.y + xViewer.getTree().getHeaderHeight());
});
}
@Override
protected void fillMenuBar(IMenuManager menuBar) {
MenuManager fileMenu = new MenuManager("&File", IWorkbenchActionConstants.M_FILE);
menuBar.add(fileMenu);
fileMenu.add(exitAction);
MenuManager editMenu = new MenuManager("&Edit", IWorkbenchActionConstants.M_EDIT);
menuBar.add(editMenu);
editMenu.add(preferenceAction);
}
protected void addMenuContribution(IConfigurationElement commandElement, IMenuManager manager,
Object[] selection){
ContributionAction action = new ContributionAction(commandElement);
if (action.isValid()) {
action.setSelection(selection);
manager.add(action);
}
}
private Menu createDgMenu(){
MenuManager contextMenuManager = new MenuManager();
contextMenuManager.setRemoveAllWhenShown(true);
contextMenuManager.addMenuListener(new IMenuListener() {
public void menuAboutToShow(IMenuManager manager){
IStructuredSelection selection = viewer.getStructuredSelection();
if (!selection.isEmpty()) {
manager.add(new Action() {
public ImageDescriptor getImageDescriptor(){
return Images.IMG_DELETE.getImageDescriptor();
};
public String getText(){
return Messages.DiagnosenDisplay_RemoveDiagnoses;
};
public void run(){
for (Object object : selection.toList()) {
if (object instanceof IDiagnosis) {
actEncounter.removeDiagnosis((IDiagnosis) object);
CoreModelServiceHolder.get().save(actEncounter);
}
}
setEncounter(actEncounter);
};
});
}
}
});
return contextMenuManager.createContextMenu(table);
}
/**
* This inserts global actions before the "additions-end" separator.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
@Override
protected void addGlobalActions ( IMenuManager menuManager )
{
menuManager.insertAfter ( "additions-end", new Separator ( "ui-actions" ) ); //$NON-NLS-1$ //$NON-NLS-2$
menuManager.insertAfter ( "ui-actions", showPropertiesViewAction ); //$NON-NLS-1$
refreshViewerAction.setEnabled ( refreshViewerAction.isEnabled () );
menuManager.insertAfter ( "ui-actions", refreshViewerAction ); //$NON-NLS-1$
super.addGlobalActions ( menuManager );
}
private void addElementMenuActions(
Collection<ContextualFilterContributor<? extends ContextualFilter>> filters,
IMenuManager mgr) {
for (ContextualFilterContributor<? extends ContextualFilter>
filter : filters) {
Action action = new ElementAction(filter.getLabel(), filter);
mgr.add(action);
}
}
/**
* This inserts global actions before the "additions-end" separator.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
@Override
protected void addGlobalActions(IMenuManager menuManager) {
menuManager.insertAfter("additions-end", new Separator("ui-actions"));
menuManager.insertAfter("ui-actions", showPropertiesViewAction);
refreshViewerAction.setEnabled(refreshViewerAction.isEnabled());
menuManager.insertAfter("ui-actions", refreshViewerAction);
super.addGlobalActions(menuManager);
}
private int addEditorAction(IMenuManager menu, String actionID) {
if (fEditor == null)
return 0;
IAction action= fEditor.getAction(actionID);
if (action == null)
return 0;
if (action instanceof IUpdate)
((IUpdate)action).update();
if (action.isEnabled()) {
menu.add(action);
return 1;
}
return 0;
}
@Override
public void fillContextMenu(IMenuManager menu) {
super.fillContextMenu(menu);
for (int i= 0; i < fActions.length; i++) {
SelectionDispatchAction action= fActions[i];
if (action == fCutAction && !fCutAction.isEnabled())
continue;
menu.appendToGroup(ICommonMenuConstants.GROUP_EDIT, action);
}
}
private void fillContextMenu(IMenuManager manager) {
//manager.add(new RerunSelectedAction(this));
manager.add(new NewInstanceAction(new ReportListViewCustom()));
manager.add(new Separator());
// Other plug-ins can contribute there actions here
manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
}
/**
* Populate the context menu with the necessary actions.
*
* @see org.eclipse.jface.action.IMenuListener#menuAboutToShow(org.eclipse.jface.action.IMenuManager)
*/
public void menuAboutToShow(IMenuManager manager) {
if (fActionGroup == null) // no context menu
return;
JavaPlugin.createStandardGroups(manager);
fActionGroup.fillContextMenu(manager);
}
private void updateDynamicItems( String actionId, IMenuManager menu )
{
IAction action = getAction( actionId );
if ( action != null && action instanceof MenuUpdateAction )
{
( (MenuUpdateAction) action ).updateMenu( (MenuManager) menu );
}
}
@Override
public void fillContextMenu(final IMenuManager menu) {
super.fillContextMenu(menu);
final ISelection selection = getContext().getSelection();
if (selection instanceof IStructuredSelection) {
if (canEnable((IStructuredSelection) selection)) {
final MenuManager newMenu = new MenuManager("Ne&w");
menu.appendToGroup(IContextMenuConstants.GROUP_NEW, newMenu);
newMenu.add(getNewWizardMenu());
}
}
}
@Override
public void contributeToMenu(IMenuManager menu) {
super.contributeToMenu(menu);
IMenuManager navigateMenu = menu.findMenuUsingPath(IWorkbenchActionConstants.M_NAVIGATE);
if (navigateMenu != null) {
navigateMenu.appendToGroup(IWorkbenchActionConstants.SHOW_EXT, fShowOutline);
}
}
private void fillQuickMenu(IMenuManager manager, List<IAction> activeActions) {
if (activeActions.isEmpty()) {
manager.add(NO_ACTION_AVAILABLE);
} else {
for (int i= 0; i < activeActions.size(); i++) {
manager.add(activeActions.get(i));
}
}
}
/**
* @since 2.0
*/
@Override
protected void fillTimeGraphEntryContextMenu(@NonNull IMenuManager menuManager) {
ISelection selection = getSite().getSelectionProvider().getSelection();
if (selection instanceof StructuredSelection) {
StructuredSelection sSel = (StructuredSelection) selection;
if (sSel.getFirstElement() instanceof TimeGraphEntry) {
TimeGraphEntry entry = (TimeGraphEntry) sSel.getFirstElement();
ITmfTreeDataModel entryModel = entry.getEntryModel();
if (entryModel instanceof ThreadEntryModel) {
menuManager.add(new FollowThreadAction(ControlFlowView.this, entry.getName(), ((ThreadEntryModel) entryModel).getThreadId(), getTrace(entry)));
}
}
}
}