public boolean runProcessWithProgressSynchronously( @NotNull final Runnable process, @NotNull final String progressTitle, final boolean canBeCanceled, @Nullable final Project project, final JComponent parentComponent, final String cancelText) { assertIsDispatchThread(); if (myExceptionalThreadWithReadAccessRunnable != null || ApplicationManager.getApplication().isUnitTestMode() || ApplicationManager.getApplication().isHeadlessEnvironment()) { try { ProgressManager.getInstance().runProcess(process, new EmptyProgressIndicator()); } catch (ProcessCanceledException e) { // ok to ignore. return false; } return true; } final ProgressWindow progress = new ProgressWindow(canBeCanceled, false, project, parentComponent, cancelText); progress.setTitle(progressTitle); try { myExceptionalThreadWithReadAccessRunnable = process; final boolean[] threadStarted = {false}; SwingUtilities.invokeLater( new Runnable() { public void run() { if (myExceptionalThreadWithReadAccessRunnable != process) { LOG.error( "myExceptionalThreadWithReadAccessRunnable != process, process = " + myExceptionalThreadWithReadAccessRunnable); } executeOnPooledThread( new Runnable() { public void run() { if (myExceptionalThreadWithReadAccessRunnable != process) { LOG.error( "myExceptionalThreadWithReadAccessRunnable != process, process = " + myExceptionalThreadWithReadAccessRunnable); } final boolean old = setExceptionalThreadWithReadAccessFlag(true); LOG.assertTrue(isReadAccessAllowed()); try { ProgressManager.getInstance().runProcess(process, progress); } catch (ProcessCanceledException e) { progress.cancel(); // ok to ignore. } catch (RuntimeException e) { progress.cancel(); throw e; } finally { setExceptionalThreadWithReadAccessFlag(old); makeChangesVisibleToEDT(); } } }); threadStarted[0] = true; } }); progress.startBlocking(); LOG.assertTrue(threadStarted[0]); LOG.assertTrue(!progress.isRunning()); } finally { myExceptionalThreadWithReadAccessRunnable = null; makeChangesVisibleToEDT(); } return !progress.isCanceled(); }
private void runLayoutCodeProcess( final Runnable readAction, final Runnable writeAction, final boolean globalAction) { final ProgressWindow progressWindow = new ProgressWindow(true, myProject); progressWindow.setTitle(myCommandName); progressWindow.setText(myProgressText); final ModalityState modalityState = ModalityState.current(); final Runnable process = new Runnable() { @Override public void run() { ApplicationManager.getApplication().runReadAction(readAction); } }; Runnable runnable = new Runnable() { @Override public void run() { try { ProgressManager.getInstance().runProcess(process, progressWindow); } catch (ProcessCanceledException e) { return; } catch (IndexNotReadyException e) { return; } final Runnable writeRunnable = new Runnable() { @Override public void run() { CommandProcessor.getInstance() .executeCommand( myProject, new Runnable() { @Override public void run() { if (globalAction) CommandProcessor.getInstance() .markCurrentCommandAsGlobal(myProject); try { writeAction.run(); if (myPostRunnable != null) { ApplicationManager.getApplication().invokeLater(myPostRunnable); } } catch (IndexNotReadyException ignored) { } } }, myCommandName, null); } }; if (ApplicationManager.getApplication().isUnitTestMode()) { writeRunnable.run(); } else { ApplicationManager.getApplication() .invokeLater(writeRunnable, modalityState, myProject.getDisposed()); } } }; if (ApplicationManager.getApplication().isUnitTestMode()) { runnable.run(); } else { ApplicationManager.getApplication().executeOnPooledThread(runnable); } }