From b897b51708895104f3dc5f33f9d749e4ce3b9ceb Mon Sep 17 00:00:00 2001 From: irengrig Date: Wed, 14 Jul 2010 15:58:35 +0400 Subject: [PATCH] continuation (cherry picked from commit 36dd886f35c6d39ae4a6134e5ecc23bd369950fb) --- .../changes/BackgroundFromStartOption.java | 34 ++++++ .../util/continuation/Continuation.java | 101 ++++++++++++++++++ .../util/continuation/TaskDescriptor.java | 42 ++++++++ .../com/intellij/util/continuation/Where.java | 21 ++++ 4 files changed, 198 insertions(+) create mode 100644 platform/vcs-api/src/com/intellij/openapi/vcs/changes/BackgroundFromStartOption.java create mode 100644 platform/vcs-api/src/com/intellij/util/continuation/Continuation.java create mode 100644 platform/vcs-api/src/com/intellij/util/continuation/TaskDescriptor.java create mode 100644 platform/vcs-api/src/com/intellij/util/continuation/Where.java diff --git a/platform/vcs-api/src/com/intellij/openapi/vcs/changes/BackgroundFromStartOption.java b/platform/vcs-api/src/com/intellij/openapi/vcs/changes/BackgroundFromStartOption.java new file mode 100644 index 000000000000..28e131174a61 --- /dev/null +++ b/platform/vcs-api/src/com/intellij/openapi/vcs/changes/BackgroundFromStartOption.java @@ -0,0 +1,34 @@ +/* + * Copyright 2000-2009 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.openapi.vcs.changes; + +import com.intellij.openapi.progress.PerformInBackgroundOption; + +public class BackgroundFromStartOption implements PerformInBackgroundOption { + private final static BackgroundFromStartOption ourInstance = new BackgroundFromStartOption(); + + public static PerformInBackgroundOption getInstance() { + return ourInstance; + } + + public boolean shouldStartInBackground() { + return true; + } + + public void processSentToBackground() { + } + +} diff --git a/platform/vcs-api/src/com/intellij/util/continuation/Continuation.java b/platform/vcs-api/src/com/intellij/util/continuation/Continuation.java new file mode 100644 index 000000000000..8599364474c6 --- /dev/null +++ b/platform/vcs-api/src/com/intellij/util/continuation/Continuation.java @@ -0,0 +1,101 @@ +/* + * Copyright 2000-2010 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.util.continuation; + +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.progress.ProgressIndicator; +import com.intellij.openapi.progress.ProgressManager; +import com.intellij.openapi.progress.Task; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.vcs.CalledInAwt; +import com.intellij.openapi.vcs.changes.BackgroundFromStartOption; +import com.intellij.util.Consumer; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class Continuation { + private GeneralConsumer myGeneralConsumer; + + public Continuation(final Project project, final boolean cancellable) { + myGeneralConsumer = new GeneralConsumer(project, cancellable); + } + + public void run(final TaskDescriptor task) { + if (! ApplicationManager.getApplication().isDispatchThread()) { + ApplicationManager.getApplication().invokeLater(new Runnable() { + public void run() { + myGeneralConsumer.consume(task); + } + }); + } else { + myGeneralConsumer.consume(task); + } + } + + private static class TaskWrapper extends Task.Backgroundable { + private final TaskDescriptor myTaskDescriptor; + private TaskDescriptor myResult; + private final Consumer myGeneralRunner; + + private TaskWrapper(@Nullable Project project, + @NotNull String title, + boolean canBeCancelled, + TaskDescriptor taskDescriptor, + Consumer generalRunner) { + super(project, title, canBeCancelled, BackgroundFromStartOption.getInstance()); + myTaskDescriptor = taskDescriptor; + myGeneralRunner = generalRunner; + } + + @Override + public void run(@NotNull ProgressIndicator indicator) { + myResult = myTaskDescriptor.run(); + } + + @Override + public void onSuccess() { + if (myResult != null) { + myGeneralRunner.consume(myResult); + } + } + } + + private static class GeneralConsumer implements Consumer { + private final Project myProject; + private final boolean myCancellable; + + private GeneralConsumer(final Project project, boolean cancellable) { + myProject = project; + myCancellable = cancellable; + } + + @CalledInAwt + public void consume(TaskDescriptor taskDescriptor) { + ApplicationManager.getApplication().assertIsDispatchThread(); + + TaskDescriptor current = taskDescriptor; + while (current != null) { + if (Where.AWT.equals(current.getWhere())) { + current = current.run(); + } else { + // dont forget cases here if more than 2 instances of Where + ProgressManager.getInstance().run(new TaskWrapper(myProject, current.getName(), myCancellable, current, this)); + return; + } + } + } + } +} diff --git a/platform/vcs-api/src/com/intellij/util/continuation/TaskDescriptor.java b/platform/vcs-api/src/com/intellij/util/continuation/TaskDescriptor.java new file mode 100644 index 000000000000..de8c88297d75 --- /dev/null +++ b/platform/vcs-api/src/com/intellij/util/continuation/TaskDescriptor.java @@ -0,0 +1,42 @@ +/* + * Copyright 2000-2010 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.util.continuation; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public abstract class TaskDescriptor { + private final String myName; + @NotNull + private final Where myWhere; + + public TaskDescriptor(final String name, @NotNull final Where where) { + myName = name; + myWhere = where; + } + + @Nullable + public abstract TaskDescriptor run(); + + public String getName() { + return myName; + } + + @NotNull + public Where getWhere() { + return myWhere; + } +} diff --git a/platform/vcs-api/src/com/intellij/util/continuation/Where.java b/platform/vcs-api/src/com/intellij/util/continuation/Where.java new file mode 100644 index 000000000000..778966506034 --- /dev/null +++ b/platform/vcs-api/src/com/intellij/util/continuation/Where.java @@ -0,0 +1,21 @@ +/* + * Copyright 2000-2010 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.util.continuation; + +public enum Where { + AWT, + POOLED +}