use of org.eclipse.core.runtime.IAdaptable in project linuxtools by eclipse.
the class ProfileLaunchShortcut method searchAndLaunch.
/**
* Search and launch binary.
*
* @param elements Binaries to search.
* @param mode Launch mode.
*/
private void searchAndLaunch(final Object[] elements, String mode) {
if (elements != null && elements.length > 0) {
IBinary bin = null;
if (elements.length == 1 && elements[0] instanceof IBinary) {
bin = (IBinary) elements[0];
} else {
final List<IBinary> results = new ArrayList<>();
ProgressMonitorDialog dialog = new ProgressMonitorDialog(getActiveWorkbenchShell());
IRunnableWithProgress runnable = pm -> {
int nElements = elements.length;
pm.beginTask(Messages.ProfileLaunchShortcut_Looking_for_executables, nElements);
try {
IProgressMonitor sub = SubMonitor.convert(pm, 1);
for (int i = 0; i < nElements; i++) {
if (elements[i] instanceof IAdaptable) {
IResource r = ((IAdaptable) elements[i]).getAdapter(IResource.class);
if (r != null) {
ICProject cproject = CoreModel.getDefault().create(r.getProject());
if (cproject != null) {
try {
IBinary[] bins = cproject.getBinaryContainer().getBinaries();
for (IBinary bin1 : bins) {
if (bin1.isExecutable()) {
results.add(bin1);
}
}
} catch (CModelException e) {
// TODO should this be simply ignored ?
}
}
}
}
if (pm.isCanceled()) {
throw new InterruptedException();
}
sub.done();
}
} finally {
pm.done();
}
};
try {
dialog.run(true, true, runnable);
} catch (InterruptedException e) {
return;
} catch (InvocationTargetException e) {
handleFail(e.getMessage());
return;
}
int count = results.size();
if (count == 0) {
handleFail(Messages.ProfileLaunchShortcut_Binary_not_found);
} else if (count > 1) {
bin = chooseBinary(results, mode);
} else {
bin = results.get(0);
}
}
if (bin != null) {
launch(bin, mode);
}
} else {
handleFail(Messages.ProfileLaunchShortcut_no_project_selected);
}
}
use of org.eclipse.core.runtime.IAdaptable in project linuxtools by eclipse.
the class RPMHandlerUtils method getResource.
/**
* Extract the IResource that was selected when the event was fired.
* @param event The fired execution event.
* @return The resource that was selected.
*/
public static IResource getResource(ExecutionEvent event) {
IWorkbenchPart part = HandlerUtil.getActivePart(event);
if (part == null) {
return null;
}
if (part instanceof EditorPart) {
IEditorInput input = ((EditorPart) part).getEditorInput();
if (input instanceof IFileEditorInput) {
return ((IFileEditorInput) input).getFile();
}
return null;
}
IWorkbenchSite site = part.getSite();
if (site == null) {
return null;
}
ISelectionProvider provider = site.getSelectionProvider();
if (provider == null) {
return null;
}
ISelection selection = provider.getSelection();
if (selection instanceof IStructuredSelection) {
Object element = ((IStructuredSelection) selection).getFirstElement();
if (element instanceof IResource) {
return (IResource) element;
} else if (element instanceof IAdaptable) {
IAdaptable adaptable = (IAdaptable) element;
return adaptable.getAdapter(IResource.class);
} else {
return null;
}
}
return null;
}
use of org.eclipse.core.runtime.IAdaptable in project yamcs-studio by yamcs.
the class OpenActionProvider method addOpenWithMenu.
private void addOpenWithMenu(IMenuManager aMenu) {
IStructuredSelection ss = (IStructuredSelection) getContext().getSelection();
if (ss == null || ss.size() != 1) {
return;
}
Object o = ss.getFirstElement();
// first try IResource
IAdaptable openable = Adapters.adapt(o, IResource.class);
// otherwise try ResourceMapping
if (openable == null) {
openable = Adapters.adapt(o, ResourceMapping.class);
} else if (((IResource) openable).getType() != IResource.FILE) {
openable = null;
}
if (openable != null) {
IMenuManager submenu = new MenuManager("Open With", ICommonMenuConstants.GROUP_OPEN_WITH);
submenu.add(new GroupMarker(ICommonMenuConstants.GROUP_TOP));
submenu.add(new OpenWithMenu(viewSite.getPage(), openable));
submenu.add(new GroupMarker(ICommonMenuConstants.GROUP_ADDITIONS));
if (submenu.getItems().length > 2 && submenu.isEnabled()) {
aMenu.appendToGroup(ICommonMenuConstants.GROUP_OPEN_WITH, submenu);
}
}
}
Aggregations