Search in sources :

Example 21 with ModalityState

use of com.intellij.openapi.application.ModalityState in project intellij-community by JetBrains.

the class ThreadDumpInfo method runUnderPerformanceMonitor.

@Override
public void runUnderPerformanceMonitor(@Nullable Project project, @NotNull Runnable action) {
    if (!shouldReport() || isUnderDebug() || ApplicationManager.getApplication().isUnitTestMode()) {
        action.run();
        return;
    }
    final ModalityState initial = ModalityState.current();
    ALARM.cancelAllRequests();
    ALARM.addRequest(() -> dumpThreads(project, initial), MAX_ALLOWED_TIME);
    try {
        action.run();
    } finally {
        ALARM.cancelAllRequests();
    }
}
Also used : ModalityState(com.intellij.openapi.application.ModalityState)

Example 22 with ModalityState

use of com.intellij.openapi.application.ModalityState in project intellij-community by JetBrains.

the class PlatformTestUtil method waitForAlarm.

@TestOnly
public static void waitForAlarm(final int delay) {
    Application app = ApplicationManager.getApplication();
    assert !app.isWriteAccessAllowed() : "It's a bad idea to wait for an alarm under the write action. Somebody creates an alarm which requires read action and you are deadlocked.";
    assert app.isDispatchThread();
    Disposable tempDisposable = Disposer.newDisposable();
    final AtomicBoolean runnableInvoked = new AtomicBoolean();
    final AtomicBoolean pooledRunnableInvoked = new AtomicBoolean();
    final AtomicBoolean alarmInvoked1 = new AtomicBoolean();
    final AtomicBoolean alarmInvoked2 = new AtomicBoolean();
    final Alarm alarm = new Alarm(Alarm.ThreadToUse.SWING_THREAD);
    final Alarm pooledAlarm = new Alarm(Alarm.ThreadToUse.POOLED_THREAD, tempDisposable);
    ModalityState initialModality = ModalityState.current();
    alarm.addRequest(() -> {
        alarmInvoked1.set(true);
        app.invokeLater(() -> {
            runnableInvoked.set(true);
            alarm.addRequest(() -> alarmInvoked2.set(true), delay);
        });
    }, delay);
    pooledAlarm.addRequest(() -> pooledRunnableInvoked.set(true), delay);
    UIUtil.dispatchAllInvocationEvents();
    long start = System.currentTimeMillis();
    boolean sleptAlready = false;
    try {
        while (!alarmInvoked2.get()) {
            AtomicBoolean laterInvoked = new AtomicBoolean();
            ApplicationManager.getApplication().invokeLater(() -> laterInvoked.set(true));
            UIUtil.dispatchAllInvocationEvents();
            Assert.assertTrue(laterInvoked.get());
            TimeoutUtil.sleep(sleptAlready ? 10 : delay);
            sleptAlready = true;
            if (System.currentTimeMillis() - start > 100 * 1000) {
                throw new AssertionError("Couldn't await alarm" + "; alarm passed=" + alarmInvoked1.get() + "; modality1=" + initialModality + "; modality2=" + ModalityState.current() + "; non-modal=" + (initialModality == ModalityState.NON_MODAL) + "; invokeLater passed=" + runnableInvoked.get() + "; pooled alarm passed=" + pooledRunnableInvoked.get() + "; app.disposed=" + app.isDisposed() + "; alarm.disposed=" + alarm.isDisposed() + "; alarm.requests=" + alarm.getActiveRequestCount() + "\n delayQueue=" + StringUtil.trimLog(((AppScheduledExecutorService) AppExecutorUtil.getAppScheduledExecutorService()).dumpQueue(), 1000) + "\n invocatorQueue=" + LaterInvocator.getLaterInvocatorQueue());
            }
        }
    } finally {
        Disposer.dispose(tempDisposable);
    }
    UIUtil.dispatchAllInvocationEvents();
}
Also used : Disposable(com.intellij.openapi.Disposable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ModalityState(com.intellij.openapi.application.ModalityState) Application(com.intellij.openapi.application.Application)

Example 23 with ModalityState

use of com.intellij.openapi.application.ModalityState in project intellij-community by JetBrains.

the class ChangesBrowserDialog method initDialog.

private void initDialog(final Project project, final CommittedChangesTableModel changes, final Mode mode) {
    myProject = project;
    myChanges = changes;
    myMode = mode;
    setTitle(VcsBundle.message("dialog.title.changes.browser"));
    setCancelButtonText(CommonBundle.getCloseButtonText());
    final ModalityState currentState = ModalityState.current();
    if ((mode != Mode.Choose) && (ModalityState.NON_MODAL.equals(currentState))) {
        setModal(false);
    }
    myAppender = new AsynchConsumer<List<CommittedChangeList>>() {

        public void finished() {
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    if (ChangesBrowserDialog.this.isShowing()) {
                        myCommittedChangesBrowser.stopLoading();
                    }
                }
            });
        }

        public void consume(final List<CommittedChangeList> committedChangeLists) {
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    if (ChangesBrowserDialog.this.isShowing()) {
                        final boolean selectFirst = (myChanges.getRowCount() == 0) && (!committedChangeLists.isEmpty());
                        myChanges.addRows(committedChangeLists);
                        if (selectFirst) {
                            myCommittedChangesBrowser.selectFirstIfAny();
                        }
                    }
                }
            });
        }
    };
    init();
    if (myInitRunnable != null) {
        new AdjustComponentWhenShown() {

            @Override
            protected boolean init() {
                myInitRunnable.consume(ChangesBrowserDialog.this);
                return true;
            }
        }.install(myCommittedChangesBrowser);
    }
}
Also used : CommittedChangeList(com.intellij.openapi.vcs.versionBrowser.CommittedChangeList) ModalityState(com.intellij.openapi.application.ModalityState) List(java.util.List) CommittedChangeList(com.intellij.openapi.vcs.versionBrowser.CommittedChangeList) AdjustComponentWhenShown(com.intellij.util.ui.AdjustComponentWhenShown)

Example 24 with ModalityState

use of com.intellij.openapi.application.ModalityState in project intellij-community by JetBrains.

the class AbstractPopup method dispose.

@Override
public void dispose() {
    if (myState == State.SHOWN) {
        LOG.debug("shown popup must be cancelled");
        cancel();
    }
    if (myState == State.DISPOSE) {
        return;
    }
    debugState("dispose popup", State.INIT, State.CANCEL);
    myState = State.DISPOSE;
    if (myDisposed) {
        return;
    }
    myDisposed = true;
    if (LOG.isDebugEnabled()) {
        LOG.debug("start disposing " + myContent);
    }
    Disposer.dispose(this, false);
    ApplicationManager.getApplication().assertIsDispatchThread();
    if (myPopup != null) {
        cancel(myDisposeEvent);
    }
    if (myContent != null) {
        Container parent = myContent.getParent();
        if (parent != null)
            parent.remove(myContent);
        myContent.removeAll();
        myContent.removeKeyListener(mySearchKeyListener);
    }
    myContent = null;
    myPreferredFocusedComponent = null;
    myComponent = null;
    myFocusTrackback = null;
    myCallBack = null;
    myListeners = null;
    if (myMouseOutCanceller != null) {
        final Toolkit toolkit = Toolkit.getDefaultToolkit();
        // http://www.jetbrains.net/jira/browse/IDEADEV-21265
        if (toolkit != null) {
            toolkit.removeAWTEventListener(myMouseOutCanceller);
        }
    }
    myMouseOutCanceller = null;
    if (myFinalRunnable != null) {
        final ActionCallback typeAheadDone = new ActionCallback();
        IdeFocusManager.getInstance(myProject).typeAheadUntil(typeAheadDone);
        ModalityState modalityState = ModalityState.current();
        Runnable finalRunnable = myFinalRunnable;
        getFocusManager().doWhenFocusSettlesDown(() -> {
            //noinspection SSBasedInspection
            SwingUtilities.invokeLater(() -> {
                if (ModalityState.current().equals(modalityState)) {
                    ((TransactionGuardImpl) TransactionGuard.getInstance()).performUserActivity(finalRunnable);
                }
            // Otherwise the UI has changed unexpectedly and the action is likely not applicable.
            // And we don't want finalRunnable to perform potentially destructive actions
            //   in the context of a suddenly appeared modal dialog.
            });
            //noinspection SSBasedInspection
            SwingUtilities.invokeLater(typeAheadDone.createSetDoneRunnable());
            myFinalRunnable = null;
        });
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("stop disposing content");
    }
}
Also used : ModalityState(com.intellij.openapi.application.ModalityState) TransactionGuardImpl(com.intellij.openapi.application.TransactionGuardImpl)

Example 25 with ModalityState

use of com.intellij.openapi.application.ModalityState in project intellij-community by JetBrains.

the class ModalityContextImpl method getCurrentModalityState.

private ModalityState getCurrentModalityState() {
    ProgressIndicator progressIndicator = ProgressManager.getInstance().getProgressIndicator();
    ModalityState modalityState = progressIndicator == null ? myDefaultModalityState : progressIndicator.getModalityState();
    if (modalityState == null)
        modalityState = ModalityState.defaultModalityState();
    return modalityState;
}
Also used : ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) ModalityState(com.intellij.openapi.application.ModalityState)

Aggregations

ModalityState (com.intellij.openapi.application.ModalityState)40 Application (com.intellij.openapi.application.Application)6 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)6 NotNull (org.jetbrains.annotations.NotNull)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 Nullable (org.jetbrains.annotations.Nullable)5 Disposable (com.intellij.openapi.Disposable)4 ListSelectionListener (javax.swing.event.ListSelectionListener)4 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)3 Project (com.intellij.openapi.project.Project)3 List (java.util.List)3 CommonBundle (com.intellij.CommonBundle)2 com.intellij.find (com.intellij.find)2 ShowUsagesAction (com.intellij.find.actions.ShowUsagesAction)2 BaseProjectTreeBuilder (com.intellij.ide.projectView.BaseProjectTreeBuilder)2 AbstractProjectTreeStructure (com.intellij.ide.projectView.impl.AbstractProjectTreeStructure)2 ProjectAbstractTreeStructureBase (com.intellij.ide.projectView.impl.ProjectAbstractTreeStructureBase)2 ProjectTreeBuilder (com.intellij.ide.projectView.impl.ProjectTreeBuilder)2 NodeRenderer (com.intellij.ide.util.treeView.NodeRenderer)2 com.intellij.openapi.actionSystem (com.intellij.openapi.actionSystem)2