Search in sources :

Example 1 with IRunnableContext

use of org.eclipse.jface.operation.IRunnableContext in project tdi-studio-se by Talend.

the class BusinessDiagramEditorUtil method createNewDiagramFile.

/**
     * <p>
     * This method should be called within a workspace modify operation since it creates resources.
     * </p>
     * 
     * @generated
     * @return the created file resource, or <code>null</code> if the file was not created
     */
public static final IFile createNewDiagramFile(DiagramFileCreator diagramFileCreator, IPath containerFullPath, String fileName, InputStream initialContents, String kind, Shell shell, IProgressMonitor progressMonitor) {
    TransactionalEditingDomain editingDomain = GMFEditingDomainFactory.INSTANCE.createEditingDomain();
    ResourceSet resourceSet = editingDomain.getResourceSet();
    //$NON-NLS-1$
    progressMonitor.beginTask("Creating diagram and model files", 4);
    final IProgressMonitor subProgressMonitor = new SubProgressMonitor(progressMonitor, 1);
    final IFile diagramFile = diagramFileCreator.createNewFile(containerFullPath, fileName, initialContents, shell, new IRunnableContext() {

        public void run(boolean fork, boolean cancelable, IRunnableWithProgress runnable) throws InvocationTargetException, InterruptedException {
            runnable.run(subProgressMonitor);
        }
    });
    final Resource diagramResource = resourceSet.createResource(URI.createPlatformResourceURI(diagramFile.getFullPath().toString()));
    List affectedFiles = new ArrayList();
    affectedFiles.add(diagramFile);
    final String kindParam = kind;
    AbstractTransactionalCommand command = new AbstractTransactionalCommand(editingDomain, //$NON-NLS-1$
    "Creating diagram and model", //$NON-NLS-1$
    affectedFiles) {

        protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
            BusinessProcess model = createInitialModel();
            diagramResource.getContents().add(model);
            Diagram diagram = ViewService.createDiagram(model, kindParam, BusinessDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
            if (diagram != null) {
                diagramResource.getContents().add(diagram);
                diagram.setName(diagramFile.getName());
                diagram.setElement(model);
            }
            try {
                diagramResource.save(Collections.EMPTY_MAP);
            } catch (IOException e) {
                BusinessDiagramEditorPlugin.getInstance().logError("Unable to store model and diagram resources", //$NON-NLS-1$
                e);
            }
            return CommandResult.newOKCommandResult();
        }
    };
    try {
        OperationHistoryFactory.getOperationHistory().execute(command, new SubProgressMonitor(progressMonitor, 1), null);
    } catch (ExecutionException e) {
        //$NON-NLS-1$
        BusinessDiagramEditorPlugin.getInstance().logError("Unable to create model and diagram", e);
    }
    try {
        //$NON-NLS-1$
        diagramFile.setCharset("UTF-8", new SubProgressMonitor(progressMonitor, 1));
    } catch (CoreException e) {
        //$NON-NLS-1$
        BusinessDiagramEditorPlugin.getInstance().logError("Unable to set charset for diagram file", e);
    }
    return diagramFile;
}
Also used : IRunnableContext(org.eclipse.jface.operation.IRunnableContext) IAdaptable(org.eclipse.core.runtime.IAdaptable) IFile(org.eclipse.core.resources.IFile) Resource(org.eclipse.emf.ecore.resource.Resource) ArrayList(java.util.ArrayList) AbstractTransactionalCommand(org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) IOException(java.io.IOException) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) Diagram(org.eclipse.gmf.runtime.notation.Diagram) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) TransactionalEditingDomain(org.eclipse.emf.transaction.TransactionalEditingDomain) CoreException(org.eclipse.core.runtime.CoreException) ArrayList(java.util.ArrayList) List(java.util.List) ExecutionException(org.eclipse.core.commands.ExecutionException) BusinessProcess(org.talend.designer.business.model.business.BusinessProcess)

Example 2 with IRunnableContext

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

the class DBeaverUI method runInProgressDialog.

/**
     * Runs task in Eclipse progress dialog.
     * NOTE: this call can't be canceled if it will block in IO
     */
public static void runInProgressDialog(final DBRRunnableWithProgress runnable) throws InvocationTargetException {
    try {
        IRunnableContext runnableContext;
        IWorkbench workbench = PlatformUI.getWorkbench();
        IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
        if (workbenchWindow != null) {
            runnableContext = new ProgressMonitorDialog(workbench.getActiveWorkbenchWindow().getShell());
        } else {
            runnableContext = workbench.getProgressService();
        }
        runnableContext.run(true, true, new IRunnableWithProgress() {

            @Override
            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                runnable.run(RuntimeUtils.makeMonitor(monitor));
            }
        });
    } catch (InterruptedException e) {
    // do nothing
    }
}
Also used : IRunnableContext(org.eclipse.jface.operation.IRunnableContext) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress)

Example 3 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 {
    IStructuredSelection structured = HandlerUtil.getCurrentStructuredSelection(event);
    if (structured.isEmpty()) {
        return null;
    }
    Object first = structured.getFirstElement();
    IResource resource = Adapters.adapt(first, IResource.class);
    IContainer container = extractContainer(resource);
    if (container == null) {
        String message = NLS.bind(IdeMessages.CreateLinkHandler_e_create_link_validation, resource);
        IStatus error = DBeaverIDEUI.createError(message);
        StatusAdapter statusAdapter = new StatusAdapter(error);
        statusAdapter.setProperty(IStatusAdapterConstants.TITLE_PROPERTY, IdeMessages.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, InvocationTargetException, InterruptedException {
            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 = DBeaverIDEUI.createError(IdeMessages.CreateLinkHandler_e_create_link_message, e.getTargetException());
        StatusAdapter statusAdapter = new StatusAdapter(error);
        statusAdapter.setProperty(IStatusAdapterConstants.TITLE_PROPERTY, IdeMessages.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) IContainer(org.eclipse.core.resources.IContainer) IResource(org.eclipse.core.resources.IResource)

Example 4 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 5 with IRunnableContext

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

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)

Aggregations

IRunnableContext (org.eclipse.jface.operation.IRunnableContext)11 InvocationTargetException (java.lang.reflect.InvocationTargetException)6 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)6 CoreException (org.eclipse.core.runtime.CoreException)5 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)4 ProgressMonitorDialog (org.eclipse.jface.dialogs.ProgressMonitorDialog)3 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 IContainer (org.eclipse.core.resources.IContainer)2 IFile (org.eclipse.core.resources.IFile)2 IResource (org.eclipse.core.resources.IResource)2 IStatus (org.eclipse.core.runtime.IStatus)2 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)2 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2 Comparator (java.util.Comparator)1 List (java.util.List)1 TreeSet (java.util.TreeSet)1 ITypedElement (org.eclipse.compare.ITypedElement)1 DiffNode (org.eclipse.compare.structuremergeviewer.DiffNode)1