<commit_before>package com.intellij.openapi.progress.util;

import com.intellij.ide.GeneralSettings;
import com.intellij.ide.IdeEventQueue;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.EmptyRunnable;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.wm.WindowManager;
import com.intellij.openapi.wm.ex.WindowManagerEx;
import com.intellij.ui.FocusTrackback;
import com.intellij.ui.TitlePanel;
import com.intellij.ui.awt.RelativePoint;
import com.intellij.util.Alarm;
import com.intellij.util.ui.BlockBorder;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.*;
import java.io.File;

@SuppressWarnings({"NonStaticInitializer"})
public class ProgressWindow extends BlockingProgressIndicator {
  private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.progress.util.ProgressWindow");

  private static final int UPDATE_INTERVAL = 50; //msec. 20 frames per second.

  private MyDialog myDialog;
  private Alarm myUpdateAlarm = new Alarm(Alarm.ThreadToUse.SWING_THREAD);
  private Alarm myInstallFunAlarm = new Alarm(Alarm.ThreadToUse.SWING_THREAD);
  private Alarm myShowWindowAlarm = new Alarm(Alarm.ThreadToUse.SWING_THREAD);

  private final Project myProject;
  private final boolean myShouldShowBackground;
  private final boolean myShouldShowCancel;
  private String myCancelText;
  private JComponent myParentComponent;

  private String myTitle = null;

  private boolean myStoppedAlready = false;
  private FocusTrackback myFocusTrackback;
  private boolean myStared = false;
  private boolean myBackgrounded = false;

  public ProgressWindow(boolean shouldShowCancel, Project project) {
    this(shouldShowCancel, false, project);
  }

  public ProgressWindow(boolean shouldShowCancel, boolean shouldShowBackground, Project project) {
    this(shouldShowCancel, shouldShowBackground, project, null);
  }

  public ProgressWindow(boolean shouldShowCancel, boolean shouldShowBackground, Project project, String cancelText) {
    myShouldShowBackground = shouldShowBackground;
    myProject = project;
    myShouldShowCancel = shouldShowCancel;
    myCancelText = cancelText;
    setModalityProgress(myShouldShowBackground ? null : this);
    myFocusTrackback = new FocusTrackback(this, WindowManager.getInstance().suggestParentWindow(project));
    if(myParentComponent != null) {
      myDialog = new MyDialog(myShouldShowCancel, myShouldShowBackground, myParentComponent, myCancelText);
    } else {
      myDialog = new MyDialog(myShouldShowCancel, myShouldShowBackground, myProject, myCancelText);
    }
  }

  public ProgressWindow(boolean shouldShowCancel, boolean shouldShowBackground, Project project, JComponent parentComponent, String cancelText) {
    myShouldShowBackground = shouldShowBackground;
    myProject = project;
    myShouldShowCancel = shouldShowCancel;
    myCancelText = cancelText;
    myParentComponent = parentComponent;
    setModalityProgress(myShouldShowBackground ? null : this);
    myFocusTrackback = new FocusTrackback(this, WindowManager.getInstance().suggestParentWindow(project));
    if(myParentComponent != null) {
      myDialog = new MyDialog(myShouldShowCancel, myShouldShowBackground, myParentComponent, myCancelText);
    } else {
      myDialog = new MyDialog(myShouldShowCancel, myShouldShowBackground, myProject, myCancelText);
    }
  }

  public synchronized void start() {
    LOG.assertTrue(!isRunning());
    LOG.assertTrue(!myStoppedAlready);

    super.start();
    if (!ApplicationManager.getApplication().isUnitTestMode()) {
      SwingUtilities.invokeLater(new Runnable() {
        public void run() {
          myShowWindowAlarm.addRequest(new Runnable() {
            public void run() {
              if (isRunning()) {
                showDialog();
              }
            }
          }, 300, ModalityState.current());
        }
      });
    }

    myStared = true;
  }

  public void startBlocking() {
    ApplicationManager.getApplication().assertIsDispatchThread();
    LOG.assertTrue(!isRunning());
    LOG.assertTrue(!myStoppedAlready);

    enterModality();

    IdeEventQueue.getInstance().flushQueue();
    IdeEventQueue.getInstance().pumpEventsForHierarchy(myDialog.myPanel, new Condition<AWTEvent>() {
      public boolean value(final AWTEvent object) {
        if (myShouldShowCancel &&
            object instanceof KeyEvent &&
            object.getID() == KeyEvent.KEY_PRESSED &&
            ((KeyEvent)object).getKeyCode() == KeyEvent.VK_ESCAPE &&
            ((KeyEvent)object).getModifiers() == 0) {
          SwingUtilities.invokeLater(new Runnable() {
            public void run() {
              ProgressWindow.this.cancel();
            }
          });
        }
        return myStared && !isRunning();
      }
    });

    exitModality();
  }

  private void showDialog() {
    if (myShouldShowBackground && GeneralSettings.getInstance().isSearchInBackground()) {
      return;
    }

    final JComponent cmp = ProgressManager.getInstance().getProvidedFunComponent(myProject, "<unknown>");
    if (cmp != null && !ApplicationManager.getApplication().isHeadlessEnvironment()) {
      Runnable installer = new Runnable() {
        public void run() {<commit_msg>Probably fixed progress dialog shown while process is already terminated.<commit_after>package com.intellij.openapi.progress.util;

import com.intellij.ide.GeneralSettings;
import com.intellij.ide.IdeEventQueue;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.EmptyRunnable;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.wm.WindowManager;
import com.intellij.openapi.wm.ex.WindowManagerEx;
import com.intellij.ui.FocusTrackback;
import com.intellij.ui.TitlePanel;
import com.intellij.ui.awt.RelativePoint;
import com.intellij.util.Alarm;
import com.intellij.util.ui.BlockBorder;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.*;
import java.io.File;

@SuppressWarnings({"NonStaticInitializer"})
public class ProgressWindow extends BlockingProgressIndicator {
  private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.progress.util.ProgressWindow");

  private static final int UPDATE_INTERVAL = 50; //msec. 20 frames per second.

  private MyDialog myDialog;
  private Alarm myUpdateAlarm = new Alarm(Alarm.ThreadToUse.SWING_THREAD);
  private Alarm myInstallFunAlarm = new Alarm(Alarm.ThreadToUse.SWING_THREAD);
  private Alarm myShowWindowAlarm = new Alarm(Alarm.ThreadToUse.SWING_THREAD);

  private final Project myProject;
  private final boolean myShouldShowBackground;
  private final boolean myShouldShowCancel;
  private String myCancelText;
  private JComponent myParentComponent;

  private String myTitle = null;

  private boolean myStoppedAlready = false;
  private FocusTrackback myFocusTrackback;
  private boolean myStared = false;
  private boolean myBackgrounded = false;

  public ProgressWindow(boolean shouldShowCancel, Project project) {
    this(shouldShowCancel, false, project);
  }

  public ProgressWindow(boolean shouldShowCancel, boolean shouldShowBackground, Project project) {
    this(shouldShowCancel, shouldShowBackground, project, null);
  }

  public ProgressWindow(boolean shouldShowCancel, boolean shouldShowBackground, Project project, String cancelText) {
    myShouldShowBackground = shouldShowBackground;
    myProject = project;
    myShouldShowCancel = shouldShowCancel;
    myCancelText = cancelText;
    setModalityProgress(myShouldShowBackground ? null : this);
    myFocusTrackback = new FocusTrackback(this, WindowManager.getInstance().suggestParentWindow(project));
    if(myParentComponent != null) {
      myDialog = new MyDialog(myShouldShowCancel, myShouldShowBackground, myParentComponent, myCancelText);
    } else {
      myDialog = new MyDialog(myShouldShowCancel, myShouldShowBackground, myProject, myCancelText);
    }
  }

  public ProgressWindow(boolean shouldShowCancel, boolean shouldShowBackground, Project project, JComponent parentComponent, String cancelText) {
    myShouldShowBackground = shouldShowBackground;
    myProject = project;
    myShouldShowCancel = shouldShowCancel;
    myCancelText = cancelText;
    myParentComponent = parentComponent;
    setModalityProgress(myShouldShowBackground ? null : this);
    myFocusTrackback = new FocusTrackback(this, WindowManager.getInstance().suggestParentWindow(project));
    if(myParentComponent != null) {
      myDialog = new MyDialog(myShouldShowCancel, myShouldShowBackground, myParentComponent, myCancelText);
    } else {
      myDialog = new MyDialog(myShouldShowCancel, myShouldShowBackground, myProject, myCancelText);
    }
  }

  public synchronized void start() {
    LOG.assertTrue(!isRunning());
    LOG.assertTrue(!myStoppedAlready);

    super.start();
    if (!ApplicationManager.getApplication().isUnitTestMode()) {
      SwingUtilities.invokeLater(new Runnable() {
        public void run() {
          myShowWindowAlarm.addRequest(new Runnable() {
            public void run() {
              if (isRunning()) {
                showDialog();
              }
            }
          }, 300, ModalityState.current());
        }
      });
    }

    myStared = true;
  }

  public void startBlocking() {
    ApplicationManager.getApplication().assertIsDispatchThread();
    LOG.assertTrue(!isRunning());
    LOG.assertTrue(!myStoppedAlready);

    enterModality();

    IdeEventQueue.getInstance().flushQueue();
    IdeEventQueue.getInstance().pumpEventsForHierarchy(myDialog.myPanel, new Condition<AWTEvent>() {
      public boolean value(final AWTEvent object) {
        if (myShouldShowCancel &&
            object instanceof KeyEvent &&
            object.getID() == KeyEvent.KEY_PRESSED &&
            ((KeyEvent)object).getKeyCode() == KeyEvent.VK_ESCAPE &&
            ((KeyEvent)object).getModifiers() == 0) {
          SwingUtilities.invokeLater(new Runnable() {
            public void run() {
              ProgressWindow.this.cancel();
            }
          });
        }
        return myStared && !isRunning();
      }
    });

    exitModality();
  }

  private void showDialog() {
    if (!isRunning() || isCanceled()) {
      return;
    }
    
    if (myShouldShowBackground && GeneralSettings.getInstance().isSearchInBackground()) {
      return;
    }

    final JComponent cmp = ProgressManager.getInstance().getProvidedFunComponent(myProject, "<unknown>");
    if (cmp != null && !ApplicationManager.getApplication().isHeadlessEnvironment()) {
      Runnable installer = new Runnable() {
        public void run() {
{'fraction_non_alphanumeric': 0.09701037217815742, 'fraction_numerical': 0.0013945785757866296, 'mean_word_length': 4.184816990510619, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5711115', 'n_tokens_mistral': 3212, 'n_tokens_neox': 3136, 'n_words': 647}