/** * Returns whether the types of the resources in the given selection are among * the specified resource types. * * @param selection the selection * @param resourceMask resource mask formed by bitwise OR of resource type * constants (defined on <code>IResource</code>) * @return <code>true</code> if all selected elements are resources of the right * type, and <code>false</code> if at least one element is either a resource * of some other type or a non-resource * @see IResource#getType() */ public static boolean allResourcesAreOfType(IStructuredSelection selection, int resourceMask) { Iterator resources = selection.iterator(); while (resources.hasNext()) { Object next = resources.next(); if (!(next instanceof IResource)) { return false; } if (!resourceIsType((IResource) next, resourceMask)) { return false; } } return true; }
@Override public void fillContextMenu(IMenuManager menu) { IStructuredSelection selection = (IStructuredSelection) getContext().getSelection(); boolean anyResourceSelected = !selection.isEmpty() && ResourceSelectionUtil.allResourcesAreOfType(selection, IResource.PROJECT | IResource.FOLDER | IResource.FILE); if (anyResourceSelected) { moveAction.selectionChanged(selection); menu.appendToGroup(ICommonMenuConstants.GROUP_REORGANIZE, moveAction); renameAction.selectionChanged(selection); menu.insertAfter(moveAction.getId(), renameAction); } }
@Override 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); } }
/** * Returns the selection adapted to IResource. Returns null if any of the * entries are not adaptable. * * @param selection * the selection * @param resourceMask * resource mask formed by bitwise OR of resource type constants * (defined on <code>IResource</code>) * @return IStructuredSelection or null if any of the entries are not * adaptable. * @see IResource#getType() */ public static IStructuredSelection allResources(IStructuredSelection selection, int resourceMask) { Iterator<?> adaptables = selection.iterator(); List<IResource> result = new ArrayList<>(); while (adaptables.hasNext()) { Object next = adaptables.next(); IResource resource = Adapters.adapt(next, IResource.class); if (resource == null) { return null; } if (resourceIsType(resource, resourceMask)) { result.add(resource); } } return new StructuredSelection(result); }