[workspace model] provide implementation of FacetManager which stores data in workspa...
[idea/community.git] / platform / workspaceModel-ide / src / com / intellij / workspace / legacyBridge / facet / FacetEntityChangeListener.kt
1 // Copyright 2000-2019 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.workspace.legacyBridge.facet
3
4 import com.intellij.facet.FacetManager
5 import com.intellij.facet.FacetManagerBase
6 import com.intellij.openapi.module.ModuleManager
7 import com.intellij.openapi.project.Project
8 import com.intellij.openapi.util.Disposer
9 import com.intellij.workspace.api.EntityChange
10 import com.intellij.workspace.api.EntityStoreChanged
11 import com.intellij.workspace.api.FacetEntity
12 import com.intellij.workspace.api.ModuleEntity
13 import com.intellij.workspace.ide.WorkspaceModelChangeListener
14
15 internal class FacetEntityChangeListener(private val project: Project) : WorkspaceModelChangeListener {
16   override fun beforeChanged(event: EntityStoreChanged) {
17     val changes = event.getChanges(FacetEntity::class.java)
18     for (change in changes) {
19       when (change) {
20         is EntityChange.Added -> {
21           val manager = getFacetManager(change.entity.module)
22           manager?.publisher?.beforeFacetAdded(manager.model.getOrCreateFacet(change.entity))
23         }
24         is EntityChange.Removed -> {
25           val manager = getFacetManager(change.entity.module)
26           if (manager != null) {
27             val facet = manager.model.getFacet(change.entity)
28             if (facet != null) {
29               manager.publisher.beforeFacetRemoved(facet)
30             }
31           }
32         }
33         is EntityChange.Replaced -> {
34           val manager = getFacetManager(change.newEntity.module)
35           if (manager != null && change.newEntity.name != change.oldEntity.name) {
36               manager.publisher.beforeFacetRenamed(manager.model.getOrCreateFacet(change.newEntity))
37           }
38         }
39       }
40     }
41   }
42
43
44   private val FacetManagerViaWorkspaceModel.publisher
45     get() = module.messageBus.syncPublisher(FacetManager.FACETS_TOPIC)
46
47   private fun getFacetManager(entity: ModuleEntity): FacetManagerViaWorkspaceModel? {
48     val module = ModuleManager.getInstance(project).findModuleByName(entity.name) ?: return null
49     return FacetManager.getInstance(module) as? FacetManagerViaWorkspaceModel
50   }
51
52   override fun changed(event: EntityStoreChanged) {
53     val changes = event.getChanges(FacetEntity::class.java)
54     for (change in changes) {
55       when (change) {
56         is EntityChange.Added -> {
57           val manager = getFacetManager(change.entity.module)
58           if (manager != null) {
59             val facet = manager.model.getOrCreateFacet(change.entity)
60             manager.model.updateEntity(change.entity, change.entity)
61             FacetManagerBase.setFacetName(facet, change.entity.name)
62             facet.initFacet()
63             manager.model.facetsChanged()
64             manager.publisher.facetAdded(facet)
65           }
66         }
67         is EntityChange.Removed -> {
68           val manager = getFacetManager(change.entity.module)
69           if (manager != null) {
70             val facet = manager.model.removeEntity(change.entity)
71             if (facet != null) {
72               Disposer.dispose(facet)
73               manager.model.facetsChanged()
74               manager.publisher.facetRemoved(facet)
75             }
76           }
77         }
78         is EntityChange.Replaced -> {
79           val manager = getFacetManager(change.newEntity.module)
80           if (manager != null) {
81             val facet = manager.model.updateEntity(change.oldEntity, change.newEntity)
82             if (facet != null) {
83               FacetManagerBase.setFacetName(facet, change.newEntity.name)
84               manager.model.facetsChanged()
85               if (change.oldEntity.name != change.newEntity.name) {
86                 manager.publisher.facetRenamed(facet, change.oldEntity.name)
87               }
88             }
89           }
90         }
91       }
92     }
93   }
94 }