Search in sources :

Example 26 with Task

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

the class ConvertParameterToMapEntryIntention method collectOwnerOccurrences.

private static boolean collectOwnerOccurrences(final Project project, final GrParametersOwner owner, final Collection<PsiElement> occurrences) {
    final PsiElement namedElem = getReferencedElement(owner);
    if (namedElem == null)
        return true;
    final Ref<Boolean> result = new Ref<>(true);
    final Task task = new Task.Modal(project, GroovyIntentionsBundle.message("find.method.ro.closure.usages.0", owner instanceof GrClosableBlock ? CLOSURE_CAPTION : METHOD_CAPTION), true) {

        @Override
        public void run(@NotNull final ProgressIndicator indicator) {
            final Collection<PsiReference> references = Collections.synchronizedSet(new HashSet<PsiReference>());
            final Processor<PsiReference> consumer = psiReference -> {
                references.add(psiReference);
                return true;
            };
            ReferencesSearch.search(namedElem).forEach(consumer);
            boolean isProperty = ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {

                @Override
                public Boolean compute() {
                    return namedElem instanceof GrField && ((GrField) namedElem).isProperty();
                }
            });
            if (isProperty) {
                final GrAccessorMethod[] getters = ApplicationManager.getApplication().runReadAction(new Computable<GrAccessorMethod[]>() {

                    @Override
                    public GrAccessorMethod[] compute() {
                        return ((GrField) namedElem).getGetters();
                    }
                });
                for (GrAccessorMethod getter : getters) {
                    MethodReferencesSearch.search(getter).forEach(consumer);
                }
            }
            for (final PsiReference reference : references) {
                ApplicationManager.getApplication().runReadAction(() -> {
                    final PsiElement element = reference.getElement();
                    if (element != null) {
                        occurrences.add(element);
                    }
                });
            }
        }

        @Override
        public void onCancel() {
            result.set(false);
        }

        @Override
        public void onThrowable(@NotNull Throwable error) {
            super.onThrowable(error);
            result.set(false);
        }

        @Override
        public void onSuccess() {
            result.set(true);
        }
    };
    ProgressManager.getInstance().run(task);
    return result.get().booleanValue();
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) HashSet(com.intellij.util.containers.HashSet) GrMethodImpl(org.jetbrains.plugins.groovy.lang.psi.impl.statements.typedef.members.GrMethodImpl) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) Task(com.intellij.openapi.progress.Task) MethodReferencesSearch(com.intellij.psi.search.searches.MethodReferencesSearch) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) Logger(com.intellij.openapi.diagnostic.Logger) MultiMap(com.intellij.util.containers.MultiMap) ProgressManager(com.intellij.openapi.progress.ProgressManager) ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrClosureSignatureUtil(org.jetbrains.plugins.groovy.lang.psi.impl.signatures.GrClosureSignatureUtil) Collection(java.util.Collection) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) Nullable(org.jetbrains.annotations.Nullable) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog) Processor(com.intellij.util.Processor) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) ApplicationManager(com.intellij.openapi.application.ApplicationManager) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) Ref(com.intellij.openapi.util.Ref) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) GrParametersOwner(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrParametersOwner) NonNls(org.jetbrains.annotations.NonNls) Computable(com.intellij.openapi.util.Computable) ContainerUtil(com.intellij.util.containers.ContainerUtil) ArrayList(java.util.ArrayList) GroovyIntentionsBundle(org.jetbrains.plugins.groovy.intentions.GroovyIntentionsBundle) Intention(org.jetbrains.plugins.groovy.intentions.base.Intention) GrMapType(org.jetbrains.plugins.groovy.lang.psi.impl.GrMapType) Project(com.intellij.openapi.project.Project) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrNamedElement(org.jetbrains.plugins.groovy.lang.psi.GrNamedElement) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) PsiElementPredicate(org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate) GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) StringUtil(com.intellij.openapi.util.text.StringUtil) Editor(com.intellij.openapi.editor.Editor) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyPropertyUtils(org.jetbrains.plugins.groovy.lang.psi.util.GroovyPropertyUtils) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) CommandProcessor(com.intellij.openapi.command.CommandProcessor) CommonRefactoringUtil(com.intellij.refactoring.util.CommonRefactoringUtil) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GroovyValidationUtil(org.jetbrains.plugins.groovy.refactoring.GroovyValidationUtil) Collections(java.util.Collections) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) Task(com.intellij.openapi.progress.Task) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) NotNull(org.jetbrains.annotations.NotNull) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) Ref(com.intellij.openapi.util.Ref) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator)

Example 27 with Task

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

the class PythonSdkUpdater method update.

/**
   * Updates the paths of an SDK and regenerates its skeletons as a background task.
   *
   * May be invoked from any thread. May freeze the current thread while evaluating sys.path.
   *
   * For a local SDK it commits all the SDK paths and runs a background task for updating skeletons. For a remote SDK it runs a background
   * task for updating skeletons that saves path mappings in the additional SDK data and then commits all the SDK paths.
   *
   * The commit of the changes in the SDK happens in the AWT thread while the current thread is waiting the result.
   *
   * @param sdkModificator if null then it tries to get an SDK modifier from the SDK table, falling back to the modifier of the SDK
   *                       passed as an argument accessed from the AWT thread
   * @return false if there was an immediate problem updating the SDK. Other problems are reported as log entries and balloons.
   */
public static boolean update(@NotNull Sdk sdk, @Nullable SdkModificator sdkModificator, @Nullable final Project project, @Nullable final Component ownerComponent) {
    final String key = PythonSdkType.getSdkKey(sdk);
    synchronized (ourLock) {
        ourScheduledToRefresh.add(key);
    }
    if (!updateLocalSdkPaths(sdk, sdkModificator, project)) {
        return false;
    }
    final Application application = ApplicationManager.getApplication();
    if (application.isUnitTestMode()) {
        // updates skeleton, see PySkeletonRefresher
        return true;
    }
    @SuppressWarnings("ThrowableInstanceNeverThrown") final Throwable methodCallStacktrace = new Throwable();
    application.invokeLater(() -> {
        synchronized (ourLock) {
            if (!ourScheduledToRefresh.contains(key)) {
                return;
            }
            ourScheduledToRefresh.remove(key);
        }
        if (project != null && project.isDisposed()) {
            return;
        }
        ProgressManager.getInstance().run(new Task.Backgroundable(project, PyBundle.message("sdk.gen.updating.interpreter"), false) {

            @Override
            public void run(@NotNull ProgressIndicator indicator) {
                final Project project1 = getProject();
                final Sdk sdkInsideTask = PythonSdkType.findSdkByKey(key);
                if (sdkInsideTask != null) {
                    ourUnderRefresh.put(key);
                    try {
                        final String skeletonsPath = getBinarySkeletonsPath(sdk.getHomePath());
                        try {
                            if (PythonSdkType.isRemote(sdkInsideTask) && project1 == null && ownerComponent == null) {
                                LOG.error("For refreshing skeletons of remote SDK, either project or owner component must be specified");
                            }
                            final String sdkPresentableName = getSdkPresentableName(sdk);
                            LOG.info("Performing background update of skeletons for SDK " + sdkPresentableName);
                            indicator.setText("Updating skeletons...");
                            PySkeletonRefresher.refreshSkeletonsOfSdk(project1, ownerComponent, skeletonsPath, sdkInsideTask);
                            updateRemoteSdkPaths(sdkInsideTask, getProject());
                            indicator.setIndeterminate(true);
                            indicator.setText("Scanning installed packages...");
                            indicator.setText2("");
                            LOG.info("Performing background scan of packages for SDK " + sdkPresentableName);
                            try {
                                PyPackageManager.getInstance(sdkInsideTask).refreshAndGetPackages(true);
                            } catch (ExecutionException e) {
                                if (LOG.isDebugEnabled()) {
                                    e.initCause(methodCallStacktrace);
                                    LOG.debug(e);
                                } else {
                                    LOG.warn(e.getMessage());
                                }
                            }
                        } catch (InvalidSdkException e) {
                            if (PythonSdkType.isVagrant(sdkInsideTask) || new CredentialsTypeExChecker() {

                                @Override
                                protected boolean checkLanguageContribution(PyCredentialsContribution languageContribution) {
                                    return languageContribution.shouldNotifySdkSkeletonFail();
                                }
                            }.check(sdkInsideTask)) {
                                PythonSdkType.notifyRemoteSdkSkeletonsFail(e, () -> {
                                    final Sdk sdkInsideNotify = PythonSdkType.findSdkByKey(key);
                                    if (sdkInsideNotify != null) {
                                        update(sdkInsideNotify, null, project1, ownerComponent);
                                    }
                                });
                            } else if (!PythonSdkType.isInvalid(sdkInsideTask)) {
                                LOG.error(e);
                            }
                        }
                    } finally {
                        try {
                            ourUnderRefresh.remove(key);
                        } catch (IllegalStateException e) {
                            LOG.error(e);
                        }
                    }
                }
            }
        });
    }, ModalityState.NON_MODAL);
    return true;
}
Also used : Task(com.intellij.openapi.progress.Task) PyCredentialsContribution(com.jetbrains.python.remote.PyCredentialsContribution) Project(com.intellij.openapi.project.Project) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) Sdk(com.intellij.openapi.projectRoots.Sdk) ExecutionException(com.intellij.execution.ExecutionException)

Example 28 with Task

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

the class CCPushTask method actionPerformed.

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
    final IdeView view = e.getData(LangDataKeys.IDE_VIEW);
    final Project project = e.getData(CommonDataKeys.PROJECT);
    if (view == null || project == null) {
        return;
    }
    final Course course = StudyTaskManager.getInstance(project).getCourse();
    if (course == null) {
        return;
    }
    PsiDirectory taskDir = DirectoryChooserUtil.getOrChooseDirectory(view);
    if (taskDir == null || !taskDir.getName().contains(EduNames.TASK)) {
        return;
    }
    final PsiDirectory lessonDir = taskDir.getParentDirectory();
    if (lessonDir == null)
        return;
    final Lesson lesson = course.getLesson(lessonDir.getName());
    if (lesson == null)
        return;
    final com.jetbrains.edu.learning.courseFormat.Task task = lesson.getTask(taskDir.getName());
    if (task == null)
        return;
    ProgressManager.getInstance().run(new Task.Modal(project, "Uploading Task", true) {

        @Override
        public void run(@NotNull ProgressIndicator indicator) {
            indicator.setText("Uploading task to " + EduStepicNames.STEPIC_URL);
            if (task.getStepId() <= 0) {
                CCStepicConnector.postTask(project, task, lesson.getId());
            } else {
                CCStepicConnector.updateTask(project, task);
            }
        }
    });
}
Also used : Task(com.intellij.openapi.progress.Task) IdeView(com.intellij.ide.IdeView) Lesson(com.jetbrains.edu.learning.courseFormat.Lesson) Project(com.intellij.openapi.project.Project) PsiDirectory(com.intellij.psi.PsiDirectory) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) Course(com.jetbrains.edu.learning.courseFormat.Course)

Example 29 with Task

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

the class FirstRunWizardHost method runSensitiveOperation.

@Override
public void runSensitiveOperation(@NotNull ProgressIndicator progressIndicator, boolean cancellable, @NotNull final Runnable operation) {
    final Application application = ApplicationManager.getApplication();
    application.assertIsDispatchThread();
    if (!myCurrentProgressIndicator.compareAndSet(null, progressIndicator)) {
        throw new IllegalStateException("Submitting an operation while another is in progress.");
    }
    final JRootPane rootPane = myFrame.getRootPane();
    final JButton defaultButton = rootPane.getDefaultButton();
    rootPane.setDefaultButton(null);
    updateButtons(false, false, true, false);
    Task.Backgroundable task = new LongRunningOperationWrapper(operation, cancellable, defaultButton);
    ProgressManager.getInstance().runProcessWithProgressAsynchronously(task, progressIndicator);
}
Also used : Task(com.intellij.openapi.progress.Task) Application(com.intellij.openapi.application.Application)

Example 30 with Task

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

the class NewGradleSync method sync.

@Override
public void sync(@NotNull Project project, @NotNull GradleSyncInvoker.Request request, @Nullable GradleSyncListener listener) {
    String title = String.format("Syncing project '%1$s' with Gradle", project.getName());
    Task task;
    ProgressExecutionMode executionMode = request.getProgressExecutionMode();
    switch(executionMode) {
        case MODAL_SYNC:
            task = new Task.Modal(project, title, true) {

                @Override
                public void run(@NotNull ProgressIndicator indicator) {
                    sync(project, indicator, listener);
                }
            };
            break;
        case IN_BACKGROUND_ASYNC:
            task = new Task.Backgroundable(project, title, true) {

                @Override
                public void run(@NotNull ProgressIndicator indicator) {
                    sync(project, indicator, listener);
                }
            };
            break;
        default:
            throw new IllegalArgumentException(executionMode + " is not a supported execution mode");
    }
    invokeAndWaitIfNeeded((Runnable) task::queue);
}
Also used : Task(com.intellij.openapi.progress.Task) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) ProgressExecutionMode(com.intellij.openapi.externalSystem.service.execution.ProgressExecutionMode)

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