Search in sources :

Example 6 with CModelException

use of org.eclipse.cdt.core.model.CModelException in project linuxtools by eclipse.

the class SystemTapLaunchShortcut method chooseUnit.

/**
 * Creates a dialog that prompts the user to select from the given list of
 * ICElements
 *
 * @param list
 *            : list of ICElements
 * @return
 */
private Object[] chooseUnit(List<ICContainer> list, int numberOfValidFiles) {
    ListTreeContentProvider prov = new ListTreeContentProvider();
    RuledTreeSelectionDialog dialog = new RuledTreeSelectionDialog(getActiveWorkbenchShell(), new WorkbenchLabelProvider(), prov);
    // $NON-NLS-1$
    dialog.setTitle(Messages.getString("SystemTapLaunchShortcut.SelectFiles"));
    // $NON-NLS-1$
    dialog.setMessage(Messages.getString("SystemTapLaunchShortcut.SelectFilesMsg"));
    dialog.setInput(list);
    dialog.setHelpAvailable(false);
    dialog.setStatusLineAboveButtons(false);
    dialog.setEmptyListMessage(Messages.getString(// $NON-NLS-1$
    "SystemTapLaunchShortcut.NoFiles"));
    dialog.setContainerMode(true);
    Object[] topLevel = prov.findElements(list);
    dialog.setInitialSelections(topLevel);
    dialog.setSize(cap(topLevel.length * 10, 30, 55), cap((int) (topLevel.length * 1.5), 3, 13));
    dialog.create();
    Button okButton = dialog.getOkButton();
    Object[] result = null;
    if (testMode) {
        okButton.setSelection(true);
        result = list.toArray();
        ArrayList<Object> output = new ArrayList<>();
        try {
            for (Object obj : result) {
                if (obj instanceof ICContainer) {
                    ICElement[] array = ((ICContainer) obj).getChildren();
                    for (ICElement c : array) {
                        if (!(validElement(c))) {
                            continue;
                        }
                        if (c.getElementName().contains(MAIN_FUNC_NAME) && !output.contains(c)) {
                            output.add(c);
                        }
                    }
                }
            }
            if (output.size() >= numberOfValidFiles) {
                output.clear();
                output.add(USER_SELECTED_ALL);
            }
        } catch (CModelException e) {
            e.printStackTrace();
        }
        result = output.toArray();
    } else {
        if (dialog.open() == Window.CANCEL) {
            return null;
        }
        result = dialog.getResult();
    }
    if (result == null) {
        return null;
    }
    ArrayList<Object> output = new ArrayList<>();
    try {
        for (Object obj : result) {
            if (obj instanceof ICContainer) {
                ICElement[] array = ((ICContainer) obj).getChildren();
                for (ICElement c : array) {
                    if (!(validElement(c))) {
                        continue;
                    }
                    if (!output.contains(c)) {
                        output.add(c);
                    }
                }
            } else if ((obj instanceof ICElement) && validElement((ICElement) obj) && !output.contains(obj)) {
                output.add(obj);
            }
        }
        if (output.size() >= numberOfValidFiles) {
            output.clear();
            output.add(USER_SELECTED_ALL);
        }
    } catch (CModelException e) {
        e.printStackTrace();
    }
    return output.toArray();
}
Also used : WorkbenchLabelProvider(org.eclipse.ui.model.WorkbenchLabelProvider) ICContainer(org.eclipse.cdt.core.model.ICContainer) Button(org.eclipse.swt.widgets.Button) CModelException(org.eclipse.cdt.core.model.CModelException) ArrayList(java.util.ArrayList) ICElement(org.eclipse.cdt.core.model.ICElement)

Example 7 with CModelException

use of org.eclipse.cdt.core.model.CModelException 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)

Aggregations

CModelException (org.eclipse.cdt.core.model.CModelException)7 ICProject (org.eclipse.cdt.core.model.ICProject)4 IResource (org.eclipse.core.resources.IResource)4 CoreException (org.eclipse.core.runtime.CoreException)4 ArrayList (java.util.ArrayList)3 IBinary (org.eclipse.cdt.core.model.IBinary)3 IFile (org.eclipse.core.resources.IFile)3 IPath (org.eclipse.core.runtime.IPath)3 List (java.util.List)2 ICContainer (org.eclipse.cdt.core.model.ICContainer)2 ICElement (org.eclipse.cdt.core.model.ICElement)2 IContainer (org.eclipse.core.resources.IContainer)2 IProject (org.eclipse.core.resources.IProject)2 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 URI (java.net.URI)1 Collections (java.util.Collections)1 CoreModel (org.eclipse.cdt.core.model.CoreModel)1 IOutputEntry (org.eclipse.cdt.core.model.IOutputEntry)1 ISourceRange (org.eclipse.cdt.core.model.ISourceRange)1