11package no.spk.fiskeoye.plugin
22
3+ import com.intellij.openapi.actionSystem.AnActionEvent
4+ import com.intellij.openapi.actionSystem.DefaultActionGroup
35import com.intellij.openapi.project.DumbAware
46import com.intellij.openapi.project.Project
57import com.intellij.openapi.wm.ToolWindow
68import com.intellij.openapi.wm.ToolWindowFactory
9+ import com.intellij.openapi.wm.ex.ToolWindowEx
710import com.intellij.ui.content.ContentFactory
11+ import com.intellij.ui.content.ContentManagerEvent
12+ import com.intellij.ui.content.ContentManagerListener
13+ import no.spk.fiskeoye.plugin.actions.FiskeoyeAction
14+ import no.spk.fiskeoye.plugin.icons.FiskeoyeIcons.Add
815import no.spk.fiskeoye.plugin.ui.FileContentPanel
916import no.spk.fiskeoye.plugin.ui.FilenamePanel
1017
1118internal class FiskeoyeToolWindowFactory : ToolWindowFactory , DumbAware {
1219
20+ private var fileContentCounter = 1
21+ private var filenameCounter = 1
22+
1323 override fun createToolWindowContent (project : Project , toolWindow : ToolWindow ) {
1424 val contentFactory: ContentFactory = ContentFactory .getInstance()
25+ val toolWindowEx = toolWindow as ToolWindowEx
1526
1627 val fileContentPanel = FileContentPanel ()
17- val fileContent = contentFactory.createContent(fileContentPanel, " File Content" , false )
18- toolWindow.contentManager.addContent(fileContent)
28+ val fileContent = contentFactory.createContent(fileContentPanel, " File Content" , false ).apply {
29+ isCloseable = false
30+ putUserData(CONTENT_TYPE_KEY , ContentType .FILE_CONTENT )
31+ }
32+ toolWindowEx.contentManager.addContent(fileContent)
1933
2034 val filenamePanel = FilenamePanel ()
21- val filename = contentFactory.createContent(filenamePanel, " Filename" , false )
22- toolWindow.contentManager.addContent(filename)
35+ val filename = contentFactory.createContent(filenamePanel, " File Name" , false ).apply {
36+ isCloseable = false
37+ putUserData(CONTENT_TYPE_KEY , ContentType .FILENAME )
38+ }
39+ toolWindowEx.contentManager.addContent(filename)
40+
41+ toolWindowEx.contentManager.addContentManagerListener(object : ContentManagerListener {
42+ override fun contentRemoved (event : ContentManagerEvent ) {
43+ val content = event.content
44+ when (content.getUserData(CONTENT_TYPE_KEY )) {
45+ ContentType .FILE_CONTENT -> if (fileContentCounter > 1 ) fileContentCounter--
46+ ContentType .FILENAME -> if (filenameCounter > 1 ) filenameCounter--
47+ else -> {}
48+ }
49+ }
50+ })
51+
52+ val newTabActionGroup = buildNewTabActionGroup(toolWindow, contentFactory)
53+ toolWindowEx.setTabActions(newTabActionGroup)
54+ }
55+
56+ private fun buildNewTabActionGroup (toolWindow : ToolWindow , contentFactory : ContentFactory ): DefaultActionGroup {
57+ val addFileContentAction = object : FiskeoyeAction (" File Content" ) {
58+ override fun actionPerformed (e : AnActionEvent ) {
59+ fileContentCounter++
60+ val newFileContent = contentFactory.createContent(
61+ FileContentPanel (),
62+ " File Content ($fileContentCounter )" ,
63+ false
64+ ).apply {
65+ isCloseable = true
66+ putUserData(CONTENT_TYPE_KEY , ContentType .FILE_CONTENT )
67+ }
68+ toolWindow.contentManager.addContent(newFileContent)
69+ toolWindow.contentManager.setSelectedContent(newFileContent, true )
70+ }
71+ }
72+
73+ val addFilenameAction = object : FiskeoyeAction (" File Name" ) {
74+ override fun actionPerformed (e : AnActionEvent ) {
75+ filenameCounter++
76+ val newFilename = contentFactory.createContent(
77+ FilenamePanel (),
78+ " File Name ($filenameCounter )" ,
79+ false
80+ ).apply {
81+ isCloseable = true
82+ putUserData(CONTENT_TYPE_KEY , ContentType .FILENAME )
83+ }
84+ toolWindow.contentManager.addContent(newFilename)
85+ toolWindow.contentManager.setSelectedContent(newFilename, true )
86+ }
87+ }
88+
89+ return DefaultActionGroup (" New Fiskeoye Tab" , true ).apply {
90+ templatePresentation.icon = Add
91+ add(addFileContentAction)
92+ add(addFilenameAction)
93+ }
2394 }
2495
25- }
96+ private enum class ContentType { FILE_CONTENT , FILENAME }
97+ private companion object {
98+ val CONTENT_TYPE_KEY = com.intellij.openapi.util.Key .create<ContentType >(" FISKEOYE_CONTENT_TYPE" )
99+ }
100+ }
0 commit comments