List of usage examples for com.intellij.openapi.application ModalityStateListener ModalityStateListener
ModalityStateListener
From source file:com.intellij.codeInsight.daemon.impl.DaemonListeners.java
License:Apache License
public DaemonListeners(@NotNull final Project project, @NotNull DaemonCodeAnalyzerImpl daemonCodeAnalyzer, @NotNull final EditorTracker editorTracker, @NotNull EditorFactory editorFactory, @NotNull PsiDocumentManager psiDocumentManager, @NotNull CommandProcessor commandProcessor, @NotNull EditorColorsManager editorColorsManager, @NotNull final Application application, @NotNull InspectionProfileManager inspectionProfileManager, @NotNull InspectionProjectProfileManager inspectionProjectProfileManager, @NotNull TodoConfiguration todoConfiguration, @NotNull ActionManagerEx actionManagerEx, @NotNull VirtualFileManager virtualFileManager, @NotNull final NamedScopeManager namedScopeManager, @NotNull final DependencyValidationManager dependencyValidationManager, @NotNull final FileDocumentManager fileDocumentManager, @NotNull final PsiManager psiManager, @NotNull final FileEditorManager fileEditorManager, @NotNull TooltipController tooltipController, @NotNull UndoManager undoManager, @NotNull ProjectLevelVcsManager projectLevelVcsManager, @NotNull VcsDirtyScopeManager vcsDirtyScopeManager, @NotNull FileStatusManager fileStatusManager, @NotNull EditorColorsManager colorsManager) { Disposer.register(project, this); myProject = project;/* w w w . ja va 2 s . co m*/ myDaemonCodeAnalyzer = daemonCodeAnalyzer; myPsiDocumentManager = psiDocumentManager; myFileEditorManager = fileEditorManager; myUndoManager = undoManager; myProjectLevelVcsManager = projectLevelVcsManager; myVcsDirtyScopeManager = vcsDirtyScopeManager; myFileStatusManager = fileStatusManager; myActionManager = actionManagerEx; myTooltipController = tooltipController; boolean replaced = ((UserDataHolderEx) myProject).replace(DAEMON_INITIALIZED, null, Boolean.TRUE); LOG.assertTrue(replaced, "Daemon listeners already initialized for the project " + myProject); MessageBus messageBus = myProject.getMessageBus(); myDaemonEventPublisher = messageBus.syncPublisher(DaemonCodeAnalyzer.DAEMON_EVENT_TOPIC); final MessageBusConnection connection = messageBus.connect(); if (project.isDefault()) return; EditorEventMulticaster eventMulticaster = editorFactory.getEventMulticaster(); eventMulticaster.addDocumentListener(new DocumentAdapter() { // clearing highlighters before changing document because change can damage editor highlighters drastically, so we'll clear more than necessary @Override public void beforeDocumentChange(final DocumentEvent e) { if (isUnderIgnoredAction(null)) return; Document document = e.getDocument(); VirtualFile virtualFile = fileDocumentManager.getFile(document); Project project = virtualFile == null ? null : ProjectUtil.guessProjectForFile(virtualFile); if (!worthBothering(document, project)) { return; //no need to stop daemon if something happened in the console } stopDaemon(true, "Document change"); UpdateHighlightersUtil.updateHighlightersByTyping(myProject, e); } }, this); eventMulticaster.addCaretListener(new CaretAdapter() { @Override public void caretPositionChanged(CaretEvent e) { final Editor editor = e.getEditor(); if (!editor.getComponent().isShowing() && !application.isUnitTestMode() || !worthBothering(editor.getDocument(), editor.getProject())) { return; //no need to stop daemon if something happened in the console } if (!application.isUnitTestMode()) { ApplicationManager.getApplication().invokeLater(new Runnable() { @Override public void run() { if (!editor.getComponent().isShowing() || myProject.isDisposed()) { return; } myDaemonCodeAnalyzer.hideLastIntentionHint(); } }, ModalityState.current()); } } }, this); eventMulticaster.addEditorMouseMotionListener(new MyEditorMouseMotionListener(), this); eventMulticaster.addEditorMouseListener(new MyEditorMouseListener(myTooltipController), this); EditorTrackerListener editorTrackerListener = new EditorTrackerListener() { private List<Editor> myActiveEditors = Collections.emptyList(); @Override public void activeEditorsChanged(@NotNull List<Editor> editors) { List<Editor> activeEditors = editorTracker.getActiveEditors(); if (myActiveEditors.equals(activeEditors)) { return; } myActiveEditors = activeEditors; stopDaemon(true, "Active editor change"); // do not stop daemon if idea loses/gains focus if (ApplicationManager.getApplication().isDispatchThread() && LaterInvocator.isInModalContext()) { // editor appear in modal context, re-enable the daemon myDaemonCodeAnalyzer.setUpdateByTimerEnabled(true); } for (Editor editor : activeEditors) { repaintErrorStripeRenderer(editor, myProject); } } }; editorTracker.addEditorTrackerListener(editorTrackerListener, this); EditorFactoryListener editorFactoryListener = new EditorFactoryListener() { @Override public void editorCreated(@NotNull EditorFactoryEvent event) { Editor editor = event.getEditor(); Document document = editor.getDocument(); Project editorProject = editor.getProject(); // worthBothering() checks for getCachedPsiFile, so call getPsiFile here PsiFile file = editorProject == null ? null : PsiDocumentManager.getInstance(editorProject).getPsiFile(document); if (!editor.getComponent().isShowing() || !worthBothering(document, editorProject)) { LOG.debug("Not worth: " + file); return; } repaintErrorStripeRenderer(editor, myProject); } @Override public void editorReleased(@NotNull EditorFactoryEvent event) { // mem leak after closing last editor otherwise UIUtil.invokeLaterIfNeeded(new Runnable() { @Override public void run() { myDaemonCodeAnalyzer.hideLastIntentionHint(); } }); } }; editorFactory.addEditorFactoryListener(editorFactoryListener, this); PsiDocumentManagerImpl documentManager = (PsiDocumentManagerImpl) psiDocumentManager; PsiChangeHandler changeHandler = new PsiChangeHandler(myProject, documentManager, editorFactory, connection, daemonCodeAnalyzer.getFileStatusMap()); Disposer.register(this, changeHandler); psiManager.addPsiTreeChangeListener(changeHandler, changeHandler); connection.subscribe(ProjectTopics.PROJECT_ROOTS, new ModuleRootAdapter() { @Override public void rootsChanged(ModuleRootEvent event) { final FileEditor[] editors = fileEditorManager.getSelectedEditors(); if (editors.length == 0) return; application.invokeLater(new Runnable() { @Override public void run() { if (myProject.isDisposed()) return; for (FileEditor fileEditor : editors) { if (fileEditor instanceof TextEditor) { repaintErrorStripeRenderer(((TextEditor) fileEditor).getEditor(), myProject); } } } }, ModalityState.stateForComponent(editors[0].getComponent())); } }); connection.subscribe(DumbService.DUMB_MODE, new DumbService.DumbModeListener() { @Override public void enteredDumbMode() { stopDaemonAndRestartAllFiles(); } @Override public void exitDumbMode() { stopDaemonAndRestartAllFiles(); } }); connection.subscribe(PowerSaveMode.TOPIC, new PowerSaveMode.Listener() { @Override public void powerSaveStateChanged() { stopDaemon(true, "Power save mode change"); } }); colorsManager.addEditorColorsListener(new EditorColorsListener() { @Override public void globalSchemeChange(EditorColorsScheme scheme) { stopDaemonAndRestartAllFiles(); } }, this); commandProcessor.addCommandListener(new MyCommandListener(), this); application.addApplicationListener(new MyApplicationListener(), this); editorColorsManager.addEditorColorsListener(new MyEditorColorsListener(), this); inspectionProfileManager.addProfileChangeListener(new MyProfileChangeListener(), this); inspectionProjectProfileManager.addProfilesListener(new MyProfileChangeListener(), this); todoConfiguration.addPropertyChangeListener(new MyTodoListener(), this); todoConfiguration.colorSettingsChanged(); actionManagerEx.addAnActionListener(new MyAnActionListener(), this); virtualFileManager.addVirtualFileListener(new VirtualFileAdapter() { @Override public void propertyChanged(@NotNull VirtualFilePropertyEvent event) { String propertyName = event.getPropertyName(); if (VirtualFile.PROP_NAME.equals(propertyName)) { stopDaemonAndRestartAllFiles(); VirtualFile virtualFile = event.getFile(); PsiFile psiFile = !virtualFile.isValid() ? null : ((PsiManagerEx) psiManager).getFileManager().getCachedPsiFile(virtualFile); if (psiFile != null && !myDaemonCodeAnalyzer.isHighlightingAvailable(psiFile)) { Document document = fileDocumentManager.getCachedDocument(virtualFile); if (document != null) { // highlight markers no more //todo clear all highlights regardless the pass id // Here color scheme required for TextEditorFields, as far as I understand this // code related to standard file editors, which always use Global color scheme, // thus we can pass null here. final EditorColorsScheme editorColorScheme = null; UpdateHighlightersUtil.setHighlightersToEditor(myProject, document, 0, document.getTextLength(), Collections.<HighlightInfo>emptyList(), editorColorScheme, Pass.UPDATE_ALL); } } } if (!propertyName.equals(PsiTreeChangeEvent.PROP_WRITABLE)) { stopDaemon(true, "Virtual file property change"); } } }, this); ((EditorEventMulticasterEx) eventMulticaster).addErrorStripeListener(new ErrorStripeHandler(myProject), this); ModalityStateListener modalityStateListener = new ModalityStateListener() { @Override public void beforeModalityStateChanged(boolean entering) { // before showing dialog we are in non-modal context yet, and before closing dialog we are still in modal context boolean inModalContext = LaterInvocator.isInModalContext(); stopDaemon(inModalContext, "Modality change"); myDaemonCodeAnalyzer.setUpdateByTimerEnabled(inModalContext); } }; LaterInvocator.addModalityStateListener(modalityStateListener, this); messageBus.connect().subscribe(SeverityRegistrar.SEVERITIES_CHANGED_TOPIC, new Runnable() { @Override public void run() { UIUtil.invokeLaterIfNeeded(new Runnable() { @Override public void run() { if (!project.isDisposed()) { for (Editor editor : editorTracker.getActiveEditors()) { repaintErrorStripeRenderer(editor, project); } } } }); } }); if (RefResolveService.ENABLED) { RefResolveService resolveService = RefResolveService.getInstance(project); resolveService.addListener(this, new RefResolveService.Listener() { @Override public void allFilesResolved() { stopDaemon(true, "RefResolveService is up to date"); } }); } }
From source file:org.jetbrains.idea.maven.utils.MavenMergingUpdateQueue.java
License:Apache License
public void makeModalAware(Project project) { MavenUtil.invokeLater(project, new Runnable() { @Override/* w w w. ja va2 s.c o m*/ public void run() { final ModalityStateListener listener = new ModalityStateListener() { @Override public void beforeModalityStateChanged(boolean entering) { if (entering) { suspend(); } else { resume(); } } }; LaterInvocator.addModalityStateListener(listener, MavenMergingUpdateQueue.this); if (MavenUtil.isInModalContext()) { suspend(); } } }); }