IDEA-250477 vcs: fix VCS scopes not being visible after project opening
[idea/community.git] / platform / vcs-impl / src / com / intellij / vcs / changes / ChangeListScopeViewUpdater.kt
1 // Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
2 package com.intellij.vcs.changes
3
4 import com.intellij.ide.projectView.ProjectView
5 import com.intellij.ide.scopeView.ScopeViewPane
6 import com.intellij.openapi.application.ApplicationManager
7 import com.intellij.openapi.components.serviceIfCreated
8 import com.intellij.openapi.project.Project
9 import com.intellij.openapi.vcs.changes.ChangeList
10 import com.intellij.openapi.vcs.changes.ChangeListAdapter
11 import com.intellij.openapi.vcs.changes.LocalChangeList
12 import com.intellij.openapi.vcs.changes.LocalChangeListsLoadedListener
13 import com.intellij.packageDependencies.DependencyValidationManager
14
15 class ChangeListScopeViewUpdater(private val project: Project) : ChangeListAdapter() {
16   class InitialRefresh(private val project: Project) : LocalChangeListsLoadedListener {
17     override fun processLoadedLists(lists: MutableList<LocalChangeList>) {
18       updateAvailableScopesList(project)
19     }
20   }
21
22   override fun changeListAdded(list: ChangeList) {
23     updateAvailableScopesList(project)
24   }
25
26   override fun changeListRemoved(list: ChangeList) {
27     updateAvailableScopesList(project)
28   }
29
30   override fun changeListRenamed(list: ChangeList, oldName: String) {
31     updateAvailableScopesList(project)
32   }
33
34   override fun changeListAvailabilityChanged() {
35     updateAvailableScopesList(project)
36   }
37
38   override fun changeListsChanged() {
39     updateActiveScope(project)
40   }
41
42   companion object {
43     private fun updateActiveScope(project: Project) {
44       ApplicationManager.getApplication().invokeLater {
45         if (project.isDisposed) return@invokeLater
46         val projectView = project.serviceIfCreated<ProjectView>() ?: return@invokeLater
47         val pane = projectView.getProjectViewPaneById(ScopeViewPane.ID) as? ScopeViewPane ?: return@invokeLater
48         if (pane.selectedScope is ChangeListScope) {
49           pane.updateSelectedScope()
50         }
51       }
52     }
53
54     private fun updateAvailableScopesList(project: Project) {
55       ApplicationManager.getApplication().invokeLater {
56         if (project.isDisposed) return@invokeLater
57         DependencyValidationManager.getInstance(project).fireScopeListeners()
58       }
59     }
60   }
61 }