/platform/platform-api/src/com/intellij/openapi/progress/Task.java

https://bitbucket.org/nbargnesi/idea · Java · 243 lines · 169 code · 43 blank · 31 comment · 8 complexity · 1a84745545169f0d28bbddd52dab816f MD5 · raw file

  1. /*
  2. * Copyright 2000-2010 JetBrains s.r.o.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.intellij.openapi.progress;
  17. import com.intellij.CommonBundle;
  18. import com.intellij.openapi.application.ApplicationManager;
  19. import com.intellij.openapi.diagnostic.Logger;
  20. import com.intellij.openapi.project.DumbModeAction;
  21. import com.intellij.openapi.project.Project;
  22. import com.intellij.openapi.util.text.StringUtil;
  23. import org.jetbrains.annotations.NotNull;
  24. import org.jetbrains.annotations.Nullable;
  25. /**
  26. * Intended to run tasks, both modal and non-modal (backgroundable)
  27. * Example of use:
  28. * <pre>
  29. * new Task.Backgroundable(project, "Synchronizing data", true) {
  30. * public void run(ProgressIndicator indicator) {
  31. * indicator.setText("Loading changes");
  32. * indicator.setFraction(0.0);
  33. * // some code
  34. * indicator.setFraction(1.0);
  35. * }
  36. * }.setCancelText("Stop loading").queue();
  37. * </pre>
  38. *
  39. * @see com.intellij.openapi.progress.ProgressManager#run(Task)
  40. */
  41. public abstract class Task implements TaskInfo, Progressive {
  42. private final static Logger LOG = Logger.getInstance("#com.intellij.openapi.progress.Task");
  43. protected final Project myProject;
  44. protected String myTitle;
  45. private final boolean myCanBeCancelled;
  46. private String myCancelText = CommonBundle.getCancelButtonText();
  47. private String myCancelTooltipText = CommonBundle.getCancelButtonText();
  48. public Task(@Nullable final Project project, @NotNull final String title, final boolean canBeCancelled) {
  49. myProject = project;
  50. myTitle = title;
  51. myCanBeCancelled = canBeCancelled;
  52. }
  53. public void onCancel() {}
  54. public void onSuccess() {}
  55. public final Project getProject() {
  56. return myProject;
  57. }
  58. public final void queue() {
  59. ProgressManager.getInstance().run(this);
  60. }
  61. public String getProcessId() {
  62. return "<unknown>";
  63. }
  64. @NotNull
  65. public final String getTitle() {
  66. return myTitle;
  67. }
  68. public final Task setTitle(@NotNull String title) {
  69. myTitle = title;
  70. return this;
  71. }
  72. public final String getCancelText() {
  73. return myCancelText;
  74. }
  75. public final Task setCancelText(final String cancelText) {
  76. myCancelText = cancelText;
  77. return this;
  78. }
  79. @Nullable
  80. public NotificationInfo getNotificationInfo() {
  81. return null;
  82. }
  83. @Nullable
  84. public NotificationInfo notifyFinished() {
  85. return getNotificationInfo();
  86. }
  87. public boolean isHeadless() {
  88. return ApplicationManager.getApplication().isUnitTestMode() || ApplicationManager.getApplication().isHeadlessEnvironment();
  89. }
  90. public final Task setCancelTooltipText(final String cancelTooltipText) {
  91. myCancelTooltipText = cancelTooltipText;
  92. return this;
  93. }
  94. public final String getCancelTooltipText() {
  95. return myCancelTooltipText;
  96. }
  97. public final boolean isCancellable() {
  98. return myCanBeCancelled;
  99. }
  100. public abstract boolean isModal();
  101. @NotNull
  102. public final Modal asModal() {
  103. if (isModal()) {
  104. return (Modal)this;
  105. }
  106. throw new IllegalStateException("Not a modal task");
  107. }
  108. @NotNull
  109. public final Backgroundable asBackgroundable() {
  110. if (!isModal()) {
  111. return (Backgroundable)this;
  112. }
  113. throw new IllegalStateException("Not a backgroundable task");
  114. }
  115. public abstract static class Backgroundable extends Task implements PerformInBackgroundOption {
  116. protected final PerformInBackgroundOption myBackgroundOption;
  117. public Backgroundable(@Nullable final Project project, @NotNull final String title, final boolean canBeCancelled, @Nullable final PerformInBackgroundOption backgroundOption) {
  118. super(project, title, canBeCancelled);
  119. myBackgroundOption = backgroundOption;
  120. if (StringUtil.isEmptyOrSpaces(title)) {
  121. LOG.warn("Empty title for backgroundable task.", new Throwable());
  122. }
  123. }
  124. public Backgroundable(@Nullable final Project project, @NotNull final String title, final boolean canBeCancelled) {
  125. this(project, title, canBeCancelled, null);
  126. }
  127. public Backgroundable(@Nullable final Project project, @NotNull final String title) {
  128. this(project, title, true);
  129. }
  130. public boolean shouldStartInBackground() {
  131. return myBackgroundOption == null || myBackgroundOption.shouldStartInBackground();
  132. }
  133. public void processSentToBackground() {
  134. if (myBackgroundOption != null) {
  135. myBackgroundOption.processSentToBackground();
  136. }
  137. }
  138. public final boolean isModal() {
  139. return false;
  140. }
  141. public boolean isConditionalModal() {
  142. return false;
  143. }
  144. public DumbModeAction getDumbModeAction() {
  145. return DumbModeAction.NOTHING;
  146. }
  147. }
  148. public abstract static class Modal extends Task {
  149. public Modal(@Nullable final Project project, @NotNull String title, boolean canBeCancelled) {
  150. super(project, title, canBeCancelled);
  151. }
  152. public final boolean isModal() {
  153. return true;
  154. }
  155. }
  156. public abstract static class ConditionalModal extends Backgroundable {
  157. public ConditionalModal(@Nullable final Project project, @NotNull final String title, final boolean canBeCancelled,
  158. @NotNull final PerformInBackgroundOption backgroundOption) {
  159. super(project, title, canBeCancelled, backgroundOption);
  160. }
  161. public final boolean isConditionalModal() {
  162. return true;
  163. }
  164. }
  165. public static class NotificationInfo {
  166. private final String myNotificationName;
  167. private final String myNotificationTitle;
  168. private final String myNotificationText;
  169. private final boolean myShowWhenFocused;
  170. public NotificationInfo(@NotNull final String notificationName,
  171. @NotNull final String notificationTitle,
  172. @NotNull final String notificationText) {
  173. this(notificationName, notificationTitle, notificationText, false);
  174. }
  175. public NotificationInfo(@NotNull final String notificationName,
  176. @NotNull final String notificationTitle,
  177. @NotNull final String notificationText,
  178. final boolean showWhenFocused) {
  179. myNotificationName = notificationName;
  180. myNotificationTitle = notificationTitle;
  181. myNotificationText = notificationText;
  182. myShowWhenFocused = showWhenFocused;
  183. }
  184. @NotNull
  185. public String getNotificationName() {
  186. return myNotificationName;
  187. }
  188. @NotNull
  189. public String getNotificationTitle() {
  190. return myNotificationTitle;
  191. }
  192. @NotNull
  193. public String getNotificationText() {
  194. return myNotificationText;
  195. }
  196. public boolean isShowWhenFocused() {
  197. return myShowWhenFocused;
  198. }
  199. }
  200. }