1
1
package com.cycode.plugin.components.toolWindow
2
2
3
- import com.cycode.plugin.CycodeBundle
4
- import com.cycode.plugin.components.Component
5
3
import com.cycode.plugin.components.toolWindow.components.authContentTab.AuthContentTab
6
4
import com.cycode.plugin.components.toolWindow.components.loadingContentTab.LoadingContentTab
7
5
import com.cycode.plugin.components.toolWindow.components.scanContentTab.ScanContentTab
8
6
import com.cycode.plugin.icons.PluginIcons
9
- import com.cycode.plugin.services.CycodeService
10
7
import com.cycode.plugin.services.cycode
11
8
import com.cycode.plugin.services.pluginState
12
9
import com.intellij.openapi.application.ApplicationManager
13
10
import com.intellij.openapi.application.WriteAction
14
11
import com.intellij.openapi.project.DumbAware
15
12
import com.intellij.openapi.project.Project
13
+ import com.intellij.openapi.util.Disposer
16
14
import com.intellij.openapi.wm.ToolWindow
17
15
import com.intellij.openapi.wm.ToolWindowFactory
18
- import com.intellij.openapi.wm.ToolWindowManager
19
16
import com.intellij.ui.content.Content
20
17
import com.intellij.ui.content.ContentFactory
21
-
18
+ import javax.swing.JPanel
22
19
23
20
class CycodeToolWindowFactory : DumbAware , ToolWindowFactory {
24
21
@@ -27,48 +24,58 @@ class CycodeToolWindowFactory : DumbAware, ToolWindowFactory {
27
24
}
28
25
29
26
override fun createToolWindowContent (project : Project , toolWindow : ToolWindow ) {
30
- createLoadingTabOnly(project)
27
+ val service = cycode(project)
28
+
29
+ val contentTab = CycodeContentTab (project)
30
+ TabManager .addTab(project, contentTab)
31
+
32
+ val defaultRightPanel = LoadingContentTab ().getContent(service)
33
+ val toolWindowContent = createToolWindowContent(contentTab.updateContent(defaultRightPanel))
34
+ toolWindow.contentManager.addContent(toolWindowContent)
35
+
36
+ Disposer .register(service, toolWindowContent)
31
37
}
32
38
33
39
override fun shouldBeAvailable (project : Project ) = true
34
- }
35
40
36
- private fun getCycodeToolWindow (project : Project ): ToolWindow ? {
37
- return ToolWindowManager .getInstance(project).getToolWindow(CycodeBundle .message(" toolWindowId" ))
38
- }
41
+ object TabManager {
42
+ private val toolWindowsTabs = mutableMapOf<Project , CycodeContentTab >()
39
43
40
- private fun replaceToolWindowContent (project : Project , content : Content ) {
41
- val window = getCycodeToolWindow(project) ? : return
42
- window.contentManager.removeAllContents(true )
43
- window.contentManager.addContent(content)
44
- }
44
+ fun addTab (project : Project , tab : CycodeContentTab ) {
45
+ toolWindowsTabs[project] = tab
46
+ }
45
47
46
- private fun createToolWindowContent (project : Project , component : Component <CycodeService >): Content {
47
- return ContentFactory .SERVICE .getInstance()
48
- .createContent(component.getContent(cycode(project)), null , false )
49
- }
48
+ fun getTab (project : Project ): CycodeContentTab ? {
49
+ return toolWindowsTabs[project]
50
+ }
50
51
51
- private fun createLoadingTabOnly (project : Project ) {
52
- replaceToolWindowContent(project, createToolWindowContent(project, LoadingContentTab ()))
53
- }
52
+ fun removeTab (project : Project ) {
53
+ toolWindowsTabs.remove(project)
54
+ }
55
+ }
54
56
57
+ }
55
58
56
- private fun createAuthTabOnly (project : Project ) {
57
- replaceToolWindowContent(project, createToolWindowContent(project, AuthContentTab ()))
59
+ private fun replaceToolWindowRightPanel (project : Project , panel : JPanel ) {
60
+ val contentTab = CycodeToolWindowFactory .TabManager .getTab(project) ? : return
61
+ contentTab.updateContent(panel)
58
62
}
59
63
60
- private fun createScanTabOnly ( project : Project ) {
61
- replaceToolWindowContent(project, createToolWindowContent(project, ScanContentTab ()) )
64
+ private fun createToolWindowContent ( component : JPanel ): Content {
65
+ return ContentFactory . SERVICE .getInstance().createContent(component, null , false )
62
66
}
63
67
68
+
64
69
fun updateToolWindowState (project : Project ) {
65
70
val pluginState = pluginState()
71
+ val service = cycode(project)
72
+
66
73
ApplicationManager .getApplication().invokeLater {
67
74
WriteAction .run<Error > {
68
75
if (pluginState.cliAuthed) {
69
- createScanTabOnly (project)
76
+ replaceToolWindowRightPanel (project, ScanContentTab ().getContent(service) )
70
77
} else {
71
- createAuthTabOnly (project)
78
+ replaceToolWindowRightPanel (project, AuthContentTab ().getContent(service) )
72
79
}
73
80
}
74
81
}
0 commit comments