Search in sources :

Example 21 with Task

use of com.intellij.openapi.progress.Task in project intellij-community by JetBrains.

the class ExtractCodeStyleAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    DataContext dataContext = e.getDataContext();
    final Project project = CommonDataKeys.PROJECT.getData(dataContext);
    if (project == null)
        return;
    Editor editor = CommonDataKeys.EDITOR.getData(dataContext);
    final VirtualFile[] files = CommonDataKeys.VIRTUAL_FILE_ARRAY.getData(dataContext);
    PsiFile file = null;
    if (editor == null && files != null && files.length == 1 && !files[0].isDirectory()) {
        file = PsiManager.getInstance(project).findFile(files[0]);
    } else if (editor != null) {
        file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
    }
    if (file == null)
        return;
    Language language = file.getLanguage();
    final LangCodeStyleExtractor extractor = LangCodeStyleExtractor.EXTENSION.forLanguage(language);
    if (extractor == null)
        return;
    final CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(project);
    final CodeStyleDeriveProcessor genProcessor = new GenProcessor(extractor);
    final PsiFile finalFile = file;
    final Task.Backgroundable task = new Task.Backgroundable(project, "Code style extractor", true) {

        @Override
        public void run(@NotNull ProgressIndicator indicator) {
            try {
                CodeStyleSettings cloneSettings = settings.clone();
                Map<Value, Object> backup = genProcessor.backupValues(cloneSettings, finalFile.getLanguage());
                ValuesExtractionResult res = genProcessor.runWithProgress(project, cloneSettings, finalFile, indicator);
                reportResult(res, project, cloneSettings, finalFile, backup);
            } catch (ProcessCanceledException e) {
                Utils.logError("Code extraction was canceled");
            } catch (Throwable t) {
                Utils.logError("Unexpected exception:\n" + t);
            }
        }
    };
    ProgressManager.getInstance().run(task);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Task(com.intellij.openapi.progress.Task) ValuesExtractionResult(com.intellij.psi.codeStyle.extractor.values.ValuesExtractionResult) CodeStyleDeriveProcessor(com.intellij.psi.codeStyle.extractor.processor.CodeStyleDeriveProcessor) NotNull(org.jetbrains.annotations.NotNull) Project(com.intellij.openapi.project.Project) Language(com.intellij.lang.Language) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) Value(com.intellij.psi.codeStyle.extractor.values.Value) PsiFile(com.intellij.psi.PsiFile) LangCodeStyleExtractor(com.intellij.psi.codeStyle.extractor.differ.LangCodeStyleExtractor) Editor(com.intellij.openapi.editor.Editor) GenProcessor(com.intellij.psi.codeStyle.extractor.processor.GenProcessor) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 22 with Task

use of com.intellij.openapi.progress.Task in project intellij-community by JetBrains.

the class UsageViewManagerImpl method doSearchAndShow.

private UsageView doSearchAndShow(@NotNull final UsageTarget[] searchFor, @NotNull final Factory<UsageSearcher> searcherFactory, @NotNull final UsageViewPresentation presentation, @NotNull final FindUsagesProcessPresentation processPresentation, @Nullable final UsageViewStateListener listener) {
    final SearchScope searchScopeToWarnOfFallingOutOf = getMaxSearchScopeToWarnOfFallingOutOf(searchFor);
    final AtomicReference<UsageViewImpl> usageViewRef = new AtomicReference<>();
    long start = System.currentTimeMillis();
    Task.Backgroundable task = new Task.Backgroundable(myProject, getProgressTitle(presentation), true, new SearchInBackgroundOption()) {

        @Override
        public void run(@NotNull final ProgressIndicator indicator) {
            new SearchForUsagesRunnable(UsageViewManagerImpl.this, UsageViewManagerImpl.this.myProject, usageViewRef, presentation, searchFor, searcherFactory, processPresentation, searchScopeToWarnOfFallingOutOf, listener).run();
        }

        @NotNull
        @Override
        public NotificationInfo getNotificationInfo() {
            UsageViewImpl usageView = usageViewRef.get();
            int count = usageView == null ? 0 : usageView.getUsagesCount();
            String notification = StringUtil.capitalizeWords(UsageViewBundle.message("usages.n", count), true);
            LOG.debug(notification + " in " + (System.currentTimeMillis() - start) + "ms.");
            return new NotificationInfo("Find Usages", "Find Usages Finished", notification);
        }
    };
    ProgressManager.getInstance().run(task);
    return usageViewRef.get();
}
Also used : Task(com.intellij.openapi.progress.Task) SearchInBackgroundOption(com.intellij.find.SearchInBackgroundOption) AtomicReference(java.util.concurrent.atomic.AtomicReference) NotNull(org.jetbrains.annotations.NotNull) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator)

Example 23 with Task

use of com.intellij.openapi.progress.Task in project intellij-community by JetBrains.

the class UpdateCopyrightAction method analyze.

@Override
protected void analyze(@NotNull final Project project, @NotNull final AnalysisScope scope) {
    PropertiesComponent.getInstance().setValue(UPDATE_EXISTING_COPYRIGHTS, String.valueOf(myUpdateExistingCopyrightsCb.isSelected()), "true");
    final Map<PsiFile, Runnable> preparations = new LinkedHashMap<>();
    Task.Backgroundable task = new Task.Backgroundable(project, "Prepare Copyright...", true) {

        @Override
        public void run(@NotNull final ProgressIndicator indicator) {
            scope.accept(new PsiElementVisitor() {

                @Override
                public void visitFile(final PsiFile file) {
                    if (indicator.isCanceled()) {
                        return;
                    }
                    final Module module = ModuleUtilCore.findModuleForPsiElement(file);
                    final UpdateCopyrightProcessor processor = new UpdateCopyrightProcessor(project, module, file);
                    final Runnable runnable = processor.preprocessFile(file, myUpdateExistingCopyrightsCb.isSelected());
                    if (runnable != EmptyRunnable.getInstance()) {
                        preparations.put(file, runnable);
                    }
                }
            });
        }

        @Override
        public void onSuccess() {
            if (!preparations.isEmpty()) {
                if (!FileModificationService.getInstance().preparePsiElementsForWrite(preparations.keySet()))
                    return;
                final SequentialModalProgressTask progressTask = new SequentialModalProgressTask(project, UpdateCopyrightProcessor.TITLE, true);
                progressTask.setMinIterationTime(200);
                progressTask.setTask(new UpdateCopyrightSequentialTask(preparations, progressTask));
                CommandProcessor.getInstance().executeCommand(project, () -> {
                    CommandProcessor.getInstance().markCurrentCommandAsGlobal(project);
                    ProgressManager.getInstance().run(progressTask);
                }, getTemplatePresentation().getText(), null);
            }
        }
    };
    ProgressManager.getInstance().run(task);
}
Also used : SequentialTask(com.intellij.util.SequentialTask) Task(com.intellij.openapi.progress.Task) SequentialModalProgressTask(com.intellij.util.SequentialModalProgressTask) NotNull(org.jetbrains.annotations.NotNull) SequentialModalProgressTask(com.intellij.util.SequentialModalProgressTask) LinkedHashMap(java.util.LinkedHashMap) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) EmptyRunnable(com.intellij.openapi.util.EmptyRunnable) Module(com.intellij.openapi.module.Module)

Example 24 with Task

use of com.intellij.openapi.progress.Task in project intellij-community by JetBrains.

the class MavenRunner method run.

public void run(final MavenRunnerParameters parameters, final MavenRunnerSettings settings, final Runnable onComplete) {
    FileDocumentManager.getInstance().saveAllDocuments();
    final MavenConsole console = createConsole();
    try {
        final MavenExecutor[] executor = new MavenExecutor[] { createExecutor(parameters, null, settings, console) };
        ProgressManager.getInstance().run(new Task.Backgroundable(myProject, executor[0].getCaption(), true) {

            public void run(@NotNull ProgressIndicator indicator) {
                try {
                    try {
                        if (executor[0].execute(indicator)) {
                            if (onComplete != null)
                                onComplete.run();
                        }
                    } catch (ProcessCanceledException ignore) {
                    }
                    executor[0] = null;
                    updateTargetFolders();
                } finally {
                    console.finish();
                }
            }

            @Nullable
            public NotificationInfo getNotificationInfo() {
                return new NotificationInfo("Maven", "Maven Task Finished", "");
            }

            public boolean shouldStartInBackground() {
                return settings.isRunMavenInBackground();
            }

            public void processSentToBackground() {
                settings.setRunMavenInBackground(true);
            }

            public void processRestoredToForeground() {
                settings.setRunMavenInBackground(false);
            }
        });
    } catch (Exception e) {
        console.printException(e);
        console.finish();
        MavenLog.LOG.warn(e);
    }
}
Also used : Task(com.intellij.openapi.progress.Task) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) MavenConsole(org.jetbrains.idea.maven.project.MavenConsole) Nullable(org.jetbrains.annotations.Nullable) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 25 with Task

use of com.intellij.openapi.progress.Task in project intellij-community by JetBrains.

the class CopiesPanel method changeFormat.

private void changeFormat(@NotNull final WCInfo wcInfo, @NotNull final Collection<WorkingCopyFormat> supportedFormats) {
    ChangeFormatDialog dialog = new ChangeFormatDialog(myProject, new File(wcInfo.getPath()), false, !wcInfo.isIsWcRoot());
    dialog.setSupported(supportedFormats);
    dialog.setData(wcInfo.getFormat());
    if (!dialog.showAndGet()) {
        return;
    }
    final WorkingCopyFormat newFormat = dialog.getUpgradeMode();
    if (!wcInfo.getFormat().equals(newFormat)) {
        ApplicationManager.getApplication().saveAll();
        final Task.Backgroundable task = new SvnFormatWorker(myProject, newFormat, wcInfo) {

            @Override
            public void onSuccess() {
                super.onSuccess();
                myRefreshLabel.doClick();
            }
        };
        ProgressManager.getInstance().run(task);
    }
}
Also used : Task(com.intellij.openapi.progress.Task) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Aggregations

Task (com.intellij.openapi.progress.Task)33 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)28 NotNull (org.jetbrains.annotations.NotNull)20 Project (com.intellij.openapi.project.Project)10 VirtualFile (com.intellij.openapi.vfs.VirtualFile)8 File (java.io.File)6 IOException (java.io.IOException)5 Editor (com.intellij.openapi.editor.Editor)3 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)3 Ref (com.intellij.openapi.util.Ref)3 Semaphore (com.intellij.util.concurrency.Semaphore)3 ArrayList (java.util.ArrayList)3 Nullable (org.jetbrains.annotations.Nullable)3 Disposable (com.intellij.openapi.Disposable)2 ProgressExecutionMode (com.intellij.openapi.externalSystem.service.execution.ProgressExecutionMode)2 BackgroundableProcessIndicator (com.intellij.openapi.progress.impl.BackgroundableProcessIndicator)2 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2 Installer (com.android.repository.api.Installer)1 InstallerFactory (com.android.repository.api.InstallerFactory)1