/platform/platform-impl/src/com/intellij/openapi/progress/util/SmoothProgressAdapter.java

https://bitbucket.org/nbargnesi/idea · Java · 191 lines · 139 code · 33 blank · 19 comment · 17 complexity · c5692f6743b1496453b009d82c0a6123 MD5 · raw file

  1. /*
  2. * Copyright 2000-2009 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.util;
  17. import com.intellij.openapi.Disposable;
  18. import com.intellij.openapi.application.ApplicationManager;
  19. import com.intellij.openapi.progress.ProgressIndicator;
  20. import com.intellij.openapi.project.Project;
  21. import com.intellij.openapi.ui.DialogWrapper;
  22. import com.intellij.openapi.util.Disposer;
  23. import com.intellij.util.Alarm;
  24. import com.intellij.util.concurrency.Semaphore;
  25. import javax.swing.*;
  26. public class SmoothProgressAdapter extends BlockingProgressIndicator {
  27. private static final int SHOW_DELAY = 500;
  28. private final Alarm myStartupAlarm = new Alarm(Alarm.ThreadToUse.SHARED_THREAD);
  29. protected ProgressIndicator myOriginal;
  30. private final Project myProject;
  31. private boolean myOriginalStarted;
  32. private DialogWrapper myDialog;
  33. private final Runnable myShowRequest = new Runnable() {
  34. public void run() {
  35. synchronized(SmoothProgressAdapter.this){
  36. if (!isRunning()) {
  37. return;
  38. }
  39. myOriginal.start();
  40. myOriginalStarted = true;
  41. myOriginal.setText(getText());
  42. myOriginal.setFraction(getFraction());
  43. myOriginal.setText2(getText2());
  44. }
  45. }
  46. };
  47. public SmoothProgressAdapter(ProgressIndicator original, Project project){
  48. myOriginal = original;
  49. myProject = project;
  50. if (myOriginal.isModal()) {
  51. myOriginal.setModalityProgress(this);
  52. this.setModalityProgress(this);
  53. }
  54. }
  55. public void setIndeterminate(boolean indeterminate) {
  56. super.setIndeterminate(indeterminate);
  57. myOriginal.setIndeterminate(indeterminate);
  58. }
  59. public boolean isIndeterminate() {
  60. return myOriginal.isIndeterminate();
  61. }
  62. public synchronized void start() {
  63. if (isRunning()) return;
  64. super.start();
  65. myOriginalStarted = false;
  66. myStartupAlarm.addRequest(myShowRequest, SHOW_DELAY);
  67. }
  68. public void startBlocking() {
  69. ApplicationManager.getApplication().assertIsDispatchThread();
  70. start();
  71. if (isModal()) {
  72. showDialog();
  73. }
  74. }
  75. private void showDialog(){
  76. if (myDialog == null){
  77. //System.out.println("showDialog()");
  78. myDialog = new DialogWrapper(myProject, false) {
  79. {
  80. getWindow().setBounds(0, 0, 1, 1);
  81. setResizable(false);
  82. }
  83. protected boolean isProgressDialog() {
  84. return true;
  85. }
  86. protected JComponent createCenterPanel() {
  87. return null;
  88. }
  89. };
  90. myDialog.setModal(true);
  91. myDialog.setUndecorated(true);
  92. myDialog.show();
  93. }
  94. }
  95. public synchronized void stop() {
  96. if (myOriginal.isRunning()) {
  97. myOriginal.stop();
  98. }
  99. else {
  100. myStartupAlarm.cancelAllRequests();
  101. if (!myOriginalStarted && myOriginal instanceof Disposable) {
  102. // dispose original because start & stop were not called so original progress might not have released its resources
  103. Disposer.dispose(((Disposable)myOriginal));
  104. }
  105. }
  106. // needed only for correct assertion of !progress.isRunning() in ApplicationImpl.runProcessWithProgressSynchroniously
  107. final Semaphore semaphore = new Semaphore();
  108. semaphore.down();
  109. SwingUtilities.invokeLater(
  110. new Runnable() {
  111. public void run() {
  112. semaphore.waitFor();
  113. if (myDialog != null){
  114. //System.out.println("myDialog.destroyProcess()");
  115. myDialog.close(DialogWrapper.OK_EXIT_CODE);
  116. myDialog = null;
  117. }
  118. }
  119. }
  120. );
  121. try {
  122. super.stop(); // should be last to not leaveModal before closing the dialog
  123. }
  124. finally {
  125. semaphore.up();
  126. }
  127. }
  128. public synchronized void setText(String text) {
  129. super.setText(text);
  130. if (myOriginal.isRunning()) {
  131. myOriginal.setText(text);
  132. }
  133. }
  134. public synchronized void setFraction(double fraction) {
  135. super.setFraction(fraction);
  136. if (myOriginal.isRunning()) {
  137. myOriginal.setFraction(fraction);
  138. }
  139. }
  140. public synchronized void setText2(String text) {
  141. super.setText2(text);
  142. if (myOriginal.isRunning()) {
  143. myOriginal.setText2(text);
  144. }
  145. }
  146. public void cancel() {
  147. super.cancel();
  148. myOriginal.cancel();
  149. }
  150. public boolean isCanceled() {
  151. if (super.isCanceled()) return true;
  152. if (!myOriginalStarted) return false;
  153. return myOriginal.isCanceled();
  154. }
  155. public ProgressIndicator getOriginal() {
  156. return myOriginal;
  157. }
  158. }