Search in sources :

Example 76 with IRunnableWithProgress

use of org.eclipse.jface.operation.IRunnableWithProgress in project xtext-eclipse by eclipse.

the class XtextNewProjectWizard method performFinish.

@Override
public boolean performFinish() {
    final IProjectInfo projectInfo = getProjectInfo();
    IRunnableWithProgress op = new IRunnableWithProgress() {

        @Override
        public void run(IProgressMonitor monitor) throws InvocationTargetException {
            try {
                doFinish(projectInfo, monitor);
            } catch (Exception e) {
                throw new InvocationTargetException(e);
            } finally {
                monitor.done();
            }
        }
    };
    try {
        getContainer().run(true, false, op);
    } catch (InterruptedException e) {
        return false;
    } catch (InvocationTargetException e) {
        logger.error(e.getMessage(), e);
        Throwable realException = e.getTargetException();
        MessageDialog.openError(getShell(), Messages.XtextNewProjectWizard_ErrorDialog_Title, realException.getMessage());
        return false;
    }
    return true;
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress)

Example 77 with IRunnableWithProgress

use of org.eclipse.jface.operation.IRunnableWithProgress in project archi by archimatetool.

the class ExportJasperReportsWizard method performFinish.

@Override
public boolean performFinish() {
    fPage1.storePreferences();
    final File mainTemplateFile = fPage2.getMainTemplateFile();
    // Check this exists
    if (mainTemplateFile == null || !mainTemplateFile.exists()) {
        MessageDialog.openError(getShell(), Messages.ExportJasperReportsWizard_1, Messages.ExportJasperReportsWizard_2);
        return false;
    }
    final Locale locale = fPage2.getLocale();
    final File exportFolder = fPage1.getExportFolder();
    final String exportFileName = fPage1.getExportFilename();
    final String reportTitle = fPage1.getReportTitle();
    final int exportOptions = fPage1.getExportOptions();
    // Check valid dir and file name
    try {
        File exportFile = new File(exportFolder, exportFileName);
        exportFile.getCanonicalPath();
        exportFolder.mkdirs();
    } catch (Exception ex) {
        MessageDialog.openError(getShell(), Messages.ExportJasperReportsWizard_3, Messages.ExportJasperReportsWizard_4);
        return false;
    }
    Display.getCurrent().asyncExec(new Runnable() {

        @Override
        public void run() {
            ProgressMonitorDialog dialog = new ProgressMonitorDialog(getShell());
            try {
                dialog.run(false, true, new IRunnableWithProgress() {

                    @Override
                    public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                        try {
                            JasperReportsExporter exporter = new JasperReportsExporter(fModel, exportFolder, exportFileName, mainTemplateFile, reportTitle, locale, exportOptions);
                            exporter.export(monitor);
                        } catch (Exception ex) {
                            ex.printStackTrace();
                            MessageDialog.openError(getShell(), Messages.ExportJasperReportsWizard_5, ex.getMessage());
                        } finally {
                            monitor.done();
                        }
                    }
                });
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    });
    return true;
}
Also used : Locale(java.util.Locale) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) File(java.io.File) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress)

Example 78 with IRunnableWithProgress

use of org.eclipse.jface.operation.IRunnableWithProgress in project xtext-eclipse by eclipse.

the class MultiOrganizeImportsHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IWorkbenchSite activeSite = HandlerUtil.getActiveSite(event);
    MultiOrganizeImportAction javaDelegate = new MultiOrganizeImportAction(activeSite);
    ISelection currentSelection = HandlerUtil.getCurrentSelection(event);
    if (currentSelection instanceof IStructuredSelection) {
        IStructuredSelection structuredSelection = (IStructuredSelection) currentSelection;
        if (shouldRunJavaOrganizeImports()) {
            ICompilationUnit[] compilationUnits = javaDelegate.getCompilationUnits(structuredSelection);
            if (compilationUnits.length > 0) {
                javaDelegate.run(structuredSelection);
            }
        }
        final Multimap<IProject, IFile> files = collectFiles(structuredSelection);
        Shell shell = activeSite.getShell();
        IRunnableWithProgress op = new IRunnableWithProgress() {

            @Override
            public void run(IProgressMonitor mon) throws InvocationTargetException, InterruptedException {
                mon.beginTask(Messages.OrganizeImports, files.size() * 2);
                mon.setTaskName(Messages.OrganizeImports + " - Calculating Import optimisations for " + files.size() + " Xtend files");
                final List<Change> organizeImports = importOrganizerProvider.get().organizeImports(files, mon);
                for (int i = 0; !mon.isCanceled() && i < organizeImports.size(); i++) {
                    Change change = organizeImports.get(i);
                    mon.setTaskName("Performing changes - Xtend " + (i + 1) + " of " + files.size() + "");
                    try {
                        mon.subTask(change.getName());
                        change.perform(SubMonitor.convert(mon, 1));
                    } catch (CoreException e) {
                        throw new InvocationTargetException(e);
                    }
                    if (mon.isCanceled()) {
                        throw new InterruptedException();
                    }
                }
            }
        };
        try {
            new ProgressMonitorDialog(shell).run(true, true, op);
        } catch (InvocationTargetException e) {
            handleException(e);
        } catch (InterruptedException e) {
        // user cancelled, ok
        }
    }
    return event.getApplicationContext();
}
Also used : IWorkbenchSite(org.eclipse.ui.IWorkbenchSite) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IFile(org.eclipse.core.resources.IFile) MultiOrganizeImportAction(org.eclipse.jdt.internal.ui.actions.MultiOrganizeImportAction) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) Change(org.eclipse.ltk.core.refactoring.Change) IProject(org.eclipse.core.resources.IProject) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) Shell(org.eclipse.swt.widgets.Shell) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) ISelection(org.eclipse.jface.viewers.ISelection)

Example 79 with IRunnableWithProgress

use of org.eclipse.jface.operation.IRunnableWithProgress in project xtext-eclipse by eclipse.

the class JvmImplementationOpener method openImplementations.

/**
 * Main parts of the logic is taken from {@link org.eclipse.jdt.internal.ui.javaeditor.JavaElementImplementationHyperlink}
 *
 * @param element - Element to show implementations for
 * @param textviewer - Viewer to show hierarchy view on
 * @param region - Region where to show hierarchy view
 */
public void openImplementations(final IJavaElement element, ITextViewer textviewer, IRegion region) {
    if (element instanceof IMethod) {
        ITypeRoot typeRoot = ((IMethod) element).getTypeRoot();
        CompilationUnit ast = SharedASTProvider.getAST(typeRoot, SharedASTProvider.WAIT_YES, null);
        if (ast == null) {
            openQuickHierarchy(textviewer, element, region);
            return;
        }
        try {
            ISourceRange nameRange = ((IMethod) element).getNameRange();
            ASTNode node = NodeFinder.perform(ast, nameRange);
            ITypeBinding parentTypeBinding = null;
            if (node instanceof SimpleName) {
                ASTNode parent = node.getParent();
                if (parent instanceof MethodInvocation) {
                    Expression expression = ((MethodInvocation) parent).getExpression();
                    if (expression == null) {
                        parentTypeBinding = Bindings.getBindingOfParentType(node);
                    } else {
                        parentTypeBinding = expression.resolveTypeBinding();
                    }
                } else if (parent instanceof SuperMethodInvocation) {
                    // Directly go to the super method definition
                    openEditor(element);
                    return;
                } else if (parent instanceof MethodDeclaration) {
                    parentTypeBinding = Bindings.getBindingOfParentType(node);
                }
            }
            final IType type = parentTypeBinding != null ? (IType) parentTypeBinding.getJavaElement() : null;
            if (type == null) {
                openQuickHierarchy(textviewer, element, region);
                return;
            }
            final String earlyExitIndicator = "EarlyExitIndicator";
            final ArrayList<IJavaElement> links = Lists.newArrayList();
            IRunnableWithProgress runnable = new IRunnableWithProgress() {

                @Override
                public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                    if (monitor == null) {
                        monitor = new NullProgressMonitor();
                    }
                    try {
                        String methodLabel = JavaElementLabels.getElementLabel(element, JavaElementLabels.DEFAULT_QUALIFIED);
                        monitor.beginTask(Messages.format("Searching for implementors of  ''{0}''", methodLabel), 100);
                        SearchRequestor requestor = new SearchRequestor() {

                            @Override
                            public void acceptSearchMatch(SearchMatch match) throws CoreException {
                                if (match.getAccuracy() == SearchMatch.A_ACCURATE) {
                                    IJavaElement element = (IJavaElement) match.getElement();
                                    if (element instanceof IMethod && !JdtFlags.isAbstract((IMethod) element)) {
                                        links.add(element);
                                        if (links.size() > 1) {
                                            throw new OperationCanceledException(earlyExitIndicator);
                                        }
                                    }
                                }
                            }
                        };
                        int limitTo = IJavaSearchConstants.DECLARATIONS | IJavaSearchConstants.IGNORE_DECLARING_TYPE | IJavaSearchConstants.IGNORE_RETURN_TYPE;
                        SearchPattern pattern = SearchPattern.createPattern(element, limitTo);
                        Assert.isNotNull(pattern);
                        SearchParticipant[] participants = new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() };
                        SearchEngine engine = new SearchEngine();
                        engine.search(pattern, participants, SearchEngine.createHierarchyScope(type), requestor, SubMonitor.convert(monitor, 100));
                        if (monitor.isCanceled()) {
                            throw new InterruptedException();
                        }
                    } catch (OperationCanceledException e) {
                        throw new InterruptedException(e.getMessage());
                    } catch (CoreException e) {
                        throw new InvocationTargetException(e);
                    } finally {
                        monitor.done();
                    }
                }
            };
            try {
                PlatformUI.getWorkbench().getProgressService().busyCursorWhile(runnable);
            } catch (InvocationTargetException e) {
                IStatus status = new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.OK, Messages.format("An error occurred while searching for implementations of method ''{0}''. See error log for details.", element.getElementName()), e.getCause());
                JavaPlugin.log(status);
                ErrorDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Open Implementation", "Problems finding implementations.", status);
            } catch (InterruptedException e) {
                if (e.getMessage() != earlyExitIndicator) {
                    return;
                }
            }
            if (links.size() == 1) {
                openEditor(links.get(0));
            } else {
                openQuickHierarchy(textviewer, element, region);
            }
        } catch (JavaModelException e) {
            log.error("An error occurred while searching for implementations", e.getCause());
        } catch (PartInitException e) {
            log.error("An error occurred while searching for implementations", e.getCause());
        }
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IStatus(org.eclipse.core.runtime.IStatus) JavaModelException(org.eclipse.jdt.core.JavaModelException) SimpleName(org.eclipse.jdt.core.dom.SimpleName) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) IType(org.eclipse.jdt.core.IType) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) SearchParticipant(org.eclipse.jdt.core.search.SearchParticipant) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) IMethod(org.eclipse.jdt.core.IMethod) PartInitException(org.eclipse.ui.PartInitException) ISourceRange(org.eclipse.jdt.core.ISourceRange) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IJavaElement(org.eclipse.jdt.core.IJavaElement) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ITypeRoot(org.eclipse.jdt.core.ITypeRoot) InvocationTargetException(java.lang.reflect.InvocationTargetException) SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) Expression(org.eclipse.jdt.core.dom.Expression)

Example 80 with IRunnableWithProgress

use of org.eclipse.jface.operation.IRunnableWithProgress in project xtext-eclipse by eclipse.

the class RenameRefactoringController method startLinkedEditing.

protected void startLinkedEditing() throws InterruptedException {
    if (activeLinkedMode != null)
        startRefactoring(RefactoringType.REFACTORING_DIALOG);
    try {
        final XtextEditor xtextEditor = getXtextEditor();
        if (xtextEditor != null) {
            workbench.getProgressService().run(true, true, new IRunnableWithProgress() {

                @Override
                public void run(final IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                    try {
                        final Provider<LinkedPositionGroup> provider = linkedPositionGroupCalculator.getLinkedPositionGroup(renameElementContext, monitor);
                        Display display = workbench.getDisplay();
                        display.syncExec(new Runnable() {

                            @Override
                            public void run() {
                                RenameLinkedMode newLinkedMode = renameLinkedModeProvider.get();
                                if (newLinkedMode.start(renameElementContext, provider, monitor)) {
                                    activeLinkedMode = newLinkedMode;
                                    undoSupport = undoSupportProvider.get();
                                    undoSupport.startRecording(xtextEditor);
                                }
                            }
                        });
                    } catch (OperationCanceledException e) {
                        throw new InterruptedException();
                    }
                }
            });
            if (activeLinkedMode == null) {
                startRefactoring(RefactoringType.REFACTORING_DIALOG);
            }
        }
    } catch (InterruptedException e) {
        throw e;
    } catch (Exception exc) {
        // unwrap invocation target exceptions
        if (exc.getCause() instanceof RuntimeException)
            throw (RuntimeException) exc.getCause();
        if (exc instanceof RuntimeException)
            throw (RuntimeException) exc;
        throw new WrappedException(exc);
    }
}
Also used : WrappedException(org.eclipse.emf.common.util.WrappedException) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) InvocationTargetException(java.lang.reflect.InvocationTargetException) NoSuchStrategyException(org.eclipse.xtext.ui.refactoring.IRenameStrategy.Provider.NoSuchStrategyException) WrappedException(org.eclipse.emf.common.util.WrappedException) InvocationTargetException(java.lang.reflect.InvocationTargetException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) Provider(com.google.inject.Provider) ISimpleNameProvider(org.eclipse.xtext.ui.refactoring2.rename.ISimpleNameProvider) IGlobalServiceProvider(org.eclipse.xtext.resource.IGlobalServiceProvider) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Display(org.eclipse.swt.widgets.Display)

Aggregations

IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)417 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)397 InvocationTargetException (java.lang.reflect.InvocationTargetException)386 ProgressMonitorDialog (org.eclipse.jface.dialogs.ProgressMonitorDialog)194 CoreException (org.eclipse.core.runtime.CoreException)123 ArrayList (java.util.ArrayList)86 IStatus (org.eclipse.core.runtime.IStatus)67 IOException (java.io.IOException)65 List (java.util.List)54 Status (org.eclipse.core.runtime.Status)53 IFile (org.eclipse.core.resources.IFile)51 File (java.io.File)47 Shell (org.eclipse.swt.widgets.Shell)44 IProject (org.eclipse.core.resources.IProject)40 PartInitException (org.eclipse.ui.PartInitException)32 IPath (org.eclipse.core.runtime.IPath)26 Display (org.eclipse.swt.widgets.Display)26 IResource (org.eclipse.core.resources.IResource)25 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)24 Path (org.eclipse.core.runtime.Path)23