Search in sources :

Example 56 with IAdaptable

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);
    }
}
Also used : IDebugModelPresentation(org.eclipse.debug.ui.IDebugModelPresentation) DebugPlugin(org.eclipse.debug.core.DebugPlugin) SubMonitor(org.eclipse.core.runtime.SubMonitor) CoreModel(org.eclipse.cdt.core.model.CoreModel) CoreException(org.eclipse.core.runtime.CoreException) DebugUITools(org.eclipse.debug.ui.DebugUITools) ArrayList(java.util.ArrayList) ILaunchConfiguration(org.eclipse.debug.core.ILaunchConfiguration) TwoPaneElementSelector(org.eclipse.ui.dialogs.TwoPaneElementSelector) Messages(org.eclipse.linuxtools.internal.profiling.launch.Messages) ILaunchShortcut(org.eclipse.debug.ui.ILaunchShortcut) IPath(org.eclipse.core.runtime.IPath) ElementListSelectionDialog(org.eclipse.ui.dialogs.ElementListSelectionDialog) ICProject(org.eclipse.cdt.core.model.ICProject) MessageDialog(org.eclipse.jface.dialogs.MessageDialog) IEditorPart(org.eclipse.ui.IEditorPart) IAdaptable(org.eclipse.core.runtime.IAdaptable) CDebugUtils(org.eclipse.cdt.debug.core.CDebugUtils) Shell(org.eclipse.swt.widgets.Shell) ProfileLaunchPlugin(org.eclipse.linuxtools.internal.profiling.launch.ProfileLaunchPlugin) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) IBinary(org.eclipse.cdt.core.model.IBinary) CModelException(org.eclipse.cdt.core.model.CModelException) ILaunchConfigurationWorkingCopy(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy) ILaunchConfigurationType(org.eclipse.debug.core.ILaunchConfigurationType) ILaunchManager(org.eclipse.debug.core.ILaunchManager) InvocationTargetException(java.lang.reflect.InvocationTargetException) CElementLabelProvider(org.eclipse.cdt.ui.CElementLabelProvider) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ILabelProvider(org.eclipse.jface.viewers.ILabelProvider) List(java.util.List) Window(org.eclipse.jface.window.Window) ICDTLaunchConfigurationConstants(org.eclipse.cdt.debug.core.ICDTLaunchConfigurationConstants) IResource(org.eclipse.core.resources.IResource) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) ISelection(org.eclipse.jface.viewers.ISelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) Collections(java.util.Collections) IAdaptable(org.eclipse.core.runtime.IAdaptable) ICProject(org.eclipse.cdt.core.model.ICProject) CModelException(org.eclipse.cdt.core.model.CModelException) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) ArrayList(java.util.ArrayList) IBinary(org.eclipse.cdt.core.model.IBinary) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IResource(org.eclipse.core.resources.IResource)

Example 57 with IAdaptable

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;
}
Also used : IWorkbenchSite(org.eclipse.ui.IWorkbenchSite) IAdaptable(org.eclipse.core.runtime.IAdaptable) ISelectionProvider(org.eclipse.jface.viewers.ISelectionProvider) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) IFileEditorInput(org.eclipse.ui.IFileEditorInput) ISelection(org.eclipse.jface.viewers.ISelection) EditorPart(org.eclipse.ui.part.EditorPart) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IEditorInput(org.eclipse.ui.IEditorInput) IResource(org.eclipse.core.resources.IResource)

Example 58 with IAdaptable

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);
        }
    }
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) ResourceMapping(org.eclipse.core.resources.mapping.ResourceMapping) MenuManager(org.eclipse.jface.action.MenuManager) IMenuManager(org.eclipse.jface.action.IMenuManager) GroupMarker(org.eclipse.jface.action.GroupMarker) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IMenuManager(org.eclipse.jface.action.IMenuManager) IResource(org.eclipse.core.resources.IResource)

Aggregations

IAdaptable (org.eclipse.core.runtime.IAdaptable)58 IResource (org.eclipse.core.resources.IResource)21 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)21 ArrayList (java.util.ArrayList)15 IFile (org.eclipse.core.resources.IFile)13 IProject (org.eclipse.core.resources.IProject)12 ISelection (org.eclipse.jface.viewers.ISelection)8 CoreException (org.eclipse.core.runtime.CoreException)7 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)6 IEditorInput (org.eclipse.ui.IEditorInput)6 List (java.util.List)5 ExecutionException (org.eclipse.core.commands.ExecutionException)5 IWorkbenchPart (org.eclipse.ui.IWorkbenchPart)5 Iterator (java.util.Iterator)4 IPath (org.eclipse.core.runtime.IPath)4 IStatus (org.eclipse.core.runtime.IStatus)4 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)4 IEditorPart (org.eclipse.ui.IEditorPart)4 IOException (java.io.IOException)3 ISelectionProvider (org.eclipse.jface.viewers.ISelectionProvider)3