Search in sources :

Example 6 with IRunnableContext

use of org.eclipse.jface.operation.IRunnableContext in project dbeaver by dbeaver.

the class UIUtils method runInUI.

public static void runInUI(final DBRRunnableWithProgress runnable) {
    IWorkbench workbench = PlatformUI.getWorkbench();
    IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
    IRunnableContext context = window != null ? window : DummyRunnableContext.INSTANCE;
    runInUI(context, runnable);
}
Also used : IRunnableContext(org.eclipse.jface.operation.IRunnableContext)

Example 7 with IRunnableContext

use of org.eclipse.jface.operation.IRunnableContext in project dbeaver by dbeaver.

the class CreateLinkHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    ISelection structured = HandlerUtil.getCurrentSelection(event);
    if (structured.isEmpty() || !(structured instanceof IStructuredSelection)) {
        return null;
    }
    Object first = ((IStructuredSelection) structured).getFirstElement();
    IResource resource = GeneralUtils.adapt(first, IResource.class);
    IContainer container = extractContainer(resource);
    if (container == null) {
        IStatus error = GeneralUtils.makeErrorStatus(NLS.bind(UINavigatorMessages.CreateLinkHandler_e_create_link_validation, resource));
        StatusAdapter statusAdapter = new StatusAdapter(error);
        statusAdapter.setProperty(IStatusAdapterConstants.TITLE_PROPERTY, UINavigatorMessages.CreateLinkHandler_e_create_link_title);
        StatusManager.getManager().handle(statusAdapter, StatusManager.SHOW);
        return null;
    }
    Path[] locations = selectTargets(event);
    if (locations == null || locations.length == 0) {
        return null;
    }
    WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {

        @Override
        protected void execute(IProgressMonitor monitor) throws CoreException {
            if (container instanceof IFolder && !container.exists()) {
                // Create parent folder
                ((IFolder) container).create(true, true, monitor);
            }
            IStatus linked = createLink(container, monitor, locations);
            int severity = linked.getSeverity();
            switch(severity) {
                case IStatus.CANCEL:
                    throw new OperationCanceledException(linked.getMessage());
                case IStatus.ERROR:
                    throw new CoreException(linked);
                default:
                    break;
            }
        }
    };
    IRunnableContext context = getRunnableContext(event);
    try {
        context.run(true, true, operation);
    } catch (InvocationTargetException e) {
        IStatus error = GeneralUtils.makeErrorStatus(UINavigatorMessages.CreateLinkHandler_e_create_link_message, e.getTargetException());
        StatusAdapter statusAdapter = new StatusAdapter(error);
        statusAdapter.setProperty(IStatusAdapterConstants.TITLE_PROPERTY, UINavigatorMessages.CreateLinkHandler_e_create_link_title);
        StatusManager.getManager().handle(statusAdapter, StatusManager.LOG | StatusManager.SHOW);
    } catch (InterruptedException e) {
    // skip
    }
    return null;
}
Also used : Path(java.nio.file.Path) IRunnableContext(org.eclipse.jface.operation.IRunnableContext) StatusAdapter(org.eclipse.ui.statushandlers.StatusAdapter) IStatus(org.eclipse.core.runtime.IStatus) WorkspaceModifyOperation(org.eclipse.ui.actions.WorkspaceModifyOperation) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) InvocationTargetException(java.lang.reflect.InvocationTargetException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) ISelection(org.eclipse.jface.viewers.ISelection) IContainer(org.eclipse.core.resources.IContainer) IResource(org.eclipse.core.resources.IResource) IFolder(org.eclipse.core.resources.IFolder)

Example 8 with IRunnableContext

use of org.eclipse.jface.operation.IRunnableContext in project egit by eclipse.

the class GitMergeEditorInput method buildDiffContainer.

private IDiffContainer buildDiffContainer(Repository repository, RevCommit headCommit, RevCommit ancestorCommit, List<String> filterPaths, RevWalk rw, IProgressMonitor monitor) throws IOException, InterruptedException {
    monitor.setTaskName(UIText.GitMergeEditorInput_CalculatingDiffTaskName);
    IDiffContainer result = new DiffNode(Differencer.CONFLICTING);
    try (TreeWalk tw = new TreeWalk(repository)) {
        int dirCacheIndex = tw.addTree(new DirCacheIterator(repository.readDirCache()));
        int fileTreeIndex = tw.addTree(new FileTreeIterator(repository));
        int repositoryTreeIndex = tw.addTree(rw.parseTree(repository.resolve(Constants.HEAD)));
        // skip ignored resources
        NotIgnoredFilter notIgnoredFilter = new NotIgnoredFilter(fileTreeIndex);
        // filter by selected resources
        if (filterPaths.size() > 1) {
            List<TreeFilter> suffixFilters = new ArrayList<>();
            for (String filterPath : filterPaths) suffixFilters.add(PathFilter.create(filterPath));
            TreeFilter otf = OrTreeFilter.create(suffixFilters);
            tw.setFilter(AndTreeFilter.create(otf, notIgnoredFilter));
        } else if (filterPaths.size() > 0) {
            String path = filterPaths.get(0);
            if (path.length() == 0)
                tw.setFilter(notIgnoredFilter);
            else
                tw.setFilter(AndTreeFilter.create(PathFilter.create(path), notIgnoredFilter));
        } else
            tw.setFilter(notIgnoredFilter);
        tw.setRecursive(true);
        while (tw.next()) {
            if (monitor.isCanceled())
                throw new InterruptedException();
            String gitPath = tw.getPathString();
            monitor.setTaskName(gitPath);
            FileTreeIterator fit = tw.getTree(fileTreeIndex, FileTreeIterator.class);
            if (fit == null)
                continue;
            DirCacheIterator dit = tw.getTree(dirCacheIndex, DirCacheIterator.class);
            final DirCacheEntry dirCacheEntry = dit == null ? null : dit.getDirCacheEntry();
            boolean conflicting = dirCacheEntry != null && dirCacheEntry.getStage() > 0;
            AbstractTreeIterator rt = tw.getTree(repositoryTreeIndex, AbstractTreeIterator.class);
            // compare local file against HEAD to see if it was modified
            boolean modified = rt != null && !fit.getEntryObjectId().equals(rt.getEntryObjectId());
            // if this is neither conflicting nor changed, we skip it
            if (!conflicting && !modified)
                continue;
            ITypedElement right;
            if (conflicting) {
                GitFileRevision revision = GitFileRevision.inIndex(repository, gitPath, DirCacheEntry.STAGE_3);
                String encoding = CompareCoreUtils.getResourceEncoding(repository, gitPath);
                right = new FileRevisionTypedElement(revision, encoding);
            } else
                right = CompareUtils.getFileRevisionTypedElement(gitPath, headCommit, repository);
            // can this really happen?
            if (right instanceof EmptyTypedElement)
                continue;
            IFileRevision rev;
            // if the file is not conflicting (as it was auto-merged)
            // we will show the auto-merged (local) version
            Path repositoryPath = new Path(repository.getWorkTree().getAbsolutePath());
            IPath location = repositoryPath.append(fit.getEntryPathString());
            IFile file = ResourceUtil.getFileForLocation(location, false);
            if (!conflicting || useWorkspace) {
                if (file != null)
                    rev = new LocalFileRevision(file);
                else
                    rev = new WorkingTreeFileRevision(location.toFile());
            } else {
                rev = GitFileRevision.inIndex(repository, gitPath, DirCacheEntry.STAGE_2);
            }
            IRunnableContext runnableContext = getContainer();
            if (runnableContext == null)
                runnableContext = PlatformUI.getWorkbench().getProgressService();
            EditableRevision leftEditable;
            if (file != null)
                leftEditable = new ResourceEditableRevision(rev, file, runnableContext);
            else
                leftEditable = new LocationEditableRevision(rev, location, runnableContext);
            // make sure we don't need a round trip later
            try {
                leftEditable.cacheContents(monitor);
            } catch (CoreException e) {
                throw new IOException(e.getMessage());
            }
            int kind = Differencer.NO_CHANGE;
            if (conflicting)
                kind = Differencer.CONFLICTING;
            else if (modified)
                kind = Differencer.PSEUDO_CONFLICT;
            IDiffContainer fileParent = getFileParent(result, repositoryPath, file, location);
            ITypedElement anc;
            if (ancestorCommit != null)
                anc = CompareUtils.getFileRevisionTypedElement(gitPath, ancestorCommit, repository);
            else
                anc = null;
            // instead of null
            if (anc instanceof EmptyTypedElement)
                anc = null;
            // create the node as child
            new DiffNode(fileParent, kind, anc, leftEditable, right);
        }
        return result;
    }
}
Also used : IRunnableContext(org.eclipse.jface.operation.IRunnableContext) NotIgnoredFilter(org.eclipse.jgit.treewalk.filter.NotIgnoredFilter) DirCacheEntry(org.eclipse.jgit.dircache.DirCacheEntry) IFile(org.eclipse.core.resources.IFile) DiffNode(org.eclipse.compare.structuremergeviewer.DiffNode) ArrayList(java.util.ArrayList) LocationEditableRevision(org.eclipse.egit.ui.internal.revision.LocationEditableRevision) IDiffContainer(org.eclipse.compare.structuremergeviewer.IDiffContainer) ResourceEditableRevision(org.eclipse.egit.ui.internal.revision.ResourceEditableRevision) GitFileRevision(org.eclipse.egit.core.internal.storage.GitFileRevision) WorkingTreeFileRevision(org.eclipse.egit.core.internal.storage.WorkingTreeFileRevision) EmptyTypedElement(org.eclipse.egit.ui.internal.revision.GitCompareFileRevisionEditorInput.EmptyTypedElement) DirCacheIterator(org.eclipse.jgit.dircache.DirCacheIterator) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) LocalFileRevision(org.eclipse.egit.ui.internal.revision.LocalFileRevision) IPath(org.eclipse.core.runtime.IPath) IFileRevision(org.eclipse.team.core.history.IFileRevision) ITypedElement(org.eclipse.compare.ITypedElement) IOException(java.io.IOException) AbstractTreeIterator(org.eclipse.jgit.treewalk.AbstractTreeIterator) CoreException(org.eclipse.core.runtime.CoreException) TreeFilter(org.eclipse.jgit.treewalk.filter.TreeFilter) AndTreeFilter(org.eclipse.jgit.treewalk.filter.AndTreeFilter) OrTreeFilter(org.eclipse.jgit.treewalk.filter.OrTreeFilter) FileRevisionTypedElement(org.eclipse.egit.ui.internal.revision.FileRevisionTypedElement) LocationEditableRevision(org.eclipse.egit.ui.internal.revision.LocationEditableRevision) ResourceEditableRevision(org.eclipse.egit.ui.internal.revision.ResourceEditableRevision) EditableRevision(org.eclipse.egit.ui.internal.revision.EditableRevision) FileTreeIterator(org.eclipse.jgit.treewalk.FileTreeIterator)

Example 9 with IRunnableContext

use of org.eclipse.jface.operation.IRunnableContext in project egradle by de-jcup.

the class GradleResourceHyperlink method open.

@Override
public void open() {
    String[] packageNames = fetchImportedPackages(fullText);
    Shell shell = EclipseUtil.getActiveWorkbenchShell();
    if (shell == null) {
        return;
    }
    IJavaSearchScope scope = SearchEngine.createWorkspaceScope();
    JDTDataAccess access = JDTDataAccess.SHARED;
    List<String> typesFound = access.scanForJavaType(resourceName, scope, packageNames);
    SelectionDialog dialog = null;
    if (!typesFound.isEmpty()) {
        if (typesFound.size() == 1) {
            /* exact macht possible */
            String found = typesFound.get(0);
            IType type = access.findType(found, new NullProgressMonitor());
            if (type instanceof IJavaElement) {
                IJavaElement javaElement = type;
                openInEditor(javaElement);
                return;
            } else {
            /* keep on going - use type dialog as fallback... */
            }
        }
        int style = IJavaElementSearchConstants.CONSIDER_ALL_TYPES;
        try {
            String found = typesFound.get(0);
            IRunnableContext runnableContext = EclipseUtil.getActiveWorkbenchWindow();
            dialog = JavaUI.createTypeDialog(shell, runnableContext, scope, style, false, found);
            dialog.setTitle("Potential Java types found:");
        } catch (JavaModelException e) {
            EditorUtil.INSTANCE.logError("Cannot create java type dialog", e);
        }
    } else {
        dialog = createResourceDialog(shell);
    }
    final int resultCode = dialog.open();
    if (resultCode != Window.OK) {
        return;
    }
    Object[] result = dialog.getResult();
    List<IFile> files = new ArrayList<>();
    List<IJavaElement> javaElements = new ArrayList<>();
    if (result != null) {
        for (int i = 0; i < result.length; i++) {
            Object object = result[i];
            if (object instanceof IFile) {
                files.add((IFile) object);
            } else if (object instanceof IJavaElement) {
                IJavaElement javaElement = (IJavaElement) object;
                javaElements.add(javaElement);
            }
        }
    }
    if (files.size() > 0) {
        final IWorkbenchPage page = EclipseUtil.getActivePage();
        if (page == null) {
            return;
        }
        IFile currentFile = null;
        try {
            for (Iterator<IFile> it = files.iterator(); it.hasNext(); ) {
                currentFile = it.next();
                IDE.openEditor(page, currentFile, true);
            }
        } catch (final PartInitException e) {
            EditorUtil.INSTANCE.logError("Cannot open file:" + currentFile, e);
        }
    } else if (javaElements.size() > 0) {
        IJavaElement javaElement = javaElements.get(0);
        openInEditor(javaElement);
    }
}
Also used : IRunnableContext(org.eclipse.jface.operation.IRunnableContext) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaModelException(org.eclipse.jdt.core.JavaModelException) IFile(org.eclipse.core.resources.IFile) ArrayList(java.util.ArrayList) SelectionDialog(org.eclipse.ui.dialogs.SelectionDialog) IType(org.eclipse.jdt.core.IType) Shell(org.eclipse.swt.widgets.Shell) JDTDataAccess(de.jcup.egradle.eclipse.gradleeditor.jdt.JDTDataAccess) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) PartInitException(org.eclipse.ui.PartInitException)

Example 10 with IRunnableContext

use of org.eclipse.jface.operation.IRunnableContext in project eclipse.jdt.ui by eclipse-jdt.

the class CallHierarchyContentProvider method fetchChildren.

protected Object[] fetchChildren(final MethodWrapper methodWrapper) {
    IRunnableContext context = JavaPlugin.getActiveWorkbenchWindow();
    MethodWrapperRunnable runnable = new MethodWrapperRunnable(methodWrapper);
    try {
        context.run(true, true, runnable);
    } catch (InvocationTargetException e) {
        ExceptionHandler.handle(e, CallHierarchyMessages.CallHierarchyContentProvider_searchError_title, CallHierarchyMessages.CallHierarchyContentProvider_searchError_message);
        return EMPTY_ARRAY;
    } catch (InterruptedException e) {
        final CallerMethodWrapper element = (CallerMethodWrapper) methodWrapper;
        if (!isExpandWithConstructors(element)) {
            Display.getDefault().asyncExec(() -> collapseAndRefresh(element));
        }
    }
    return runnable.getCalls();
}
Also used : IRunnableContext(org.eclipse.jface.operation.IRunnableContext) CallerMethodWrapper(org.eclipse.jdt.internal.corext.callhierarchy.CallerMethodWrapper) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

IRunnableContext (org.eclipse.jface.operation.IRunnableContext)48 InvocationTargetException (java.lang.reflect.InvocationTargetException)30 CoreException (org.eclipse.core.runtime.CoreException)20 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)16 ArrayList (java.util.ArrayList)12 IStatus (org.eclipse.core.runtime.IStatus)10 JavaModelException (org.eclipse.jdt.core.JavaModelException)10 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)10 ProgressMonitorDialog (org.eclipse.jface.dialogs.ProgressMonitorDialog)9 Shell (org.eclipse.swt.widgets.Shell)9 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)8 BusyIndicatorRunnableContext (org.eclipse.jdt.internal.ui.util.BusyIndicatorRunnableContext)8 PlatformUI (org.eclipse.ui.PlatformUI)7 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)6 Status (org.eclipse.core.runtime.Status)6 IJavaSearchScope (org.eclipse.jdt.core.search.IJavaSearchScope)6 Composite (org.eclipse.swt.widgets.Composite)6 Assert (org.eclipse.core.runtime.Assert)5 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)5 IType (org.eclipse.jdt.core.IType)5